Blender  V3.3
subdiv_eval.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2018 Blender Foundation. All rights reserved. */
3 
8 #include "BKE_subdiv_eval.h"
9 
10 #include "DNA_mesh_types.h"
11 #include "DNA_meshdata_types.h"
12 
13 #include "BLI_bitmap.h"
14 #include "BLI_math_vector.h"
15 #include "BLI_task.h"
16 #include "BLI_utildefines.h"
17 
18 #include "BKE_customdata.h"
19 #include "BKE_subdiv.h"
20 
21 #include "MEM_guardedalloc.h"
22 
25 
26 /* --------------------------------------------------------------------
27  * Helper functions.
28  */
29 
31  eSubdivEvaluatorType evaluator_type)
32 {
33  switch (evaluator_type) {
36  }
39  }
40  }
41  BLI_assert_msg(0, "Unknown evaluator type");
43 }
44 
45 /* --------------------------------------------------------------------
46  * Main subdivision evaluation.
47  */
48 
50  eSubdivEvaluatorType evaluator_type,
51  OpenSubdiv_EvaluatorCache *evaluator_cache,
52  const OpenSubdiv_EvaluatorSettings *settings)
53 {
55  if (subdiv->topology_refiner == NULL) {
56  /* Happens on input mesh with just loose geometry,
57  * or when OpenSubdiv is disabled */
58  return false;
59  }
60  if (subdiv->evaluator == NULL) {
61  eOpenSubdivEvaluator opensubdiv_evaluator_type =
65  subdiv->topology_refiner, opensubdiv_evaluator_type, evaluator_cache);
67  if (subdiv->evaluator == NULL) {
68  return false;
69  }
70  }
71  else {
72  /* TODO(sergey): Check for topology change. */
73  }
74  subdiv->evaluator->setSettings(subdiv->evaluator, settings);
76  return true;
77 }
78 
79 static void set_coarse_positions(Subdiv *subdiv,
80  const Mesh *mesh,
81  const float (*coarse_vertex_cos)[3])
82 {
83  const MVert *mvert = mesh->mvert;
84  const MLoop *mloop = mesh->mloop;
85  const MPoly *mpoly = mesh->mpoly;
86  /* Mark vertices which needs new coordinates. */
87  /* TODO(sergey): This is annoying to calculate this on every update,
88  * maybe it's better to cache this mapping. Or make it possible to have
89  * OpenSubdiv's vertices match mesh ones? */
90  BLI_bitmap *vertex_used_map = BLI_BITMAP_NEW(mesh->totvert, "vert used map");
91  for (int poly_index = 0; poly_index < mesh->totpoly; poly_index++) {
92  const MPoly *poly = &mpoly[poly_index];
93  for (int corner = 0; corner < poly->totloop; corner++) {
94  const MLoop *loop = &mloop[poly->loopstart + corner];
95  BLI_BITMAP_ENABLE(vertex_used_map, loop->v);
96  }
97  }
98  /* Use a temporary buffer so we do not upload vertices one at a time to the GPU. */
99  float(*buffer)[3] = MEM_mallocN(sizeof(float[3]) * mesh->totvert, "subdiv tmp coarse positions");
100  int manifold_vertex_count = 0;
101  for (int vertex_index = 0, manifold_vertex_index = 0; vertex_index < mesh->totvert;
102  vertex_index++) {
103  if (!BLI_BITMAP_TEST_BOOL(vertex_used_map, vertex_index)) {
104  continue;
105  }
106  const float *vertex_co;
107  if (coarse_vertex_cos != NULL) {
108  vertex_co = coarse_vertex_cos[vertex_index];
109  }
110  else {
111  const MVert *vertex = &mvert[vertex_index];
112  vertex_co = vertex->co;
113  }
114  copy_v3_v3(&buffer[manifold_vertex_index][0], vertex_co);
115  manifold_vertex_index++;
116  manifold_vertex_count++;
117  }
118  subdiv->evaluator->setCoarsePositions(
119  subdiv->evaluator, &buffer[0][0], 0, manifold_vertex_count);
120  MEM_freeN(vertex_used_map);
121  MEM_freeN(buffer);
122 }
123 
124 /* Context which is used to fill face varying data in parallel. */
127  const Mesh *mesh;
128  const MLoopUV *mloopuv;
129  float (*buffer)[2];
132 
133 static void set_face_varying_data_from_uv_task(void *__restrict userdata,
134  const int face_index,
135  const TaskParallelTLS *__restrict UNUSED(tls))
136 {
137  FaceVaryingDataFromUVContext *ctx = userdata;
138  OpenSubdiv_TopologyRefiner *topology_refiner = ctx->topology_refiner;
139  const int layer_index = ctx->layer_index;
140  const Mesh *mesh = ctx->mesh;
141  const MPoly *mpoly = &mesh->mpoly[face_index];
142  const MLoopUV *mluv = &ctx->mloopuv[mpoly->loopstart];
143 
144  /* TODO(sergey): OpenSubdiv's C-API converter can change winding of
145  * loops of a face, need to watch for that, to prevent wrong UVs assigned.
146  */
147  const int num_face_vertices = topology_refiner->getNumFaceVertices(topology_refiner, face_index);
148  const int *uv_indices = topology_refiner->getFaceFVarValueIndices(
149  topology_refiner, face_index, layer_index);
150  for (int vertex_index = 0; vertex_index < num_face_vertices; vertex_index++, mluv++) {
151  copy_v2_v2(ctx->buffer[uv_indices[vertex_index]], mluv->uv);
152  }
153 }
154 
156  const Mesh *mesh,
157  const MLoopUV *mloopuv,
158  const int layer_index)
159 {
160  OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
161  OpenSubdiv_Evaluator *evaluator = subdiv->evaluator;
162  const int num_faces = topology_refiner->getNumFaces(topology_refiner);
163  const MLoopUV *mluv = mloopuv;
164 
165  const int num_fvar_values = topology_refiner->getNumFVarValues(topology_refiner, layer_index);
166  /* Use a temporary buffer so we do not upload UVs one at a time to the GPU. */
167  float(*buffer)[2] = MEM_mallocN(sizeof(float[2]) * num_fvar_values, "temp UV storage");
168 
170  ctx.topology_refiner = topology_refiner;
171  ctx.layer_index = layer_index;
172  ctx.mloopuv = mluv;
173  ctx.mesh = mesh;
174  ctx.buffer = buffer;
175 
176  TaskParallelSettings parallel_range_settings;
177  BLI_parallel_range_settings_defaults(&parallel_range_settings);
178  parallel_range_settings.min_iter_per_thread = 1;
179 
181  0, num_faces, &ctx, set_face_varying_data_from_uv_task, &parallel_range_settings);
182 
183  evaluator->setFaceVaryingData(evaluator, layer_index, &buffer[0][0], 0, num_fvar_values);
184 
185  MEM_freeN(buffer);
186 }
187 
188 static void set_vertex_data_from_orco(Subdiv *subdiv, const Mesh *mesh)
189 {
190  const float(*orco)[3] = CustomData_get_layer(&mesh->vdata, CD_ORCO);
191  const float(*cloth_orco)[3] = CustomData_get_layer(&mesh->vdata, CD_CLOTH_ORCO);
192 
193  if (orco || cloth_orco) {
194  OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
195  OpenSubdiv_Evaluator *evaluator = subdiv->evaluator;
196  const int num_verts = topology_refiner->getNumVertices(topology_refiner);
197 
198  if (orco && cloth_orco) {
199  /* Set one by one if have both. */
200  for (int i = 0; i < num_verts; i++) {
201  float data[6];
202  copy_v3_v3(data, orco[i]);
203  copy_v3_v3(data + 3, cloth_orco[i]);
204  evaluator->setVertexData(evaluator, data, i, 1);
205  }
206  }
207  else {
208  /* Faster single call if we have either. */
209  if (orco) {
210  evaluator->setVertexData(evaluator, orco[0], 0, num_verts);
211  }
212  else if (cloth_orco) {
213  evaluator->setVertexData(evaluator, cloth_orco[0], 0, num_verts);
214  }
215  }
216  }
217 }
218 
220 {
221  settings->num_vertex_data = (CustomData_has_layer(&mesh->vdata, CD_ORCO) ? 3 : 0) +
223 }
224 
226  const Mesh *mesh,
227  const float (*coarse_vertex_cos)[3],
228  eSubdivEvaluatorType evaluator_type,
229  OpenSubdiv_EvaluatorCache *evaluator_cache)
230 {
231  OpenSubdiv_EvaluatorSettings settings = {0};
232  get_mesh_evaluator_settings(&settings, mesh);
233  if (!BKE_subdiv_eval_begin(subdiv, evaluator_type, evaluator_cache, &settings)) {
234  return false;
235  }
236  return BKE_subdiv_eval_refine_from_mesh(subdiv, mesh, coarse_vertex_cos);
237 }
238 
240  const Mesh *mesh,
241  const float (*coarse_vertex_cos)[3])
242 {
243  if (subdiv->evaluator == NULL) {
244  /* NOTE: This situation is supposed to be handled by begin(). */
245  BLI_assert_msg(0, "Is not supposed to happen");
246  return false;
247  }
248  /* Set coordinates of base mesh vertices. */
249  set_coarse_positions(subdiv, mesh, coarse_vertex_cos);
250  /* Set face-varying data to UV maps. */
251  const int num_uv_layers = CustomData_number_of_layers(&mesh->ldata, CD_MLOOPUV);
252  for (int layer_index = 0; layer_index < num_uv_layers; layer_index++) {
253  const MLoopUV *mloopuv = CustomData_get_layer_n(&mesh->ldata, CD_MLOOPUV, layer_index);
254  set_face_varying_data_from_uv(subdiv, mesh, mloopuv, layer_index);
255  }
256  /* Set vertex data to orco. */
258  /* Update evaluator to the new coarse geometry. */
260  subdiv->evaluator->refine(subdiv->evaluator);
262  return true;
263 }
264 
266 {
267  if (subdiv->displacement_evaluator == NULL) {
268  return;
269  }
270  if (subdiv->displacement_evaluator->initialize == NULL) {
271  return;
272  }
274 }
275 
276 /* --------------------------------------------------------------------
277  * Single point queries.
278  */
279 
281  Subdiv *subdiv, const int ptex_face_index, const float u, const float v, float r_P[3])
282 {
283  BKE_subdiv_eval_limit_point_and_derivatives(subdiv, ptex_face_index, u, v, r_P, NULL, NULL);
284 }
285 
287  const int ptex_face_index,
288  const float u,
289  const float v,
290  float r_P[3],
291  float r_dPdu[3],
292  float r_dPdv[3])
293 {
294  subdiv->evaluator->evaluateLimit(subdiv->evaluator, ptex_face_index, u, v, r_P, r_dPdu, r_dPdv);
295 
296  /* NOTE: In a very rare occasions derivatives are evaluated to zeros or are exactly equal.
297  * This happens, for example, in single vertex on Suzannne's nose (where two quads have 2 common
298  * edges).
299  *
300  * This makes tangent space displacement (such as multi-resolution) impossible to be used in
301  * those vertices, so those needs to be addressed in one way or another.
302  *
303  * Simplest thing to do: step inside of the face a little bit, where there is known patch at
304  * which there must be proper derivatives. This might break continuity of normals, but is better
305  * that giving totally unusable derivatives. */
306 
307  if (r_dPdu != NULL && r_dPdv != NULL) {
308  if ((is_zero_v3(r_dPdu) || is_zero_v3(r_dPdv)) || equals_v3v3(r_dPdu, r_dPdv)) {
309  subdiv->evaluator->evaluateLimit(subdiv->evaluator,
310  ptex_face_index,
311  u * 0.999f + 0.0005f,
312  v * 0.999f + 0.0005f,
313  r_P,
314  r_dPdu,
315  r_dPdv);
316  }
317  }
318 }
319 
321  const int ptex_face_index,
322  const float u,
323  const float v,
324  float r_P[3],
325  float r_N[3])
326 {
327  float dPdu[3], dPdv[3];
328  BKE_subdiv_eval_limit_point_and_derivatives(subdiv, ptex_face_index, u, v, r_P, dPdu, dPdv);
329  cross_v3_v3v3(r_N, dPdu, dPdv);
330  normalize_v3(r_N);
331 }
332 
334  Subdiv *subdiv, const int ptex_face_index, const float u, const float v, float r_vertex_data[])
335 {
336  subdiv->evaluator->evaluateVertexData(subdiv->evaluator, ptex_face_index, u, v, r_vertex_data);
337 }
338 
340  const int face_varying_channel,
341  const int ptex_face_index,
342  const float u,
343  const float v,
344  float r_face_varying[2])
345 {
347  subdiv->evaluator, face_varying_channel, ptex_face_index, u, v, r_face_varying);
348 }
349 
351  const int ptex_face_index,
352  const float u,
353  const float v,
354  const float dPdu[3],
355  const float dPdv[3],
356  float r_D[3])
357 {
358  if (subdiv->displacement_evaluator == NULL) {
359  zero_v3(r_D);
360  return;
361  }
363  subdiv->displacement_evaluator, ptex_face_index, u, v, dPdu, dPdv, r_D);
364 }
365 
367  Subdiv *subdiv, const int ptex_face_index, const float u, const float v, float r_P[3])
368 {
369  if (subdiv->displacement_evaluator) {
370  float dPdu[3], dPdv[3], D[3];
371  BKE_subdiv_eval_limit_point_and_derivatives(subdiv, ptex_face_index, u, v, r_P, dPdu, dPdv);
372  BKE_subdiv_eval_displacement(subdiv, ptex_face_index, u, v, dPdu, dPdv, D);
373  add_v3_v3(r_P, D);
374  }
375  else {
376  BKE_subdiv_eval_limit_point(subdiv, ptex_face_index, u, v, r_P);
377  }
378 }
typedef float(TangentPoint)[2]
CustomData interface, see also DNA_customdata_types.h.
int CustomData_number_of_layers(const struct CustomData *data, int type)
bool CustomData_has_layer(const struct CustomData *data, int type)
void * CustomData_get_layer_n(const struct CustomData *data, int type, int n)
void * CustomData_get_layer(const struct CustomData *data, int type)
@ SUBDIV_STATS_EVALUATOR_REFINE
Definition: BKE_subdiv.h:80
@ SUBDIV_STATS_EVALUATOR_CREATE
Definition: BKE_subdiv.h:79
void BKE_subdiv_stats_end(SubdivStats *stats, eSubdivStatsValue value)
Definition: subdiv_stats.c:31
void BKE_subdiv_stats_reset(SubdivStats *stats, eSubdivStatsValue value)
Definition: subdiv_stats.c:36
void BKE_subdiv_stats_begin(SubdivStats *stats, eSubdivStatsValue value)
Definition: subdiv_stats.c:26
eSubdivEvaluatorType
@ SUBDIV_EVALUATOR_TYPE_GPU
@ SUBDIV_EVALUATOR_TYPE_CPU
#define BLI_assert_msg(a, msg)
Definition: BLI_assert.h:53
#define BLI_BITMAP_NEW(_num, _alloc_string)
Definition: BLI_bitmap.h:40
#define BLI_BITMAP_ENABLE(_bitmap, _index)
Definition: BLI_bitmap.h:81
#define BLI_BITMAP_TEST_BOOL(_bitmap, _index)
Definition: BLI_bitmap.h:74
unsigned int BLI_bitmap
Definition: BLI_bitmap.h:16
MINLINE float normalize_v3(float r[3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE bool is_zero_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE bool equals_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void zero_v3(float r[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
void BLI_task_parallel_range(int start, int stop, void *userdata, TaskParallelRangeFunc func, const TaskParallelSettings *settings)
Definition: task_range.cc:94
BLI_INLINE void BLI_parallel_range_settings_defaults(TaskParallelSettings *settings)
Definition: BLI_task.h:293
#define UNUSED(x)
@ CD_CLOTH_ORCO
@ CD_MLOOPUV
Read Guarded memory(de)allocation.
ATTR_WARN_UNUSED_RESULT const BMVert * v
OpenSubdiv_Evaluator * openSubdiv_createEvaluatorFromTopologyRefiner(OpenSubdiv_TopologyRefiner *topology_refiner, eOpenSubdivEvaluator evaluator_type, OpenSubdiv_EvaluatorCache *evaluator_cache)
ccl_global float * buffer
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
eOpenSubdivEvaluator
@ OPENSUBDIV_EVALUATOR_GPU
@ OPENSUBDIV_EVALUATOR_CPU
OpenSubdiv_TopologyRefiner * topology_refiner
Definition: subdiv_eval.c:126
unsigned int v
float co[3]
CustomData vdata
struct MVert * mvert
int totvert
struct MLoop * mloop
int totpoly
struct MPoly * mpoly
CustomData ldata
void(* setVertexData)(struct OpenSubdiv_Evaluator *evaluator, const float *data, const int start_vertex_index, const int num_vertices)
void(* setCoarsePositions)(struct OpenSubdiv_Evaluator *evaluator, const float *positions, const int start_vertex_index, const int num_vertices)
void(* evaluateLimit)(struct OpenSubdiv_Evaluator *evaluator, const int ptex_face_index, float face_u, float face_v, float P[3], float dPdu[3], float dPdv[3])
void(* setFaceVaryingData)(struct OpenSubdiv_Evaluator *evaluator, const int face_varying_channel, const float *face_varying_data, const int start_vertex_index, const int num_vertices)
void(* evaluateVertexData)(struct OpenSubdiv_Evaluator *evaluator, const int ptex_face_index, float face_u, float face_v, float data[])
void(* evaluateFaceVarying)(struct OpenSubdiv_Evaluator *evaluator, const int face_varying_channel, const int ptex_face_index, float face_u, float face_v, float face_varying[2])
void(* refine)(struct OpenSubdiv_Evaluator *evaluator)
void(* setSettings)(struct OpenSubdiv_Evaluator *evaluator, const OpenSubdiv_EvaluatorSettings *settings)
int(* getNumFaceVertices)(const struct OpenSubdiv_TopologyRefiner *topology_refiner, const int face_index)
int(* getNumFVarValues)(const struct OpenSubdiv_TopologyRefiner *topology_refiner, const int channel)
const int *(* getFaceFVarValueIndices)(const struct OpenSubdiv_TopologyRefiner *topology_refiner, const int face_index, const int channel)
int(* getNumFaces)(const struct OpenSubdiv_TopologyRefiner *topology_refiner)
int(* getNumVertices)(const struct OpenSubdiv_TopologyRefiner *topology_refiner)
void(* initialize)(struct SubdivDisplacement *displacement)
Definition: BKE_subdiv.h:125
void(* eval_displacement)(struct SubdivDisplacement *displacement, int ptex_face_index, float u, float v, const float dPdu[3], const float dPdv[3], float r_D[3])
Definition: BKE_subdiv.h:137
SubdivStats stats
Definition: BKE_subdiv.h:170
struct SubdivDisplacement * displacement_evaluator
Definition: BKE_subdiv.h:168
struct OpenSubdiv_TopologyRefiner * topology_refiner
Definition: BKE_subdiv.h:164
struct OpenSubdiv_Evaluator * evaluator
Definition: BKE_subdiv.h:166
void BKE_subdiv_eval_limit_point(Subdiv *subdiv, const int ptex_face_index, const float u, const float v, float r_P[3])
Definition: subdiv_eval.c:280
struct FaceVaryingDataFromUVContext FaceVaryingDataFromUVContext
void BKE_subdiv_eval_init_displacement(Subdiv *subdiv)
Definition: subdiv_eval.c:265
bool BKE_subdiv_eval_begin(Subdiv *subdiv, eSubdivEvaluatorType evaluator_type, OpenSubdiv_EvaluatorCache *evaluator_cache, const OpenSubdiv_EvaluatorSettings *settings)
Definition: subdiv_eval.c:49
static void set_face_varying_data_from_uv(Subdiv *subdiv, const Mesh *mesh, const MLoopUV *mloopuv, const int layer_index)
Definition: subdiv_eval.c:155
void BKE_subdiv_eval_displacement(Subdiv *subdiv, const int ptex_face_index, const float u, const float v, const float dPdu[3], const float dPdv[3], float r_D[3])
Definition: subdiv_eval.c:350
static void set_vertex_data_from_orco(Subdiv *subdiv, const Mesh *mesh)
Definition: subdiv_eval.c:188
bool BKE_subdiv_eval_begin_from_mesh(Subdiv *subdiv, const Mesh *mesh, const float(*coarse_vertex_cos)[3], eSubdivEvaluatorType evaluator_type, OpenSubdiv_EvaluatorCache *evaluator_cache)
Definition: subdiv_eval.c:225
void BKE_subdiv_eval_face_varying(Subdiv *subdiv, const int face_varying_channel, const int ptex_face_index, const float u, const float v, float r_face_varying[2])
Definition: subdiv_eval.c:339
bool BKE_subdiv_eval_refine_from_mesh(Subdiv *subdiv, const Mesh *mesh, const float(*coarse_vertex_cos)[3])
Definition: subdiv_eval.c:239
void BKE_subdiv_eval_final_point(Subdiv *subdiv, const int ptex_face_index, const float u, const float v, float r_P[3])
Definition: subdiv_eval.c:366
void BKE_subdiv_eval_vertex_data(Subdiv *subdiv, const int ptex_face_index, const float u, const float v, float r_vertex_data[])
Definition: subdiv_eval.c:333
static void get_mesh_evaluator_settings(OpenSubdiv_EvaluatorSettings *settings, const Mesh *mesh)
Definition: subdiv_eval.c:219
static void set_face_varying_data_from_uv_task(void *__restrict userdata, const int face_index, const TaskParallelTLS *__restrict UNUSED(tls))
Definition: subdiv_eval.c:133
static eOpenSubdivEvaluator opensubdiv_evalutor_from_subdiv_evaluator_type(eSubdivEvaluatorType evaluator_type)
Definition: subdiv_eval.c:30
void BKE_subdiv_eval_limit_point_and_normal(Subdiv *subdiv, const int ptex_face_index, const float u, const float v, float r_P[3], float r_N[3])
Definition: subdiv_eval.c:320
static void set_coarse_positions(Subdiv *subdiv, const Mesh *mesh, const float(*coarse_vertex_cos)[3])
Definition: subdiv_eval.c:79
void BKE_subdiv_eval_limit_point_and_derivatives(Subdiv *subdiv, const int ptex_face_index, const float u, const float v, float r_P[3], float r_dPdu[3], float r_dPdv[3])
Definition: subdiv_eval.c:286
BLI_INLINE float D(const float *data, const int res[3], int x, int y, int z)
Definition: voxel.c:13