Skip to content

Commit

Permalink
Update to Bevy 0.12.
Browse files Browse the repository at this point in the history
Everything seems to work in both the simple example and in my own app.
  • Loading branch information
pcwalton committed Dec 22, 2023
1 parent ad61b5f commit 64da6c5
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 99 deletions.
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
91 changes: 49 additions & 42 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, 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,
},
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,14 @@ 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 {
let bind_group = render_device.create_bind_group(
"grid-view-bind-group",
&pipeline.view_layout,
&[BindGroupEntry {
binding: 0,
resource: binding.clone(),
}],
});
);
commands
.entity(entity)
.insert(GridViewBindGroup { value: bind_group });
Expand Down Expand Up @@ -416,34 +413,22 @@ 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: &[
render_device.create_bind_group(
"infinite-grid-bind-group",
&pipeline.infinite_grid_layout,
&[
BindGroupEntry {
binding: 0,
resource: position_binding.clone(),
Expand All @@ -453,12 +438,27 @@ fn queue_infinite_grids(
resource: settings_binding.clone(),
},
],
})
)
} else {
return;
};
commands.insert_resource(InfiniteGridBindGroup { value: bind_group });
}

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 +498,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 +641,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 +677,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 +697,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 +720,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),
)
.add_systems(Render, queue_infinite_grids.in_set(RenderSet::Queue));

shadow::register_shadow(app);
}
Loading

0 comments on commit 64da6c5

Please sign in to comment.