From: Jacob Lifshay Date: Mon, 8 Oct 2018 09:25:34 +0000 (-0700) Subject: started adding shader compiler X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=adc7bca0aba2e4b7c10a78b72339ff07148e6d8f;p=kazan.git started adding shader compiler --- diff --git a/Cargo.toml b/Cargo.toml index 08e4079..e4c4905 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,8 @@ # Copyright 2018 Jacob Lifshay [workspace] members = [ + "shader-compiler", + "shader-compiler-llvm-7", "vulkan-driver", ] diff --git a/Dockerfile b/Dockerfile index 556a55e..c3509d3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 index 0000000..676d9d9 --- /dev/null +++ b/shader-compiler-llvm-7/Cargo.toml @@ -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 "] +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 index 0000000..2c24f81 --- /dev/null +++ b/shader-compiler-llvm-7/src/backend.rs @@ -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(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 index 0000000..70249dd --- /dev/null +++ b/shader-compiler-llvm-7/src/lib.rs @@ -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 index 0000000..25f1138 --- /dev/null +++ b/shader-compiler/Cargo.toml @@ -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 "] +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 index 0000000..446f3a0 --- /dev/null +++ b/shader-compiler/src/lib.rs @@ -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(shader_compiler_user: SCU) -> SCU::ReturnType; + } +} diff --git a/vulkan-driver/Cargo.toml b/vulkan-driver/Cargo.toml index a2749b8..b901326 100644 --- a/vulkan-driver/Cargo.toml +++ b/vulkan-driver/Cargo.toml @@ -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"]} diff --git a/vulkan-driver/src/lib.rs b/vulkan-driver/src/lib.rs index 4253861..722c3b1 100644 --- a/vulkan-driver/src/lib.rs +++ b/vulkan-driver/src/lib.rs @@ -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)]