/
pipelines.rs
376 lines (353 loc) · 13.2 KB
/
pipelines.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
use crate::blend::{ComplexBlend, TrivialBlend};
use crate::layouts::BindLayouts;
use crate::shaders::Shaders;
use crate::{MaskState, PosColorVertex, PosVertex};
use enum_map::{enum_map, Enum, EnumMap};
use wgpu::{vertex_attr_array, BlendState};
pub const VERTEX_BUFFERS_DESCRIPTION_POS: [wgpu::VertexBufferLayout; 1] =
[wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<PosVertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &vertex_attr_array![
0 => Float32x2,
],
}];
pub const VERTEX_BUFFERS_DESCRIPTION_COLOR: [wgpu::VertexBufferLayout; 1] =
[wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<PosColorVertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &vertex_attr_array![
0 => Float32x2,
1 => Float32x4,
],
}];
#[derive(Debug)]
pub struct ShapePipeline {
pub pipelines: EnumMap<MaskState, wgpu::RenderPipeline>,
stencilless: wgpu::RenderPipeline,
}
#[derive(Debug)]
pub struct Pipelines {
pub color: ShapePipeline,
/// Renders a bitmap without any blending, and does
/// not write to the alpha channel. This is used for
/// drawing a finished Stage3D buffer onto the background.
pub bitmap_opaque: wgpu::RenderPipeline,
/// Like `bitmap_opaque`, but with a no-op `DepthStencilState`.
/// This is used when we're inside a `RenderPass` that is
/// using a stencil buffer, but we don't want to write to it
/// or use it any any way.
pub bitmap_opaque_dummy_stencil: wgpu::RenderPipeline,
pub bitmap: EnumMap<TrivialBlend, ShapePipeline>,
pub gradients: ShapePipeline,
pub complex_blends: EnumMap<ComplexBlend, ShapePipeline>,
}
impl ShapePipeline {
pub fn pipeline_for(&self, mask_state: MaskState) -> &wgpu::RenderPipeline {
&self.pipelines[mask_state]
}
pub fn stencilless_pipeline(&self) -> &wgpu::RenderPipeline {
&self.stencilless
}
/// Builds of a nested `EnumMap` that maps a `MaskState` to
/// a `RenderPipeline`. The provided callback is used to construct the `RenderPipeline`
/// for each possible `MaskState`.
fn build(
stencilless: wgpu::RenderPipeline,
mut f: impl FnMut(MaskState) -> wgpu::RenderPipeline,
) -> Self {
let mask_array: [wgpu::RenderPipeline; MaskState::LENGTH] = (0..MaskState::LENGTH)
.map(|mask_enum| {
let mask_state = MaskState::from_usize(mask_enum);
f(mask_state)
})
.collect::<Vec<_>>()
.try_into()
.unwrap();
ShapePipeline {
pipelines: EnumMap::from_array(mask_array),
stencilless,
}
}
}
impl Pipelines {
pub fn new(
device: &wgpu::Device,
shaders: &Shaders,
format: wgpu::TextureFormat,
msaa_sample_count: u32,
bind_layouts: &BindLayouts,
) -> Self {
let colort_bindings = vec![&bind_layouts.globals, &bind_layouts.transforms];
let color_pipelines = create_shape_pipeline(
"Color",
device,
format,
&shaders.color_shader,
msaa_sample_count,
&VERTEX_BUFFERS_DESCRIPTION_COLOR,
&colort_bindings,
wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING,
&[],
);
let gradient_bindings = vec![
&bind_layouts.globals,
&bind_layouts.transforms,
&bind_layouts.gradient,
];
let gradient_pipeline = create_shape_pipeline(
"Gradient",
device,
format,
&shaders.gradient_shader,
msaa_sample_count,
&VERTEX_BUFFERS_DESCRIPTION_POS,
&gradient_bindings,
wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING,
&[],
);
let complex_blend_bindings = vec![
&bind_layouts.globals,
&bind_layouts.transforms,
&bind_layouts.blend,
];
let complex_blend_pipelines = enum_map! {
blend => create_shape_pipeline(
&format!("Complex Blend: {blend:?}"),
device,
format,
&shaders.blend_shaders[blend],
msaa_sample_count,
&VERTEX_BUFFERS_DESCRIPTION_POS,
&complex_blend_bindings,
wgpu::BlendState::REPLACE,
&[],
)
};
let bitmap_blend_bindings = vec![
&bind_layouts.globals,
&bind_layouts.transforms,
&bind_layouts.bitmap,
];
let bitmap_pipelines: [ShapePipeline; TrivialBlend::LENGTH] = (0..TrivialBlend::LENGTH)
.map(|blend| {
let blend = TrivialBlend::from_usize(blend);
let name = format!("Bitmap ({blend:?})");
create_shape_pipeline(
&name,
device,
format,
&shaders.bitmap_shader,
msaa_sample_count,
&VERTEX_BUFFERS_DESCRIPTION_POS,
&bitmap_blend_bindings,
blend.blend_state(),
&[],
)
})
.collect::<Vec<_>>()
.try_into()
.unwrap();
let bitmap_opaque_pipeline_layout_label =
create_debug_label!("Opaque bitmap pipeline layout");
let bitmap_opaque_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: bitmap_opaque_pipeline_layout_label.as_deref(),
bind_group_layouts: &bitmap_blend_bindings,
push_constant_ranges: &[],
});
let bitmap_opaque = device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("Bitmap opaque copy").as_deref(),
&shaders.bitmap_late_saturate_shader,
&shaders.bitmap_late_saturate_shader,
&bitmap_opaque_pipeline_layout,
None,
&[Some(wgpu::ColorTargetState {
format,
blend: Some(BlendState::REPLACE),
write_mask: wgpu::ColorWrites::COLOR,
})],
&VERTEX_BUFFERS_DESCRIPTION_POS,
msaa_sample_count,
));
let bitmap_opaque_dummy_depth = device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("Bitmap opaque copy").as_deref(),
&shaders.bitmap_shader,
&shaders.bitmap_shader,
&bitmap_opaque_pipeline_layout,
Some(wgpu::DepthStencilState {
format: wgpu::TextureFormat::Stencil8,
depth_write_enabled: false,
depth_compare: wgpu::CompareFunction::Always,
stencil: wgpu::StencilState {
front: wgpu::StencilFaceState::IGNORE,
back: wgpu::StencilFaceState::IGNORE,
read_mask: 0,
write_mask: 0,
},
bias: wgpu::DepthBiasState::default(),
}),
&[Some(wgpu::ColorTargetState {
format,
blend: Some(BlendState::REPLACE),
write_mask: wgpu::ColorWrites::COLOR,
})],
&VERTEX_BUFFERS_DESCRIPTION_POS,
msaa_sample_count,
));
Self {
color: color_pipelines,
bitmap: EnumMap::from_array(bitmap_pipelines),
bitmap_opaque,
bitmap_opaque_dummy_stencil: bitmap_opaque_dummy_depth,
gradients: gradient_pipeline,
complex_blends: complex_blend_pipelines,
}
}
}
#[allow(clippy::too_many_arguments)]
fn create_pipeline_descriptor<'a>(
label: Option<&'a str>,
vertex_shader: &'a wgpu::ShaderModule,
fragment_shader: &'a wgpu::ShaderModule,
pipeline_layout: &'a wgpu::PipelineLayout,
depth_stencil_state: Option<wgpu::DepthStencilState>,
color_target_state: &'a [Option<wgpu::ColorTargetState>],
vertex_buffer_layout: &'a [wgpu::VertexBufferLayout<'a>],
msaa_sample_count: u32,
) -> wgpu::RenderPipelineDescriptor<'a> {
wgpu::RenderPipelineDescriptor {
label,
layout: Some(pipeline_layout),
vertex: wgpu::VertexState {
module: vertex_shader,
entry_point: "main_vertex",
buffers: vertex_buffer_layout,
},
fragment: Some(wgpu::FragmentState {
module: fragment_shader,
entry_point: "main_fragment",
targets: color_target_state,
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: None,
polygon_mode: wgpu::PolygonMode::default(),
unclipped_depth: false,
conservative: false,
},
depth_stencil: depth_stencil_state,
multisample: wgpu::MultisampleState {
count: msaa_sample_count,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
}
}
#[allow(clippy::too_many_arguments)]
fn create_shape_pipeline(
name: &str,
device: &wgpu::Device,
format: wgpu::TextureFormat,
shader: &wgpu::ShaderModule,
msaa_sample_count: u32,
vertex_buffers_layout: &[wgpu::VertexBufferLayout<'_>],
bind_group_layouts: &[&wgpu::BindGroupLayout],
blend: wgpu::BlendState,
push_constant_ranges: &[wgpu::PushConstantRange],
) -> ShapePipeline {
let pipeline_layout_label = create_debug_label!("{} shape pipeline layout", name);
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: pipeline_layout_label.as_deref(),
bind_group_layouts,
push_constant_ranges,
});
let mask_render_state = |mask_name, stencil_state, write_mask| {
device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("{} pipeline {}", name, mask_name).as_deref(),
shader,
shader,
&pipeline_layout,
Some(wgpu::DepthStencilState {
format: wgpu::TextureFormat::Stencil8,
depth_write_enabled: false,
depth_compare: wgpu::CompareFunction::Always,
stencil: wgpu::StencilState {
front: stencil_state,
back: stencil_state,
read_mask: !0,
write_mask: !0,
},
bias: Default::default(),
}),
&[Some(wgpu::ColorTargetState {
format,
blend: Some(blend),
write_mask,
})],
vertex_buffers_layout,
msaa_sample_count,
))
};
ShapePipeline::build(
device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("{} stencilless pipeline", name).as_deref(),
shader,
shader,
&pipeline_layout,
None,
&[Some(wgpu::ColorTargetState {
format,
blend: Some(blend),
write_mask: wgpu::ColorWrites::ALL,
})],
vertex_buffers_layout,
msaa_sample_count,
)),
|mask_state| match mask_state {
MaskState::NoMask => mask_render_state(
"no mask",
wgpu::StencilFaceState {
compare: wgpu::CompareFunction::Always,
fail_op: wgpu::StencilOperation::Keep,
depth_fail_op: wgpu::StencilOperation::Keep,
pass_op: wgpu::StencilOperation::Keep,
},
wgpu::ColorWrites::ALL,
),
MaskState::DrawMaskStencil => mask_render_state(
"draw mask stencil",
wgpu::StencilFaceState {
compare: wgpu::CompareFunction::Equal,
fail_op: wgpu::StencilOperation::Keep,
depth_fail_op: wgpu::StencilOperation::Keep,
pass_op: wgpu::StencilOperation::IncrementClamp,
},
wgpu::ColorWrites::empty(),
),
MaskState::DrawMaskedContent => mask_render_state(
"draw masked content",
wgpu::StencilFaceState {
compare: wgpu::CompareFunction::Equal,
fail_op: wgpu::StencilOperation::Keep,
depth_fail_op: wgpu::StencilOperation::Keep,
pass_op: wgpu::StencilOperation::Keep,
},
wgpu::ColorWrites::ALL,
),
MaskState::ClearMaskStencil => mask_render_state(
"clear mask stencil",
wgpu::StencilFaceState {
compare: wgpu::CompareFunction::Equal,
fail_op: wgpu::StencilOperation::Keep,
depth_fail_op: wgpu::StencilOperation::Keep,
pass_op: wgpu::StencilOperation::DecrementClamp,
},
wgpu::ColorWrites::empty(),
),
},
)
}