[Bf-blender-cvs] [5545a9c0ddb] temp-vert-normals-cleanup: More fixes for float mesh normals in subdivision code

Hans Goudey noreply at git.blender.org
Wed Sep 22 06:03:31 CEST 2021


Commit: 5545a9c0ddb03b6ff87baf321c6dd13a35808b0b
Author: Hans Goudey
Date:   Tue Sep 21 23:03:22 2021 -0500
Branches: temp-vert-normals-cleanup
https://developer.blender.org/rB5545a9c0ddb03b6ff87baf321c6dd13a35808b0b

More fixes for float mesh normals in subdivision code

===================================================================

M	source/blender/blenkernel/BKE_subdiv_eval.h
M	source/blender/blenkernel/intern/particle_distribute.c
M	source/blender/blenkernel/intern/shrinkwrap.c
M	source/blender/blenkernel/intern/subdiv_eval.c
M	source/blender/blenkernel/intern/subdiv_mesh.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_subdiv_eval.h b/source/blender/blenkernel/BKE_subdiv_eval.h
index 0b61e62c89c..0c258a7baad 100644
--- a/source/blender/blenkernel/BKE_subdiv_eval.h
+++ b/source/blender/blenkernel/BKE_subdiv_eval.h
@@ -69,12 +69,6 @@ void BKE_subdiv_eval_limit_point_and_normal(struct Subdiv *subdiv,
                                             const float v,
                                             float r_P[3],
                                             float r_N[3]);
