srs2dge_core/shader/
module.rs1use crate::{label, target::Target};
2use std::borrow::Cow;
3use wgpu::ShaderModuleDescriptor;
4
5pub use naga::{FastHashMap, ShaderStage};
8pub use wgpu::ShaderSource;
9
10pub struct ShaderModule<'a> {
13 pub(crate) inner: wgpu::ShaderModule,
14 pub(crate) source: ShaderSource<'a>,
15}
16
17impl<'a> ShaderModule<'a> {
20 pub fn new_wgsl_source(target: &Target, source: Cow<'a, str>) -> Result<Self, String> {
21 Self::new(target, ShaderSource::Wgsl(source))
22 }
23
24 #[cfg(feature = "glsl")]
25 pub fn new_glsl_source(
26 target: &Target,
27 source: Cow<'a, str>,
28 stage: ShaderStage,
29 defines: FastHashMap<String, String>,
30 ) -> Result<Self, String> {
31 Self::new(
32 target,
33 ShaderSource::Glsl {
34 shader: source,
35 stage,
36 defines,
37 },
38 )
39 }
40
41 #[cfg(feature = "spirv")]
42 pub fn new_spirv_source(target: &Target, source: &'a [u32]) -> Result<Self, String> {
43 Self::new(target, ShaderSource::SpirV(Cow::Borrowed(source)))
44 }
45
46 pub fn new(target: &Target, source: ShaderSource<'a>) -> Result<Self, String> {
47 Self::from_descriptor(
48 target,
49 ShaderModuleDescriptor {
50 label: label!(),
51 source,
52 },
53 )
54 }
55
56 fn from_descriptor(
57 target: &Target,
58 descriptor: ShaderModuleDescriptor<'a>,
59 ) -> Result<Self, String> {
60 Ok(Self {
61 inner: target.catch_error(|engine| engine.device.create_shader_module(&descriptor))?,
62 source: descriptor.source,
63 })
64 }
65}