reflect: move compile_preset_passes from librashader-runtime into librashader-reflect

This commit is contained in:
chyyran 2023-01-18 21:58:13 -05:00
parent 4e51704e35
commit fc54c7f65c
11 changed files with 203 additions and 182 deletions

1
Cargo.lock generated
View file

@ -685,6 +685,7 @@ dependencies = [
"bitflags", "bitflags",
"librashader-common", "librashader-common",
"librashader-preprocess", "librashader-preprocess",
"librashader-presets",
"naga", "naga",
"rustc-hash", "rustc-hash",
"shaderc", "shaderc",

View file

@ -21,6 +21,7 @@ rustc-hash = "1.1.0"
librashader-common = { path = "../librashader-common", version = "0.1.0-beta.7" } librashader-common = { path = "../librashader-common", version = "0.1.0-beta.7" }
librashader-preprocess = { path = "../librashader-preprocess", 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 } naga = { version = "0.10.0", features = ["glsl-in", "spv-in", "spv-out", "glsl-out", "wgsl-out"], optional = true }

View file

@ -7,6 +7,9 @@ pub mod cross;
/// Shader semantics and reflection information. /// Shader semantics and reflection information.
pub mod semantics; pub mod semantics;
/// Reflection helpers for reflecting and compiling shaders as part of a shader preset.
pub mod presets;
mod helper; mod helper;
#[cfg(feature = "unstable-naga")] #[cfg(feature = "unstable-naga")]

View file

@ -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<T> = (ShaderPassConfig, ShaderSource, CompilerBackend<T>);
impl<T: OutputTarget> 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<C, E>(
passes: Vec<ShaderPassConfig>,
textures: &[TextureConfig],
)-> Result<
(
Vec<ShaderPassMeta<<Self as FromCompilation<C>>::Output>>,
ShaderSemantics,
),
E,
>
where
Self: Sized,
Self: FromCompilation<C>,
C: ShaderCompilation,
E: Error,
E: From<PreprocessError>,
E: From<ShaderReflectError>,
E: From<ShaderCompileError> {
compile_preset_passes::<Self, C, E>(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<T, C, E>(
passes: Vec<ShaderPassConfig>,
textures: &[TextureConfig],
) -> Result<
(
Vec<ShaderPassMeta<<T as FromCompilation<C>>::Output>>,
ShaderSemantics,
),
E,
>
where
T: OutputTarget,
T: FromCompilation<C>,
C: ShaderCompilation,
E: Error,
E: From<PreprocessError>,
E: From<ShaderReflectError>,
E: From<ShaderCompileError>,
{
let mut uniform_semantics: FxHashMap<String, UniformSemantic> = Default::default();
let mut texture_semantics: FxHashMap<String, Semantic<TextureSemantics>> = 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::<Result<Vec<(ShaderPassConfig, ShaderSource, CompilerBackend<_>)>, 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<String, UniformSemantic>,
texture_semantics: &mut FxHashMap<String, Semantic<TextureSemantics>>,
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<String, UniformSemantic>,
texture_semantics: &mut FxHashMap<String, Semantic<TextureSemantics>>,
) {
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,
}),
);
}
}

View file

