Path: blob/main/crates/bevy_render/src/experimental/occlusion_culling/mod.rs
6598 views
//! GPU occlusion culling.1//!2//! See [`OcclusionCulling`] for a detailed description of occlusion culling in3//! Bevy.45use bevy_app::{App, Plugin};6use bevy_ecs::{component::Component, entity::Entity, prelude::ReflectComponent};7use bevy_reflect::{prelude::ReflectDefault, Reflect};8use bevy_shader::load_shader_library;910use crate::{extract_component::ExtractComponent, render_resource::TextureView};1112/// Enables GPU occlusion culling.13///14/// See [`OcclusionCulling`] for a detailed description of occlusion culling in15/// Bevy.16pub struct OcclusionCullingPlugin;1718impl Plugin for OcclusionCullingPlugin {19fn build(&self, app: &mut App) {20load_shader_library!(app, "mesh_preprocess_types.wgsl");21}22}2324/// Add this component to a view in order to enable experimental GPU occlusion25/// culling.26///27/// *Bevy's occlusion culling is currently marked as experimental.* There are28/// known issues whereby, in rare circumstances, occlusion culling can result in29/// meshes being culled that shouldn't be (i.e. meshes that turn invisible).30/// Please try it out and report issues.31///32/// *Occlusion culling* allows Bevy to avoid rendering objects that are fully33/// behind other opaque or alpha tested objects. This is different from, and34/// complements, depth fragment rejection as the `DepthPrepass` enables. While35/// depth rejection allows Bevy to avoid rendering *pixels* that are behind36/// other objects, the GPU still has to examine those pixels to reject them,37/// which requires transforming the vertices of the objects and performing38/// skinning if the objects were skinned. Occlusion culling allows the GPU to go39/// a step further, avoiding even transforming the vertices of objects that it40/// can quickly prove to be behind other objects.41///42/// Occlusion culling inherently has some overhead, because Bevy must examine43/// the objects' bounding boxes, and create an acceleration structure44/// (hierarchical Z-buffer) to perform the occlusion tests. Therefore, occlusion45/// culling is disabled by default. Only enable it if you measure it to be a46/// speedup on your scene. Note that, because Bevy's occlusion culling runs on47/// the GPU and is quite efficient, it's rare for occlusion culling to result in48/// a significant slowdown.49///50/// Occlusion culling currently requires a `DepthPrepass`. If no depth prepass51/// is present on the view, the [`OcclusionCulling`] component will be ignored.52/// Additionally, occlusion culling is currently incompatible with deferred53/// shading; including both `DeferredPrepass` and [`OcclusionCulling`] results54/// in unspecified behavior.55///56/// The algorithm that Bevy uses is known as [*two-phase occlusion culling*].57/// When you enable occlusion culling, Bevy splits the depth prepass into two:58/// an *early* depth prepass and a *late* depth prepass. The early depth prepass59/// renders all the meshes that were visible last frame to produce a60/// conservative approximation of the depth buffer. Then, after producing an61/// acceleration structure known as a hierarchical Z-buffer or depth pyramid,62/// Bevy tests the bounding boxes of all meshes against that depth buffer. Those63/// that can be quickly proven to be behind the geometry rendered during the64/// early depth prepass are skipped entirely. The other potentially-visible65/// meshes are rendered during the late prepass, and finally all the visible66/// meshes are rendered as usual during the opaque, transparent, etc. passes.67///68/// Unlike other occlusion culling systems you may be familiar with, Bevy's69/// occlusion culling is fully dynamic and requires no baking step. The CPU70/// overhead is minimal. Large skinned meshes and other dynamic objects can71/// occlude other objects.72///73/// [*two-phase occlusion culling*]:74/// https://medium.com/@mil_kru/two-pass-occlusion-culling-4100edcad50175#[derive(Component, ExtractComponent, Clone, Copy, Default, Reflect)]76#[reflect(Component, Default, Clone)]77pub struct OcclusionCulling;7879/// A render-world component that contains resources necessary to perform80/// occlusion culling on any view other than a camera.81///82/// Bevy automatically places this component on views created for shadow83/// mapping. You don't ordinarily need to add this component yourself.84#[derive(Clone, Component)]85pub struct OcclusionCullingSubview {86/// A texture view of the Z-buffer.87pub depth_texture_view: TextureView,88/// The size of the texture along both dimensions.89///90/// Because [`OcclusionCullingSubview`] is only currently used for shadow91/// maps, they're guaranteed to have sizes equal to a power of two, so we92/// don't have to store the two dimensions individually here.93pub depth_texture_size: u32,94}9596/// A render-world component placed on each camera that stores references to all97/// entities other than cameras that need occlusion culling.98///99/// Bevy automatically places this component on cameras that are drawing100/// shadows, when those shadows come from lights with occlusion culling enabled.101/// You don't ordinarily need to add this component yourself.102#[derive(Clone, Component)]103pub struct OcclusionCullingSubviewEntities(pub Vec<Entity>);104105106