@@ -81,6 +81,95 @@ pub struct VertexAttributeDesc {
8181 pub format : ColorFormat ,
8282}
8383
84+ /// Compare function used for depth and stencil tests.
85+ #[ derive( Clone , Copy , Debug ) ]
86+ pub enum CompareFunction {
87+ Never ,
88+ Less ,
89+ LessEqual ,
90+ Greater ,
91+ GreaterEqual ,
92+ Equal ,
93+ NotEqual ,
94+ Always ,
95+ }
96+
97+ impl CompareFunction {
98+ fn to_wgpu ( self ) -> wgpu:: CompareFunction {
99+ match self {
100+ CompareFunction :: Never => wgpu:: CompareFunction :: Never ,
101+ CompareFunction :: Less => wgpu:: CompareFunction :: Less ,
102+ CompareFunction :: LessEqual => wgpu:: CompareFunction :: LessEqual ,
103+ CompareFunction :: Greater => wgpu:: CompareFunction :: Greater ,
104+ CompareFunction :: GreaterEqual => wgpu:: CompareFunction :: GreaterEqual ,
105+ CompareFunction :: Equal => wgpu:: CompareFunction :: Equal ,
106+ CompareFunction :: NotEqual => wgpu:: CompareFunction :: NotEqual ,
107+ CompareFunction :: Always => wgpu:: CompareFunction :: Always ,
108+ }
109+ }
110+ }
111+
112+ /// Stencil operation applied when the stencil test or depth test passes/fails.
113+ #[ derive( Clone , Copy , Debug ) ]
114+ pub enum StencilOperation {
115+ Keep ,
116+ Zero ,
117+ Replace ,
118+ Invert ,
119+ IncrementClamp ,
120+ DecrementClamp ,
121+ IncrementWrap ,
122+ DecrementWrap ,
123+ }
124+
125+ impl StencilOperation {
126+ fn to_wgpu ( self ) -> wgpu:: StencilOperation {
127+ match self {
128+ StencilOperation :: Keep => wgpu:: StencilOperation :: Keep ,
129+ StencilOperation :: Zero => wgpu:: StencilOperation :: Zero ,
130+ StencilOperation :: Replace => wgpu:: StencilOperation :: Replace ,
131+ StencilOperation :: Invert => wgpu:: StencilOperation :: Invert ,
132+ StencilOperation :: IncrementClamp => {
133+ wgpu:: StencilOperation :: IncrementClamp
134+ }
135+ StencilOperation :: DecrementClamp => {
136+ wgpu:: StencilOperation :: DecrementClamp
137+ }
138+ StencilOperation :: IncrementWrap => wgpu:: StencilOperation :: IncrementWrap ,
139+ StencilOperation :: DecrementWrap => wgpu:: StencilOperation :: DecrementWrap ,
140+ }
141+ }
142+ }
143+
144+ /// Per-face stencil state.
145+ #[ derive( Clone , Copy , Debug ) ]
146+ pub struct StencilFaceState {
147+ pub compare : CompareFunction ,
148+ pub fail_op : StencilOperation ,
149+ pub depth_fail_op : StencilOperation ,
150+ pub pass_op : StencilOperation ,
151+ }
152+
153+ impl StencilFaceState {
154+ fn to_wgpu ( self ) -> wgpu:: StencilFaceState {
155+ wgpu:: StencilFaceState {
156+ compare : self . compare . to_wgpu ( ) ,
157+ fail_op : self . fail_op . to_wgpu ( ) ,
158+ depth_fail_op : self . depth_fail_op . to_wgpu ( ) ,
159+ pass_op : self . pass_op . to_wgpu ( ) ,
160+ }
161+ }
162+ }
163+
164+ /// Full stencil state (front/back + masks).
165+ #[ derive( Clone , Copy , Debug ) ]
166+ pub struct StencilState {
167+ pub front : StencilFaceState ,
168+ pub back : StencilFaceState ,
169+ pub read_mask : u32 ,
170+ pub write_mask : u32 ,
171+ }
172+
84173/// Wrapper around `wgpu::ShaderModule` that preserves a label.
85174#[ derive( Debug ) ]
86175pub struct ShaderModule {
@@ -202,6 +291,10 @@ impl RenderPipeline {
202291 pub ( crate ) fn into_raw ( self ) -> wgpu:: RenderPipeline {
203292 return self . raw ;
204293 }
294+ /// Pipeline label if provided.
295+ pub fn label ( & self ) -> Option < & str > {
296+ return self . label . as_deref ( ) ;
297+ }
205298}
206299
207300/// Builder for creating a graphics render pipeline.
@@ -212,6 +305,7 @@ pub struct RenderPipelineBuilder<'a> {
212305 cull_mode : CullingMode ,
213306 color_target_format : Option < wgpu:: TextureFormat > ,
214307 depth_stencil : Option < wgpu:: DepthStencilState > ,
308+ sample_count : u32 ,
215309}
216310
217311impl < ' a > RenderPipelineBuilder < ' a > {
@@ -224,6 +318,7 @@ impl<'a> RenderPipelineBuilder<'a> {
224318 cull_mode : CullingMode :: Back ,
225319 color_target_format : None ,
226320 depth_stencil : None ,
321+ sample_count : 1 ,
227322 } ;
228323 }
229324
@@ -275,6 +370,56 @@ impl<'a> RenderPipelineBuilder<'a> {
275370 return self ;
276371 }
277372
373+ /// Set the depth compare function. Requires depth to be enabled.
374+ pub fn with_depth_compare ( mut self , compare : CompareFunction ) -> Self {
375+ let ds = self . depth_stencil . get_or_insert ( wgpu:: DepthStencilState {
376+ format : wgpu:: TextureFormat :: Depth32Float ,
377+ depth_write_enabled : true ,
378+ depth_compare : wgpu:: CompareFunction :: Less ,
379+ stencil : wgpu:: StencilState :: default ( ) ,
380+ bias : wgpu:: DepthBiasState :: default ( ) ,
381+ } ) ;
382+ ds. depth_compare = compare. to_wgpu ( ) ;
383+ return self ;
384+ }
385+
386+ /// Enable or disable depth writes. Requires depth-stencil enabled.
387+ pub fn with_depth_write_enabled ( mut self , enabled : bool ) -> Self {
388+ let ds = self . depth_stencil . get_or_insert ( wgpu:: DepthStencilState {
389+ format : wgpu:: TextureFormat :: Depth32Float ,
390+ depth_write_enabled : true ,
391+ depth_compare : wgpu:: CompareFunction :: Less ,
392+ stencil : wgpu:: StencilState :: default ( ) ,
393+ bias : wgpu:: DepthBiasState :: default ( ) ,
394+ } ) ;
395+ ds. depth_write_enabled = enabled;
396+ return self ;
397+ }
398+
399+ /// Configure stencil state (front/back ops and masks). Requires depth-stencil enabled.
400+ pub fn with_stencil ( mut self , stencil : StencilState ) -> Self {
401+ let ds = self . depth_stencil . get_or_insert ( wgpu:: DepthStencilState {
402+ format : wgpu:: TextureFormat :: Depth24PlusStencil8 ,
403+ depth_write_enabled : true ,
404+ depth_compare : wgpu:: CompareFunction :: Less ,
405+ stencil : wgpu:: StencilState :: default ( ) ,
406+ bias : wgpu:: DepthBiasState :: default ( ) ,
407+ } ) ;
408+ ds. stencil = wgpu:: StencilState {
409+ front : stencil. front . to_wgpu ( ) ,
410+ back : stencil. back . to_wgpu ( ) ,
411+ read_mask : stencil. read_mask ,
412+ write_mask : stencil. write_mask ,
413+ } ;
414+ return self ;
415+ }
416+
417+ /// Configure multisampling. Count MUST be >= 1 and supported by the device.
418+ pub fn with_sample_count ( mut self , count : u32 ) -> Self {
419+ self . sample_count = count. max ( 1 ) ;
420+ return self ;
421+ }
422+
278423 /// Build the render pipeline from provided shader modules.
279424 pub fn build (
280425 self ,
@@ -351,7 +496,10 @@ impl<'a> RenderPipelineBuilder<'a> {
351496 vertex : vertex_state,
352497 primitive : primitive_state,
353498 depth_stencil : self . depth_stencil ,
354- multisample : wgpu:: MultisampleState :: default ( ) ,
499+ multisample : wgpu:: MultisampleState {
500+ count : self . sample_count ,
501+ ..wgpu:: MultisampleState :: default ( )
502+ } ,
355503 fragment,
356504 multiview : None ,
357505 cache : None ,
0 commit comments