feat: Compile to WGSL
This commit is contained in:
parent
82d74ee790
commit
8ca07019ba
@ -15,6 +15,7 @@ wgpu.workspace = true
|
||||
|
||||
[build-dependencies]
|
||||
spirv-builder.workspace = true
|
||||
wgpu.workspace = true
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
@ -1,23 +1,36 @@
|
||||
use spirv_builder::{Capability, MetadataPrintout, SpirvBuilder};
|
||||
use spirv_builder::{MetadataPrintout, SpirvBuilder};
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use wgpu::naga;
|
||||
use wgpu::naga::back::wgsl::WriterFlags;
|
||||
use wgpu::naga::front::spv::Options;
|
||||
use wgpu::naga::valid::{Capabilities, ValidationFlags};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let gpu_crate_path = Path::new("../flare-shader/");
|
||||
|
||||
// Compile the shader crate with SpirvBuilder.
|
||||
let result = SpirvBuilder::new(gpu_crate_path, "spirv-unknown-vulkan1.2")
|
||||
.capability(Capability::Int64)
|
||||
let result = SpirvBuilder::new(gpu_crate_path, "spirv-unknown-spv1.3")
|
||||
.print_metadata(MetadataPrintout::Full)
|
||||
.build()?;
|
||||
|
||||
// Copy the SPIR-V to this crate's output directory
|
||||
let shader_path = result.module.unwrap_single();
|
||||
let output_path = PathBuf::from(env::var("OUT_DIR")?).join("shader_binary.spv");
|
||||
|
||||
fs::copy(shader_path, &output_path)?;
|
||||
|
||||
// Re-compile as wgsl
|
||||
let mut spv_binary = vec![];
|
||||
BufReader::new(File::open(&output_path)?).read_to_end(&mut spv_binary)?;
|
||||
let module = naga::front::spv::parse_u8_slice(&spv_binary, &Options::default())?;
|
||||
let module_info = naga::valid::Validator::new(ValidationFlags::default(), Capabilities::default()).validate(&module)?;
|
||||
let wgsl_string = naga::back::wgsl::write_string(&module, &module_info, WriterFlags::empty())?;
|
||||
|
||||
File::create(PathBuf::from(env::var("OUT_DIR")?).join("shader.wgsl"))?.write_all(wgsl_string.as_bytes())?;
|
||||
|
||||
println!("Generated shader binary constant at {:?}", output_path);
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user