Blender V5.3
draw_resource_finalize.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#pragma once
12
13#include "draw_shader_shared.hh"
18
19namespace draw {
20
23 [[storage(1, read_write)]] ObjectBounds (&bounds_buf)[];
24 [[storage(2, read_write)]] ObjectInfos (&infos_buf)[];
25
27};
28
30 [[resource_table]] ResourceFinalize &srt, [[global_invocation_id]] const uint3 global_id)
31{
32 uint resource_id = global_id.x;
33 if (resource_id >= uint(srt.resource_len)) {
34 return;
35 }
36
37 float4x4 model_mat = srt.matrix_buf[resource_id].model;
38 ObjectInfos infos = srt.infos_buf[resource_id];
39 ObjectBounds bounds = srt.bounds_buf[resource_id];
40
41 if (drw_bounds_corners_are_valid(bounds)) {
42 /* Convert corners to origin + sides in world space. */
43 float3 p0 = bounds.bounding_corners[0].xyz;
44 float3 p01 = bounds.bounding_corners[1].xyz - p0;
45 float3 p02 = bounds.bounding_corners[2].xyz - p0;
46 float3 p03 = bounds.bounding_corners[3].xyz - p0;
47 /* Avoid flat box. */
48 p01.x = max(p01.x, 1e-4f);
49 p02.y = max(p02.y, 1e-4f);
50 p03.z = max(p03.z, 1e-4f);
51 float3 diagonal = p01 + p02 + p03;
52 float3 center = p0 + diagonal * 0.5f;
53 float min_axis = reduce_min(abs(diagonal));
54 bounds.bounding_sphere.xyz = transform_point(model_mat, center);
55 /* We have to apply scaling to the diagonal. */
56 bounds.bounding_sphere.w = length(transform_direction(model_mat, diagonal)) * 0.5f;
57 bounds.inner_sphere_radius_set(min_axis);
58 bounds.bounding_corners[0].xyz = transform_point(model_mat, p0);
59 bounds.bounding_corners[1].xyz = transform_direction(model_mat, p01);
60 bounds.bounding_corners[2].xyz = transform_direction(model_mat, p02);
61 bounds.bounding_corners[3].xyz = transform_direction(model_mat, p03);
62 /* Always have correct handedness in the corners vectors. */
64 bounds.bounding_corners[0].xyz += bounds.bounding_corners[1].xyz;
65 bounds.bounding_corners[1].xyz = -bounds.bounding_corners[1].xyz;
66 }
67
68 /* TODO: Bypass test for very large objects (see #67319). */
69 if (bounds.bounding_sphere.w > 1e12f) {
70 bounds.bounding_sphere.w = -2.0f;
71 }
72
73 /* Bypass culling test for objects that are flattened on one or more axes (see #127774).
74 * Fixing them is too much computation but might be worth doing if a use case for it.
75 * Do not compute the real length to save some instructions. */
76 float3 object_scale = float3(reduce_add(abs(model_mat[0].xyz)),
77 reduce_add(abs(model_mat[1].xyz)),
78 reduce_add(abs(model_mat[2].xyz)));
79 if (any(lessThan(abs(object_scale), float3(1e-10f)))) {
80 bounds.bounding_sphere.w = -2.0f;
81 }
82
83 /* Update bounds. */
84 srt.bounds_buf[resource_id] = bounds;
85 }
86
87 float3 loc = infos.orco_add; /* Box center. */
88 float3 size = infos.orco_mul; /* Box half-extent. */
89 float3 orco_mul = safe_rcp(size * 2.0f);
90 float3 orco_add = (loc - size) * -orco_mul;
91 srt.infos_buf[resource_id].orco_add = orco_add;
92 srt.infos_buf[resource_id].orco_mul = orco_mul;
93}
94
96
97} // namespace draw
btScalar max
bool drw_bounds_corners_are_valid(ObjectBounds bounds)
@ OBJECT_NEGATIVE_SCALE
#define storage(slot, qualifiers)
#define push_constant
#define resource_table
#define global_invocation_id
#define compute
#define local_size(...)
bool any(VecOp< bool, D >) RET
#define abs
VecBase< bool, D > lessThan(VecOp< T, D >, VecOp< T, D >) RET
float length(VecOp< float, D >) RET
float3 transform_direction(float3x3 mat, float3 direction)
float3 transform_point(float3x3 mat, float3 point)
float safe_rcp(float a)
float reduce_add(float2 a)
float reduce_min(float2 a)
bool flag_test(uint flag, uint val)
MatBase< float, 4, 4 > float4x4
PipelineCompute resource_finalize(resource_finalize_main)
void resource_finalize_main(ResourceFinalize &srt, const uint3 global_id)
float4 bounding_corners[4]
void inner_sphere_radius_set(float value)
enum eObjectInfoFlag flag
packed_float3 orco_add
packed_float3 orco_mul
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136