Blender V5.3
subdiv_attrib_interp.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "subdiv_common.bsl.hh"
12
13namespace subdiv {
14
15enum ComponentType : int {
16 U16 = 0,
17 I32 = 1,
18 F32 = 2,
19};
20
25
28
29 uint get_vertex_count(uint coarse_face) const
30 {
31 uint number_of_patches = face_ptex_offset[coarse_face + 1] - face_ptex_offset[coarse_face];
32 if (number_of_patches == 1) {
33 /* If there is only one patch for the current coarse polygon, then it is a quad. */
34 return 4;
35 }
36 /* Otherwise, the number of patches is the number of vertices. */
37 return number_of_patches;
38 }
39
40 uint get_polygon_corner_index(uint coarse_face, uint patch_index) const
41 {
42 uint patch_offset = face_ptex_offset[coarse_face];
43 return patch_index - patch_offset;
44 }
45
46 uint get_loop_start([[resource_table]] const SubdivResources &srt, uint coarse_face) const
47 {
48 return extra_coarse_face_data[coarse_face] & srt.shader_data.coarse_face_loopstart_mask;
49 }
50};
51
52template<typename T, int Dim, enum ComponentType Comp> T read(const AttribInterp &res, uint index)
53{
54 T value;
55 if constexpr (Comp == U16) {
56 const uint base_index = index * 2;
57 const uint xy = res.src_data[base_index];
58 const uint zw = res.src_data[base_index + 1];
59
60 value[0] = float((xy >> 16) & 0xFFFFu) / 65535.0f;
61 value[1] = float(xy & 0xFFFFu) / 65535.0f;
62 value[2] = float((zw >> 16) & 0xFFFFu) / 65535.0f;
63 value[3] = float(zw & 0xFFFFu) / 65535.0f;
64#ifdef GLSL_CPP_STUBS
65 /* Other cases unsupported for now. */
66 static_assert(Dim == 4);
67#endif
68 }
69 else if constexpr (Comp == I32) {
70 const uint base_index = index * Dim;
71 if constexpr (Dim == 1) {
72 value = float(int(res.src_data[base_index]));
73 }
74 else {
75 for (int i = 0; i < Dim; i++) [[unroll]] {
76 value[i] = float(int(res.src_data[base_index + i]));
77 }
78 }
79 }
80 else if constexpr (Comp == F32) {
81 const uint base_index = index * Dim;
82 if constexpr (Dim == 1) {
83 value = uintBitsToFloat(res.src_data[base_index]);
84 }
85 else {
86 for (int i = 0; i < Dim; i++) [[unroll]] {
87 value[i] = uintBitsToFloat(res.src_data[base_index + uint(i)]);
88 }
89 }
90 }
91 return value;
92}
93
95template float read<float, 1, I32>(const AttribInterp &, uint);
99template float read<float, 1, F32>(const AttribInterp &, uint);
103
104template<typename T, int Dim, enum ComponentType Comp>
105void write(AttribInterp &res, uint dst_offset, uint index, const T value)
106{
107 if constexpr (Comp == U16) {
108 const uint base_index = dst_offset + index * 2;
109 uint x = uint(value[0] * 65535.0f);
110 uint y = uint(value[1] * 65535.0f);
111 uint z = uint(value[2] * 65535.0f);
112 uint w = uint(value[3] * 65535.0f);
113
114 res.dst_data[base_index + 0] = x << 16 | y;
115 res.dst_data[base_index + 1] = z << 16 | w;
116#ifdef GLSL_CPP_STUBS
117 /* Other cases unsupported for now. */
118 static_assert(Dim == 4);
119#endif
120 }
121 else if constexpr (Comp == I32) {
122 uint base_index = dst_offset + index * Dim;
123 if constexpr (Dim == 1) {
124 res.dst_data[base_index] = uint(int(round(value)));
125 }
126 else {
127 for (int i = 0; i < Dim; i++) {
128 res.dst_data[base_index + i] = uint(int(round(value[i])));
129 }
130 }
131 }
132 else if constexpr (Comp == F32) {
133 uint base_index = dst_offset + index * Dim;
134 if constexpr (Dim == 1) {
135 res.dst_data[base_index] = floatBitsToUint(value);
136 }
137 else {
138 for (uint i = 0; i < Dim; i++) {
139 res.dst_data[base_index + i] = floatBitsToUint(value[i]);
140 }
141 }
142 }
143}
144
145template void write<float4, 4, U16>(AttribInterp &, uint, uint, float4);
146template void write<float, 1, I32>(AttribInterp &, uint, uint, float);
147template void write<float2, 2, I32>(AttribInterp &, uint, uint, float2);
148template void write<float3, 3, I32>(AttribInterp &, uint, uint, float3);
149template void write<float4, 4, I32>(AttribInterp &, uint, uint, float4);
150template void write<float, 1, F32>(AttribInterp &, uint, uint, float);
151template void write<float2, 2, F32>(AttribInterp &, uint, uint, float2);
152template void write<float3, 3, F32>(AttribInterp &, uint, uint, float3);
153template void write<float4, 4, F32>(AttribInterp &, uint, uint, float4);
154
155template<typename T, int Dim, enum ComponentType Comp, bool Normalize>
157 [[resource_table]] const SubdivResources &srt,
158 [[resource_table]] const PolygonOffsetBase &poly_ofs,
160 [[global_invocation_id]] const uint3 global_id,
162{
163 /* We execute for each quad. */
164 const uint quad_index = srt.get_global_invocation_index(global_id, num_work_groups);
165 if (quad_index >= srt.shader_data.total_dispatch_size) {
166 return;
167 }
168
169 const uint start_loop_index = quad_index * 4;
170
171 /* Find which coarse polygon we came from. */
172 const uint coarse_face = poly_ofs.coarse_face_index_from_subdiv_quad_index(
173 quad_index, uint(srt.shader_data.coarse_face_count));
174 const uint loop_start = res.get_loop_start(srt, coarse_face);
175
176 /* Find the number of vertices for the coarse polygon. */
177 T v0 = T(0.0f), v1 = T(0.0f), v2 = T(0.0f), v3 = T(0.0f);
178
179 const uint number_of_vertices = res.get_vertex_count(coarse_face);
180 if (number_of_vertices == 4) {
181 /* Interpolate the src data. */
182 v0 = read<T, Dim, Comp>(res, loop_start + 0);
183 v1 = read<T, Dim, Comp>(res, loop_start + 1);
184 v2 = read<T, Dim, Comp>(res, loop_start + 2);
185 v3 = read<T, Dim, Comp>(res, loop_start + 3);
186 }
187 else {
188 /* Interpolate the src data for the center. */
189 uint loop_end = loop_start + number_of_vertices;
190 T center_value = T(0);
191
192 float weight = 1.0f / float(number_of_vertices);
193
194 for (uint l = loop_start; l < loop_end; l++) {
195 center_value += read<T, Dim, Comp>(res, l) * weight;
196 }
197
198 /* Interpolate between the previous and next corner for the middle values for the edges. */
199 uint patch_index = uint(res.patch_coords[start_loop_index].patch_index);
200 uint current_coarse_corner = res.get_polygon_corner_index(coarse_face, patch_index);
201 uint next_coarse_corner = (current_coarse_corner + 1) % number_of_vertices;
202 uint prev_coarse_corner = (current_coarse_corner + number_of_vertices - 1) %
203 number_of_vertices;
204
205 v0 = read<T, Dim, Comp>(res, loop_start + current_coarse_corner);
206 v1 = read<T, Dim, Comp>(res, loop_start + next_coarse_corner);
207 v3 = read<T, Dim, Comp>(res, loop_start + prev_coarse_corner);
208 /* Midpoint. */
209 v1 = (v0 + v1) * 0.5f;
210 v3 = (v0 + v3) * 0.5f;
211 /* Interpolate between the current value, and the ones for the center and mid-edges. */
212 v2 = center_value;
213 }
214
215 /* Do a linear interpolation of the data based on the UVs for each loop of this subdivided quad.
216 */
217 for (uint loop_index = start_loop_index; loop_index < start_loop_index + 4; loop_index++) {
218 BlenderPatchCoord co = res.patch_coords[loop_index];
219 float2 uv = decode_uv(co.encoded_uv);
220 /* NOTE: v2 and v3 are reversed to stay consistent with the interpolation weight on the x-axis:
221 *
222 * v3 +-----+ v2
223 * | |
224 * | |
225 * v0 +-----+ v1
226 *
227 * otherwise, weight would be `1.0f - uv.x` for `v2 <-> v3`, but `uv.x` for `v0 <-> v1`.
228 */
229 T e = mix(v0, v1, uv.x);
230 T f = mix(v3, v2, uv.x);
231 T result = mix(e, f, uv.y);
232
233 if constexpr (Normalize) {
234 result = normalize(result);
235 }
236
237 write<T, Dim, Comp>(res, srt.shader_data.dst_offset, loop_index, result);
238 }
239}
240
241/* clang-format off */
252/* clang-format on */
253
264
265} // namespace subdiv
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define CUSTOM_DATA_SOURCE_DATA_BUF_SLOT
#define CUSTOM_DATA_FACE_PTEX_OFFSET_BUF_SLOT
#define SUBDIV_GROUP_SIZE
#define CUSTOM_DATA_DESTINATION_DATA_BUF_SLOT
#define CUSTOM_DATA_EXTRA_COARSE_FACE_DATA_BUF_SLOT
#define CUSTOM_DATA_PATCH_COORDS_BUF_SLOT
IMETHOD Vector Normalize(const Vector &a, double eps)
Definition frames.inl:170
#define storage(slot, qualifiers)
#define index(i)
#define resource_table
#define global_invocation_id
#define unroll
#define compute
#define num_work_groups
#define local_size(...)
#define round
VecBase< float, D > normalize(VecOp< float, D >) RET
VecBase< uint, D > floatBitsToUint(VecOp< float, D >) RET
VecBase< float, D > uintBitsToFloat(VecOp< uint, D >) RET
#define T
PipelineCompute custom_data_interp_4d_u16(attribute_interp< float4, 4, U16, false >)
PipelineCompute custom_data_interp_2d_i32(attribute_interp< float2, 2, I32, false >)
void write(AttribInterp &res, uint dst_offset, uint index, const T value)
T read(const AttribInterp &res, uint index)
PipelineCompute custom_data_interp_1d_f32(attribute_interp< float, 1, F32, false >)
PipelineCompute custom_data_interp_4d_i32(attribute_interp< float4, 4, I32, false >)
PipelineCompute custom_data_interp_3d_f32(attribute_interp< float3, 3, F32, false >)
PipelineCompute custom_data_interp_1d_i32(attribute_interp< float, 1, I32, false >)
PipelineCompute custom_data_interp_3d_i32(attribute_interp< float3, 3, I32, false >)
PipelineCompute custom_data_interp_3d_f32_normalize(attribute_interp< float3, 3, F32, true >)
void attribute_interp(const SubdivResources &srt, const PolygonOffsetBase &poly_ofs, AttribInterp &res, const uint3 global_id, const uint3 num_work_groups)
PipelineCompute custom_data_interp_4d_f32(attribute_interp< float4, 4, F32, false >)
PipelineCompute custom_data_interp_2d_f32(attribute_interp< float2, 2, F32, false >)
#define mix
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float x
float y
uint get_polygon_corner_index(uint coarse_face, uint patch_index) const
BlenderPatchCoord(& patch_coords)[]
uint get_vertex_count(uint coarse_face) const
uint get_loop_start(const SubdivResources &srt, uint coarse_face) const
float2 decode_uv(uint encoded_uv)