Skip to content

Commit

Permalink
Add frustum to shader View (bevyengine#10306)
Browse files Browse the repository at this point in the history
# Objective
- Work towards GPU-driven culling
(bevyengine#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
  • Loading branch information
JMS55 authored and Ray Redondo committed Jan 9, 2024
1 parent 39639e9 commit afd1d98
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
camera::CameraProjection,
camera::{ManualTextureViewHandle, ManualTextureViews},
prelude::Image,
primitives::Frustum,
render_asset::RenderAssets,
render_resource::TextureView,
view::{ColorGrading, ExtractedView, ExtractedWindows, RenderLayers, VisibleEntities},
Expand Down Expand Up @@ -642,6 +643,7 @@ pub fn extract_cameras(
&CameraRenderGraph,
&GlobalTransform,
&VisibleEntities,
&Frustum,
Option<&ColorGrading>,
Option<&TemporalJitter>,
Option<&RenderLayers>,
Expand All @@ -657,6 +659,7 @@ pub fn extract_cameras(
camera_render_graph,
transform,
visible_entities,
frustum,
color_grading,
temporal_jitter,
render_layers,
Expand Down Expand Up @@ -714,6 +717,7 @@ pub fn extract_cameras(
color_grading,
},
visible_entities.clone(),
*frustum,
));

if let Some(temporal_jitter) = temporal_jitter {
Expand Down
11 changes: 10 additions & 1 deletion crates/bevy_render/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
camera::{ExtractedCamera, ManualTextureViews, MipBias, TemporalJitter},
extract_resource::{ExtractResource, ExtractResourcePlugin},
prelude::{Image, Shader},
primitives::Frustum,
render_asset::RenderAssets,
render_phase::ViewRangefinder3d,
render_resource::{DynamicUniformBuffer, ShaderType, Texture, TextureView},
Expand Down Expand Up @@ -168,6 +169,7 @@ pub struct ViewUniform {
world_position: Vec3,
// viewport(x_origin, y_origin, width, height)
viewport: Vec4,
frustum: [Vec4; 6],
color_grading: ColorGrading,
mip_bias: f32,
}
Expand Down Expand Up @@ -352,6 +354,7 @@ pub fn prepare_view_uniforms(
views: Query<(
Entity,
&ExtractedView,
Option<&Frustum>,
Option<&TemporalJitter>,
Option<&MipBias>,
)>,
Expand All @@ -365,7 +368,7 @@ pub fn prepare_view_uniforms(
else {
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 unjittered_projection = camera.projection;
let mut projection = unjittered_projection;
Expand All @@ -386,6 +389,11 @@ pub fn prepare_view_uniforms(
.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 {
offset: writer.write(&ViewUniform {
view_proj,
Expand All @@ -397,6 +405,7 @@ pub fn prepare_view_uniforms(
inverse_projection,
world_position: camera.transform.translation(),
viewport,
frustum,
color_grading: camera.color_grading,
mip_bias: mip_bias.unwrap_or(&MipBias(0.0)).0,
}),
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_render/src/view/view.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct View {
world_position: vec3<f32>,
// viewport(x_origin, y_origin, width, height)
viewport: vec4<f32>,
frustum: array<vec4<f32>, 6>,
color_grading: ColorGrading,
mip_bias: f32,
};

0 comments on commit afd1d98

Please sign in to comment.