diff --git a/librashader-cache/Cargo.toml b/librashader-cache/Cargo.toml index 18f3599..d9f1f13 100644 --- a/librashader-cache/Cargo.toml +++ b/librashader-cache/Cargo.toml @@ -12,7 +12,7 @@ description = "RetroArch shaders for all." [dependencies] serde = { version = "1.0" } -librashader-reflect = { path = "../librashader-reflect", version = "0.4.5", features = ["serialize"] } +librashader-reflect = { path = "../librashader-reflect", version = "0.4.5", features = ["serde"] } librashader-preprocess = { path = "../librashader-preprocess", version = "0.4.5" } platform-dirs = "0.3.0" blake3 = { version = "1.5.4" } diff --git a/librashader-reflect/Cargo.toml b/librashader-reflect/Cargo.toml index c9a70e4..bd1415e 100644 --- a/librashader-reflect/Cargo.toml +++ b/librashader-reflect/Cargo.toml @@ -36,12 +36,13 @@ version = "0.4.7" optional = true [features] -default = ["cross", "naga", "serialize", "wgsl", "msl"] +default = ["cross", "naga", "wgsl", "msl"] dxil = ["spirv-cross2/hlsl", "dep:spirv-to-dxil"] wgsl = ["cross", "naga/wgsl-out", "dep:spirv", "dep:rspirv"] cross = [ "dep:spirv-cross2", "spirv-cross2/glsl", "spirv-cross2/hlsl", "spirv-cross2/msl" ] naga = [ "dep:rspirv", "dep:spirv", "naga/spv-in", "naga/spv-out", "naga/wgsl-out", "naga/msl-out" ] -serialize = [ "dep:serde" ] +serde = ["dep:serde", "serde/derive", "librashader-common/serde", "bitflags/serde"] + msl = [ "spirv-cross2/msl", "naga/msl-out" ] stable = [] diff --git a/librashader-reflect/src/front/mod.rs b/librashader-reflect/src/front/mod.rs index a1a122b..96d816e 100644 --- a/librashader-reflect/src/front/mod.rs +++ b/librashader-reflect/src/front/mod.rs @@ -1,6 +1,5 @@ use crate::error::ShaderCompileError; use librashader_preprocess::ShaderSource; -use serde::{Deserialize, Serialize}; pub(crate) mod spirv_passes; mod glslang; @@ -25,8 +24,8 @@ impl ShaderReflectObject for SpirvCompilation { } /// A reflectable shader compilation via glslang. -#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))] #[derive(Debug, Clone)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct SpirvCompilation { pub(crate) vertex: Vec, pub(crate) fragment: Vec, diff --git a/librashader-reflect/src/reflect/presets.rs b/librashader-reflect/src/reflect/presets.rs index de57ca9..65b523a 100644 --- a/librashader-reflect/src/reflect/presets.rs +++ b/librashader-reflect/src/reflect/presets.rs @@ -7,7 +7,7 @@ use crate::reflect::semantics::{ }; use librashader_common::map::{FastHashMap, ShortString}; use librashader_preprocess::{PreprocessError, ShaderSource}; -use librashader_presets::{ShaderPassConfig, TextureConfig}; +use librashader_presets::{ShaderPassConfig, ShaderPreset, TextureConfig}; /// Artifacts of a reflected and compiled shader pass. /// @@ -214,3 +214,60 @@ fn insert_lut_semantics( ); } } + +impl ShaderSemantics { + /// Create pass semantics for a single pass in the given shader preset. + /// + /// This is meant as a convenience function for reflection use only. + pub fn create_pass_semantics(preset: &ShaderPreset, index: usize) -> Result + where + E: From, + E: From, + { + let mut uniform_semantics: FastHashMap = Default::default(); + let mut texture_semantics: FastHashMap> = + Default::default(); + + let config = preset + .shaders + .get(index) + .ok_or_else(|| PreprocessError::InvalidStage)?; + + let source = ShaderSource::load(&config.name)?; + + for parameter in source.parameters.values() { + uniform_semantics.insert( + parameter.id.clone(), + UniformSemantic::Unique(Semantic { + semantics: UniqueSemantics::FloatParameter, + index: (), + }), + ); + } + + insert_pass_semantics( + &mut uniform_semantics, + &mut texture_semantics, + config.alias.as_ref(), + config.id as usize, + ); + insert_pass_semantics( + &mut uniform_semantics, + &mut texture_semantics, + source.name.as_ref(), + config.id as usize, + ); + insert_lut_semantics( + preset.textures.as_slice(), + &mut uniform_semantics, + &mut texture_semantics, + ); + + Ok(ShaderSemantics { + uniform_semantics, + texture_semantics, + }) + } + +} + diff --git a/librashader-reflect/src/reflect/semantics.rs b/librashader-reflect/src/reflect/semantics.rs index b4dbbdc..af77fa9 100644 --- a/librashader-reflect/src/reflect/semantics.rs +++ b/librashader-reflect/src/reflect/semantics.rs @@ -9,6 +9,7 @@ pub const MAX_PUSH_BUFFER_SIZE: u32 = 128; /// The type of a uniform. #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum UniformType { /// A matrix of 4x4 floats (`mat4`). Mat4, @@ -26,6 +27,7 @@ pub enum UniformType { /// that are always available. #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Hash)] #[repr(i32)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum UniqueSemantics { // mat4, MVP /// The Model View Projection matrix for the frame. @@ -84,6 +86,7 @@ impl UniqueSemantics { /// Texture semantics are used to relate both texture samplers and `*Size` uniforms. #[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Copy, Clone, Hash)] #[repr(i32)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum TextureSemantics { /// The original input of the filter chain. Original = 0, @@ -163,6 +166,7 @@ pub(crate) trait ValidateTypeSemantics { /// A unit of unique or indexed semantic. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Semantic { /// The semantics of this unit. pub semantics: T, @@ -173,6 +177,8 @@ pub struct Semantic { bitflags! { /// The pipeline stage for which a uniform is bound. #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] + #[cfg_attr(feature = "serde", serde(transparent))] pub struct BindingStage: u8 { const NONE = 0b00000000; const VERTEX = 0b00000001; @@ -181,7 +187,8 @@ bitflags! { } /// Reflection information for the Uniform Buffer or Push Constant Block -#[derive(Debug)] +#[derive(Clone, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct BufferReflection { /// The binding point for this buffer, if applicable pub binding: T, @@ -195,6 +202,7 @@ pub struct BufferReflection { /// /// A uniform can be bound to both the UBO, or as a Push Constant. #[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct MemberOffset { /// The offset of the uniform member within the UBO. pub ubo: Option, @@ -203,6 +211,7 @@ pub struct MemberOffset { } #[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] /// The block where a uniform member is located. pub enum UniformMemberBlock { /// The offset is for a UBO. @@ -247,7 +256,8 @@ impl MemberOffset { } /// Reflection information about a non-texture related uniform variable. -#[derive(Debug)] +#[derive(Clone, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct VariableMeta { /// The offset of this variable uniform. pub offset: MemberOffset, @@ -258,7 +268,8 @@ pub struct VariableMeta { } /// Reflection information about a texture size uniform variable. -#[derive(Debug)] +#[derive(Clone, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct TextureSizeMeta { // this might bite us in the back because retroarch keeps separate UBO/push offsets.. /// The offset of this size uniform. @@ -270,14 +281,16 @@ pub struct TextureSizeMeta { } /// Reflection information about texture samplers. -#[derive(Debug)] +#[derive(Clone, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct TextureBinding { /// The binding index of the texture. pub binding: u32, } /// Reflection information about a shader. -#[derive(Debug)] +#[derive(Clone, Debug)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ShaderReflection { /// Reflection information about the UBO for this shader. pub ubo: Option>, @@ -436,6 +449,7 @@ impl UniqueSemanticMap for FastHashMap { /// Semantic assignment of a shader uniform to filter chain semantics. #[derive(Debug, Clone)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum UniformSemantic { /// A unique semantic. Unique(Semantic), @@ -445,6 +459,7 @@ pub enum UniformSemantic { /// The runtime provided maps of uniform and texture variables to filter chain semantics. #[derive(Debug, Clone)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ShaderSemantics { /// A map of uniform names to filter chain semantics. pub uniform_semantics: FastHashMap, @@ -457,6 +472,7 @@ pub struct ShaderSemantics { /// Used in combination with [`MemberOffset`] to keep track /// of semantics at each frame pass. #[derive(Debug, Clone, Eq, Hash, PartialEq)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum UniformBinding { /// A user parameter (`float`) binding. Parameter(ShortString), @@ -479,7 +495,8 @@ impl From> for UniformBinding { } /// Reflection metadata about the various bindings for this shader. -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct BindingMeta { /// A map of parameter names to uniform binding metadata. pub parameter_meta: FastHashMap,