Add frustum to shader View (#10306)

# Objective
- Work towards GPU-driven culling
(https://github.com/bevyengine/bevy/pull/10164)

## Solution
- Pass the view frustum to the shader view uniform

---

## Changelog
- View Frustums are now extracted to the render world and made available
to shaders
This commit is contained in:
JMS55 2023-10-30 19:00:21 -07:00 committed by GitHub
parent d3e41e2ff7
commit 3628e09045
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 1 deletions

View file

@ -2,6 +2,7 @@ use crate::{
camera::CameraProjection, camera::CameraProjection,
camera::{ManualTextureViewHandle, ManualTextureViews}, camera::{ManualTextureViewHandle, ManualTextureViews},
prelude::Image, prelude::Image,
primitives::Frustum,
render_asset::RenderAssets, render_asset::RenderAssets,
render_resource::TextureView, render_resource::TextureView,
view::{ColorGrading, ExtractedView, ExtractedWindows, RenderLayers, VisibleEntities}, view::{ColorGrading, ExtractedView, ExtractedWindows, RenderLayers, VisibleEntities},
@ -642,6 +643,7 @@ pub fn extract_cameras(
&CameraRenderGraph, &CameraRenderGraph,
&GlobalTransform, &GlobalTransform,
&VisibleEntities, &VisibleEntities,
&Frustum,
Option<&ColorGrading>, Option<&ColorGrading>,
Option<&TemporalJitter>, Option<&TemporalJitter>,
Option<&RenderLayers>, Option<&RenderLayers>,
@ -657,6 +659,7 @@ pub fn extract_cameras(
camera_render_graph, camera_render_graph,
transform, transform,
visible_entities, visible_entities,
frustum,
color_grading, color_grading,
temporal_jitter, temporal_jitter,
render_layers, render_layers,
@ -714,6 +717,7 @@ pub fn extract_cameras(
color_grading, color_grading,
}, },
visible_entities.clone(), visible_entities.clone(),
*frustum,
)); ));
if let Some(temporal_jitter) = temporal_jitter { if let Some(temporal_jitter) = temporal_jitter {

View file

@ -9,6 +9,7 @@ use crate::{
camera::{ExtractedCamera, ManualTextureViews, MipBias, TemporalJitter}, camera::{ExtractedCamera, ManualTextureViews, MipBias, TemporalJitter},
extract_resource::{ExtractResource, ExtractResourcePlugin}, extract_resource::{ExtractResource, ExtractResourcePlugin},
prelude::{Image, Shader}, prelude::{Image, Shader},
primitives::Frustum,
render_asset::RenderAssets, render_asset::RenderAssets,
render_phase::ViewRangefinder3d, render_phase::ViewRangefinder3d,
render_resource::{DynamicUniformBuffer, ShaderType, Texture, TextureView}, render_resource::{DynamicUniformBuffer, ShaderType, Texture, TextureView},
@ -168,6 +169,7 @@ pub struct ViewUniform {
world_position: Vec3, world_position: Vec3,
// viewport(x_origin, y_origin, width, height) // viewport(x_origin, y_origin, width, height)
viewport: Vec4, viewport: Vec4,
frustum: [Vec4; 6],
color_grading: ColorGrading, color_grading: ColorGrading,
mip_bias: f32, mip_bias: f32,
} }
@ -352,6 +354,7 @@ pub fn prepare_view_uniforms(
views: Query<( views: Query<(
Entity, Entity,
&ExtractedView, &ExtractedView,
Option<&Frustum>,
Option<&TemporalJitter>, Option<&TemporalJitter>,
Option<&MipBias>, Option<&MipBias>,
)>, )>,
@ -365,7 +368,7 @@ pub fn prepare_view_uniforms(
else { else {
return; return;
}; };
for (entity, camera, temporal_jitter, mip_bias) in &views { for (entity, camera, frustum, temporal_jitter, mip_bias) in &views {
let viewport = camera.viewport.as_vec4(); let viewport = camera.viewport.as_vec4();
let unjittered_projection = camera.projection; let unjittered_projection = camera.projection;
let mut projection = unjittered_projection; let mut projection = unjittered_projection;
@ -386,6 +389,11 @@ pub fn prepare_view_uniforms(
.unwrap_or_else(|| projection * inverse_view) .unwrap_or_else(|| projection * inverse_view)
}; };
// Map Frustum type to shader array<vec4<f32>, 6>
let frustum = frustum
.map(|frustum| frustum.half_spaces.map(|h| h.normal_d()))
.unwrap_or([Vec4::ZERO; 6]);
let view_uniforms = ViewUniformOffset { let view_uniforms = ViewUniformOffset {
offset: writer.write(&ViewUniform { offset: writer.write(&ViewUniform {
view_proj, view_proj,
@ -397,6 +405,7 @@ pub fn prepare_view_uniforms(
inverse_projection, inverse_projection,
world_position: camera.transform.translation(), world_position: camera.transform.translation(),
viewport, viewport,
frustum,
color_grading: camera.color_grading, color_grading: camera.color_grading,
mip_bias: mip_bias.unwrap_or(&MipBias(0.0)).0, mip_bias: mip_bias.unwrap_or(&MipBias(0.0)).0,
}), }),

View file

@ -18,6 +18,7 @@ struct View {
world_position: vec3<f32>, world_position: vec3<f32>,
// viewport(x_origin, y_origin, width, height) // viewport(x_origin, y_origin, width, height)
viewport: vec4<f32>, viewport: vec4<f32>,
frustum: array<vec4<f32>, 6>,
color_grading: ColorGrading, color_grading: ColorGrading,
mip_bias: f32, mip_bias: f32,
}; };