@@ -22,7 +22,7 @@ use bevy_render::{
22
22
render_resource:: { std140:: AsStd140 , * } ,
23
23
renderer:: { RenderDevice , RenderQueue } ,
24
24
texture:: { BevyDefault , Image } ,
25
- view:: { ComputedVisibility , ExtractedView , ViewUniform , ViewUniformOffset , ViewUniforms } ,
25
+ view:: { ComputedVisibility , ExtractedView , Msaa , ViewUniform , ViewUniformOffset , ViewUniforms } ,
26
26
RenderWorld ,
27
27
} ;
28
28
use bevy_transform:: components:: GlobalTransform ;
@@ -82,9 +82,29 @@ impl FromWorld for SpritePipeline {
82
82
}
83
83
}
84
84
85
- #[ derive( Clone , Copy , Hash , PartialEq , Eq ) ]
86
- pub struct SpritePipelineKey {
87
- colored : bool ,
85
+ bitflags:: bitflags! {
86
+ #[ repr( transparent) ]
87
+ // NOTE: Apparently quadro drivers support up to 64x MSAA.
88
+ // MSAA uses the highest 6 bits for the MSAA sample count - 1 to support up to 64x MSAA.
89
+ pub struct SpritePipelineKey : u32 {
90
+ const NONE = 0 ;
91
+ const COLORED = ( 1 << 0 ) ;
92
+ const MSAA_RESERVED_BITS = SpritePipelineKey :: MSAA_MASK_BITS << SpritePipelineKey :: MSAA_SHIFT_BITS ;
93
+ }
94
+ }
95
+
96
+ impl SpritePipelineKey {
97
+ const MSAA_MASK_BITS : u32 = 0b111111 ;
98
+ const MSAA_SHIFT_BITS : u32 = 32 - 6 ;
99
+
100
+ pub fn from_msaa_samples ( msaa_samples : u32 ) -> Self {
101
+ let msaa_bits = ( ( msaa_samples - 1 ) & Self :: MSAA_MASK_BITS ) << Self :: MSAA_SHIFT_BITS ;
102
+ SpritePipelineKey :: from_bits ( msaa_bits) . unwrap ( )
103
+ }
104
+
105
+ pub fn msaa_samples ( & self ) -> u32 {
106
+ ( ( self . bits >> Self :: MSAA_SHIFT_BITS ) & Self :: MSAA_MASK_BITS ) + 1
107
+ }
88
108
}
89
109
90
110
impl SpecializedPipeline for SpritePipeline {
@@ -108,7 +128,7 @@ impl SpecializedPipeline for SpritePipeline {
108
128
] ,
109
129
} ;
110
130
let mut shader_defs = Vec :: new ( ) ;
111
- if key. colored {
131
+ if key. contains ( SpritePipelineKey :: COLORED ) {
112
132
shader_defs. push ( "COLORED" . to_string ( ) ) ;
113
133
vertex_buffer_layout. attributes . push ( VertexAttribute {
114
134
format : VertexFormat :: Uint32 ,
@@ -147,7 +167,7 @@ impl SpecializedPipeline for SpritePipeline {
147
167
} ,
148
168
depth_stencil : None ,
149
169
multisample : MultisampleState {
150
- count : 1 ,
170
+ count : key . msaa_samples ( ) ,
151
171
mask : !0 ,
152
172
alpha_to_coverage_enabled : false ,
153
173
} ,
@@ -529,6 +549,7 @@ pub fn queue_sprites(
529
549
mut pipeline_cache : ResMut < RenderPipelineCache > ,
530
550
mut image_bind_groups : ResMut < ImageBindGroups > ,
531
551
gpu_images : Res < RenderAssets < Image > > ,
552
+ msaa : Res < Msaa > ,
532
553
sprite_batches : Query < ( Entity , & SpriteBatch ) > ,
533
554
mut views : Query < ( & ExtractedView , & mut RenderPhase < Transparent2d > ) > ,
534
555
events : Res < SpriteAssetEvents > ,
@@ -552,15 +573,12 @@ pub fn queue_sprites(
552
573
layout : & sprite_pipeline. view_layout ,
553
574
} ) ) ;
554
575
let draw_sprite_function = draw_functions. read ( ) . get_id :: < DrawSprite > ( ) . unwrap ( ) ;
555
- let pipeline = pipelines. specialize (
556
- & mut pipeline_cache,
557
- & sprite_pipeline,
558
- SpritePipelineKey { colored : false } ,
559
- ) ;
576
+ let key = SpritePipelineKey :: from_msaa_samples ( msaa. samples ) ;
577
+ let pipeline = pipelines. specialize ( & mut pipeline_cache, & sprite_pipeline, key) ;
560
578
let colored_pipeline = pipelines. specialize (
561
579
& mut pipeline_cache,
562
580
& sprite_pipeline,
563
- SpritePipelineKey { colored : true } ,
581
+ key | SpritePipelineKey :: COLORED ,
564
582
) ;
565
583
for ( view, mut transparent_phase) in views. iter_mut ( ) {
566
584
let inverse_view_matrix = view. transform . compute_matrix ( ) . inverse ( ) ;
0 commit comments