@@ -75,6 +75,52 @@ impl CullingMode {
7575 }
7676}
7777
78+ /// Blend mode for the (single) color target.
79+ ///
80+ /// Notes
81+ /// - Most pipelines are opaque and should use `BlendMode::None` for best
82+ /// performance, especially on tile-based GPUs.
83+ /// - This is currently a single blend state for a single color attachment.
84+ /// Per-attachment blending for MRT is future work.
85+ #[ derive( Clone , Copy , Debug ) ]
86+ pub enum BlendMode {
87+ /// No blending; replace destination (default).
88+ None ,
89+ /// Standard alpha blending (`src_alpha`, `one_minus_src_alpha`).
90+ AlphaBlending ,
91+ /// Premultiplied alpha blending.
92+ PremultipliedAlpha ,
93+ /// Additive blending (`one`, `one`).
94+ Additive ,
95+ /// Custom blend state.
96+ Custom ( wgpu:: BlendState ) ,
97+ }
98+
99+ impl BlendMode {
100+ fn to_wgpu ( self ) -> Option < wgpu:: BlendState > {
101+ return match self {
102+ BlendMode :: None => None ,
103+ BlendMode :: AlphaBlending => Some ( wgpu:: BlendState :: ALPHA_BLENDING ) ,
104+ BlendMode :: PremultipliedAlpha => {
105+ Some ( wgpu:: BlendState :: PREMULTIPLIED_ALPHA_BLENDING )
106+ }
107+ BlendMode :: Additive => Some ( wgpu:: BlendState {
108+ color : wgpu:: BlendComponent {
109+ src_factor : wgpu:: BlendFactor :: One ,
110+ dst_factor : wgpu:: BlendFactor :: One ,
111+ operation : wgpu:: BlendOperation :: Add ,
112+ } ,
113+ alpha : wgpu:: BlendComponent {
114+ src_factor : wgpu:: BlendFactor :: One ,
115+ dst_factor : wgpu:: BlendFactor :: One ,
116+ operation : wgpu:: BlendOperation :: Add ,
117+ } ,
118+ } ) ,
119+ BlendMode :: Custom ( state) => Some ( state) ,
120+ } ;
121+ }
122+ }
123+
78124/// Description of a single vertex attribute used by a pipeline.
79125#[ derive( Clone , Copy , Debug ) ]
80126pub struct VertexAttributeDesc {
@@ -488,6 +534,7 @@ pub struct RenderPipelineBuilder<'a> {
488534 vertex_buffers : Vec < VertexBufferLayoutDesc > ,
489535 cull_mode : CullingMode ,
490536 color_target_format : Option < wgpu:: TextureFormat > ,
537+ blend_mode : BlendMode ,
491538 depth_stencil : Option < wgpu:: DepthStencilState > ,
492539 sample_count : u32 ,
493540}
@@ -507,6 +554,7 @@ impl<'a> RenderPipelineBuilder<'a> {
507554 vertex_buffers : Vec :: new ( ) ,
508555 cull_mode : CullingMode :: Back ,
509556 color_target_format : None ,
557+ blend_mode : BlendMode :: None ,
510558 depth_stencil : None ,
511559 sample_count : 1 ,
512560 } ;
@@ -565,6 +613,12 @@ impl<'a> RenderPipelineBuilder<'a> {
565613 return self ;
566614 }
567615
616+ /// Set the blend mode for the color target. Defaults to `BlendMode::None`.
617+ pub fn with_blend ( mut self , mode : BlendMode ) -> Self {
618+ self . blend_mode = mode;
619+ return self ;
620+ }
621+
568622 /// Enable depth testing/writes using the provided depth format and default compare/write settings.
569623 ///
570624 /// Defaults: compare Less, depth writes enabled, no stencil.
@@ -672,7 +726,7 @@ impl<'a> RenderPipelineBuilder<'a> {
672726 match & self . color_target_format {
673727 Some ( fmt) => vec ! [ Some ( wgpu:: ColorTargetState {
674728 format: * fmt,
675- blend: Some ( wgpu :: BlendState :: ALPHA_BLENDING ) ,
729+ blend: self . blend_mode . to_wgpu ( ) ,
676730 write_mask: wgpu:: ColorWrites :: ALL ,
677731 } ) ] ,
678732 None => Vec :: new ( ) ,
@@ -755,4 +809,23 @@ mod tests {
755809 VertexStepMode :: Vertex
756810 ) ) ;
757811 }
812+
813+ #[ test]
814+ fn render_pipeline_builder_defaults_to_no_blending ( ) {
815+ let builder = RenderPipelineBuilder :: new ( ) ;
816+ assert ! ( matches!( builder. blend_mode, BlendMode :: None ) ) ;
817+ }
818+
819+ #[ test]
820+ fn blend_mode_maps_to_wgpu_blend_state ( ) {
821+ assert_eq ! (
822+ BlendMode :: AlphaBlending . to_wgpu( ) ,
823+ Some ( wgpu:: BlendState :: ALPHA_BLENDING )
824+ ) ;
825+ assert_eq ! (
826+ BlendMode :: PremultipliedAlpha . to_wgpu( ) ,
827+ Some ( wgpu:: BlendState :: PREMULTIPLIED_ALPHA_BLENDING )
828+ ) ;
829+ assert_eq ! ( BlendMode :: None . to_wgpu( ) , None ) ;
830+ }
758831}
0 commit comments