Blender V5.3
subdiv_vbo_generate.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
14#include "subdiv_common.bsl.hh"
15
16namespace subdiv {
17
19{
20 n[0] += (v_prev[1] - v_curr[1]) * (v_prev[2] + v_curr[2]);
21 n[1] += (v_prev[2] - v_curr[2]) * (v_prev[0] + v_curr[0]);
22 n[2] += (v_prev[0] - v_curr[0]) * (v_prev[1] + v_curr[1]);
23}
24
25/* Adapted from BLI_math_vector_c.hh */
27{
28 /* this is the same as acos(dot_v3v3(v1, v2)), but more accurate */
29 bool q = (dot(v1, v2) >= 0.0f);
30 float3 v = (q) ? (v1 - v2) : (v1 + v2);
31 float a = 2.0f * asin(length(v) / 2.0f);
32 return (q) ? a : M_PI - a;
33}
34
35/* -------------------------------------------------------------------- */
41
50
52 uint face_index, uint vertex_index, uint &curr, uint &next, uint &prev)
53 {
54 uint start_loop_index = face_index * 4;
55
56 for (uint i = 0; i < 4; i++) {
57 uint subdiv_vert_index = vert_loop_map[start_loop_index + i];
58
59 if (subdiv_vert_index == vertex_index) {
60 curr = i;
61 next = (i + 1) % 4;
62 prev = (i + 4 - 1) % 4;
63 break;
64 }
65 }
66 }
67};
68
72 [[global_invocation_id]] const uint3 global_id,
74{
75 uint vertex_index = srt.get_global_invocation_index(global_id, num_work_groups);
76 if (vertex_index >= srt.shader_data.total_dispatch_size) {
77 return;
78 }
79
80 uint first_adjacent_face_offset = accum.face_adjacency_offsets[vertex_index];
81 uint number_of_adjacent_faces = accum.face_adjacency_offsets[vertex_index + 1] -
82 first_adjacent_face_offset;
83
84 float3 accumulated_normal = float3(0.0f);
85
86 /* For each adjacent face. */
87 for (uint i = 0; i < number_of_adjacent_faces; i++) {
88 uint adjacent_face = accum.face_adjacency_lists[first_adjacent_face_offset + i];
89 uint start_loop_index = adjacent_face * 4;
90
91 /* Compute the face normal using Newell's method. */
92 float3 verts[4];
93 for (uint j = 0; j < 4; j++) [[unroll]] {
94 StoredFloat3 data = accum.positions[start_loop_index + j];
95 verts[j] = load_data(data);
96 }
97
98 float3 face_normal = float3(0.0f);
99 add_newell_cross_v3_v3v3(face_normal, verts[0], verts[1]);
100 add_newell_cross_v3_v3v3(face_normal, verts[1], verts[2]);
101 add_newell_cross_v3_v3v3(face_normal, verts[2], verts[3]);
102 add_newell_cross_v3_v3v3(face_normal, verts[3], verts[0]);
103
104 /* Accumulate angle weighted normal. */
105 uint curr_vert = 0;
106 uint next_vert = 0;
107 uint prev_vert = 0;
108 accum.find_prev_and_next_vertex_on_face(
109 adjacent_face, vertex_index, curr_vert, next_vert, prev_vert);
110
111 float3 curr_co = verts[curr_vert];
112 float3 prev_co = verts[next_vert];
113 float3 next_co = verts[prev_vert];
114
115 float3 edvec_prev = normalize(prev_co - curr_co);
116 float3 edvec_next = normalize(curr_co - next_co);
117
118 float fac = acos(-dot(edvec_prev, edvec_next));
119
120 accumulated_normal += face_normal * fac;
121 }
122
123 accum.vert_normals[vertex_index] = as_data(normalize(accumulated_normal));
124}
125
127
128/* -------------------------------------------------------------------- */
131
132/* From extract_mesh_vbo_edge_fac.cc, keep in sync! */
134{
135 float cosine = dot(fa_no, fb_no);
136 /* Re-scale to the slider range. */
137 float fac = (200 * (cosine - 1.0f)) + 1.0f;
138 /* The maximum value (255) is unreachable through the UI. */
139 return clamp(fac, 0.0f, 1.0f) * (254.0f / 255.0f);
140}
141
142struct EdgeFac {
147
148 float compute_line_factor(uint corner_index, float3 face_normal)
149 {
150 if (input_edge_draw_flag[corner_index] == 0) {
151 return 1.0f;
152 }
153
154 int quad_other = input_poly_other_map[corner_index];
155 if (quad_other == -1) {
156 /* Boundary edge or non-manifold. */
157 return 0.0f;
158 }
159
160 const uint start_corner_index_other = uint(quad_other * 4);
161 const StoredFloat3 f0 = positions[start_corner_index_other + 0];
162 const StoredFloat3 f1 = positions[start_corner_index_other + 1];
163 const StoredFloat3 f2 = positions[start_corner_index_other + 2];
164 const float3 v0 = load_data(f0);
165 const float3 v1 = load_data(f1);
166 const float3 v2 = load_data(f2);
167 const float3 face_normal_other = normalize(cross(v1 - v0, v2 - v0));
168
169 return loop_edge_factor_get(face_normal, face_normal_other);
170 }
171};
172
175 [[resource_table]] EdgeFac &edges,
176 [[global_invocation_id]] const uint3 global_id,
178{
179 /* We execute for each quad. */
180 const uint quad_index = srt.get_global_invocation_index(global_id, num_work_groups);
181 if (quad_index >= srt.shader_data.total_dispatch_size) {
182 return;
183 }
184
185 /* The start index of the loop is quad_index * 4. */
186 const uint start_loop_index = quad_index * 4;
187
188 /* First compute the face normal, we need it to compute the bihedral edge angle. */
189 StoredFloat3 f0 = edges.positions[start_loop_index + 0];
190 StoredFloat3 f1 = edges.positions[start_loop_index + 1];
191 StoredFloat3 f2 = edges.positions[start_loop_index + 2];
192 float3 v0 = load_data(f0);
193 float3 v1 = load_data(f1);
194 float3 v2 = load_data(f2);
195 float3 face_normal = normalize(cross(v1 - v0, v2 - v0));
196
197 for (uint i = 0; i < 4; i++) {
198 edges.output_edge_fac[start_loop_index + i] = edges.compute_line_factor(start_loop_index + i,
199 face_normal);
200 }
201}
202
204
205/* -------------------------------------------------------------------- */
208
213
216 [[resource_table]] const PolygonOffsetBase &poly_ofs,
218 [[global_invocation_id]] const uint3 global_id,
220{
221 /* We execute for each quad. */
222 const uint quad_index = srt.get_global_invocation_index(global_id, num_work_groups);
223 if (quad_index >= srt.shader_data.total_dispatch_size) {
224 return;
225 }
226
227 /* The start index of the loop is quad_index * 4. */
228 uint start_loop_index = quad_index * 4;
229
230 uint coarse_quad_index = poly_ofs.coarse_face_index_from_subdiv_quad_index(
231 quad_index, uint(srt.shader_data.coarse_face_count));
232
233 for (uint i = 0; i < 4; i++) [[unroll]] {
234 edit.subdiv_stretch_area[start_loop_index + i] = edit.coarse_stretch_area[coarse_quad_index];
235 }
236}
237
243
247 [[global_invocation_id]] const uint3 global_id,
249{
250 /* We execute for each quad. */
251 const uint quad_index = srt.get_global_invocation_index(global_id, num_work_groups);
252 if (quad_index >= srt.shader_data.total_dispatch_size) {
253 return;
254 }
255
256 uint start_loop_index = quad_index * 4;
257
258 for (uint i = 0; i < 4; i++) {
259 uint cur_loop_index = start_loop_index + i;
260 uint next_loop_index = start_loop_index + (i + 1) % 4;
261 uint prev_loop_index = start_loop_index + (i + 3) % 4;
262
263 /* Compute 2d edge vectors from UVs. */
264 float2 cur_uv = edit.uvs[uint(srt.shader_data.src_offset) + cur_loop_index];
265 float2 next_uv = edit.uvs[uint(srt.shader_data.src_offset) + next_loop_index];
266 float2 prev_uv = edit.uvs[uint(srt.shader_data.src_offset) + prev_loop_index];
267
268 float2 norm_uv_edge0 = normalize(prev_uv - cur_uv);
269 float2 norm_uv_edge1 = normalize(cur_uv - next_uv);
270
271 /* Compute 3d edge vectors from positions. */
272 StoredFloat3 curr_data = edit.positions[cur_loop_index];
273 StoredFloat3 next_data = edit.positions[next_loop_index];
274 StoredFloat3 prev_data = edit.positions[prev_loop_index];
275 float3 curr_pos = load_data(curr_data);
276 float3 next_pos = load_data(next_data);
277 float3 prev_pos = load_data(prev_data);
278
279 float3 norm_pos_edge0 = normalize(prev_pos - curr_pos);
280 float3 norm_pos_edge1 = normalize(curr_pos - next_pos);
281
282 /* Compute stretches, this logic is adapted from #edituv_get_edituv_stretch_angle.
283 * Keep in sync! */
284 UVStretchAngle stretch;
285 stretch.uv_angle0 = atan(norm_uv_edge0.y, norm_uv_edge0.x) * M_1_PI;
286 stretch.uv_angle1 = atan(norm_uv_edge1.y, norm_uv_edge1.x) * M_1_PI;
287 stretch.angle = angle_normalized_v3v3(norm_pos_edge0, norm_pos_edge1) * M_1_PI;
288
289 edit.uv_stretches[cur_loop_index] = stretch;
290 }
291}
292
294
295/* -------------------------------------------------------------------- */
298
306
309 [[resource_table]] const PolygonOffsetBase &poly_ofs,
311 [[global_invocation_id]] const uint3 global_id,
313{
314 /* We execute for each quad. */
315 const uint quad_index = srt.get_global_invocation_index(global_id, num_work_groups);
316 if (quad_index >= srt.shader_data.total_dispatch_size) {
317 return;
318 }
319
320 /* The start index of the loop is quad_index * 4. */
321 const uint start_loop_index = quad_index * 4;
322
323 const uint coarse_quad_index = poly_ofs.coarse_face_index_from_subdiv_quad_index(
324 quad_index, uint(srt.shader_data.coarse_face_count));
325
326 if ((lnors.extra_coarse_face_data[coarse_quad_index] &
327 srt.shader_data.coarse_face_smooth_mask) != 0)
328 {
329 /* Face is smooth, use vertex normals. */
330 for (uint i = 0; i < 4u; i++) [[unroll]] {
331 const uint subdiv_vert_index = lnors.vert_loop_map[start_loop_index + i];
332 lnors.output_lnor[start_loop_index + i] = lnors.vert_normals[subdiv_vert_index];
333 }
334 }
335 else {
336 StoredFloat3 f0 = lnors.positions[start_loop_index + 0];
337 StoredFloat3 f1 = lnors.positions[start_loop_index + 1];
338 StoredFloat3 f2 = lnors.positions[start_loop_index + 2];
339 StoredFloat3 f3 = lnors.positions[start_loop_index + 3];
340 float3 v0 = load_data(f0);
341 float3 v1 = load_data(f1);
342 float3 v2 = load_data(f2);
343 float3 v3 = load_data(f3);
344
345 float3 face_normal = float3(0.0f);
346 add_newell_cross_v3_v3v3(face_normal, v0, v1);
347 add_newell_cross_v3_v3v3(face_normal, v1, v2);
348 add_newell_cross_v3_v3v3(face_normal, v2, v3);
349 add_newell_cross_v3_v3v3(face_normal, v3, v0);
350
351 StoredFloat3 normal = as_data(normalize(face_normal));
352
353 for (uint i = 0; i < 4; i++) [[unroll]] {
354 lnors.output_lnor[start_loop_index + i] = normal;
355 }
356 }
357}
358
360
361/* -------------------------------------------------------------------- */
364
367
372
373 bool is_face_selected(uint coarse_quad_index)
374 {
375 return (extra_coarse_face_data[coarse_quad_index] & srt.shader_data.coarse_face_select_mask) !=
376 0u;
377 }
378
379 bool is_face_hidden(uint coarse_quad_index)
380 {
381 return (extra_coarse_face_data[coarse_quad_index] & srt.shader_data.coarse_face_hidden_mask) !=
382 0u;
383 }
384
385 /* Flag for paint mode overlay and normals drawing in edit-mode. */
386 int get_loop_flag(uint coarse_quad_index, int vert_origindex)
387 {
388 if (is_face_hidden(coarse_quad_index) ||
389 (srt.shader_data.is_edit_mode && vert_origindex == -1))
390 {
391 return -1;
392 }
393 if (is_face_selected(coarse_quad_index)) {
394 return 1;
395 }
396 return 0;
397 }
398};
399
402 [[resource_table]] const PolygonOffsetBase &poly_ofs,
403 [[resource_table]] PaintResource &paint,
404 [[global_invocation_id]] const uint3 global_id,
406{
407 /* We execute for each quad. */
408 const uint quad_index = srt.get_global_invocation_index(global_id, num_work_groups);
409 if (quad_index >= srt.shader_data.total_dispatch_size) {
410 return;
411 }
412
413 /* The start index of the loop is quad_index * 4. */
414 const uint start_loop_index = quad_index * 4;
415
416 const uint coarse_quad_index = poly_ofs.coarse_face_index_from_subdiv_quad_index(
417 quad_index, uint(srt.shader_data.coarse_face_count));
418 for (uint i = 0; i < 4; i++) [[unroll]] {
419 int origindex = paint.input_vert_origindex[start_loop_index + i];
420 int flag = paint.get_loop_flag(coarse_quad_index, origindex);
421
422 paint.flags[start_loop_index + i] = flag;
423 }
424}
425
427
428/* -------------------------------------------------------------------- */
431
437
441 [[global_invocation_id]] const uint3 global_id,
443{
444 /* We execute for each quad. */
445 const uint quad_index = srt.get_global_invocation_index(global_id, num_work_groups);
446 if (quad_index >= srt.shader_data.total_dispatch_size) {
447 return;
448 }
449
450 const uint start_loop_index = quad_index * 4;
451
452 for (uint loop_index = start_loop_index; loop_index < start_loop_index + 4; loop_index++) {
453 SculptData data = {
454 .face_set_color = sculpt.face_set_color[loop_index],
455 .mask = 0.0f,
456 };
457
458 if (srt.shader_data.has_sculpt_mask) {
459 data.mask = sculpt.mask[loop_index];
460 }
461
462 sculpt.data_buf[loop_index] = data;
463 }
464}
465
467
475
476} // namespace subdiv
#define M_1_PI
#define M_PI
StoredFloat4 as_data(float4 interp)
float4 load_data(StoredFloat4 data)
#define LOOP_NORMALS_POS_SLOT
#define LOOP_NORMALS_VERT_NORMALS_BUF_SLOT
#define EDGE_FAC_EDGE_FAC_BUF_SLOT
#define NORMALS_ACCUMULATE_POS_BUF_SLOT
#define PAINT_OVERLAY_EXTRA_INPUT_VERT_ORIG_INDEX_SLOT
#define LOOP_NORMALS_OUTPUT_LNOR_BUF_SLOT
#define SCULPT_DATA_SCULPT_DATA_BUF_SLOT
#define STRETCH_ANGLE_UV_STRETCHES_BUF_SLOT
#define NORMALS_ACCUMULATE_FACE_ADJACENCY_LISTS_BUF_SLOT
#define EDGE_FAC_POS_BUF_SLOT
#define EDGE_FAC_POLY_OTHER_MAP_BUF_SLOT
#define SUBDIV_GROUP_SIZE
#define NORMALS_ACCUMULATE_NORMALS_BUF_SLOT
#define SCULPT_DATA_SCULPT_FACE_SET_COLOR_BUF_SLOT
#define LOOP_NORMALS_EXTRA_COARSE_FACE_DATA_BUF_SLOT
#define SCULPT_DATA_SCULPT_MASK_BUF_SLOT
#define PAINT_OVERLAY_EXTRA_COARSE_FACE_DATA_BUF_SLOT
#define EDGE_FAC_EDGE_DRAW_FLAG_BUF_SLOT
#define STRETCH_ANGLE_POS_BUF_SLOT
#define STRETCH_AREA_COARSE_STRETCH_AREA_BUF_SLOT
#define NORMALS_ACCUMULATE_VERTEX_LOOP_MAP_BUF_SLOT
#define STRETCH_AREA_SUBDIV_STRETCH_AREA_BUF_SLOT
#define NORMALS_ACCUMULATE_FACE_ADJACENCY_OFFSETS_BUF_SLOT
#define LOOP_NORMALS_VERTEX_LOOP_MAP_BUF_SLOT
#define PAINT_OVERLAY_OUTPUT_FLAG_SLOT
#define STRETCH_ANGLE_UVS_BUF_SLOT
#define storage(slot, qualifiers)
#define resource_table
#define global_invocation_id
#define unroll
#define compute
#define num_work_groups
#define local_size(...)
#define atan
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define asin
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
constexpr T clamp(T, U, U) RET
float length(VecOp< float, D >) RET
#define acos
float2 packed_float2
float loop_edge_factor_get(float3 fa_no, float3 fb_no)
void sculpt_data_main(const SubdivResources &srt, SculptResources &sculpt, const uint3 global_id, const uint3 num_work_groups)
void normals_accumulate_main(const SubdivResources &srt, NormalAccumulate &accum, const uint3 global_id, const uint3 num_work_groups)
void edituv_stretch_area_main(const SubdivResources &srt, const PolygonOffsetBase &poly_ofs, EditUVStretchArea &edit, const uint3 global_id, const uint3 num_work_groups)
void edituv_stretch_angle_main(const SubdivResources &srt, EditUVStretchAngle &edit, const uint3 global_id, const uint3 num_work_groups)
void paint_overlay_flag_main(const SubdivResources &srt, const PolygonOffsetBase &poly_ofs, PaintResource &paint, const uint3 global_id, const uint3 num_work_groups)
PipelineCompute loop_normals(loop_normals_main)
PipelineCompute edge_fac(edge_fac_main)
void write(AttribInterp &res, uint dst_offset, uint index, const T value)
T read(const AttribInterp &res, uint index)
void loop_normals_main(const SubdivResources &srt, const PolygonOffsetBase &poly_ofs, LoopNormalsResource &lnors, const uint3 global_id, const uint3 num_work_groups)
void add_newell_cross_v3_v3v3(float3 &n, float3 v_prev, float3 v_curr)
float angle_normalized_v3v3(float3 v1, float3 v2)
PipelineCompute sculpt_data(sculpt_data_main)
PipelineCompute normals_accumulate(normals_accumulate_main)
PipelineCompute edituv_stretch_area(edituv_stretch_area_main)
PipelineCompute edituv_stretch_angle(edituv_stretch_angle_main)
void edge_fac_main(const SubdivResources &srt, EdgeFac &edges, const uint3 global_id, const uint3 num_work_groups)
PipelineCompute paint_overlay_flag(paint_overlay_flag_main)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
const DRWSubdivUboStorage shader_data
float x
float y
float compute_line_factor(uint corner_index, float3 face_normal)
StoredFloat3(& positions)[]
void find_prev_and_next_vertex_on_face(uint face_index, uint vertex_index, uint &curr, uint &next, uint &prev)
int get_loop_flag(uint coarse_quad_index, int vert_origindex)
const SubdivResources & srt
bool is_face_hidden(uint coarse_quad_index)
bool is_face_selected(uint coarse_quad_index)