tirbofish/dropbear
main / crates / dropbear-engine / src / shader.rs · 3061 bytes
crates/dropbear-engine/src/shader.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! Deals with shaders, primarily around the [Shader] struct.
use crate::graphics::SharedGraphicsContext;
use slank::{CompiledSlangShader, utils::WgpuUtils};
use std::ops::Deref;
use std::sync::Arc;
use wgpu::ShaderModule;
pub mod code {
wesl::wesl_pkg!(dropbear_shaders);
pub use dropbear_shaders::*;
}
/// A nice little struct that stored basic information about a WGPU shaders.
pub struct Shader {
/// The label of the shader.
///
/// If it is not set in [Shader::new], the default is "shader".
pub label: String,
/// The compiled content of the WGSL shader.
///
/// When [Shader] is dereferenced (such as that in `&shader`), it will automatically reference
/// this module.
pub module: ShaderModule,
/// The content of the shader as a readable string content, in the case you need to look
/// at the original source.
pub content: Option<String>,
}
impl Deref for Shader {
type Target = ShaderModule;
fn deref(&self) -> &Self::Target {
&self.module
}
}
impl Shader {
/// Creates a new [`ShaderModule`] from its file contents.
pub fn new(
graphics: Arc<SharedGraphicsContext>,
shader_file_contents: &str,
label: Option<&str>,
) -> Self {
puffin::profile_function!();
let module = graphics
.device
.create_shader_module(wgpu::ShaderModuleDescriptor {
label,
source: wgpu::ShaderSource::Wgsl(shader_file_contents.into()),
});
log::debug!("Created new shaders under the label: {:?}", label);
Self {
label: match label {
Some(label) => label.into(),
None => "shader".into(),
},
module,
content: Some(shader_file_contents.to_string()),
}
}
pub fn from_slang(graphics: Arc<SharedGraphicsContext>, shader: &CompiledSlangShader) -> Self {
puffin::profile_function!();
let module = graphics
.device
.create_shader_module(shader.create_wgpu_shader());
log::debug!(
"Created new shaders [slang] under the label: {}",
shader.label()
);
Self {
label: shader.label().clone(),
module,
content: None,
}
}
/// At this point, just create the shader manually lol
pub fn from_descriptor(
graphics: Arc<SharedGraphicsContext>,
desc: wgpu::ShaderModuleDescriptor,
label: Option<&str>
) -> Self {
let module = graphics.device.create_shader_module(desc);
puffin::profile_function!();
log::debug!("Created new shaders under the label: {:?}", label);
CompiledSlangShader::from_bytes("light cube", slank::include_slang!("light_cube"));
Self {
label: match label {
Some(label) => label.into(),
None => "shader".into(),
},
module,
content: None,
}
}
}