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
/// Internal namespace.
mod private
{
use crate::*;
/// A builder for creating a `web_sys::GpuDepthStencilState`.
#[ derive( Clone ) ]
pub struct DepthStencilState
{
/// The texture format of the depth-stencil attachment.
///
/// This must match the format of the texture view used in the render pass.
///
/// Defaults to `GpuTextureFormat::Depth24plus`.
format : GpuTextureFormat,
/// The comparison function used for depth testing.
///
/// This determines whether a new fragment's depth value should be written
/// to the depth buffer based on its comparison with the existing value.
///
/// Defaults to `GpuCompareFunction::Less`.
depth_compare : GpuCompareFunction,
/// A flag to enable or disable writing to the depth buffer.
///
/// If `true`, the depth value of a fragment that passes the depth test will
/// be written to the depth buffer.
///
/// Defaults to `true`.
depth_write_enabled : bool,
/// A constant value added to the depth value of a fragment.
///
/// This is used for depth biasing to prevent "z-fighting" artifacts.
///
/// Defaults to `0`.
depth_bias : Option< i32 >,
/// The maximum depth bias value.
///
/// This clamps the depth bias to a maximum value.
///
/// Defaults to `0`.
depth_bias_clamp : Option< f32 >,
/// A scale factor applied to the depth bias.
///
/// This is based on the slope of the fragment's depth value.
///
/// Defaults to `0`.
depth_bias_slope_scale : Option< f32 >,
/// Stencil state configuration for fragments that face away from the camera.
///
/// This must be provided if stencil testing is enabled.
///
/// Defaults to `None`.
stencil_back : Option< StencilFaceState >,
/// Stencil state configuration for fragments that face towards the camera.
///
/// This must be provided if stencil testing is enabled.
///
/// Defaults to `None`.
stencil_front : Option< StencilFaceState >,
/// A bitmask that is ANDed with the stencil reference value and the value
/// in the stencil buffer during stencil testing.
///
/// Defaults to `0xFFFFFFFF`.
stencil_read_mask : Option< u32 >,
/// A bitmask that is ANDed with the stencil value before it is written to
/// the stencil buffer.
///
/// Defaults to `0xFFFFFFFF`.F
stencil_write_mask : Option< u32 >
}
impl DepthStencilState
{
/// Creates a new `DepthStencilState` with default values.
pub fn new() -> Self
{
let format = GpuTextureFormat::Depth24plus;
let depth_compare = GpuCompareFunction::Less;
let depth_write_enabled = true;
let depth_bias = None;
let depth_bias_clamp = None;
let depth_bias_slope_scale = None;
let stencil_back = None;
let stencil_front = None;
let stencil_read_mask = None;
let stencil_write_mask = None;
DepthStencilState
{
format,
depth_compare,
depth_bias,
depth_bias_clamp,
depth_bias_slope_scale,
depth_write_enabled,
stencil_back,
stencil_front,
stencil_read_mask,
stencil_write_mask
}
}
/// Sets the format of the depth-stencil texture.
pub fn format( mut self, format : GpuTextureFormat ) -> Self
{
self.format = format;
self
}
/// Sets the depth comparison function.
pub fn depth_compare( mut self, compare : GpuCompareFunction ) -> Self
{
self.depth_compare = compare;
self
}
/// Sets the constant depth bias value.
pub fn depth_bias( mut self, bias : i32 ) -> Self
{
self.depth_bias = Some( bias );
self
}
/// Sets the depth bias clamp value.
pub fn depth_bias_clamp( mut self, clamp : f32 ) -> Self
{
self.depth_bias_clamp = Some( clamp );
self
}
/// Sets the depth bias slope scale.
pub fn depth_bias_slope_scale( mut self, scale : f32 ) -> Self
{
self.depth_bias_slope_scale = Some( scale );
self
}
/// Disables writing to the depth buffer.
pub fn disable_depth_write( mut self ) -> Self
{
self.depth_write_enabled = false;
self
}
/// Sets the stencil state for back-facing fragments.
pub fn stencil_back( mut self, stencil : StencilFaceState ) -> Self
{
self.stencil_back = Some( stencil );
self
}
/// Sets the stencil state for front-facing fragments.
pub fn stencil_front( mut self, stencil : StencilFaceState ) -> Self
{
self.stencil_front = Some( stencil );
self
}
/// Sets the stencil read mask.
pub fn stencil_read_mask( mut self, mask : u32 ) -> Self
{
self.stencil_read_mask = Some( mask );
self
}
/// Sets the stencil write mask.
pub fn stencil_write_mask( mut self, mask : u32 ) -> Self
{
self.stencil_write_mask = Some( mask );
self
}
}
impl From< DepthStencilState > for web_sys::GpuDepthStencilState
{
fn from( value: DepthStencilState ) -> Self
{
let state = web_sys::GpuDepthStencilState::new( value.format );
state.set_depth_compare( value.depth_compare );
state.set_depth_write_enabled( value.depth_write_enabled );
if let Some( v ) = value.depth_bias { state.set_depth_bias( v ); }
if let Some( v ) = value.depth_bias_clamp { state.set_depth_bias_clamp( v ); }
if let Some( v ) = value.depth_bias_slope_scale { state.set_depth_bias_slope_scale( v ); }
if let Some( v ) = value.stencil_back { state.set_stencil_back( &v.into() ); }
if let Some( v ) = value.stencil_front { state.set_stencil_front( &v.into() ); }
if let Some( v ) = value.stencil_read_mask { state.set_stencil_read_mask( v ); }
if let Some( v ) = value.stencil_write_mask { state.set_stencil_write_mask( v ); }
state
}
}
}
crate::mod_interface!
{
exposed use
{
DepthStencilState
};
}