Blender V5.3
subdiv_eval.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_attribute.hh"
10#include "BKE_subdiv_eval.hh"
11
12#include "BLI_array_utils.hh"
13#include "BLI_math_vector.hh"
14#include "BLI_math_vector_c.hh"
15#include "BLI_task_c.hh"
16
17#include "BKE_customdata.hh"
18#include "BKE_mesh.hh"
19#include "BKE_subdiv.hh"
20
21#include "MEM_guardedalloc.h"
22
24#ifdef WITH_OPENSUBDIV
27#endif
28
29/* --------------------------------------------------------------------
30 * Helper functions.
31 */
32
33namespace blender::bke::subdiv {
34
35#ifdef WITH_OPENSUBDIV
36
37static eOpenSubdivEvaluator opensubdiv_evaluator_from_subdiv_evaluator_type(
38 eSubdivEvaluatorType evaluator_type)
39{
40 switch (evaluator_type) {
43 }
46 }
47 }
48 BLI_assert_msg(0, "Unknown evaluator type");
50}
51
52#endif
53
54/* --------------------------------------------------------------------
55 * Main subdivision evaluation.
56 */
57
59 eSubdivEvaluatorType evaluator_type,
60 OpenSubdiv_EvaluatorCache *evaluator_cache,
61 const OpenSubdiv_EvaluatorSettings *settings)
62{
63#ifdef WITH_OPENSUBDIV
65 if (subdiv->topology_refiner == nullptr) {
66 /* Happens on input mesh with just loose geometry,
67 * or when OpenSubdiv is disabled */
68 return false;
69 }
70 if (subdiv->evaluator == nullptr) {
71 eOpenSubdivEvaluator opensubdiv_evaluator_type =
72 opensubdiv_evaluator_from_subdiv_evaluator_type(evaluator_type);
75 subdiv->topology_refiner, opensubdiv_evaluator_type, evaluator_cache);
77 if (subdiv->evaluator == nullptr) {
78 return false;
79 }
80 }
81 else {
82 /* TODO(sergey): Check for topology change. */
83 }
84 subdiv->evaluator->eval_output->setSettings(settings);
86 return true;
87#else
88 UNUSED_VARS(subdiv, evaluator_type, evaluator_cache, settings);
89 return false;
90#endif
91}
92
93#ifdef WITH_OPENSUBDIV
94
95static void set_coarse_positions(Subdiv *subdiv,
96 const Span<float3> positions,
97 const IndexMask &verts_no_face)
98{
99 OpenSubdiv_Evaluator *evaluator = subdiv->evaluator;
100 if (verts_no_face.is_empty()) {
102 reinterpret_cast<const float *>(positions.data()), 0, positions.size());
103 return;
104 }
105 Array<float3> used_vert_positions(positions.size() - verts_no_face.size());
106 IndexMaskMemory memory;
107 const IndexMask verts = verts_no_face.complement(positions.index_range(), memory);
108 array_utils::gather(positions, verts, used_vert_positions.as_mutable_span());
110 reinterpret_cast<const float *>(used_vert_positions.data()), 0, used_vert_positions.size());
111}
112
113/* Context which is used to fill face varying data in parallel. */
114struct FaceVaryingDataFromUVContext {
115 opensubdiv::TopologyRefinerImpl *topology_refiner;
116 const Mesh *mesh;
117 OffsetIndices<int> faces;
118 Span<float2> uv_map;
119 float (*buffer)[2];
120 int layer_index;
121};
122
123static void set_face_varying_data_from_uv_task(void *__restrict userdata,
124 const int face_index,
125 const TaskParallelTLS *__restrict /*tls*/)
126{
127 FaceVaryingDataFromUVContext *ctx = static_cast<FaceVaryingDataFromUVContext *>(userdata);
128 opensubdiv::TopologyRefinerImpl *topology_refiner = ctx->topology_refiner;
129 const int layer_index = ctx->layer_index;
130 const Span<float2> face_uvs = ctx->uv_map.slice(ctx->faces[face_index]);
131
132 /* TODO(sergey): OpenSubdiv's C-API converter can change winding of
133 * loops of a face, need to watch for that, to prevent wrong UVs assigned.
134 */
135 const OpenSubdiv::Vtr::ConstIndexArray uv_indices =
136 topology_refiner->base_level().GetFaceFVarValues(face_index, layer_index);
137 for (int i = 0; i < uv_indices.size(); i++) {
138 copy_v2_v2(ctx->buffer[uv_indices[i]], face_uvs[i]);
139 }
140}
141
142static void set_face_varying_data_from_uv(Subdiv *subdiv,
143 const Mesh *mesh,
144 const Span<float2> uv_map,
145 const int layer_index)
146{
147 opensubdiv::TopologyRefinerImpl *topology_refiner = subdiv->topology_refiner;
148 OpenSubdiv_Evaluator *evaluator = subdiv->evaluator;
149 const int num_faces = topology_refiner->base_level().GetNumFaces();
150
151 const int num_fvar_values = topology_refiner->base_level().GetNumFVarValues(layer_index);
152 /* Use a temporary buffer so we do not upload UVs one at a time to the GPU. */
153 float (*buffer)[2] = MEM_new_array_uninitialized<float[2]>(size_t(num_fvar_values), __func__);
154
155 FaceVaryingDataFromUVContext ctx;
156 ctx.topology_refiner = topology_refiner;
157 ctx.layer_index = layer_index;
158 ctx.uv_map = uv_map;
159 ctx.mesh = mesh;
160 ctx.faces = mesh->faces();
161 ctx.buffer = buffer;
162
163 TaskParallelSettings parallel_range_settings;
164 BLI_parallel_range_settings_defaults(&parallel_range_settings);
165 parallel_range_settings.min_iter_per_thread = 1;
166
168 0, num_faces, &ctx, set_face_varying_data_from_uv_task, &parallel_range_settings);
169
170 evaluator->eval_output->setFaceVaryingData(layer_index, &buffer[0][0], 0, num_fvar_values);
171
172 MEM_delete(buffer);
173}
174
175static void set_vert_data_from_orco(Subdiv *subdiv, const Mesh *mesh)
176{
177 const float (*orco)[3] = static_cast<const float (*)[3]>(
178 CustomData_get_layer(&mesh->vert_data, CD_ORCO));
179 const float (*cloth_orco)[3] = static_cast<const float (*)[3]>(
180 CustomData_get_layer(&mesh->vert_data, CD_CLOTH_ORCO));
181
182 if (orco || cloth_orco) {
183 opensubdiv::TopologyRefinerImpl *topology_refiner = subdiv->topology_refiner;
184 OpenSubdiv_Evaluator *evaluator = subdiv->evaluator;
185 const int num_verts = topology_refiner->base_level().GetNumVertices();
186
187 if (orco && cloth_orco) {
188 /* Set one by one if have both. */
189 for (int i = 0; i < num_verts; i++) {
190 float data[6];
191 copy_v3_v3(data, orco[i]);
192 copy_v3_v3(data + 3, cloth_orco[i]);
193 evaluator->eval_output->setVertexData(data, i, 1);
194 }
195 }
196 else {
197 /* Faster single call if we have either. */
198 if (orco) {
199 evaluator->eval_output->setVertexData(orco[0], 0, num_verts);
200 }
201 else if (cloth_orco) {
202 evaluator->eval_output->setVertexData(cloth_orco[0], 0, num_verts);
203 }
204 }
205 }
206}
207
208static void get_mesh_evaluator_settings(OpenSubdiv_EvaluatorSettings *settings, const Mesh *mesh)
209{
210 settings->num_vertex_data = (CustomData_has_layer(&mesh->vert_data, CD_ORCO) ? 3 : 0) +
211 (CustomData_has_layer(&mesh->vert_data, CD_CLOTH_ORCO) ? 3 : 0);
212}
213
214#endif
215
217 const Mesh *mesh,
218 eSubdivEvaluatorType evaluator_type,
219 const Span<float3> coarse_vert_positions,
220 OpenSubdiv_EvaluatorCache *evaluator_cache)
221{
222#ifdef WITH_OPENSUBDIV
223 OpenSubdiv_EvaluatorSettings settings = {0};
224 get_mesh_evaluator_settings(&settings, mesh);
225 if (!eval_begin(subdiv, evaluator_type, evaluator_cache, &settings)) {
226 return false;
227 }
228 return eval_refine_from_mesh(subdiv, mesh, coarse_vert_positions);
229#else
230 UNUSED_VARS(subdiv, mesh, coarse_vert_positions, evaluator_type, evaluator_cache);
231 return false;
232#endif
233}
234
236 const Mesh *mesh,
237 const Span<float3> coarse_vert_positions)
238{
239#ifdef WITH_OPENSUBDIV
240 if (subdiv->evaluator == nullptr) {
241 /* NOTE: This situation is supposed to be handled by begin(). */
242 BLI_assert_msg(0, "Is not supposed to happen");
243 return false;
244 }
245 /* Set coordinates of base mesh vertices. */
246 set_coarse_positions(subdiv,
247 coarse_vert_positions.is_empty() ? mesh->vert_positions() :
248 coarse_vert_positions,
249 mesh->verts_no_face());
250
251 /* Set face-varying data to UV maps. */
252 const AttributeAccessor attributes = mesh->attributes();
253 VectorSet<StringRefNull> uv_map_names = mesh->uv_map_names();
254 for (const int i : uv_map_names.index_range()) {
255 const VArraySpan uv_map = *attributes.lookup<float2>(uv_map_names[i], bke::AttrDomain::Corner);
256 set_face_varying_data_from_uv(subdiv, mesh, uv_map, i);
257 }
258 /* Set vertex data to orco. */
259 set_vert_data_from_orco(subdiv, mesh);
260 /* Update evaluator to the new coarse geometry. */
262 subdiv->evaluator->eval_output->refine();
264 return true;
265#else
266 UNUSED_VARS(subdiv, mesh, coarse_vert_positions);
267 return false;
268#endif
269}
270
272{
273 if (subdiv->displacement_evaluator == nullptr) {
274 return;
275 }
276 if (subdiv->displacement_evaluator->initialize == nullptr) {
277 return;
278 }
279 subdiv->displacement_evaluator->initialize(subdiv->displacement_evaluator);
280}
281
282/* --------------------------------------------------------------------
283 * Single point queries.
284 */
285
286float3 eval_limit_point(Subdiv *subdiv, const int ptex_face_index, const float u, const float v)
287{
288#ifdef WITH_OPENSUBDIV
289 float3 r_P;
290 subdiv->evaluator->eval_output->evaluateLimit(ptex_face_index, u, v, r_P, nullptr, nullptr);
291 return r_P;
292#else
293 UNUSED_VARS(subdiv, ptex_face_index, u, v);
294 return {0.0f, 0.0f, 0.0f};
295#endif
296}
297
299 const int ptex_face_index,
300 const float u,
301 const float v,
302 float3 &r_P,
303 float3 &r_dPdu,
304 float3 &r_dPdv)
305{
306#ifdef WITH_OPENSUBDIV
307 subdiv->evaluator->eval_output->evaluateLimit(ptex_face_index, u, v, r_P, r_dPdu, r_dPdv);
308
309 /* NOTE: In a very rare occasions derivatives are evaluated to zeros or are exactly equal.
310 * This happens, for example, in single vertex on Suzannne's nose (where two quads have 2 common
311 * edges).
312 *
313 * This makes tangent space displacement (such as multi-resolution) impossible to be used in
314 * those vertices, so those needs to be addressed in one way or another.
315 *
316 * Simplest thing to do: step inside of the face a little bit, where there is known patch at
317 * which there must be proper derivatives. This might break continuity of normals, but is better
318 * that giving totally unusable derivatives. */
319
320 if ((math::is_zero(r_dPdu) || math::is_zero(r_dPdv)) || math::is_equal(r_dPdu, r_dPdv)) {
321 subdiv->evaluator->eval_output->evaluateLimit(
322 ptex_face_index, u * 0.999f + 0.0005f, v * 0.999f + 0.0005f, r_P, r_dPdu, r_dPdv);
323 }
324#else
325 UNUSED_VARS(subdiv, ptex_face_index, u, v, r_P, r_dPdu, r_dPdv);
326#endif
327}
328
330 const int ptex_face_index,
331 const float u,
332 const float v,
333 float3 &r_P,
334 float3 &r_N)
335{
336 float3 dPdu;
337 float3 dPdv;
338 eval_limit_point_and_derivatives(subdiv, ptex_face_index, u, v, r_P, dPdu, dPdv);
339 r_N = math::normalize(math::cross(dPdu, dPdv));
340}
341
343 Subdiv *subdiv, const int ptex_face_index, const float u, const float v, float r_vert_data[])
344{
345#ifdef WITH_OPENSUBDIV
346 subdiv->evaluator->eval_output->evaluateVertexData(ptex_face_index, u, v, r_vert_data);
347#else
348 UNUSED_VARS(subdiv, ptex_face_index, u, v, r_vert_data);
349#endif
350}
351
353 const int face_varying_channel,
354 const int ptex_face_index,
355 const float u,
356 const float v,
357 float2 &r_face_varying)
358{
359#ifdef WITH_OPENSUBDIV
360 subdiv->evaluator->eval_output->evaluateFaceVarying(
361 face_varying_channel, ptex_face_index, u, v, r_face_varying);
362#else
363 UNUSED_VARS(subdiv, ptex_face_index, face_varying_channel, u, v, r_face_varying);
364#endif
365}
366
368 const int ptex_face_index,
369 const float u,
370 const float v,
371 const float3 &dPdu,
372 const float3 &dPdv,
373 float3 &r_D)
374{
375 if (subdiv->displacement_evaluator == nullptr) {
376 r_D = float3(0.0f);
377 return;
378 }
379 subdiv->displacement_evaluator->eval_displacement(
380 subdiv->displacement_evaluator, ptex_face_index, u, v, dPdu, dPdv, r_D);
381}
382
383float3 eval_final_point(Subdiv *subdiv, const int ptex_face_index, const float u, const float v)
384{
385 float3 r_P;
386 if (subdiv->displacement_evaluator) {
387 float3 dPdu;
388 float3 dPdv;
389 float3 D;
390 eval_limit_point_and_derivatives(subdiv, ptex_face_index, u, v, r_P, dPdu, dPdv);
391 eval_displacement(subdiv, ptex_face_index, u, v, dPdu, dPdv, D);
392 r_P += D;
393 }
394 else {
395 r_P = eval_limit_point(subdiv, ptex_face_index, u, v);
396 }
397 return r_P;
398}
399
400} // namespace blender::bke::subdiv
CustomData interface, see also DNA_customdata_types.h.
Read Blender memory allocation and freeing.
#define UNUSED_VARS(...)
#define BLI_assert_msg(a, msg)
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
IndexRange index_range() const
GAttributeReader lookup(const StringRef name) const
IndexMask complement(const IndexMask &universe, LinearAllocator<> &memory) const
void setFaceVaryingData(const int face_varying_channel, const float *varying_data, const int start_vertex_index, const int num_vertices)
void setVertexData(const float *data, const int start_vertex_index, const int num_vertices)
void setCoarsePositions(const float *positions, const int start_vertex_index, const int num_vertices)
OpenSubdiv_Evaluator * openSubdiv_createEvaluatorFromTopologyRefiner(blender::opensubdiv::TopologyRefinerImpl *topology_refiner, eOpenSubdivEvaluator evaluator_type, OpenSubdiv_EvaluatorCache *evaluator_cache_descr)
void gather(const GVArray &src, const IndexMask &indices, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
void eval_init_displacement(Subdiv *subdiv)
float3 eval_final_point(Subdiv *subdiv, int ptex_face_index, float u, float v)
bool eval_begin_from_mesh(Subdiv *subdiv, const Mesh *mesh, eSubdivEvaluatorType evaluator_type, Span< float3 > coarse_vert_positions={}, OpenSubdiv_EvaluatorCache *evaluator_cache=nullptr)
float3 eval_limit_point(Subdiv *subdiv, int ptex_face_index, float u, float v)
bool eval_begin(Subdiv *subdiv, eSubdivEvaluatorType evaluator_type, OpenSubdiv_EvaluatorCache *evaluator_cache, const OpenSubdiv_EvaluatorSettings *settings)
void eval_limit_point_and_normal(Subdiv *subdiv, int ptex_face_index, float u, float v, float3 &r_P, float3 &r_N)
void stats_reset(SubdivStats *stats, StatsValue value)
void eval_limit_point_and_derivatives(Subdiv *subdiv, int ptex_face_index, float u, float v, float3 &r_P, float3 &r_dPdu, float3 &r_dPdv)
void eval_displacement(Subdiv *subdiv, int ptex_face_index, float u, float v, const float3 &dPdu, const float3 &dPdv, float3 &r_D)
void stats_begin(SubdivStats *stats, StatsValue value)
void stats_end(SubdivStats *stats, StatsValue value)
bool eval_refine_from_mesh(Subdiv *subdiv, const Mesh *mesh, Span< float3 > coarse_vert_positions)
void eval_vert_data(Subdiv *subdiv, int ptex_face_index, float u, float v, float r_vert_data[])
void eval_face_varying(Subdiv *subdiv, int face_varying_channel, int ptex_face_index, float u, float v, float2 &r_face_varying)
bool is_zero(const T &a)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
bool is_equal(const MatBase< T, NumCol, NumRow > &a, const MatBase< T, NumCol, NumRow > &b, const T epsilon=T(0))
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
BLI_INLINE float D(const float *data, const int res[3], int x, int y, int z)
Definition voxel.cc:16
MINLINE void copy_v3_v3(float r[3], const float a[3])
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
bool CustomData_has_layer(const CustomData *data, eCustomDataType type)
MINLINE void copy_v2_v2(float r[2], const float a[2])
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
ATTR_WARN_UNUSED_RESULT const BMVert * v
VecBase< float, 2 > float2
BMesh const char void * data
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
VecBase< float, 3 > float3
eOpenSubdivEvaluator
@ OPENSUBDIV_EVALUATOR_GPU
@ OPENSUBDIV_EVALUATOR_CPU
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
blender::opensubdiv::EvalOutputAPI * eval_output