Skip to content

Commit

Permalink
Merge pull request #22 from johanhelsing/hdr
Browse files Browse the repository at this point in the history
Fix panic when hdr is enabled
  • Loading branch information
johanhelsing authored Nov 26, 2022
2 parents 5c5a8d5 + 6766712 commit e7f3a55
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
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

0 comments on commit e7f3a55

Please sign in to comment.