use crate::front::storage::ShaderObject;
use wgpu;
pub struct WGPUShader {
pub module: wgpu::ShaderModule,
pub stage: naga::ShaderStage,
pub entry_point: String,
}
impl WGPUShader {
pub fn from_shader_object(device: &wgpu::Device, shader_obj: &ShaderObject) -> Self {
let info = naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::all(),
)
.validate(&shader_obj.ir.module)
.expect("Shader validation failed");
let wgsl_string = naga::back::wgsl::write_string(
&shader_obj.ir.module,
&info,
naga::back::wgsl::WriterFlags::empty(),
)
.expect("Failed to convert Naga to WGSL");
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("shdrlib shader"),
source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Owned(wgsl_string)),
});
WGPUShader {
module,
stage: shader_obj.ir.metadata.stage,
entry_point: shader_obj.ir.metadata.entry_point.clone(),
}
}
pub fn from_ir(
device: &wgpu::Device,
module: &naga::Module,
stage: naga::ShaderStage,
entry_point: String,
) -> Self {
let info = naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::all(),
)
.validate(module)
.expect("Shader validation failed");
let wgsl_string = naga::back::wgsl::write_string(
module,
&info,
naga::back::wgsl::WriterFlags::empty(),
)
.expect("Failed to convert Naga to WGSL");
let shader_module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("shdrlib shader"),
source: wgpu::ShaderSource::Wgsl(std::borrow::Cow::Owned(wgsl_string)),
});
WGPUShader {
module: shader_module,
stage,
entry_point,
}
}
}