Skip to content

Commit 798a0ba

Browse files
committed
Enable MSAA in the sprite pipeline
1 parent ad1da1e commit 798a0ba

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

crates/bevy_core_pipeline/src/main_pass_2d.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bevy_ecs::prelude::*;
33
use bevy_render::{
44
render_graph::{Node, NodeRunError, RenderGraphContext, SlotInfo, SlotType},
55
render_phase::{DrawFunctions, RenderPhase, TrackedRenderPass},
6-
render_resource::{LoadOp, Operations, RenderPassColorAttachment, RenderPassDescriptor},
6+
render_resource::{LoadOp, Operations, RenderPassDescriptor},
77
renderer::RenderContext,
88
view::{ExtractedView, ViewTarget},
99
};
@@ -46,14 +46,10 @@ impl Node for MainPass2dNode {
4646

4747
let pass_descriptor = RenderPassDescriptor {
4848
label: Some("main_pass_2d"),
49-
color_attachments: &[RenderPassColorAttachment {
50-
view: &target.view,
51-
resolve_target: None,
52-
ops: Operations {
53-
load: LoadOp::Load,
54-
store: true,
55-
},
56-
}],
49+
color_attachments: &[target.get_color_attachment(Operations {
50+
load: LoadOp::Load,
51+
store: true,
52+
})],
5753
depth_stencil_attachment: None,
5854
};
5955

crates/bevy_sprite/src/render/mod.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use bevy_render::{
2222
render_resource::{std140::AsStd140, *},
2323
renderer::{RenderDevice, RenderQueue},
2424
texture::{BevyDefault, Image},
25-
view::{ComputedVisibility, ExtractedView, ViewUniform, ViewUniformOffset, ViewUniforms},
25+
view::{ComputedVisibility, ExtractedView, Msaa, ViewUniform, ViewUniformOffset, ViewUniforms},
2626
RenderWorld,
2727
};
2828
use bevy_transform::components::GlobalTransform;
@@ -82,9 +82,29 @@ impl FromWorld for SpritePipeline {
8282
}
8383
}
8484

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+
}
88108
}
89109

90110
impl SpecializedPipeline for SpritePipeline {
@@ -108,7 +128,7 @@ impl SpecializedPipeline for SpritePipeline {
108128
],
109129
};
110130
let mut shader_defs = Vec::new();
111-
if key.colored {
131+
if key.contains(SpritePipelineKey::COLORED) {
112132
shader_defs.push("COLORED".to_string());
113133
vertex_buffer_layout.attributes.push(VertexAttribute {
114134
format: VertexFormat::Uint32,
@@ -147,7 +167,7 @@ impl SpecializedPipeline for SpritePipeline {
147167
},
148168
depth_stencil: None,
149169
multisample: MultisampleState {
150-
count: 1,
170+
count: key.msaa_samples(),
151171
mask: !0,
152172
alpha_to_coverage_enabled: false,
153173
},
@@ -529,6 +549,7 @@ pub fn queue_sprites(
529549
mut pipeline_cache: ResMut<RenderPipelineCache>,
530550
mut image_bind_groups: ResMut<ImageBindGroups>,
531551
gpu_images: Res<RenderAssets<Image>>,
552+
msaa: Res<Msaa>,
532553
sprite_batches: Query<(Entity, &SpriteBatch)>,
533554
mut views: Query<(&ExtractedView, &mut RenderPhase<Transparent2d>)>,
534555
events: Res<SpriteAssetEvents>,
@@ -552,15 +573,12 @@ pub fn queue_sprites(
552573
layout: &sprite_pipeline.view_layout,
553574
}));
554575
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);
560578
let colored_pipeline = pipelines.specialize(
561579
&mut pipeline_cache,
562580
&sprite_pipeline,
563-
SpritePipelineKey { colored: true },
581+
key | SpritePipelineKey::COLORED,
564582
);
565583
for (view, mut transparent_phase) in views.iter_mut() {
566584
let inverse_view_matrix = view.transform.compute_matrix().inverse();

0 commit comments

Comments
 (0)