reflect: move naga stuff to naga

This commit is contained in:
chyyran 2022-10-25 01:50:08 -04:00
parent 6ab3775b23
commit 809c58f34d
2 changed files with 65 additions and 72 deletions

View file

@ -18,10 +18,14 @@ pub fn compile_spirv(source: &ShaderSource) -> Result<NagaCompilation, ShaderCom
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use rspirv::binary::Disassemble;
use crate::front::naga::compile_spirv; use crate::front::naga::compile_spirv;
use naga::front::glsl::{Options, Parser}; use naga::front::glsl::{Options, Parser};
use naga::ShaderStage; use naga::back::glsl::{PipelineOptions, Version};
use naga::back::spv::{Capability, WriterFlags};
use naga::{FastHashSet, ShaderStage};
use naga::front::spv::Options as SpvOptions;
use naga::valid::{Capabilities, ModuleInfo, ValidationFlags};
#[test] #[test]
pub fn compile_naga_test() { pub fn compile_naga_test() {
let result = librashader_preprocess::load_shader_source( let result = librashader_preprocess::load_shader_source(
@ -54,10 +58,68 @@ mod test {
) )
.unwrap(); .unwrap();
let cross = crate::front::shaderc::compile_spirv(&result).unwrap(); let cross = crate::front::shaderc::compile_spirv(&result).unwrap();
let naga_fragment = naga::front::spv::parse_u8_slice(cross.fragment.as_binary_u8(), &naga::front::spv::Options::default()) let naga_fragment = naga::front::spv::parse_u8_slice(cross.fragment.as_binary_u8(), &SpvOptions::default())
.unwrap(); .unwrap();
println!("{:#?}", naga_fragment.constants); println!("{:#?}", naga_fragment.constants);
println!("{:#?}", naga_fragment.global_variables); println!("{:#?}", naga_fragment.global_variables);
println!("{:#?}", naga_fragment.types); println!("{:#?}", naga_fragment.types);
} }
#[test]
pub fn naga_playground() {
let result = librashader_preprocess::load_shader_source(
"../test/basic.slang",
)
.unwrap();
let spirv = crate::front::shaderc::compile_spirv(&result).unwrap();
let module = naga::front::spv::parse_u8_slice(spirv.fragment.as_binary_u8(), &SpvOptions::default()).unwrap();
let capability = FastHashSet::from_iter([Capability::Shader]);
let mut writer = naga::back::spv::Writer::new(&naga::back::spv::Options {
lang_version: (1, 0),
flags: WriterFlags::all(),
binding_map: Default::default(),
capabilities: Some(capability),
bounds_check_policies: Default::default()
}).unwrap();
let mut validator = naga::valid::Validator::new(ValidationFlags::empty(), Capabilities::all());
let info = validator.validate(&module).unwrap();
let mut out = Vec::new();
writer.write(&module, &info, None, &mut out).unwrap();
let mut glsl_out = String::new();
let opts = naga::back::glsl::Options {
version: Version::Desktop(330),
writer_flags: naga::back::glsl::WriterFlags::all(),
binding_map: Default::default()
};
let pipe = PipelineOptions {
shader_stage: ShaderStage::Fragment,
entry_point: "main".to_string(),
multiview: None
};
let mut glsl_naga = naga::back::glsl::Writer::new(&mut glsl_out, &module, &info, &opts, &pipe, Default::default()).unwrap();
glsl_naga.write().unwrap();
let wgsl = naga::back::wgsl::write_string(&module, &info, naga::back::wgsl::WriterFlags::all()).unwrap();
let mut loader = rspirv::dr::Loader::new();
rspirv::binary::parse_words(&out, &mut loader).unwrap();
let module = loader.module();
println!("--- spirv --");
println!("{:#}", module.disassemble());
println!("--- cross glsl --");
let loaded = spirv_cross::spirv::Module::from_words(&out);
let mut ast = spirv_cross::spirv::Ast::<spirv_cross::glsl::Target>::parse(&loaded)
.unwrap();
println!("{:#}", ast.compile().unwrap());
println!("--- naga glsl---");
println!("{:#}", glsl_out);
println!("--- naga wgsl---");
println!("{:#}", wgsl)
}
} }

View file

@ -123,14 +123,7 @@ pub fn compile_spirv(source: &ShaderSource) -> Result<GlslangCompilation, Shader
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use rspirv::binary::Disassemble;
use naga::back::glsl::{PipelineOptions, Version};
use naga::back::spv::{Capability, WriterFlags};
use naga::{FastHashSet, ShaderStage};
use naga::front::spv::Options;
use naga::valid::{Capabilities, ModuleInfo, ValidationFlags};
use crate::front::shaderc::compile_spirv; use crate::front::shaderc::compile_spirv;
#[test] #[test]
pub fn compile_shader() { pub fn compile_shader() {
let result = librashader_preprocess::load_shader_source( let result = librashader_preprocess::load_shader_source(
@ -139,66 +132,4 @@ mod test {
.unwrap(); .unwrap();
let spirv = compile_spirv(&result).unwrap(); let spirv = compile_spirv(&result).unwrap();
} }
#[test]
pub fn naga_playground() {
let result = librashader_preprocess::load_shader_source(
"../test/basic.slang",
)
.unwrap();
let spirv = compile_spirv(&result).unwrap();
let module = naga::front::spv::parse_u8_slice(spirv.fragment.as_binary_u8(), &Options {
adjust_coordinate_space: false,
strict_capabilities: false,
block_ctx_dump_prefix: None
}).unwrap();
let capability = FastHashSet::from_iter([Capability::Shader]);
let mut writer = naga::back::spv::Writer::new(&naga::back::spv::Options {
lang_version: (1, 0),
flags: WriterFlags::all(),
binding_map: Default::default(),
capabilities: Some(capability),
bounds_check_policies: Default::default()
}).unwrap();
let mut validator = naga::valid::Validator::new(ValidationFlags::empty(), Capabilities::all());
let info = validator.validate(&module).unwrap();
let mut out = Vec::new();
writer.write(&module, &info, None, &mut out).unwrap();
let mut glsl_out = String::new();
let opts = naga::back::glsl::Options {
version: Version::Desktop(330),
writer_flags: naga::back::glsl::WriterFlags::all(),
binding_map: Default::default()
};
let pipe = PipelineOptions {
shader_stage: ShaderStage::Fragment,
entry_point: "main".to_string(),
multiview: None
};
let mut glsl_naga = naga::back::glsl::Writer::new(&mut glsl_out, &module, &info, &opts, &pipe, Default::default()).unwrap();
glsl_naga.write().unwrap();
let wgsl = naga::back::wgsl::write_string(&module, &info, naga::back::wgsl::WriterFlags::all()).unwrap();
let mut loader = rspirv::dr::Loader::new();
rspirv::binary::parse_words(&out, &mut loader).unwrap();
let module = loader.module();
println!("--- spirv --");
println!("{:#}", module.disassemble());
println!("--- cross glsl --");
let loaded = spirv_cross::spirv::Module::from_words(&out);
let mut ast = spirv_cross::spirv::Ast::<spirv_cross::glsl::Target>::parse(&loaded)
.unwrap();
println!("{:#}", ast.compile().unwrap());
println!("--- naga glsl---");
println!("{:#}", glsl_out);
println!("--- naga wgsl---");
println!("{:#}", wgsl)
}
} }