-void BKE_subdiv_eval_limit_point_and_short_normal(struct Subdiv *subdiv,
-                                                  const int ptex_face_index,
-                                                  const float u,
-                                                  const float v,
-                                                  float r_P[3],
-                                                  short r_N[3]);
 
 /* Evaluate face-varying layer (such as UV). */
 void BKE_subdiv_eval_face_varying(struct Subdiv *subdiv,
@@ -135,15 +129,6 @@ void BKE_subdiv_eval_limit_patch_resolution_point_and_normal(struct Subdiv *subd
                                                              void *normal_buffer,
                                                              const int normal_offset,
                                                              const int normal_stride);
-void BKE_subdiv_eval_limit_patch_resolution_point_and_short_normal(struct Subdiv *subdiv,
-                                                                   const int ptex_face_index,
-                                                                   const int resolution,
-                                                                   void *point_buffer,
-                                                                   const int point_offset,
-                                                                   const int point_stride,
-                                                                   void *normal_buffer,
-                                                                   const int normal_offset,
-                                                                   const int normal_stride);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/blenkernel/intern/particle_distribute.c b/source/blender/blenkernel/intern/particle_distribute.c
index 863476c6638..aef53d93f41 100644
--- a/source/blender/blenkernel/intern/particle_distribute.c
+++ b/source/blender/blenkernel/intern/particle_distribute.c
@@ -626,7 +626,8 @@ static void distribute_from_volume_exec(ParticleTask *thread, ParticleData *pa,
   /* experimental */
   tot = mesh->totface;
 
-  psys_interpolate_face(mvert, mface, 0, 0, pa->fuv, co, nor, 0, 0, 0);
+  psys_interpolate_face(
+      mvert, BKE_mesh_ensure_vertex_normals(mesh), mface, 0, 0, pa->fuv, co, nor, 0, 0, 0);
 
   normalize_v3(nor);
   negate_v3(nor);
diff --git a/source/blender/blenkernel/intern/shrinkwrap.c b/source/blender/blenkernel/intern/shrinkwrap.c
index 6514dcfce20..2cf4208022c 100644
--- a/source/blender/blenkernel/intern/shrinkwrap.c
+++ b/source/blender/blenkernel/intern/shrinkwrap.c
@@ -552,7 +552,7 @@ static void shrinkwrap_calc_normal_projection_cb_ex(void *__restrict userdata,
      * (to get correct normals) for other cases calc->verts contains undeformed coordinates and
      * vertexCos should be used */
     copy_v3_v3(tmp_co, calc->vert[i].co);
-    normal_short_to_float_v3(tmp_no, calc->vert[i].no);
+    copy_v3_v3(tmp_no, calc->vert_normals[i]);
   }
   else {
     copy_v3_v3(tmp_co, co);
@@ -1217,9 +1217,9 @@ void BKE_shrinkwrap_compute_smooth_normal(const struct ShrinkwrapTreeData *tree,
 
   /* Interpolate smooth normals if enabled. */
   if ((tree->mesh->mpoly[tri->poly].flag & ME_SMOOTH) != 0) {
-    const int vert_indices[3] = {treeData->loop[tri->tri[0]].v,
-                                 treeData->loop[tri->tri[1]].v,
-                                 treeData->loop[tri->tri[2]].v};
+    const uint32_t vert_indices[3] = {treeData->loop[tri->tri[0]].v,
+                                      treeData->loop[tri->tri[1]].v,
+                                      treeData->loop[tri->tri[2]].v};
     float w[3], no[3][3], tmp_co[3];
 
     /* Custom and auto smooth split normals. */
diff --git a/source/blender/blenkernel/intern/subdiv_eval.c b/source/blender/blenkernel/intern/subdiv_eval.c
index 0001eb8a205..f9df082ca3a 100644
--- a/source/blender/blenkernel/intern/subdiv_eval.c
+++ b/source/blender/blenkernel/intern/subdiv_eval.c
@@ -221,18 +221,6 @@ void BKE_subdiv_eval_limit_point_and_normal(Subdiv *subdiv,
   normalize_v3(r_N);
 }
 
-void BKE_subdiv_eval_limit_point_and_short_normal(Subdiv *subdiv,
-                                                  const int ptex_face_index,
-                                                  const float u,
-                                                  const float v,
-                                                  float r_P[3],
-                                                  short r_N[3])
-{
-  float N_float[3];
-  BKE_subdiv_eval_limit_point_and_normal(subdiv, ptex_face_index, u, v, r_P, N_float);
-  normal_float_to_short_v3(r_N, N_float);
-}
-
 void BKE_subdiv_eval_face_varying(Subdiv *subdiv,
                                   const int face_varying_channel,
                                   const int ptex_face_index,
@@ -368,30 +356,3 @@ void BKE_subdiv_eval_limit_patch_resolution_point_and_normal(Subdiv *subdiv,
     }
   }
 }
-
-void BKE_subdiv_eval_limit_patch_resolution_point_and_short_normal(Subdiv *subdiv,
-                                                                   const int ptex_face_index,
-                                                                   const int resolution,
-                                                                   void *point_buffer,
-                                                                   const int point_offset,
-                                                                   const int point_stride,
-                                                                   void *normal_buffer,
-                                                                   const int normal_offset,
-                                                                   const int normal_stride)
-{
-  buffer_apply_offset(&point_buffer, point_offset);
-  buffer_apply_offset(&normal_buffer, normal_offset);
-  const float inv_resolution_1 = 1.0f / (float)(resolution - 1);
-  for (int y = 0; y < resolution; y++) {
-    const float v = y * inv_resolution_1;
-    for (int x = 0; x < resolution; x++) {
-      const float u = x * inv_resolution_1;
-      short normal[3];
-      BKE_subdiv_eval_limit_point_and_short_normal(
-          subdiv, ptex_face_index, u, v, point_buffer, normal);
-      buffer_write_short_value(&normal_buffer, normal, 3);
-      buffer_apply_offset(&point_buffer, point_stride);
-      buffer_apply_offset(&normal_buffer, normal_stride);
-    }
-  }
-}
diff --git a/source/blender/blenkernel/intern/subdiv_mesh.c b/source/blender/blenkernel/intern/subdiv_mesh.c
index e9cd0b70019..c6f2f7cc52a 100644
--- a/source/blender/blenkernel/intern/subdiv_mesh.c
+++ b/source/blender/blenkernel/intern/subdiv_mesh.c
@@ -66,9 +66,7 @@ typedef struct SubdivMeshContext {
    * Displacement is being accumulated to a vertices coordinates, since those
    * are not needed during traversal of edge/corner vertices.
    *
-   * For normals we are using dedicated array, since we can not use same
-   * vertices (normals are `short`, which will cause a lot of precision
-   * issues). */
+   * This normal array is owned by #subdiv_mesh. */
   float (*accumulated_normals)[3];
   /* Per-subdivided vertex counter of averaged values. */
   int *accumulated_counters;
@@ -107,8 +105,9 @@ static void subdiv_mesh_prepare_accumulator(SubdivMeshContext *ctx, int num_vert
   }
   /* TODO(sergey): Technically, this is overallocating, we don't need memory
    * for an inner subdivision vertices. */
-  ctx->accumulated_normals = MEM_calloc_arrayN(
-      sizeof(*ctx->accumulated_normals), num_vertices, "subdiv accumulated normals");
+  ctx->accumulated_normals = (float(*)[3])CustomData_add_layer(
+      &ctx->subdiv_mesh->vdata, CD_NORMAL, CD_DEFAULT, NULL, num_vertices);
+  memset(ctx->accumulated_normals, 0, sizeof(float[3]) * num_vertices);
   ctx->accumulated_counters = MEM_calloc_arrayN(
       sizeof(*ctx->accumulated_counters), num_vertices, "subdiv accumulated counters");
 }
@@ -459,10 +458,10 @@ static void eval_final_point_and_vertex_normal(Subdiv *subdiv,
                                                const float u,
                                                const float v,
                                                float r_P[3],
-                                               short r_N[3])
+                                               float r_N[3])
 {
   if (subdiv->displacement_evaluator == NULL) {
-    BKE_subdiv_eval_limit_point_and_short_normal(subdiv, ptex_face_index, u, v, r_P, r_N);
+    BKE_subdiv_eval_limit_point_and_normal(subdiv, ptex_face_index, u, v, r_P, r_N);
   }
   else {
     BKE_subdiv_eval_final_point(subdiv, ptex_face_index, u, v, r_P);
@@ -586,15 +585,8 @@ static void evaluate_vertex_and_apply_displacement_copy(const SubdivMeshContext
   /* Copy custom data and evaluate position. */
   subdiv_vertex_data_copy(ctx, coarse_vert, subdiv_vert);
   BKE_subdiv_eval_limit_point(ctx->subdiv, ptex_face_index, u, v, subdiv_vert->co);
-  /* Apply displacement. */
+  /* Apply displacement. The normals have already been accumulated. */
   add_v3_v3(subdiv_vert->co, D);
-  /* Copy normal from accumulated storage. */
-  if (ctx->can_evaluate_normals) {
-    float N[3];
-    copy_v3_v3(N, ctx->accumulated_normals[subdiv_vertex_index]);
-    normalize_v3(N);
-    normal_float_to_short_v3(subdiv_vert->no, N);
-  }
   /* Remove facedot flag. This can happen if there is more than one subsurf modifier. */
   subdiv_vert->flag &= ~ME_VERT_FACEDOT;
 }
@@ -624,11 +616,8 @@ static void evaluate_vertex_and_apply_displacement_interpolate(
   /* Copy normal from accumulated storage. */
   if (ctx->can_evaluate_normals) {
     const float i

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list