Blender V5.3
workbench_shadow_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
8
9#pragma once
10
11#include "draw_intersect.bsl.hh"
13
15
19
20struct Resources {
22
24 [[storage(0, read)]] const ObjectBounds (&bounds_buf)[];
25
27 [[push_constant]] const int view_len;
28
31
33
34 [[storage(1, read_write), condition(!dynamic_pass_selection)]] uint (&visibility_buf)[];
35
36 [[storage(1, read_write), condition(dynamic_pass_selection)]] uint (&pass_visibility_buf)[];
37 [[storage(2, read_write), condition(dynamic_pass_selection)]] uint (&fail_visibility_buf)[];
38
40 {
41 for (int i_plane = 0; i_plane < extruded_frustum.planes_count; i_plane++) {
42 float4 plane = extruded_frustum.planes[i_plane];
43 bool separating_axis = true;
44 for (int i_corner = 0; i_corner < 8; i_corner++) {
45 float signed_distance = dot(box.corners[i_corner], plane.xyz) - plane.w;
46 if (signed_distance <= 0) {
47 separating_axis = false;
48 break;
49 }
50 }
51 if (separating_axis) {
52 return false;
53 }
54 }
55 return true;
56 }
57
59 {
60 float4 near_plane = view_culling.get(0).frustum_planes.planes[4];
61 bool on_positive_side = false;
62 bool on_negative_side = false;
63
64 for (int i_corner = 0; i_corner < 8; i_corner++) {
65 for (int i_displace = 0; i_displace < 2; i_displace++) {
66 float3 corner = box.corners[i_corner] + (shadow_direction * 1e5f * i_displace);
67 float signed_distance = dot(corner, -near_plane.xyz) - near_plane.w;
68 if (signed_distance <= 0) {
69 on_negative_side = true;
70 }
71 else {
72 on_positive_side = true;
73 }
74 if (on_negative_side && on_positive_side) {
75 return true;
76 }
77 }
78 }
79
80 return false;
81 }
82
83 void set_visibility(bool pass, bool fail, uint instance_id)
84 {
86 if (!pass) {
87 atomicAnd(pass_visibility_buf[instance_id / 32u], ~(1u << (instance_id & 31u)));
88 }
89 if (!fail) {
90 atomicAnd(fail_visibility_buf[instance_id / 32u], ~(1u << (instance_id & 31u)));
91 }
92 }
93 }
94
96 {
98 if (!visibility) {
99 atomicAnd(visibility_buf[instance_id / 32u], ~(1u << (instance_id & 31u)));
100 }
101 }
102 }
103};
104
107 [[global_invocation_id]] const uint3 global_id)
108{
109 if (int(global_id.x) >= srt.resource_len) {
110 return;
111 }
112
113 ObjectBounds bounds = srt.bounds_buf[global_id.x];
114 if (!drw_bounds_are_valid(bounds)) {
115 /* Invalid bounding box. */
116 return;
117 }
118 IsectBox box = isect_box_setup(bounds.bounding_corners[0].xyz,
119 bounds.bounding_corners[1].xyz,
120 bounds.bounding_corners[2].xyz,
121 bounds.bounding_corners[3].xyz);
122
123 if (srt.consts.dynamic_pass_selection) [[static_branch]] {
124 if (srt.is_visible(box)) {
125 bool use_fail_pass = srt.force_fail_method || srt.intersects_near_plane(box);
126 srt.set_visibility(!use_fail_pass, use_fail_pass, global_id.x);
127 }
128 else {
129 srt.set_visibility(false, false, global_id.x);
130 }
131 }
132 else {
133 srt.set_visibility(srt.is_visible(box), global_id.x);
134 }
135}
136
139
140} // namespace workbench::shadow::visibility
#define DRW_VISIBILITY_GROUP_SIZE
IsectBox isect_box_setup(Box shape)
bool drw_bounds_are_valid(ObjectBounds bounds)
#define instance_id
#define uniform(slot)
#define storage(slot, qualifiers)
#define push_constant
#define compilation_constant
#define resource_table
#define static_branch
#define global_invocation_id
#define compute
#define condition(cond)
#define local_size(...)
float dot(VecOp< float, D >, VecOp< float, D >) RET
int atomicAnd(int &, int) RET
void culling_main(Resources &srt, const uint3 global_id)
PipelineCompute compute_static_pass_type(culling_main, Constants{.dynamic_pass_selection=false})
PipelineCompute compute_dynamic_pass_type(culling_main, Constants{.dynamic_pass_selection=true})
float3 corners[8]
float4 bounding_corners[4]
struct FrustumPlanes frustum_planes
ViewCullingData get(uint view_id) const
float w
Definition sky_math.h:225
void set_visibility(bool pass, bool fail, uint instance_id)
void set_visibility(bool visibility, uint instance_id)