cli: ensure shaders are validated before compile

This commit is contained in:
chyyran 2024-09-26 03:21:24 -04:00 committed by Ronny Chan
parent 5573f13227
commit 1537c1bcd7

View file

@ -5,11 +5,11 @@ use librashader::presets::context::ContextItem;
use librashader::presets::{ShaderPreset, WildcardContext}; use librashader::presets::{ShaderPreset, WildcardContext};
use librashader::reflect::cross::{GlslVersion, HlslShaderModel, MslVersion, SpirvCross}; use librashader::reflect::cross::{GlslVersion, HlslShaderModel, MslVersion, SpirvCross};
use librashader::reflect::naga::{Naga, NagaLoweringOptions}; use librashader::reflect::naga::{Naga, NagaLoweringOptions};
use librashader::reflect::semantics::{ ShaderSemantics}; use librashader::reflect::semantics::ShaderSemantics;
use librashader::reflect::{CompileShader, FromCompilation, ReflectShader, SpirvCompilation}; use librashader::reflect::{CompileShader, FromCompilation, ReflectShader, SpirvCompilation};
use librashader_test::render::RenderTest; use librashader_test::render::RenderTest;
use std::path::{Path, PathBuf};
use ron::ser::PrettyConfig; use ron::ser::PrettyConfig;
use std::path::{Path, PathBuf};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about)] #[command(version, about)]
@ -303,8 +303,10 @@ pub fn main() -> Result<(), anyhow::Error> {
let compilation = SpirvCompilation::try_from(&source)?; let compilation = SpirvCompilation::try_from(&source)?;
let output = match format { let output = match format {
TranspileFormat::GLSL => { TranspileFormat::GLSL => {
let compilation = let mut compilation =
librashader::reflect::targets::GLSL::from_compilation(compilation)?; librashader::reflect::targets::GLSL::from_compilation(compilation)?;
compilation.validate()?;
let output = compilation.compile(GlslVersion::Glsl330)?; let output = compilation.compile(GlslVersion::Glsl330)?;
TranspileOutput { TranspileOutput {
vertex: output.vertex, vertex: output.vertex,
@ -312,8 +314,9 @@ pub fn main() -> Result<(), anyhow::Error> {
} }
} }
TranspileFormat::HLSL => { TranspileFormat::HLSL => {
let compilation = let mut compilation =
librashader::reflect::targets::HLSL::from_compilation(compilation)?; librashader::reflect::targets::HLSL::from_compilation(compilation)?;
compilation.validate()?;
let output = compilation.compile(Some(HlslShaderModel::ShaderModel5_0))?; let output = compilation.compile(Some(HlslShaderModel::ShaderModel5_0))?;
TranspileOutput { TranspileOutput {
vertex: output.vertex, vertex: output.vertex,
@ -321,8 +324,9 @@ pub fn main() -> Result<(), anyhow::Error> {
} }
} }
TranspileFormat::WGSL => { TranspileFormat::WGSL => {
let compilation = let mut compilation =
librashader::reflect::targets::WGSL::from_compilation(compilation)?; librashader::reflect::targets::WGSL::from_compilation(compilation)?;
compilation.validate()?;
let output = compilation.compile(NagaLoweringOptions { let output = compilation.compile(NagaLoweringOptions {
write_pcb_as_ubo: true, write_pcb_as_ubo: true,
sampler_bind_group: 1, sampler_bind_group: 1,
@ -333,21 +337,24 @@ pub fn main() -> Result<(), anyhow::Error> {
} }
} }
TranspileFormat::MSL => { TranspileFormat::MSL => {
let compilation = <librashader::reflect::targets::MSL as FromCompilation< let mut compilation = <librashader::reflect::targets::MSL as FromCompilation<
SpirvCompilation, SpirvCompilation,
SpirvCross, SpirvCross,
>>::from_compilation(compilation)?; >>::from_compilation(compilation)?;
compilation.validate()?;
let output = compilation.compile(Some(MslVersion::new(1, 2, 0)))?; let output = compilation.compile(Some(MslVersion::new(1, 2, 0)))?;
TranspileOutput { TranspileOutput {
vertex: output.vertex, vertex: output.vertex,
fragment: output.fragment, fragment: output.fragment,
} }
} }
TranspileFormat::SPIRV => { TranspileFormat::SPIRV => {
let compilation = <librashader::reflect::targets::SPIRV as FromCompilation< let mut compilation = <librashader::reflect::targets::SPIRV as FromCompilation<
SpirvCompilation, SpirvCompilation,
SpirvCross, SpirvCross,
>>::from_compilation(compilation)?; >>::from_compilation(compilation)?;
compilation.validate()?;
let output = compilation.compile(None)?; let output = compilation.compile(None)?;
TranspileOutput { TranspileOutput {
@ -378,7 +385,8 @@ pub fn main() -> Result<(), anyhow::Error> {
let source = librashader::preprocess::ShaderSource::load(shader.name.as_path())?; let source = librashader::preprocess::ShaderSource::load(shader.name.as_path())?;
let compilation = SpirvCompilation::try_from(&source)?; let compilation = SpirvCompilation::try_from(&source)?;
let semantics = ShaderSemantics::create_pass_semantics::<anyhow::Error>(&preset, index)?; let semantics =
ShaderSemantics::create_pass_semantics::<anyhow::Error>(&preset, index)?;
let reflection = match backend { let reflection = match backend {
ReflectionBackend::SpirvCross => { ReflectionBackend::SpirvCross => {
@ -399,7 +407,10 @@ pub fn main() -> Result<(), anyhow::Error> {
} }
}; };
print!("{}", ron::ser::to_string_pretty(&reflection, PrettyConfig::new())?); print!(
"{}",
ron::ser::to_string_pretty(&reflection, PrettyConfig::new())?
);
} }
} }