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

upgrade to bevy 0.12 #221

Merged
merged 11 commits into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 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,7 +36,7 @@ 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",
Expand Down
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
52 changes: 32 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ 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,
window::{PrimaryWindow, Window},
};
use std::borrow::Cow;
use std::{borrow::Cow, mem::discriminant};
#[cfg(all(feature = "manage_clipboard", not(target_arch = "wasm32")))]
use std::cell::{RefCell, RefMut};
#[cfg(all(feature = "manage_clipboard", not(target_arch = "wasm32")))]
Expand Down Expand Up @@ -143,8 +143,18 @@ impl PartialEq for EguiSettings {
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
(ImageSampler::Descriptor(a), ImageSampler::Descriptor(b)) => {
a.label == b.label
&& discriminant(&a.address_mode_u) == discriminant(&b.address_mode_u)
&& discriminant(&a.address_mode_v) == discriminant(&b.address_mode_v)
&& discriminant(&a.address_mode_w) == discriminant(&b.address_mode_w)
&& discriminant(&a.mag_filter) == discriminant(&b.mag_filter)
&& discriminant(&a.min_filter) == discriminant(&b.min_filter)
&& discriminant(&a.mipmap_filter) == discriminant(&b.mipmap_filter)
&& a.lod_min_clamp == b.lod_min_clamp
&& a.lod_max_clamp == b.lod_max_clamp
&& a.anisotropy_clamp == b.anisotropy_clamp
&& discriminant(&a.border_color) == discriminant(&b.border_color)
vladbat00 marked this conversation as resolved.
Show resolved Hide resolved
}
_ => false,
}
Expand All @@ -156,10 +166,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 +178,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 @@ -802,9 +812,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 @@ -838,10 +848,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 @@ -58,7 +56,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 @@ -157,14 +155,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 @@ -186,11 +184,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 @@ -200,7 +198,7 @@ pub fn queue_bind_groups_system(
resource: BindingResource::Sampler(&gpu_image.sampler),
},
],
});
);
Some((texture, bind_group))
})
.collect();
Expand Down
42 changes: 21 additions & 21 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,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 @@ -88,11 +88,11 @@ pub fn process_input_system(
) {
// 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 +127,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 +147,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 +176,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 +197,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 +220,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 +242,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 +278,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 +358,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();
raffaeleragni marked this conversation as resolved.
Show resolved Hide resolved
}

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