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
/// Internal namespace.
mod private
{
use web_sys::gpu_color_write;
use crate::*;
/// A builder for creating a `web_sys::GpuColorTargetState`.
#[ derive( Clone ) ]
pub struct ColorTargetState
{
/// The texture format of the color target.
///
/// This must match the format of the texture view used in the render pass.
///
/// Defaults to `GpuTextureFormat::Rgba8unormSrgb`.
format :GpuTextureFormat,
/// An optional blend state configuration.
///
/// This defines how the output of the fragment shader is combined with the
/// existing color in the render target. If `None`, blending is disabled.
///
/// Defaults to `None`.
blend : Option< BlendState >,
/// A bitmask that controls which color channels (R, G, B, A) can be written to.
///
/// The bitmask is a combination of `gpu_color_write::RED`, `GREEN`, `BLUE`,
/// and `ALPHA`.
///
/// Defaults to `gpu_color_write::ALL`.
write_mask : Option< u32 >
}
impl From< ColorTargetState > for web_sys::GpuColorTargetState
{
fn from( value: ColorTargetState ) -> Self
{
let state = web_sys::GpuColorTargetState::new( value.format );
if let Some( v ) = value.blend { state.set_blend( &v.into() ); }
if let Some( v ) = value.write_mask { state.set_write_mask( v ); }
state
}
}
impl ColorTargetState
{
/// Creates a new `ColorTargetState` builder with default values.
pub fn new() -> Self
{
let blend = None;
let write_mask = None;
let format = GpuTextureFormat::Rgba8unormSrgb;
ColorTargetState
{
format,
blend,
write_mask
}
}
/// Sets the color target's texture format.
pub fn format( mut self, format : GpuTextureFormat ) -> Self
{
self.format = format;
self
}
/// Sets the blend state configuration.
pub fn blend( mut self, blend : BlendState ) -> Self
{
self.blend = Some( blend );
self
}
/// Sets the write mask to allow writing to all color channels.
pub fn write_all( mut self ) -> Self
{
self.write_mask = Some( gpu_color_write::ALL );
self
}
/// Enables writing to the red channel.
pub fn write_r( mut self ) -> Self
{
self.write_mask = add_mask( self.write_mask, gpu_color_write::RED );
self
}
/// Enables writing to the green channel.
pub fn write_g( mut self ) -> Self
{
self.write_mask = add_mask( self.write_mask, gpu_color_write::GREEN );
self
}
/// Enables writing to the blue channel.
pub fn write_b( mut self ) -> Self
{
self.write_mask = add_mask( self.write_mask, gpu_color_write::BLUE );
self
}
/// Enables writing to the alpha channel.
pub fn write_a( mut self ) -> Self
{
self.write_mask = add_mask( self.write_mask, gpu_color_write::ALPHA );
self
}
}
fn add_mask( mask : Option< u32 >, value : u32 ) -> Option< u32 >
{
if mask.is_some()
{
mask.map( | m | m | value )
}
else
{
Some( value )
}
}
}
crate::mod_interface!
{
exposed use
{
ColorTargetState
};
}