preprocess: store parameters in FxHashMap

This commit is contained in:
chyyran 2023-01-15 14:06:38 -05:00
parent 3b89c065fa
commit 40e4ce908f
12 changed files with 26 additions and 25 deletions

1
Cargo.lock generated
View file

@ -814,6 +814,7 @@ version = "0.1.0-beta.6"
dependencies = [ dependencies = [
"librashader-common", "librashader-common",
"nom", "nom",
"rustc-hash",
"thiserror", "thiserror",
] ]

View file

@ -194,10 +194,6 @@ impl LibrashaderError {
None None
} }
pub(crate) fn panic(panic: Box<dyn Any + Send + 'static>) -> libra_error_t {
LibrashaderError::UnknownError(panic).export()
}
pub(crate) fn export(self) -> libra_error_t { pub(crate) fn export(self) -> libra_error_t {
NonNull::new(Box::into_raw(Box::new(self))) NonNull::new(Box::into_raw(Box::new(self)))
} }

View file

@ -141,7 +141,7 @@ extern_fn! {
} }
extern_fn! { extern_fn! {
/// Get a list of runtime parameter names. /// Get a list of runtime parameters.
/// ///
/// ## Safety /// ## Safety
/// - `preset` must be null or a valid and aligned pointer to a shader preset. /// - `preset` must be null or a valid and aligned pointer to a shader preset.

View file

@ -15,6 +15,7 @@ description = "RetroArch shaders for all."
thiserror = "1.0.37" thiserror = "1.0.37"
nom = "7.1.1" nom = "7.1.1"
librashader-common = { path = "../librashader-common", version = "0.1.0-beta.6" } librashader-common = { path = "../librashader-common", version = "0.1.0-beta.6" }
rustc-hash = "1.1.0"
[features] [features]
default = [ "line_directives" ] default = [ "line_directives" ]

View file

@ -7,6 +7,7 @@ use crate::include::read_source;
pub use error::*; pub use error::*;
use librashader_common::ImageFormat; use librashader_common::ImageFormat;
use std::path::Path; use std::path::Path;
use rustc_hash::FxHashMap;
/// The source file for a single shader pass. /// The source file for a single shader pass.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -21,7 +22,7 @@ pub struct ShaderSource {
pub name: Option<String>, pub name: Option<String>,
/// The list of shader parameters found in the shader source. /// The list of shader parameters found in the shader source.
pub parameters: Vec<ShaderParameter>, pub parameters: FxHashMap<String, ShaderParameter>,
/// The image format the shader expects. /// The image format the shader expects.
pub format: ImageFormat, pub format: ImageFormat,
@ -71,12 +72,14 @@ pub(crate) fn load_shader_source(path: impl AsRef<Path>) -> Result<ShaderSource,
let source = read_source(path)?; let source = read_source(path)?;
let meta = pragma::parse_pragma_meta(&source)?; let meta = pragma::parse_pragma_meta(&source)?;
let text = stage::process_stages(&source)?; let text = stage::process_stages(&source)?;
let parameters = FxHashMap::from_iter(meta.parameters.into_iter()
.map(|p| (p.id.clone(), p)));
Ok(ShaderSource { Ok(ShaderSource {
vertex: text.vertex, vertex: text.vertex,
fragment: text.fragment, fragment: text.fragment,
name: meta.name, name: meta.name,
parameters: meta.parameters, parameters,
format: meta.format, format: meta.format,
}) })
} }

View file

@ -224,16 +224,16 @@ where
} }
struct UboData { struct UboData {
id: u32, // id: u32,
descriptor_set: u32, // descriptor_set: u32,
binding: u32, binding: u32,
size: u32, size: u32,
} }
struct TextureData<'a> { struct TextureData<'a> {
id: u32, // id: u32,
// descriptor_set: u32,
name: &'a str, name: &'a str,
descriptor_set: u32,
binding: u32, binding: u32,
} }
@ -276,9 +276,9 @@ where
let size = ast.get_declared_struct_size(ubo.base_type_id)?; let size = ast.get_declared_struct_size(ubo.base_type_id)?;
Ok(UboData { Ok(UboData {
descriptor_set, // descriptor_set,
// id: ubo.id,
binding, binding,
id: ubo.id,
size, size,
}) })
} }
@ -538,9 +538,9 @@ where
} }
Ok(TextureData { Ok(TextureData {
id: texture.id, // id: texture.id,
// descriptor_set,
name: &texture.name, name: &texture.name,
descriptor_set,
binding, binding,
}) })
} }

