Blender V5.3
draw_view_finalize.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#pragma once
12
13#include "draw_defines.hh"
14#include "draw_shader_shared.hh"
15#include "draw_view.bsl.hh"
17
18namespace draw {
19
21 float &r_left,
22 float &r_right,
23 float &r_bottom,
24 float &r_top,
25 float &r_near,
26 float &r_far)
27{
28 const bool is_persp = winmat[3][3] == 0.0f;
29 if (is_persp) {
30 float near = winmat[3][2] / (winmat[2][2] - 1.0f);
31 r_left = near * ((winmat[2][0] - 1.0f) / winmat[0][0]);
32 r_right = near * ((winmat[2][0] + 1.0f) / winmat[0][0]);
33 r_bottom = near * ((winmat[2][1] - 1.0f) / winmat[1][1]);
34 r_top = near * ((winmat[2][1] + 1.0f) / winmat[1][1]);
35 r_near = near;
36 r_far = winmat[3][2] / (winmat[2][2] + 1.0f);
37 }
38 else {
39 r_left = (-winmat[3][0] - 1.0f) / winmat[0][0];
40 r_right = (-winmat[3][0] + 1.0f) / winmat[0][0];
41 r_bottom = (-winmat[3][1] - 1.0f) / winmat[1][1];
42 r_top = (-winmat[3][1] + 1.0f) / winmat[1][1];
43 r_near = (winmat[3][2] + 1.0f) / winmat[2][2];
44 r_far = (winmat[3][2] - 1.0f) / winmat[2][2];
45 }
46}
47
49{
50 float left = 0.0f, right = 0.0f, bottom = 0.0f, top = 0.0f, near = 0.0f, far = 0.0f;
51 bool is_persp = winmat[3][3] == 0.0f;
52
53 projmat_dimensions(winmat, left, right, bottom, top, near, far);
54
55 frustum.corners[0][2] = frustum.corners[3][2] = frustum.corners[7][2] = frustum.corners[4][2] =
56 -near;
57 frustum.corners[0][0] = frustum.corners[3][0] = left;
58 frustum.corners[4][0] = frustum.corners[7][0] = right;
59 frustum.corners[0][1] = frustum.corners[4][1] = bottom;
60 frustum.corners[7][1] = frustum.corners[3][1] = top;
61
62 /* Get the coordinates of the far plane. */
63 if (is_persp) {
64 float sca_far = far / near;
65 left *= sca_far;
66 right *= sca_far;
67 bottom *= sca_far;
68 top *= sca_far;
69 }
70
71 frustum.corners[1][2] = frustum.corners[2][2] = frustum.corners[6][2] = frustum.corners[5][2] =
72 -far;
73 frustum.corners[1][0] = frustum.corners[2][0] = left;
74 frustum.corners[6][0] = frustum.corners[5][0] = right;
75 frustum.corners[1][1] = frustum.corners[5][1] = bottom;
76 frustum.corners[2][1] = frustum.corners[6][1] = top;
77
78 /* Transform into world space. */
79 for (int i = 0; i < 8; i++) [[unroll]] {
80 frustum.corners[i].xyz = transform_point(viewinv, frustum.corners[i].xyz);
81 }
82}
83
84void planes_from_projmat(float4x4 mat, FrustumPlanes &frustum_planes)
85{
86 /* References:
87 *
88 * https://fgiesen.wordpress.com/2012/08/31/frustum-planes-from-the-projection-matrix/
89 * http://www8.cs.umu.se/kurser/5DV051/HT12/lab/plane_extraction.pdf
90 */
91 mat = transpose(mat);
92 frustum_planes.planes[0] = mat[3] + mat[0];
93 frustum_planes.planes[1] = mat[3] - mat[0];
94 frustum_planes.planes[2] = mat[3] + mat[1];
95 frustum_planes.planes[3] = mat[3] - mat[1];
96 frustum_planes.planes[4] = mat[3] + mat[2];
97 frustum_planes.planes[5] = mat[3] - mat[2];
98}
99
100void frustum_culling_planes_calc(float4x4 winmat, float4x4 viewmat, FrustumPlanes &frustum_planes)
101{
102 float4x4 persmat = winmat * viewmat;
103 planes_from_projmat(persmat, frustum_planes);
104
105 /* Normalize. */
106 for (int p = 0; p < 6; p++) {
107 frustum_planes.planes[p] /= length(frustum_planes.planes[p].xyz);
108 }
109}
110
112{
113 /* Extract Bounding Sphere */
114 /* TODO(fclem): This is significantly less precise than CPU, but it isn't used in most cases. */
115
116 float4 bsphere;
117 bsphere.xyz = (frustum_corners.corners[0].xyz + frustum_corners.corners[6].xyz) * 0.5f;
118 bsphere.w = 0.0f;
119 for (int i = 0; i < 8; i++) {
120 bsphere.w = max(bsphere.w, distance(bsphere.xyz, frustum_corners.corners[i].xyz));
121 }
122 return bsphere;
123}
124
126 [[storage(0, read_write)]] ViewCullingData (&view_culling_buf)[DRW_VIEW_MAX];
127};
128
129[[compute, local_size(DRW_VIEW_MAX)]] void view_finalize_main(
131 [[resource_table]] View &views,
132 [[local_invocation_id]] const uint3 local_id)
133{
134 /* Note: Only one thread group is dispatched. */
135 uint view_id = local_id.x;
136
137 ViewMatrices view = views.get(view_id);
138
139 /* Invalid views are disabled. */
140 if (all(equal(view.viewinv[2].xyz, float3(0.0f)))) {
141 /* Views with negative radius are treated as disabled. */
142 srt.view_culling_buf[view_id].bound_sphere = float4(-1.0f);
143 return;
144 }
145
146 /* Read frustum_corners from device memory, update, and write back. */
147 FrustumCorners frustum_corners = srt.view_culling_buf[view_id].frustum_corners;
148 frustum_boundbox_calc(view.winmat, view.viewinv, frustum_corners);
149 srt.view_culling_buf[view_id].frustum_corners = frustum_corners;
150
151 /* Read frustum_planes from device memory, update, and write back. */
152 FrustumPlanes frustum_planes = srt.view_culling_buf[view_id].frustum_planes;
153 frustum_culling_planes_calc(view.winmat, view.viewmat, frustum_planes);
154
155 srt.view_culling_buf[view_id].frustum_planes = frustum_planes;
156 srt.view_culling_buf[view_id].bound_sphere = frustum_culling_sphere_calc(frustum_corners);
157}
158
160
161} // namespace draw
btScalar max
#define storage(slot, qualifiers)
#define resource_table
#define local_invocation_id
#define unroll
#define compute
#define local_size(...)
float distance(float, float) RET
bool all(VecOp< bool, D >) RET
MatBase< R, C > transpose(MatBase< C, R >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
float length(VecOp< float, D >) RET
float3 transform_point(float3x3 mat, float3 point)
MatBase< float, 4, 4 > float4x4
void frustum_culling_planes_calc(float4x4 winmat, float4x4 viewmat, FrustumPlanes &frustum_planes)
void planes_from_projmat(float4x4 mat, FrustumPlanes &frustum_planes)
void view_finalize_main(ViewFinalize &srt, View &views, const uint3 local_id)
PipelineCompute view_finalize(view_finalize_main)
void projmat_dimensions(float4x4 winmat, float &r_left, float &r_right, float &r_bottom, float &r_top, float &r_near, float &r_far)
void frustum_boundbox_calc(float4x4 winmat, float4x4 viewinv, FrustumCorners &frustum)
float4 frustum_culling_sphere_calc(FrustumCorners frustum_corners)
ViewCullingData(& view_culling_buf)[DRW_VIEW_MAX]
float w
Definition sky_math.h:225