Skip to content

Commit

Permalink
upgrade to bevy 0.12 (vladbat00#221)
Browse files Browse the repository at this point in the history
* upgrade to bevy 0.12

* upgrade

* bevy release

* Update src/lib.rs

Co-authored-by: Matthew S. Klosak <[email protected]>

* 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 <[email protected]>
Co-authored-by: mvlabat <[email protected]>
  • Loading branch information
3 people authored and Schmarni-Dev committed Dec 23, 2023
1 parent 0c9d6e7 commit 3a2cdca
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 70 deletions.
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 2 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
] }
Expand All @@ -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",
] }
2 changes: 1 addition & 1 deletion examples/render_to_image_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/egui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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<Shader> = Handle::weak_from_u128(9898276442290979394);

/// Egui render pipeline.
#[derive(Resource)]
Expand Down Expand Up @@ -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(
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
38 changes: 20 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
Expand All @@ -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()
}),
}
}
Expand All @@ -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.
Expand Down Expand Up @@ -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::<Image>::Weak(*id));
}
}
}
Expand Down Expand Up @@ -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::<WinitPlugin>(),
Expand Down
30 changes: 14 additions & 16 deletions src/render_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ use crate::{
EguiManagedTextures, EguiSettings, EguiUserTextures, WindowSize,
};
use bevy::{
asset::HandleId,
ecs::system::SystemParam,
prelude::*,
render::{
extract_resource::ExtractResource,
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,
Expand Down Expand Up @@ -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<Item = (EguiTextureId, HandleId)> + '_ {
pub fn handles(&self) -> impl Iterator<Item = (EguiTextureId, AssetId<Image>)> + '_ {
self.egui_textures
.0
.iter()
Expand Down Expand Up @@ -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));
}
};
Expand All @@ -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),
Expand All @@ -199,7 +197,7 @@ pub fn queue_bind_groups_system(
resource: BindingResource::Sampler(&gpu_image.sampler),
},
],
});
);
Some((texture, bind_group))
})
.collect();
Expand Down
45 changes: 23 additions & 22 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use bevy::{
ButtonState, Input,
},
prelude::{Entity, EventReader, Query, Resource, Time},
time::Real,
window::{
CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, RequestRedraw, WindowCreated,
WindowFocused,
Expand All @@ -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();
}
}

Expand Down Expand Up @@ -84,15 +85,15 @@ pub fn process_input_system(
mut context_params: ContextSystemParams,
egui_settings: Res<EguiSettings>,
mut egui_mouse_position: ResMut<EguiMousePosition>,
time: Res<Time>,
time: Res<Time<Real>>,
) {
// This is a workaround for Windows. For some reason, `WindowFocused` event isn't fired
// when a window is created.
if let Some(event) = input_events.ev_window_created.iter().last() {
if let Some(event) = input_events.ev_window_created.read().last() {
*context_params.focused_window = Some(event.window);
}

for event in input_events.ev_window_focused.iter() {
for event in input_events.ev_window_focused.read() {
*context_params.focused_window = if event.focused {
Some(event.window)
} else {
Expand Down Expand Up @@ -127,12 +128,12 @@ pub fn process_input_system(
};

let mut cursor_left_window = None;
if let Some(cursor_left) = input_events.ev_cursor_left.iter().last() {
if let Some(cursor_left) = input_events.ev_cursor_left.read().last() {
cursor_left_window = Some(cursor_left.window);
}
let cursor_entered_window = input_events
.ev_cursor_entered
.iter()
.read()
.last()
.map(|event| event.window);

Expand All @@ -147,7 +148,7 @@ pub fn process_input_system(
None
};

if let Some(cursor_moved) = input_events.ev_cursor.iter().last() {
if let Some(cursor_moved) = input_events.ev_cursor.read().last() {
// If we've left the window, it's unlikely that we've moved the cursor back to the same
// window this exact frame, so we are safe to ignore all `CursorMoved` events for the window
// that has been left.
Expand Down Expand Up @@ -176,7 +177,7 @@ pub fn process_input_system(
if let Ok(mut context) = context_params.contexts.get_mut(window_id) {
let events = &mut context.egui_input.events;

for mouse_button_event in input_events.ev_mouse_button_input.iter() {
for mouse_button_event in input_events.ev_mouse_button_input.read() {
let button = match mouse_button_event.button {
MouseButton::Left => Some(egui::PointerButton::Primary),
MouseButton::Right => Some(egui::PointerButton::Secondary),
Expand All @@ -197,7 +198,7 @@ pub fn process_input_system(
}
}

for event in input_events.ev_mouse_wheel.iter() {
for event in input_events.ev_mouse_wheel.read() {
let mut delta = egui::vec2(event.x, event.y);
if let MouseScrollUnit::Line = event.unit {
// https://github.com/emilk/egui/blob/a689b623a669d54ea85708a8c748eb07e23754b0/egui-winit/src/lib.rs#L449
Expand All @@ -220,7 +221,7 @@ pub fn process_input_system(
}

if !command || cfg!(target_os = "windows") && ctrl && alt {
for event in input_events.ev_received_character.iter() {
for event in input_events.ev_received_character.read() {
if !event.char.is_control() {
let mut context = context_params.contexts.get_mut(event.window).unwrap();
context
Expand All @@ -242,7 +243,7 @@ pub fn process_input_system(
}
})
{
for ev in input_events.ev_keyboard_input.iter() {
for ev in input_events.ev_keyboard_input.read() {
if let Some(key) = ev.key_code.and_then(bevy_to_egui_key) {
let pressed = match ev.state {
ButtonState::Pressed => true,
Expand Down Expand Up @@ -278,7 +279,7 @@ pub fn process_input_system(
}
}

for touch in input_events.ev_touch.iter() {
for touch in input_events.ev_touch.read() {
let scale_factor = egui_settings.scale_factor as f32;
let touch_position: (f32, f32) = (touch.position / scale_factor).into();

Expand Down Expand Up @@ -358,7 +359,7 @@ pub fn process_input_system(
}

for mut context in context_params.contexts.iter_mut() {
context.egui_input.predicted_dt = time.raw_delta_seconds();
context.egui_input.predicted_dt = time.delta_seconds();
}

// In some cases, we may skip certain events. For example, we ignore `ReceivedCharacter` events
Expand Down

0 comments on commit 3a2cdca

Please sign in to comment.