From 743bd30bc7afd7e88b252de0f839a29cbd91683f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Fri, 6 May 2022 22:05:45 +0000 Subject: [PATCH] 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 --- crates/bevy_pbr/src/light.rs | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 5140eae18a..ca72b12ba7 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -2,7 +2,9 @@ use std::collections::HashSet; use bevy_asset::Assets; 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_render::{ camera::{Camera, CameraProjection, OrthographicProjection}, @@ -487,8 +489,7 @@ fn ndc_position_to_cluster( view_z: f32, ) -> UVec3 { let cluster_dimensions_f32 = cluster_dimensions.as_vec3(); - let frag_coord = - (ndc_p.xy() * Vec2::new(0.5, -0.5) + Vec2::splat(0.5)).clamp(Vec2::ZERO, Vec2::ONE); + let frag_coord = (ndc_p.xy() * VEC2_HALF_NEGATIVE_Y + VEC2_HALF).clamp(Vec2::ZERO, Vec2::ONE); let xy = (frag_coord * cluster_dimensions_f32.xy()).floor(); let z_slice = view_z_to_z_slice( cluster_factors, @@ -501,6 +502,9 @@ fn ndc_position_to_cluster( .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. // Returns a (Vec3, Vec3) containing min and max with // 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), ); - // pack unadjusted z depth into the vecs - let (aabb_min, aabb_max) = ( - light_aabb_ndc_min.xy().extend(light_aabb_view_min.z), - light_aabb_ndc_max.xy().extend(light_aabb_view_max.z), + // clamp to ndc coords without depth + let (aabb_min_ndc, aabb_max_ndc) = ( + light_aabb_ndc_min.xy().clamp(NDC_MIN, NDC_MAX), + light_aabb_ndc_max.xy().clamp(NDC_MIN, NDC_MAX), ); - // clamp to ndc coords + + // pack unadjusted z depth into the vecs ( - aabb_min.clamp( - Vec3::new(-1.0, -1.0, f32::MIN), - 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), - ), + aabb_min_ndc.extend(light_aabb_view_min.z), + aabb_max_ndc.extend(light_aabb_view_max.z), ) } +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 // 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