From 15c323fd0125e7f2007dceec742c2b11ef97ceb3 Mon Sep 17 00:00:00 2001 From: Danielhu Date: Tue, 7 May 2024 23:31:34 +0800 Subject: [PATCH 01/12] refactor: upgrade to bevy 0.13 --- Cargo.toml | 25 +++++++++++++------------ examples/bench.rs | 8 +++++--- examples/gallery.rs | 10 ++++++---- src/lib.rs | 41 ++++++++++++++++++++++------------------- 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c7beaa1..b4361bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,29 +10,30 @@ repository = "https://github.com/johanhelsing/bevy_smud" version = "0.7.0" [dependencies] -bevy = { version = "0.12", default-features = false, features = [ +bevy = { version = "0.13", default-features = false, features = [ "bevy_core_pipeline", "bevy_render", - "bevy_asset", # needed for handle ids -]} -bytemuck = { version = "1.7", features = ["derive"] } + "bevy_asset", # needed for handle ids +] } +bytemuck = { version = "1.15.0", features = ["derive"] } copyless = "0.1" -bitflags = "2.4" -fixedbitset = "0.4" +bitflags = "2.5" +fixedbitset = "0.5" [dev-dependencies] -bevy = { version = "0.12", default-features = false, features = [ +bevy = { version = "0.13.2", default-features = false, features = [ "bevy_winit", - "x11", # github actions runners don't have libxkbcommon installed, so can't use wayland + "x11", # github actions runners don't have libxkbcommon installed, so can't use wayland "file_watcher", + "multi-threaded", ] } -bevy_asset_loader = "0.18" -bevy_lospec = "0.6" -bevy_pancam = "0.10" +bevy_asset_loader = "0.20" +bevy_lospec = "0.7" +bevy_pancam = "0.11" rand = "0.8" [profile.dev] opt-level = 1 [profile.dev.package."*"] -opt-level = 3 \ No newline at end of file +opt-level = 3 diff --git a/examples/bench.rs b/examples/bench.rs index 5ce0654..0419361 100644 --- a/examples/bench.rs +++ b/examples/bench.rs @@ -9,14 +9,15 @@ use rand::prelude::*; fn main() { App::new() - .add_state::() + .init_state::() // bevy_smud comes with anti-aliasing built into the standards fills // which is more efficient than MSAA, and also works on Linux, wayland .insert_resource(Msaa::Off) .add_loading_state( - LoadingState::new(GameState::Loading).continue_to_state(GameState::Running), + LoadingState::new(GameState::Loading) + .continue_to_state(GameState::Running) + .load_collection::(), ) - .add_collection_to_loading_state::<_, AssetHandles>(GameState::Loading) .add_plugins(( DefaultPlugins, LogDiagnosticsPlugin::default(), @@ -43,6 +44,7 @@ struct AssetHandles { palette: Handle, } +#[allow(dead_code)] #[derive(Component)] struct Index(usize); diff --git a/examples/gallery.rs b/examples/gallery.rs index 76591b0..a93b2b0 100644 --- a/examples/gallery.rs +++ b/examples/gallery.rs @@ -6,14 +6,15 @@ use rand::prelude::*; fn main() { App::new() - .add_state::() + .init_state::() // bevy_smud comes with anti-aliasing built into the standards fills // which is more efficient than MSAA, and also works on Linux, wayland .insert_resource(Msaa::Off) .add_loading_state( - LoadingState::new(GameState::Loading).continue_to_state(GameState::Running), + LoadingState::new(GameState::Loading) + .continue_to_state(GameState::Running) + .load_collection::(), ) - .add_collection_to_loading_state::<_, AssetHandles>(GameState::Loading) .add_plugins(( DefaultPlugins, SmudPlugin, @@ -39,6 +40,7 @@ struct AssetHandles { palette: Handle, } +#[allow(dead_code)] #[derive(Component)] struct Index(usize); @@ -85,7 +87,7 @@ fn setup( asset_server.load("gallery/donut.wgsl"), ]; - let fills = vec![ + let fills = [ // asset_server.load("fills/simple.wgsl"), asset_server.load("fills/cubic_falloff.wgsl"), asset_server.load("fills/outline.wgsl"), diff --git a/src/lib.rs b/src/lib.rs index 68d8797..6e33966 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ use std::ops::Range; use bevy::{ core_pipeline::core_2d::Transparent2d, ecs::{ + entity::EntityHashMap, query::ROQueryItem, system::{ lifetimeless::{Read, SRes}, @@ -22,11 +23,11 @@ use bevy::{ RenderPhase, SetItemPipeline, TrackedRenderPass, }, render_resource::{ - BindGroup, BindGroupEntries, BindGroupLayout, BindGroupLayoutDescriptor, - BindGroupLayoutEntry, BindingType, BlendState, BufferBindingType, BufferUsages, - BufferVec, CachedRenderPipelineId, ColorTargetState, ColorWrites, Face, FragmentState, - FrontFace, MultisampleState, PipelineCache, PolygonMode, PrimitiveState, - PrimitiveTopology, RenderPipelineDescriptor, ShaderImport, ShaderStages, ShaderType, + BindGroup, BindGroupEntries, BindGroupLayout, BindGroupLayoutEntry, BindingType, + BlendState, BufferBindingType, BufferUsages, BufferVec, CachedRenderPipelineId, + ColorTargetState, ColorWrites, Face, FragmentState, FrontFace, MultisampleState, + PipelineCache, PolygonMode, PrimitiveState, PrimitiveTopology, + RenderPipelineDescriptor, ShaderImport, ShaderStages, ShaderType, SpecializedRenderPipeline, SpecializedRenderPipelines, TextureFormat, VertexAttribute, VertexBufferLayout, VertexFormat, VertexState, VertexStepMode, }, @@ -38,7 +39,7 @@ use bevy::{ }, Extract, MainWorld, Render, RenderApp, RenderSet, }, - utils::{EntityHashMap, FloatOrd, HashMap}, + utils::{FloatOrd, HashMap}, }; use bytemuck::{Pod, Zeroable}; use fixedbitset::FixedBitSet; @@ -118,13 +119,13 @@ type DrawSmudShape = (SetItemPipeline, SetShapeViewBindGroup<0>, DrawShapeBatch) struct SetShapeViewBindGroup; impl RenderCommand

for SetShapeViewBindGroup { type Param = SRes; - type ViewWorldQuery = Read; - type ItemWorldQuery = (); + type ViewQuery = Read; + type ItemQuery = (); fn render<'w>( _item: &P, - view_uniform: ROQueryItem<'w, Self::ViewWorldQuery>, - _view: (), + view_uniform: ROQueryItem<'w, Self::ViewQuery>, + _entity: Option>, shape_meta: SystemParamItem<'w, '_, Self::Param>, pass: &mut TrackedRenderPass<'w>, ) -> RenderCommandResult { @@ -140,19 +141,21 @@ impl RenderCommand

for SetShapeViewBindGroup struct DrawShapeBatch; impl RenderCommand

for DrawShapeBatch { type Param = SRes; - type ViewWorldQuery = (); - type ItemWorldQuery = Read; + type ViewQuery = (); + type ItemQuery = Read; fn render<'w>( _item: &P, _view: (), - batch: &'_ ShapeBatch, + batch: Option>, shape_meta: SystemParamItem<'w, '_, Self::Param>, pass: &mut TrackedRenderPass<'w>, ) -> RenderCommandResult { let shape_meta = shape_meta.into_inner(); pass.set_vertex_buffer(0, shape_meta.vertices.buffer().unwrap().slice(..)); - pass.draw(0..4, batch.range.clone()); + if let Some(batch) = batch { + pass.draw(0..4, batch.range.clone()); + } RenderCommandResult::Success } } @@ -167,8 +170,9 @@ impl FromWorld for SmudPipeline { fn from_world(world: &mut World) -> Self { let render_device = world.get_resource::().unwrap(); - let view_layout = render_device.create_bind_group_layout(&BindGroupLayoutDescriptor { - entries: &[ + let view_layout = render_device.create_bind_group_layout( + Some("shape_view_layout"), + &[ BindGroupLayoutEntry { binding: 0, visibility: ShaderStages::VERTEX_FRAGMENT, @@ -190,8 +194,7 @@ impl FromWorld for SmudPipeline { count: None, }, ], - label: Some("shape_view_layout"), - }); + ); Self { view_layout, @@ -397,7 +400,7 @@ struct ExtractedShape { #[derive(Resource, Default, Debug)] struct ExtractedShapes { - shapes: EntityHashMap, + shapes: EntityHashMap, } fn extract_shapes( From d590eaf407b3e749196158067cdacf3a9a7c61a5 Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Wed, 24 Jul 2024 00:38:04 -0400 Subject: [PATCH 02/12] chore: Update to bevy 0.14. --- Cargo.toml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b4361bb..f4b70fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ repository = "https://github.com/johanhelsing/bevy_smud" version = "0.7.0" [dependencies] -bevy = { version = "0.13", default-features = false, features = [ +bevy = { version = "0.14", default-features = false, features = [ "bevy_core_pipeline", "bevy_render", "bevy_asset", # needed for handle ids @@ -21,15 +21,14 @@ bitflags = "2.5" fixedbitset = "0.5" [dev-dependencies] -bevy = { version = "0.13.2", default-features = false, features = [ +bevy = { version = "0.14", default-features = false, features = [ "bevy_winit", "x11", # github actions runners don't have libxkbcommon installed, so can't use wayland "file_watcher", - "multi-threaded", ] } -bevy_asset_loader = "0.20" -bevy_lospec = "0.7" -bevy_pancam = "0.11" +bevy_asset_loader = "0.21" +bevy_lospec = "0.8" +bevy_pancam = "0.12" rand = "0.8" [profile.dev] From 29994978e070b6a95ff987ae39f51dc2d7bc46fc Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Wed, 24 Jul 2024 01:22:21 -0400 Subject: [PATCH 03/12] hack: It compiles. --- Cargo.toml | 3 +++ examples/basic.rs | 3 ++- src/components.rs | 3 ++- src/lib.rs | 38 ++++++++++++++++++++++++-------------- src/util.rs | 2 +- 5 files changed, 32 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f4b70fb..26e077b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,11 +14,14 @@ bevy = { version = "0.14", default-features = false, features = [ "bevy_core_pipeline", "bevy_render", "bevy_asset", # needed for handle ids + # "bevy_sprite", + "multi_threaded", ] } bytemuck = { version = "1.15.0", features = ["derive"] } copyless = "0.1" bitflags = "2.5" fixedbitset = "0.5" +uuid = "1.10.0" [dev-dependencies] bevy = { version = "0.14", default-features = false, features = [ diff --git a/examples/basic.rs b/examples/basic.rs index b0816af..591746f 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,4 +1,5 @@ use bevy::prelude::*; +use bevy::color::palettes::css; // The prelude contains the basic things needed to create shapes use bevy_smud::prelude::*; @@ -39,7 +40,7 @@ return smud::sd_circle(p_2 - vec2(20., 0.), 40.); commands.spawn(ShapeBundle { shape: SmudShape { - color: Color::TOMATO, + color: css::TOMATO.into(), 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. diff --git a/src/components.rs b/src/components.rs index be95ec3..118cc17 100644 --- a/src/components.rs +++ b/src/components.rs @@ -1,4 +1,5 @@ use bevy::prelude::*; +use bevy::color::palettes::css; use crate::DEFAULT_FILL_HANDLE; @@ -23,7 +24,7 @@ pub struct SmudShape { impl Default for SmudShape { fn default() -> Self { Self { - color: Color::PINK, + color: css::PINK.into(), sdf: default(), frame: default(), fill: DEFAULT_FILL_HANDLE, diff --git a/src/lib.rs b/src/lib.rs index 6e33966..5e61cb8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ use std::ops::Range; use bevy::{ + sprite::WithMesh2d, core_pipeline::core_2d::Transparent2d, ecs::{ entity::EntityHashMap, @@ -14,17 +15,17 @@ use bevy::{ SystemParamItem, }, }, - math::Vec3Swizzles, + math::{FloatOrd, Vec3Swizzles}, prelude::*, render::{ globals::{GlobalsBuffer, GlobalsUniform}, render_phase::{ AddRenderCommand, DrawFunctions, PhaseItem, RenderCommand, RenderCommandResult, - RenderPhase, SetItemPipeline, TrackedRenderPass, + SetItemPipeline, TrackedRenderPass, ViewSortedRenderPhases, PhaseItemExtraIndex, }, render_resource::{ BindGroup, BindGroupEntries, BindGroupLayout, BindGroupLayoutEntry, BindingType, - BlendState, BufferBindingType, BufferUsages, BufferVec, CachedRenderPipelineId, + BlendState, BufferBindingType, BufferUsages, RawBufferVec, CachedRenderPipelineId, ColorTargetState, ColorWrites, Face, FragmentState, FrontFace, MultisampleState, PipelineCache, PolygonMode, PrimitiveState, PrimitiveTopology, RenderPipelineDescriptor, ShaderImport, ShaderStages, ShaderType, @@ -39,7 +40,7 @@ use bevy::{ }, Extract, MainWorld, Render, RenderApp, RenderSet, }, - utils::{FloatOrd, HashMap}, + utils::{HashMap}, }; use bytemuck::{Pod, Zeroable}; use fixedbitset::FixedBitSet; @@ -88,7 +89,7 @@ impl Plugin for SmudPlugin { app.add_plugins(ShaderLoadingPlugin); // app.add_plugins(UiShapePlugin); - if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { + if let Some(render_app) = app.get_sub_app_mut(RenderApp) { render_app .add_render_command::() .init_resource::() @@ -485,8 +486,10 @@ fn queue_shapes( pipeline_cache: ResMut, msaa: Res, extracted_shapes: ResMut, + mut transparent_render_phases: ResMut>, mut views: Query<( - &mut RenderPhase, + Entity, + // &mut RenderPhase, &VisibleEntities, &ExtractedView, )>, @@ -495,15 +498,20 @@ fn queue_shapes( let draw_smud_shape_function = draw_functions.read().get_id::().unwrap(); // Iterate over each view (a camera is a view) - for (mut transparent_phase, visible_entities, view) in &mut views { + for (view_entity, visible_entities, view) in &mut views { // todo: bevy_sprite does some hdr stuff, should we? // let mut view_key = SpritePipelineKey::from_hdr(view.hdr) | msaa_key; + // + // + let Some(transparent_phase) = transparent_render_phases.get_mut(&view_entity) else { + continue; + }; let mesh_key = PipelineKey::from_msaa_samples(msaa.samples()) | PipelineKey::from_primitive_topology(PrimitiveTopology::TriangleStrip); view_entities.clear(); - view_entities.extend(visible_entities.entities.iter().map(|e| e.index() as usize)); + view_entities.extend(visible_entities.iter::().map(|e| e.index() as usize)); transparent_phase .items @@ -543,7 +551,7 @@ fn queue_shapes( sort_key, // batch_range and dynamic_offset will be calculated in prepare_shapes batch_range: 0..0, - dynamic_offset: None, + extra_index: PhaseItemExtraIndex::NONE, }); } } @@ -558,7 +566,8 @@ fn prepare_shapes( view_uniforms: Res, smud_pipeline: Res, extracted_shapes: Res, - mut phases: Query<&mut RenderPhase>, + mut phases: ResMut>, + // mut phases: Query<&mut RenderPhase>, globals_buffer: Res, ) { let globals = globals_buffer.buffer.binding().unwrap(); // todo if-let @@ -578,7 +587,7 @@ fn prepare_shapes( // Vertex buffer index let mut index = 0; - for mut transparent_phase in &mut phases { + for (_entity, transparent_phase) in phases.iter_mut() { let mut batch_item_index = 0; // let mut batch_image_size = Vec2::ZERO; // let mut batch_image_handle = AssetId::invalid(); @@ -604,7 +613,8 @@ fn prepare_shapes( let batch_shader_changed = batch_shader_handles != shader_handles; - let color = extracted_shape.color.as_linear_rgba_f32(); + let lrgba: LinearRgba = extracted_shape.color.into(); + let color = lrgba.to_f32_array(); let position = extracted_shape.transform.translation(); let position = position.into(); @@ -669,14 +679,14 @@ struct ShapeVertex { #[derive(Resource)] pub(crate) struct ShapeMeta { - vertices: BufferVec, + vertices: RawBufferVec, view_bind_group: Option, } impl Default for ShapeMeta { fn default() -> Self { Self { - vertices: BufferVec::new(BufferUsages::VERTEX), + vertices: RawBufferVec::new(BufferUsages::VERTEX), view_bind_group: None, } } diff --git a/src/util.rs b/src/util.rs index c81992a..361283b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,4 @@ -use bevy::utils::Uuid; +use uuid::Uuid; pub fn generate_shader_id() -> String { Uuid::new_v4().to_string().replace('-', "_") From 7f85287aff915c651830ad375ad1d40561e0932a Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Wed, 24 Jul 2024 01:36:01 -0400 Subject: [PATCH 04/12] bug: Fix color in examples. --- examples/basic.rs | 2 +- examples/bench.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index 591746f..18b9a49 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -53,7 +53,7 @@ return smud::sd_circle(p_2 - vec2(20., 0.), 40.); commands.spawn(ShapeBundle { transform: Transform::from_translation(Vec3::X * 200.), shape: SmudShape { - color: Color::rgb(0.7, 0.6, 0.4), + color: Color::srgb(0.7, 0.6, 0.4), sdf: peanut, frame: Frame::Quad(80.), ..default() diff --git a/examples/bench.rs b/examples/bench.rs index 0419361..6358dc8 100644 --- a/examples/bench.rs +++ b/examples/bench.rs @@ -1,5 +1,6 @@ use bevy::{ diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, + color::palettes::css, prelude::*, }; use bevy_asset_loader::prelude::*; @@ -75,7 +76,7 @@ fn setup( .filter(|c| *c != &clear_color) .choose(&mut rng) .copied() - .unwrap_or(Color::PINK); + .unwrap_or(css::PINK.into()); commands.spawn(( ShapeBundle { From 350d8c094cfe165dd731c4f0ef0081f56618bfc4 Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Wed, 24 Jul 2024 01:36:17 -0400 Subject: [PATCH 05/12] hack: Try stuff; add debug lines. --- src/lib.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5e61cb8..babb4ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,6 @@ use std::ops::Range; use bevy::{ - sprite::WithMesh2d, core_pipeline::core_2d::Transparent2d, ecs::{ entity::EntityHashMap, @@ -36,7 +35,7 @@ use bevy::{ texture::BevyDefault, view::{ ExtractedView, ViewTarget, ViewUniform, ViewUniformOffset, ViewUniforms, - VisibleEntities, + VisibleEntities, WithMesh, }, Extract, MainWorld, Render, RenderApp, RenderSet, }, @@ -103,8 +102,9 @@ impl Plugin for SmudPlugin { prepare_shapes.in_set(RenderSet::PrepareBindGroups), ), ); + } else { + panic!("No render app"); } - app.register_type::(); } @@ -504,18 +504,21 @@ fn queue_shapes( // // let Some(transparent_phase) = transparent_render_phases.get_mut(&view_entity) else { + eprintln!("punt"); continue; }; + // eprintln!("got it"); let mesh_key = PipelineKey::from_msaa_samples(msaa.samples()) | PipelineKey::from_primitive_topology(PrimitiveTopology::TriangleStrip); view_entities.clear(); - view_entities.extend(visible_entities.iter::().map(|e| e.index() as usize)); + view_entities.extend(visible_entities.iter::().map(|e| e.index() as usize)); transparent_phase .items .reserve(extracted_shapes.shapes.len()); + eprintln!("shapes count {}", extracted_shapes.shapes.len()); for (entity, extracted_shape) in extracted_shapes.shapes.iter() { let shader = ( @@ -543,6 +546,7 @@ fn queue_shapes( // These items will be sorted by depth with other phase items let sort_key = FloatOrd(extracted_shape.transform.translation().z); + eprintln!("added"); // Add the item to the render phase transparent_phase.add(Transparent2d { draw_function: draw_smud_shape_function, From b08fa8efd63933b43f21e9bfc0b96c437634b8cb Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Wed, 24 Jul 2024 02:08:20 -0400 Subject: [PATCH 06/12] feature: It works! --- Cargo.toml | 2 +- src/lib.rs | 49 +++++++++++++++++++++++-------------------------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 26e077b..610f382 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,6 @@ bevy = { version = "0.14", default-features = false, features = [ "bevy_core_pipeline", "bevy_render", "bevy_asset", # needed for handle ids - # "bevy_sprite", "multi_threaded", ] } bytemuck = { version = "1.15.0", features = ["derive"] } @@ -25,6 +24,7 @@ uuid = "1.10.0" [dev-dependencies] bevy = { version = "0.14", default-features = false, features = [ + "bevy_state", "bevy_winit", "x11", # github actions runners don't have libxkbcommon installed, so can't use wayland "file_watcher", diff --git a/src/lib.rs b/src/lib.rs index babb4ec..8858619 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -35,7 +35,7 @@ use bevy::{ texture::BevyDefault, view::{ ExtractedView, ViewTarget, ViewUniform, ViewUniformOffset, ViewUniforms, - VisibleEntities, WithMesh, + VisibleEntities, WithMesh, check_visibility, VisibilitySystems, }, Extract, MainWorld, Render, RenderApp, RenderSet, }, @@ -85,33 +85,34 @@ pub struct SmudPlugin; impl Plugin for SmudPlugin { fn build(&self, app: &mut App) { // All the messy boiler-plate for loading a bunch of shaders - app.add_plugins(ShaderLoadingPlugin); + app.add_plugins(ShaderLoadingPlugin) + .add_systems( + PostUpdate, + check_visibility::> + .in_set(VisibilitySystems::CheckVisibility) + ); // app.add_plugins(UiShapePlugin); - if let Some(render_app) = app.get_sub_app_mut(RenderApp) { - render_app - .add_render_command::() - .init_resource::() - .init_resource::() - .init_resource::>() - .add_systems(ExtractSchedule, (extract_shapes, extract_sdf_shaders)) - .add_systems( - Render, - ( - queue_shapes.in_set(RenderSet::Queue), - prepare_shapes.in_set(RenderSet::PrepareBindGroups), - ), - ); - } else { - panic!("No render app"); - } app.register_type::(); } fn finish(&self, app: &mut App) { - app.get_sub_app_mut(RenderApp) - .unwrap() - .init_resource::(); + + let render_app = app.sub_app_mut(RenderApp); + render_app + .add_render_command::() + .init_resource::() + .init_resource::() + .init_resource::>() + .init_resource::() + .add_systems(ExtractSchedule, (extract_shapes, extract_sdf_shaders)) + .add_systems( + Render, + ( + queue_shapes.in_set(RenderSet::Queue), + prepare_shapes.in_set(RenderSet::PrepareBindGroups), + ), + ); } } @@ -504,10 +505,8 @@ fn queue_shapes( // // let Some(transparent_phase) = transparent_render_phases.get_mut(&view_entity) else { - eprintln!("punt"); continue; }; - // eprintln!("got it"); let mesh_key = PipelineKey::from_msaa_samples(msaa.samples()) | PipelineKey::from_primitive_topology(PrimitiveTopology::TriangleStrip); @@ -518,7 +517,6 @@ fn queue_shapes( transparent_phase .items .reserve(extracted_shapes.shapes.len()); - eprintln!("shapes count {}", extracted_shapes.shapes.len()); for (entity, extracted_shape) in extracted_shapes.shapes.iter() { let shader = ( @@ -546,7 +544,6 @@ fn queue_shapes( // These items will be sorted by depth with other phase items let sort_key = FloatOrd(extracted_shape.transform.translation().z); - eprintln!("added"); // Add the item to the render phase transparent_phase.add(Transparent2d { draw_function: draw_smud_shape_function, From 7508037eeefb5b85cfff1b2090de01ee5ead6978 Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Wed, 24 Jul 2024 02:08:30 -0400 Subject: [PATCH 07/12] bug: Fix examples. --- examples/bench.rs | 16 ++++++++-------- examples/bloom.rs | 3 ++- examples/custom_fill.rs | 7 ++++--- examples/gallery.rs | 17 +++++++++-------- examples/sorting.rs | 8 ++++---- examples/transforms.rs | 4 ++-- 6 files changed, 29 insertions(+), 26 deletions(-) diff --git a/examples/bench.rs b/examples/bench.rs index 6358dc8..14fb693 100644 --- a/examples/bench.rs +++ b/examples/bench.rs @@ -10,6 +10,14 @@ use rand::prelude::*; fn main() { App::new() + .add_plugins(( + DefaultPlugins, + LogDiagnosticsPlugin::default(), + FrameTimeDiagnosticsPlugin, + SmudPlugin, + PanCamPlugin, + bevy_lospec::PalettePlugin, + )) .init_state::() // bevy_smud comes with anti-aliasing built into the standards fills // which is more efficient than MSAA, and also works on Linux, wayland @@ -19,14 +27,6 @@ fn main() { .continue_to_state(GameState::Running) .load_collection::(), ) - .add_plugins(( - DefaultPlugins, - LogDiagnosticsPlugin::default(), - FrameTimeDiagnosticsPlugin, - SmudPlugin, - PanCamPlugin, - bevy_lospec::PalettePlugin, - )) .add_systems(OnEnter(GameState::Running), setup) // .add_system_set(SystemSet::on_update(GameState::Running).with_system(update)) .run(); diff --git a/examples/bloom.rs b/examples/bloom.rs index 4a2da01..1a17a8a 100644 --- a/examples/bloom.rs +++ b/examples/bloom.rs @@ -4,6 +4,7 @@ //! effects by creating a custom fill. use bevy::{core_pipeline::bloom::BloomSettings, prelude::*}; +use bevy::color::palettes::css; // The prelude contains the basic things needed to create shapes use bevy_smud::prelude::*; @@ -27,7 +28,7 @@ fn setup(mut commands: Commands, mut shaders: ResMut>) { commands.spawn(ShapeBundle { shape: SmudShape { - color: Color::TOMATO, + color: css::TOMATO.into(), 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. diff --git a/examples/custom_fill.rs b/examples/custom_fill.rs index 67204d1..595f4da 100644 --- a/examples/custom_fill.rs +++ b/examples/custom_fill.rs @@ -1,4 +1,5 @@ use bevy::prelude::*; +use bevy::color::palettes::css; use bevy_pancam::*; use bevy_smud::{prelude::*, SIMPLE_FILL_HANDLE}; @@ -23,7 +24,7 @@ fn setup( commands.spawn(ShapeBundle { shape: SmudShape { - color: Color::TEAL, + color: css::TEAL.into(), sdf: asset_server.load("bevy.wgsl"), fill: sin_fill, frame: Frame::Quad(295.), @@ -34,7 +35,7 @@ fn setup( commands.spawn(ShapeBundle { transform: Transform::from_translation(Vec3::X * 600.), shape: SmudShape { - color: Color::BLUE, + color: css::BLUE.into(), sdf: asset_server.load("bevy.wgsl"), fill: SIMPLE_FILL_HANDLE, frame: Frame::Quad(295.), @@ -45,7 +46,7 @@ fn setup( commands.spawn(ShapeBundle { transform: Transform::from_translation(Vec3::X * -600.), shape: SmudShape { - color: Color::ORANGE, + color: css::ORANGE.into(), sdf: asset_server.load("bevy.wgsl"), fill: shaders.add_fill_body( r" diff --git a/examples/gallery.rs b/examples/gallery.rs index a93b2b0..4e1487a 100644 --- a/examples/gallery.rs +++ b/examples/gallery.rs @@ -1,4 +1,5 @@ use bevy::prelude::*; +use bevy::color::palettes::css; use bevy_asset_loader::prelude::*; use bevy_pancam::*; use bevy_smud::*; @@ -6,15 +7,8 @@ use rand::prelude::*; fn main() { App::new() - .init_state::() // bevy_smud comes with anti-aliasing built into the standards fills // which is more efficient than MSAA, and also works on Linux, wayland - .insert_resource(Msaa::Off) - .add_loading_state( - LoadingState::new(GameState::Loading) - .continue_to_state(GameState::Running) - .load_collection::(), - ) .add_plugins(( DefaultPlugins, SmudPlugin, @@ -23,6 +17,13 @@ fn main() { PanCamPlugin, bevy_lospec::PalettePlugin, )) + .init_state::() + .insert_resource(Msaa::Off) + .add_loading_state( + LoadingState::new(GameState::Loading) + .continue_to_state(GameState::Running) + .load_collection::(), + ) .add_systems(OnEnter(GameState::Running), setup) .run(); } @@ -100,7 +101,7 @@ fn setup( .filter(|c| *c != &clear_color) .choose(&mut rng) .copied() - .unwrap_or(Color::PINK); + .unwrap_or(css::PINK.into()); let index = i + j * w; diff --git a/examples/sorting.rs b/examples/sorting.rs index 69b306d..429cdce 100644 --- a/examples/sorting.rs +++ b/examples/sorting.rs @@ -7,7 +7,7 @@ fn main() { // bevy_smud comes with anti-aliasing built into the standards fills // which is more efficient than MSAA, and also works on Linux, wayland .insert_resource(Msaa::Off) - .insert_resource(ClearColor(Color::rgb(0.7, 0.8, 0.7))) + .insert_resource(ClearColor(Color::srgb(0.7, 0.8, 0.7))) .add_plugins((DefaultPlugins, SmudPlugin, PanCamPlugin)) .add_systems(Startup, setup) .run(); @@ -18,7 +18,7 @@ fn setup(mut commands: Commands, mut shaders: ResMut>) { commands.spawn(ShapeBundle { transform: Transform::from_translation(Vec3::Z * 3.), shape: SmudShape { - color: Color::rgb(0.0, 0.0, 0.0), + color: Color::srgb(0.0, 0.0, 0.0), sdf: shaders.add_sdf_body("return smud::sd_circle(p, 70.);"), frame: Frame::Quad(80.), ..default() @@ -30,7 +30,7 @@ fn setup(mut commands: Commands, mut shaders: ResMut>) { commands.spawn(ShapeBundle { transform: Transform::from_translation(Vec3::Z * 2.), shape: SmudShape { - color: Color::rgb(0.46, 0.42, 0.80), + color: Color::srgb(0.46, 0.42, 0.80), sdf: shaders.add_sdf_body("return smud::sd_circle(p, 150.);"), frame: Frame::Quad(200.), ..default() @@ -42,7 +42,7 @@ fn setup(mut commands: Commands, mut shaders: ResMut>) { commands.spawn(ShapeBundle { transform: Transform::from_translation(Vec3::Z * 1.), shape: SmudShape { - color: Color::rgb(0.83, 0.82, 0.80), + color: Color::srgb(0.83, 0.82, 0.80), sdf: shaders.add_sdf_body("return smud::sd_vesica(p.yx, 400., 150.);"), frame: Frame::Quad(400.), ..default() diff --git a/examples/transforms.rs b/examples/transforms.rs index 9f4186b..3f7d30f 100644 --- a/examples/transforms.rs +++ b/examples/transforms.rs @@ -9,7 +9,7 @@ fn main() { // bevy_smud comes with anti-aliasing built into the standards fills // which is more efficient than MSAA, and also works on Linux, wayland .insert_resource(Msaa::Off) - .insert_resource(ClearColor(Color::rgb(0.7, 0.8, 0.7))) + .insert_resource(ClearColor(Color::srgb(0.7, 0.8, 0.7))) .add_plugins((DefaultPlugins, SmudPlugin, PanCamPlugin)) .add_systems(Startup, setup) .run(); @@ -25,7 +25,7 @@ fn setup(mut commands: Commands, asset_server: Res) { }; let shape = SmudShape { - color: Color::rgb(0.36, 0.41, 0.45), + color: Color::srgb(0.36, 0.41, 0.45), sdf: bevy_shape_shader, frame: Frame::Quad(295.), ..default() From f1f5aa59c9b4336a21aa52f4057d866c5d4eca29 Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Wed, 24 Jul 2024 02:09:30 -0400 Subject: [PATCH 08/12] chore: Follow clippy's advice. --- examples/bevy.rs | 4 ++-- examples/time.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/bevy.rs b/examples/bevy.rs index 5ed4315..d1c4c2a 100644 --- a/examples/bevy.rs +++ b/examples/bevy.rs @@ -7,7 +7,7 @@ fn main() { // bevy_smud comes with anti-aliasing built into the standards fills // which is more efficient than MSAA, and also works on Linux, wayland .insert_resource(Msaa::Off) - .insert_resource(ClearColor(Color::rgb(0.7, 0.8, 0.7))) + .insert_resource(ClearColor(Color::srgb(0.7, 0.8, 0.7))) .add_plugins((DefaultPlugins, SmudPlugin, PanCamPlugin)) .add_systems(Startup, setup) .run(); @@ -18,7 +18,7 @@ fn setup(mut commands: Commands, asset_server: Res) { commands.spawn(ShapeBundle { shape: SmudShape { - color: Color::rgb(0.36, 0.41, 0.45), + color: Color::srgb(0.36, 0.41, 0.45), sdf: bevy_shape_shader, frame: Frame::Quad(400.), ..default() diff --git a/examples/time.rs b/examples/time.rs index 117e0e4..d9713d4 100644 --- a/examples/time.rs +++ b/examples/time.rs @@ -7,7 +7,7 @@ fn main() { // bevy_smud comes with anti-aliasing built into the standards fills // which is more efficient than MSAA, and also works on Linux, wayland .insert_resource(Msaa::Off) - .insert_resource(ClearColor(Color::rgb(0.7, 0.8, 0.7))) + .insert_resource(ClearColor(Color::srgb(0.7, 0.8, 0.7))) .add_plugins((DefaultPlugins, SmudPlugin, PanCamPlugin)) .add_systems(Startup, setup) .run(); @@ -18,7 +18,7 @@ fn setup(mut commands: Commands, asset_server: Res) { commands.spawn(ShapeBundle { shape: SmudShape { - color: Color::rgb(0.36, 0.41, 0.45), + color: Color::srgb(0.36, 0.41, 0.45), sdf: bevy_shape_shader, frame: Frame::Quad(400.), ..default() From ebab059c26b70b38939820ab356fd5d4d6263be4 Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Wed, 24 Jul 2024 02:10:06 -0400 Subject: [PATCH 09/12] style: Run cargo fmt. --- examples/basic.rs | 2 +- examples/bench.rs | 2 +- examples/bloom.rs | 2 +- examples/custom_fill.rs | 2 +- examples/gallery.rs | 2 +- src/components.rs | 2 +- src/lib.rs | 39 ++++++++++++++++++++------------------- 7 files changed, 26 insertions(+), 25 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index 18b9a49..1e0b10a 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,5 +1,5 @@ -use bevy::prelude::*; use bevy::color::palettes::css; +use bevy::prelude::*; // The prelude contains the basic things needed to create shapes use bevy_smud::prelude::*; diff --git a/examples/bench.rs b/examples/bench.rs index 14fb693..27b4496 100644 --- a/examples/bench.rs +++ b/examples/bench.rs @@ -1,6 +1,6 @@ use bevy::{ - diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, color::palettes::css, + diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, prelude::*, }; use bevy_asset_loader::prelude::*; diff --git a/examples/bloom.rs b/examples/bloom.rs index 1a17a8a..5f918e4 100644 --- a/examples/bloom.rs +++ b/examples/bloom.rs @@ -3,8 +3,8 @@ //! Note that you could probably achieve cheaper and higher quality bloom-like //! effects by creating a custom fill. -use bevy::{core_pipeline::bloom::BloomSettings, prelude::*}; use bevy::color::palettes::css; +use bevy::{core_pipeline::bloom::BloomSettings, prelude::*}; // The prelude contains the basic things needed to create shapes use bevy_smud::prelude::*; diff --git a/examples/custom_fill.rs b/examples/custom_fill.rs index 595f4da..200d262 100644 --- a/examples/custom_fill.rs +++ b/examples/custom_fill.rs @@ -1,5 +1,5 @@ -use bevy::prelude::*; use bevy::color::palettes::css; +use bevy::prelude::*; use bevy_pancam::*; use bevy_smud::{prelude::*, SIMPLE_FILL_HANDLE}; diff --git a/examples/gallery.rs b/examples/gallery.rs index 4e1487a..748ce20 100644 --- a/examples/gallery.rs +++ b/examples/gallery.rs @@ -1,5 +1,5 @@ -use bevy::prelude::*; use bevy::color::palettes::css; +use bevy::prelude::*; use bevy_asset_loader::prelude::*; use bevy_pancam::*; use bevy_smud::*; diff --git a/src/components.rs b/src/components.rs index 118cc17..7f1a6cf 100644 --- a/src/components.rs +++ b/src/components.rs @@ -1,5 +1,5 @@ -use bevy::prelude::*; use bevy::color::palettes::css; +use bevy::prelude::*; use crate::DEFAULT_FILL_HANDLE; diff --git a/src/lib.rs b/src/lib.rs index 8858619..920be69 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,27 +19,27 @@ use bevy::{ render::{ globals::{GlobalsBuffer, GlobalsUniform}, render_phase::{ - AddRenderCommand, DrawFunctions, PhaseItem, RenderCommand, RenderCommandResult, - SetItemPipeline, TrackedRenderPass, ViewSortedRenderPhases, PhaseItemExtraIndex, + AddRenderCommand, DrawFunctions, PhaseItem, PhaseItemExtraIndex, RenderCommand, + RenderCommandResult, SetItemPipeline, TrackedRenderPass, ViewSortedRenderPhases, }, render_resource::{ BindGroup, BindGroupEntries, BindGroupLayout, BindGroupLayoutEntry, BindingType, - BlendState, BufferBindingType, BufferUsages, RawBufferVec, CachedRenderPipelineId, - ColorTargetState, ColorWrites, Face, FragmentState, FrontFace, MultisampleState, - PipelineCache, PolygonMode, PrimitiveState, PrimitiveTopology, - RenderPipelineDescriptor, ShaderImport, ShaderStages, ShaderType, - SpecializedRenderPipeline, SpecializedRenderPipelines, TextureFormat, VertexAttribute, - VertexBufferLayout, VertexFormat, VertexState, VertexStepMode, + BlendState, BufferBindingType, BufferUsages, CachedRenderPipelineId, ColorTargetState, + ColorWrites, Face, FragmentState, FrontFace, MultisampleState, PipelineCache, + PolygonMode, PrimitiveState, PrimitiveTopology, RawBufferVec, RenderPipelineDescriptor, + ShaderImport, ShaderStages, ShaderType, SpecializedRenderPipeline, + SpecializedRenderPipelines, TextureFormat, VertexAttribute, VertexBufferLayout, + VertexFormat, VertexState, VertexStepMode, }, renderer::{RenderDevice, RenderQueue}, texture::BevyDefault, view::{ - ExtractedView, ViewTarget, ViewUniform, ViewUniformOffset, ViewUniforms, - VisibleEntities, WithMesh, check_visibility, VisibilitySystems, + check_visibility, ExtractedView, ViewTarget, ViewUniform, ViewUniformOffset, + ViewUniforms, VisibilitySystems, VisibleEntities, WithMesh, }, Extract, MainWorld, Render, RenderApp, RenderSet, }, - utils::{HashMap}, + utils::HashMap, }; use bytemuck::{Pod, Zeroable}; use fixedbitset::FixedBitSet; @@ -85,19 +85,16 @@ pub struct SmudPlugin; impl Plugin for SmudPlugin { fn build(&self, app: &mut App) { // All the messy boiler-plate for loading a bunch of shaders - app.add_plugins(ShaderLoadingPlugin) - .add_systems( - PostUpdate, - check_visibility::> - .in_set(VisibilitySystems::CheckVisibility) - ); + app.add_plugins(ShaderLoadingPlugin).add_systems( + PostUpdate, + check_visibility::>.in_set(VisibilitySystems::CheckVisibility), + ); // app.add_plugins(UiShapePlugin); app.register_type::(); } fn finish(&self, app: &mut App) { - let render_app = app.sub_app_mut(RenderApp); render_app .add_render_command::() @@ -512,7 +509,11 @@ fn queue_shapes( | PipelineKey::from_primitive_topology(PrimitiveTopology::TriangleStrip); view_entities.clear(); - view_entities.extend(visible_entities.iter::().map(|e| e.index() as usize)); + view_entities.extend( + visible_entities + .iter::() + .map(|e| e.index() as usize), + ); transparent_phase .items From 34dc6b99dabf942b910f97386b25455eb80fd016 Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Wed, 24 Jul 2024 02:13:25 -0400 Subject: [PATCH 10/12] chore: Bump version to 0.9.0. I skipped 0.8.0 in case we want to release the update for bevy 0.13. --- Cargo.toml | 2 +- README.md | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 610f382..ba8131e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ keywords = ["gamedev", "bevy", "sdf"] license = "MIT OR Apache-2.0" name = "bevy_smud" repository = "https://github.com/johanhelsing/bevy_smud" -version = "0.7.0" +version = "0.9.0" [dependencies] bevy = { version = "0.14", default-features = false, features = [ diff --git a/README.md b/README.md index 64204da..f6b57f2 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,8 @@ The `main` branch targets the latest bevy release. |bevy|bevy_smud| |----|---------| -|0.12|0.7, main| +|0.14|0.9, main| +|0.12|0.7 | |0.11|0.6 | |0.10|0.5 | |0.9 |0.4 | From 2e37ce86b45fcb31a3d92152d83381dc2036d0a5 Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Wed, 24 Jul 2024 03:44:22 -0400 Subject: [PATCH 11/12] feature: Add oscilloscope example. --- examples/oscilloscope.rs | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 examples/oscilloscope.rs diff --git a/examples/oscilloscope.rs b/examples/oscilloscope.rs new file mode 100644 index 0000000..36a683f --- /dev/null +++ b/examples/oscilloscope.rs @@ -0,0 +1,59 @@ +use bevy::color::palettes::css; +use bevy::prelude::*; +use bevy_pancam::*; +use bevy_smud::{prelude::*, SIMPLE_FILL_HANDLE}; + +fn main() { + App::new() + // bevy_smud comes with anti-aliasing built into the standards fills + // which is more efficient than MSAA, and also works on Linux, wayland + .insert_resource(Msaa::Off) + .insert_resource(ClearColor(Color::BLACK)) + .add_plugins((DefaultPlugins, SmudPlugin, PanCamPlugin)) + .add_systems(Startup, setup) + .run(); +} + +fn setup( + mut commands: Commands, + asset_server: Res, + mut shaders: ResMut>, +) { + // The fill takes a distance and a color and returns another color + commands.spawn(ShapeBundle { + shape: SmudShape { + color: css::GREEN.into(), + sdf: asset_server.load("bevy.wgsl"), + fill: shaders.add_fill_body( + r" +var col = color.rgb / sqrt(abs(d)); +// col *= smoothstep(1.5, 0.5, length(p)); // We don't have p. This would give a vignette. + +// This is a brighter effect. +return vec4(col, color.a); +// This is a darker effect. +// return vec4(aces_approx(col), color.a); +} + +// HACK: We're gonna cheat on this template and add some auxiliary functions. + +// License: Unknown, author: Matt Taylor (https://github.com/64), found: https://64.github.io/tonemapping/ +fn aces_approx(v_: vec3) -> vec3 { + var v = max(v_, vec3(0.0)); + v *= 0.6; + let a: f32 = 2.51; + let b: f32 = 0.03; + let c: f32 = 2.43; + let d: f32 = 0.59; + let e: f32 = 0.14; + return saturate((v * (a * v + b)) / (v * (c * v + d) + e)); +", + ), + + frame: Frame::Quad(295.), + }, + ..default() + }); + + commands.spawn((Camera2dBundle::default(), PanCam::default())); +} From 64fabbbeea1c505b1a35d995dc2570c9fc6f17db Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Wed, 24 Jul 2024 03:47:02 -0400 Subject: [PATCH 12/12] doc: Fix up some wording. --- examples/oscilloscope.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/oscilloscope.rs b/examples/oscilloscope.rs index 36a683f..93c46ac 100644 --- a/examples/oscilloscope.rs +++ b/examples/oscilloscope.rs @@ -22,7 +22,8 @@ fn setup( // The fill takes a distance and a color and returns another color commands.spawn(ShapeBundle { shape: SmudShape { - color: css::GREEN.into(), + // color: css::GREEN.into(), + color: css::ORANGE.into(), sdf: asset_server.load("bevy.wgsl"), fill: shaders.add_fill_body( r" @@ -35,7 +36,7 @@ return vec4(col, color.a); // return vec4(aces_approx(col), color.a); } -// HACK: We're gonna cheat on this template and add some auxiliary functions. +// HACK: We're gonna cheat on this template and add an auxiliary function. // License: Unknown, author: Matt Taylor (https://github.com/64), found: https://64.github.io/tonemapping/ fn aces_approx(v_: vec3) -> vec3 {