From 3a2cdca35e7a07c770f16420f20f2902005091b1 Mon Sep 17 00:00:00 2001 From: Raffaele Ragni Date: Sun, 5 Nov 2023 21:12:09 +0100 Subject: [PATCH] upgrade to bevy 0.12 (#221) * upgrade to bevy 0.12 * upgrade * bevy release * Update src/lib.rs Co-authored-by: Matthew S. Klosak <9565094+msklosak@users.noreply.github.com> * compare all fields * use real time * Simplify the compare_descriptors implementation * Fix compiling in WASM * Fix formatting * Fix web-sys version just in the CI instead * Remove redundant dev-dependencies features --------- Co-authored-by: Matthew S. Klosak <9565094+msklosak@users.noreply.github.com> Co-authored-by: mvlabat --- .github/workflows/check.yml | 1 + Cargo.toml | 8 ++---- examples/render_to_image_widget.rs | 2 +- src/egui_node.rs | 12 ++++---- src/lib.rs | 38 +++++++++++++------------ src/render_systems.rs | 30 ++++++++++---------- src/systems.rs | 45 +++++++++++++++--------------- 7 files changed, 66 insertions(+), 70 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 640d71469..9de05747a 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -80,6 +80,7 @@ jobs: restore-keys: | cache-wasm32-cargo-${{ hashFiles('**/Cargo.toml') }} cache-wasm32-cargo + - run: cargo update -p web-sys --precise 0.3.64 - run: cargo clippy --no-default-features --target=wasm32-unknown-unknown --all-targets --features=${{ matrix.features }} -- -D warnings doc: diff --git a/Cargo.toml b/Cargo.toml index ba7e2584b..7260ed373 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ default_fonts = ["egui/default_fonts"] serde = ["egui/serde"] [dependencies] -bevy = { version = "0.11", default-features = false, features = [ +bevy = { version = "0.12", default-features = false, features = [ "bevy_render", "bevy_asset", ] } @@ -36,13 +36,9 @@ thread_local = { version = "1.1.0", optional = true } [dev-dependencies] once_cell = "1.16.0" version-sync = "0.9.4" -bevy = { version = "0.11", default-features = false, features = [ +bevy = { version = "0.12", default-features = false, features = [ "x11", "png", "bevy_pbr", "bevy_core_pipeline", - # TODO: remove the following features after https://github.com/bevyengine/bevy/pull/9073 is released: - "ktx2", - "tonemapping_luts", - "zstd", ] } diff --git a/examples/render_to_image_widget.rs b/examples/render_to_image_widget.rs index 3ec89372b..9f1f10c17 100644 --- a/examples/render_to_image_widget.rs +++ b/examples/render_to_image_widget.rs @@ -192,7 +192,7 @@ fn render_to_image_example_system( let material_clone = preview_material.clone(); let main_material_handle = main_cube_query.single(); - let _ = materials.set(main_material_handle, material_clone); + materials.insert(main_material_handle, material_clone); } } diff --git a/src/egui_node.rs b/src/egui_node.rs index ebe0b67c9..ff6f76550 100644 --- a/src/egui_node.rs +++ b/src/egui_node.rs @@ -7,8 +7,7 @@ use crate::{ use bevy::{ core::cast_slice, ecs::world::{FromWorld, World}, - prelude::{Entity, HandleUntyped, Resource}, - reflect::TypeUuid, + prelude::{Entity, Handle, Resource}, render::{ render_graph::{Node, NodeRunError, RenderGraphContext}, render_resource::{ @@ -28,8 +27,7 @@ use bevy::{ }; /// Egui shader. -pub const EGUI_SHADER_HANDLE: HandleUntyped = - HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 9898276442290979394); +pub const EGUI_SHADER_HANDLE: Handle = Handle::weak_from_u128(9898276442290979394); /// Egui render pipeline. #[derive(Resource)] @@ -107,7 +105,7 @@ impl SpecializedRenderPipeline for EguiPipeline { self.texture_bind_group_layout.clone(), ], vertex: VertexState { - shader: EGUI_SHADER_HANDLE.typed(), + shader: EGUI_SHADER_HANDLE, shader_defs: Vec::new(), entry_point: "vs_main".into(), buffers: vec![VertexBufferLayout::from_vertex_formats( @@ -120,7 +118,7 @@ impl SpecializedRenderPipeline for EguiPipeline { )], }, fragment: Some(FragmentState { - shader: EGUI_SHADER_HANDLE.typed(), + shader: EGUI_SHADER_HANDLE, shader_defs: Vec::new(), entry_point: "fs_main".into(), targets: vec![Some(ColorTargetState { @@ -442,7 +440,7 @@ pub(crate) fn color_image_as_bevy_image( .collect(); Image { - sampler_descriptor, + sampler: sampler_descriptor, ..Image::new( Extent3d { width: egui_image.width() as u32, diff --git a/src/lib.rs b/src/lib.rs index 59ecd3985..2cbc31fe4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,8 +86,8 @@ use bevy::{ render::{ extract_component::{ExtractComponent, ExtractComponentPlugin}, extract_resource::{ExtractResource, ExtractResourcePlugin}, - render_resource::{AddressMode, SamplerDescriptor, SpecializedRenderPipelines}, - texture::{Image, ImageSampler}, + render_resource::SpecializedRenderPipelines, + texture::{Image, ImageAddressMode, ImageSampler, ImageSamplerDescriptor}, ExtractSchedule, Render, RenderApp, RenderSet, }, utils::HashMap, @@ -144,7 +144,7 @@ fn compare_descriptors(a: &ImageSampler, b: &ImageSampler) -> bool { match (a, b) { (ImageSampler::Default, ImageSampler::Default) => true, (ImageSampler::Descriptor(descriptor_a), ImageSampler::Descriptor(descriptor_b)) => { - descriptor_a == descriptor_b + descriptor_a.as_wgpu() == descriptor_b.as_wgpu() } _ => false, } @@ -156,10 +156,10 @@ impl Default for EguiSettings { scale_factor: 1.0, #[cfg(feature = "open_url")] default_open_url_target: None, - sampler_descriptor: ImageSampler::Descriptor(SamplerDescriptor { - address_mode_u: AddressMode::ClampToEdge, - address_mode_v: AddressMode::ClampToEdge, - ..ImageSampler::linear_descriptor() + sampler_descriptor: ImageSampler::Descriptor(ImageSamplerDescriptor { + address_mode_u: ImageAddressMode::ClampToEdge, + address_mode_v: ImageAddressMode::ClampToEdge, + ..ImageSamplerDescriptor::linear() }), } } @@ -168,10 +168,10 @@ impl Default for EguiSettings { impl EguiSettings { /// Use nearest descriptor instead of linear. pub fn use_nearest_descriptor(&mut self) { - self.sampler_descriptor = ImageSampler::Descriptor(SamplerDescriptor { - address_mode_u: AddressMode::ClampToEdge, - address_mode_v: AddressMode::ClampToEdge, - ..ImageSampler::nearest_descriptor() + self.sampler_descriptor = ImageSampler::Descriptor(ImageSamplerDescriptor { + address_mode_u: ImageAddressMode::ClampToEdge, + address_mode_v: ImageAddressMode::ClampToEdge, + ..ImageSamplerDescriptor::nearest() }) } /// Use default image sampler, derived from the [`ImagePlugin`](bevy::render::texture::ImagePlugin) setup. @@ -804,9 +804,9 @@ fn free_egui_textures_system( } } - for image_event in image_events.iter() { - if let AssetEvent::Removed { handle } = image_event { - egui_user_textures.remove_image(handle); + for image_event in image_events.read() { + if let AssetEvent::Removed { id } = image_event { + egui_user_textures.remove_image(&Handle::::Weak(*id)); } } } @@ -840,10 +840,12 @@ mod tests { .add_plugins( DefaultPlugins .set(RenderPlugin { - wgpu_settings: WgpuSettings { - backends: None, - ..Default::default() - }, + render_creation: bevy::render::settings::RenderCreation::Automatic( + WgpuSettings { + backends: None, + ..Default::default() + }, + ), }) .build() .disable::(), diff --git a/src/render_systems.rs b/src/render_systems.rs index 106165509..ec73b5111 100644 --- a/src/render_systems.rs +++ b/src/render_systems.rs @@ -3,7 +3,6 @@ use crate::{ EguiManagedTextures, EguiSettings, EguiUserTextures, WindowSize, }; use bevy::{ - asset::HandleId, ecs::system::SystemParam, prelude::*, render::{ @@ -11,9 +10,8 @@ use bevy::{ render_asset::RenderAssets, render_graph::RenderGraph, render_resource::{ - BindGroup, BindGroupDescriptor, BindGroupEntry, BindingResource, BufferId, - CachedRenderPipelineId, DynamicUniformBuffer, PipelineCache, ShaderType, - SpecializedRenderPipelines, + BindGroup, BindGroupEntry, BindingResource, BufferId, CachedRenderPipelineId, + DynamicUniformBuffer, PipelineCache, ShaderType, SpecializedRenderPipelines, }, renderer::{RenderDevice, RenderQueue}, texture::Image, @@ -57,7 +55,7 @@ pub struct ExtractedEguiTextures<'w> { impl ExtractedEguiTextures<'_> { /// Returns an iterator over all textures (both Egui and Bevy managed). - pub fn handles(&self) -> impl Iterator + '_ { + pub fn handles(&self) -> impl Iterator)> + '_ { self.egui_textures .0 .iter() @@ -156,14 +154,14 @@ pub fn prepare_egui_transforms_system( match egui_transforms.bind_group { Some((id, _)) if buffer.id() == id => {} _ => { - let transform_bind_group = render_device.create_bind_group(&BindGroupDescriptor { - label: Some("egui transform bind group"), - layout: &egui_pipeline.transform_bind_group_layout, - entries: &[BindGroupEntry { + let transform_bind_group = render_device.create_bind_group( + Some("egui transform bind group"), + &egui_pipeline.transform_bind_group_layout, + &[BindGroupEntry { binding: 0, resource: egui_transforms.buffer.binding().unwrap(), }], - }); + ); egui_transforms.bind_group = Some((buffer.id(), transform_bind_group)); } }; @@ -185,11 +183,11 @@ pub fn queue_bind_groups_system( let bind_groups = egui_textures .handles() .filter_map(|(texture, handle_id)| { - let gpu_image = gpu_images.get(&Handle::weak(handle_id))?; - let bind_group = render_device.create_bind_group(&BindGroupDescriptor { - label: None, - layout: &egui_pipeline.texture_bind_group_layout, - entries: &[ + let gpu_image = gpu_images.get(&Handle::Weak(handle_id))?; + let bind_group = render_device.create_bind_group( + None, + &egui_pipeline.texture_bind_group_layout, + &[ BindGroupEntry { binding: 0, resource: BindingResource::TextureView(&gpu_image.texture_view), @@ -199,7 +197,7 @@ pub fn queue_bind_groups_system( resource: BindingResource::Sampler(&gpu_image.sampler), }, ], - }); + ); Some((texture, bind_group)) }) .collect(); diff --git a/src/systems.rs b/src/systems.rs index 5a31238d8..5fee41d88 100644 --- a/src/systems.rs +++ b/src/systems.rs @@ -15,6 +15,7 @@ use bevy::{ ButtonState, Input, }, prelude::{Entity, EventReader, Query, Resource, Time}, + time::Real, window::{ CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, RequestRedraw, WindowCreated, WindowFocused, @@ -40,16 +41,16 @@ pub struct InputEvents<'w, 's> { impl<'w, 's> InputEvents<'w, 's> { /// Consumes all the events. pub fn clear(&mut self) { - self.ev_touch.iter().last(); - self.ev_cursor_entered.iter().last(); - self.ev_cursor_left.iter().last(); - self.ev_cursor.iter().last(); - self.ev_mouse_button_input.iter().last(); - self.ev_mouse_wheel.iter().last(); - self.ev_received_character.iter().last(); - self.ev_keyboard_input.iter().last(); - self.ev_window_focused.iter().last(); - self.ev_window_created.iter().last(); + self.ev_touch.read().last(); + self.ev_cursor_entered.read().last(); + self.ev_cursor_left.read().last(); + self.ev_cursor.read().last(); + self.ev_mouse_button_input.read().last(); + self.ev_mouse_wheel.read().last(); + self.ev_received_character.read().last(); + self.ev_keyboard_input.read().last(); + self.ev_window_focused.read().last(); + self.ev_window_created.read().last(); } } @@ -84,15 +85,15 @@ pub fn process_input_system( mut context_params: ContextSystemParams, egui_settings: Res, mut egui_mouse_position: ResMut, - time: Res