2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
* Copyright 2018 Blender Foundation. All rights reserved. */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
*/
|
|
|
|
|
|
2018-07-25 16:51:48 +02:00
|
|
|
#include "atomic_ops.h"
|
|
|
|
|
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_key_types.h"
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
#include "DNA_mesh_types.h"
|
|
|
|
|
#include "DNA_meshdata_types.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_alloca.h"
|
2022-04-18 23:48:43 -05:00
|
|
|
#include "BLI_bitmap.h"
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
#include "BLI_math_vector.h"
|
|
|
|
|
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
#include "BKE_customdata.h"
|
2018-07-31 15:09:29 +02:00
|
|
|
#include "BKE_key.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BKE_mesh.h"
|
2018-09-04 15:32:01 +02:00
|
|
|
#include "BKE_subdiv.h"
|
2018-09-04 15:26:58 +02:00
|
|
|
#include "BKE_subdiv_eval.h"
|
2018-09-04 15:22:36 +02:00
|
|
|
#include "BKE_subdiv_foreach.h"
|
2022-06-10 10:29:35 +02:00
|
|
|
#include "BKE_subdiv_mesh.h"
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
|
2018-07-23 18:40:04 +02:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Subdivision Context
|
|
|
|
|
* \{ */
|
2018-07-23 18:40:04 +02:00
|
|
|
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
typedef struct SubdivMeshContext {
|
2018-12-03 16:59:11 +01:00
|
|
|
const SubdivToMeshSettings *settings;
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
const Mesh *coarse_mesh;
|
|
|
|
|
Subdiv *subdiv;
|
|
|
|
|
Mesh *subdiv_mesh;
|
2021-09-25 09:15:25 +02:00
|
|
|
/* Cached custom data arrays for faster access. */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
int *vert_origindex;
|
|
|
|
|
int *edge_origindex;
|
|
|
|
|
int *loop_origindex;
|
|
|
|
|
int *poly_origindex;
|
|
|
|
|
/* UV layers interpolation. */
|
|
|
|
|
int num_uv_layers;
|
|
|
|
|
MLoopUV *uv_layers[MAX_MTFACE];
|
2022-06-01 15:11:56 +10:00
|
|
|
/* Original coordinates (ORCO) interpolation. */
|
2021-01-14 16:33:52 +01:00
|
|
|
float (*orco)[3];
|
|
|
|
|
float (*cloth_orco)[3];
|
2019-01-23 12:14:34 +01:00
|
|
|
/* Per-subdivided vertex counter of averaged values. */
|
|
|
|
|
int *accumulated_counters;
|
|
|
|
|
bool have_displacement;
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
} SubdivMeshContext;
|
|
|
|
|
|
2018-07-23 18:40:04 +02:00
|
|
|
static void subdiv_mesh_ctx_cache_uv_layers(SubdivMeshContext *ctx)
|
|
|
|
|
{
|
|
|
|
|
Mesh *subdiv_mesh = ctx->subdiv_mesh;
|
|
|
|
|
ctx->num_uv_layers = CustomData_number_of_layers(&subdiv_mesh->ldata, CD_MLOOPUV);
|
2019-09-08 00:12:26 +10:00
|
|
|
for (int layer_index = 0; layer_index < ctx->num_uv_layers; layer_index++) {
|
2018-07-23 18:40:04 +02:00
|
|
|
ctx->uv_layers[layer_index] = CustomData_get_layer_n(
|
|
|
|
|
&subdiv_mesh->ldata, CD_MLOOPUV, layer_index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void subdiv_mesh_ctx_cache_custom_data_layers(SubdivMeshContext *ctx)
|
|
|
|
|
{
|
|
|
|
|
Mesh *subdiv_mesh = ctx->subdiv_mesh;
|
|
|
|
|
/* Pointers to original indices layers. */
|
|
|
|
|
ctx->vert_origindex = CustomData_get_layer(&subdiv_mesh->vdata, CD_ORIGINDEX);
|
|
|
|
|
ctx->edge_origindex = CustomData_get_layer(&subdiv_mesh->edata, CD_ORIGINDEX);
|
|
|
|
|
ctx->loop_origindex = CustomData_get_layer(&subdiv_mesh->ldata, CD_ORIGINDEX);
|
|
|
|
|
ctx->poly_origindex = CustomData_get_layer(&subdiv_mesh->pdata, CD_ORIGINDEX);
|
|
|
|
|
/* UV layers interpolation. */
|
|
|
|
|
subdiv_mesh_ctx_cache_uv_layers(ctx);
|
2021-01-14 16:33:52 +01:00
|
|
|
/* Orco interpolation. */
|
|
|
|
|
ctx->orco = CustomData_get_layer(&subdiv_mesh->vdata, CD_ORCO);
|
|
|
|
|
ctx->cloth_orco = CustomData_get_layer(&subdiv_mesh->vdata, CD_CLOTH_ORCO);
|
2018-07-23 18:40:04 +02:00
|
|
|
}
|
|
|
|
|
|
2019-01-23 12:14:34 +01:00
|
|
|
static void subdiv_mesh_prepare_accumulator(SubdivMeshContext *ctx, int num_vertices)
|
|
|
|
|
{
|
2022-02-18 21:34:41 +01:00
|
|
|
if (!ctx->have_displacement) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-01-23 12:14:34 +01:00
|
|
|
ctx->accumulated_counters = MEM_calloc_arrayN(
|
2021-10-14 10:17:33 +11:00
|
|
|
num_vertices, sizeof(*ctx->accumulated_counters), "subdiv accumulated counters");
|
2019-01-23 12:14:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void subdiv_mesh_context_free(SubdivMeshContext *ctx)
|
|
|
|
|
{
|
|
|
|
|
MEM_SAFE_FREE(ctx->accumulated_counters);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Loop custom data copy helpers
|
|
|
|
|
* \{ */
|
2018-07-23 18:40:04 +02:00
|
|
|
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
typedef struct LoopsOfPtex {
|
|
|
|
|
/* First loop of the ptex, starts at ptex (0, 0) and goes in u direction. */
|
|
|
|
|
const MLoop *first_loop;
|
|
|
|
|
/* Last loop of the ptex, starts at ptex (0, 0) and goes in v direction. */
|
|
|
|
|
const MLoop *last_loop;
|
|
|
|
|
/* For quad coarse faces only. */
|
|
|
|
|
const MLoop *second_loop;
|
|
|
|
|
const MLoop *third_loop;
|
|
|
|
|
} LoopsOfPtex;
|
|
|
|
|
|
|
|
|
|
static void loops_of_ptex_get(const SubdivMeshContext *ctx,
|
|
|
|
|
LoopsOfPtex *loops_of_ptex,
|
|
|
|
|
const MPoly *coarse_poly,
|
2018-07-25 16:51:48 +02:00
|
|
|
const int ptex_of_poly_index)
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
{
|
|
|
|
|
const MLoop *coarse_mloop = ctx->coarse_mesh->mloop;
|
2018-07-25 16:51:48 +02:00
|
|
|
const int first_ptex_loop_index = coarse_poly->loopstart + ptex_of_poly_index;
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
/* Loop which look in the (opposite) V direction of the current
|
|
|
|
|
* ptex face.
|
|
|
|
|
*
|
2019-01-23 12:14:34 +01:00
|
|
|
* TODO(sergey): Get rid of using module on every iteration. */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
const int last_ptex_loop_index = coarse_poly->loopstart +
|
2018-07-25 16:51:48 +02:00
|
|
|
(ptex_of_poly_index + coarse_poly->totloop - 1) %
|
|
|
|
|
coarse_poly->totloop;
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
loops_of_ptex->first_loop = &coarse_mloop[first_ptex_loop_index];
|
|
|
|
|
loops_of_ptex->last_loop = &coarse_mloop[last_ptex_loop_index];
|
|
|
|
|
if (coarse_poly->totloop == 4) {
|
|
|
|
|
loops_of_ptex->second_loop = loops_of_ptex->first_loop + 1;
|
|
|
|
|
loops_of_ptex->third_loop = loops_of_ptex->first_loop + 2;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
loops_of_ptex->second_loop = NULL;
|
|
|
|
|
loops_of_ptex->third_loop = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vertex custom data interpolation helpers
|
|
|
|
|
* \{ */
|
2018-07-23 18:40:04 +02:00
|
|
|
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
/* TODO(sergey): Somehow de-duplicate with loops storage, without too much
|
2019-01-23 12:14:34 +01:00
|
|
|
* exception cases all over the code. */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
|
|
|
|
|
typedef struct VerticesForInterpolation {
|
|
|
|
|
/* This field points to a vertex data which is to be used for interpolation.
|
|
|
|
|
* The idea is to avoid unnecessary allocations for regular faces, where
|
2019-06-17 12:51:53 +10:00
|
|
|
* we can simply use corner vertices. */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
const CustomData *vertex_data;
|
|
|
|
|
/* Vertices data calculated for ptex corners. There are always 4 elements
|
|
|
|
|
* in this custom data, aligned the following way:
|
|
|
|
|
*
|
|
|
|
|
* index 0 -> uv (0, 0)
|
|
|
|
|
* index 1 -> uv (0, 1)
|
|
|
|
|
* index 2 -> uv (1, 1)
|
|
|
|
|
* index 3 -> uv (1, 0)
|
|
|
|
|
*
|
2019-01-23 12:14:34 +01:00
|
|
|
* Is allocated for non-regular faces (triangles and n-gons). */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
CustomData vertex_data_storage;
|
|
|
|
|
bool vertex_data_storage_allocated;
|
2019-06-17 12:51:53 +10:00
|
|
|
/* Indices within vertex_data to interpolate for. The indices are aligned
|
2019-01-23 12:14:34 +01:00
|
|
|
* with uv coordinates in a similar way as indices in loop_data_storage. */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
int vertex_indices[4];
|
|
|
|
|
} VerticesForInterpolation;
|
|
|
|
|
|
|
|
|
|
static void vertex_interpolation_init(const SubdivMeshContext *ctx,
|
|
|
|
|
VerticesForInterpolation *vertex_interpolation,
|
2018-07-19 16:06:37 +10:00
|
|
|
const MPoly *coarse_poly)
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
{
|
|
|
|
|
const Mesh *coarse_mesh = ctx->coarse_mesh;
|
|
|
|
|
const MLoop *coarse_mloop = coarse_mesh->mloop;
|
|
|
|
|
if (coarse_poly->totloop == 4) {
|
|
|
|
|
vertex_interpolation->vertex_data = &coarse_mesh->vdata;
|
|
|
|
|
vertex_interpolation->vertex_indices[0] = coarse_mloop[coarse_poly->loopstart + 0].v;
|
|
|
|
|
vertex_interpolation->vertex_indices[1] = coarse_mloop[coarse_poly->loopstart + 1].v;
|
|
|
|
|
vertex_interpolation->vertex_indices[2] = coarse_mloop[coarse_poly->loopstart + 2].v;
|
|
|
|
|
vertex_interpolation->vertex_indices[3] = coarse_mloop[coarse_poly->loopstart + 3].v;
|
|
|
|
|
vertex_interpolation->vertex_data_storage_allocated = false;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
vertex_interpolation->vertex_data = &vertex_interpolation->vertex_data_storage;
|
|
|
|
|
/* Allocate storage for loops corresponding to ptex corners. */
|
|
|
|
|
CustomData_copy(&ctx->coarse_mesh->vdata,
|
|
|
|
|
&vertex_interpolation->vertex_data_storage,
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
CD_MASK_EVERYTHING.vmask,
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
CD_CALLOC,
|
|
|
|
|
4);
|
|
|
|
|
/* Initialize indices. */
|
|
|
|
|
vertex_interpolation->vertex_indices[0] = 0;
|
|
|
|
|
vertex_interpolation->vertex_indices[1] = 1;
|
|
|
|
|
vertex_interpolation->vertex_indices[2] = 2;
|
|
|
|
|
vertex_interpolation->vertex_indices[3] = 3;
|
|
|
|
|
vertex_interpolation->vertex_data_storage_allocated = true;
|
|
|
|
|
/* Interpolate center of poly right away, it stays unchanged for all
|
2019-01-23 12:14:34 +01:00
|
|
|
* ptex faces. */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
const float weight = 1.0f / (float)coarse_poly->totloop;
|
|
|
|
|
float *weights = BLI_array_alloca(weights, coarse_poly->totloop);
|
|
|
|
|
int *indices = BLI_array_alloca(indices, coarse_poly->totloop);
|
2019-09-08 00:12:26 +10:00
|
|
|
for (int i = 0; i < coarse_poly->totloop; i++) {
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
weights[i] = weight;
|
2018-07-23 19:09:16 +02:00
|
|
|
indices[i] = coarse_mloop[coarse_poly->loopstart + i].v;
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
}
|
|
|
|
|
CustomData_interp(&coarse_mesh->vdata,
|
|
|
|
|
&vertex_interpolation->vertex_data_storage,
|
|
|
|
|
indices,
|
|
|
|
|
weights,
|
|
|
|
|
NULL,
|
|
|
|
|
coarse_poly->totloop,
|
|
|
|
|
2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void vertex_interpolation_from_corner(const SubdivMeshContext *ctx,
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
VerticesForInterpolation *vertex_interpolation,
|
|
|
|
|
const MPoly *coarse_poly,
|
2018-08-20 12:46:44 +02:00
|
|
|
const int corner)
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
{
|
|
|
|
|
if (coarse_poly->totloop == 4) {
|
|
|
|
|
/* Nothing to do, all indices and data is already assigned. */
|
2018-07-19 16:06:37 +10:00
|
|
|
}
|
|
|
|
|
else {
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
const CustomData *vertex_data = &ctx->coarse_mesh->vdata;
|
|
|
|
|
const Mesh *coarse_mesh = ctx->coarse_mesh;
|
|
|
|
|
const MLoop *coarse_mloop = coarse_mesh->mloop;
|
|
|
|
|
LoopsOfPtex loops_of_ptex;
|
2018-08-20 12:46:44 +02:00
|
|
|
loops_of_ptex_get(ctx, &loops_of_ptex, coarse_poly, corner);
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
/* Ptex face corner corresponds to a poly loop with same index. */
|
|
|
|
|
CustomData_copy_data(vertex_data,
|
|
|
|
|
&vertex_interpolation->vertex_data_storage,
|
2018-08-20 12:46:44 +02:00
|
|
|
coarse_mloop[coarse_poly->loopstart + corner].v,
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
0,
|
|
|
|
|
1);
|
|
|
|
|
/* Interpolate remaining ptex face corners, which hits loops
|
|
|
|
|
* middle points.
|
|
|
|
|
*
|
|
|
|
|
* TODO(sergey): Re-use one of interpolation results from previous
|
2019-01-23 12:14:34 +01:00
|
|
|
* iteration. */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
const float weights[2] = {0.5f, 0.5f};
|
2018-07-23 19:09:16 +02:00
|
|
|
const int first_loop_index = loops_of_ptex.first_loop - coarse_mloop;
|
|
|
|
|
const int last_loop_index = loops_of_ptex.last_loop - coarse_mloop;
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
const int first_indices[2] = {
|
2018-07-23 19:09:16 +02:00
|
|
|
coarse_mloop[first_loop_index].v,
|
|
|
|
|
coarse_mloop[coarse_poly->loopstart +
|
|
|
|
|
(first_loop_index - coarse_poly->loopstart + 1) % coarse_poly->totloop]
|
|
|
|
|
.v};
|
2019-04-18 17:17:32 +02:00
|
|
|
const int last_indices[2] = {
|
|
|
|
|
coarse_mloop[first_loop_index].v,
|
|
|
|
|
coarse_mloop[last_loop_index].v,
|
|
|
|
|
};
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
CustomData_interp(vertex_data,
|
|
|
|
|
&vertex_interpolation->vertex_data_storage,
|
|
|
|
|
first_indices,
|
|
|
|
|
weights,
|
|
|
|
|
NULL,
|
|
|
|
|
2,
|
|
|
|
|
1);
|
|
|
|
|
CustomData_interp(vertex_data,
|
|
|
|
|
&vertex_interpolation->vertex_data_storage,
|
|
|
|
|
last_indices,
|
|
|
|
|
weights,
|
|
|
|
|
NULL,
|
|
|
|
|
2,
|
|
|
|
|
3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void vertex_interpolation_end(VerticesForInterpolation *vertex_interpolation)
|
|
|
|
|
{
|
|
|
|
|
if (vertex_interpolation->vertex_data_storage_allocated) {
|
|
|
|
|
CustomData_free(&vertex_interpolation->vertex_data_storage, 4);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Loop custom data interpolation helpers
|
|
|
|
|
* \{ */
|
2018-07-23 18:40:04 +02:00
|
|
|
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
typedef struct LoopsForInterpolation {
|
|
|
|
|
/* This field points to a loop data which is to be used for interpolation.
|
|
|
|
|
* The idea is to avoid unnecessary allocations for regular faces, where
|
2019-06-17 12:51:53 +10:00
|
|
|
* we can simply interpolate corner vertices. */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
const CustomData *loop_data;
|
|
|
|
|
/* Loops data calculated for ptex corners. There are always 4 elements
|
|
|
|
|
* in this custom data, aligned the following way:
|
|
|
|
|
*
|
|
|
|
|
* index 0 -> uv (0, 0)
|
|
|
|
|
* index 1 -> uv (0, 1)
|
|
|
|
|
* index 2 -> uv (1, 1)
|
|
|
|
|
* index 3 -> uv (1, 0)
|
|
|
|
|
*
|
2019-01-23 12:14:34 +01:00
|
|
|
* Is allocated for non-regular faces (triangles and n-gons). */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
CustomData loop_data_storage;
|
|
|
|
|
bool loop_data_storage_allocated;
|
|
|
|
|
/* Infices within loop_data to interpolate for. The indices are aligned with
|
2019-01-23 12:14:34 +01:00
|
|
|
* uv coordinates in a similar way as indices in loop_data_storage. */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
int loop_indices[4];
|
|
|
|
|
} LoopsForInterpolation;
|
|
|
|
|
|
|
|
|
|
static void loop_interpolation_init(const SubdivMeshContext *ctx,
|
|
|
|
|
LoopsForInterpolation *loop_interpolation,
|
2018-07-19 16:06:37 +10:00
|
|
|
const MPoly *coarse_poly)
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
{
|
|
|
|
|
const Mesh *coarse_mesh = ctx->coarse_mesh;
|
|
|
|
|
if (coarse_poly->totloop == 4) {
|
|
|
|
|
loop_interpolation->loop_data = &coarse_mesh->ldata;
|
|
|
|
|
loop_interpolation->loop_indices[0] = coarse_poly->loopstart + 0;
|
|
|
|
|
loop_interpolation->loop_indices[1] = coarse_poly->loopstart + 1;
|
|
|
|
|
loop_interpolation->loop_indices[2] = coarse_poly->loopstart + 2;
|
|
|
|
|
loop_interpolation->loop_indices[3] = coarse_poly->loopstart + 3;
|
|
|
|
|
loop_interpolation->loop_data_storage_allocated = false;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
loop_interpolation->loop_data = &loop_interpolation->loop_data_storage;
|
|
|
|
|
/* Allocate storage for loops corresponding to ptex corners. */
|
|
|
|
|
CustomData_copy(&ctx->coarse_mesh->ldata,
|
|
|
|
|
&loop_interpolation->loop_data_storage,
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
CD_MASK_EVERYTHING.lmask,
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
CD_CALLOC,
|
|
|
|
|
4);
|
|
|
|
|
/* Initialize indices. */
|
|
|
|
|
loop_interpolation->loop_indices[0] = 0;
|
|
|
|
|
loop_interpolation->loop_indices[1] = 1;
|
|
|
|
|
loop_interpolation->loop_indices[2] = 2;
|
|
|
|
|
loop_interpolation->loop_indices[3] = 3;
|
|
|
|
|
loop_interpolation->loop_data_storage_allocated = true;
|
|
|
|
|
/* Interpolate center of poly right away, it stays unchanged for all
|
2019-01-23 12:14:34 +01:00
|
|
|
* ptex faces. */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
const float weight = 1.0f / (float)coarse_poly->totloop;
|
|
|
|
|
float *weights = BLI_array_alloca(weights, coarse_poly->totloop);
|
|
|
|
|
int *indices = BLI_array_alloca(indices, coarse_poly->totloop);
|
2019-09-08 00:12:26 +10:00
|
|
|
for (int i = 0; i < coarse_poly->totloop; i++) {
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
weights[i] = weight;
|
|
|
|
|
indices[i] = coarse_poly->loopstart + i;
|
|
|
|
|
}
|
|
|
|
|
CustomData_interp(&coarse_mesh->ldata,
|
|
|
|
|
&loop_interpolation->loop_data_storage,
|
|
|
|
|
indices,
|
|
|
|
|
weights,
|
|
|
|
|
NULL,
|
|
|
|
|
coarse_poly->totloop,
|
|
|
|
|
2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void loop_interpolation_from_corner(const SubdivMeshContext *ctx,
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
LoopsForInterpolation *loop_interpolation,
|
|
|
|
|
const MPoly *coarse_poly,
|
2018-08-20 12:46:44 +02:00
|
|
|
const int corner)
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
{
|
|
|
|
|
if (coarse_poly->totloop == 4) {
|
|
|
|
|
/* Nothing to do, all indices and data is already assigned. */
|
2018-07-19 16:06:37 +10:00
|
|
|
}
|
|
|
|
|
else {
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
const CustomData *loop_data = &ctx->coarse_mesh->ldata;
|
|
|
|
|
const Mesh *coarse_mesh = ctx->coarse_mesh;
|
|
|
|
|
const MLoop *coarse_mloop = coarse_mesh->mloop;
|
|
|
|
|
LoopsOfPtex loops_of_ptex;
|
2018-08-20 12:46:44 +02:00
|
|
|
loops_of_ptex_get(ctx, &loops_of_ptex, coarse_poly, corner);
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
/* Ptex face corner corresponds to a poly loop with same index. */
|
2018-08-15 11:28:00 +02:00
|
|
|
CustomData_free_elem(&loop_interpolation->loop_data_storage, 0, 1);
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
CustomData_copy_data(
|
|
|
|
|
loop_data, &loop_interpolation->loop_data_storage, coarse_poly->loopstart + corner, 0, 1);
|
|
|
|
|
/* Interpolate remaining ptex face corners, which hits loops
|
|
|
|
|
* middle points.
|
|
|
|
|
*
|
|
|
|
|
* TODO(sergey): Re-use one of interpolation results from previous
|
2019-01-10 17:01:35 +01:00
|
|
|
* iteration. */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
const float weights[2] = {0.5f, 0.5f};
|
2019-01-10 17:01:35 +01:00
|
|
|
const int base_loop_index = coarse_poly->loopstart;
|
|
|
|
|
const int first_loop_index = loops_of_ptex.first_loop - coarse_mloop;
|
|
|
|
|
const int second_loop_index = base_loop_index +
|
|
|
|
|
(first_loop_index - base_loop_index + 1) % coarse_poly->totloop;
|
|
|
|
|
const int first_indices[2] = {first_loop_index, second_loop_index};
|
2019-04-18 17:17:32 +02:00
|
|
|
const int last_indices[2] = {
|
|
|
|
|
loops_of_ptex.last_loop - coarse_mloop,
|
|
|
|
|
loops_of_ptex.first_loop - coarse_mloop,
|
|
|
|
|
};
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
CustomData_interp(
|
|
|
|
|
loop_data, &loop_interpolation->loop_data_storage, first_indices, weights, NULL, 2, 1);
|
|
|
|
|
CustomData_interp(
|
|
|
|
|
loop_data, &loop_interpolation->loop_data_storage, last_indices, weights, NULL, 2, 3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void loop_interpolation_end(LoopsForInterpolation *loop_interpolation)
|
|
|
|
|
{
|
|
|
|
|
if (loop_interpolation->loop_data_storage_allocated) {
|
|
|
|
|
CustomData_free(&loop_interpolation->loop_data_storage, 4);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name TLS
|
|
|
|
|
* \{ */
|
2018-08-20 12:46:44 +02:00
|
|
|
|
|
|
|
|
typedef struct SubdivMeshTLS {
|
|
|
|
|
bool vertex_interpolation_initialized;
|
|
|
|
|
VerticesForInterpolation vertex_interpolation;
|
|
|
|
|
const MPoly *vertex_interpolation_coarse_poly;
|
|
|
|
|
int vertex_interpolation_coarse_corner;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
bool loop_interpolation_initialized;
|
|
|
|
|
LoopsForInterpolation loop_interpolation;
|
|
|
|
|
const MPoly *loop_interpolation_coarse_poly;
|
|
|
|
|
int loop_interpolation_coarse_corner;
|
|
|
|
|
} SubdivMeshTLS;
|
|
|
|
|
|
|
|
|
|
static void subdiv_mesh_tls_free(void *tls_v)
|
|
|
|
|
{
|
|
|
|
|
SubdivMeshTLS *tls = tls_v;
|
|
|
|
|
if (tls->vertex_interpolation_initialized) {
|
|
|
|
|
vertex_interpolation_end(&tls->vertex_interpolation);
|
|
|
|
|
}
|
|
|
|
|
if (tls->loop_interpolation_initialized) {
|
|
|
|
|
loop_interpolation_end(&tls->loop_interpolation);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
2021-01-14 16:33:52 +01:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Evaluation helper functions
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
static void subdiv_vertex_orco_evaluate(const SubdivMeshContext *ctx,
|
|
|
|
|
const int ptex_face_index,
|
|
|
|
|
const float u,
|
|
|
|
|
const float v,
|
|
|
|
|
const int subdiv_vertex_index)
|
|
|
|
|
{
|
|
|
|
|
if (ctx->orco || ctx->cloth_orco) {
|
|
|
|
|
float vertex_data[6];
|
|
|
|
|
BKE_subdiv_eval_vertex_data(ctx->subdiv, ptex_face_index, u, v, vertex_data);
|
|
|
|
|
|
|
|
|
|
if (ctx->orco) {
|
|
|
|
|
copy_v3_v3(ctx->orco[subdiv_vertex_index], vertex_data);
|
|
|
|
|
if (ctx->cloth_orco) {
|
|
|
|
|
copy_v3_v3(ctx->orco[subdiv_vertex_index], vertex_data + 3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (ctx->cloth_orco) {
|
|
|
|
|
copy_v3_v3(ctx->orco[subdiv_vertex_index], vertex_data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Accumulation helpers
|
|
|
|
|
* \{ */
|
2018-08-14 17:05:54 +02:00
|
|
|
|
Refactor: Move normals out of MVert, lazy calculation
As described in T91186, this commit moves mesh vertex normals into a
contiguous array of float vectors in a custom data layer, how face
normals are currently stored.
The main interface is documented in `BKE_mesh.h`. Vertex and face
normals are now calculated on-demand and cached, retrieved with an
"ensure" function. Since the logical state of a mesh is now "has
normals when necessary", they can be retrieved from a `const` mesh.
The goal is to use on-demand calculation for all derived data, but
leave room for eager calculation for performance purposes (modifier
evaluation is threaded, but viewport data generation is not).
**Benefits**
This moves us closer to a SoA approach rather than the current AoS
paradigm. Accessing a contiguous `float3` is much more efficient than
retrieving data from a larger struct. The memory requirements for
accessing only normals or vertex locations are smaller, and at the
cost of more memory usage for just normals, they now don't have to
be converted between float and short, which also simplifies code
In the future, the remaining items can be removed from `MVert`,
leaving only `float3`, which has similar benefits (see T93602).
Removing the combination of derived and original data makes it
conceptually simpler to only calculate normals when necessary.
This is especially important now that we have more opportunities
for temporary meshes in geometry nodes.
**Performance**
In addition to the theoretical future performance improvements by
making `MVert == float3`, I've done some basic performance testing
on this patch directly. The data is fairly rough, but it gives an idea
about where things stand generally.
- Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms),
showing that accessing just `MVert` is now more efficient.
- Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight
change that at least shows there is no regression.
- Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small
but observable speedup.
- Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms),
shows that using normals in geometry nodes is faster.
- Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms),
shows that calculating normals is slightly faster now.
- File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB),
Normals are not saved in files, which can help with large meshes.
As for memory usage, it may be slightly more in some cases, but
I didn't observe any difference in the production files I tested.
**Tests**
Some modifiers and cycles test results need to be updated with this
commit, for two reasons:
- The subdivision surface modifier is not responsible for calculating
normals anymore. In master, the modifier creates different normals
than the result of the `Mesh` normal calculation, so this is a bug
fix.
- There are small differences in the results of some modifiers that
use normals because they are not converted to and from `short`
anymore.
**Future improvements**
- Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier
already retrieves normals if they are needed anyway.
- Copy normals as part of a better CoW system for attributes.
- Make more areas use lazy instead of eager normal calculation.
- Remove `BKE_mesh_normals_tag_dirty` in more places since that is
now the default state of a new mesh.
- Possibly apply a similar change to derived face corner normals.
Differential Revision: https://developer.blender.org/D12770
2022-01-13 14:37:58 -06:00
|
|
|
static void subdiv_accumulate_vertex_displacement(SubdivMeshContext *ctx,
|
|
|
|
|
const int ptex_face_index,
|
|
|
|
|
const float u,
|
|
|
|
|
const float v,
|
|
|
|
|
MVert *subdiv_vert)
|
2018-08-14 17:05:54 +02:00
|
|
|
{
|
2022-02-18 21:34:41 +01:00
|
|
|
/* Accumulate displacement. */
|
2019-01-23 12:14:34 +01:00
|
|
|
Subdiv *subdiv = ctx->subdiv;
|
|
|
|
|
const int subdiv_vertex_index = subdiv_vert - ctx->subdiv_mesh->mvert;
|
2018-08-14 17:05:54 +02:00
|
|
|
float dummy_P[3], dPdu[3], dPdv[3], D[3];
|
|
|
|
|
BKE_subdiv_eval_limit_point_and_derivatives(subdiv, ptex_face_index, u, v, dummy_P, dPdu, dPdv);
|
Refactor: Move normals out of MVert, lazy calculation
As described in T91186, this commit moves mesh vertex normals into a
contiguous array of float vectors in a custom data layer, how face
normals are currently stored.
The main interface is documented in `BKE_mesh.h`. Vertex and face
normals are now calculated on-demand and cached, retrieved with an
"ensure" function. Since the logical state of a mesh is now "has
normals when necessary", they can be retrieved from a `const` mesh.
The goal is to use on-demand calculation for all derived data, but
leave room for eager calculation for performance purposes (modifier
evaluation is threaded, but viewport data generation is not).
**Benefits**
This moves us closer to a SoA approach rather than the current AoS
paradigm. Accessing a contiguous `float3` is much more efficient than
retrieving data from a larger struct. The memory requirements for
accessing only normals or vertex locations are smaller, and at the
cost of more memory usage for just normals, they now don't have to
be converted between float and short, which also simplifies code
In the future, the remaining items can be removed from `MVert`,
leaving only `float3`, which has similar benefits (see T93602).
Removing the combination of derived and original data makes it
conceptually simpler to only calculate normals when necessary.
This is especially important now that we have more opportunities
for temporary meshes in geometry nodes.
**Performance**
In addition to the theoretical future performance improvements by
making `MVert == float3`, I've done some basic performance testing
on this patch directly. The data is fairly rough, but it gives an idea
about where things stand generally.
- Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms),
showing that accessing just `MVert` is now more efficient.
- Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight
change that at least shows there is no regression.
- Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small
but observable speedup.
- Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms),
shows that using normals in geometry nodes is faster.
- Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms),
shows that calculating normals is slightly faster now.
- File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB),
Normals are not saved in files, which can help with large meshes.
As for memory usage, it may be slightly more in some cases, but
I didn't observe any difference in the production files I tested.
**Tests**
Some modifiers and cycles test results need to be updated with this
commit, for two reasons:
- The subdivision surface modifier is not responsible for calculating
normals anymore. In master, the modifier creates different normals
than the result of the `Mesh` normal calculation, so this is a bug
fix.
- There are small differences in the results of some modifiers that
use normals because they are not converted to and from `short`
anymore.
**Future improvements**
- Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier
already retrieves normals if they are needed anyway.
- Copy normals as part of a better CoW system for attributes.
- Make more areas use lazy instead of eager normal calculation.
- Remove `BKE_mesh_normals_tag_dirty` in more places since that is
now the default state of a new mesh.
- Possibly apply a similar change to derived face corner normals.
Differential Revision: https://developer.blender.org/D12770
2022-01-13 14:37:58 -06:00
|
|
|
|
2022-02-18 21:34:41 +01:00
|
|
|
/* NOTE: The subdivided mesh is allocated in this module, and its vertices are kept at zero
|
|
|
|
|
* locations as a default calloc(). */
|
|
|
|
|
BKE_subdiv_eval_displacement(subdiv, ptex_face_index, u, v, dPdu, dPdv, D);
|
|
|
|
|
add_v3_v3(subdiv_vert->co, D);
|
|
|
|
|
|
2020-10-05 16:20:06 +02:00
|
|
|
if (ctx->accumulated_counters) {
|
|
|
|
|
++ctx->accumulated_counters[subdiv_vertex_index];
|
|
|
|
|
}
|
2018-08-14 17:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Callbacks
|
|
|
|
|
* \{ */
|
2018-07-23 18:40:04 +02:00
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static bool subdiv_mesh_topology_info(const SubdivForeachContext *foreach_context,
|
|
|
|
|
const int num_vertices,
|
|
|
|
|
const int num_edges,
|
|
|
|
|
const int num_loops,
|
OpenSubDiv: add support for an OpenGL evaluator
This evaluator is used in order to evaluate subdivision at render time, allowing for
faster renders of meshes with a subdivision surface modifier placed at the last
position in the modifier list.
When evaluating the subsurf modifier, we detect whether we can delegate evaluation
to the draw code. If so, the subdivision is first evaluated on the GPU using our own
custom evaluator (only the coarse data needs to be initially sent to the GPU), then,
buffers for the final `MeshBufferCache` are filled on the GPU using a set of
compute shaders. However, some buffers are still filled on the CPU side, if doing so
on the GPU is impractical (e.g. the line adjacency buffer used for x-ray, whose
logic is hardly GPU compatible).
This is done at the mesh buffer extraction level so that the result can be readily used
in the various OpenGL engines, without having to write custom geometry or tesselation
shaders.
We use our own subdivision evaluation shaders, instead of OpenSubDiv's vanilla one, in
order to control the data layout, and interpolation. For example, we store vertex colors
as compressed 16-bit integers, while OpenSubDiv's default evaluator only work for float
types.
In order to still access the modified geometry on the CPU side, for use in modifiers
or transform operators, a dedicated wrapper type is added `MESH_WRAPPER_TYPE_SUBD`.
Subdivision will be lazily evaluated via `BKE_object_get_evaluated_mesh` which will
create such a wrapper if possible. If the final subdivision surface is not needed on
the CPU side, `BKE_object_get_evaluated_mesh_no_subsurf` should be used.
Enabling or disabling GPU subdivision can be done through the user preferences (under
Viewport -> Subdivision).
See patch description for benchmarks.
Reviewed By: campbellbarton, jbakker, fclem, brecht, #eevee_viewport
Differential Revision: https://developer.blender.org/D12406
2021-12-27 16:34:47 +01:00
|
|
|
const int num_polygons,
|
|
|
|
|
const int *UNUSED(subdiv_polygon_offset))
|
2018-08-20 12:46:44 +02:00
|
|
|
{
|
2022-01-06 13:54:52 +11:00
|
|
|
/* Multi-resolution grid data will be applied or become invalid after subdivision,
|
2019-08-27 18:17:27 +02:00
|
|
|
* so don't try to preserve it and use memory. */
|
|
|
|
|
CustomData_MeshMasks mask = CD_MASK_EVERYTHING;
|
|
|
|
|
mask.lmask &= ~CD_MASK_MULTIRES_GRIDS;
|
|
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
SubdivMeshContext *subdiv_context = foreach_context->user_data;
|
2019-08-27 18:17:27 +02:00
|
|
|
subdiv_context->subdiv_mesh = BKE_mesh_new_nomain_from_template_ex(
|
|
|
|
|
subdiv_context->coarse_mesh, num_vertices, num_edges, 0, num_loops, num_polygons, mask);
|
2018-08-20 12:46:44 +02:00
|
|
|
subdiv_mesh_ctx_cache_custom_data_layers(subdiv_context);
|
2019-01-23 12:14:34 +01:00
|
|
|
subdiv_mesh_prepare_accumulator(subdiv_context, num_vertices);
|
2022-04-18 23:48:43 -05:00
|
|
|
MEM_SAFE_FREE(subdiv_context->subdiv_mesh->runtime.subsurf_face_dot_tags);
|
|
|
|
|
subdiv_context->subdiv_mesh->runtime.subsurf_face_dot_tags = BLI_BITMAP_NEW(num_vertices,
|
|
|
|
|
__func__);
|
2018-08-20 12:46:44 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Vertex subdivision process
|
|
|
|
|
* \{ */
|
2018-07-25 16:51:48 +02:00
|
|
|
|
|
|
|
|
static void subdiv_vertex_data_copy(const SubdivMeshContext *ctx,
|
|
|
|
|
const MVert *coarse_vertex,
|
|
|
|
|
MVert *subdiv_vertex)
|
|
|
|
|
{
|
|
|
|
|
const Mesh *coarse_mesh = ctx->coarse_mesh;
|
|
|
|
|
Mesh *subdiv_mesh = ctx->subdiv_mesh;
|
|
|
|
|
const int coarse_vertex_index = coarse_vertex - coarse_mesh->mvert;
|
|
|
|
|
const int subdiv_vertex_index = subdiv_vertex - subdiv_mesh->mvert;
|
|
|
|
|
CustomData_copy_data(
|
|
|
|
|
&coarse_mesh->vdata, &ctx->subdiv_mesh->vdata, coarse_vertex_index, subdiv_vertex_index, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void subdiv_vertex_data_interpolate(const SubdivMeshContext *ctx,
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
MVert *subdiv_vertex,
|
|
|
|
|
const VerticesForInterpolation *vertex_interpolation,
|
|
|
|
|
const float u,
|
|
|
|
|
const float v)
|
|
|
|
|
{
|
|
|
|
|
const int subdiv_vertex_index = subdiv_vertex - ctx->subdiv_mesh->mvert;
|
|
|
|
|
const float weights[4] = {(1.0f - u) * (1.0f - v), u * (1.0f - v), u * v, (1.0f - u) * v};
|
|
|
|
|
CustomData_interp(vertex_interpolation->vertex_data,
|
|
|
|
|
&ctx->subdiv_mesh->vdata,
|
|
|
|
|
vertex_interpolation->vertex_indices,
|
|
|
|
|
weights,
|
|
|
|
|
NULL,
|
|
|
|
|
4,
|
|
|
|
|
subdiv_vertex_index);
|
2018-07-18 17:34:44 +02:00
|
|
|
if (ctx->vert_origindex != NULL) {
|
|
|
|
|
ctx->vert_origindex[subdiv_vertex_index] = ORIGINDEX_NONE;
|
2018-07-25 16:51:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void evaluate_vertex_and_apply_displacement_copy(const SubdivMeshContext *ctx,
|
|
|
|
|
const int ptex_face_index,
|
|
|
|
|
const float u,
|
|
|
|
|
const float v,
|
|
|
|
|
const MVert *coarse_vert,
|
2018-08-24 10:26:59 +10:00
|
|
|
MVert *subdiv_vert)
|
2018-07-25 16:51:48 +02:00
|
|
|
{
|
2019-01-23 12:14:34 +01:00
|
|
|
const int subdiv_vertex_index = subdiv_vert - ctx->subdiv_mesh->mvert;
|
2018-08-20 12:46:44 +02:00
|
|
|
/* Displacement is accumulated in subdiv vertex position.
|
2019-02-04 01:23:48 +01:00
|
|
|
* Needs to be backed up before copying data from original vertex. */
|
2019-01-23 12:14:34 +01:00
|
|
|
float D[3] = {0.0f, 0.0f, 0.0f};
|
|
|
|
|
if (ctx->have_displacement) {
|
2020-10-05 16:20:06 +02:00
|
|
|
const float inv_num_accumulated = 1.0f / ctx->accumulated_counters[subdiv_vertex_index];
|
2019-01-23 12:14:34 +01:00
|
|
|
copy_v3_v3(D, subdiv_vert->co);
|
|
|
|
|
mul_v3_fl(D, inv_num_accumulated);
|
|
|
|
|
}
|
|
|
|
|
/* Copy custom data and evaluate position. */
|
2018-08-20 12:46:44 +02:00
|
|
|
subdiv_vertex_data_copy(ctx, coarse_vert, subdiv_vert);
|
2019-01-23 12:14:34 +01:00
|
|
|
BKE_subdiv_eval_limit_point(ctx->subdiv, ptex_face_index, u, v, subdiv_vert->co);
|
2018-08-20 12:46:44 +02:00
|
|
|
/* Apply displacement. */
|
|
|
|
|
add_v3_v3(subdiv_vert->co, D);
|
2021-01-14 16:33:52 +01:00
|
|
|
/* Evaluate undeformed texture coordinate. */
|
|
|
|
|
subdiv_vertex_orco_evaluate(ctx, ptex_face_index, u, v, subdiv_vertex_index);
|
2019-07-07 18:58:11 +02:00
|
|
|
/* Remove facedot flag. This can happen if there is more than one subsurf modifier. */
|
2022-04-18 23:48:43 -05:00
|
|
|
BLI_BITMAP_DISABLE(ctx->subdiv_mesh->runtime.subsurf_face_dot_tags, subdiv_vertex_index);
|
2018-07-25 16:51:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void evaluate_vertex_and_apply_displacement_interpolate(
|
|
|
|
|
const SubdivMeshContext *ctx,
|
|
|
|
|
const int ptex_face_index,
|
|
|
|
|
const float u,
|
|
|
|
|
const float v,
|
|
|
|
|
VerticesForInterpolation *vertex_interpolation,
|
2018-08-24 10:26:59 +10:00
|
|
|
MVert *subdiv_vert)
|
2018-08-14 17:05:54 +02:00
|
|
|
{
|
2019-01-23 12:14:34 +01:00
|
|
|
const int subdiv_vertex_index = subdiv_vert - ctx->subdiv_mesh->mvert;
|
2018-08-20 12:46:44 +02:00
|
|
|
/* Displacement is accumulated in subdiv vertex position.
|
2019-02-04 01:23:48 +01:00
|
|
|
* Needs to be backed up before copying data from original vertex. */
|
2019-01-23 12:14:34 +01:00
|
|
|
float D[3] = {0.0f, 0.0f, 0.0f};
|
|
|
|
|
if (ctx->have_displacement) {
|
2020-10-05 16:20:06 +02:00
|
|
|
const float inv_num_accumulated = 1.0f / ctx->accumulated_counters[subdiv_vertex_index];
|
2019-01-23 12:14:34 +01:00
|
|
|
copy_v3_v3(D, subdiv_vert->co);
|
|
|
|
|
mul_v3_fl(D, inv_num_accumulated);
|
|
|
|
|
}
|
|
|
|
|
/* Interpolate custom data and evaluate position. */
|
|
|
|
|
subdiv_vertex_data_interpolate(ctx, subdiv_vert, vertex_interpolation, u, v);
|
|
|
|
|
BKE_subdiv_eval_limit_point(ctx->subdiv, ptex_face_index, u, v, subdiv_vert->co);
|
2018-08-20 12:46:44 +02:00
|
|
|
/* Apply displacement. */
|
|
|
|
|
add_v3_v3(subdiv_vert->co, D);
|
2021-01-14 16:33:52 +01:00
|
|
|
/* Evaluate undeformed texture coordinate. */
|
|
|
|
|
subdiv_vertex_orco_evaluate(ctx, ptex_face_index, u, v, subdiv_vertex_index);
|
2018-08-14 17:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-18 21:34:41 +01:00
|
|
|
static void subdiv_mesh_vertex_displacement_every_corner_or_edge(
|
|
|
|
|
const SubdivForeachContext *foreach_context,
|
|
|
|
|
void *UNUSED(tls),
|
|
|
|
|
const int ptex_face_index,
|
|
|
|
|
const float u,
|
|
|
|
|
const float v,
|
|
|
|
|
const int subdiv_vertex_index)
|
2018-08-14 17:05:54 +02:00
|
|
|
{
|
2018-08-20 12:46:44 +02:00
|
|
|
SubdivMeshContext *ctx = foreach_context->user_data;
|
2018-08-14 17:05:54 +02:00
|
|
|
Mesh *subdiv_mesh = ctx->subdiv_mesh;
|
|
|
|
|
MVert *subdiv_mvert = subdiv_mesh->mvert;
|
2018-08-20 12:46:44 +02:00
|
|
|
MVert *subdiv_vert = &subdiv_mvert[subdiv_vertex_index];
|
Refactor: Move normals out of MVert, lazy calculation
As described in T91186, this commit moves mesh vertex normals into a
contiguous array of float vectors in a custom data layer, how face
normals are currently stored.
The main interface is documented in `BKE_mesh.h`. Vertex and face
normals are now calculated on-demand and cached, retrieved with an
"ensure" function. Since the logical state of a mesh is now "has
normals when necessary", they can be retrieved from a `const` mesh.
The goal is to use on-demand calculation for all derived data, but
leave room for eager calculation for performance purposes (modifier
evaluation is threaded, but viewport data generation is not).
**Benefits**
This moves us closer to a SoA approach rather than the current AoS
paradigm. Accessing a contiguous `float3` is much more efficient than
retrieving data from a larger struct. The memory requirements for
accessing only normals or vertex locations are smaller, and at the
cost of more memory usage for just normals, they now don't have to
be converted between float and short, which also simplifies code
In the future, the remaining items can be removed from `MVert`,
leaving only `float3`, which has similar benefits (see T93602).
Removing the combination of derived and original data makes it
conceptually simpler to only calculate normals when necessary.
This is especially important now that we have more opportunities
for temporary meshes in geometry nodes.
**Performance**
In addition to the theoretical future performance improvements by
making `MVert == float3`, I've done some basic performance testing
on this patch directly. The data is fairly rough, but it gives an idea
about where things stand generally.
- Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms),
showing that accessing just `MVert` is now more efficient.
- Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight
change that at least shows there is no regression.
- Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small
but observable speedup.
- Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms),
shows that using normals in geometry nodes is faster.
- Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms),
shows that calculating normals is slightly faster now.
- File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB),
Normals are not saved in files, which can help with large meshes.
As for memory usage, it may be slightly more in some cases, but
I didn't observe any difference in the production files I tested.
**Tests**
Some modifiers and cycles test results need to be updated with this
commit, for two reasons:
- The subdivision surface modifier is not responsible for calculating
normals anymore. In master, the modifier creates different normals
than the result of the `Mesh` normal calculation, so this is a bug
fix.
- There are small differences in the results of some modifiers that
use normals because they are not converted to and from `short`
anymore.
**Future improvements**
- Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier
already retrieves normals if they are needed anyway.
- Copy normals as part of a better CoW system for attributes.
- Make more areas use lazy instead of eager normal calculation.
- Remove `BKE_mesh_normals_tag_dirty` in more places since that is
now the default state of a new mesh.
- Possibly apply a similar change to derived face corner normals.
Differential Revision: https://developer.blender.org/D12770
2022-01-13 14:37:58 -06:00
|
|
|
subdiv_accumulate_vertex_displacement(ctx, ptex_face_index, u, v, subdiv_vert);
|
2018-08-14 17:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-18 21:34:41 +01:00
|
|
|
static void subdiv_mesh_vertex_displacement_every_corner(
|
|
|
|
|
const SubdivForeachContext *foreach_context,
|
|
|
|
|
void *tls,
|
|
|
|
|
const int ptex_face_index,
|
|
|
|
|
const float u,
|
|
|
|
|
const float v,
|
|
|
|
|
const int UNUSED(coarse_vertex_index),
|
|
|
|
|
const int UNUSED(coarse_poly_index),
|
|
|
|
|
const int UNUSED(coarse_corner),
|
|
|
|
|
const int subdiv_vertex_index)
|
2018-08-14 17:05:54 +02:00
|
|
|
{
|
2022-02-18 21:34:41 +01:00
|
|
|
subdiv_mesh_vertex_displacement_every_corner_or_edge(
|
2018-08-20 12:46:44 +02:00
|
|
|
foreach_context, tls, ptex_face_index, u, v, subdiv_vertex_index);
|
2018-08-14 17:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-18 21:34:41 +01:00
|
|
|
static void subdiv_mesh_vertex_displacement_every_edge(const SubdivForeachContext *foreach_context,
|
|
|
|
|
void *tls,
|
|
|
|
|
const int ptex_face_index,
|
|
|
|
|
const float u,
|
|
|
|
|
const float v,
|
|
|
|
|
const int UNUSED(coarse_edge_index),
|
|
|
|
|
const int UNUSED(coarse_poly_index),
|
|
|
|
|
const int UNUSED(coarse_corner),
|
|
|
|
|
const int subdiv_vertex_index)
|
2018-07-25 16:51:48 +02:00
|
|
|
{
|
2022-02-18 21:34:41 +01:00
|
|
|
subdiv_mesh_vertex_displacement_every_corner_or_edge(
|
2018-08-20 12:46:44 +02:00
|
|
|
foreach_context, tls, ptex_face_index, u, v, subdiv_vertex_index);
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void subdiv_mesh_vertex_corner(const SubdivForeachContext *foreach_context,
|
|
|
|
|
void *UNUSED(tls),
|
|
|
|
|
const int ptex_face_index,
|
|
|
|
|
const float u,
|
|
|
|
|
const float v,
|
|
|
|
|
const int coarse_vertex_index,
|
|
|
|
|
const int UNUSED(coarse_poly_index),
|
|
|
|
|
const int UNUSED(coarse_corner),
|
|
|
|
|
const int subdiv_vertex_index)
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
{
|
2018-08-20 12:46:44 +02:00
|
|
|
BLI_assert(coarse_vertex_index != ORIGINDEX_NONE);
|
|
|
|
|
SubdivMeshContext *ctx = foreach_context->user_data;
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
const Mesh *coarse_mesh = ctx->coarse_mesh;
|
2018-08-20 12:46:44 +02:00
|
|
|
const MVert *coarse_mvert = coarse_mesh->mvert;
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
Mesh *subdiv_mesh = ctx->subdiv_mesh;
|
2018-07-25 16:51:48 +02:00
|
|
|
MVert *subdiv_mvert = subdiv_mesh->mvert;
|
2018-08-20 12:46:44 +02:00
|
|
|
const MVert *coarse_vert = &coarse_mvert[coarse_vertex_index];
|
|
|
|
|
MVert *subdiv_vert = &subdiv_mvert[subdiv_vertex_index];
|
|
|
|
|
evaluate_vertex_and_apply_displacement_copy(
|
|
|
|
|
ctx, ptex_face_index, u, v, coarse_vert, subdiv_vert);
|
2018-07-25 16:51:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void subdiv_mesh_ensure_vertex_interpolation(SubdivMeshContext *ctx,
|
|
|
|
|
SubdivMeshTLS *tls,
|
2018-07-25 16:51:48 +02:00
|
|
|
const MPoly *coarse_poly,
|
2018-08-20 12:46:44 +02:00
|
|
|
const int coarse_corner)
|
2018-07-25 16:51:48 +02:00
|
|
|
{
|
2018-08-20 12:46:44 +02:00
|
|
|
/* Check whether we've moved to another corner or polygon. */
|
|
|
|
|
if (tls->vertex_interpolation_initialized) {
|
|
|
|
|
if (tls->vertex_interpolation_coarse_poly != coarse_poly ||
|
|
|
|
|
tls->vertex_interpolation_coarse_corner != coarse_corner) {
|
|
|
|
|
vertex_interpolation_end(&tls->vertex_interpolation);
|
|
|
|
|
tls->vertex_interpolation_initialized = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Initialize the interpolation. */
|
|
|
|
|
if (!tls->vertex_interpolation_initialized) {
|
|
|
|
|
vertex_interpolation_init(ctx, &tls->vertex_interpolation, coarse_poly);
|
|
|
|
|
}
|
|
|
|
|
/* Update it for a new corner if needed. */
|
|
|
|
|
if (!tls->vertex_interpolation_initialized ||
|
2018-08-24 10:26:59 +10:00
|
|
|
tls->vertex_interpolation_coarse_corner != coarse_corner) {
|
2018-08-20 12:46:44 +02:00
|
|
|
vertex_interpolation_from_corner(ctx, &tls->vertex_interpolation, coarse_poly, coarse_corner);
|
2018-08-24 10:26:59 +10:00
|
|
|
}
|
2018-08-20 12:46:44 +02:00
|
|
|
/* Store settings used for the current state of interpolator. */
|
|
|
|
|
tls->vertex_interpolation_initialized = true;
|
|
|
|
|
tls->vertex_interpolation_coarse_poly = coarse_poly;
|
|
|
|
|
tls->vertex_interpolation_coarse_corner = coarse_corner;
|
2018-08-14 17:05:54 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void subdiv_mesh_vertex_edge(const SubdivForeachContext *foreach_context,
|
|
|
|
|
void *tls_v,
|
|
|
|
|
const int ptex_face_index,
|
|
|
|
|
const float u,
|
|
|
|
|
const float v,
|
|
|
|
|
const int UNUSED(coarse_edge_index),
|
|
|
|
|
const int coarse_poly_index,
|
|
|
|
|
const int coarse_corner,
|
|
|
|
|
const int subdiv_vertex_index)
|
2018-08-14 17:05:54 +02:00
|
|
|
{
|
2018-08-20 12:46:44 +02:00
|
|
|
SubdivMeshContext *ctx = foreach_context->user_data;
|
|
|
|
|
SubdivMeshTLS *tls = tls_v;
|
2018-08-14 17:05:54 +02:00
|
|
|
const Mesh *coarse_mesh = ctx->coarse_mesh;
|
|
|
|
|
const MPoly *coarse_mpoly = coarse_mesh->mpoly;
|
2018-08-20 12:46:44 +02:00
|
|
|
const MPoly *coarse_poly = &coarse_mpoly[coarse_poly_index];
|
2018-07-25 16:51:48 +02:00
|
|
|
Mesh *subdiv_mesh = ctx->subdiv_mesh;
|
|
|
|
|
MVert *subdiv_mvert = subdiv_mesh->mvert;
|
2018-08-20 12:46:44 +02:00
|
|
|
MVert *subdiv_vert = &subdiv_mvert[subdiv_vertex_index];
|
|
|
|
|
subdiv_mesh_ensure_vertex_interpolation(ctx, tls, coarse_poly, coarse_corner);
|
|
|
|
|
evaluate_vertex_and_apply_displacement_interpolate(
|
|
|
|
|
ctx, ptex_face_index, u, v, &tls->vertex_interpolation, subdiv_vert);
|
2018-07-25 16:51:48 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-07 18:58:11 +02:00
|
|
|
static bool subdiv_mesh_is_center_vertex(const MPoly *coarse_poly, const float u, const float v)
|
|
|
|
|
{
|
|
|
|
|
if (coarse_poly->totloop == 4) {
|
|
|
|
|
if (u == 0.5f && v == 0.5f) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (u == 1.0f && v == 1.0f) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void subdiv_mesh_tag_center_vertex(const MPoly *coarse_poly,
|
2022-04-18 23:48:43 -05:00
|
|
|
const int subdiv_vertex_index,
|
2019-07-07 18:58:11 +02:00
|
|
|
const float u,
|
2022-04-18 23:48:43 -05:00
|
|
|
const float v,
|
|
|
|
|
Mesh *subdiv_mesh)
|
2019-07-07 18:58:11 +02:00
|
|
|
{
|
|
|
|
|
if (subdiv_mesh_is_center_vertex(coarse_poly, u, v)) {
|
2022-04-18 23:48:43 -05:00
|
|
|
BLI_BITMAP_ENABLE(subdiv_mesh->runtime.subsurf_face_dot_tags, subdiv_vertex_index);
|
2019-07-07 18:58:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void subdiv_mesh_vertex_inner(const SubdivForeachContext *foreach_context,
|
|
|
|
|
void *tls_v,
|
|
|
|
|
const int ptex_face_index,
|
|
|
|
|
const float u,
|
|
|
|
|
const float v,
|
|
|
|
|
const int coarse_poly_index,
|
|
|
|
|
const int coarse_corner,
|
|
|
|
|
const int subdiv_vertex_index)
|
2018-07-25 16:51:48 +02:00
|
|
|
{
|
2018-08-20 12:46:44 +02:00
|
|
|
SubdivMeshContext *ctx = foreach_context->user_data;
|
|
|
|
|
SubdivMeshTLS *tls = tls_v;
|
2018-07-25 16:51:48 +02:00
|
|
|
Subdiv *subdiv = ctx->subdiv;
|
|
|
|
|
const Mesh *coarse_mesh = ctx->coarse_mesh;
|
2018-08-20 12:46:44 +02:00
|
|
|
const MPoly *coarse_mpoly = coarse_mesh->mpoly;
|
|
|
|
|
const MPoly *coarse_poly = &coarse_mpoly[coarse_poly_index];
|
2018-07-25 16:51:48 +02:00
|
|
|
Mesh *subdiv_mesh = ctx->subdiv_mesh;
|
|
|
|
|
MVert *subdiv_mvert = subdiv_mesh->mvert;
|
2018-08-20 12:46:44 +02:00
|
|
|
MVert *subdiv_vert = &subdiv_mvert[subdiv_vertex_index];
|
|
|
|
|
subdiv_mesh_ensure_vertex_interpolation(ctx, tls, coarse_poly, coarse_corner);
|
|
|
|
|
subdiv_vertex_data_interpolate(ctx, subdiv_vert, &tls->vertex_interpolation, u, v);
|
Refactor: Move normals out of MVert, lazy calculation
As described in T91186, this commit moves mesh vertex normals into a
contiguous array of float vectors in a custom data layer, how face
normals are currently stored.
The main interface is documented in `BKE_mesh.h`. Vertex and face
normals are now calculated on-demand and cached, retrieved with an
"ensure" function. Since the logical state of a mesh is now "has
normals when necessary", they can be retrieved from a `const` mesh.
The goal is to use on-demand calculation for all derived data, but
leave room for eager calculation for performance purposes (modifier
evaluation is threaded, but viewport data generation is not).
**Benefits**
This moves us closer to a SoA approach rather than the current AoS
paradigm. Accessing a contiguous `float3` is much more efficient than
retrieving data from a larger struct. The memory requirements for
accessing only normals or vertex locations are smaller, and at the
cost of more memory usage for just normals, they now don't have to
be converted between float and short, which also simplifies code
In the future, the remaining items can be removed from `MVert`,
leaving only `float3`, which has similar benefits (see T93602).
Removing the combination of derived and original data makes it
conceptually simpler to only calculate normals when necessary.
This is especially important now that we have more opportunities
for temporary meshes in geometry nodes.
**Performance**
In addition to the theoretical future performance improvements by
making `MVert == float3`, I've done some basic performance testing
on this patch directly. The data is fairly rough, but it gives an idea
about where things stand generally.
- Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms),
showing that accessing just `MVert` is now more efficient.
- Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight
change that at least shows there is no regression.
- Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small
but observable speedup.
- Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms),
shows that using normals in geometry nodes is faster.
- Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms),
shows that calculating normals is slightly faster now.
- File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB),
Normals are not saved in files, which can help with large meshes.
As for memory usage, it may be slightly more in some cases, but
I didn't observe any difference in the production files I tested.
**Tests**
Some modifiers and cycles test results need to be updated with this
commit, for two reasons:
- The subdivision surface modifier is not responsible for calculating
normals anymore. In master, the modifier creates different normals
than the result of the `Mesh` normal calculation, so this is a bug
fix.
- There are small differences in the results of some modifiers that
use normals because they are not converted to and from `short`
anymore.
**Future improvements**
- Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier
already retrieves normals if they are needed anyway.
- Copy normals as part of a better CoW system for attributes.
- Make more areas use lazy instead of eager normal calculation.
- Remove `BKE_mesh_normals_tag_dirty` in more places since that is
now the default state of a new mesh.
- Possibly apply a similar change to derived face corner normals.
Differential Revision: https://developer.blender.org/D12770
2022-01-13 14:37:58 -06:00
|
|
|
BKE_subdiv_eval_final_point(subdiv, ptex_face_index, u, v, subdiv_vert->co);
|
2022-04-18 23:48:43 -05:00
|
|
|
subdiv_mesh_tag_center_vertex(coarse_poly, subdiv_vertex_index, u, v, subdiv_mesh);
|
2021-01-14 16:33:52 +01:00
|
|
|
subdiv_vertex_orco_evaluate(ctx, ptex_face_index, u, v, subdiv_vertex_index);
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Edge subdivision process
|
|
|
|
|
* \{ */
|
2018-07-23 18:40:04 +02:00
|
|
|
|
2018-07-19 16:06:37 +10:00
|
|
|
static void subdiv_copy_edge_data(SubdivMeshContext *ctx,
|
|
|
|
|
MEdge *subdiv_edge,
|
|
|
|
|
const MEdge *coarse_edge)
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
{
|
2018-07-18 17:49:12 +02:00
|
|
|
const int subdiv_edge_index = subdiv_edge - ctx->subdiv_mesh->medge;
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
if (coarse_edge == NULL) {
|
|
|
|
|
subdiv_edge->crease = 0;
|
|
|
|
|
subdiv_edge->bweight = 0;
|
|
|
|
|
subdiv_edge->flag = 0;
|
2018-12-03 16:59:11 +01:00
|
|
|
if (!ctx->settings->use_optimal_display) {
|
|
|
|
|
subdiv_edge->flag |= ME_EDGERENDER;
|
|
|
|
|
}
|
2018-07-18 17:49:12 +02:00
|
|
|
if (ctx->edge_origindex != NULL) {
|
|
|
|
|
ctx->edge_origindex[subdiv_edge_index] = ORIGINDEX_NONE;
|
|
|
|
|
}
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const int coarse_edge_index = coarse_edge - ctx->coarse_mesh->medge;
|
|
|
|
|
CustomData_copy_data(
|
|
|
|
|
&ctx->coarse_mesh->edata, &ctx->subdiv_mesh->edata, coarse_edge_index, subdiv_edge_index, 1);
|
2018-12-03 16:59:11 +01:00
|
|
|
subdiv_edge->flag |= ME_EDGERENDER;
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void subdiv_mesh_edge(const SubdivForeachContext *foreach_context,
|
|
|
|
|
void *UNUSED(tls),
|
|
|
|
|
const int coarse_edge_index,
|
|
|
|
|
const int subdiv_edge_index,
|
2022-02-10 15:51:19 +01:00
|
|
|
const bool UNUSED(is_loose),
|
2018-08-20 12:46:44 +02:00
|
|
|
const int subdiv_v1,
|
|
|
|
|
const int subdiv_v2)
|
2018-07-25 16:51:48 +02:00
|
|
|
{
|
2018-08-20 12:46:44 +02:00
|
|
|
SubdivMeshContext *ctx = foreach_context->user_data;
|
2018-07-25 16:51:48 +02:00
|
|
|
Mesh *subdiv_mesh = ctx->subdiv_mesh;
|
|
|
|
|
MEdge *subdiv_medge = subdiv_mesh->medge;
|
2018-08-20 12:46:44 +02:00
|
|
|
MEdge *subdiv_edge = &subdiv_medge[subdiv_edge_index];
|
2018-11-28 20:13:14 +01:00
|
|
|
const MEdge *coarse_edge = NULL;
|
2018-08-20 12:46:44 +02:00
|
|
|
if (coarse_edge_index != ORIGINDEX_NONE) {
|
|
|
|
|
const Mesh *coarse_mesh = ctx->coarse_mesh;
|
|
|
|
|
const MEdge *coarse_medge = coarse_mesh->medge;
|
2018-11-28 20:13:14 +01:00
|
|
|
coarse_edge = &coarse_medge[coarse_edge_index];
|
2018-07-25 16:51:48 +02:00
|
|
|
}
|
2018-11-28 20:13:14 +01:00
|
|
|
subdiv_copy_edge_data(ctx, subdiv_edge, coarse_edge);
|
2018-08-20 12:46:44 +02:00
|
|
|
subdiv_edge->v1 = subdiv_v1;
|
|
|
|
|
subdiv_edge->v2 = subdiv_v2;
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Loops creation/interpolation
|
|
|
|
|
* \{ */
|
2018-07-23 18:40:04 +02:00
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void subdiv_interpolate_loop_data(const SubdivMeshContext *ctx,
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
MLoop *subdiv_loop,
|
|
|
|
|
const LoopsForInterpolation *loop_interpolation,
|
|
|
|
|
const float u,
|
|
|
|
|
const float v)
|
|
|
|
|
{
|
|
|
|
|
const int subdiv_loop_index = subdiv_loop - ctx->subdiv_mesh->mloop;
|
|
|
|
|
const float weights[4] = {(1.0f - u) * (1.0f - v), u * (1.0f - v), u * v, (1.0f - u) * v};
|
|
|
|
|
CustomData_interp(loop_interpolation->loop_data,
|
|
|
|
|
&ctx->subdiv_mesh->ldata,
|
|
|
|
|
loop_interpolation->loop_indices,
|
|
|
|
|
weights,
|
|
|
|
|
NULL,
|
|
|
|
|
4,
|
|
|
|
|
subdiv_loop_index);
|
|
|
|
|
/* TODO(sergey): Set ORIGINDEX. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void subdiv_eval_uv_layer(SubdivMeshContext *ctx,
|
|
|
|
|
MLoop *subdiv_loop,
|
|
|
|
|
const int ptex_face_index,
|
2018-08-20 12:46:44 +02:00
|
|
|
const float u,
|
|
|
|
|
const float v)
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
{
|
|
|
|
|
if (ctx->num_uv_layers == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Subdiv *subdiv = ctx->subdiv;
|
|
|
|
|
const int mloop_index = subdiv_loop - ctx->subdiv_mesh->mloop;
|
|
|
|
|
for (int layer_index = 0; layer_index < ctx->num_uv_layers; layer_index++) {
|
|
|
|
|
MLoopUV *subdiv_loopuv = &ctx->uv_layers[layer_index][mloop_index];
|
|
|
|
|
BKE_subdiv_eval_face_varying(subdiv, layer_index, ptex_face_index, u, v, subdiv_loopuv->uv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void subdiv_mesh_ensure_loop_interpolation(SubdivMeshContext *ctx,
|
|
|
|
|
SubdivMeshTLS *tls,
|
|
|
|
|
const MPoly *coarse_poly,
|
|
|
|
|
const int coarse_corner)
|
2018-07-25 16:51:48 +02:00
|
|
|
{
|
2018-08-20 12:46:44 +02:00
|
|
|
/* Check whether we've moved to another corner or polygon. */
|
|
|
|
|
if (tls->loop_interpolation_initialized) {
|
|
|
|
|
if (tls->loop_interpolation_coarse_poly != coarse_poly ||
|
|
|
|
|
tls->loop_interpolation_coarse_corner != coarse_corner) {
|
|
|
|
|
loop_interpolation_end(&tls->loop_interpolation);
|
|
|
|
|
tls->loop_interpolation_initialized = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Initialize the interpolation. */
|
|
|
|
|
if (!tls->loop_interpolation_initialized) {
|
|
|
|
|
loop_interpolation_init(ctx, &tls->loop_interpolation, coarse_poly);
|
|
|
|
|
}
|
|
|
|
|
/* Update it for a new corner if needed. */
|
|
|
|
|
if (!tls->loop_interpolation_initialized ||
|
2018-08-24 10:26:59 +10:00
|
|
|
tls->loop_interpolation_coarse_corner != coarse_corner) {
|
2018-08-20 12:46:44 +02:00
|
|
|
loop_interpolation_from_corner(ctx, &tls->loop_interpolation, coarse_poly, coarse_corner);
|
2018-08-24 10:26:59 +10:00
|
|
|
}
|
2018-08-20 12:46:44 +02:00
|
|
|
/* Store settings used for the current state of interpolator. */
|
|
|
|
|
tls->loop_interpolation_initialized = true;
|
|
|
|
|
tls->loop_interpolation_coarse_poly = coarse_poly;
|
|
|
|
|
tls->loop_interpolation_coarse_corner = coarse_corner;
|
2018-07-25 16:51:48 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void subdiv_mesh_loop(const SubdivForeachContext *foreach_context,
|
|
|
|
|
void *tls_v,
|
2018-07-25 16:51:48 +02:00
|
|
|
const int ptex_face_index,
|
|
|
|
|
const float u,
|
|
|
|
|
const float v,
|
2018-08-20 12:46:44 +02:00
|
|
|
const int UNUSED(coarse_loop_index),
|
|
|
|
|
const int coarse_poly_index,
|
2018-08-24 10:26:59 +10:00
|
|
|
const int coarse_corner,
|
2018-08-20 12:46:44 +02:00
|
|
|
const int subdiv_loop_index,
|
|
|
|
|
const int subdiv_vertex_index,
|
|
|
|
|
const int subdiv_edge_index)
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
{
|
2018-08-20 12:46:44 +02:00
|
|
|
SubdivMeshContext *ctx = foreach_context->user_data;
|
|
|
|
|
SubdivMeshTLS *tls = tls_v;
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
const Mesh *coarse_mesh = ctx->coarse_mesh;
|
2018-07-23 18:40:04 +02:00
|
|
|
const MPoly *coarse_mpoly = coarse_mesh->mpoly;
|
2018-08-20 12:46:44 +02:00
|
|
|
const MPoly *coarse_poly = &coarse_mpoly[coarse_poly_index];
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
Mesh *subdiv_mesh = ctx->subdiv_mesh;
|
2018-08-20 12:46:44 +02:00
|
|
|
MLoop *subdiv_mloop = subdiv_mesh->mloop;
|
|
|
|
|
MLoop *subdiv_loop = &subdiv_mloop[subdiv_loop_index];
|
|
|
|
|
subdiv_mesh_ensure_loop_interpolation(ctx, tls, coarse_poly, coarse_corner);
|
|
|
|
|
subdiv_interpolate_loop_data(ctx, subdiv_loop, &tls->loop_interpolation, u, v);
|
|
|
|
|
subdiv_eval_uv_layer(ctx, subdiv_loop, ptex_face_index, u, v);
|
|
|
|
|
subdiv_loop->v = subdiv_vertex_index;
|
|
|
|
|
subdiv_loop->e = subdiv_edge_index;
|
2018-07-25 16:51:48 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Polygons subdivision process
|
|
|
|
|
* \{ */
|
2018-07-23 18:40:04 +02:00
|
|
|
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
static void subdiv_copy_poly_data(const SubdivMeshContext *ctx,
|
|
|
|
|
MPoly *subdiv_poly,
|
|
|
|
|
const MPoly *coarse_poly)
|
|
|
|
|
{
|
|
|
|
|
const int coarse_poly_index = coarse_poly - ctx->coarse_mesh->mpoly;
|
|
|
|
|
const int subdiv_poly_index = subdiv_poly - ctx->subdiv_mesh->mpoly;
|
|
|
|
|
CustomData_copy_data(
|
|
|
|
|
&ctx->coarse_mesh->pdata, &ctx->subdiv_mesh->pdata, coarse_poly_index, subdiv_poly_index, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void subdiv_mesh_poly(const SubdivForeachContext *foreach_context,
|
|
|
|
|
void *UNUSED(tls),
|
|
|
|
|
const int coarse_poly_index,
|
|
|
|
|
const int subdiv_poly_index,
|
|
|
|
|
const int start_loop_index,
|
|
|
|
|
const int num_loops)
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
{
|
2018-08-20 12:46:44 +02:00
|
|
|
BLI_assert(coarse_poly_index != ORIGINDEX_NONE);
|
|
|
|
|
SubdivMeshContext *ctx = foreach_context->user_data;
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
const Mesh *coarse_mesh = ctx->coarse_mesh;
|
2018-07-23 18:40:04 +02:00
|
|
|
const MPoly *coarse_mpoly = coarse_mesh->mpoly;
|
2018-08-20 12:46:44 +02:00
|
|
|
const MPoly *coarse_poly = &coarse_mpoly[coarse_poly_index];
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
Mesh *subdiv_mesh = ctx->subdiv_mesh;
|
|
|
|
|
MPoly *subdiv_mpoly = subdiv_mesh->mpoly;
|
2018-08-20 12:46:44 +02:00
|
|
|
MPoly *subdiv_poly = &subdiv_mpoly[subdiv_poly_index];
|
|
|
|
|
subdiv_copy_poly_data(ctx, subdiv_poly, coarse_poly);
|
|
|
|
|
subdiv_poly->loopstart = start_loop_index;
|
|
|
|
|
subdiv_poly->totloop = num_loops;
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Loose elements subdivision process
|
|
|
|
|
* \{ */
|
2018-07-31 15:09:29 +02:00
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void subdiv_mesh_vertex_loose(const SubdivForeachContext *foreach_context,
|
|
|
|
|
void *UNUSED(tls),
|
|
|
|
|
const int coarse_vertex_index,
|
|
|
|
|
const int subdiv_vertex_index)
|
2018-07-31 15:09:29 +02:00
|
|
|
{
|
2018-08-20 12:46:44 +02:00
|
|
|
SubdivMeshContext *ctx = foreach_context->user_data;
|
2018-07-31 15:09:29 +02:00
|
|
|
const Mesh *coarse_mesh = ctx->coarse_mesh;
|
|
|
|
|
const MVert *coarse_mvert = coarse_mesh->mvert;
|
2018-08-20 12:46:44 +02:00
|
|
|
const MVert *coarse_vertex = &coarse_mvert[coarse_vertex_index];
|
2018-07-31 15:09:29 +02:00
|
|
|
Mesh *subdiv_mesh = ctx->subdiv_mesh;
|
|
|
|
|
MVert *subdiv_mvert = subdiv_mesh->mvert;
|
2018-08-20 12:46:44 +02:00
|
|
|
MVert *subdiv_vertex = &subdiv_mvert[subdiv_vertex_index];
|
2018-07-31 15:09:29 +02:00
|
|
|
subdiv_vertex_data_copy(ctx, coarse_vertex, subdiv_vertex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Get neighbor edges of the given one.
|
|
|
|
|
* - neighbors[0] is an edge adjacent to edge->v1.
|
2019-01-23 12:14:34 +01:00
|
|
|
* - neighbors[1] is an edge adjacent to edge->v2. */
|
2022-03-02 15:10:26 +01:00
|
|
|
static void find_edge_neighbors(const Mesh *coarse_mesh,
|
2018-07-31 15:09:29 +02:00
|
|
|
const MEdge *edge,
|
|
|
|
|
const MEdge *neighbors[2])
|
|
|
|
|
{
|
|
|
|
|
const MEdge *coarse_medge = coarse_mesh->medge;
|
|
|
|
|
neighbors[0] = NULL;
|
|
|
|
|
neighbors[1] = NULL;
|
2019-01-24 12:05:32 +01:00
|
|
|
int neighbor_counters[2] = {0, 0};
|
2018-07-31 15:09:29 +02:00
|
|
|
for (int edge_index = 0; edge_index < coarse_mesh->totedge; edge_index++) {
|
|
|
|
|
const MEdge *current_edge = &coarse_medge[edge_index];
|
|
|
|
|
if (current_edge == edge) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (ELEM(edge->v1, current_edge->v1, current_edge->v2)) {
|
|
|
|
|
neighbors[0] = current_edge;
|
2019-01-24 12:05:32 +01:00
|
|
|
++neighbor_counters[0];
|
2018-07-31 15:09:29 +02:00
|
|
|
}
|
|
|
|
|
if (ELEM(edge->v2, current_edge->v1, current_edge->v2)) {
|
|
|
|
|
neighbors[1] = current_edge;
|
2019-01-24 12:05:32 +01:00
|
|
|
++neighbor_counters[1];
|
2018-07-31 15:09:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-24 12:05:32 +01:00
|
|
|
/* Vertices which has more than one neighbor are considered infinitely
|
|
|
|
|
* sharp. This is also how topology factory treats vertices of a surface
|
|
|
|
|
* which are adjacent to a loose edge. */
|
|
|
|
|
if (neighbor_counters[0] > 1) {
|
|
|
|
|
neighbors[0] = NULL;
|
|
|
|
|
}
|
|
|
|
|
if (neighbor_counters[1] > 1) {
|
|
|
|
|
neighbors[1] = NULL;
|
|
|
|
|
}
|
2018-07-31 15:09:29 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-02 15:10:26 +01:00
|
|
|
static void points_for_loose_edges_interpolation_get(const Mesh *coarse_mesh,
|
2018-07-31 15:09:29 +02:00
|
|
|
const MEdge *coarse_edge,
|
|
|
|
|
const MEdge *neighbors[2],
|
2018-08-08 10:59:50 +10:00
|
|
|
float points_r[4][3])
|
2018-07-31 15:09:29 +02:00
|
|
|
{
|
|
|
|
|
const MVert *coarse_mvert = coarse_mesh->mvert;
|
|
|
|
|
/* Middle points corresponds to the edge. */
|
|
|
|
|
copy_v3_v3(points_r[1], coarse_mvert[coarse_edge->v1].co);
|
|
|
|
|
copy_v3_v3(points_r[2], coarse_mvert[coarse_edge->v2].co);
|
|
|
|
|
/* Start point, duplicate from edge start if no neighbor. */
|
|
|
|
|
if (neighbors[0] != NULL) {
|
|
|
|
|
if (neighbors[0]->v1 == coarse_edge->v1) {
|
|
|
|
|
copy_v3_v3(points_r[0], coarse_mvert[neighbors[0]->v2].co);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
copy_v3_v3(points_r[0], coarse_mvert[neighbors[0]->v1].co);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
sub_v3_v3v3(points_r[0], points_r[1], points_r[2]);
|
|
|
|
|
add_v3_v3(points_r[0], points_r[1]);
|
|
|
|
|
}
|
|
|
|
|
/* End point, duplicate from edge end if no neighbor. */
|
|
|
|
|
if (neighbors[1] != NULL) {
|
|
|
|
|
if (neighbors[1]->v1 == coarse_edge->v2) {
|
|
|
|
|
copy_v3_v3(points_r[3], coarse_mvert[neighbors[1]->v2].co);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
copy_v3_v3(points_r[3], coarse_mvert[neighbors[1]->v1].co);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
sub_v3_v3v3(points_r[3], points_r[2], points_r[1]);
|
|
|
|
|
add_v3_v3(points_r[3], points_r[2]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-02 15:10:26 +01:00
|
|
|
void BKE_subdiv_mesh_interpolate_position_on_edge(const Mesh *coarse_mesh,
|
|
|
|
|
const MEdge *coarse_edge,
|
|
|
|
|
const bool is_simple,
|
|
|
|
|
const float u,
|
|
|
|
|
float pos_r[3])
|
|
|
|
|
{
|
|
|
|
|
if (is_simple) {
|
|
|
|
|
const MVert *coarse_mvert = coarse_mesh->mvert;
|
|
|
|
|
const MVert *vert_1 = &coarse_mvert[coarse_edge->v1];
|
|
|
|
|
const MVert *vert_2 = &coarse_mvert[coarse_edge->v2];
|
|
|
|
|
interp_v3_v3v3(pos_r, vert_1->co, vert_2->co, u);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* Find neighbors of the coarse edge. */
|
|
|
|
|
const MEdge *neighbors[2];
|
|
|
|
|
find_edge_neighbors(coarse_mesh, coarse_edge, neighbors);
|
|
|
|
|
float points[4][3];
|
|
|
|
|
points_for_loose_edges_interpolation_get(coarse_mesh, coarse_edge, neighbors, points);
|
|
|
|
|
float weights[4];
|
|
|
|
|
key_curve_position_weights(u, weights, KEY_BSPLINE);
|
|
|
|
|
interp_v3_v3v3v3v3(pos_r, points[0], points[1], points[2], points[3], weights);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-12 15:06:39 +01:00
|
|
|
static void subdiv_mesh_vertex_of_loose_edge_interpolate(SubdivMeshContext *ctx,
|
|
|
|
|
const MEdge *coarse_edge,
|
|
|
|
|
const float u,
|
|
|
|
|
const int subdiv_vertex_index)
|
|
|
|
|
{
|
|
|
|
|
const Mesh *coarse_mesh = ctx->coarse_mesh;
|
|
|
|
|
Mesh *subdiv_mesh = ctx->subdiv_mesh;
|
2021-11-09 00:20:51 +11:00
|
|
|
/* This is never used for end-points (which are copied from the original). */
|
|
|
|
|
BLI_assert(u > 0.0f);
|
|
|
|
|
BLI_assert(u < 1.0f);
|
|
|
|
|
const float interpolation_weights[2] = {1.0f - u, u};
|
|
|
|
|
const int coarse_vertex_indices[2] = {coarse_edge->v1, coarse_edge->v2};
|
|
|
|
|
CustomData_interp(&coarse_mesh->vdata,
|
|
|
|
|
&subdiv_mesh->vdata,
|
|
|
|
|
coarse_vertex_indices,
|
|
|
|
|
interpolation_weights,
|
|
|
|
|
NULL,
|
|
|
|
|
2,
|
|
|
|
|
subdiv_vertex_index);
|
|
|
|
|
if (ctx->vert_origindex != NULL) {
|
|
|
|
|
ctx->vert_origindex[subdiv_vertex_index] = ORIGINDEX_NONE;
|
2018-12-12 15:06:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-20 12:46:44 +02:00
|
|
|
static void subdiv_mesh_vertex_of_loose_edge(const struct SubdivForeachContext *foreach_context,
|
|
|
|
|
void *UNUSED(tls),
|
|
|
|
|
const int coarse_edge_index,
|
|
|
|
|
const float u,
|
|
|
|
|
const int subdiv_vertex_index)
|
2018-07-31 15:09:29 +02:00
|
|
|
{
|
2018-08-20 12:46:44 +02:00
|
|
|
SubdivMeshContext *ctx = foreach_context->user_data;
|
2018-07-31 15:09:29 +02:00
|
|
|
const Mesh *coarse_mesh = ctx->coarse_mesh;
|
2018-08-20 12:46:44 +02:00
|
|
|
const MEdge *coarse_edge = &coarse_mesh->medge[coarse_edge_index];
|
2018-07-31 15:09:29 +02:00
|
|
|
Mesh *subdiv_mesh = ctx->subdiv_mesh;
|
|
|
|
|
MVert *subdiv_mvert = subdiv_mesh->mvert;
|
2019-07-25 16:33:04 +02:00
|
|
|
const bool is_simple = ctx->subdiv->settings.is_simple;
|
2021-11-09 00:20:51 +11:00
|
|
|
/* Interpolate custom data when not an end point.
|
|
|
|
|
* This data has already been copied from the original vertex by #subdiv_mesh_vertex_loose. */
|
2022-01-07 15:30:14 +11:00
|
|
|
if (!ELEM(u, 0.0, 1.0)) {
|
2021-11-09 00:20:51 +11:00
|
|
|
subdiv_mesh_vertex_of_loose_edge_interpolate(ctx, coarse_edge, u, subdiv_vertex_index);
|
|
|
|
|
}
|
2019-07-25 16:33:04 +02:00
|
|
|
/* Interpolate coordinate. */
|
2018-08-20 12:46:44 +02:00
|
|
|
MVert *subdiv_vertex = &subdiv_mvert[subdiv_vertex_index];
|
2022-03-02 15:10:26 +01:00
|
|
|
BKE_subdiv_mesh_interpolate_position_on_edge(
|
|
|
|
|
coarse_mesh, coarse_edge, is_simple, u, subdiv_vertex->co);
|
2018-08-20 12:46:44 +02:00
|
|
|
/* Reset flags and such. */
|
|
|
|
|
subdiv_vertex->flag = 0;
|
2018-12-12 15:06:39 +01:00
|
|
|
/* TODO(sergey): This matches old behavior, but we can as well interpolate
|
|
|
|
|
* it. Maybe even using vertex varying attributes. */
|
2018-08-20 12:46:44 +02:00
|
|
|
subdiv_vertex->bweight = 0.0f;
|
2018-07-31 15:09:29 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Initialization
|
|
|
|
|
* \{ */
|
2018-07-23 18:40:04 +02:00
|
|
|
|
2019-01-23 12:14:34 +01:00
|
|
|
static void setup_foreach_callbacks(const SubdivMeshContext *subdiv_context,
|
|
|
|
|
SubdivForeachContext *foreach_context)
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
{
|
2018-08-20 12:46:44 +02:00
|
|
|
memset(foreach_context, 0, sizeof(*foreach_context));
|
2018-09-24 18:46:51 +02:00
|
|
|
/* General information. */
|
2018-08-20 12:46:44 +02:00
|
|
|
foreach_context->topology_info = subdiv_mesh_topology_info;
|
Refactor: Move normals out of MVert, lazy calculation
As described in T91186, this commit moves mesh vertex normals into a
contiguous array of float vectors in a custom data layer, how face
normals are currently stored.
The main interface is documented in `BKE_mesh.h`. Vertex and face
normals are now calculated on-demand and cached, retrieved with an
"ensure" function. Since the logical state of a mesh is now "has
normals when necessary", they can be retrieved from a `const` mesh.
The goal is to use on-demand calculation for all derived data, but
leave room for eager calculation for performance purposes (modifier
evaluation is threaded, but viewport data generation is not).
**Benefits**
This moves us closer to a SoA approach rather than the current AoS
paradigm. Accessing a contiguous `float3` is much more efficient than
retrieving data from a larger struct. The memory requirements for
accessing only normals or vertex locations are smaller, and at the
cost of more memory usage for just normals, they now don't have to
be converted between float and short, which also simplifies code
In the future, the remaining items can be removed from `MVert`,
leaving only `float3`, which has similar benefits (see T93602).
Removing the combination of derived and original data makes it
conceptually simpler to only calculate normals when necessary.
This is especially important now that we have more opportunities
for temporary meshes in geometry nodes.
**Performance**
In addition to the theoretical future performance improvements by
making `MVert == float3`, I've done some basic performance testing
on this patch directly. The data is fairly rough, but it gives an idea
about where things stand generally.
- Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms),
showing that accessing just `MVert` is now more efficient.
- Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight
change that at least shows there is no regression.
- Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small
but observable speedup.
- Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms),
shows that using normals in geometry nodes is faster.
- Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms),
shows that calculating normals is slightly faster now.
- File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB),
Normals are not saved in files, which can help with large meshes.
As for memory usage, it may be slightly more in some cases, but
I didn't observe any difference in the production files I tested.
**Tests**
Some modifiers and cycles test results need to be updated with this
commit, for two reasons:
- The subdivision surface modifier is not responsible for calculating
normals anymore. In master, the modifier creates different normals
than the result of the `Mesh` normal calculation, so this is a bug
fix.
- There are small differences in the results of some modifiers that
use normals because they are not converted to and from `short`
anymore.
**Future improvements**
- Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier
already retrieves normals if they are needed anyway.
- Copy normals as part of a better CoW system for attributes.
- Make more areas use lazy instead of eager normal calculation.
- Remove `BKE_mesh_normals_tag_dirty` in more places since that is
now the default state of a new mesh.
- Possibly apply a similar change to derived face corner normals.
Differential Revision: https://developer.blender.org/D12770
2022-01-13 14:37:58 -06:00
|
|
|
/* Every boundary geometry. Used for displacement averaging. */
|
|
|
|
|
if (subdiv_context->have_displacement) {
|
2022-02-18 21:34:41 +01:00
|
|
|
foreach_context->vertex_every_corner = subdiv_mesh_vertex_displacement_every_corner;
|
|
|
|
|
foreach_context->vertex_every_edge = subdiv_mesh_vertex_displacement_every_edge;
|
2018-08-20 12:46:44 +02:00
|
|
|
}
|
|
|
|
|
foreach_context->vertex_corner = subdiv_mesh_vertex_corner;
|
|
|
|
|
foreach_context->vertex_edge = subdiv_mesh_vertex_edge;
|
|
|
|
|
foreach_context->vertex_inner = subdiv_mesh_vertex_inner;
|
|
|
|
|
foreach_context->edge = subdiv_mesh_edge;
|
|
|
|
|
foreach_context->loop = subdiv_mesh_loop;
|
|
|
|
|
foreach_context->poly = subdiv_mesh_poly;
|
|
|
|
|
foreach_context->vertex_loose = subdiv_mesh_vertex_loose;
|
|
|
|
|
foreach_context->vertex_of_loose_edge = subdiv_mesh_vertex_of_loose_edge;
|
|
|
|
|
foreach_context->user_data_tls_free = subdiv_mesh_tls_free;
|
2018-07-25 16:51:48 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:51:21 +10:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Public entry point
|
|
|
|
|
* \{ */
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
|
|
|
|
|
Mesh *BKE_subdiv_to_mesh(Subdiv *subdiv,
|
|
|
|
|
const SubdivToMeshSettings *settings,
|
|
|
|
|
const Mesh *coarse_mesh)
|
|
|
|
|
{
|
2018-07-19 16:27:18 +02:00
|
|
|
BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_SUBDIV_TO_MESH);
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
/* Make sure evaluator is up to date with possible new topology, and that
|
2019-11-25 00:55:11 +11:00
|
|
|
* it is refined for the new positions of coarse vertices. */
|
OpenSubDiv: add support for an OpenGL evaluator
This evaluator is used in order to evaluate subdivision at render time, allowing for
faster renders of meshes with a subdivision surface modifier placed at the last
position in the modifier list.
When evaluating the subsurf modifier, we detect whether we can delegate evaluation
to the draw code. If so, the subdivision is first evaluated on the GPU using our own
custom evaluator (only the coarse data needs to be initially sent to the GPU), then,
buffers for the final `MeshBufferCache` are filled on the GPU using a set of
compute shaders. However, some buffers are still filled on the CPU side, if doing so
on the GPU is impractical (e.g. the line adjacency buffer used for x-ray, whose
logic is hardly GPU compatible).
This is done at the mesh buffer extraction level so that the result can be readily used
in the various OpenGL engines, without having to write custom geometry or tesselation
shaders.
We use our own subdivision evaluation shaders, instead of OpenSubDiv's vanilla one, in
order to control the data layout, and interpolation. For example, we store vertex colors
as compressed 16-bit integers, while OpenSubDiv's default evaluator only work for float
types.
In order to still access the modified geometry on the CPU side, for use in modifiers
or transform operators, a dedicated wrapper type is added `MESH_WRAPPER_TYPE_SUBD`.
Subdivision will be lazily evaluated via `BKE_object_get_evaluated_mesh` which will
create such a wrapper if possible. If the final subdivision surface is not needed on
the CPU side, `BKE_object_get_evaluated_mesh_no_subsurf` should be used.
Enabling or disabling GPU subdivision can be done through the user preferences (under
Viewport -> Subdivision).
See patch description for benchmarks.
Reviewed By: campbellbarton, jbakker, fclem, brecht, #eevee_viewport
Differential Revision: https://developer.blender.org/D12406
2021-12-27 16:34:47 +01:00
|
|
|
if (!BKE_subdiv_eval_begin_from_mesh(
|
|
|
|
|
subdiv, coarse_mesh, NULL, SUBDIV_EVALUATOR_TYPE_CPU, NULL)) {
|
2018-08-13 12:21:29 +02:00
|
|
|
/* This could happen in two situations:
|
|
|
|
|
* - OpenSubdiv is disabled.
|
|
|
|
|
* - Something totally bad happened, and OpenSubdiv rejected our
|
|
|
|
|
* topology.
|
2019-01-23 12:14:34 +01:00
|
|
|
* In either way, we can't safely continue. */
|
2018-08-13 12:21:29 +02:00
|
|
|
if (coarse_mesh->totpoly) {
|
|
|
|
|
BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_SUBDIV_TO_MESH);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2020-08-12 17:21:34 +02:00
|
|
|
/* Initialize subdivision mesh creation context. */
|
2018-08-20 12:46:44 +02:00
|
|
|
SubdivMeshContext subdiv_context = {0};
|
2018-12-03 16:59:11 +01:00
|
|
|
subdiv_context.settings = settings;
|
2018-08-20 12:46:44 +02:00
|
|
|
subdiv_context.coarse_mesh = coarse_mesh;
|
|
|
|
|
subdiv_context.subdiv = subdiv;
|
2019-01-23 12:14:34 +01:00
|
|
|
subdiv_context.have_displacement = (subdiv->displacement_evaluator != NULL);
|
2018-08-20 12:46:44 +02:00
|
|
|
/* Multi-threaded traversal/evaluation. */
|
2018-07-25 16:51:48 +02:00
|
|
|
BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_SUBDIV_TO_MESH_GEOMETRY);
|
2018-08-20 12:46:44 +02:00
|
|
|
SubdivForeachContext foreach_context;
|
2019-01-23 12:14:34 +01:00
|
|
|
setup_foreach_callbacks(&subdiv_context, &foreach_context);
|
2018-08-22 17:01:29 +02:00
|
|
|
SubdivMeshTLS tls = {0};
|
2018-08-20 12:46:44 +02:00
|
|
|
foreach_context.user_data = &subdiv_context;
|
|
|
|
|
foreach_context.user_data_tls_size = sizeof(SubdivMeshTLS);
|
|
|
|
|
foreach_context.user_data_tls = &tls;
|
2019-01-23 12:14:34 +01:00
|
|
|
BKE_subdiv_foreach_subdiv_geometry(subdiv, &foreach_context, settings, coarse_mesh);
|
2018-07-25 16:51:48 +02:00
|
|
|
BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_SUBDIV_TO_MESH_GEOMETRY);
|
2018-08-20 12:46:44 +02:00
|
|
|
Mesh *result = subdiv_context.subdiv_mesh;
|
2018-07-23 18:40:04 +02:00
|
|
|
// BKE_mesh_validate(result, true, true);
|
2018-07-19 16:27:18 +02:00
|
|
|
BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_SUBDIV_TO_MESH);
|
Refactor: Move normals out of MVert, lazy calculation
As described in T91186, this commit moves mesh vertex normals into a
contiguous array of float vectors in a custom data layer, how face
normals are currently stored.
The main interface is documented in `BKE_mesh.h`. Vertex and face
normals are now calculated on-demand and cached, retrieved with an
"ensure" function. Since the logical state of a mesh is now "has
normals when necessary", they can be retrieved from a `const` mesh.
The goal is to use on-demand calculation for all derived data, but
leave room for eager calculation for performance purposes (modifier
evaluation is threaded, but viewport data generation is not).
**Benefits**
This moves us closer to a SoA approach rather than the current AoS
paradigm. Accessing a contiguous `float3` is much more efficient than
retrieving data from a larger struct. The memory requirements for
accessing only normals or vertex locations are smaller, and at the
cost of more memory usage for just normals, they now don't have to
be converted between float and short, which also simplifies code
In the future, the remaining items can be removed from `MVert`,
leaving only `float3`, which has similar benefits (see T93602).
Removing the combination of derived and original data makes it
conceptually simpler to only calculate normals when necessary.
This is especially important now that we have more opportunities
for temporary meshes in geometry nodes.
**Performance**
In addition to the theoretical future performance improvements by
making `MVert == float3`, I've done some basic performance testing
on this patch directly. The data is fairly rough, but it gives an idea
about where things stand generally.
- Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms),
showing that accessing just `MVert` is now more efficient.
- Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight
change that at least shows there is no regression.
- Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small
but observable speedup.
- Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms),
shows that using normals in geometry nodes is faster.
- Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms),
shows that calculating normals is slightly faster now.
- File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB),
Normals are not saved in files, which can help with large meshes.
As for memory usage, it may be slightly more in some cases, but
I didn't observe any difference in the production files I tested.
**Tests**
Some modifiers and cycles test results need to be updated with this
commit, for two reasons:
- The subdivision surface modifier is not responsible for calculating
normals anymore. In master, the modifier creates different normals
than the result of the `Mesh` normal calculation, so this is a bug
fix.
- There are small differences in the results of some modifiers that
use normals because they are not converted to and from `short`
anymore.
**Future improvements**
- Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier
already retrieves normals if they are needed anyway.
- Copy normals as part of a better CoW system for attributes.
- Make more areas use lazy instead of eager normal calculation.
- Remove `BKE_mesh_normals_tag_dirty` in more places since that is
now the default state of a new mesh.
- Possibly apply a similar change to derived face corner normals.
Differential Revision: https://developer.blender.org/D12770
2022-01-13 14:37:58 -06:00
|
|
|
/* Using normals from the limit surface gives different results than Blender's vertex normal
|
|
|
|
|
* calculation. Since vertex normals are supposed to be a consistent cache, don't bother
|
|
|
|
|
* calculating them here. The work may have been pointless anyway if the mesh is deformed or
|
|
|
|
|
* changed afterwards. */
|
2022-04-19 17:08:02 -05:00
|
|
|
BLI_assert(BKE_mesh_vertex_normals_are_dirty(result) || BKE_mesh_poly_normals_are_dirty(result));
|
2019-09-11 11:08:30 +02:00
|
|
|
/* Free used memory. */
|
2019-01-23 12:14:34 +01:00
|
|
|
subdiv_mesh_context_free(&subdiv_context);
|
Subsurf: Begin new subdivision surface module
The idea is to use this as a replacement of old CCG, now it is
based on OpenSubdiv. The goal is to reduce any possible overhead
which was happening with OpenSubdiv used by CCG.
Currently implemented/supported:
- Creation from mesh, including topology on OpenSubdiv side,
its refinement.
- Evaluation of limit point, first order derivatives, normal,
and face-varying data for individual coarse position.
- Evaluation of whole patches.
Currently not optimized, uses evaluation of individual coarse
positions.
- Creation of Mesh from subdiv, with all geometry being real:
all mvert, medge, mloop, and mpoly.
This includes custom data interpolation, but all faces currently
are getting separated (they are converted to ptex patches, which
we need to weld back together).
Still need to support lighter weights grids and such, but this
is already a required part to have subsurf working in the middle
of modifier stack.
Annoying part is ifdef all over the place, to keep it compilable
when OpenSubdiv is disabled. More cleaner approach would be to
have stub API for OpenSubdiv, so everything gets ifdef-ed in a
much fewer places.
2018-07-17 18:07:26 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
2020-04-29 11:51:21 +10:00
|
|
|
|
|
|
|
|
/** \} */
|