use super::{SovereignError, ValidatedSpirv};
use naga::back::spv;
use naga::valid::ModuleInfo;
pub fn emit_spirv(
module: &naga::Module,
info: &ModuleInfo,
) -> Result<ValidatedSpirv, SovereignError> {
let options = spv::Options {
lang_version: (1, 3),
flags: spv::WriterFlags::ADJUST_COORDINATE_SPACE,
capabilities: None,
bounds_check_policies: naga::proc::BoundsCheckPolicies::default(),
binding_map: spv::BindingMap::default(),
zero_initialize_workgroup_memory: spv::ZeroInitializeWorkgroupMemoryMode::Polyfill,
debug_info: None,
..Default::default()
};
let mut words = Vec::new();
let mut writer = spv::Writer::new(&options)
.map_err(|e| SovereignError::SpirvEmit(format!("failed to create SPIR-V writer: {e}")))?;
writer
.write(module, info, None, &None, &mut words)
.map_err(|e| SovereignError::SpirvEmit(format!("SPIR-V write failed: {e}")))?;
Ok(ValidatedSpirv::from_validated(words))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_emit_spirv_trivial() {
let wgsl = r"
@group(0) @binding(0) var<storage, read> input: array<f32>;
@group(0) @binding(1) var<storage, read_write> output: array<f32>;
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
let idx = gid.x;
output[idx] = input[idx] * 2.0;
}
";
let module = naga::front::wgsl::parse_str(wgsl).expect("parse");
let mut validator = naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::all(),
);
let info = validator.validate(&module).expect("validate");
let validated = emit_spirv(&module, &info).expect("emit");
assert!(!validated.words().is_empty());
assert_eq!(validated.words()[0], 0x07230203, "SPIR-V magic number");
}
#[test]
fn test_emit_spirv_f64() {
let wgsl = r"
enable f16;
@group(0) @binding(0) var<storage, read> input: array<f64>;
@group(0) @binding(1) var<storage, read_write> output: array<f64>;
@compute @workgroup_size(64)
fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
let idx = gid.x;
let a = input[idx];
let b = input[idx + 1u];
output[idx] = a * b + a;
}
";
let module = naga::front::wgsl::parse_str(wgsl);
if module.is_err() {
return;
}
let module = module.unwrap();
let mut validator = naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::all(),
);
if let Ok(info) = validator.validate(&module) {
let validated = emit_spirv(&module, &info).expect("emit f64 spirv");
assert_eq!(validated.words()[0], 0x07230203);
}
}
}