Blender V5.0
MOD_subsurf.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstddef>
10#include <cstring>
11
12#include <fmt/format.h>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_utildefines.h"
17
18#include "BLT_translation.hh"
19
20#include "DNA_defaults.h"
21#include "DNA_mesh_types.h"
22#include "DNA_modifier_types.h"
23#include "DNA_object_types.h"
24#include "DNA_scene_types.h"
25#include "DNA_screen_types.h"
26
27#include "BKE_context.hh"
28#include "BKE_editmesh.hh"
29#include "BKE_global.hh"
30#include "BKE_mesh.hh"
31#include "BKE_mesh_types.hh"
32#include "BKE_mesh_wrapper.hh"
33#include "BKE_scene.hh"
34#include "BKE_subdiv.hh"
35#include "BKE_subdiv_ccg.hh"
36#include "BKE_subdiv_deform.hh"
37#include "BKE_subdiv_mesh.hh"
39
41#include "UI_resources.hh"
42
43#include "RE_engine.h"
44
45#include "RNA_access.hh"
46#include "RNA_prototypes.hh"
47
48#include "DEG_depsgraph.hh"
50
51#include "MOD_modifiertypes.hh"
52#include "MOD_ui_common.hh"
53
62
63static void free_runtime_data(void *runtime_data_v)
64{
65 if (runtime_data_v == nullptr) {
66 return;
67 }
68 SubsurfRuntimeData *runtime_data = (SubsurfRuntimeData *)runtime_data_v;
69 if (runtime_data->subdiv_cpu != nullptr) {
71 }
72 if (runtime_data->subdiv_gpu != nullptr) {
74 }
75 MEM_freeN(runtime_data);
76}
77
84
85static bool is_disabled(const Scene *scene, ModifierData *md, bool use_render_params)
86{
88 int levels = (use_render_params) ? smd->renderLevels : smd->levels;
89
90 return get_render_subsurf_level(&scene->r, levels, use_render_params != 0) == 0;
91}
92
94 const ModifierEvalContext *ctx)
95{
97 const bool use_render_params = (ctx->flag & MOD_APPLY_RENDER);
98 const int requested_levels = (use_render_params) ? smd->renderLevels : smd->levels;
99 return get_render_subsurf_level(&scene->r, requested_levels, use_render_params);
100}
101
102/* Subdivide into fully qualified mesh. */
103
105 const SubsurfModifierData *smd,
106 const ModifierEvalContext *ctx)
107{
108 const int level = subdiv_levels_for_modifier_get(smd, ctx);
109 settings->resolution = (1 << level) + 1;
111 !(ctx->flag & MOD_APPLY_TO_ORIGINAL);
112}
113
115 const ModifierEvalContext *ctx,
116 Mesh *mesh,
118{
119 Mesh *result = mesh;
121 subdiv_mesh_settings_init(&mesh_settings, smd, ctx);
122 if (mesh_settings.resolution < 3) {
123 return result;
124 }
125 result = blender::bke::subdiv::subdiv_to_mesh(subdiv, &mesh_settings, mesh);
126 return result;
127}
128
129/* Subdivide into CCG. */
130
132 const SubsurfModifierData *smd,
133 const ModifierEvalContext *ctx)
134{
135 const int level = subdiv_levels_for_modifier_get(smd, ctx);
136 settings->resolution = (1 << level) + 1;
137 settings->need_normal = true;
138 settings->need_mask = false;
139}
140
142 const ModifierEvalContext *ctx,
143 Mesh *mesh,
145{
146 Mesh *result = mesh;
147 SubdivToCCGSettings ccg_settings;
148 subdiv_ccg_settings_init(&ccg_settings, smd, ctx);
149 if (ccg_settings.resolution < 3) {
150 return result;
151 }
152 result = BKE_subdiv_to_ccg_mesh(*subdiv, ccg_settings, *mesh);
153 return result;
154}
155
156/* Cache settings for lazy CPU evaluation. */
157
159 Mesh *mesh,
161 SubsurfRuntimeData *runtime_data,
162 const bool has_gpu_subdiv)
163{
165 subdiv_mesh_settings_init(&mesh_settings, smd, ctx);
166
167 runtime_data->has_gpu_subdiv = has_gpu_subdiv;
168 runtime_data->resolution = mesh_settings.resolution;
169 runtime_data->use_optimal_display = mesh_settings.use_optimal_display;
171
172 mesh->runtime->subsurf_runtime_data = runtime_data;
173}
174
176 const Object *ob,
177 int required_mode)
178{
179 ModifierData *md = static_cast<ModifierData *>(ob->modifiers.last);
180
181 while (md) {
182 if (BKE_modifier_is_enabled(scene, md, required_mode)) {
183 break;
184 }
185
186 md = md->prev;
187 }
188
189 return md;
190}
191
192/* Modifier itself. */
193
194static Mesh *modify_mesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
195{
196 using namespace blender;
197 Mesh *result = mesh;
198#if !defined(WITH_OPENSUBDIV)
199 BKE_modifier_set_error(ctx->object, md, "Disabled, built without OpenSubdiv");
200 return result;
201#endif
204 return result;
205 }
206
208
209 /* Decrement the recent usage counters. */
210 if (runtime_data->used_cpu) {
211 runtime_data->used_cpu--;
212 }
213
214 if (runtime_data->used_gpu) {
215 runtime_data->used_gpu--;
216 }
217
218 /* Delay evaluation to the draw code if possible, provided we do not have to apply the modifier.
219 */
220 if ((ctx->flag & MOD_APPLY_TO_ORIGINAL) == 0) {
222
223 /* Same check as in `DRW_mesh_batch_cache_create_requested` to keep both code coherent. The
224 * difference is that here we do not check for the final edit mesh pointer as it is not yet
225 * assigned at this stage of modifier stack evaluation. */
226 const bool is_render_mode = (ctx->flag & MOD_APPLY_RENDER) != 0;
227 const bool is_editmode = (mesh->runtime->edit_mesh != nullptr);
228 const int required_mode = BKE_subsurf_modifier_eval_required_mode(is_render_mode, is_editmode);
229
230 /* Check if we are the last modifier in the stack. */
231 ModifierData *md = modifier_get_last_enabled_for_mode(scene, ctx->object, required_mode);
232 if (md == (const ModifierData *)smd) {
233 const bool has_gpu_subdiv = BKE_subsurf_modifier_can_do_gpu_subdiv(smd, mesh);
234 subdiv_cache_mesh_wrapper_settings(ctx, mesh, smd, runtime_data, has_gpu_subdiv);
235
236 /* Delay for:
237 * - Background mode: Not sure if we are going to use the tessellated mesh.
238 * - Render: Engine might do its own subdivision and not need this.
239 * - GPU subdivision support: Might only need to display and not access tessellated mesh.
240 *
241 * If we can't delay, we still create the wrapper so external renderers can get the base
242 * mesh. But we tessellate immediately to take advantage of better parallellization
243 * as part of multithreaded depsgraph evaluation. */
244 const bool delay = G.background || is_render_mode || has_gpu_subdiv;
245 if (!delay) {
247 }
248
249 return result;
250 }
251 }
252
254 runtime_data, mesh, false);
255 if (subdiv == nullptr) {
256 /* Happens on bad topology, but also on empty input mesh. */
257 return result;
258 }
259 const bool use_clnors = BKE_subsurf_modifier_use_custom_loop_normals(smd, mesh);
260 if (use_clnors) {
263 memcpy(data, mesh->corner_normals().data(), mesh->corner_normals().size_in_bytes());
264 }
265 /* TODO(sergey): Decide whether we ever want to use CCG for subsurf,
266 * maybe when it is a last modifier in the stack? */
267 if (true) {
268 result = subdiv_as_mesh(smd, ctx, mesh, subdiv);
269 }
270 else {
271 result = subdiv_as_ccg(smd, ctx, mesh, subdiv);
272 }
273
274 if (use_clnors) {
276 *result,
277 {static_cast<float3 *>(
278 CustomData_get_layer_for_write(&result->corner_data, CD_NORMAL, result->corners_num)),
279 result->corners_num});
281 }
282 // blender::bke::subdiv::stats_print(&subdiv->stats);
283 if (!ELEM(subdiv, runtime_data->subdiv_cpu, runtime_data->subdiv_gpu)) {
285 }
286 return result;
287}
288
290 const ModifierEvalContext *ctx,
291 Mesh *mesh,
294{
295#if !defined(WITH_OPENSUBDIV)
296 BKE_modifier_set_error(ctx->object, md, "Disabled, built without OpenSubdiv");
297 return;
298#endif
299
300 /* Subsurf does not require extra space mapping, keep matrices as is. */
301
304 return;
305 }
308 runtime_data, mesh, false);
309 if (subdiv == nullptr) {
310 /* Happens on bad topology, but also on empty input mesh. */
311 return;
312 }
313 blender::bke::subdiv::deform_coarse_vertices(subdiv, mesh, positions);
314 if (!ELEM(subdiv, runtime_data->subdiv_cpu, runtime_data->subdiv_gpu)) {
316 }
317}
318
319static bool get_show_adaptive_options(const bContext *C, Panel *panel)
320{
321 /* Don't show adaptive options if cycles isn't the active engine. */
322 const RenderEngineType *engine_type = CTX_data_engine_type(C);
323 if (!STREQ(engine_type->idname, "CYCLES")) {
324 return false;
325 }
326
327 /* Only show adaptive options if this is the last modifier. */
329 ModifierData *md = static_cast<ModifierData *>(ptr->data);
330 if (md->next != nullptr) {
331 return false;
332 }
333
334 return true;
335}
336
337static void panel_draw(const bContext *C, Panel *panel)
338{
339 uiLayout *layout = panel->layout;
340
341 PointerRNA ob_ptr;
343
344 layout->prop(ptr, "subdivision_type", UI_ITEM_R_EXPAND, std::nullopt, ICON_NONE);
345
346 layout->use_property_split_set(true);
347
348 uiLayout *col = &layout->column(true);
349 col->prop(ptr, "levels", UI_ITEM_NONE, IFACE_("Levels Viewport"), ICON_NONE);
350 col->prop(ptr, "render_levels", UI_ITEM_NONE, IFACE_("Render"), ICON_NONE);
351
352 layout->prop(ptr, "show_only_control_edges", UI_ITEM_NONE, std::nullopt, ICON_NONE);
353
355 SubsurfModifierData *smd = static_cast<SubsurfModifierData *>(ptr->data);
356 Object *ob = static_cast<Object *>(ob_ptr.data);
358 smd, static_cast<const Mesh *>(ob->data)))
359 {
360 layout->label(RPT_("Sharp edges or custom normals detected, disabling GPU subdivision"),
361 ICON_INFO);
362 }
363 else if (Object *ob_eval = DEG_get_evaluated(depsgraph, ob)) {
364 if (ModifierData *md_eval = BKE_modifiers_findby_name(ob_eval, smd->modifier.name)) {
365 if (md_eval->type == eModifierType_Subsurf) {
366 SubsurfRuntimeData *runtime_data = (SubsurfRuntimeData *)md_eval->runtime;
367
368 if (runtime_data && runtime_data->used_gpu) {
369 if (runtime_data->used_cpu) {
370 layout->label(RPT_("Using both CPU and GPU subdivision"), ICON_INFO);
371 }
372 }
373 }
374 }
375 }
376
377 if (get_show_adaptive_options(C, panel)) {
378 PanelLayout adaptive_panel = layout->panel_prop_with_bool_header(
379 C,
380 ptr,
381 "open_adaptive_subdivision_panel",
382 ptr,
383 "use_adaptive_subdivision",
384 IFACE_("Adaptive Subdivision"));
385 if (adaptive_panel.body) {
386 Scene *scene = CTX_data_scene(C);
387 PointerRNA scene_ptr = RNA_id_pointer_create(&scene->id);
388 PointerRNA cycles_ptr = RNA_pointer_get(&scene_ptr, "cycles");
389 const float render_rate = RNA_float_get(&cycles_ptr, "dicing_rate");
390 const float preview_rate = RNA_float_get(&cycles_ptr, "preview_dicing_rate");
391 std::string render_str, preview_str;
392
394 adaptive_panel.body->prop(ptr, "adaptive_space", UI_ITEM_NONE, IFACE_("Space"), ICON_NONE);
396 adaptive_panel.body->prop(
397 ptr, "adaptive_object_edge_length", UI_ITEM_NONE, std::nullopt, ICON_NONE);
398 preview_str = fmt::format("{:.5g}", preview_rate * smd->adaptive_object_edge_length);
399 render_str = fmt::format("{:.5g}", render_rate * smd->adaptive_object_edge_length);
400 }
401 else {
402 adaptive_panel.body->prop(
403 ptr, "adaptive_pixel_size", UI_ITEM_NONE, std::nullopt, ICON_NONE);
404 preview_str = fmt::format("{:.2f} px",
405 std::max(preview_rate * smd->adaptive_pixel_size, 0.1f));
406 render_str = fmt::format("{:.2f} px",
407 std::max(render_rate * smd->adaptive_pixel_size, 0.1f));
408 }
409
410 uiLayout *split = &adaptive_panel.body->split(0.4f, false);
411 uiLayout *col = &split->column(true);
412 col->alignment_set(blender::ui::LayoutAlign::Right);
413 col->label(IFACE_("Viewport"), ICON_NONE);
414 col->label(IFACE_("Render"), ICON_NONE);
415 col = &split->column(true);
416 col->label(preview_str, ICON_NONE);
417 col->label(render_str, ICON_NONE);
418 }
419 }
420
421 if (uiLayout *advanced_layout = layout->panel_prop(
422 C, ptr, "open_advanced_panel", IFACE_("Advanced")))
423 {
424 advanced_layout->use_property_split_set(true);
425
426 advanced_layout->prop(ptr, "use_limit_surface", UI_ITEM_NONE, std::nullopt, ICON_NONE);
427
428 uiLayout *col = &advanced_layout->column(true);
430 RNA_boolean_get(ptr, "use_limit_surface"));
431 col->prop(ptr, "quality", UI_ITEM_NONE, std::nullopt, ICON_NONE);
432
433 advanced_layout->prop(ptr, "uv_smooth", UI_ITEM_NONE, std::nullopt, ICON_NONE);
434 advanced_layout->prop(ptr, "boundary_smooth", UI_ITEM_NONE, std::nullopt, ICON_NONE);
435 advanced_layout->prop(ptr, "use_creases", UI_ITEM_NONE, std::nullopt, ICON_NONE);
436 advanced_layout->prop(ptr, "use_custom_normals", UI_ITEM_NONE, std::nullopt, ICON_NONE);
437 }
438
440}
441
442static void panel_register(ARegionType *region_type)
443{
445}
446
448 /*idname*/ "Subdivision",
449 /*name*/ N_("Subdivision"),
450 /*struct_name*/ "SubsurfModifierData",
451 /*struct_size*/ sizeof(SubsurfModifierData),
452 /*srna*/ &RNA_SubsurfModifier,
457 /*icon*/ ICON_MOD_SUBSURF,
458
459 /*copy_data*/ BKE_modifier_copydata_generic,
460
461 /*deform_verts*/ nullptr,
462 /*deform_matrices*/ deform_matrices,
463 /*deform_verts_EM*/ nullptr,
464 /*deform_matrices_EM*/ nullptr,
465 /*modify_mesh*/ modify_mesh,
466 /*modify_geometry_set*/ nullptr,
467
468 /*init_data*/ init_data,
469 /*required_data_mask*/ nullptr,
470 /*free_data*/ free_data,
471 /*is_disabled*/ is_disabled,
472 /*update_depsgraph*/ nullptr,
473 /*depends_on_time*/ nullptr,
474 /*depends_on_normals*/ nullptr,
475 /*foreach_ID_link*/ nullptr,
476 /*foreach_tex_link*/ nullptr,
477 /*free_runtime_data*/ free_runtime_data,
478 /*panel_register*/ panel_register,
479 /*blend_write*/ nullptr,
480 /*blend_read*/ nullptr,
481 /*foreach_cache*/ nullptr,
482 /*foreach_working_space_color*/ nullptr,
483};
Depsgraph * CTX_data_depsgraph_pointer(const bContext *C)
RenderEngineType * CTX_data_engine_type(const bContext *C)
Scene * CTX_data_scene(const bContext *C)
@ CD_CONSTRUCT
void CustomData_free_layers(CustomData *data, eCustomDataType type)
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)
void * CustomData_add_layer(CustomData *data, eCustomDataType type, eCDAllocType alloctype, int totelem)
Mesh * BKE_mesh_wrapper_ensure_subdivision(Mesh *mesh)
bool BKE_modifier_is_enabled(const Scene *scene, ModifierData *md, int required_mode)
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
@ eModifierTypeFlag_AcceptsCVs
@ eModifierTypeFlag_SupportsMapping
@ eModifierTypeFlag_EnableInEditmode
@ eModifierTypeFlag_SupportsEditmode
@ eModifierTypeFlag_AcceptsMesh
ModifierData * BKE_modifiers_findby_name(const Object *ob, const char *name)
void BKE_modifier_set_error(const Object *ob, ModifierData *md, const char *format,...) ATTR_PRINTF_FORMAT(3
@ MOD_APPLY_TO_ORIGINAL
@ MOD_APPLY_RENDER
int get_render_subsurf_level(const RenderData *r, int lvl, bool for_render)
Definition scene.cc:2753
Mesh * BKE_subdiv_to_ccg_mesh(blender::bke::subdiv::Subdiv &subdiv, const SubdivToCCGSettings &settings, const Mesh &coarse_mesh)
bool BKE_subsurf_modifier_can_do_gpu_subdiv(const SubsurfModifierData *smd, const Mesh *mesh)
int BKE_subsurf_modifier_eval_required_mode(bool is_final_render, bool is_edit_mode)
bool BKE_subsurf_modifier_force_disable_gpu_evaluation_for_mesh(const SubsurfModifierData *smd, const Mesh *mesh)
bool BKE_subsurf_modifier_use_custom_loop_normals(const SubsurfModifierData *smd, const Mesh *mesh)
bool BKE_subsurf_modifier_runtime_init(SubsurfModifierData *smd, bool use_render_params)
blender::bke::subdiv::Subdiv * BKE_subsurf_modifier_subdiv_descriptor_ensure(SubsurfRuntimeData *runtime_data, const Mesh *mesh, bool for_draw_code)
#define BLI_assert(a)
Definition BLI_assert.h:46
#define ELEM(...)
#define MEMCMP_STRUCT_AFTER_IS_ZERO(struct_var, member)
#define MEMCPY_STRUCT_AFTER(struct_dst, struct_src, member)
#define STREQ(a, b)
#define RPT_(msgid)
#define IFACE_(msgid)
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
#define DNA_struct_default_get(struct_name)
@ eModifierType_Subsurf
@ eSubsurfModifierFlag_UseAdaptiveSubdivision
@ eSubsurfModifierFlag_ControlEdges
@ eSubsurfModifierFlag_UseCustomNormals
@ SUBSURF_ADAPTIVE_SPACE_OBJECT
Object is a sort of wrapper for general info.
@ OB_MESH
static void split(const char *text, const char *seps, char ***str, int *count)
static bool is_disabled
Read Guarded memory(de)allocation.
static void deform_matrices(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, blender::MutableSpan< blender::float3 > positions, blender::MutableSpan< blender::float3x3 > matrices)
static void init_data(ModifierData *md)
static void panel_register(ARegionType *region_type)
static void panel_draw(const bContext *, Panel *panel)
static Mesh * modify_mesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
Definition MOD_array.cc:862
static void free_data(ModifierData *md)
Definition MOD_bevel.cc:272
static void free_runtime_data(void *runtime_data_v)
static void init_data(ModifierData *md)
static void subdiv_cache_mesh_wrapper_settings(const ModifierEvalContext *ctx, Mesh *mesh, SubsurfModifierData *smd, SubsurfRuntimeData *runtime_data, const bool has_gpu_subdiv)
static void panel_register(ARegionType *region_type)
static void free_runtime_data(void *runtime_data_v)
static Mesh * subdiv_as_ccg(SubsurfModifierData *smd, const ModifierEvalContext *ctx, Mesh *mesh, blender::bke::subdiv::Subdiv *subdiv)
static ModifierData * modifier_get_last_enabled_for_mode(const Scene *scene, const Object *ob, int required_mode)
static Mesh * modify_mesh(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh)
static void free_data(ModifierData *md)
static void subdiv_mesh_settings_init(blender::bke::subdiv::ToMeshSettings *settings, const SubsurfModifierData *smd, const ModifierEvalContext *ctx)
static void subdiv_ccg_settings_init(SubdivToCCGSettings *settings, const SubsurfModifierData *smd, const ModifierEvalContext *ctx)
ModifierTypeInfo modifierType_Subsurf
static Mesh * subdiv_as_mesh(SubsurfModifierData *smd, const ModifierEvalContext *ctx, Mesh *mesh, blender::bke::subdiv::Subdiv *subdiv)
static void deform_matrices(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, blender::MutableSpan< blender::float3 > positions, blender::MutableSpan< blender::float3x3 >)
static int subdiv_levels_for_modifier_get(const SubsurfModifierData *smd, const ModifierEvalContext *ctx)
static void panel_draw(const bContext *C, Panel *panel)
static bool get_show_adaptive_options(const bContext *C, Panel *panel)
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
void modifier_error_message_draw(uiLayout *layout, PointerRNA *ptr)
#define C
Definition RandGen.cpp:29
@ UI_ITEM_R_EXPAND
#define UI_ITEM_NONE
BMesh const char void * data
BPy_StructRNA * depsgraph
uint col
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
#define G(x, y, z)
void free(Subdiv *subdiv)
Definition subdiv.cc:190
void deform_coarse_vertices(Subdiv *subdiv, const Mesh *coarse_mesh, MutableSpan< float3 > vert_positions)
Mesh * subdiv_to_mesh(Subdiv *subdiv, const ToMeshSettings *settings, const Mesh *coarse_mesh)
void mesh_set_custom_normals_normalized(Mesh &mesh, MutableSpan< float3 > corner_normals)
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
float RNA_float_get(PointerRNA *ptr, const char *name)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
PointerRNA RNA_id_pointer_create(ID *id)
void * last
int corners_num
MeshRuntimeHandle * runtime
CustomData corner_data
struct ModifierData * next
struct ModifierData * prev
ModifierApplyFlag flag
ListBase modifiers
struct uiLayout * layout
void * data
Definition RNA_types.hh:53
char idname[64]
Definition RE_engine.h:73
struct RenderData r
blender::bke::subdiv::Subdiv * subdiv_cpu
blender::bke::subdiv::Subdiv * subdiv_gpu
PanelLayout panel_prop(const bContext *C, PointerRNA *open_prop_owner, blender::StringRefNull open_prop_name)
PanelLayout panel_prop_with_bool_header(const bContext *C, PointerRNA *open_prop_owner, blender::StringRefNull open_prop_name, PointerRNA *bool_prop_owner, blender::StringRefNull bool_prop_name, std::optional< blender::StringRef > label)
void label(blender::StringRef name, int icon)
uiLayout & column(bool align)
void active_set(bool active)
uiLayout & split(float percentage, bool align)
void use_property_split_set(bool value)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
#define N_(msgid)
PointerRNA * ptr
Definition wm_files.cc:4238