Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when hdr is enabled #22

Merged
merged 2 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/bloom.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! This example shows that bevy_smud works with bloom enabled
//!
//! Note that you could probably achieve chieper and higher quality bloom-like
//! effects by creating a custom fill.
use bevy::{core_pipeline::bloom::BloomSettings, prelude::*};
// The prelude contains the basic things needed to create shapes
use bevy_smud::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(SmudPlugin)
.add_startup_system(setup)
.run();
}

fn setup(mut commands: Commands, mut shaders: ResMut<Assets<Shader>>) {
// add_sdf_expr expects a wgsl expression
// p is the position of a fragment within the sdf shape, with 0, 0 at the center.
// Here we are using the built-in sd_circle function, which accepts the
// radius as a parameter.
let circle = shaders.add_sdf_expr("sd_circle(p, 70.)");

commands.spawn(ShapeBundle {
shape: SmudShape {
color: Color::TOMATO,
sdf: circle,
// The frame needs to be bigger than the shape we're drawing
// Since the circle has radius 70, we make the half-size of the quad 80.
frame: Frame::Quad(80.),
fill: SIMPLE_FILL_HANDLE.typed(),
..default()
},
..default()
});

commands.spawn((
Camera2dBundle {
camera: Camera {
hdr: true,
..default()
},
..default()
},
BloomSettings {
threshold: 0.2,
..default()
},
));
}
21 changes: 17 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ use bevy::{
},
renderer::{RenderDevice, RenderQueue},
texture::BevyDefault,
view::{ViewUniform, ViewUniformOffset, ViewUniforms, VisibleEntities},
view::{
ExtractedView, ViewTarget, ViewUniform, ViewUniformOffset, ViewUniforms,
VisibleEntities,
},
Extract, MainWorld, RenderApp, RenderStage,
},
sprite::Mesh2dPipelineKey,
Expand Down Expand Up @@ -185,6 +188,7 @@ impl FromWorld for SmudPipeline {
struct SmudPipelineKey {
mesh: Mesh2dPipelineKey,
shader: (HandleId, HandleId),
hdr: bool,
}

impl SpecializedRenderPipeline for SmudPipeline {
Expand Down Expand Up @@ -248,7 +252,11 @@ impl SpecializedRenderPipeline for SmudPipeline {
entry_point: "fragment".into(),
shader_defs: Vec::new(),
targets: vec![Some(ColorTargetState {
format: TextureFormat::bevy_default(),
format: if key.hdr {
ViewTarget::TEXTURE_FORMAT_HDR
} else {
TextureFormat::bevy_default()
},
blend: Some(BlendState::ALPHA_BLENDING),
write_mask: ColorWrites::ALL,
})],
Expand Down Expand Up @@ -385,7 +393,11 @@ fn extract_shapes(

fn queue_shapes(
mut commands: Commands,
mut views: Query<(&mut RenderPhase<Transparent2d>, &VisibleEntities)>,
mut views: Query<(
&mut RenderPhase<Transparent2d>,
&ExtractedView,
&VisibleEntities,
)>,
mut pipelines: ResMut<SpecializedRenderPipelines<SmudPipeline>>,
mut pipeline_cache: ResMut<PipelineCache>,
mut extracted_shapes: ResMut<ExtractedShapes>, // todo needs mut?
Expand Down Expand Up @@ -434,7 +446,7 @@ fn queue_shapes(
let shape_meta = &mut shape_meta;

// Iterate over each view (a camera is a view)
for (mut transparent_phase, _visible_entities) in views.iter_mut() {
for (mut transparent_phase, view, _visible_entities) in views.iter_mut() {
// todo: check visible entities?

let extracted_shapes = &mut extracted_shapes.0;
Expand Down Expand Up @@ -490,6 +502,7 @@ fn queue_shapes(
let specialize_key = SmudPipelineKey {
mesh: mesh_key,
shader: current_batch.shader,
hdr: view.hdr,
};
current_batch_pipeline =
pipelines.specialize(&mut pipeline_cache, &smud_pipeline, specialize_key);
Expand Down