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

Update to Bevy 0.12. #43

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -15,15 +15,15 @@ keywords = ["bevy"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = { version = "0.11.0", default-features = false, features = [
bevy = { version = "0.12.0", default-features = false, features = [
"bevy_render",
"bevy_core_pipeline",
"bevy_pbr",
"bevy_asset",
] }

[dev-dependencies]
bevy = { version = "0.11.0", default-features = false, features = [
bevy = { version = "0.12.0", default-features = false, features = [
"bevy_winit",
"x11",
"tonemapping_luts",
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ mod camera_controller {
// Handle mouse input
let mut mouse_delta = Vec2::ZERO;
if mouse_button_input.pressed(MouseButton::Left) {
for mouse_event in mouse_events.iter() {
for mouse_event in mouse_events.read() {
mouse_delta += mouse_event.delta;
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ pub struct InfiniteGridBundle {
pub grid: InfiniteGrid,
pub frustum_intersect: GridFrustumIntersect,
pub visibility: Visibility,
pub computed_visibility: ComputedVisibility,
pub view_visibility: ViewVisibility,
pub inherited_visibility: InheritedVisibility,
pub shadow_casters: VisibleEntities,
pub no_frustum_culling: NoFrustumCulling,
}
Expand Down Expand Up @@ -231,21 +232,21 @@ fn track_caster_visibility(
(
Entity,
&Visibility,
&mut ComputedVisibility,
&mut InheritedVisibility,
Option<(&GlobalTransform, &Aabb)>,
),
(With<Handle<Mesh>>, Without<NotShadowCaster>),
>,
) {
for (mut visibles, _grid_transform, _grid) in grids.iter_mut() {
visibles.entities.clear();
for (entity, visibility, mut computed, _intersect_testable) in meshes.iter_mut() {
for (entity, visibility, mut inherited_visibility, _intersect_testable) in meshes.iter_mut() {
if let Visibility::Hidden = visibility {
continue;
}

// TODO: add a check here for if the projection of the aabb onto the plane has any overlap with the grid frustum intersect
computed.set_visible_in_view();
*inherited_visibility = InheritedVisibility::VISIBLE;
visibles.entities.push(entity);
}
}
Expand Down
104 changes: 50 additions & 54 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@ use bevy::{
},
pbr::MeshPipelineKey,
prelude::*,
reflect::TypeUuid,
render::{
mesh::PrimitiveTopology,
render_phase::{
AddRenderCommand, DrawFunctions, PhaseItem, RenderCommand, RenderCommandResult,
RenderPhase, SetItemPipeline,
},
render_resource::{
BindGroup, BindGroupDescriptor, BindGroupEntry, BindGroupLayout,
BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingType, BlendState,
BufferBindingType, BufferSize, ColorTargetState, ColorWrites, CompareFunction,
DepthBiasState, DepthStencilState, DynamicUniformBuffer, FragmentState,
MultisampleState, PipelineCache, PolygonMode, PrimitiveState, RenderPipelineDescriptor,
SamplerBindingType, ShaderStages, ShaderType, SpecializedRenderPipeline,
SpecializedRenderPipelines, StencilFaceState, StencilState, TextureFormat,
TextureSampleType, TextureViewDimension, VertexState,
BindGroup, BindGroupEntries, BindGroupLayout, BindGroupLayoutDescriptor,
BindGroupLayoutEntry, BindingType, BlendState, BufferBindingType, BufferSize,
ColorTargetState, ColorWrites, CompareFunction, DepthBiasState, DepthStencilState,
DynamicUniformBuffer, FragmentState, MultisampleState, PipelineCache, PolygonMode,
PrimitiveState, RenderPipelineDescriptor, SamplerBindingType, ShaderStages, ShaderType,
SpecializedRenderPipeline, SpecializedRenderPipelines, StencilFaceState, StencilState,
TextureFormat, TextureSampleType, TextureViewDimension, VertexState,
},
renderer::{RenderDevice, RenderQueue},
texture::BevyDefault,
Expand All @@ -45,8 +43,7 @@ use shadow::{GridShadow, SetGridShadowBindGroup};

static PLANE_RENDER: &str = include_str!("plane_render.wgsl");

const SHADER_HANDLE: HandleUntyped =
HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 15204473893972682982);
const SHADER_HANDLE: Handle<Shader> = Handle::weak_from_u128(15204473893972682982);

#[derive(Component)]
struct ExtractedInfiniteGrid {
Expand Down Expand Up @@ -212,7 +209,7 @@ impl<P: PhaseItem> RenderCommand<P> for FinishDrawInfiniteGrid {
}
}

fn prepare_grid_view_bind_groups(
fn prepare_grid_view_uniforms(
mut commands: Commands,
render_device: Res<RenderDevice>,
render_queue: Res<RenderQueue>,
Expand Down Expand Up @@ -240,7 +237,7 @@ fn prepare_grid_view_bind_groups(
.write_buffer(&render_device, &render_queue)
}

fn queue_grid_view_bind_groups(
fn prepare_grid_view_bind_groups(
mut commands: Commands,
render_device: Res<RenderDevice>,
uniforms: Res<GridViewUniforms>,
Expand All @@ -249,14 +246,11 @@ fn queue_grid_view_bind_groups(
) {
if let Some(binding) = uniforms.uniforms.binding() {
for entity in views.iter() {
let bind_group = render_device.create_bind_group(&BindGroupDescriptor {
label: Some("grid-view-bind-group"),
layout: &pipeline.view_layout,
entries: &[BindGroupEntry {
binding: 0,
resource: binding.clone(),
}],
});
let bind_group = render_device.create_bind_group(
"grid-view-bind-group",
&pipeline.view_layout,
&BindGroupEntries::single(binding.clone()),
);
commands
.entity(entity)
.insert(GridViewBindGroup { value: bind_group });
Expand Down Expand Up @@ -416,49 +410,44 @@ fn prepare_grid_shadows(
.write_buffer(&render_device, &render_queue);
}

#[allow(clippy::too_many_arguments)]
fn queue_infinite_grids(
pipeline_cache: Res<PipelineCache>,
transparent_draw_functions: Res<DrawFunctions<Transparent3d>>,
fn prepare_bind_groups_for_infinite_grids(
mut commands: Commands,
position_uniforms: Res<InfiniteGridUniforms>,
settings_uniforms: Res<GridDisplaySettingsUniforms>,
pipeline: Res<InfiniteGridPipeline>,
mut pipelines: ResMut<SpecializedRenderPipelines<InfiniteGridPipeline>>,
render_device: Res<RenderDevice>,
infinite_grids: Query<&ExtractedInfiniteGrid>,
intersects: Query<&GridFrustumIntersect>,
mut views: Query<(
&VisibleEntities,
&mut RenderPhase<Transparent3d>,
&ExtractedView,
)>,
msaa: Res<Msaa>,
) {
let bind_group = if let Some((position_binding, settings_binding)) = position_uniforms
.uniforms
.binding()
.zip(settings_uniforms.uniforms.binding())
{
render_device.create_bind_group(&BindGroupDescriptor {
label: Some("infinite-grid-bind-group"),
layout: &pipeline.infinite_grid_layout,
entries: &[
BindGroupEntry {
binding: 0,
resource: position_binding.clone(),
},
BindGroupEntry {
binding: 1,
resource: settings_binding.clone(),
},
],
})
render_device.create_bind_group(
"infinite-grid-bind-group",
&pipeline.infinite_grid_layout,
&BindGroupEntries::sequential((position_binding.clone(), settings_binding.clone())),
)
} else {
return;
};
commands.insert_resource(InfiniteGridBindGroup { value: bind_group });
}

#[allow(clippy::too_many_arguments)]
fn queue_infinite_grids(
pipeline_cache: Res<PipelineCache>,
transparent_draw_functions: Res<DrawFunctions<Transparent3d>>,
pipeline: Res<InfiniteGridPipeline>,
mut pipelines: ResMut<SpecializedRenderPipelines<InfiniteGridPipeline>>,
infinite_grids: Query<&ExtractedInfiniteGrid>,
intersects: Query<&GridFrustumIntersect>,
mut views: Query<(
&VisibleEntities,
&mut RenderPhase<Transparent3d>,
&ExtractedView,
)>,
msaa: Res<Msaa>,
) {
let draw_function_id = transparent_draw_functions
.read()
.get_id::<DrawInfiniteGrid>()
Expand Down Expand Up @@ -498,6 +487,8 @@ fn queue_infinite_grids(
entity,
draw_function: draw_function_id,
distance: f32::NEG_INFINITY,
batch_range: 0..1,
dynamic_offset: None,
});
}
}
Expand Down Expand Up @@ -639,7 +630,7 @@ impl SpecializedRenderPipeline for InfiniteGridPipeline {
.collect(),
push_constant_ranges: Vec::new(),
vertex: VertexState {
shader: SHADER_HANDLE.typed(),
shader: SHADER_HANDLE,
shader_defs: vec![],
entry_point: Cow::Borrowed("vertex"),
buffers: vec![],
Expand Down Expand Up @@ -675,7 +666,7 @@ impl SpecializedRenderPipeline for InfiniteGridPipeline {
alpha_to_coverage_enabled: false,
},
fragment: Some(FragmentState {
shader: SHADER_HANDLE.typed(),
shader: SHADER_HANDLE,
shader_defs: key
.has_shadows
.then(|| "SHADOWS".into())
Expand All @@ -695,7 +686,7 @@ impl SpecializedRenderPipeline for InfiniteGridPipeline {
pub fn render_app_builder(app: &mut App) {
app.world
.resource_mut::<Assets<Shader>>()
.set_untracked(SHADER_HANDLE, Shader::from_wgsl(PLANE_RENDER, file!()));
.get_or_insert_with(SHADER_HANDLE, || Shader::from_wgsl(PLANE_RENDER, file!()));

let Ok(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
Expand All @@ -718,14 +709,19 @@ pub fn render_app_builder(app: &mut App) {
(
prepare_infinite_grids,
prepare_grid_shadows,
prepare_grid_view_bind_groups,
prepare_grid_view_uniforms,
)
.in_set(RenderSet::Prepare),
)
.add_systems(
Render,
(queue_infinite_grids, queue_grid_view_bind_groups).in_set(RenderSet::Queue),
);
(
prepare_bind_groups_for_infinite_grids,
prepare_grid_view_bind_groups,
)
.in_set(RenderSet::PrepareBindGroups),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand why this is necessary. I understand that the sets were reordered but the Queue set should still be used to queue things. I haven't touched this code that much though so it's possible it's doing something incompatible with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem was that you put bind group creation and queuing into the same system, while Bevy 0.12 wants them to be in separate systems. I've split up the logic appropriately.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thank you for the investigation, I didn't implement the crate originally so I wasn't aware of that.

)
.add_systems(Render, queue_infinite_grids.in_set(RenderSet::Queue));

shadow::register_shadow(app);
}
Loading
Loading