Blender V5.3
draw_curves_interpolation.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
14#pragma once
15
20#include "gpu_shader_offset_indices_lib.glsl"
21
22namespace curves {
23
24/* We workaround the lack of function pointers by using different type to overload the attribute
25 * implementation. */
27 /* Position, Radius. */
29};
30
31template<typename StoredT, typename T> struct AttribBuf {
32 /* Inputs. */
33 [[storage(POINT_ATTR_SLOT, read)]] StoredT (&attribute_buf)[];
34 /* Outputs. */
35 [[storage(EVALUATED_ATTR_SLOT, read_write)]] StoredT (&evaluated_buf)[];
36
37 T input_load(int point_index) const
38 {
39 StoredT attr = attribute_buf[point_index];
40 return load_data(attr);
41 }
42
43 T output_load(int evaluated_point_index) const
44 {
45 StoredT attr = evaluated_buf[evaluated_point_index];
46 return load_data(attr);
47 }
48
49 void output_write(int evaluated_point_index, T data)
50 {
51 evaluated_buf[evaluated_point_index] = as_data(data);
52 }
53
54 void output_weighted_add(int evaluated_point_index, float w, const T src)
55 {
56 T dst = output_load(evaluated_point_index);
57 dst += src * w;
58 output_write(evaluated_point_index, dst);
59 }
60
61 void output_mul(int evaluated_point_index, float w)
62 {
63 T dst = output_load(evaluated_point_index);
64 dst *= w;
65 output_write(evaluated_point_index, dst);
66 }
67
68 void output_set_zero(int evaluated_point_index)
69 {
70 output_write(evaluated_point_index, T(0.0f));
71 }
72};
73
74template struct AttribBuf<StoredFloat, float>;
75template struct AttribBuf<StoredFloat2, float2>;
76template struct AttribBuf<StoredFloat3, float3>;
77template struct AttribBuf<StoredFloat4, float4>;
78
80 /* Inputs. */
83 /* Outputs. */
85
87
88 float4 input_load(int point_index) const
89 {
90 float4 data;
91 data.xyz = float3(positions_buf[gpu_attr_load_index(uint(point_index), int2(3, 0)) + 0],
92 positions_buf[gpu_attr_load_index(uint(point_index), int2(3, 0)) + 1],
93 positions_buf[gpu_attr_load_index(uint(point_index), int2(3, 0)) + 2]);
94 data.w = radii_buf[point_index];
95 /* Bake object transform for legacy hair particle. */
96 data.xyz = transform_point(transform, data.xyz);
97 return data;
98 }
99
100 float4 output_load(int evaluated_point_index) const
101 {
102 return evaluated_positions_radii_buf[evaluated_point_index];
103 }
104
105 void output_write(int evaluated_point_index, float4 data)
106 {
107 /* Clamp radius to 0 to avoid negative radius due to interpolation. */
108 data.w = max(0.0f, data.w);
109 evaluated_positions_radii_buf[evaluated_point_index] = data;
110 }
111
112 void output_weighted_add(int evaluated_point_index, float w, const float4 src)
113 {
114 evaluated_positions_radii_buf[evaluated_point_index] += src * w;
115 }
116
117 void output_mul(int evaluated_point_index, float w)
118 {
119 evaluated_positions_radii_buf[evaluated_point_index] *= w;
120 }
121
122 void output_set_zero(int evaluated_point_index)
123 {
124 evaluated_positions_radii_buf[evaluated_point_index] = float4(0.0f);
125 }
126};
127
129
132{
133 v0.data = v0.data * w.x + v1.data * w.y + v2.data * w.z + v3.data * w.w;
134 return v0;
135}
136
137template<typename DataT> DataT mix4(DataT v0, DataT v1, DataT v2, DataT v3, float4 w)
138{
139 v0 = v0 * w.x + v1 * w.y + v2 * w.z + v3 * w.w;
140 return v0;
141}
145template float mix4<float>(float, float, float, float, float4);
146
148 /* Offsets giving the start and end of the curve. */
152 /* Note: Actually int8_t. */
153 [[storage(CURVE_TYPE_SLOT, read)]] const uint (&curves_type_buf)[];
154 /* Note: Actually bool (1 byte). */
155 [[storage(CURVE_CYCLIC_SLOT, read)]] const uint (&curves_cyclic_buf)[];
156 /* Bezier handles (if needed). */
160 /* Nurbs (alias of other buffers). */
161 // [[storage(CURVES_ORDER_SLOT, read)]] uint (&curves_order_buf)[]; /* Actually int8_t. */
162 // [[storage(BASIS_CACHE_SLOT, read)]] float (&basis_cache_buf)[];
163 // [[storage(CONTROL_WEIGHTS_SLOT, read)]] float (&control_weights_buf)[];
164 // [[storage(BASIS_CACHE_OFFSET_SLOT, read)]] int (&basis_cache_offset_buf)[];
165 //
170
177 [[specialization_constant(3 /* CURVE_TYPE_NURBS */)]] int evaluated_type;
178
179 bool curve_cyclic_get(int curve_index) const
180 {
181 if (use_cyclic) {
182 return gpu_attr_load_bool(curves_cyclic_buf, curve_index);
183 }
184 return false;
185 }
186};
187
189
190namespace catmull_rom {
191
192float4 calculate_basis(const float parameter)
193{
194 /* Adapted from Cycles #catmull_rom_basis_eval function. */
195 const float t = parameter;
196 const float s = 1.0f - parameter;
197 return 0.5f * float4(-t * s * s,
198 2.0f + t * t * (3.0f * t - 5.0f),
199 2.0f + s * s * (3.0f * s - 5.0f),
200 -s * t * t);
201}
202
203int4 get_points(uint point_id, IndexRange points, const bool cyclic)
204{
205 int4 point_ids = int(point_id) + int4(-1, +0, +1, +2);
206 if (cyclic) {
207 /* Wrap around. Note the offset by size to avoid modulo with negative values. */
208 point_ids = ((point_ids + points.size()) % points.size());
209 }
210 else {
211 point_ids = clamp(point_ids, int4(0), int4(points.size() - 1));
212 }
213 return points.start() + point_ids;
214}
215
216template<typename StorageT, typename AttrT>
219 const IndexRange points,
220 const IndexRange evaluated_points,
221 const int curve_index)
222{
223 const uint curve_resolution = srt.curves_resolution_buf[curve_index];
224 const bool is_curve_cyclic = srt.curve_cyclic_get(curve_index);
225
226 const uint evaluated_points_count = uint(evaluated_points.size());
227 for (uint i = 0; i < evaluated_points_count; i++) {
228 const int evaluated_point_id = evaluated_points.start() + int(i);
229 const uint point_id = i / curve_resolution;
230 const float parameter = float(i % curve_resolution) / float(curve_resolution);
231 const float4 weights = calculate_basis(parameter);
232 const int4 point_ids = get_points(point_id, points, is_curve_cyclic);
233
234 attr.output_write(evaluated_point_id,
235 mix4(attr.input_load(point_ids.x),
236 attr.input_load(point_ids.y),
237 attr.input_load(point_ids.z),
238 attr.input_load(point_ids.w),
239 weights));
240 }
241}
242
244 const CurvesData &, AttribBuf<InterpPosition, float4> &, IndexRange, IndexRange, int);
246 const CurvesData &, AttribBuf<StoredFloat, float> &, IndexRange, IndexRange, int);
248 const CurvesData &, AttribBuf<StoredFloat2, float2> &, IndexRange, IndexRange, int);
250 const CurvesData &, AttribBuf<StoredFloat3, float3> &, IndexRange, IndexRange, int);
252 const CurvesData &, AttribBuf<StoredFloat4, float4> &, IndexRange, IndexRange, int);
253
254} // namespace catmull_rom
255
256namespace bezier {
257
258template<typename StorageT, typename AttrT>
261 const int2 points,
262 const IndexRange result)
263{
264 AttrT p0 = attr.input_load(points.x);
265 AttrT p1 = attr.input_load(points.y);
266
267 const float step = 1.0f / float(result.size());
268 for (int i = 0; i < result.size(); i++) {
269 attr.output_write(result.start() + i, mix(p0, p1, float(i) * step));
270 }
271}
272
273template<>
275 [[resource_table]] const CurvesData &srt,
277 const int2 points,
278 const IndexRange result)
279{
280 const auto &handles_right = srt.handles_positions_right_buf;
281 const auto &handles_left = srt.handles_positions_left_buf;
282
283 float4 p0 = attr.input_load(points.x);
284 float4 p1 = attr.input_load(points.y);
285
286 const float3 point_0 = p0.xyz;
287 float3 point_1 = gpu_attr_load_float3(handles_right, int2(3, 0), uint(points.x));
288 float3 point_2 = gpu_attr_load_float3(handles_left, int2(3, 0), uint(points.y));
289 const float3 point_3 = p1.xyz;
290
291 const float rad_0 = p0.w;
292 const float rad_1 = p1.w;
293
294 assert(result.size > 0);
295 const float inv_len = 1.0f / float(result.size());
296 const float inv_len_squared = inv_len * inv_len;
297 const float inv_len_cubed = inv_len_squared * inv_len;
298
299 const float3 rt1 = 3.0f * (point_1 - point_0) * inv_len;
300 const float3 rt2 = 3.0f * (point_0 - 2.0f * point_1 + point_2) * inv_len_squared;
301 const float3 rt3 = (point_3 - point_0 + 3.0f * (point_1 - point_2)) * inv_len_cubed;
302
303 float3 q0 = point_0;
304 float3 q1 = rt1 + rt2 + rt3;
305 float3 q2 = 2.0f * rt2 + 6.0f * rt3;
306 float3 q3 = 6.0f * rt3;
307 for (int i = 0; i < result.size(); i++) {
308 float rad = mix(rad_0, rad_1, float(i) * inv_len);
309 attr.output_write(result.start() + i, float4(q0, rad));
310 q0 += q1;
311 q1 += q2;
312 q2 += q3;
313 }
314}
315
318 int2,
319 IndexRange);
322 int2,
323 IndexRange);
326 int2,
327 IndexRange);
330 int2,
331 IndexRange);
332
333IndexRange per_curve_point_offsets_range(const IndexRange points, const int curve_index)
334{
335 return {curve_index + points.start(), points.size() + 1};
336}
337
338int2 get_points(uint point_id, IndexRange points, const bool cyclic)
339{
340 int2 point_ids = int(point_id) + int2(+0, +1);
341 if (cyclic) {
342 /* Wrap around. Note the offset by size to avoid modulo with negative values. */
343 point_ids = ((point_ids + points.size()) % points.size());
344 }
345 else {
346 point_ids = clamp(point_ids, int2(0), int2(points.size() - 1));
347 }
348 return points.start() + point_ids;
349}
350
351template<typename StorageT, typename AttrT>
354 const IndexRange points,
355 const IndexRange evaluated_points,
356 const int curve_index)
357{
358 /* Range used for indexing bezier offsets. */
359 const IndexRange offsets = per_curve_point_offsets_range(points, curve_index);
360 const bool is_curve_cyclic = srt.curve_cyclic_get(curve_index);
361
362 for (int i = 0; i < points.size(); i++) {
363 /* Bezier curves can have different number of evaluated segment per curve segment. */
364 const IndexRange segment_range = offset_indices::load_range_from_buffer(srt.bezier_offsets_buf,
365 offsets.start() + i);
366 const IndexRange evaluated_segment_range = evaluated_points.slice(segment_range);
367 const int2 point_ids = get_points(uint(i), points, is_curve_cyclic);
368
369 evaluate_segment<StorageT, AttrT>(srt, attr, point_ids, evaluated_segment_range);
370 }
371
372 if (is_curve_cyclic) {
373 /* The closing point is not contained inside `bezier_offsets_buf` so we do manual copy. */
374 attr.output_write(evaluated_points.last(), attr.output_load(evaluated_points.first()));
375 }
376}
377
379 const CurvesData &, AttribBuf<InterpPosition, float4> &, IndexRange, IndexRange, int);
381 const CurvesData &, AttribBuf<StoredFloat, float> &, IndexRange, IndexRange, int);
383 const CurvesData &, AttribBuf<StoredFloat2, float2> &, IndexRange, IndexRange, int);
385 const CurvesData &, AttribBuf<StoredFloat3, float3> &, IndexRange, IndexRange, int);
387 const CurvesData &, AttribBuf<StoredFloat4, float4> &, IndexRange, IndexRange, int);
388
389} // namespace bezier
390
391template<typename StorageT, typename AttrT>
394 const IndexRange points,
395 const IndexRange evaluated_points,
396 const int curve_index)
397{
398 assert(points.size == evaluated_points.size);
399 const bool is_curve_cyclic = srt.curve_cyclic_get(curve_index);
400
401 for (int i = 0; i < points.size(); i++) {
402 attr.output_write(evaluated_points.start() + i, attr.input_load(points.start() + i));
403 }
404
405 if (is_curve_cyclic) {
406 /* The closing point is not contained inside `bezier_offsets_buf` so we do manual copy. */
407 attr.output_write(evaluated_points.last(), attr.output_load(evaluated_points.first()));
408 }
409}
410
412 const CurvesData &, AttribBuf<InterpPosition, float4> &, IndexRange, IndexRange, int);
414 const CurvesData &, AttribBuf<StoredFloat, float> &, IndexRange, IndexRange, int);
416 const CurvesData &, AttribBuf<StoredFloat2, float2> &, IndexRange, IndexRange, int);
418 const CurvesData &, AttribBuf<StoredFloat3, float3> &, IndexRange, IndexRange, int);
420 const CurvesData &, AttribBuf<StoredFloat4, float4> &, IndexRange, IndexRange, int);
421
422namespace nurbs {
423
424template<typename StorageT, typename AttrT>
427 const IndexRange points,
428 const IndexRange evaluated_points_padded,
429 const int curve_index)
430{
431 /* Buffer aliasing to same bind point. We cannot dispatch with different type of curve. */
432 const auto &curves_order_buf = srt.curves_resolution_buf;
433 const auto &basis_cache_offset_buf = srt.bezier_offsets_buf;
434
435 uint order = gpu_attr_load_uchar(curves_order_buf, curve_index);
436
437 const int basis_cache_start = basis_cache_offset_buf[curve_index];
438 const bool invalid = basis_cache_start < 0;
439
440 if (invalid) {
441 copy_curve_data<StorageT, AttrT>(srt, attr, points, evaluated_points_padded, curve_index);
442 return;
443 }
444
445 const bool is_curve_cyclic = srt.curve_cyclic_get(curve_index);
446
447 /* Recover original points range without closing cyclic point. */
448 const IndexRange evaluated_points{evaluated_points_padded.start(),
449 evaluated_points_padded.size() - int(srt.use_cyclic)};
450
451 const int start_indices_range_start = basis_cache_start;
452 const int weights_range_start = basis_cache_start + evaluated_points.size();
453
454 /* Buffer aliasing to same bind point. We cannot dispatch with different type of curve. */
455 const auto &basis_cache_buf = srt.handles_positions_left_buf;
456 const auto &control_weights_buf = srt.handles_positions_right_buf;
457
458 for (int i = 0; i < evaluated_points.size(); i++) {
459 int evaluated_point_index = evaluated_points.start() + i;
460 /* Equivalent to `attribute_math::DefaultMixer<T> mixer{dst}`. */
461 attr.output_set_zero(evaluated_point_index);
462 float total_weight = 0.0f;
463
464 const IndexRange point_weights{weights_range_start + i * int(order), int(order)};
465 const int start_index = floatBitsToInt(basis_cache_buf[start_indices_range_start + i]);
466
467 for (int j = 0; j < point_weights.size(); j++) {
468 const int point_index = points.start() + (start_index + j) % points.size();
469 const float point_weight = basis_cache_buf[point_weights.start() + j];
470 const float control_weight = srt.use_point_weight ? control_weights_buf[point_index] : 1.0f;
471 const float weight = point_weight * control_weight;
472 /* Equivalent to `mixer.mix_in()`. */
473 attr.output_weighted_add(evaluated_point_index, weight, attr.input_load(point_index));
474 total_weight += weight;
475 }
476 /* Equivalent to `mixer.finalize()` */
477 attr.output_mul(evaluated_point_index, safe_rcp(total_weight));
478 }
479
480 if (is_curve_cyclic) {
481 /* The closing point is not contained inside the NURBS data structure so we do manual copy. */
482 attr.output_write(evaluated_points_padded.last(),
483 attr.output_load(evaluated_points_padded.first()));
484 }
485}
486
488 const CurvesData &, AttribBuf<InterpPosition, float4> &, IndexRange, IndexRange, int);
490 const CurvesData &, AttribBuf<StoredFloat, float> &, IndexRange, IndexRange, int);
492 const CurvesData &, AttribBuf<StoredFloat2, float2> &, IndexRange, IndexRange, int);
494 const CurvesData &, AttribBuf<StoredFloat3, float3> &, IndexRange, IndexRange, int);
496 const CurvesData &, AttribBuf<StoredFloat4, float4> &, IndexRange, IndexRange, int);
497
498} // namespace nurbs
499
500template<typename StorageT, typename AttrT>
502 [[resource_table]] const CurvesData &srt,
504 [[global_invocation_id]] const uint3 global_id)
505{
506 if (global_id.x >= uint(srt.curves_count)) {
507 return;
508 }
509 int curve_index = int(global_id.x) + srt.curves_start;
510
511 uint curve_type = gpu_attr_load_uchar(srt.curves_type_buf, curve_index);
512 if (CurveType(curve_type) != CurveType(srt.evaluated_type)) {
513 return;
514 }
515 IndexRange points = offset_indices::load_range_from_buffer(srt.points_by_curve_buf, curve_index);
516 IndexRange evaluated_points = offset_indices::load_range_from_buffer(
517 srt.evaluated_points_by_curve_buf, curve_index);
518
519 if (srt.use_cyclic) {
520 evaluated_points = IndexRange{evaluated_points.start() + curve_index,
521 evaluated_points.size() + 1};
522 }
523
524 if (CurveType(srt.evaluated_type) == CURVE_TYPE_CATMULL_ROM) {
525 catmull_rom::evaluate_curve<StorageT, AttrT>(srt, attr, points, evaluated_points, curve_index);
526 }
527 else if (CurveType(srt.evaluated_type) == CURVE_TYPE_BEZIER) {
528 bezier::evaluate_curve<StorageT, AttrT>(srt, attr, points, evaluated_points, curve_index);
529 }
530 else if (CurveType(srt.evaluated_type) == CURVE_TYPE_NURBS) {
531 nurbs::evaluate_curve<StorageT, AttrT>(srt, attr, points, evaluated_points, curve_index);
532 }
533 else if (CurveType(srt.evaluated_type) == CURVE_TYPE_POLY) {
534 /* Simple copy. */
535 copy_curve_data<StorageT, AttrT>(srt, attr, points, evaluated_points, curve_index);
536 }
537}
538
541 const uint3);
544 const uint3);
547 const uint3);
550 const uint3);
553 const uint3);
554
555} // namespace curves
556
btScalar max
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
StoredFloat4 as_data(float4 interp)
float4 load_data(StoredFloat4 data)
#define HANDLES_POS_LEFT_SLOT
#define CURVES_PER_THREADGROUP
#define POINT_ATTR_SLOT
#define EVALUATED_ATTR_SLOT
#define CURVE_TYPE_SLOT
#define POINT_RADII_SLOT
#define POINTS_BY_CURVES_SLOT
#define EVALUATED_POS_RAD_SLOT
#define EVALUATED_POINT_SLOT
#define HANDLES_POS_RIGHT_SLOT
#define CURVE_CYCLIC_SLOT
#define BEZIER_OFFSETS_SLOT
#define POINT_POSITIONS_SLOT
#define CURVE_RESOLUTION_SLOT
PipelineCompute draw_curves_interpolate_float_attribute(curves::evaluate_curve< StoredFloat, float >)
PipelineCompute draw_curves_interpolate_float4_attribute(curves::evaluate_curve< StoredFloat4, float4 >)
PipelineCompute draw_curves_interpolate_float3_attribute(curves::evaluate_curve< StoredFloat3, float3 >)
PipelineCompute draw_curves_interpolate_float2_attribute(curves::evaluate_curve< StoredFloat2, float2 >)
PipelineCompute draw_curves_interpolate_position(curves::evaluate_curve< curves::InterpPosition, float4 >)
uint gpu_attr_load_uchar(const uint(&data_buf)[], uint i)
uint gpu_attr_load_index(uint vertex_index, int2 stride_and_offset)
float3 gpu_attr_load_float3(const float(&data_buf)[], int2 stride_and_offset, uint i)
bool gpu_attr_load_bool(const uint(&data)[], uint i)
#define assert(assertion)
#define storage(slot, qualifiers)
#define push_constant
#define resource_table
#define specialization_constant(default_value)
#define global_invocation_id
#define compute
#define local_size(...)
VecBase< int, D > floatBitsToInt(VecOp< float, D >) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
float3 transform_point(float3x3 mat, float3 point)
float safe_rcp(float a)
#define T
MatBase< float, 4, 4 > float4x4
int2 get_points(uint point_id, IndexRange points, const bool cyclic)
IndexRange per_curve_point_offsets_range(const IndexRange points, const int curve_index)
void evaluate_curve(const CurvesData &srt, AttribBuf< StorageT, AttrT > &attr, const IndexRange points, const IndexRange evaluated_points, const int curve_index)
void evaluate_segment(const CurvesData &, AttribBuf< StorageT, AttrT > &attr, const int2 points, const IndexRange result)
void evaluate_curve(const CurvesData &srt, AttribBuf< StorageT, AttrT > &attr, const IndexRange points, const IndexRange evaluated_points, const int curve_index)
float4 calculate_basis(const float parameter)
int4 get_points(uint point_id, IndexRange points, const bool cyclic)
void evaluate_curve(const CurvesData &srt, AttribBuf< StorageT, AttrT > &attr, const IndexRange points, const IndexRange evaluated_points_padded, const int curve_index)
InterpPosition mix4(InterpPosition v0, InterpPosition v1, InterpPosition v2, InterpPosition v3, float4 w)
void copy_curve_data(const CurvesData &srt, AttribBuf< StorageT, AttrT > &attr, const IndexRange points, const IndexRange evaluated_points, const int curve_index)
void evaluate_curve(const CurvesData &srt, AttribBuf< StorageT, AttrT > &attr, const uint3 global_id)
#define mix
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void output_mul(int evaluated_point_index, float w)
float4 output_load(int evaluated_point_index) const
void output_weighted_add(int evaluated_point_index, float w, const float4 src)
void output_write(int evaluated_point_index, float4 data)
void output_set_zero(int evaluated_point_index)
void output_weighted_add(int evaluated_point_index, float w, const T src)
void output_write(int evaluated_point_index, T data)
T input_load(int point_index) const
T output_load(int evaluated_point_index) const
void output_mul(int evaluated_point_index, float w)
bool curve_cyclic_get(int curve_index) const
float w
Definition sky_math.h:225