Fix: Draw: Bounds usage

The first commit ensures IsectBoxes are not set up unless
they are valid.

The second commit renames
`drw_bounds_are_valid` to `drw_bounds_corners_are_valid`,
and `drw_bounds_culling_enabled` to `drw_bounds_are_valid`
so it's harder to set up an invalid `IsectBox` by mistake.

(Continuation of #127807)

Pull Request: https://projects.blender.org/blender/blender/pulls/128125
This commit is contained in:
Miguel Pozo
2024-09-25 19:58:38 +02:00
parent df251784cd
commit 2223d995fc
5 changed files with 20 additions and 15 deletions

View File

@@ -22,7 +22,7 @@ void main()
}
ObjectBounds bounds = bounds_buf[index];
if (!drw_bounds_are_valid(bounds) || bounds._inner_sphere_radius <= 0.0) {
if (!drw_bounds_are_valid(bounds)) {
return;
}

View File

@@ -20,7 +20,7 @@ shared int global_max;
void main()
{
IsectBox box;
Box box;
bool is_valid = true;
if (resource_len > 0) {
@@ -31,16 +31,18 @@ void main()
resource_id = (resource_id & 0x7FFFFFFFu);
ObjectBounds bounds = bounds_buf[resource_id];
is_valid = drw_bounds_are_valid(bounds);
box = isect_box_setup(bounds.bounding_corners[0].xyz,
bounds.bounding_corners[1].xyz,
bounds.bounding_corners[2].xyz,
bounds.bounding_corners[3].xyz);
is_valid = drw_bounds_corners_are_valid(bounds);
box = shape_box(bounds.bounding_corners[0].xyz,
bounds.bounding_corners[0].xyz + bounds.bounding_corners[1].xyz,
bounds.bounding_corners[0].xyz + bounds.bounding_corners[2].xyz,
bounds.bounding_corners[0].xyz + bounds.bounding_corners[3].xyz);
}
else {
/* Create a dummy box so initialization happens even when there are no shadow casters. */
box = isect_box_setup(
vec3(-1.0), vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
box = shape_box(vec3(-1.0),
vec3(-1.0) + vec3(1.0, 0.0, 0.0),
vec3(-1.0) + vec3(0.0, 1.0, 0.0),
vec3(-1.0) + vec3(0.0, 0.0, 1.0));
}
LIGHT_FOREACH_BEGIN_DIRECTIONAL (light_cull_buf, l_idx) {

View File

@@ -205,15 +205,18 @@ struct ObjectBounds {
BLI_STATIC_ASSERT_ALIGN(ObjectBounds, 16)
/* Return true if `bounding_corners` are valid. Should be checked before accessing them.
* Does not guarantee that `bounding_sphere` is valid. */
inline bool drw_bounds_are_valid(ObjectBounds bounds)
* Does not guarantee that `bounding_sphere` is valid.
* Converting these bounds to an `IsectBox` may generate invalid clip planes.
* For safe `IsectBox` generation check `drw_bounds_are_valid`. */
inline bool drw_bounds_corners_are_valid(ObjectBounds bounds)
{
return bounds.bounding_sphere.w != -1.0f;
}
/* Return true if bounds are ready for culling.
* In this case, both `bounding_corners` and `bounding_sphere` are valid. */
inline bool drw_bounds_culling_enabled(ObjectBounds bounds)
* In this case, both `bounding_corners` and `bounding_sphere` are valid.
* These bounds can be safely converted to an `IsectBox` with valid clip planes. */
inline bool drw_bounds_are_valid(ObjectBounds bounds)
{
return bounds.bounding_sphere.w >= 0.0f;
}

View File

@@ -20,7 +20,7 @@ void main()
ObjectInfos infos = infos_buf[resource_id];
ObjectBounds bounds = bounds_buf[resource_id];
if (drw_bounds_are_valid(bounds)) {
if (drw_bounds_corners_are_valid(bounds)) {
/* Convert corners to origin + sides in world space. */
vec3 p0 = bounds.bounding_corners[0].xyz;
vec3 p01 = bounds.bounding_corners[1].xyz - p0;

View File

@@ -32,7 +32,7 @@ void main()
ObjectBounds bounds = bounds_buf[gl_GlobalInvocationID.x];
if (drw_bounds_culling_enabled(bounds)) {
if (drw_bounds_are_valid(bounds)) {
IsectBox box = isect_box_setup(bounds.bounding_corners[0].xyz,
bounds.bounding_corners[1].xyz,
bounds.bounding_corners[2].xyz,