diff --git a/Cargo.toml b/Cargo.toml index e2a51a6..5960493 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ + "librashader", "librashader-common", "librashader-presets", "librashader-preprocess", diff --git a/librashader-common/Cargo.toml b/librashader-common/Cargo.toml index cc03dc0..5aa54b8 100644 --- a/librashader-common/Cargo.toml +++ b/librashader-common/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = [ "opengl" ] +default = [] opengl = ["gl"] [dependencies] diff --git a/librashader-reflect/src/back/cross.rs b/librashader-reflect/src/back/cross.rs index cb5804c..15b7a7c 100644 --- a/librashader-reflect/src/back/cross.rs +++ b/librashader-reflect/src/back/cross.rs @@ -1,5 +1,5 @@ -use crate::back::targets::{CompilerBackend, FromCompilation, GLSL, HLSL}; -use crate::back::CompileShader; +use crate::back::targets::{GLSL, HLSL}; +use crate::back::{CompilerBackend, CompileShader, FromCompilation}; use crate::error::ShaderReflectError; use crate::front::shaderc::GlslangCompilation; use crate::reflect::cross::{CompiledAst, GlslReflect, HlslReflect}; diff --git a/librashader-reflect/src/back/mod.rs b/librashader-reflect/src/back/mod.rs index 7150cbd..b543532 100644 --- a/librashader-reflect/src/back/mod.rs +++ b/librashader-reflect/src/back/mod.rs @@ -2,8 +2,10 @@ pub mod cross; pub mod targets; use std::fmt::Debug; - -pub use targets::CompileShader; +use crate::back::targets::OutputTarget; +use crate::error::{ShaderCompileError, ShaderReflectError}; +use crate::reflect::{ReflectShader, ShaderReflection}; +use crate::reflect::semantics::ReflectSemantics; #[derive(Debug)] pub struct ShaderCompilerOutput { @@ -11,3 +13,59 @@ pub struct ShaderCompilerOutput { pub fragment: T, pub context: Context, } + +pub trait CompileShader { + type Options; + type Context; + + fn compile( + self, + options: Self::Options, + ) -> Result, ShaderCompileError>; +} + +impl CompileShader for CompilerBackend +where + T: CompileShader, + E: OutputTarget, +{ + type Options = T::Options; + type Context = T::Context; + + fn compile( + self, + options: Self::Options, + ) -> Result, ShaderCompileError> { + self.backend.compile(options) + } +} + +pub trait FromCompilation { + type Target: OutputTarget; + type Options; + type Context; + + fn from_compilation( + compile: T, + ) -> Result< + CompilerBackend + ReflectShader>, + ShaderReflectError, + >; +} + +pub struct CompilerBackend { + pub(crate) backend: T, +} + +impl ReflectShader for CompilerBackend +where + T: ReflectShader, +{ + fn reflect( + &mut self, + pass_number: usize, + semantics: &ReflectSemantics, + ) -> Result { + self.backend.reflect(pass_number, semantics) + } +} diff --git a/librashader-reflect/src/back/targets.rs b/librashader-reflect/src/back/targets.rs index 7ae9e7a..3b358ba 100644 --- a/librashader-reflect/src/back/targets.rs +++ b/librashader-reflect/src/back/targets.rs @@ -1,6 +1,7 @@ -use crate::back::ShaderCompilerOutput; +use crate::back::{CompileShader, ShaderCompilerOutput}; use crate::error::{ShaderCompileError, ShaderReflectError}; -use crate::reflect::{ReflectSemantics, ReflectShader, ShaderReflection}; +use crate::reflect::{ReflectShader, ShaderReflection}; +use crate::reflect::semantics::ReflectSemantics; pub trait OutputTarget { type Output; @@ -8,7 +9,7 @@ pub trait OutputTarget { pub struct GLSL; pub struct HLSL; -pub struct SpirV; +pub struct SPIRV; pub struct MSL; impl OutputTarget for GLSL { @@ -17,68 +18,13 @@ impl OutputTarget for GLSL { impl OutputTarget for HLSL { type Output = String; } -impl OutputTarget for SpirV { +impl OutputTarget for SPIRV { type Output = Vec; } -pub struct CompilerBackend { - pub(crate) backend: T, -} - -pub trait FromCompilation { - type Target: OutputTarget; - type Options; - type Context; - - fn from_compilation( - compile: T, - ) -> Result< - CompilerBackend + ReflectShader>, - ShaderReflectError, - >; -} - -pub trait CompileShader { - type Options; - type Context; - - fn compile( - self, - options: Self::Options, - ) -> Result, ShaderCompileError>; -} - -impl ReflectShader for CompilerBackend -where - T: ReflectShader, -{ - fn reflect( - &mut self, - pass_number: usize, - semantics: &ReflectSemantics, - ) -> Result { - self.backend.reflect(pass_number, semantics) - } -} - -impl CompileShader for CompilerBackend -where - T: CompileShader, - E: OutputTarget, -{ - type Options = T::Options; - type Context = T::Context; - - fn compile( - self, - options: Self::Options, - ) -> Result, ShaderCompileError> { - self.backend.compile(options) - } -} - mod test { - use crate::back::targets::{FromCompilation, GLSL}; + use crate::back::FromCompilation; + use crate::back::targets::GLSL; use crate::front::shaderc::GlslangCompilation; pub fn huh(value: GlslangCompilation) { let _x = GLSL::from_compilation(value).unwrap(); diff --git a/librashader-reflect/src/reflect/cross.rs b/librashader-reflect/src/reflect/cross.rs index 9cdde44..c6ebd50 100644 --- a/librashader-reflect/src/reflect/cross.rs +++ b/librashader-reflect/src/reflect/cross.rs @@ -1,17 +1,13 @@ use crate::error::{SemanticsErrorKind, ShaderCompileError, ShaderReflectError}; use crate::front::shaderc::GlslangCompilation; -use crate::reflect::semantics::{ - BindingStage, MemberOffset, PushReflection, ShaderReflection, TextureImage, TextureSemantics, - TextureSizeMeta, TypeInfo, UboReflection, ValidateTypeSemantics, VariableMeta, - VariableSemantics, MAX_BINDINGS_COUNT, MAX_PUSH_BUFFER_SIZE, -}; +use crate::reflect::semantics::{BindingStage, MAX_BINDINGS_COUNT, MAX_PUSH_BUFFER_SIZE, MemberOffset, PushReflection, ReflectSemantics, ShaderReflection, TextureImage, TextureSemanticMap, TextureSemantics, TextureSizeMeta, TypeInfo, UboReflection, ValidateTypeSemantics, VariableMeta, VariableSemanticMap, VariableSemantics}; use crate::reflect::{ - ReflectMeta, ReflectSemantics, ReflectShader, TextureSemanticMap, VariableSemanticMap, + ReflectMeta, ReflectShader, }; use spirv_cross::hlsl::ShaderModel; use spirv_cross::spirv::{Ast, Decoration, Module, Resource, ShaderResources, Type}; -use spirv_cross::{glsl, hlsl, ErrorCode}; +use spirv_cross::{ErrorCode, glsl, hlsl}; use crate::back::cross::GlslangGlslContext; use crate::back::targets::{GLSL, HLSL}; @@ -846,11 +842,11 @@ impl CompileShader for CrossReflect { #[cfg(test)] mod test { use crate::reflect::cross::CrossReflect; - use crate::reflect::{ReflectSemantics, ReflectShader, UniformSemantic}; + use crate::reflect::ReflectShader; use rustc_hash::FxHashMap; use crate::back::CompileShader; - use crate::reflect::semantics::{SemanticMap, VariableSemantics}; + use crate::reflect::semantics::{ReflectSemantics, SemanticMap, UniformSemantic, VariableSemantics}; use librashader_preprocess::ShaderSource; use spirv_cross::glsl; use spirv_cross::glsl::{CompilerOptions, Version}; diff --git a/librashader-reflect/src/reflect/mod.rs b/librashader-reflect/src/reflect/mod.rs index fef2dc5..5c8e1e8 100644 --- a/librashader-reflect/src/reflect/mod.rs +++ b/librashader-reflect/src/reflect/mod.rs @@ -4,6 +4,7 @@ use crate::reflect::semantics::{ }; use rustc_hash::FxHashMap; use std::str::FromStr; +use semantics::ReflectSemantics; pub mod cross; @@ -22,120 +23,6 @@ pub trait ReflectShader { ) -> Result; } -pub trait TextureSemanticMap { - fn get_texture_semantic(&self, name: &str) -> Option>; -} - -pub trait VariableSemanticMap { - fn get_variable_semantic(&self, name: &str) -> Option>; -} - -impl VariableSemanticMap for FxHashMap { - fn get_variable_semantic(&self, name: &str) -> Option> { - match self.get(name) { - // existing uniforms in the semantic map have priority - None => match name { - "MVP" => Some(SemanticMap { - semantics: VariableSemantics::MVP, - index: (), - }), - "OutputSize" => Some(SemanticMap { - semantics: VariableSemantics::Output, - index: (), - }), - "FinalViewportSize" => Some(SemanticMap { - semantics: VariableSemantics::FinalViewport, - index: (), - }), - "FrameCount" => Some(SemanticMap { - semantics: VariableSemantics::FrameCount, - index: (), - }), - "FrameDirection" => Some(SemanticMap { - semantics: VariableSemantics::FrameDirection, - index: (), - }), - _ => None, - }, - Some(UniformSemantic::Variable(variable)) => Some(*variable), - Some(UniformSemantic::Texture(_)) => None, - } - } -} - -impl TextureSemanticMap for FxHashMap { - fn get_texture_semantic(&self, name: &str) -> Option> { - match self.get(name) { - None => { - if let Some(semantics) = TextureSemantics::TEXTURE_SEMANTICS - .iter() - .find(|f| name.starts_with(f.size_uniform_name())) - { - if semantics.is_array() { - let index = &name[semantics.size_uniform_name().len()..]; - let Ok(index) = usize::from_str(index) else { - return None; - }; - return Some(SemanticMap { - semantics: *semantics, - index, - }); - } else if name == semantics.size_uniform_name() { - return Some(SemanticMap { - semantics: *semantics, - index: 0, - }); - } - } - None - } - Some(UniformSemantic::Variable(_)) => None, - Some(UniformSemantic::Texture(texture)) => Some(*texture), - } - } -} - -impl TextureSemanticMap for FxHashMap> { - fn get_texture_semantic(&self, name: &str) -> Option> { - match self.get(name) { - None => { - if let Some(semantics) = TextureSemantics::TEXTURE_SEMANTICS - .iter() - .find(|f| name.starts_with(f.texture_name())) - { - if semantics.is_array() { - let index = &name[semantics.texture_name().len()..]; - let Ok(index) = usize::from_str(index) else {return None}; - return Some(SemanticMap { - semantics: *semantics, - index, - }); - } else if name == semantics.texture_name() { - return Some(SemanticMap { - semantics: *semantics, - index: 0, - }); - } - } - None - } - Some(texture) => Some(*texture), - } - } -} - -#[derive(Debug, Clone)] -pub enum UniformSemantic { - Variable(SemanticMap), - Texture(SemanticMap), -} - -#[derive(Debug, Clone)] -pub struct ReflectSemantics { - pub uniform_semantics: FxHashMap, - pub non_uniform_semantics: FxHashMap>, -} - #[derive(Debug, Default)] pub struct ReflectMeta { pub parameter_meta: FxHashMap, diff --git a/librashader-reflect/src/reflect/semantics.rs b/librashader-reflect/src/reflect/semantics.rs index dcc7205..2fd4a6e 100644 --- a/librashader-reflect/src/reflect/semantics.rs +++ b/librashader-reflect/src/reflect/semantics.rs @@ -1,5 +1,7 @@ use crate::reflect::ReflectMeta; use bitflags::bitflags; +use rustc_hash::FxHashMap; +use std::str::FromStr; pub const BASE_SEMANTICS_COUNT: usize = 5; pub const MAX_BINDINGS_COUNT: u32 = 16; @@ -208,3 +210,117 @@ impl UniformMeta for TextureSizeMeta { &self.id } } + +pub trait TextureSemanticMap { + fn get_texture_semantic(&self, name: &str) -> Option>; +} + +impl TextureSemanticMap for FxHashMap { + fn get_texture_semantic(&self, name: &str) -> Option> { + match self.get(name) { + None => { + if let Some(semantics) = TextureSemantics::TEXTURE_SEMANTICS + .iter() + .find(|f| name.starts_with(f.size_uniform_name())) + { + if semantics.is_array() { + let index = &name[semantics.size_uniform_name().len()..]; + let Ok(index) = usize::from_str(index) else { + return None; + }; + return Some(SemanticMap { + semantics: *semantics, + index, + }); + } else if name == semantics.size_uniform_name() { + return Some(SemanticMap { + semantics: *semantics, + index: 0, + }); + } + } + None + } + Some(UniformSemantic::Variable(_)) => None, + Some(UniformSemantic::Texture(texture)) => Some(*texture), + } + } +} + +impl TextureSemanticMap for FxHashMap> { + fn get_texture_semantic(&self, name: &str) -> Option> { + match self.get(name) { + None => { + if let Some(semantics) = TextureSemantics::TEXTURE_SEMANTICS + .iter() + .find(|f| name.starts_with(f.texture_name())) + { + if semantics.is_array() { + let index = &name[semantics.texture_name().len()..]; + let Ok(index) = usize::from_str(index) else {return None}; + return Some(SemanticMap { + semantics: *semantics, + index, + }); + } else if name == semantics.texture_name() { + return Some(SemanticMap { + semantics: *semantics, + index: 0, + }); + } + } + None + } + Some(texture) => Some(*texture), + } + } +} + +pub trait VariableSemanticMap { + fn get_variable_semantic(&self, name: &str) -> Option>; +} + +impl VariableSemanticMap for FxHashMap { + fn get_variable_semantic(&self, name: &str) -> Option> { + match self.get(name) { + // existing uniforms in the semantic map have priority + None => match name { + "MVP" => Some(SemanticMap { + semantics: VariableSemantics::MVP, + index: (), + }), + "OutputSize" => Some(SemanticMap { + semantics: VariableSemantics::Output, + index: (), + }), + "FinalViewportSize" => Some(SemanticMap { + semantics: VariableSemantics::FinalViewport, + index: (), + }), + "FrameCount" => Some(SemanticMap { + semantics: VariableSemantics::FrameCount, + index: (), + }), + "FrameDirection" => Some(SemanticMap { + semantics: VariableSemantics::FrameDirection, + index: (), + }), + _ => None, + }, + Some(UniformSemantic::Variable(variable)) => Some(*variable), + Some(UniformSemantic::Texture(_)) => None, + } + } +} + +#[derive(Debug, Clone)] +pub enum UniformSemantic { + Variable(SemanticMap), + Texture(SemanticMap), +} + +#[derive(Debug, Clone)] +pub struct ReflectSemantics { + pub uniform_semantics: FxHashMap, + pub non_uniform_semantics: FxHashMap>, +} diff --git a/librashader-runtime-dx11/src/lib.rs b/librashader-runtime-dx11/src/lib.rs index 2b8de57..b6e813f 100644 --- a/librashader-runtime-dx11/src/lib.rs +++ b/librashader-runtime-dx11/src/lib.rs @@ -1,14 +1,14 @@ use librashader_preprocess::ShaderSource; use librashader_presets::ShaderPassConfig; -use librashader_reflect::back::targets::{FromCompilation, HLSL}; -use librashader_reflect::back::CompileShader; +use librashader_reflect::back::targets::HLSL; +use librashader_reflect::back::{CompileShader, FromCompilation}; use rustc_hash::FxHashMap; use std::error::Error; use std::path::Path; use librashader_reflect::front::shaderc::GlslangCompilation; -use librashader_reflect::reflect::semantics::{SemanticMap, TextureSemantics, VariableSemantics}; -use librashader_reflect::reflect::{ReflectSemantics, ReflectShader, UniformSemantic}; +use librashader_reflect::reflect::semantics::{ReflectSemantics, SemanticMap, TextureSemantics, UniformSemantic, VariableSemantics}; +use librashader_reflect::reflect::ReflectShader; pub fn load_pass_semantics( uniform_semantics: &mut FxHashMap, diff --git a/librashader-runtime-gl/src/filter_chain.rs b/librashader-runtime-gl/src/filter_chain.rs index 9cf253b..b5368e5 100644 --- a/librashader-runtime-gl/src/filter_chain.rs +++ b/librashader-runtime-gl/src/filter_chain.rs @@ -10,18 +10,16 @@ use librashader_common::image::Image; use librashader_common::{FilterMode, Size, WrapMode}; use librashader_preprocess::ShaderSource; use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig}; -use librashader_reflect::back::cross::{GlVersion, GlslangGlslContext}; -use librashader_reflect::back::targets::{CompilerBackend, FromCompilation, GLSL}; -use librashader_reflect::back::CompileShader; -use librashader_reflect::reflect::semantics::{ - MemberOffset, SemanticMap, TextureSemantics, UniformMeta, VariableSemantics, -}; -use librashader_reflect::reflect::{ReflectSemantics, ReflectShader, UniformSemantic}; +use librashader_reflect::back::cross::{GlslangGlslContext, GlVersion}; +use librashader_reflect::back::targets::GLSL; +use librashader_reflect::reflect::semantics::{MemberOffset, ReflectSemantics, SemanticMap, TextureSemantics, UniformMeta, UniformSemantic, VariableSemantics}; +use librashader_reflect::reflect::ReflectShader; use rustc_hash::FxHashMap; use spirv_cross::spirv::Decoration; use std::collections::VecDeque; use std::error::Error; use std::path::Path; +use librashader_reflect::back::{CompilerBackend, CompileShader, FromCompilation}; use librashader_reflect::front::shaderc::GlslangCompilation; pub struct FilterChain { diff --git a/librashader/Cargo.toml b/librashader/Cargo.toml new file mode 100644 index 0000000..31f94ef --- /dev/null +++ b/librashader/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "librashader" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +librashader-common = { path = "../librashader-common" } +librashader-presets = { path = "../librashader-presets" } +librashader-preprocess = { path = "../librashader-preprocess" } +librashader-reflect = { path = "../librashader-reflect" } +librashader-runtime-dx11 = { path = "../librashader-runtime-dx11" } +librashader-runtime-gl = { path = "../librashader-runtime-gl" } + + +[features] +gl = [ "librashader-common/opengl" ] \ No newline at end of file diff --git a/librashader/src/lib.rs b/librashader/src/lib.rs new file mode 100644 index 0000000..608eca4 --- /dev/null +++ b/librashader/src/lib.rs @@ -0,0 +1,72 @@ + +pub mod presets { + pub use librashader_presets::*; +} + +pub mod preprocess { + pub use librashader_preprocess::*; +} + +pub mod reflect { + pub use librashader_reflect::error::*; + + pub use librashader_reflect::reflect::{ + ReflectMeta, ReflectShader, semantics, ShaderReflection + }; + + pub use librashader_reflect::front::shaderc::GlslangCompilation; + pub use librashader_reflect::back::{ + CompileShader, + FromCompilation, + ShaderCompilerOutput, + CompilerBackend, + targets::OutputTarget, + }; +} + +pub mod targets { + /// Shader compiler targets and runtime for OpenGL. + pub mod gl { + /// Shader compiler target for GLSL. + pub use librashader_reflect::back::targets::GLSL; + + /// Shader runtime for OpenGL. + pub mod runtime { + pub use librashader_runtime_gl::*; + } + } + + /// Shader compiler targets and runtime for DirectX. + pub mod dx { + /// Shader compiler target for HLSL. + pub use librashader_reflect::back::targets::HLSL; + + /// Shader runtime for DirectX. + pub mod runtime { + + /// Shader runtime for DirectX 11. + pub mod dx11 { + pub use librashader_runtime_dx11::*; + } + + /// Shader runtime for DirectX 12. + pub mod dx12 { + pub use librashader_runtime_dx11::*; + } + } + } + + /// Shader compiler targets and runtime for Vulkan. + pub mod vk { + /// Shader compiler target for SPIR-V. + pub use librashader_reflect::back::targets::SPIRV; + } +} + +pub use librashader_common::{ + FilterMode, + ShaderFormat, + Size, + WrapMode +}; +