started adding shader compiler
authorJacob Lifshay <programmerjake@gmail.com>
Mon, 8 Oct 2018 09:25:34 +0000 (02:25 -0700)
committerJacob Lifshay <programmerjake@gmail.com>
Mon, 8 Oct 2018 09:25:34 +0000 (02:25 -0700)
Cargo.toml
Dockerfile
shader-compiler-llvm-7/Cargo.toml [new file with mode: 0644]
shader-compiler-llvm-7/src/backend.rs [new file with mode: 0644]
shader-compiler-llvm-7/src/lib.rs [new file with mode: 0644]
shader-compiler/Cargo.toml [new file with mode: 0644]
shader-compiler/src/lib.rs [new file with mode: 0644]
vulkan-driver/Cargo.toml
vulkan-driver/src/lib.rs

index 08e4079fc01b86a5d33a8d139312eb9eb4db8172..e4c4905306f43811edc1fd5bc43270a4bc849cd8 100644 (file)
@@ -2,6 +2,8 @@
 # Copyright 2018 Jacob Lifshay
 [workspace]
 members = [
+    "shader-compiler",
+    "shader-compiler-llvm-7",
     "vulkan-driver",
 ]
 
index 556a55e27d0bfa00fbf02f585902859b49ceff90..c3509d3f724669d9f244bfaa47aaf7bee83ddacd 100644 (file)
@@ -9,6 +9,7 @@ RUN set -e; \
     apt-get install -y \
         clang-7 \
         libclang-7-dev \
+        llvm-7-dev \
         cmake \
         ninja-build \
         libgl1-mesa-dev \
@@ -25,11 +26,17 @@ RUN ./run-cts.sh --update-only
 COPY external/ external/
 COPY Cargo.toml Cargo.toml
 COPY vulkan-driver/Cargo.toml vulkan-driver/build.rs vulkan-driver/vulkan-wrapper.h vulkan-driver/
+COPY shader-compiler/Cargo.toml shader-compiler/
+COPY shader-compiler-llvm-7/Cargo.toml shader-compiler-llvm-7/
 RUN set -e; \
     mkdir -p vulkan-driver/src; \
+    mkdir -p shader-compiler/src; \
+    mkdir -p shader-compiler-llvm-7/src; \
     echo "// empty" > vulkan-driver/src/lib.rs; \
+    echo "// empty" > shader-compiler/src/lib.rs; \
+    echo "// empty" > shader-compiler-llvm-7/src/lib.rs; \
     cargo build; \
-    rm vulkan-driver/src/lib.rs
+    rm */src/lib.rs
 COPY . .
-RUN touch -c vulkan-driver/src/lib.rs && cargo build
+RUN touch -c */src/lib.rs && cargo build
 CMD ["./run-cts.sh", "--no-update"]
