Skip to content

Commit

Permalink
use const Vec2 in lights cluster and bounding box when possible (bevy…
Browse files Browse the repository at this point in the history
…engine#4602)

- noticed a few Vec3 and Vec2 that could be const

- 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
  • Loading branch information
mockersf authored and robtfm committed May 10, 2022
1 parent e209890 commit 292310b
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::collections::HashSet;

use bevy_asset::Assets;
use bevy_ecs::prelude::*;
use bevy_math::{Mat4, Quat, 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},
Expand Down Expand Up @@ -489,8 +491,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,
Expand All @@ -503,6 +504,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]
Expand Down Expand Up @@ -577,21 +581,16 @@ 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),
)
}

Expand Down Expand Up @@ -686,6 +685,8 @@ fn compute_aabb_for_cluster(

Aabb::from_min_max(cluster_min, cluster_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
// can be used to limit the number of point light shadows to render based on the device and
Expand Down

0 comments on commit 292310b

Please sign in to comment.