Blender V5.3
draw_visibility.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/* TODO(fclem): This could be augmented by a 2 pass occlusion culling system. */
12
13#pragma once
14
15#include "draw_intersect.bsl.hh"
16
17namespace draw {
18
19struct Visibility {
20 [[storage(0, read)]] ObjectBounds (&bounds_buf)[];
21 [[storage(1, read_write)]] uint (&visibility_buf)[];
22
26
27 void mask_visibility_bit(const uint3 global_id,
28 const uint3 local_id,
29 const uint3 workgroup_id,
30 uint view_id)
31 {
32 if (view_len > 1) {
33 uint index = global_id.x * uint(visibility_word_per_draw) + (view_id / 32u);
34 visibility_buf[index] &= ~(1u << (view_id & 31u));
35 }
36 else {
37 atomicAnd(visibility_buf[workgroup_id.x], ~(1u << local_id.x));
38 }
39 }
40};
41
44 [[resource_table]] const ViewCulling &culling,
45 [[global_invocation_id]] const uint3 global_id,
46 [[local_invocation_id]] const uint3 local_id,
47 [[work_group_id]] const uint3 workgroup_id)
48{
49 if (int(global_id.x) >= srt.resource_len) {
50 return;
51 }
52
53 ObjectBounds bounds = srt.bounds_buf[global_id.x];
54
55 if (drw_bounds_are_valid(bounds)) {
56 IsectBox box = isect_box_setup(bounds.bounding_corners[0].xyz,
57 bounds.bounding_corners[1].xyz,
58 bounds.bounding_corners[2].xyz,
59 bounds.bounding_corners[3].xyz);
60 Sphere bounding_sphere = shape_sphere(bounds.bounding_sphere.xyz, bounds.bounding_sphere.w);
61 Sphere inscribed_sphere = shape_sphere(bounds.bounding_sphere.xyz,
62 bounds.inner_sphere_radius());
63
64 for (uint view_id = 0u; view_id < uint(srt.view_len); view_id++) {
65 if (culling.get(view_id).bound_sphere.w == -1.0f) {
66 /* View disabled. */
67 srt.mask_visibility_bit(global_id, local_id, workgroup_id, view_id);
68 }
69 else if (culling.intersect_view(inscribed_sphere, view_id) == true) {
70 /* Visible. */
71 }
72 else if (culling.intersect_view(bounding_sphere, view_id) == false) {
73 /* Not visible. */
74 srt.mask_visibility_bit(global_id, local_id, workgroup_id, view_id);
75 }
76 else if (culling.intersect_view(box, view_id) == false) {
77 /* Not visible. */
78 srt.mask_visibility_bit(global_id, local_id, workgroup_id, view_id);
79 }
80 }
81 }
82 else {
83 /* Culling is disabled, but we need to mask the bits for disabled views. */
84 for (uint view_id = 0u; view_id < uint(srt.view_len); view_id++) {
85 if (culling.get(view_id).bound_sphere.w == -1.0f) {
86 /* View disabled. */
87 srt.mask_visibility_bit(global_id, local_id, workgroup_id, view_id);
88 }
89 }
90 }
91}
92
94
95} // namespace draw
#define DRW_VISIBILITY_GROUP_SIZE
IsectBox isect_box_setup(Box shape)
bool drw_bounds_are_valid(ObjectBounds bounds)
Sphere shape_sphere(float3 center, float radius)
#define storage(slot, qualifiers)
#define work_group_id
#define index(i)
#define push_constant
#define resource_table
#define global_invocation_id
#define local_invocation_id
#define compute
#define local_size(...)
int atomicAnd(int &, int) RET
void visibility(Visibility &srt, const ViewCulling &culling, const uint3 global_id, const uint3 local_id, const uint3 workgroup_id)
PipelineCompute visibility_compute(visibility)
float4 bounding_corners[4]
float inner_sphere_radius() const
ObjectBounds(& bounds_buf)[]
void mask_visibility_bit(const uint3 global_id, const uint3 local_id, const uint3 workgroup_id, uint view_id)
uint x
Definition types_uint3.h:13