diff --git a/shader-compiler-llvm-7/Cargo.toml b/shader-compiler-llvm-7/Cargo.toml
new file mode 100644 (file)
index 0000000..676d9d9
--- /dev/null
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: LGPL-2.1-or-later
+# Copyright 2018 Jacob Lifshay
+[package]
+name = "shader-compiler-llvm-7"
+version = "0.1.0"
+authors = ["Jacob Lifshay <programmerjake@gmail.com>"]
+license = "LGPL-2.1-or-later"
+
+[lib]
+crate-type = ["rlib"]
+
+[dependencies]
+shader-compiler = {path = "../shader-compiler"}
+llvm-sys = "70"
diff --git a/shader-compiler-llvm-7/src/backend.rs b/shader-compiler-llvm-7/src/backend.rs
new file mode 100644 (file)
index 0000000..2c24f81
--- /dev/null
@@ -0,0 +1,67 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+// Copyright 2018 Jacob Lifshay
+use llvm_sys;
+use shader_compiler::backend::*;
+use std::ffi::CString;
+use std::os::raw::c_char;
+
+#[repr(transparent)]
+pub struct LLVM7Context(llvm_sys::prelude::LLVMContextRef);
+
+impl Drop for LLVM7Context {
+    fn drop(&mut self) {
+        unsafe {
+            llvm_sys::core::LLVMContextDispose(self.0);
+        }
+    }
+}
+
+unsafe impl Send for LLVM7Context {}
+
+impl<'a> Context<'a> for LLVM7Context {
+    type Module = LLVM7Module;
+    fn create_module(&self, name: &str) -> LLVM7Module {
+        let name = CString::new(name).unwrap();
+        unsafe {
+            LLVM7Module(llvm_sys::core::LLVMModuleCreateWithNameInContext(
+                name.as_ptr(),
+                self.0,
+            ))
+        }
+    }
+}
+
+#[repr(transparent)]
+pub struct LLVM7Module(llvm_sys::prelude::LLVMModuleRef);
+
+impl Drop for LLVM7Module {
+    fn drop(&mut self) {
+        unsafe {
+            llvm_sys::core::LLVMDisposeModule(self.0);
+        }
+    }
+}
+
+impl<'a> Module<'a> for LLVM7Module {
+    fn set_source_file_name(&mut self, source_file_name: &str) {
+        unsafe {
+            llvm_sys::core::LLVMSetSourceFileName(
+                self.0,
+                source_file_name.as_ptr() as *const c_char,
+                source_file_name.len(),
+            )
+        }
+    }
+}
+
+pub struct LLVM7ShaderCompiler;
+
+impl ShaderCompiler for LLVM7ShaderCompiler {
+    fn name() -> &'static str {
+        "LLVM 7"
+    }
+    fn run_with_user<SCU: ShaderCompilerUser>(shader_compiler_user: SCU) -> SCU::ReturnType {
+        let context = unsafe { LLVM7Context(llvm_sys::core::LLVMContextCreate()) };
+        shader_compiler_user.run_with_context(&context)
+    }
+}
diff --git a/shader-compiler-llvm-7/src/lib.rs b/shader-compiler-llvm-7/src/lib.rs
new file mode 100644 (file)
index 0000000..70249dd
--- /dev/null
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+// Copyright 2018 Jacob Lifshay
+extern crate llvm_sys;
+extern crate shader_compiler;
+
+mod backend;
+
+pub fn create_shader_compiler() -> backend::LLVM7ShaderCompiler {
+    backend::LLVM7ShaderCompiler
+}
diff --git a/shader-compiler/Cargo.toml b/shader-compiler/Cargo.toml
new file mode 100644 (file)
index 0000000..25f1138
--- /dev/null
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: LGPL-2.1-or-later
+# Copyright 2018 Jacob Lifshay
+[package]
+name = "shader-compiler"
+version = "0.1.0"
+authors = ["Jacob Lifshay <programmerjake@gmail.com>"]
+license = "LGPL-2.1-or-later"
+
+[lib]
+crate-type = ["rlib"]
+
+[dependencies]
diff --git a/shader-compiler/src/lib.rs b/shader-compiler/src/lib.rs
new file mode 100644 (file)
index 0000000..446f3a0
--- /dev/null
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+// Copyright 2018 Jacob Lifshay
+#![deny(missing_docs)]
+
+//! Shader Compiler for Kazan
+
+/// Shader Compiler Backend traits
+pub mod backend {
+    /// equivalent to LLVM's 'Module'
+    pub trait Module<'a> {
+        /// set's the source file name for this module
+        fn set_source_file_name(&mut self, source_file_name: &str);
+    }
+
+    /// instance of a compiler backend; equivalent to LLVM's `LLVMContext`
+    pub trait Context<'a> {
+        /// the `Module` type
+        type Module: Module<'a>;
+        /// create a new `Module`
+        fn create_module(&self, name: &str) -> Self::Module;
+    }
+
+    /// trait that the user of `ShaderCompiler` implements
+    pub trait ShaderCompilerUser {
+        /// the return type of `run_with_context`
+        type ReturnType;
+        /// the function that the user of `ShaderCompiler` implements
+        fn run_with_context<'a, C: Context<'a>>(self, context: &'a C) -> Self::ReturnType;
+    }
+
+    /// main shader compiler backend trait
+    pub trait ShaderCompiler: Send + Sync + 'static {
+        /// get shader compiler's name
+        fn name() -> &'static str;
+        /// run a passed-in function with a new compiler context.
+        /// this round-about method is used because generic associated types are not in stable Rust yet
+        fn run_with_user<SCU: ShaderCompilerUser>(shader_compiler_user: SCU) -> SCU::ReturnType;
+    }
+}
index a2749b82f63fe02613c472568e160317d137cefa..b9013264b4395036d875a6792ca57dc68ae790fa 100644 (file)
@@ -14,6 +14,8 @@ crate-type = ["cdylib"]
 enum-map = "0.4"
 uuid = {version = "0.7", features = ["v5"]}
 sys-info = "0.5"
+shader-compiler = {path = "../shader-compiler"}
+shader-compiler-llvm-7 = {path = "../shader-compiler-llvm-7"}
 
 [target.'cfg(unix)'.dependencies]
 xcb = {version = "0.8", features = ["shm"]}
index 4253861cb4fe0fe38cba9370fbaee9d1c5858fc2..722c3b1a1d41e67da5443471abb6ade281d09223 100644 (file)
@@ -6,6 +6,7 @@ extern crate enum_map;
 extern crate errno;
 #[cfg(unix)]
 extern crate libc;
+extern crate shader_compiler;
 extern crate sys_info;
 extern crate uuid;
 #[cfg(unix)]