use const Vec2 in lights cluster and bounding box when possible (#4602)

# Objective

- noticed a few Vec3 and Vec2 that could be const

## Solution

- Declared them as const
- It seems to make a tiny improvement in example `many_light`, but given that the change is not complex at all it could still be worth it
This commit is contained in:
François 2022-05-06 22:05:45 +00:00
parent aabc47f290
commit 743bd30bc7

View file

@ -2,7 +2,9 @@ use std::collections::HashSet;
use bevy_asset::Assets; use bevy_asset::Assets;
use bevy_ecs::prelude::*; use bevy_ecs::prelude::*;
use bevy_math::{Mat4, UVec2, UVec3, Vec2, Vec3, Vec3A, Vec3Swizzles, Vec4, Vec4Swizzles}; use bevy_math::{
const_vec2, Mat4, UVec2, UVec3, Vec2, Vec3, Vec3A, Vec3Swizzles, Vec4, Vec4Swizzles,
};
use bevy_reflect::prelude::*; use bevy_reflect::prelude::*;
use bevy_render::{ use bevy_render::{
camera::{Camera, CameraProjection, OrthographicProjection}, camera::{Camera, CameraProjection, OrthographicProjection},
@ -487,8 +489,7 @@ fn ndc_position_to_cluster(
view_z: f32, view_z: f32,
) -> UVec3 { ) -> UVec3 {
let cluster_dimensions_f32 = cluster_dimensions.as_vec3(); let cluster_dimensions_f32 = cluster_dimensions.as_vec3();
let frag_coord = let frag_coord = (ndc_p.xy() * VEC2_HALF_NEGATIVE_Y + VEC2_HALF).clamp(Vec2::ZERO, Vec2::ONE);
(ndc_p.xy() * Vec2::new(0.5, -0.5) + Vec2::splat(0.5)).clamp(Vec2::ZERO, Vec2::ONE);
let xy = (frag_coord * cluster_dimensions_f32.xy()).floor(); let xy = (frag_coord * cluster_dimensions_f32.xy()).floor();
let z_slice = view_z_to_z_slice( let z_slice = view_z_to_z_slice(
cluster_factors, cluster_factors,
@ -501,6 +502,9 @@ fn ndc_position_to_cluster(
.clamp(UVec3::ZERO, cluster_dimensions - UVec3::ONE) .clamp(UVec3::ZERO, cluster_dimensions - UVec3::ONE)
} }
const VEC2_HALF: Vec2 = const_vec2!([0.5, 0.5]);
const VEC2_HALF_NEGATIVE_Y: Vec2 = const_vec2!([0.5, -0.5]);
// Calculate bounds for the light using a view space aabb. // Calculate bounds for the light using a view space aabb.
// Returns a (Vec3, Vec3) containing min and max with // Returns a (Vec3, Vec3) containing min and max with
// x and y in normalized device coordinates with range [-1, 1] // x and y in normalized device coordinates with range [-1, 1]
@ -575,24 +579,22 @@ fn cluster_space_light_aabb(
.max(light_aabb_ndc_xymax_far), .max(light_aabb_ndc_xymax_far),
); );
// pack unadjusted z depth into the vecs // clamp to ndc coords without depth
let (aabb_min, aabb_max) = ( let (aabb_min_ndc, aabb_max_ndc) = (
light_aabb_ndc_min.xy().extend(light_aabb_view_min.z), light_aabb_ndc_min.xy().clamp(NDC_MIN, NDC_MAX),
light_aabb_ndc_max.xy().extend(light_aabb_view_max.z), light_aabb_ndc_max.xy().clamp(NDC_MIN, NDC_MAX),
); );
// clamp to ndc coords
// pack unadjusted z depth into the vecs
( (
aabb_min.clamp( aabb_min_ndc.extend(light_aabb_view_min.z),
Vec3::new(-1.0, -1.0, f32::MIN), aabb_max_ndc.extend(light_aabb_view_max.z),
Vec3::new(1.0, 1.0, f32::MAX),
),
aabb_max.clamp(
Vec3::new(-1.0, -1.0, f32::MIN),
Vec3::new(1.0, 1.0, f32::MAX),
),
) )
} }
const NDC_MIN: Vec2 = const_vec2!([-1.0, -1.0]);
const NDC_MAX: Vec2 = const_vec2!([1.0, 1.0]);
// Sort point lights with shadows enabled first, then by a stable key so that the index // Sort point lights with shadows enabled first, then by a stable key so that the index
// can be used to limit the number of point light shadows to render based on the device and // can be used to limit the number of point light shadows to render based on the device and
// we keep a stable set of lights visible // we keep a stable set of lights visible