Blender V5.3
subdiv_patch_evaluation.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "gpu_shader_compat.hh"
12
14
15/* `osd_patch_defines.glsl` must be included before `osd_patch_basis.glsl` */
16#include "osd_patch_defines.glsl" /* IWYU pragma: export */
17
18#include "osd_patch_basis.glsl"
19
21#include "subdiv_common.bsl.hh"
22
23namespace subdiv {
24
25/* -------------------------------------------------------------------- */
31
33{
34 PatchHandle ret;
35 ret.array_index = -1;
36 ret.vertex_index = -1;
37 ret.patch_index = -1;
38 return ret;
39}
40
41int transformUVToQuadQuadrant(float median, float &u, float &v)
42{
43 int uHalf = (u >= median) ? 1 : 0;
44 if (uHalf != 0) {
45 u -= median;
46 }
47 int vHalf = (v >= median) ? 1 : 0;
48 if (vHalf != 0) {
49 v -= median;
50 }
51 return (vHalf << 1) | uHalf;
52}
53
54int transformUVToTriQuadrant(float median, float &u, float &v, bool &rotated)
55{
56
57 if (!rotated) {
58 if (u >= median) {
59 u -= median;
60 return 1;
61 }
62 if (v >= median) {
63 v -= median;
64 return 2;
65 }
66 if ((u + v) >= median) {
67 rotated = true;
68 return 3;
69 }
70 return 0;
71 }
72 else {
73 if (u < median) {
74 v -= median;
75 return 1;
76 }
77 if (v < median) {
78 u -= median;
79 return 2;
80 }
81 u -= median;
82 v -= median;
83 if ((u + v) < median) {
84 rotated = false;
85 return 3;
86 }
87 return 0;
88 }
89}
90
91OsdPatchCoord bogus_patch_coord(int face_index, float u, float v)
92{
93 OsdPatchCoord coord;
94 coord.arrayIndex = 0;
95 coord.patchIndex = face_index;
96 coord.vertIndex = 0;
97 coord.s = u;
98 coord.t = v;
99 return coord;
100}
101
103
104/* -------------------------------------------------------------------- */
107
110 /* WORKAROUND: Needed define for OSD. */
112};
113
114struct PatchEval {
117
120
123
125
129 read)]] const OsdPatchArray (&patchArrayBuffer)[];
132 read)]] const OsdPatchParam (&patchParamBuffer)[];
133
135 read)]] [[condition(do_orcos)]] const float (&srcExtraVertexBuffer)[];
136
138 {
139 float2 result;
140 result.x = srcVertexBuffer[index * 2];
141 result.y = srcVertexBuffer[index * 2 + 1];
142 return result;
143 }
144
146 {
147 float3 result;
148 result.x = srcVertexBuffer[index * 3];
149 result.y = srcVertexBuffer[index * 3 + 1];
150 result.z = srcVertexBuffer[index * 3 + 2];
151 return result;
152 }
153
155 {
156 float3 result = float3(0);
157 if (consts.do_orcos) [[static_branch]] {
158 result.x = srcExtraVertexBuffer[index * 3];
159 result.y = srcExtraVertexBuffer[index * 3 + 1];
160 result.z = srcExtraVertexBuffer[index * 3 + 2];
161 }
162 return result;
163 }
164
165 OsdPatchArray GetPatchArray(int arrayIndex) const
166 {
167 return patchArrayBuffer[arrayIndex];
168 }
169
170 OsdPatchParam GetPatchParam(int patchIndex) const
171 {
172 return patchParamBuffer[patchIndex];
173 }
174
175 PatchHandle find_patch(int face_index, float u, float v) const
176 {
177 if (face_index < srt.shader_data.min_patch_face || face_index > srt.shader_data.max_patch_face)
178 {
179 return bogus_patch_handle();
180 }
181
183
184 if (!is_set(node.child[0])) {
185 return bogus_patch_handle();
186 }
187
188 float median = 0.5f;
189 bool tri_rotated = false;
190
191 for (int depth = 0; depth <= srt.shader_data.max_depth; ++depth, median *= 0.5f) {
192 int quadrant = srt.shader_data.patches_are_triangular ?
193 transformUVToTriQuadrant(median, u, v, tri_rotated) :
194 transformUVToQuadQuadrant(median, u, v);
195
196 if (is_leaf(node.child[quadrant])) {
197 return input_patch_handles[get_index(node.child[quadrant])];
198 }
199
200 node = quad_nodes[get_index(node.child[quadrant])];
201 }
202 return bogus_patch_handle();
203 }
204
205 OsdPatchCoord GetPatchCoord(int face_index, float u, float v) const
206 {
207 PatchHandle patch_handle = find_patch(face_index, u, v);
208
209 if (patch_handle.array_index == -1) {
210 return bogus_patch_coord(face_index, u, v);
211 }
212
213 OsdPatchCoord coord;
214 coord.arrayIndex = patch_handle.array_index;
215 coord.patchIndex = patch_handle.patch_index;
216 coord.vertIndex = patch_handle.vertex_index;
217 coord.s = u;
218 coord.t = v;
219 return coord;
220 }
221
222 /* ------------------------------------------------------------------------------
223 * Patch evaluation. Note that the 1st and 2nd derivatives are always computed, although we
224 * only return and use the 1st derivatives if adaptive patches are used. This could
225 * perhaps be optimized.
226 */
227
228 void evaluate_patches_limits(int patch_index, float u, float v, float2 &dst) const
229 {
230 OsdPatchCoord coord = GetPatchCoord(patch_index, u, v);
231 OsdPatchArray array = GetPatchArray(coord.arrayIndex);
232 OsdPatchParam param = GetPatchParam(coord.patchIndex);
233
234 int patchType = OsdPatchParamIsRegular(param) ? array.regDesc : array.desc;
235
236 float wP[20], wDu[20], wDv[20], wDuu[20], wDuv[20], wDvv[20];
237 int nPoints = OsdEvaluatePatchBasis(
238 patchType, param, coord.s, coord.t, wP, wDu, wDv, wDuu, wDuv, wDvv);
239
240 int indexBase = array.indexBase + array.stride * (coord.patchIndex - array.primitiveIdBase);
241
242 for (int cv = 0; cv < nPoints; ++cv) {
243 int index = patchIndexBuffer[indexBase + cv];
245 dst += src_fvar * wP[cv];
246 }
247 }
248
250 int patch_index, float u, float v, float3 &dst, float3 &du, float3 &dv) const
251 {
252 OsdPatchCoord coord = GetPatchCoord(patch_index, u, v);
253 OsdPatchArray array = GetPatchArray(coord.arrayIndex);
254 OsdPatchParam param = GetPatchParam(coord.patchIndex);
255
256 int patchType = OsdPatchParamIsRegular(param) ? array.regDesc : array.desc;
257
258 float wP[20], wDu[20], wDv[20], wDuu[20], wDuv[20], wDvv[20];
259 int nPoints = OsdEvaluatePatchBasis(
260 patchType, param, coord.s, coord.t, wP, wDu, wDv, wDuu, wDuv, wDvv);
261
262 int indexBase = array.indexBase + array.stride * (coord.patchIndex - array.primitiveIdBase);
263
264 for (int cv = 0; cv < nPoints; ++cv) {
265 int index = patchIndexBuffer[indexBase + cv];
266 float3 src_vertex = read_vec3(index);
267
268 dst += src_vertex * wP[cv];
269 du += src_vertex * wDu[cv];
270 dv += src_vertex * wDv[cv];
271 }
272 }
273
274 /* Evaluate the patches limits from the extra source vertex buffer. */
275 void evaluate_patches_limits_extra(int patch_index, float u, float v, float3 &dst) const
276 {
277 if (consts.do_orcos) [[static_branch]] {
278 OsdPatchCoord coord = GetPatchCoord(patch_index, u, v);
279 OsdPatchArray array = GetPatchArray(coord.arrayIndex);
280 OsdPatchParam param = GetPatchParam(coord.patchIndex);
281
282 int patchType = OsdPatchParamIsRegular(param) ? array.regDesc : array.desc;
283
284 float wP[20], wDu[20], wDv[20], wDuu[20], wDuv[20], wDvv[20];
285 int nPoints = OsdEvaluatePatchBasis(
286 patchType, param, coord.s, coord.t, wP, wDu, wDv, wDuu, wDuv, wDvv);
287
288 int indexBase = array.indexBase + array.stride * (coord.patchIndex - array.primitiveIdBase);
289
290 for (int cv = 0; cv < nPoints; ++cv) {
291 int index = patchIndexBuffer[indexBase + cv];
292 float3 src_vertex = read_vec3_extra(index);
293
294 dst += src_vertex * wP[cv];
295 }
296 }
297 }
298};
299
301
302/* -------------------------------------------------------------------- */
305
309
312 [[resource_table]] const PatchEval &eval,
313 [[resource_table]] FvarEval &res,
314 [[global_invocation_id]] const uint3 global_id,
316{
317 /* We execute for each quad. */
318 uint quad_index = srt.get_global_invocation_index(global_id, num_work_groups);
319 if (quad_index >= srt.shader_data.total_dispatch_size) {
320 return;
321 }
322
323 uint start_loop_index = quad_index * 4;
324
325 for (uint loop_index = start_loop_index; loop_index < start_loop_index + 4; loop_index++) {
326 float2 fvar = float2(0.0f);
327
328 BlenderPatchCoord patch_co = eval.patch_coords[loop_index];
329 float2 uv = decode_uv(patch_co.encoded_uv);
330
331 eval.evaluate_patches_limits(patch_co.patch_index, uv.x, uv.y, fvar);
332 res.fvar_buf[srt.shader_data.dst_offset + loop_index] = fvar;
333 }
334}
335
337
338/* -------------------------------------------------------------------- */
341
345
346struct FdotsEval {
349
351 read)]] const uint (&extra_coarse_face_data)[];
352
355
357 write)]] [[condition(do_normals)]] FDotNor (&nors_buf)[];
358
359 bool is_face_selected(uint coarse_quad_index)
360 {
361 return (extra_coarse_face_data[coarse_quad_index] & srt.shader_data.coarse_face_select_mask) !=
362 0;
363 }
364
365 bool is_face_active(uint coarse_quad_index)
366 {
367 return (extra_coarse_face_data[coarse_quad_index] & srt.shader_data.coarse_face_active_mask) !=
368 0;
369 }
370
371 bool is_face_hidden(uint coarse_quad_index)
372 {
373 return (extra_coarse_face_data[coarse_quad_index] & srt.shader_data.coarse_face_hidden_mask) !=
374 0;
375 }
376
377 float get_face_flag(uint coarse_quad_index)
378 {
379 if (is_face_active(coarse_quad_index)) {
380 return -1.0f;
381 }
382
383 if (is_face_selected(coarse_quad_index)) {
384 return 1.0f;
385 }
386
387 return 0.0f;
388 }
389};
390
393 [[resource_table]] const PatchEval &eval,
394 [[resource_table]] FdotsEval &res,
395 [[global_invocation_id]] const uint3 global_id,
397{
398 /* We execute for each coarse quad. */
399 uint coarse_quad_index = srt.get_global_invocation_index(global_id, num_work_groups);
400 if (coarse_quad_index >= srt.shader_data.total_dispatch_size) {
401 return;
402 }
403
404 BlenderPatchCoord patch_co = eval.patch_coords[coarse_quad_index];
405 float2 uv = decode_uv(patch_co.encoded_uv);
406
407 float3 pos = float3(0.0f);
408 float3 du = float3(0.0f);
409 float3 dv = float3(0.0f);
410 eval.evaluate_patches_limits(patch_co.patch_index, uv.x, uv.y, pos, du, dv);
411 float3 nor = normalize(cross(du, dv));
412
413 FDotVert vert;
414 vert.x = pos.x;
415 vert.y = pos.y;
416 vert.z = pos.z;
417
418 FDotNor fnor;
419 fnor.x = nor.x;
420 fnor.y = nor.y;
421 fnor.z = nor.z;
422 fnor.flag = res.get_face_flag(coarse_quad_index);
423
424 res.verts_buf[coarse_quad_index] = vert;
425 if (res.consts.do_normals) [[static_branch]] {
426 res.nors_buf[coarse_quad_index] = fnor;
427 }
428
429 if (srt.shader_data.use_hide && res.is_face_hidden(coarse_quad_index)) {
430 res.indices_buf[coarse_quad_index] = 0xffffffff;
431 }
432 else {
433 res.indices_buf[coarse_quad_index] = coarse_quad_index;
434 }
435}
436
438
439/* -------------------------------------------------------------------- */
442
450
453 [[resource_table]] const PatchEval &eval,
454 [[resource_table]] VertsEval &res,
455 [[global_invocation_id]] const uint3 global_id,
457{
458 /* We execute for each quad. */
459 uint quad_index = srt.get_global_invocation_index(global_id, num_work_groups);
460 if (quad_index >= srt.shader_data.total_dispatch_size) {
461 return;
462 }
463
464 uint start_loop_index = quad_index * 4;
465
466 for (uint loop_index = start_loop_index; loop_index < start_loop_index + 4; loop_index++) {
467 float3 pos = float3(0.0f);
468 float3 du = float3(0.0f);
469 float3 dv = float3(0.0f);
470
471 BlenderPatchCoord patch_co = eval.patch_coords[loop_index];
472 float2 uv = decode_uv(patch_co.encoded_uv);
473
474 eval.evaluate_patches_limits(patch_co.patch_index, uv.x, uv.y, pos, du, dv);
475
477 position.x = pos.x;
478 position.y = pos.y;
479 position.z = pos.z;
480 res.pos_buf[loop_index] = position;
481
482 if (eval.consts.do_orcos) [[static_branch]] {
483 pos = float3(0.0f);
484 eval.evaluate_patches_limits_extra(patch_co.patch_index, uv.x, uv.y, pos);
485
486 /* Set w = 0.0f to indicate that this is not a generic attribute.
487 * See comments in `extract_mesh_vbo_orco.cc`. */
488 float4 orco_data = float4(pos, 0.0f);
489 res.orco_buf[loop_index] = orco_data;
490 }
491 }
492}
493
495
498 .do_orcos = false,
499 .use_1st_derivatives = true,
500 });
503 .do_orcos = false,
504 .use_1st_derivatives = true,
505 },
506 FdotsConsts{
507 .do_normals = false,
508 });
511 .do_orcos = false,
512 .use_1st_derivatives = true,
513 },
514 FdotsConsts{
515 .do_normals = true,
516 });
519 .do_orcos = false,
520 .use_1st_derivatives = true,
521 });
524 .do_orcos = true,
525 .use_1st_derivatives = true,
526 });
527
528} // namespace subdiv
#define PATCH_EVALUATION_OUTPUT_NORMALS_BUF_SLOT
#define PATCH_EVALUATION_OUTPUT_POS_BUF_SLOT
#define PATCH_EVALUATION_PATCH_ARRAY_BUFFER_BUF_SLOT
#define PATCH_EVALUATION_QUAD_NODES_BUF_SLOT
#define PATCH_EVALUATION_INPUT_PATCH_HANDLES_BUF_SLOT
#define PATCH_EVALUATION_SOURCE_EXTRA_VERTEX_BUFFER_BUF_SLOT
#define PATCH_EVALUATION_PATCH_PARAM_BUFFER_BUF_SLOT
#define PATCH_EVALUATION_OUTPUT_FDOTS_VERTEX_BUFFER_BUF_SLOT
#define SUBDIV_GROUP_SIZE
#define PATCH_EVALUATION_PATCH_COORDS_BUF_SLOT
#define PATCH_EVALUATION_PATCH_INDEX_BUFFER_BUF_SLOT
#define PATCH_EVALUATION_OUTPUT_INDICES_BUF_SLOT
#define PATCH_EVALUATION_OUTPUT_ORCOS_BUF_SLOT
#define PATCH_EVALUATION_SOURCE_VERTEX_BUFFER_BUF_SLOT
#define PATCH_EVALUATION_OUTPUT_FVAR_BUF_SLOT
#define PATCH_EVALUATION_EXTRA_COARSE_FACE_DATA_BUF_SLOT
#define storage(slot, qualifiers)
#define position
#define index(i)
#define compilation_constant
#define resource_table
#define static_branch
#define global_invocation_id
#define compute
#define num_work_groups
#define condition(cond)
#define local_size(...)
VecBase< float, D > normalize(VecOp< float, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
float2 packed_float2
PipelineCompute patch_evaluation_verts_orcos(patch_eval_verts, PatchEvalConsts{ .do_orcos=true,.use_1st_derivatives=true, })
void patch_eval_verts(const SubdivResources &srt, const PatchEval &eval, VertsEval &res, const uint3 global_id, const uint3 num_work_groups)
OsdPatchCoord bogus_patch_coord(int face_index, float u, float v)
PipelineCompute patch_evaluation_fvar(patch_eval_fvar, PatchEvalConsts{ .do_orcos=false,.use_1st_derivatives=true, })
void patch_eval_fdots(const SubdivResources &srt, const PatchEval &eval, FdotsEval &res, const uint3 global_id, const uint3 num_work_groups)
void write(AttribInterp &res, uint dst_offset, uint index, const T value)
PatchHandle bogus_patch_handle()
T read(const AttribInterp &res, uint index)
int transformUVToTriQuadrant(float median, float &u, float &v, bool &rotated)
PipelineCompute patch_evaluation_verts(patch_eval_verts, PatchEvalConsts{ .do_orcos=false,.use_1st_derivatives=true, })
PipelineCompute patch_evaluation_fdots_normals(patch_eval_fdots, PatchEvalConsts{ .do_orcos=false,.use_1st_derivatives=true, }, FdotsConsts{ .do_normals=true, })
PipelineCompute patch_evaluation_fdots(patch_eval_fdots, PatchEvalConsts{ .do_orcos=false,.use_1st_derivatives=true, }, FdotsConsts{ .do_normals=false, })
int transformUVToQuadQuadrant(float median, float &u, float &v)
void patch_eval_fvar(const SubdivResources &srt, const PatchEval &eval, FvarEval &res, const uint3 global_id, const uint3 num_work_groups)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const DRWSubdivUboStorage shader_data
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
bool is_face_selected(uint coarse_quad_index)
bool is_face_hidden(uint coarse_quad_index)
float get_face_flag(uint coarse_quad_index)
bool is_face_active(uint coarse_quad_index)
const PatchHandle(& input_patch_handles)[]
const BlenderPatchCoord(& patch_coords)[]
const OsdPatchParam(& patchParamBuffer)[]
OsdPatchCoord GetPatchCoord(int face_index, float u, float v) const
void evaluate_patches_limits_extra(int patch_index, float u, float v, float3 &dst) const
const OsdPatchArray(& patchArrayBuffer)[]
float3 read_vec3(int index) const
void evaluate_patches_limits(int patch_index, float u, float v, float2 &dst) const
OsdPatchParam GetPatchParam(int patchIndex) const
PatchHandle find_patch(int face_index, float u, float v) const
float2 read_vec2(int index) const
void evaluate_patches_limits(int patch_index, float u, float v, float3 &dst, float3 &du, float3 &dv) const
float3 read_vec3_extra(int index) const
OsdPatchArray GetPatchArray(int arrayIndex) const
float2 decode_uv(uint encoded_uv)
bool is_set(uint i)
uint get_index(uint i)
bool is_leaf(uint i)