Blender V5.0
subdiv.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_subdiv.hh"
10
11#include "DNA_mesh_types.h"
12#include "DNA_modifier_types.h"
13
15
16#include "MEM_guardedalloc.h"
17
18#include "subdiv_converter.hh"
19
20#include "opensubdiv_capi.hh"
22#ifdef WITH_OPENSUBDIV
25#endif
26
27namespace blender::bke::subdiv {
28
29/* --------------------------------------------------------------------
30 * Module.
31 */
32
33void init()
34{
36}
37
38void exit()
39{
41}
42
43/* --------------------------------------------------------------------
44 * Conversion helpers.
45 */
46
66
68{
69 switch (boundary_smooth) {
74 }
75 BLI_assert_msg(0, "Unknown boundary smooth flag");
77}
78
79/* --------------------------------------------------------------------
80 * Settings.
81 */
82
83bool settings_equal(const Settings *settings_a, const Settings *settings_b)
84{
85 return (settings_a->is_simple == settings_b->is_simple &&
86 settings_a->is_adaptive == settings_b->is_adaptive &&
87 settings_a->level == settings_b->level &&
88 settings_a->vtx_boundary_interpolation == settings_b->vtx_boundary_interpolation &&
89 settings_a->fvar_linear_interpolation == settings_b->fvar_linear_interpolation);
90}
91
92/* --------------------------------------------------------------------
93 * Construction.
94 */
95
96/* Creation from scratch. */
97
99{
100#ifdef WITH_OPENSUBDIV
101 SubdivStats stats;
102 stats_init(&stats);
104 OpenSubdiv_TopologyRefinerSettings topology_refiner_settings;
105 topology_refiner_settings.level = settings->level;
106 topology_refiner_settings.is_adaptive = settings->is_adaptive;
107 blender::opensubdiv::TopologyRefinerImpl *osd_topology_refiner = nullptr;
108 if (converter->getNumVertices(converter) != 0) {
110 converter, topology_refiner_settings);
111 }
112 else {
113 /* TODO(sergey): Check whether original geometry had any vertices.
114 * The thing here is: OpenSubdiv can only deal with faces, but our
115 * side of subdiv also deals with loose vertices and edges. */
116 }
117 Subdiv *subdiv = MEM_new<Subdiv>(__func__);
118 subdiv->settings = *settings;
119 subdiv->topology_refiner = osd_topology_refiner;
120 subdiv->evaluator = nullptr;
121 subdiv->displacement_evaluator = nullptr;
123 subdiv->stats = stats;
124 return subdiv;
125#else
126 UNUSED_VARS(settings, converter);
127 return nullptr;
128#endif
129}
130
131Subdiv *new_from_mesh(const Settings *settings, const Mesh *mesh)
132{
133 if (mesh->verts_num == 0) {
134 return nullptr;
135 }
136 OpenSubdiv_Converter converter;
137 converter_init_for_mesh(&converter, settings, mesh);
138 Subdiv *subdiv = new_from_converter(settings, &converter);
139 converter_free(&converter);
140 return subdiv;
141}
142
143/* Creation with cached-aware semantic. */
144
146 const Settings *settings,
147 OpenSubdiv_Converter *converter)
148{
149#ifdef WITH_OPENSUBDIV
150 /* Check if the existing descriptor can be re-used. */
151 bool can_reuse_subdiv = true;
152 if (subdiv != nullptr && subdiv->topology_refiner != nullptr) {
153 if (!settings_equal(&subdiv->settings, settings)) {
154 can_reuse_subdiv = false;
155 }
156 else {
158 can_reuse_subdiv = subdiv->topology_refiner->isEqualToConverter(converter);
160 }
161 }
162 else {
163 can_reuse_subdiv = false;
164 }
165 if (can_reuse_subdiv) {
166 return subdiv;
167 }
168 /* Create new subdiv. */
169 if (subdiv != nullptr) {
170 free(subdiv);
171 }
172 return new_from_converter(settings, converter);
173#else
174 UNUSED_VARS(subdiv, settings, converter);
175 return nullptr;
176#endif
177}
178
180{
181 OpenSubdiv_Converter converter;
182 converter_init_for_mesh(&converter, settings, mesh);
183 subdiv = update_from_converter(subdiv, settings, &converter);
184 converter_free(&converter);
185 return subdiv;
186}
187
188/* Memory release. */
189
191{
192#ifdef WITH_OPENSUBDIV
193 if (subdiv->evaluator != nullptr) {
194 const eOpenSubdivEvaluator evaluator_type = subdiv->evaluator->type;
195 if (evaluator_type != OPENSUBDIV_EVALUATOR_CPU) {
196 /* Let the draw code do the freeing, to ensure that the OpenGL context is valid. */
198 return;
199 }
200 delete subdiv->evaluator;
201 }
202 delete subdiv->topology_refiner;
204 MEM_delete(subdiv);
205#else
207#endif
208}
209
210/* --------------------------------------------------------------------
211 * Topology helpers.
212 */
213
215{
216#ifdef WITH_OPENSUBDIV
217 if (!subdiv->cache_.face_ptex_offset.is_empty()) {
218 return subdiv->cache_.face_ptex_offset;
219 }
220 const blender::opensubdiv::TopologyRefinerImpl *topology_refiner = subdiv->topology_refiner;
221 if (topology_refiner == nullptr) {
222 return Span<int>();
223 }
224 const int num_coarse_faces = topology_refiner->base_level().GetNumFaces();
225 subdiv->cache_.face_ptex_offset.reinitialize(num_coarse_faces + 1);
226 int ptex_offset = 0;
227 for (int face_index = 0; face_index < num_coarse_faces; face_index++) {
228 const int face_size = topology_refiner->base_level().GetFaceVertices(face_index).size();
229 const int num_ptex_faces = face_size == 4 ? 1 : face_size;
230 subdiv->cache_.face_ptex_offset[face_index] = ptex_offset;
231 ptex_offset += num_ptex_faces;
232 }
233 subdiv->cache_.face_ptex_offset[num_coarse_faces] = ptex_offset;
234 return subdiv->cache_.face_ptex_offset;
235#else
237 return Span<int>();
238#endif
239}
240
241} // namespace blender::bke::subdiv
void(* BKE_subsurf_modifier_free_gpu_cache_cb)(blender::bke::subdiv::Subdiv *subdiv)
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define UNUSED_VARS(...)
@ SUBSURF_BOUNDARY_SMOOTH_ALL
@ SUBSURF_BOUNDARY_SMOOTH_PRESERVE_CORNERS
@ SUBSURF_UV_SMOOTH_PRESERVE_CORNERS_AND_JUNCTIONS
@ SUBSURF_UV_SMOOTH_ALL
@ SUBSURF_UV_SMOOTH_PRESERVE_CORNERS
@ SUBSURF_UV_SMOOTH_NONE
@ SUBSURF_UV_SMOOTH_PRESERVE_BOUNDARIES
@ SUBSURF_UV_SMOOTH_PRESERVE_CORNERS_JUNCTIONS_AND_CONCAVE
Read Guarded memory(de)allocation.
const OpenSubdiv::Far::TopologyLevel & base_level() const
static TopologyRefinerImpl * createFromConverter(OpenSubdiv_Converter *converter, const OpenSubdiv_TopologyRefinerSettings &settings)
void free(Subdiv *subdiv)
Definition subdiv.cc:190
void converter_init_for_mesh(OpenSubdiv_Converter *converter, const Settings *settings, const Mesh *mesh)
@ SUBDIV_FVAR_LINEAR_INTERPOLATION_CORNERS_AND_JUNCTIONS
Definition BKE_subdiv.hh:37
@ SUBDIV_FVAR_LINEAR_INTERPOLATION_BOUNDARIES
Definition BKE_subdiv.hh:39
@ SUBDIV_FVAR_LINEAR_INTERPOLATION_NONE
Definition BKE_subdiv.hh:35
@ SUBDIV_FVAR_LINEAR_INTERPOLATION_CORNERS_ONLY
Definition BKE_subdiv.hh:36
@ SUBDIV_FVAR_LINEAR_INTERPOLATION_CORNERS_JUNCTIONS_AND_CONCAVE
Definition BKE_subdiv.hh:38
Subdiv * new_from_mesh(const Settings *settings, const Mesh *mesh)
Definition subdiv.cc:131
void stats_init(SubdivStats *stats)
void displacement_detach(Subdiv *subdiv)
FVarLinearInterpolation fvar_interpolation_from_uv_smooth(int uv_smooth)
Definition subdiv.cc:47
Subdiv * update_from_converter(Subdiv *subdiv, const Settings *settings, OpenSubdiv_Converter *converter)
Definition subdiv.cc:145
Subdiv * new_from_converter(const Settings *settings, OpenSubdiv_Converter *converter)
Definition subdiv.cc:98
void stats_begin(SubdivStats *stats, StatsValue value)
void stats_end(SubdivStats *stats, StatsValue value)
void converter_free(OpenSubdiv_Converter *converter)
VtxBoundaryInterpolation vtx_boundary_interpolation_from_subsurf(int boundary_smooth)
Definition subdiv.cc:67
@ SUBDIV_STATS_TOPOLOGY_REFINER_CREATION_TIME
Definition BKE_subdiv.hh:85
Subdiv * update_from_mesh(Subdiv *subdiv, const Settings *settings, const Mesh *mesh)
Definition subdiv.cc:179
bool settings_equal(const Settings *settings_a, const Settings *settings_b)
Definition subdiv.cc:83
Span< int > face_ptex_offset_get(Subdiv *subdiv)
Definition subdiv.cc:214
void openSubdiv_cleanup()
void openSubdiv_init()
eOpenSubdivEvaluator
@ OPENSUBDIV_EVALUATOR_CPU
int(* getNumVertices)(const OpenSubdiv_Converter *converter)
VtxBoundaryInterpolation vtx_boundary_interpolation
Definition BKE_subdiv.hh:77
FVarLinearInterpolation fvar_linear_interpolation
Definition BKE_subdiv.hh:78