-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpipeline.rs
More file actions
1061 lines (969 loc) · 31.9 KB
/
pipeline.rs
File metadata and controls
1061 lines (969 loc) · 31.9 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Graphics render pipelines and builders.
//!
//! Purpose
//! - Define how vertex data flows into the vertex shader (buffer layouts and
//! attributes) and how fragments are produced (optional fragment stage and
//! color target).
//! - Compose a pipeline layout from bind group layouts and optional push
//! constant ranges.
//!
//! Usage
//! - Accumulate vertex buffers and `VertexAttribute` lists matching shader
//! `location`s.
//! - Provide one or more `BindGroupLayout`s used by the shaders.
//! - Supply a vertex shader and optional fragment shader compiled to SPIR‑V.
//!
//! Example
//! ```rust,ignore
//! // Single vertex buffer with position/color; one immediate data range for the vertex stage
//! use lambda::render::pipeline::{RenderPipelineBuilder, CullingMode};
//! let pipeline = RenderPipelineBuilder::new()
//! .with_buffer(vertex_buffer, attributes)
//! .with_immediate_data(64)
//! .with_layouts(&[&globals_bgl])
//! .with_culling(CullingMode::Back)
//! .build(&mut render_context, &render_pass, &vs, Some(&fs));
//! ```
use std::rc::Rc;
use lambda_platform::wgpu::pipeline as platform_pipeline;
use logging;
use super::{
bind,
buffer::{
Buffer,
BufferType,
},
gpu::Gpu,
render_pass::RenderPass,
shader::Shader,
texture,
vertex::{
VertexAttribute,
VertexBufferLayout,
VertexStepMode,
},
RenderContext,
};
use crate::render::validation;
/// A created graphics pipeline and the vertex buffers it expects.
#[derive(Debug)]
///
/// Pipelines are immutable; destroy them with the context when no longer needed.
pub struct RenderPipeline {
pipeline: Rc<platform_pipeline::RenderPipeline>,
buffers: Vec<Rc<Buffer>>,
sample_count: u32,
color_target_count: u32,
color_target_format: Option<texture::TextureFormat>,
expects_depth_stencil: bool,
depth_format: Option<texture::DepthFormat>,
uses_stencil: bool,
per_instance_slots: Vec<bool>,
}
impl RenderPipeline {
/// Destroy the render pipeline with the render context that created it.
pub fn destroy(self, _render_context: &RenderContext) {}
/// Access the vertex buffers associated with this pipeline.
pub(super) fn buffers(&self) -> &Vec<Rc<Buffer>> {
return &self.buffers;
}
/// Access the underlying platform render pipeline.
pub(super) fn pipeline(&self) -> &platform_pipeline::RenderPipeline {
return self.pipeline.as_ref();
}
/// Multisample count configured on this pipeline.
pub fn sample_count(&self) -> u32 {
return self.sample_count.max(1);
}
/// Whether the pipeline declares one or more color targets.
pub(super) fn has_color_targets(&self) -> bool {
return self.color_target_count > 0;
}
pub(super) fn color_target_format(&self) -> Option<texture::TextureFormat> {
return self.color_target_format;
}
/// Whether the pipeline expects a depth-stencil attachment.
pub(super) fn expects_depth_stencil(&self) -> bool {
return self.expects_depth_stencil;
}
pub(super) fn depth_format(&self) -> Option<texture::DepthFormat> {
return self.depth_format;
}
/// Whether the pipeline configured a stencil test/state.
pub(super) fn uses_stencil(&self) -> bool {
return self.uses_stencil;
}
/// Per-vertex-buffer flags indicating which slots advance per instance.
pub(super) fn per_instance_slots(&self) -> &Vec<bool> {
return &self.per_instance_slots;
}
}
/// Public alias for platform shader stage flags.
///
/// Stage flags remain useful for APIs such as bind group visibility, even
/// though wgpu v28 immediates no longer use stage-scoped updates.
pub use platform_pipeline::PipelineStage;
/// Blend mode for the pipeline's (single) color target.
///
/// Notes
/// - Defaults to `BlendMode::None` (opaque/replace). Opt in to blending for
/// transparent geometry.
/// - This is currently a single blend state for a single color attachment.
/// Per-attachment blending for MRT is future work.
#[derive(Clone, Copy, Debug)]
pub enum BlendMode {
/// No blending; replace destination (default).
None,
/// Standard alpha blending.
AlphaBlending,
/// Premultiplied alpha blending.
PremultipliedAlpha,
/// Additive blending.
Additive,
}
impl BlendMode {
fn to_platform(self) -> platform_pipeline::BlendMode {
return match self {
BlendMode::None => platform_pipeline::BlendMode::None,
BlendMode::AlphaBlending => platform_pipeline::BlendMode::AlphaBlending,
BlendMode::PremultipliedAlpha => {
platform_pipeline::BlendMode::PremultipliedAlpha
}
BlendMode::Additive => platform_pipeline::BlendMode::Additive,
};
}
}
struct BufferBinding {
buffer: Rc<Buffer>,
layout: VertexBufferLayout,
attributes: Vec<VertexAttribute>,
}
#[derive(Clone, Copy, Debug)]
/// Engine-level compare function for depth/stencil tests.
pub enum CompareFunction {
Never,
Less,
LessEqual,
Greater,
GreaterEqual,
Equal,
NotEqual,
Always,
}
impl CompareFunction {
fn to_platform(self) -> platform_pipeline::CompareFunction {
return match self {
CompareFunction::Never => platform_pipeline::CompareFunction::Never,
CompareFunction::Less => platform_pipeline::CompareFunction::Less,
CompareFunction::LessEqual => {
platform_pipeline::CompareFunction::LessEqual
}
CompareFunction::Greater => platform_pipeline::CompareFunction::Greater,
CompareFunction::GreaterEqual => {
platform_pipeline::CompareFunction::GreaterEqual
}
CompareFunction::Equal => platform_pipeline::CompareFunction::Equal,
CompareFunction::NotEqual => platform_pipeline::CompareFunction::NotEqual,
CompareFunction::Always => platform_pipeline::CompareFunction::Always,
};
}
}
#[derive(Clone, Copy, Debug)]
/// Engine-level face culling mode for graphics pipelines.
pub enum CullingMode {
None,
Front,
Back,
}
impl CullingMode {
fn to_platform(self) -> platform_pipeline::CullingMode {
return match self {
CullingMode::None => platform_pipeline::CullingMode::None,
CullingMode::Front => platform_pipeline::CullingMode::Front,
CullingMode::Back => platform_pipeline::CullingMode::Back,
};
}
}
fn to_platform_step_mode(
step_mode: VertexStepMode,
) -> platform_pipeline::VertexStepMode {
return match step_mode {
VertexStepMode::PerVertex => platform_pipeline::VertexStepMode::Vertex,
VertexStepMode::PerInstance => platform_pipeline::VertexStepMode::Instance,
};
}
/// Engine-level stencil operation.
#[derive(Clone, Copy, Debug)]
pub enum StencilOperation {
Keep,
Zero,
Replace,
Invert,
IncrementClamp,
DecrementClamp,
IncrementWrap,
DecrementWrap,
}
impl StencilOperation {
fn to_platform(self) -> platform_pipeline::StencilOperation {
return match self {
StencilOperation::Keep => platform_pipeline::StencilOperation::Keep,
StencilOperation::Zero => platform_pipeline::StencilOperation::Zero,
StencilOperation::Replace => platform_pipeline::StencilOperation::Replace,
StencilOperation::Invert => platform_pipeline::StencilOperation::Invert,
StencilOperation::IncrementClamp => {
platform_pipeline::StencilOperation::IncrementClamp
}
StencilOperation::DecrementClamp => {
platform_pipeline::StencilOperation::DecrementClamp
}
StencilOperation::IncrementWrap => {
platform_pipeline::StencilOperation::IncrementWrap
}
StencilOperation::DecrementWrap => {
platform_pipeline::StencilOperation::DecrementWrap
}
};
}
}
/// Engine-level per-face stencil state.
#[derive(Clone, Copy, Debug)]
pub struct StencilFaceState {
pub compare: CompareFunction,
pub fail_op: StencilOperation,
pub depth_fail_op: StencilOperation,
pub pass_op: StencilOperation,
}
impl StencilFaceState {
fn to_platform(self) -> platform_pipeline::StencilFaceState {
return platform_pipeline::StencilFaceState {
compare: self.compare.to_platform(),
fail_op: self.fail_op.to_platform(),
depth_fail_op: self.depth_fail_op.to_platform(),
pass_op: self.pass_op.to_platform(),
};
}
}
/// Engine-level full stencil state.
#[derive(Clone, Copy, Debug)]
pub struct StencilState {
pub front: StencilFaceState,
pub back: StencilFaceState,
pub read_mask: u32,
pub write_mask: u32,
}
/// Builder for creating a graphics `RenderPipeline`.
///
/// Notes
/// - The number of bind group layouts MUST NOT exceed the device limit; the
/// builder asserts this against the current device.
/// - If a fragment shader is omitted, no color target is attached and the
/// pipeline can still be used for vertex‑only workloads.
pub struct RenderPipelineBuilder {
immediate_data: Vec<std::ops::Range<u32>>,
bindings: Vec<BufferBinding>,
culling: CullingMode,
blend_mode: BlendMode,
bind_group_layouts: Vec<bind::BindGroupLayout>,
label: Option<String>,
use_depth: bool,
depth_format: Option<texture::DepthFormat>,
sample_count: u32,
depth_compare: Option<CompareFunction>,
stencil: Option<platform_pipeline::StencilState>,
depth_write_enabled: Option<bool>,
}
impl Default for RenderPipelineBuilder {
fn default() -> Self {
return Self::new();
}
}
impl RenderPipelineBuilder {
/// Creates a new render pipeline builder.
pub fn new() -> Self {
Self {
immediate_data: Vec::new(),
bindings: Vec::new(),
culling: CullingMode::Back,
blend_mode: BlendMode::None,
bind_group_layouts: Vec::new(),
label: None,
use_depth: false,
depth_format: None,
sample_count: 1,
depth_compare: None,
stencil: None,
depth_write_enabled: None,
}
}
/// Declare a vertex buffer and the vertex attributes consumed by the shader.
pub fn with_buffer(
self,
buffer: Buffer,
attributes: Vec<VertexAttribute>,
) -> Self {
return self.with_buffer_step_mode(
buffer,
attributes,
VertexStepMode::PerVertex,
);
}
/// Declare a vertex buffer with an explicit step mode.
pub fn with_buffer_step_mode(
mut self,
buffer: Buffer,
attributes: Vec<VertexAttribute>,
step_mode: VertexStepMode,
) -> Self {
#[cfg(any(debug_assertions, feature = "render-validation-encoder",))]
{
if buffer.buffer_type() != BufferType::Vertex {
logging::error!(
"RenderPipelineBuilder::with_buffer called with a non-vertex buffer type {:?}; expected BufferType::Vertex",
buffer.buffer_type()
);
}
}
let layout = VertexBufferLayout {
stride: buffer.stride(),
step_mode,
};
self.bindings.push(BufferBinding {
buffer: Rc::new(buffer),
layout,
attributes,
});
return self;
}
/// Declare a per-instance vertex buffer.
pub fn with_instance_buffer(
self,
buffer: Buffer,
attributes: Vec<VertexAttribute>,
) -> Self {
return self.with_buffer_step_mode(
buffer,
attributes,
VertexStepMode::PerInstance,
);
}
/// Declare an immediate data byte range size.
///
/// wgpu v28 uses a single immediate data region sized by the pipeline
/// layout. This method records a range starting at 0 whose end defines the
/// required allocation size. Multiple calls are allowed; the final
/// allocation is derived from the union of ranges.
pub fn with_immediate_data(mut self, bytes: u32) -> Self {
self.immediate_data.push(0..bytes);
return self;
}
/// Attach a debug label to the pipeline.
pub fn with_label(mut self, label: &str) -> Self {
self.label = Some(label.to_string());
return self;
}
/// Configure triangle face culling. Defaults to culling back faces.
pub fn with_culling(mut self, mode: CullingMode) -> Self {
self.culling = mode;
return self;
}
/// Configure blending for the pipeline's color target.
///
/// Defaults to `BlendMode::None` (opaque).
pub fn with_blend(mut self, mode: BlendMode) -> Self {
self.blend_mode = mode;
return self;
}
/// Provide one or more bind group layouts used to create the pipeline layout.
pub fn with_layouts(mut self, layouts: &[&bind::BindGroupLayout]) -> Self {
self.bind_group_layouts = layouts.iter().map(|l| (*l).clone()).collect();
return self;
}
/// Enable depth testing/writes using the render context's depth format.
pub fn with_depth(mut self) -> Self {
self.use_depth = true;
return self;
}
/// Enable depth with an explicit depth format.
pub fn with_depth_format(mut self, format: texture::DepthFormat) -> Self {
self.use_depth = true;
self.depth_format = Some(format);
return self;
}
/// Configure multi-sampling for this pipeline.
pub fn with_multi_sample(mut self, samples: u32) -> Self {
// Always apply a cheap validity check; log under feature/debug gates.
if matches!(samples, 1 | 2 | 4 | 8) {
self.sample_count = samples;
} else {
#[cfg(any(debug_assertions, feature = "render-validation-msaa",))]
{
if let Err(msg) = validation::validate_sample_count(samples) {
logging::error!(
"{}; falling back to sample_count=1 for pipeline",
msg
);
}
}
self.sample_count = 1;
}
return self;
}
/// Set a non-default depth compare function.
pub fn with_depth_compare(mut self, compare: CompareFunction) -> Self {
self.depth_compare = Some(compare);
return self;
}
/// Configure stencil state for the pipeline using engine types.
pub fn with_stencil(mut self, stencil: StencilState) -> Self {
let mapped = platform_pipeline::StencilState {
front: stencil.front.to_platform(),
back: stencil.back.to_platform(),
read_mask: stencil.read_mask,
write_mask: stencil.write_mask,
};
self.stencil = Some(mapped);
return self;
}
/// Enable or disable depth writes for this pipeline.
pub fn with_depth_write(mut self, enabled: bool) -> Self {
self.depth_write_enabled = Some(enabled);
return self;
}
/// Build a graphics pipeline using the provided shader modules and
/// previously registered vertex inputs and immediate data.
///
/// # Arguments
/// * `gpu` - The GPU device to create the pipeline on.
/// * `surface_format` - The texture format of the render target surface.
/// * `depth_format` - The depth format for depth/stencil operations.
/// * `render_pass` - The render pass this pipeline will be used with.
/// * `vertex_shader` - The vertex shader module.
/// * `fragment_shader` - Optional fragment shader module.
pub fn build(
self,
gpu: &Gpu,
surface_format: texture::TextureFormat,
depth_format: texture::DepthFormat,
render_pass: &RenderPass,
vertex_shader: &Shader,
fragment_shader: Option<&Shader>,
) -> RenderPipeline {
// Shader modules
let vertex_module = platform_pipeline::ShaderModule::from_spirv(
gpu.platform(),
vertex_shader.binary(),
Some("lambda-vertex-shader"),
);
let fragment_module = fragment_shader.map(|shader| {
platform_pipeline::ShaderModule::from_spirv(
gpu.platform(),
shader.binary(),
Some("lambda-fragment-shader"),
)
});
// Immediate data ranges
let immediate_data_ranges: Vec<platform_pipeline::ImmediateDataRange> =
self
.immediate_data
.iter()
.map(|range| platform_pipeline::ImmediateDataRange {
range: range.clone(),
})
.collect();
// Bind group layouts limit check
let max_bind_groups = gpu.limit_max_bind_groups() as usize;
if self.bind_group_layouts.len() > max_bind_groups {
logging::error!(
"Pipeline declares {} bind group layouts, exceeds device max {}",
self.bind_group_layouts.len(),
max_bind_groups
);
}
debug_assert!(
self.bind_group_layouts.len() <= max_bind_groups,
"Pipeline declares {} bind group layouts, exceeds device max {}",
self.bind_group_layouts.len(),
max_bind_groups
);
// Vertex buffer slot and attribute count limit checks.
let max_vertex_buffers = gpu.limit_max_vertex_buffers() as usize;
if self.bindings.len() > max_vertex_buffers {
logging::error!(
"Pipeline declares {} vertex buffers, exceeds device max {}",
self.bindings.len(),
max_vertex_buffers
);
}
debug_assert!(
self.bindings.len() <= max_vertex_buffers,
"Pipeline declares {} vertex buffers, exceeds device max {}",
self.bindings.len(),
max_vertex_buffers
);
let total_vertex_attributes: usize = self
.bindings
.iter()
.map(|binding| binding.attributes.len())
.sum();
let max_vertex_attributes = gpu.limit_max_vertex_attributes() as usize;
if total_vertex_attributes > max_vertex_attributes {
logging::error!(
"Pipeline declares {} vertex attributes across all vertex buffers, exceeds device max {}",
total_vertex_attributes,
max_vertex_attributes
);
}
debug_assert!(
total_vertex_attributes <= max_vertex_attributes,
"Pipeline declares {} vertex attributes across all vertex buffers, exceeds device max {}",
total_vertex_attributes,
max_vertex_attributes
);
// Pipeline layout via platform
let bgl_platform: Vec<&lambda_platform::wgpu::bind::BindGroupLayout> = self
.bind_group_layouts
.iter()
.map(|l| l.platform_layout())
.collect();
let pipeline_layout = platform_pipeline::PipelineLayoutBuilder::new()
.with_label("lambda-pipeline-layout")
.with_layouts(&bgl_platform)
.with_immediate_data_ranges(immediate_data_ranges)
.build(gpu.platform());
// Vertex buffers and attributes
let mut buffers = Vec::with_capacity(self.bindings.len());
let mut per_instance_slots = Vec::with_capacity(self.bindings.len());
let mut rp_builder = platform_pipeline::RenderPipelineBuilder::new()
.with_label(self.label.as_deref().unwrap_or("lambda-render-pipeline"))
.with_layout(&pipeline_layout)
.with_cull_mode(self.culling.to_platform())
.with_blend(self.blend_mode.to_platform());
for binding in &self.bindings {
let attributes: Vec<platform_pipeline::VertexAttributeDesc> = binding
.attributes
.iter()
.map(|attribute| platform_pipeline::VertexAttributeDesc {
shader_location: attribute.location,
offset: (attribute.offset + attribute.element.offset) as u64,
format: attribute.element.format.to_platform(),
})
.collect();
rp_builder = rp_builder.with_vertex_buffer_step_mode(
binding.layout.stride,
to_platform_step_mode(binding.layout.step_mode),
attributes,
);
buffers.push(binding.buffer.clone());
per_instance_slots.push(matches!(
binding.layout.step_mode,
VertexStepMode::PerInstance
));
}
if fragment_module.is_some() {
rp_builder = rp_builder.with_color_target(surface_format.to_platform());
}
let pipeline_color_target_format = if fragment_module.is_some() {
Some(surface_format)
} else {
None
};
let mut pipeline_depth_format: Option<texture::DepthFormat> = None;
if self.use_depth {
// Engine-level depth format with default
let mut dfmt = self
.depth_format
.unwrap_or(texture::DepthFormat::Depth32Float);
// If stencil state is configured, ensure a stencil-capable depth format.
if self.stencil.is_some()
&& dfmt != texture::DepthFormat::Depth24PlusStencil8
{
#[cfg(any(debug_assertions, feature = "render-validation-stencil",))]
logging::error!(
"Stencil configured but depth format {:?} lacks stencil; upgrading to Depth24PlusStencil8",
dfmt
);
dfmt = texture::DepthFormat::Depth24PlusStencil8;
}
let requested_depth_format = dfmt.to_platform();
// Derive the pass attachment depth format from pass configuration.
let pass_has_stencil = render_pass.stencil_operations().is_some();
let pass_depth_format = if pass_has_stencil {
texture::DepthFormat::Depth24PlusStencil8
} else {
depth_format
};
pipeline_depth_format = Some(pass_depth_format);
// Align the pipeline depth format with the pass attachment format to
// avoid hidden global state on the render context. When formats differ,
// prefer the pass attachment format and log for easier debugging.
let final_depth_format = if requested_depth_format
!= pass_depth_format.to_platform()
{
#[cfg(any(
debug_assertions,
feature = "render-validation-depth",
feature = "render-validation-stencil",
))]
logging::error!(
"Render pipeline depth format {:?} does not match pass depth attachment format {:?}; aligning pipeline to pass format",
requested_depth_format,
pass_depth_format
);
pass_depth_format.to_platform()
} else {
pass_depth_format.to_platform()
};
rp_builder = rp_builder.with_depth_stencil(final_depth_format);
if let Some(compare) = self.depth_compare {
rp_builder = rp_builder.with_depth_compare(compare.to_platform());
}
if let Some(stencil) = self.stencil {
rp_builder = rp_builder.with_stencil(stencil);
}
if let Some(enabled) = self.depth_write_enabled {
rp_builder = rp_builder.with_depth_write_enabled(enabled);
}
}
// Apply multi-sampling to the pipeline.
// Always align to the pass sample count; gate logs.
let mut pipeline_samples = self.sample_count;
let pass_samples = render_pass.sample_count();
if pipeline_samples != pass_samples {
#[cfg(any(debug_assertions, feature = "render-validation-msaa",))]
logging::error!(
"Pipeline sample_count={} does not match pass sample_count={}; aligning to pass",
pipeline_samples,
pass_samples
);
pipeline_samples = pass_samples;
}
if !matches!(pipeline_samples, 1 | 2 | 4 | 8) {
#[cfg(any(debug_assertions, feature = "render-validation-msaa",))]
{
let _ = validation::validate_sample_count(pipeline_samples);
}
pipeline_samples = 1;
}
rp_builder = rp_builder.with_sample_count(pipeline_samples);
let pipeline = rp_builder.build(
gpu.platform(),
&vertex_module,
fragment_module.as_ref(),
);
return RenderPipeline {
pipeline: Rc::new(pipeline),
buffers,
sample_count: pipeline_samples,
color_target_count: if fragment_module.is_some() { 1 } else { 0 },
color_target_format: pipeline_color_target_format,
// Depth/stencil is enabled when `with_depth*` was called on the builder.
expects_depth_stencil: self.use_depth,
depth_format: pipeline_depth_format,
uses_stencil: self.stencil.is_some(),
per_instance_slots,
};
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Ensures vertex step modes map to the platform vertex step modes.
#[test]
fn engine_step_mode_maps_to_platform_step_mode() {
let per_vertex = to_platform_step_mode(VertexStepMode::PerVertex);
let per_instance = to_platform_step_mode(VertexStepMode::PerInstance);
assert!(matches!(
per_vertex,
platform_pipeline::VertexStepMode::Vertex
));
assert!(matches!(
per_instance,
platform_pipeline::VertexStepMode::Instance
));
}
/// Ensures depth compare functions map to the platform compare functions.
#[test]
fn compare_function_maps_to_platform() {
assert!(matches!(
CompareFunction::Less.to_platform(),
platform_pipeline::CompareFunction::Less
));
assert!(matches!(
CompareFunction::Always.to_platform(),
platform_pipeline::CompareFunction::Always
));
}
/// Ensures culling mode configuration maps to the platform culling modes.
#[test]
fn culling_mode_maps_to_platform() {
assert!(matches!(
CullingMode::None.to_platform(),
platform_pipeline::CullingMode::None
));
assert!(matches!(
CullingMode::Back.to_platform(),
platform_pipeline::CullingMode::Back
));
}
/// Ensures blend modes default to `None` and map to platform blend modes.
#[test]
fn blend_mode_defaults_and_maps_to_platform() {
let builder = RenderPipelineBuilder::new();
assert!(matches!(builder.blend_mode, BlendMode::None));
assert!(matches!(
BlendMode::None.to_platform(),
platform_pipeline::BlendMode::None
));
assert!(matches!(
BlendMode::AlphaBlending.to_platform(),
platform_pipeline::BlendMode::AlphaBlending
));
assert!(matches!(
BlendMode::PremultipliedAlpha.to_platform(),
platform_pipeline::BlendMode::PremultipliedAlpha
));
assert!(matches!(
BlendMode::Additive.to_platform(),
platform_pipeline::BlendMode::Additive
));
}
/// Ensures invalid MSAA sample counts are clamped/fallen back to `1`.
#[test]
fn pipeline_builder_invalid_sample_count_falls_back_to_one() {
let builder = RenderPipelineBuilder::new().with_multi_sample(3);
assert_eq!(builder.sample_count, 1);
}
/// Builds a pipeline with depth+stencil enabled and both per-vertex and
/// per-instance buffers, covering format upgrade and instance slot tracking.
#[test]
fn pipeline_builds_with_depth_stencil_and_instance_layout() {
use crate::render::{
bind::{
BindGroupLayoutBuilder,
BindingVisibility,
},
buffer::{
BufferBuilder,
BufferType,
Properties,
Usage,
},
gpu::create_test_gpu,
render_pass::RenderPassBuilder,
shader::{
ShaderBuilder,
ShaderKind,
VirtualShader,
},
texture::{
DepthFormat,
TextureFormat,
},
vertex::{
ColorFormat,
VertexAttribute,
VertexElement,
},
};
let Some(gpu) = create_test_gpu("lambda-pipeline-depth-test") else {
return;
};
let mut shaders = ShaderBuilder::new();
let vs = shaders.build(VirtualShader::Source {
source: r#"
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) in vec3 a_pos;
layout(location = 1) in vec3 a_inst;
void main() { gl_Position = vec4(a_pos + a_inst * 0.0, 1.0); }
"#
.to_string(),
kind: ShaderKind::Vertex,
name: "lambda-pipeline-depth-vs".to_string(),
entry_point: "main".to_string(),
});
let fs = shaders.build(VirtualShader::Source {
source: r#"
#version 450
#extension GL_ARB_separate_shader_objects : enable
layout(location = 0) out vec4 fragment_color;
void main() { fragment_color = vec4(1.0); }
"#
.to_string(),
kind: ShaderKind::Fragment,
name: "lambda-pipeline-depth-fs".to_string(),
entry_point: "main".to_string(),
});
let pass = RenderPassBuilder::new()
.with_label("lambda-pipeline-depth-pass")
.with_depth()
.with_stencil()
.build(&gpu, TextureFormat::Rgba8Unorm, DepthFormat::Depth24Plus);
let layout = BindGroupLayoutBuilder::new()
.with_uniform(0, BindingVisibility::VertexAndFragment)
.build(&gpu);
let vertex_buffer = BufferBuilder::new()
.with_label("lambda-pipeline-depth-vertex")
.with_usage(Usage::VERTEX)
.with_properties(Properties::CPU_VISIBLE)
.with_buffer_type(BufferType::Vertex)
.build(&gpu, vec![[0.0_f32; 3]; 3])
.expect("build vertex buffer");
let instance_buffer = BufferBuilder::new()
.with_label("lambda-pipeline-depth-instance")
.with_usage(Usage::VERTEX)
.with_properties(Properties::CPU_VISIBLE)
.with_buffer_type(BufferType::Vertex)
.build(&gpu, vec![[0.0_f32; 3]; 1])
.expect("build instance buffer");
let attrs_pos = vec![VertexAttribute {
location: 0,
offset: 0,
element: VertexElement {
format: ColorFormat::Rgb32Sfloat,
offset: 0,
},
}];
let attrs_inst = vec![VertexAttribute {
location: 1,
offset: 0,
element: VertexElement {
format: ColorFormat::Rgb32Sfloat,
offset: 0,
},
}];
let stencil = StencilState {
front: StencilFaceState {
compare: CompareFunction::Always,
fail_op: StencilOperation::Keep,
depth_fail_op: StencilOperation::Keep,
pass_op: StencilOperation::Replace,
},
back: StencilFaceState {
compare: CompareFunction::Always,
fail_op: StencilOperation::Keep,
depth_fail_op: StencilOperation::Keep,
pass_op: StencilOperation::Replace,
},
read_mask: 0xff,
write_mask: 0xff,
};
// Intentionally request a mismatched depth format; build should align to the pass.
let pipeline = RenderPipelineBuilder::new()
.with_label("lambda-pipeline-depth-pipeline")
.with_layouts(&[&layout])
.with_buffer(vertex_buffer, attrs_pos)
.with_instance_buffer(instance_buffer, attrs_inst)
.with_depth_format(DepthFormat::Depth32Float)
.with_depth_compare(CompareFunction::Less)
.with_depth_write(true)
.with_stencil(stencil)
.build(
&gpu,
TextureFormat::Rgba8Unorm,
DepthFormat::Depth24Plus,
&pass,
&vs,
Some(&fs),
);
assert!(pipeline.expects_depth_stencil());
assert!(pipeline.uses_stencil());
assert_eq!(
pipeline.depth_format(),
Some(DepthFormat::Depth24PlusStencil8)
);
assert_eq!(pipeline.per_instance_slots().len(), 2);
}
/// Ensures pipeline construction aligns its MSAA sample count to the render
/// pass sample count to avoid target incompatibility.
#[test]
fn pipeline_build_aligns_sample_count_to_render_pass() {
use crate::render::{
buffer::{
BufferBuilder,
BufferType,
Properties,
Usage,
},
gpu::create_test_gpu,
render_pass::RenderPassBuilder,
shader::{
ShaderBuilder,
ShaderKind,
VirtualShader,
},
texture::{
DepthFormat,
TextureFormat,
},
vertex::{
ColorFormat,
VertexAttribute,
VertexElement,
},
};
let Some(gpu) = create_test_gpu("lambda-pipeline-test") else {
return;
};
let vert_path = format!(
"{}/assets/shaders/triangle.vert",
env!("CARGO_MANIFEST_DIR")
);
let frag_path = format!(