From fc54c7f65cb1d5bb0fd0af317af700d1dd8b94b7 Mon Sep 17 00:00:00 2001 From: chyyran Date: Wed, 18 Jan 2023 21:58:13 -0500 Subject: [PATCH] reflect: move compile_preset_passes from librashader-runtime into librashader-reflect --- Cargo.lock | 1 + librashader-reflect/Cargo.toml | 1 + librashader-reflect/src/reflect/mod.rs | 3 + librashader-reflect/src/reflect/presets.rs | 186 ++++++++++++++++++ librashader-runtime-d3d11/src/filter_chain.rs | 7 +- .../src/filter_chain/filter_impl.rs | 8 +- librashader-runtime-vk/src/filter_chain.rs | 7 +- librashader-runtime/src/lib.rs | 6 - librashader-runtime/src/reflect.rs | 79 -------- librashader-runtime/src/semantics.rs | 84 -------- librashader/src/lib.rs | 3 +- 11 files changed, 203 insertions(+), 182 deletions(-) create mode 100644 librashader-reflect/src/reflect/presets.rs delete mode 100644 librashader-runtime/src/reflect.rs delete mode 100644 librashader-runtime/src/semantics.rs diff --git a/Cargo.lock b/Cargo.lock index 4af7d8a..20d255e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -685,6 +685,7 @@ dependencies = [ "bitflags", "librashader-common", "librashader-preprocess", + "librashader-presets", "naga", "rustc-hash", "shaderc", diff --git a/librashader-reflect/Cargo.toml b/librashader-reflect/Cargo.toml index 2a341cc..8a22701 100644 --- a/librashader-reflect/Cargo.toml +++ b/librashader-reflect/Cargo.toml @@ -21,6 +21,7 @@ rustc-hash = "1.1.0" librashader-common = { path = "../librashader-common", version = "0.1.0-beta.7" } librashader-preprocess = { path = "../librashader-preprocess", version = "0.1.0-beta.7" } +librashader-presets = { path = "../librashader-presets", version = "0.1.0-beta.7" } naga = { version = "0.10.0", features = ["glsl-in", "spv-in", "spv-out", "glsl-out", "wgsl-out"], optional = true } diff --git a/librashader-reflect/src/reflect/mod.rs b/librashader-reflect/src/reflect/mod.rs index 10a8cb9..881f6f3 100644 --- a/librashader-reflect/src/reflect/mod.rs +++ b/librashader-reflect/src/reflect/mod.rs @@ -7,6 +7,9 @@ pub mod cross; /// Shader semantics and reflection information. pub mod semantics; +/// Reflection helpers for reflecting and compiling shaders as part of a shader preset. +pub mod presets; + mod helper; #[cfg(feature = "unstable-naga")] diff --git a/librashader-reflect/src/reflect/presets.rs b/librashader-reflect/src/reflect/presets.rs new file mode 100644 index 0000000..52e4b2c --- /dev/null +++ b/librashader-reflect/src/reflect/presets.rs @@ -0,0 +1,186 @@ +use librashader_preprocess::{PreprocessError, ShaderSource}; +use librashader_presets::{ShaderPassConfig, TextureConfig}; +use crate::back::targets::OutputTarget; +use crate::back::{CompilerBackend, FromCompilation}; +use crate::error::{ShaderCompileError, ShaderReflectError}; +use crate::front::ShaderCompilation; +use crate::reflect::semantics::{ + Semantic, ShaderSemantics, TextureSemantics, UniformSemantic, UniqueSemantics, +}; +use rustc_hash::FxHashMap; +use std::error::Error; + +/// Artifacts of a reflected and compiled shader pass. +pub type ShaderPassMeta = (ShaderPassConfig, ShaderSource, CompilerBackend); + +impl CompilePreset for T {} + +/// Trait for target shading languages that can compile output with +/// shader preset metdata. +pub trait CompilePreset: OutputTarget { + /// Compile passes of a shader preset given the applicable + /// shader output target, compilation type, and resulting error. + fn compile_preset_passes( + passes: Vec, + textures: &[TextureConfig], + )-> Result< + ( + Vec>::Output>>, + ShaderSemantics, + ), + E, + > + where + Self: Sized, + Self: FromCompilation, + C: ShaderCompilation, + E: Error, + E: From, + E: From, + E: From { + compile_preset_passes::(passes, textures) + } +} + + +/// Compile passes of a shader preset given the applicable +/// shader output target, compilation type, and resulting error. +pub(crate) fn compile_preset_passes( + passes: Vec, + textures: &[TextureConfig], +) -> Result< + ( + Vec>::Output>>, + ShaderSemantics, + ), + E, +> + where + T: OutputTarget, + T: FromCompilation, + C: ShaderCompilation, + E: Error, + E: From, + E: From, + E: From, +{ + let mut uniform_semantics: FxHashMap = Default::default(); + let mut texture_semantics: FxHashMap> = Default::default(); + + let passes = passes + .into_iter() + .map(|shader| { + let source: ShaderSource = ShaderSource::load(&shader.name)?; + + let compiled = C::compile(&source)?; + let reflect = T::from_compilation(compiled)?; + + for parameter in source.parameters.values() { + uniform_semantics.insert( + parameter.id.clone(), + UniformSemantic::Unique(Semantic { + semantics: UniqueSemantics::FloatParameter, + index: (), + }), + ); + } + Ok::<_, E>((shader, source, reflect)) + }) + .collect::)>, E>>()?; + + for details in &passes { + insert_pass_semantics( + &mut uniform_semantics, + &mut texture_semantics, + &details.0, + ) + } + insert_lut_semantics( + textures, + &mut uniform_semantics, + &mut texture_semantics, + ); + + let semantics = ShaderSemantics { + uniform_semantics, + texture_semantics, + }; + + Ok((passes, semantics)) +} + + +/// Insert the available semantics for the input pass config into the provided semantic maps. +fn insert_pass_semantics( + uniform_semantics: &mut FxHashMap, + texture_semantics: &mut FxHashMap>, + config: &ShaderPassConfig, +) { + let Some(alias) = &config.alias else { + return; + }; + + // Ignore empty aliases + if alias.trim().is_empty() { + return; + } + + let index = config.id as usize; + + // PassOutput + texture_semantics.insert( + alias.clone(), + Semantic { + semantics: TextureSemantics::PassOutput, + index, + }, + ); + uniform_semantics.insert( + format!("{alias}Size"), + UniformSemantic::Texture(Semantic { + semantics: TextureSemantics::PassOutput, + index, + }), + ); + + // PassFeedback + texture_semantics.insert( + format!("{alias}Feedback"), + Semantic { + semantics: TextureSemantics::PassFeedback, + index, + }, + ); + uniform_semantics.insert( + format!("{alias}FeedbackSize"), + UniformSemantic::Texture(Semantic { + semantics: TextureSemantics::PassFeedback, + index, + }), + ); +} + +/// Insert the available semantics for the input texture config into the provided semantic maps. +fn insert_lut_semantics( + textures: &[TextureConfig], + uniform_semantics: &mut FxHashMap, + texture_semantics: &mut FxHashMap>, +) { + for (index, texture) in textures.iter().enumerate() { + texture_semantics.insert( + texture.name.clone(), + Semantic { + semantics: TextureSemantics::User, + index, + }, + ); + + uniform_semantics.insert( + format!("{}Size", texture.name), + UniformSemantic::Texture(Semantic { + semantics: TextureSemantics::User, + index, + }), + ); + } +} diff --git a/librashader-runtime-d3d11/src/filter_chain.rs b/librashader-runtime-d3d11/src/filter_chain.rs index 6c4902a..455d53e 100644 --- a/librashader-runtime-d3d11/src/filter_chain.rs +++ b/librashader-runtime-d3d11/src/filter_chain.rs @@ -23,7 +23,6 @@ use crate::render_target::RenderTarget; use crate::samplers::SamplerSet; use crate::util::d3d11_compile_bound_shader; use crate::{error, util, D3D11OutputView}; -use librashader_runtime::reflect; use librashader_runtime::uniforms::UniformStorage; use windows::Win32::Graphics::Direct3D11::{ ID3D11Buffer, ID3D11Device, ID3D11DeviceContext, D3D11_BIND_CONSTANT_BUFFER, D3D11_BUFFER_DESC, @@ -31,13 +30,14 @@ use windows::Win32::Graphics::Direct3D11::{ D3D11_TEXTURE2D_DESC, D3D11_USAGE_DEFAULT, D3D11_USAGE_DYNAMIC, }; use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM; +use librashader_reflect::reflect::presets::CompilePreset; pub struct FilterMutable { pub(crate) passes_enabled: usize, pub(crate) parameters: FxHashMap, } -type ShaderPassMeta = reflect::ShaderPassMeta< +type ShaderPassMeta = librashader_reflect::reflect::presets::ShaderPassMeta< impl CompileShader, Context = CrossHlslContext> + ReflectShader, >; @@ -87,8 +87,7 @@ impl FilterChainD3D11 { preset: ShaderPreset, options: Option<&FilterChainOptionsD3D11>, ) -> error::Result { - let (passes, semantics) = reflect::compile_preset_passes::< - HLSL, + let (passes, semantics) = HLSL::compile_preset_passes::< GlslangCompilation, FilterChainError, >(preset.shaders, &preset.textures)?; diff --git a/librashader-runtime-gl/src/filter_chain/filter_impl.rs b/librashader-runtime-gl/src/filter_chain/filter_impl.rs index 0136323..f15378f 100644 --- a/librashader-runtime-gl/src/filter_chain/filter_impl.rs +++ b/librashader-runtime-gl/src/filter_chain/filter_impl.rs @@ -19,11 +19,12 @@ use librashader_reflect::front::shaderc::GlslangCompilation; use librashader_reflect::reflect::semantics::{ MemberOffset, ShaderSemantics, TextureSemantics, UniformBinding, UniformMeta, }; + use librashader_reflect::reflect::ReflectShader; -use librashader_runtime::reflect; use rustc_hash::FxHashMap; use spirv_cross::spirv::Decoration; use std::collections::VecDeque; +use librashader_reflect::reflect::presets::CompilePreset; pub(crate) struct FilterChainImpl { pub(crate) common: FilterCommon, @@ -79,7 +80,7 @@ impl FilterChainImpl { } } -type ShaderPassMeta = reflect::ShaderPassMeta< +type ShaderPassMeta = librashader_reflect::reflect::presets::ShaderPassMeta< impl CompileShader + ReflectShader, >; @@ -89,8 +90,7 @@ impl FilterChainImpl { preset: ShaderPreset, options: Option<&FilterChainOptionsGL>, ) -> error::Result { - let (passes, semantics) = reflect::compile_preset_passes::< - GLSL, + let (passes, semantics) = GLSL::compile_preset_passes::< GlslangCompilation, FilterChainError, >(preset.shaders, &preset.textures)?; diff --git a/librashader-runtime-vk/src/filter_chain.rs b/librashader-runtime-vk/src/filter_chain.rs index 5999c25..c8cf578 100644 --- a/librashader-runtime-vk/src/filter_chain.rs +++ b/librashader-runtime-vk/src/filter_chain.rs @@ -22,12 +22,12 @@ use librashader_reflect::front::shaderc::GlslangCompilation; use librashader_reflect::reflect::semantics::{ShaderSemantics, TextureSemantics, UniformBinding}; use librashader_reflect::reflect::ReflectShader; use librashader_runtime::image::{Image, UVDirection}; -use librashader_runtime::reflect; use librashader_runtime::uniforms::UniformStorage; use rustc_hash::FxHashMap; use std::collections::VecDeque; use std::path::Path; use std::sync::Arc; +use librashader_reflect::reflect::presets::CompilePreset; /// A Vulkan device and metadata that is required by the shader runtime. pub struct VulkanObjects { @@ -37,7 +37,7 @@ pub struct VulkanObjects { pipeline_cache: vk::PipelineCache, } -type ShaderPassMeta = reflect::ShaderPassMeta< +type ShaderPassMeta = librashader_reflect::reflect::presets::ShaderPassMeta< impl CompileShader, Context = ()> + ReflectShader, >; @@ -208,8 +208,7 @@ impl FilterChainVulkan { preset: ShaderPreset, options: Option<&FilterChainOptionsVulkan>, ) -> error::Result { - let (passes, semantics) = reflect::compile_preset_passes::< - SPIRV, + let (passes, semantics) = SPIRV::compile_preset_passes::< GlslangCompilation, FilterChainError, >(preset.shaders, &preset.textures)?; diff --git a/librashader-runtime/src/lib.rs b/librashader-runtime/src/lib.rs index fe16923..1e20d9c 100644 --- a/librashader-runtime/src/lib.rs +++ b/librashader-runtime/src/lib.rs @@ -9,9 +9,6 @@ /// Scaling helpers. pub mod scaling; -/// Semantics helpers. -pub mod semantics; - /// Uniform binding helpers. pub mod uniforms; @@ -26,6 +23,3 @@ pub mod ringbuffer; /// Generic implementation of semantics binding. pub mod binding; - -/// Generic helpers for loading shader passes into compiled shader targets and semantics. -pub mod reflect; diff --git a/librashader-runtime/src/reflect.rs b/librashader-runtime/src/reflect.rs deleted file mode 100644 index 4f2d65b..0000000 --- a/librashader-runtime/src/reflect.rs +++ /dev/null @@ -1,79 +0,0 @@ -use librashader_preprocess::{PreprocessError, ShaderSource}; -use librashader_presets::{ShaderPassConfig, TextureConfig}; -use librashader_reflect::back::targets::OutputTarget; -use librashader_reflect::back::{CompilerBackend, FromCompilation}; -use librashader_reflect::error::{ShaderCompileError, ShaderReflectError}; -use librashader_reflect::front::ShaderCompilation; -use librashader_reflect::reflect::semantics::{ - Semantic, ShaderSemantics, TextureSemantics, UniformSemantic, UniqueSemantics, -}; -use rustc_hash::FxHashMap; -use std::error::Error; - -pub type ShaderPassMeta = (ShaderPassConfig, ShaderSource, CompilerBackend); - -/// Compile passes of a shader preset given the applicable -/// shader output target, compilation type, and resulting error. -pub fn compile_preset_passes( - passes: Vec, - textures: &[TextureConfig], -) -> Result< - ( - Vec>::Output>>, - ShaderSemantics, - ), - E, -> -where - T: OutputTarget, - T: FromCompilation, - C: ShaderCompilation, - E: Error, - E: From, - E: From, - E: From, -{ - let mut uniform_semantics: FxHashMap = Default::default(); - let mut texture_semantics: FxHashMap> = Default::default(); - - let passes = passes - .into_iter() - .map(|shader| { - let source: ShaderSource = ShaderSource::load(&shader.name)?; - - let compiled = C::compile(&source)?; - let reflect = T::from_compilation(compiled)?; - - for parameter in source.parameters.values() { - uniform_semantics.insert( - parameter.id.clone(), - UniformSemantic::Unique(Semantic { - semantics: UniqueSemantics::FloatParameter, - index: (), - }), - ); - } - Ok::<_, E>((shader, source, reflect)) - }) - .collect::)>, E>>()?; - - for details in &passes { - crate::semantics::insert_pass_semantics( - &mut uniform_semantics, - &mut texture_semantics, - &details.0, - ) - } - crate::semantics::insert_lut_semantics( - textures, - &mut uniform_semantics, - &mut texture_semantics, - ); - - let semantics = ShaderSemantics { - uniform_semantics, - texture_semantics, - }; - - Ok((passes, semantics)) -} diff --git a/librashader-runtime/src/semantics.rs b/librashader-runtime/src/semantics.rs deleted file mode 100644 index c7a6f71..0000000 --- a/librashader-runtime/src/semantics.rs +++ /dev/null @@ -1,84 +0,0 @@ -use librashader_presets::{ShaderPassConfig, TextureConfig}; -use librashader_reflect::reflect::semantics::{Semantic, TextureSemantics, UniformSemantic}; -use rustc_hash::FxHashMap; - -/// A map for variable names and uniform semantics -pub type UniformSemanticsMap = FxHashMap; - -/// A map for sampler names and texture semantics. -pub type TextureSemanticsMap = FxHashMap>; - -/// Insert the available semantics for the input pass config into the provided semantic maps. -pub fn insert_pass_semantics( - uniform_semantics: &mut UniformSemanticsMap, - texture_semantics: &mut TextureSemanticsMap, - config: &ShaderPassConfig, -) { - let Some(alias) = &config.alias else { - return; - }; - - // Ignore empty aliases - if alias.trim().is_empty() { - return; - } - - let index = config.id as usize; - - // PassOutput - texture_semantics.insert( - alias.clone(), - Semantic { - semantics: TextureSemantics::PassOutput, - index, - }, - ); - uniform_semantics.insert( - format!("{alias}Size"), - UniformSemantic::Texture(Semantic { - semantics: TextureSemantics::PassOutput, - index, - }), - ); - - // PassFeedback - texture_semantics.insert( - format!("{alias}Feedback"), - Semantic { - semantics: TextureSemantics::PassFeedback, - index, - }, - ); - uniform_semantics.insert( - format!("{alias}FeedbackSize"), - UniformSemantic::Texture(Semantic { - semantics: TextureSemantics::PassFeedback, - index, - }), - ); -} - -/// Insert the available semantics for the input texture config into the provided semantic maps. -pub fn insert_lut_semantics( - textures: &[TextureConfig], - uniform_semantics: &mut UniformSemanticsMap, - texture_semantics: &mut TextureSemanticsMap, -) { - for (index, texture) in textures.iter().enumerate() { - texture_semantics.insert( - texture.name.clone(), - Semantic { - semantics: TextureSemantics::User, - index, - }, - ); - - uniform_semantics.insert( - format!("{}Size", texture.name), - UniformSemantic::Texture(Semantic { - semantics: TextureSemantics::User, - index, - }), - ); - } -} diff --git a/librashader/src/lib.rs b/librashader/src/lib.rs index b708321..68f3395 100644 --- a/librashader/src/lib.rs +++ b/librashader/src/lib.rs @@ -113,6 +113,8 @@ pub mod reflect { } pub use librashader_reflect::reflect::semantics::BindingMeta; + pub use librashader_reflect::reflect::presets; + #[doc(hidden)] #[cfg(feature = "internal")] /// Helper methods for runtimes. @@ -120,7 +122,6 @@ pub mod reflect { /// This is internal to librashader runtimes and is exempt from semantic versioning. pub mod helper { pub use librashader_runtime::image; - pub use librashader_runtime::reflect::compile_preset_passes; } }