Skip to content
Open
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
9 changes: 3 additions & 6 deletions crates/bevy_camera/src/visibility/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bevy_app::{App, Plugin, PostUpdate};
use bevy_ecs::{
component::Component,
entity::{Entity, EntityHashMap},
query::{With, Without},
query::With,
reflect::ReflectComponent,
resource::Resource,
schedule::IntoScheduleConfigs as _,
Expand All @@ -22,7 +22,7 @@ use bevy_transform::components::GlobalTransform;
use bevy_utils::Parallel;

use super::{check_visibility_cpu_culling, VisibilitySystems};
use crate::{camera::Camera, primitives::Aabb, visibility::NoCpuCulling};
use crate::{camera::Camera, primitives::Aabb};

/// A plugin that enables [`VisibilityRange`]s, which allow entities to be
/// hidden or shown based on distance to the camera.
Expand Down Expand Up @@ -243,10 +243,7 @@ pub fn check_visibility_ranges(
mut visible_entity_ranges: ResMut<VisibleEntityRanges>,
view_query: Query<(Entity, &GlobalTransform), With<Camera>>,
mut par_local: Local<Parallel<Vec<(Entity, u32)>>>,
entity_query: Query<
(Entity, &GlobalTransform, Option<&Aabb>, &VisibilityRange),
Without<NoCpuCulling>,
>,
entity_query: Query<(Entity, &GlobalTransform, Option<&Aabb>, &VisibilityRange)>,
) {
visible_entity_ranges.clear();

Expand Down
66 changes: 21 additions & 45 deletions crates/bevy_pbr/src/render/gpu_preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use bevy_ecs::{
world::{FromWorld, World},
};
use bevy_log::warn_once;
use bevy_math::Vec4;
use bevy_platform::collections::HashMap;
use bevy_render::{
batching::gpu_preprocessing::{
Expand All @@ -51,18 +50,17 @@ use bevy_render::{
render_resource::{
binding_types::{storage_buffer, storage_buffer_read_only, texture_2d, uniform_buffer},
BindGroup, BindGroupEntries, BindGroupLayoutDescriptor, BindGroupLayoutEntries,
BindingResource, Buffer, BufferBinding, BufferVec, CachedComputePipelineId,
ComputePassDescriptor, ComputePipelineDescriptor, DynamicBindGroupLayoutEntries,
PartialBufferVec, PipelineCache, RawBufferVec, ShaderStages, ShaderType,
SparseBufferUpdateBindGroups, SparseBufferUpdateJobs, SparseBufferUpdatePipelines,
SpecializedComputePipeline, SpecializedComputePipelines, TextureSampleType,
UninitBufferVec,
BindingResource, Buffer, BufferBinding, CachedComputePipelineId, ComputePassDescriptor,
ComputePipelineDescriptor, DynamicBindGroupLayoutEntries, PartialBufferVec, PipelineCache,
RawBufferVec, ShaderStages, ShaderType, SparseBufferUpdateBindGroups,
SparseBufferUpdateJobs, SparseBufferUpdatePipelines, SpecializedComputePipeline,
SpecializedComputePipelines, TextureSampleType, UninitBufferVec,
},
renderer::{RenderContext, RenderDevice, RenderQueue, ViewQuery},
settings::WgpuFeatures,
view::{
ExtractedView, NoIndirectDrawing, RenderVisibilityRanges, RetainedViewEntity, ViewUniform,
ViewUniformOffset, ViewUniforms,
ExtractedView, NoIndirectDrawing, RetainedViewEntity, ViewUniform, ViewUniformOffset,
ViewUniforms,
},
GpuResourceAppExt, Render, RenderApp, RenderSystems,
};
Expand Down Expand Up @@ -1267,19 +1265,19 @@ impl FromWorld for PreprocessPipelines {
let gpu_early_occlusion_culling_bind_group_layout_entries =
gpu_occlusion_culling_bind_group_layout_entries().extend_with_indices((
(
12,
11,
storage_buffer::<PreprocessWorkItem>(/*has_dynamic_offset=*/ false),
),
(
13,
12,
storage_buffer::<LatePreprocessWorkItemIndirectParameters>(
/*has_dynamic_offset=*/ false,
),
),
));
let gpu_late_occlusion_culling_bind_group_layout_entries =
gpu_occlusion_culling_bind_group_layout_entries().extend_with_indices(((
13,
12,
storage_buffer_read_only::<LatePreprocessWorkItemIndirectParameters>(
/*has_dynamic_offset=*/ false,
),
Expand Down Expand Up @@ -1470,11 +1468,6 @@ fn gpu_culling_bind_group_layout_entries() -> DynamicBindGroupLayoutEntries {
9,
storage_buffer_read_only::<MeshCullingData>(/* has_dynamic_offset= */ false),
),
// `visibility_ranges`
(
10,
storage_buffer_read_only::<Vec4>(/* has_dynamic_offset= */ false),
),
))
}

Expand All @@ -1485,7 +1478,7 @@ fn gpu_occlusion_culling_bind_group_layout_entries() -> DynamicBindGroupLayoutEn
uniform_buffer::<PreviousViewData>(/*has_dynamic_offset=*/ false),
),
(
11,
10,
texture_2d(TextureSampleType::Float { filterable: true }),
),
))
Expand Down Expand Up @@ -1805,7 +1798,6 @@ pub fn prepare_preprocess_bind_groups(
indirect_parameters_buffers: Res<IndirectParametersBuffers>,
bin_unpacking_buffers: Res<BinUnpackingBuffers>,
mesh_culling_data_buffer: Res<MeshCullingDataBuffer>,
visibility_ranges: Res<RenderVisibilityRanges>,
view_uniforms: Res<ViewUniforms>,
previous_view_uniforms: Res<PreviousViewUniforms>,
pipelines: Res<PreprocessPipelines>,
Expand Down Expand Up @@ -1866,7 +1858,6 @@ pub fn prepare_preprocess_bind_groups(
pipeline_cache: &pipeline_cache,
phase_indirect_parameters_buffers,
mesh_culling_data_buffer: &mesh_culling_data_buffer,
visibility_range_data_buffer: visibility_ranges.buffer(),
view_uniforms: &view_uniforms,
previous_view_uniforms: &previous_view_uniforms,
pipelines: &pipelines,
Expand Down Expand Up @@ -1976,9 +1967,6 @@ struct PreprocessBindGroupBuilder<'a> {
phase_indirect_parameters_buffers: &'a UntypedPhaseIndirectParametersBuffers,
/// The GPU buffer that stores the information needed to cull each mesh.
mesh_culling_data_buffer: &'a MeshCullingDataBuffer,
/// The device buffer that stores the information needed to process
/// visibility ranges on the GPU.
visibility_range_data_buffer: &'a BufferVec<Vec4>,
/// The GPU buffer that stores information about the view.
view_uniforms: &'a ViewUniforms,
/// The GPU buffer that stores information about the view from last frame.
Expand Down Expand Up @@ -2095,7 +2083,6 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
late_indexed_work_item_buffer: &UninitBufferVec<PreprocessWorkItem>,
) -> Option<BindGroup> {
let mesh_culling_data_buffer = self.mesh_culling_data_buffer.buffer()?;
let visibility_range_binding = self.visibility_range_data_buffer.binding()?;
let view_uniforms_binding = self.view_uniforms.uniforms.binding()?;
let previous_view_buffer = self.previous_view_uniforms.uniforms.buffer()?;

Expand Down Expand Up @@ -2150,9 +2137,8 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
(7, indexed_cpu_metadata_buffer.as_entire_binding()),
(8, indexed_gpu_metadata_buffer.as_entire_binding()),
(9, mesh_culling_data_buffer.as_entire_binding()),
(10, visibility_range_binding.clone()),
(0, view_uniforms_binding.clone()),
(11, &view_depth_pyramid.all_mips),
(10, &view_depth_pyramid.all_mips),
(
2,
BufferBinding {
Expand All @@ -2162,15 +2148,15 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
},
),
(
12,
11,
BufferBinding {
buffer: late_indexed_work_item_gpu_buffer,
offset: 0,
size: indexed_work_item_buffer_size,
},
),
(
13,
12,
BufferBinding {
buffer: late_indexed_indirect_parameters_buffer,
offset: 0,
Expand All @@ -2197,7 +2183,6 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
late_non_indexed_work_item_buffer: &UninitBufferVec<PreprocessWorkItem>,
) -> Option<BindGroup> {
let mesh_culling_data_buffer = self.mesh_culling_data_buffer.buffer()?;
let visibility_range_binding = self.visibility_range_data_buffer.binding()?;
let view_uniforms_binding = self.view_uniforms.uniforms.binding()?;
let previous_view_buffer = self.previous_view_uniforms.uniforms.buffer()?;

Expand Down Expand Up @@ -2252,9 +2237,8 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
(7, non_indexed_cpu_metadata_buffer.as_entire_binding()),
(8, non_indexed_gpu_metadata_buffer.as_entire_binding()),
(9, mesh_culling_data_buffer.as_entire_binding()),
(10, visibility_range_binding.clone()),
(0, view_uniforms_binding.clone()),
(11, &view_depth_pyramid.all_mips),
(10, &view_depth_pyramid.all_mips),
(
2,
BufferBinding {
Expand All @@ -2264,15 +2248,15 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
},
),
(
12,
11,
BufferBinding {
buffer: late_non_indexed_work_item_buffer,
offset: 0,
size: non_indexed_work_item_buffer_size,
},
),
(
13,
12,
BufferBinding {
buffer: late_non_indexed_indirect_parameters_buffer,
offset: 0,
Expand All @@ -2298,7 +2282,6 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
late_indexed_work_item_buffer: &UninitBufferVec<PreprocessWorkItem>,
) -> Option<BindGroup> {
let mesh_culling_data_buffer = self.mesh_culling_data_buffer.buffer()?;
let visibility_range_binding = self.visibility_range_data_buffer.binding()?;
let view_uniforms_binding = self.view_uniforms.uniforms.binding()?;
let previous_view_buffer = self.previous_view_uniforms.uniforms.buffer()?;

Expand Down Expand Up @@ -2351,9 +2334,8 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
(7, indexed_cpu_metadata_buffer.as_entire_binding()),
(8, indexed_gpu_metadata_buffer.as_entire_binding()),
(9, mesh_culling_data_buffer.as_entire_binding()),
(10, visibility_range_binding.clone()),
(0, view_uniforms_binding.clone()),
(11, &view_depth_pyramid.all_mips),
(10, &view_depth_pyramid.all_mips),
(
2,
BufferBinding {
Expand All @@ -2363,7 +2345,7 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
},
),
(
13,
12,
BufferBinding {
buffer: late_indexed_indirect_parameters_buffer,
offset: 0,
Expand All @@ -2389,7 +2371,6 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
late_non_indexed_work_item_buffer: &UninitBufferVec<PreprocessWorkItem>,
) -> Option<BindGroup> {
let mesh_culling_data_buffer = self.mesh_culling_data_buffer.buffer()?;
let visibility_range_binding = self.visibility_range_data_buffer.binding()?;
let view_uniforms_binding = self.view_uniforms.uniforms.binding()?;
let previous_view_buffer = self.previous_view_uniforms.uniforms.buffer()?;

Expand Down Expand Up @@ -2442,9 +2423,8 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
(7, non_indexed_cpu_metadata_buffer.as_entire_binding()),
(8, non_indexed_gpu_metadata_buffer.as_entire_binding()),
(9, mesh_culling_data_buffer.as_entire_binding()),
(10, visibility_range_binding.clone()),
(0, view_uniforms_binding.clone()),
(11, &view_depth_pyramid.all_mips),
(10, &view_depth_pyramid.all_mips),
(
2,
BufferBinding {
Expand All @@ -2454,7 +2434,7 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
},
),
(
13,
12,
BufferBinding {
buffer: late_non_indexed_indirect_parameters_buffer,
offset: 0,
Expand Down Expand Up @@ -2494,7 +2474,6 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
indexed_work_item_buffer: &PartialBufferVec<PreprocessWorkItem>,
) -> Option<BindGroup> {
let mesh_culling_data_buffer = self.mesh_culling_data_buffer.buffer()?;
let visibility_range_binding = self.visibility_range_data_buffer.binding()?;
let view_uniforms_binding = self.view_uniforms.uniforms.binding()?;

match (
Expand Down Expand Up @@ -2544,7 +2523,6 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
(7, indexed_cpu_metadata_buffer.as_entire_binding()),
(8, indexed_gpu_metadata_buffer.as_entire_binding()),
(9, mesh_culling_data_buffer.as_entire_binding()),
(10, visibility_range_binding.clone()),
(0, view_uniforms_binding.clone()),
)),
),
Expand All @@ -2561,7 +2539,6 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
non_indexed_work_item_buffer: &PartialBufferVec<PreprocessWorkItem>,
) -> Option<BindGroup> {
let mesh_culling_data_buffer = self.mesh_culling_data_buffer.buffer()?;
let visibility_range_binding = self.visibility_range_data_buffer.binding()?;
let view_uniforms_binding = self.view_uniforms.uniforms.binding()?;

match (
Expand Down Expand Up @@ -2611,7 +2588,6 @@ impl<'a> PreprocessBindGroupBuilder<'a> {
(7, non_indexed_cpu_metadata_buffer.as_entire_binding()),
(8, non_indexed_gpu_metadata_buffer.as_entire_binding()),
(9, mesh_culling_data_buffer.as_entire_binding()),
(10, visibility_range_binding.clone()),
(0, view_uniforms_binding.clone()),
)),
),
Expand Down
20 changes: 4 additions & 16 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,12 +707,6 @@ bitflags::bitflags! {
///
/// This will be `u16::MAX` if this mesh has no LOD.
const LOD_INDEX_MASK = (1 << 16) - 1;
/// Whether visibility ranges use the center of the AABB to compute
/// distance from the camera.
///
/// If false, this uses distance from the world-space translation of the
/// mesh instead.
const AABB_BASED_VISIBILITY_RANGE = 1 << 27;
/// Disables frustum culling for this mesh.
///
/// This corresponds to the
Expand All @@ -732,7 +726,6 @@ impl MeshFlags {
fn from_components(
transform: &GlobalTransform,
lod_index: Option<NonMaxU16>,
visibility_range: Option<&VisibilityRange>,
no_frustum_culling: bool,
not_shadow_receiver: bool,
transmitted_receiver: bool,
Expand All @@ -742,9 +735,6 @@ impl MeshFlags {
} else {
MeshFlags::SHADOW_RECEIVER
};
if visibility_range.is_some_and(|visibility_range| visibility_range.use_aabb) {
mesh_flags |= MeshFlags::AABB_BASED_VISIBILITY_RANGE;
}
if no_frustum_culling {
mesh_flags |= MeshFlags::NO_FRUSTUM_CULLING;
}
Expand Down Expand Up @@ -1774,7 +1764,7 @@ pub fn extract_meshes_for_cpu_building(
Has<TransmittedShadowReceiver>,
Has<NotShadowCaster>,
Has<NoAutomaticBatching>,
Option<&VisibilityRange>,
Has<VisibilityRange>,
Option<&RenderLayers>,
)>,
>,
Expand Down Expand Up @@ -1802,14 +1792,13 @@ pub fn extract_meshes_for_cpu_building(
}

let mut lod_index = None;
if visibility_range.is_some() {
if visibility_range {
lod_index = render_visibility_ranges.lod_index_for_entity(entity.into());
}

let mesh_flags = MeshFlags::from_components(
transform,
lod_index,
visibility_range,
no_frustum_culling,
not_shadow_receiver,
transmitted_receiver,
Expand Down Expand Up @@ -1886,7 +1875,7 @@ type GpuMeshExtractionQuery = (
Has<NoAutomaticBatching>,
Has<NoCpuCulling>,
),
Option<Read<VisibilityRange>>,
Has<VisibilityRange>,
Option<Read<RenderLayers>>,
);

Expand Down Expand Up @@ -2115,15 +2104,14 @@ fn extract_mesh_for_gpu_building(

// If the entity has a visibility range, determine its LOD index.
let mut lod_index = None;
if visibility_range.is_some() {
if visibility_range {
lod_index = render_visibility_ranges.lod_index_for_entity(entity.into());
}

// Calculate the mesh flags.
let mesh_flags = MeshFlags::from_components(
transform,
lod_index,
visibility_range,
no_frustum_culling,
not_shadow_receiver,
transmitted_receiver,
Expand Down
Loading
Loading