librashader/librashader-runtime-dx11/src/lib.rs

148 lines
4.1 KiB
Rust
Raw Normal View History

use librashader_preprocess::ShaderSource;
2022-11-07 16:25:11 +11:00
use librashader_presets::ShaderPassConfig;
use librashader_reflect::back::targets::{FromCompilation, HLSL};
2022-11-22 08:21:50 +11:00
use librashader_reflect::back::CompileShader;
use rustc_hash::FxHashMap;
use std::error::Error;
use std::path::Path;
2022-11-07 16:25:11 +11:00
2022-11-22 08:21:50 +11:00
use librashader_reflect::reflect::semantics::{SemanticMap, TextureSemantics, VariableSemantics};
use librashader_reflect::reflect::{ReflectSemantics, ReflectShader, UniformSemantic};
2022-11-07 16:25:11 +11:00
2022-11-22 08:21:50 +11:00
pub fn load_pass_semantics(
uniform_semantics: &mut FxHashMap<String, UniformSemantic>,
texture_semantics: &mut FxHashMap<String, SemanticMap<TextureSemantics>>,
config: &ShaderPassConfig,
) {
2022-11-07 16:25:11 +11:00
let Some(alias) = &config.alias else {
return;
};
// Ignore empty aliases
if alias.trim().is_empty() {
return;
}
2022-11-21 18:39:39 +11:00
let index = config.id as usize;
2022-11-07 16:25:11 +11:00
// PassOutput
2022-11-22 08:21:50 +11:00
texture_semantics.insert(
alias.clone(),
SemanticMap {
semantics: TextureSemantics::PassOutput,
index,
},
);
uniform_semantics.insert(
format!("{alias}Size"),
UniformSemantic::Texture(SemanticMap {
semantics: TextureSemantics::PassOutput,
index,
}),
);
2022-11-07 16:25:11 +11:00
// PassFeedback
2022-11-22 08:21:50 +11:00
texture_semantics.insert(
format!("{alias}Feedback"),
SemanticMap {
semantics: TextureSemantics::PassFeedback,
index,
},
);
uniform_semantics.insert(
format!("{alias}FeedbackSize"),
UniformSemantic::Texture(SemanticMap {
semantics: TextureSemantics::PassFeedback,
index,
}),
);
2022-11-07 16:25:11 +11:00
}
2022-11-22 08:21:50 +11:00
pub fn load(path: impl AsRef<Path>) -> Result<(), Box<dyn Error>> {
2022-11-07 16:25:11 +11:00
let preset = librashader_presets::ShaderPreset::try_parse(path)?;
2022-11-22 08:21:50 +11:00
let passes: Vec<(&ShaderPassConfig, ShaderSource, _)> = preset
.shaders
.iter()
2022-11-07 16:25:11 +11:00
.map(|shader| {
2022-11-22 08:21:50 +11:00
let source = ShaderSource::load(&shader.name).unwrap();
let spirv = librashader_reflect::front::shaderc::compile_spirv(&source).unwrap();
let reflect = HLSL::from_compilation(spirv).unwrap();
2022-11-07 16:25:11 +11:00
(shader, source, reflect)
2022-11-22 08:21:50 +11:00
})
.collect();
2022-11-07 16:25:11 +11:00
// todo: this can probably be extracted out.
let mut uniform_semantics: FxHashMap<String, UniformSemantic> = Default::default();
2022-11-22 08:21:50 +11:00
let mut texture_semantics: FxHashMap<String, SemanticMap<TextureSemantics>> =
Default::default();
2022-11-07 16:25:11 +11:00
for details in &passes {
load_pass_semantics(&mut uniform_semantics, &mut texture_semantics, details.0)
}
// add float params
2022-11-22 08:21:50 +11:00
for (_index, parameter) in preset.parameters.iter().enumerate() {
uniform_semantics.insert(
parameter.name.clone(),
UniformSemantic::Variable(SemanticMap {
semantics: VariableSemantics::FloatParameter,
index: (),
}),
);
2022-11-07 16:25:11 +11:00
}
// add lut params
for (index, texture) in preset.textures.iter().enumerate() {
2022-11-22 08:21:50 +11:00
texture_semantics.insert(
texture.name.clone(),
SemanticMap {
semantics: TextureSemantics::User,
index,
},
);
2022-11-07 16:25:11 +11:00
}
let semantics = ReflectSemantics {
uniform_semantics,
2022-11-22 08:21:50 +11:00
non_uniform_semantics: texture_semantics,
2022-11-07 16:25:11 +11:00
};
let mut reflections = Vec::new();
let mut compiled = Vec::new();
for (index, (_, _, mut reflect)) in passes.into_iter().enumerate() {
2022-11-22 08:21:50 +11:00
let reflection = reflect.reflect(index, &semantics).unwrap();
2022-11-07 16:25:11 +11:00
2022-11-22 08:21:50 +11:00
let hlsl = reflect.compile(None).unwrap();
2022-11-07 16:25:11 +11:00
eprintln!("{:#}", hlsl.vertex);
eprintln!("{:#}", hlsl.fragment);
compiled.push(hlsl);
reflections.push(reflection);
}
eprintln!("{:#?}", reflections);
// //todo: add the semantics for other shit (slang_process:68)
// eprintln!("{:?}", preset);
// eprintln!("{:?}", reflect.reflect(&ReflectOptions {
// pass_number: i as u32,
// uniform_semantics,
// non_uniform_semantics: Default::default(),
// }));
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn load_preset() {
2022-11-22 08:21:50 +11:00
load("../test/basic.slangp").unwrap();
2022-11-07 16:25:11 +11:00
}
}