View file

@ -426,7 +426,7 @@ impl FilterChainD3D11 {
let spirv = GlslangCompilation::compile(&source)?; let spirv = GlslangCompilation::compile(&source)?;
let reflect = HLSL::from_compilation(spirv)?; let reflect = HLSL::from_compilation(spirv)?;
for parameter in source.parameters.iter() { for parameter in source.parameters.values() {
uniform_semantics.insert( uniform_semantics.insert(
parameter.id.clone(), parameter.id.clone(),
UniformSemantic::Unique(Semantic { UniformSemantic::Unique(Semantic {

View file

@ -173,7 +173,7 @@ impl<T: GLInterface> FilterChainImpl<T> {
let spirv = GlslangCompilation::compile(&source)?; let spirv = GlslangCompilation::compile(&source)?;
let reflect = GLSL::from_compilation(spirv)?; let reflect = GLSL::from_compilation(spirv)?;
for parameter in source.parameters.iter() { for parameter in source.parameters.values() {
uniform_semantics.insert( uniform_semantics.insert(
parameter.id.clone(), parameter.id.clone(),
UniformSemantic::Unique(Semantic { UniformSemantic::Unique(Semantic {

View file

@ -297,7 +297,7 @@ impl FilterChainVulkan {
let spirv = GlslangCompilation::compile(&source)?; let spirv = GlslangCompilation::compile(&source)?;
let reflect = SPIRV::from_compilation(spirv)?; let reflect = SPIRV::from_compilation(spirv)?;
for parameter in source.parameters.iter() { for parameter in source.parameters.values() {
uniform_semantics.insert( uniform_semantics.insert(
parameter.id.clone(), parameter.id.clone(),
UniformSemantic::Unique(Semantic { UniformSemantic::Unique(Semantic {

View file

@ -99,7 +99,7 @@ where
pass_feedback: impl Iterator<Item = Option<impl AsRef<Self::InputTexture>>>, pass_feedback: impl Iterator<Item = Option<impl AsRef<Self::InputTexture>>>,
original_history: impl Iterator<Item = Option<impl AsRef<Self::InputTexture>>>, original_history: impl Iterator<Item = Option<impl AsRef<Self::InputTexture>>>,
lookup_textures: impl Iterator<Item = (usize, impl AsRef<Self::InputTexture>)>, lookup_textures: impl Iterator<Item = (usize, impl AsRef<Self::InputTexture>)>,
parameter_defaults: &[ShaderParameter], parameter_defaults: &HashMap<String, ShaderParameter, impl BuildHasher>,
runtime_parameters: &HashMap<String, f32, impl BuildHasher>, runtime_parameters: &HashMap<String, f32, impl BuildHasher>,
) { ) {
// Bind MVP // Bind MVP
@ -238,13 +238,12 @@ where
{ {
let id = id.as_str(); let id = id.as_str();
let default = parameter_defaults let default = parameter_defaults.get(id)
.iter()
.find(|&p| p.id == id)
.map(|f| f.initial) .map(|f| f.initial)
.unwrap_or(0f32); .unwrap_or(0f32);
let value = *runtime_parameters.get(id).unwrap_or(&default); let value = *runtime_parameters.get(id)
.unwrap_or(&default);
uniform_storage.bind_scalar(offset.offset(), value, offset.context()); uniform_storage.bind_scalar(offset.offset(), value, offset.context());
} }

View file

@ -1,4 +1,3 @@
use librashader_common::Size;
use librashader_reflect::reflect::semantics::MemberOffset; use librashader_reflect::reflect::semantics::MemberOffset;
use std::marker::PhantomData; use std::marker::PhantomData;

View file

@ -47,7 +47,9 @@ pub mod presets {
let iters: Result<Vec<Vec<ShaderParameter>>, PreprocessError> = preset let iters: Result<Vec<Vec<ShaderParameter>>, PreprocessError> = preset
.shaders .shaders
.iter() .iter()
.map(|s| ShaderSource::load(&s.name).map(|s| s.parameters)) .map(|s| ShaderSource::load(&s.name).map(|s|
s.parameters.into_values()
.collect()))
.into_iter() .into_iter()
.collect(); .collect();
let iters = iters?; let iters = iters?;