@ -23,7 +23,6 @@ use crate::render_target::RenderTarget;
use crate::samplers::SamplerSet; use crate::samplers::SamplerSet;
use crate::util::d3d11_compile_bound_shader; use crate::util::d3d11_compile_bound_shader;
use crate::{error, util, D3D11OutputView}; use crate::{error, util, D3D11OutputView};
use librashader_runtime::reflect;
use librashader_runtime::uniforms::UniformStorage; use librashader_runtime::uniforms::UniformStorage;
use windows::Win32::Graphics::Direct3D11::{ use windows::Win32::Graphics::Direct3D11::{
ID3D11Buffer, ID3D11Device, ID3D11DeviceContext, D3D11_BIND_CONSTANT_BUFFER, D3D11_BUFFER_DESC, 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, D3D11_TEXTURE2D_DESC, D3D11_USAGE_DEFAULT, D3D11_USAGE_DYNAMIC,
}; };
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM; use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R8G8B8A8_UNORM;
use librashader_reflect::reflect::presets::CompilePreset;
pub struct FilterMutable { pub struct FilterMutable {
pub(crate) passes_enabled: usize, pub(crate) passes_enabled: usize,
pub(crate) parameters: FxHashMap<String, f32>, pub(crate) parameters: FxHashMap<String, f32>,
} }
type ShaderPassMeta = reflect::ShaderPassMeta< type ShaderPassMeta = librashader_reflect::reflect::presets::ShaderPassMeta<
impl CompileShader<HLSL, Options = Option<()>, Context = CrossHlslContext> + ReflectShader, impl CompileShader<HLSL, Options = Option<()>, Context = CrossHlslContext> + ReflectShader,
>; >;
@ -87,8 +87,7 @@ impl FilterChainD3D11 {
preset: ShaderPreset, preset: ShaderPreset,
options: Option<&FilterChainOptionsD3D11>, options: Option<&FilterChainOptionsD3D11>,
) -> error::Result<FilterChainD3D11> { ) -> error::Result<FilterChainD3D11> {
let (passes, semantics) = reflect::compile_preset_passes::< let (passes, semantics) = HLSL::compile_preset_passes::<
HLSL,
GlslangCompilation, GlslangCompilation,
FilterChainError, FilterChainError,
>(preset.shaders, &preset.textures)?; >(preset.shaders, &preset.textures)?;

View file

@ -19,11 +19,12 @@ use librashader_reflect::front::shaderc::GlslangCompilation;
use librashader_reflect::reflect::semantics::{ use librashader_reflect::reflect::semantics::{
MemberOffset, ShaderSemantics, TextureSemantics, UniformBinding, UniformMeta, MemberOffset, ShaderSemantics, TextureSemantics, UniformBinding, UniformMeta,
}; };
use librashader_reflect::reflect::ReflectShader; use librashader_reflect::reflect::ReflectShader;
use librashader_runtime::reflect;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use spirv_cross::spirv::Decoration; use spirv_cross::spirv::Decoration;
use std::collections::VecDeque; use std::collections::VecDeque;
use librashader_reflect::reflect::presets::CompilePreset;
pub(crate) struct FilterChainImpl<T: GLInterface> { pub(crate) struct FilterChainImpl<T: GLInterface> {
pub(crate) common: FilterCommon, pub(crate) common: FilterCommon,
@ -79,7 +80,7 @@ impl<T: GLInterface> FilterChainImpl<T> {
} }
} }
type ShaderPassMeta = reflect::ShaderPassMeta< type ShaderPassMeta = librashader_reflect::reflect::presets::ShaderPassMeta<
impl CompileShader<GLSL, Options = GlslVersion, Context = CrossGlslContext> + ReflectShader, impl CompileShader<GLSL, Options = GlslVersion, Context = CrossGlslContext> + ReflectShader,
>; >;
@ -89,8 +90,7 @@ impl<T: GLInterface> FilterChainImpl<T> {
preset: ShaderPreset, preset: ShaderPreset,
options: Option<&FilterChainOptionsGL>, options: Option<&FilterChainOptionsGL>,
) -> error::Result<Self> { ) -> error::Result<Self> {
let (passes, semantics) = reflect::compile_preset_passes::< let (passes, semantics) = GLSL::compile_preset_passes::<
GLSL,
GlslangCompilation, GlslangCompilation,
FilterChainError, FilterChainError,
>(preset.shaders, &preset.textures)?; >(preset.shaders, &preset.textures)?;

View file

@ -22,12 +22,12 @@ use librashader_reflect::front::shaderc::GlslangCompilation;
use librashader_reflect::reflect::semantics::{ShaderSemantics, TextureSemantics, UniformBinding}; use librashader_reflect::reflect::semantics::{ShaderSemantics, TextureSemantics, UniformBinding};
use librashader_reflect::reflect::ReflectShader; use librashader_reflect::reflect::ReflectShader;
use librashader_runtime::image::{Image, UVDirection}; use librashader_runtime::image::{Image, UVDirection};
use librashader_runtime::reflect;
use librashader_runtime::uniforms::UniformStorage; use librashader_runtime::uniforms::UniformStorage;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use librashader_reflect::reflect::presets::CompilePreset;
/// A Vulkan device and metadata that is required by the shader runtime. /// A Vulkan device and metadata that is required by the shader runtime.
pub struct VulkanObjects { pub struct VulkanObjects {
@ -37,7 +37,7 @@ pub struct VulkanObjects {
pipeline_cache: vk::PipelineCache, pipeline_cache: vk::PipelineCache,
} }
type ShaderPassMeta = reflect::ShaderPassMeta< type ShaderPassMeta = librashader_reflect::reflect::presets::ShaderPassMeta<
impl CompileShader<SPIRV, Options = Option<()>, Context = ()> + ReflectShader, impl CompileShader<SPIRV, Options = Option<()>, Context = ()> + ReflectShader,
>; >;
@ -208,8 +208,7 @@ impl FilterChainVulkan {
preset: ShaderPreset, preset: ShaderPreset,
options: Option<&FilterChainOptionsVulkan>, options: Option<&FilterChainOptionsVulkan>,
) -> error::Result<FilterChainVulkan> { ) -> error::Result<FilterChainVulkan> {
let (passes, semantics) = reflect::compile_preset_passes::< let (passes, semantics) = SPIRV::compile_preset_passes::<
SPIRV,
GlslangCompilation, GlslangCompilation,
FilterChainError, FilterChainError,
>(preset.shaders, &preset.textures)?; >(preset.shaders, &preset.textures)?;

View file

@ -9,9 +9,6 @@
/// Scaling helpers. /// Scaling helpers.
pub mod scaling; pub mod scaling;
/// Semantics helpers.
pub mod semantics;
/// Uniform binding helpers. /// Uniform binding helpers.
pub mod uniforms; pub mod uniforms;
@ -26,6 +23,3 @@ pub mod ringbuffer;
/// Generic implementation of semantics binding. /// Generic implementation of semantics binding.
pub mod binding; pub mod binding;
/// Generic helpers for loading shader passes into compiled shader targets and semantics.
pub mod reflect;

View file

@ -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<T> = (ShaderPassConfig, ShaderSource, CompilerBackend<T>);
/// Compile passes of a shader preset given the applicable
/// shader output target, compilation type, and resulting error.
pub fn compile_preset_passes<T, C, E>(
passes: Vec<ShaderPassConfig>,
textures: &[TextureConfig],
) -> Result<
(
Vec<ShaderPassMeta<<T as FromCompilation<C>>::Output>>,
ShaderSemantics,
),
E,
>
where
T: OutputTarget,
T: FromCompilation<C>,
C: ShaderCompilation,
E: Error,
E: From<PreprocessError>,
E: From<ShaderReflectError>,
E: From<ShaderCompileError>,
{
let mut uniform_semantics: FxHashMap<String, UniformSemantic> = Default::default();
let mut texture_semantics: FxHashMap<String, Semantic<TextureSemantics>> = 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::<Result<Vec<(ShaderPassConfig, ShaderSource, CompilerBackend<_>)>, 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))
}

View file

@ -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<String, UniformSemantic>;
/// A map for sampler names and texture semantics.
pub type TextureSemanticsMap = FxHashMap<String, Semantic<TextureSemantics>>;
/// 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,
}),
);
}
}

View file

@ -113,6 +113,8 @@ pub mod reflect {
} }
pub use librashader_reflect::reflect::semantics::BindingMeta; pub use librashader_reflect::reflect::semantics::BindingMeta;
pub use librashader_reflect::reflect::presets;
#[doc(hidden)] #[doc(hidden)]
#[cfg(feature = "internal")] #[cfg(feature = "internal")]
/// Helper methods for runtimes. /// Helper methods for runtimes.
@ -120,7 +122,6 @@ pub mod reflect {
/// This is internal to librashader runtimes and is exempt from semantic versioning. /// This is internal to librashader runtimes and is exempt from semantic versioning.
pub mod helper { pub mod helper {
pub use librashader_runtime::image; pub use librashader_runtime::image;
pub use librashader_runtime::reflect::compile_preset_passes;
} }
} }