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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use crate::bind_group_layout::BindGroupLayoutDesc;
// use anyhow::Context as _;
use smallvec::SmallVec;
use std::borrow::Cow;
use wgpu;
pub const DEFAULT_VS_SHADER_ENTRY_POINT: &str = "vs_main";
pub const DEFAULT_FS_SHADER_ENTRY_POINT: &str = "fs_main";
/// Creates a render pipeline descriptor, can then be used to create a concrete
/// pipeline or for later for using the descriptor as a hash key in a pipeline
/// arena `RenderPipelineDesc` can be converted into [`wgpu::RenderPipeline`]
/// (which isn't hashable or comparable)
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct RenderPipelineDesc {
pub label: Option<String>,
// pub shader_path: String,
pub shader_code: Option<String>, //single file for both vertex and fragment
pub shader_code_vert: Option<String>,
pub shader_code_frag: Option<String>,
pub shader_label: Option<String>,
pub disable_fragment_shader: bool, //fragment state can be none when rendering to shadow map
//layout for each group of bindings in the pipline
pub bind_group_layouts_desc: SmallVec<[BindGroupLayoutDesc; 4]>,
/// The format of any vertex buffers used with this pipeline.
pub vertex_buffers_layouts: SmallVec<[wgpu::VertexBufferLayout<'static>; 4]>,
/// The color state of the render targets.
pub render_targets: SmallVec<[Option<wgpu::ColorTargetState>; 4]>,
//most of the stuff after this are usually default
/// The properties of the pipeline at the primitive assembly and
/// rasterization level.
pub primitive: wgpu::PrimitiveState,
/// The effect of draw calls on the depth and stencil aspects of the output
/// target, if any.
pub depth_stencil: Option<wgpu::DepthStencilState>,
/// The multi-sampling properties of the pipeline.
pub multisample: wgpu::MultisampleState,
}
impl Default for RenderPipelineDesc {
fn default() -> Self {
Self {
label: None,
// pub shader_path: String,
shader_code: None,
shader_code_vert: None,
shader_code_frag: None,
shader_label: Some(String::from("Shader")),
disable_fragment_shader: false,
//layout for each group of bindings in the pipline
bind_group_layouts_desc: SmallVec::new(),
// The format of any vertex buffers used with this pipeline.
vertex_buffers_layouts: SmallVec::new(),
// The color state of the render targets.
render_targets: SmallVec::new(),
//most of the stuff after this are usually default
// The properties of the pipeline at the primitive assembly and rasterization level.
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
// cull_mode: Some(wgpu::Face::Back),
cull_mode: None, //LEAVE it here because we want to visualize the backside of the planes used for Lights
// Setting this to anything other than Fill requires Features::POLYGON_MODE_LINE
// or Features::POLYGON_MODE_POINT
polygon_mode: wgpu::PolygonMode::Fill,
// Requires Features::DEPTH_CLIP_CONTROL
unclipped_depth: false,
// Requires Features::CONSERVATIVE_RASTERIZATION
conservative: false,
},
// The effect of draw calls on the depth and stencil aspects of the output target, if any.
depth_stencil: None,
// The multi-sampling properties of the pipeline.
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
}
}
}
impl RenderPipelineDesc {
/// # Panics
/// Will panic if no shader was set
pub fn into_render_pipeline(
self,
device: &wgpu::Device,
// ) -> wgpu::RenderPipelineDescriptor<'a> {
) -> wgpu::RenderPipeline {
//load shader
//we declare the shader here because we want it to live long enough so we can
// have reference to it
let shader_monolithic: Option<wgpu::ShaderModule>;
let shader_vert: Option<wgpu::ShaderModule>;
let shader_frag: Option<wgpu::ShaderModule>;
let (shader_vert, shader_frag) = if let Some(shader_code_monlithic) = self.shader_code {
shader_monolithic = Some(device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: self.shader_label.as_deref(),
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(&shader_code_monlithic)),
}));
(shader_monolithic.as_ref().unwrap(), shader_monolithic.as_ref().unwrap())
} else {
shader_vert = Some(device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: self.shader_label.as_deref(),
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(self.shader_code_vert.as_ref().unwrap())),
}));
shader_frag = Some(device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: self.shader_label.as_deref(),
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(self.shader_code_frag.as_ref().unwrap())),
}));
(shader_vert.as_ref().unwrap(), shader_frag.as_ref().unwrap())
};
// let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
// label: self.shader_label.as_deref(),
// source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(&self.shader_code)),
// });
// //make the bind group layouts
let mut bind_group_layouts: Vec<wgpu::BindGroupLayout> = Vec::new();
// for bgl_desc in self.pipeline_layout_desc.bind_group_layouts_desc {
for bgl_desc in self.bind_group_layouts_desc {
// let bgl = device
// .create_bind_group_layout(&bgl_desc.
// into_bind_group_layout_descriptor(device));
let bgl = bgl_desc.into_bind_group_layout(device);
bind_group_layouts.push(bgl);
}
//from &[T] to &[&T] because the bind group layouts expects that
let bind_group_layouts: Vec<&wgpu::BindGroupLayout> = bind_group_layouts.iter().collect();
// //make a render pipeline layout
let render_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: bind_group_layouts.as_slice(),
push_constant_ranges: &[],
});
//fragment state can be none when rendering to shadow map
let fragment_state: Option<wgpu::FragmentState> = if self.disable_fragment_shader {
None
} else {
Some(wgpu::FragmentState {
module: shader_frag,
entry_point: Some(DEFAULT_FS_SHADER_ENTRY_POINT),
targets: self.render_targets.as_slice(),
compilation_options: wgpu::PipelineCompilationOptions::default(),
})
};
let pipeline_desc = wgpu::RenderPipelineDescriptor {
label: self.label.as_deref(),
// layout: Some(&render_pipeline_layout),
layout: Some(&render_pipeline_layout),
vertex: wgpu::VertexState {
module: shader_vert,
entry_point: Some(DEFAULT_VS_SHADER_ENTRY_POINT),
buffers: self.vertex_buffers_layouts.as_slice(),
compilation_options: wgpu::PipelineCompilationOptions::default(),
},
fragment: fragment_state,
primitive: self.primitive,
depth_stencil: self.depth_stencil,
multisample: self.multisample,
multiview: None,
cache: None,
};
// pipeline_desc
device.create_render_pipeline(&pipeline_desc)
}
}
/// A builder type to help simplify the construction of a `RenderPipelineDesc`.
/// We've attempted to provide a suite of reasonable defaults in the case that
/// none are provided.
pub struct RenderPipelineDescBuilder {
pipeline_desc: Option<RenderPipelineDesc>,
}
impl Default for RenderPipelineDescBuilder {
fn default() -> Self {
Self::new()
}
}
impl RenderPipelineDescBuilder {
pub fn new() -> Self {
Self {
pipeline_desc: Some(RenderPipelineDesc::default()),
}
}
/// # Panics
/// Will panic if the builder was not constructed with ``new()``
#[must_use]
pub fn label(mut self, label: &str) -> Self {
self.pipeline_desc.as_mut().unwrap().label = Some(String::from(label));
self
}
/// # Panics
/// Will panic if the builder was not constructed with ``new()``
#[must_use]
pub fn shader_code(mut self, code: &str) -> Self {
self.pipeline_desc.as_mut().unwrap().shader_code = Some(String::from(code));
self
}
/// # Panics
/// Will panic if the builder was not constructed with ``new()``
#[must_use]
pub fn shader_code_vert(mut self, code: &str) -> Self {
self.pipeline_desc.as_mut().unwrap().shader_code_vert = Some(String::from(code));
self
}
/// # Panics
/// Will panic if the builder was not constructed with ``new()``
#[must_use]
pub fn shader_code_frag(mut self, code: &str) -> Self {
self.pipeline_desc.as_mut().unwrap().shader_code_frag = Some(String::from(code));
self
}
/// # Panics
/// Will panic if the builder was not constructed with ``new()``
#[must_use]
pub fn shader_label(mut self, label: &str) -> Self {
self.pipeline_desc.as_mut().unwrap().shader_label = Some(String::from(label));
self
}
/// # Panics
/// Will panic if the builder was not constructed with ``new()``
#[must_use]
pub fn disable_fragment_shader(mut self) -> Self {
self.pipeline_desc.as_mut().unwrap().disable_fragment_shader = true;
self
}
/// # Panics
/// Will panic if the builder was not constructed with ``new()``
#[must_use]
pub fn add_bind_group_layout_desc(mut self, layout_desc: BindGroupLayoutDesc) -> Self {
self.pipeline_desc.as_mut().unwrap().bind_group_layouts_desc.push(layout_desc);
self
}
/// # Panics
/// Will panic if the builder was not constructed with ``new()``
#[must_use]
pub fn add_vertex_buffer_layout(mut self, vertex_layout: wgpu::VertexBufferLayout<'static>) -> Self {
self.pipeline_desc.as_mut().unwrap().vertex_buffers_layouts.push(vertex_layout);
self
}
/// # Panics
/// Will panic if the builder was not constructed with ``new()``
#[must_use]
pub fn add_render_target(mut self, render_target: wgpu::ColorTargetState) -> Self {
self.pipeline_desc.as_mut().unwrap().render_targets.push(Some(render_target));
self
}
/// # Panics
/// Will panic if the builder was not constructed with ``new()``
#[must_use]
pub fn primitive(mut self, primitive: wgpu::PrimitiveState) -> Self {
self.pipeline_desc.as_mut().unwrap().primitive = primitive;
self
}
/// # Panics
/// Will panic if the builder was not constructed with ``new()``
#[must_use]
pub fn depth_state(mut self, depth_state: Option<wgpu::DepthStencilState>) -> Self {
self.pipeline_desc.as_mut().unwrap().depth_stencil = depth_state;
self
}
/// # Panics
/// Will panic if the builder was not constructed with ``new()``
#[must_use]
pub fn multisample(mut self, multisample: wgpu::MultisampleState) -> Self {
self.pipeline_desc.as_mut().unwrap().multisample = multisample;
self
}
/// # Panics
/// Will panic if the builder was not constructed with ``new()``
pub fn build_desc(&mut self) -> RenderPipelineDesc {
self.pipeline_desc.take().unwrap() //move out of the builder and
// returns a fully fledged object
}
/// # Panics
/// Will panic if the builder was not constructed with ``new()``
pub fn build_pipeline(&mut self, device: &wgpu::Device) -> wgpu::RenderPipeline {
let desc = self.pipeline_desc.take().unwrap();
desc.into_render_pipeline(device)
}
}