Files
test2/source/blender/blenkernel/intern/subdiv_modifier.cc
Hans Goudey 89e3ba4e25 Mesh: Replace auto smooth with node group
Design task: #93551

This PR replaces the auto smooth option with a geometry nodes modifier
that sets the sharp edge attribute. This solves a fair number of long-
standing problems related to auto smooth, simplifies the process of
normal computation, and allows Blender to automatically choose between
face, vertex, and face corner normals based on the sharp edge and face
attributes.

Versioning adds a geometry node group to objects with meshes that had
auto-smooth enabled. The modifier can be applied, which also improves
performance.

Auto smooth is now unnecessary to get a combination of sharp and smooth
edges. In general workflows are changed a bit. Separate procedural and
destructive workflows are available. Custom normals can be used
immediately without turning on the removed auto smooth option.

**Procedural**

The node group asset "Smooth by Angle" is the main way to set sharp
normals based on the edge angle. It can be accessed directly in the add
modifier menu. Of course the modifier can be reordered, muted, or
applied like any other, or changed internally like any geometry nodes
modifier.

**Destructive**
Often the sharp edges don't need to be dynamic. This can give better
performance since edge angles don't need to be recalculated. In edit
mode the two operators "Select Sharp Edges" and "Mark Sharp" can be
used. In other modes, the "Shade Smooth by Angle" controls the edge
sharpness directly.

### Breaking API Changes
- `use_auto_smooth` is removed. Face corner normals are now used
  automatically   if there are mixed smooth vs. not smooth tags. Meshes
  now always use custom normals if they exist.
- In Cycles, the lack of the separate auto smooth state makes normals look
  triangulated when all faces are shaded smooth.
- `auto_smooth_angle` is removed. Replaced by a modifier (or operator)
  controlling the sharp edge attribute. This means the mesh itself
  (without an object) doesn't know anything about automatically smoothing
  by angle anymore.
- `create_normals_split`, `calc_normals_split`, and `free_normals_split`
  are removed, and are replaced by the simpler `Mesh.corner_normals`
  collection property. Since it gives access to the normals cache, it
  is automatically updated when relevant data changes.

Addons are updated here: https://projects.blender.org/blender/blender-addons/pulls/104609

### Tests
- `geo_node_curves_test_deform_curves_on_surface` has slightly different
   results because face corner normals are used instead of interpolated
   vertex normals.
- `bf_wavefront_obj_tests` has different export results for one file
  which mixed sharp and smooth faces without turning on auto smooth.
- `cycles_mesh_cpu` has one object which is completely flat shaded.
  Previously every edge was split before rendering, now it looks triangulated.

Pull Request: https://projects.blender.org/blender/blender/pulls/108014
2023-10-20 16:54:08 +02:00

182 lines
5.7 KiB
C++

/* SPDX-FileCopyrightText: 2021 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#include "BKE_attribute.h"
#include "BKE_subdiv_modifier.hh"
#include "MEM_guardedalloc.h"
#include "DNA_mesh_types.h"
#include "DNA_modifier_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "DNA_userdef_types.h"
#include "BKE_mesh.hh"
#include "BKE_modifier.h"
#include "BKE_subdiv.hh"
#include "GPU_capabilities.h"
#include "GPU_context.h"
#include "opensubdiv_capi.h"
SubdivSettings BKE_subsurf_modifier_settings_init(const SubsurfModifierData *smd,
const bool use_render_params)
{
const int requested_levels = (use_render_params) ? smd->renderLevels : smd->levels;
SubdivSettings settings{};
settings.is_simple = (smd->subdivType == SUBSURF_TYPE_SIMPLE);
settings.is_adaptive = !(smd->flags & eSubsurfModifierFlag_UseRecursiveSubdivision);
settings.level = settings.is_simple ? 1 :
(settings.is_adaptive ? smd->quality : requested_levels);
settings.use_creases = (smd->flags & eSubsurfModifierFlag_UseCrease);
settings.vtx_boundary_interpolation = BKE_subdiv_vtx_boundary_interpolation_from_subsurf(
smd->boundary_smooth);
settings.fvar_linear_interpolation = BKE_subdiv_fvar_interpolation_from_uv_smooth(
smd->uv_smooth);
return settings;
}
bool BKE_subsurf_modifier_runtime_init(SubsurfModifierData *smd, const bool use_render_params)
{
SubdivSettings settings = BKE_subsurf_modifier_settings_init(smd, use_render_params);
SubsurfRuntimeData *runtime_data = (SubsurfRuntimeData *)smd->modifier.runtime;
if (settings.level == 0) {
/* Modifier is effectively disabled, but still update settings if runtime data
* was already allocated. */
if (runtime_data) {
runtime_data->settings = settings;
runtime_data->used_cpu = runtime_data->used_gpu = 0;
}
return false;
}
/* Allocate runtime data if it did not exist yet. */
if (runtime_data == nullptr) {
runtime_data = MEM_cnew<SubsurfRuntimeData>(__func__);
smd->modifier.runtime = runtime_data;
}
runtime_data->settings = settings;
return true;
}
static ModifierData *modifier_get_last_enabled_for_mode(const Scene *scene,
const Object *ob,
int required_mode)
{
ModifierData *md = static_cast<ModifierData *>(ob->modifiers.last);
while (md) {
if (BKE_modifier_is_enabled(scene, md, required_mode)) {
break;
}
md = md->prev;
}
return md;
}
bool BKE_subsurf_modifier_use_custom_loop_normals(const SubsurfModifierData *smd, const Mesh *mesh)
{
return smd->flags & eSubsurfModifierFlag_UseCustomNormals &&
mesh->normals_domain() == blender::bke::MeshNormalDomain::Corner;
}
static bool is_subdivision_evaluation_possible_on_gpu()
{
/* Only OpenGL is supported for OpenSubdiv evaluation for now. */
if (GPU_backend_get_type() != GPU_BACKEND_OPENGL) {
return false;
}
if (!(GPU_compute_shader_support())) {
return false;
}
if (GPU_max_compute_shader_storage_blocks() < MAX_GPU_SUBDIV_SSBOS) {
return false;
}
return true;
}
bool BKE_subsurf_modifier_force_disable_gpu_evaluation_for_mesh(const SubsurfModifierData *smd,
const Mesh *mesh)
{
if ((U.gpu_flag & USER_GPU_FLAG_SUBDIVISION_EVALUATION) == 0) {
/* GPU subdivision is explicitly disabled, so we don't force it. */
return false;
}
if (!is_subdivision_evaluation_possible_on_gpu()) {
/* The GPU type is not compatible with the subdivision. */
return false;
}
return BKE_subsurf_modifier_use_custom_loop_normals(smd, mesh);
}
bool BKE_subsurf_modifier_can_do_gpu_subdiv(const Scene *scene,
const Object *ob,
const Mesh *mesh,
const SubsurfModifierData *smd,
int required_mode)
{
if ((U.gpu_flag & USER_GPU_FLAG_SUBDIVISION_EVALUATION) == 0) {
return false;
}
/* Deactivate GPU subdivision if autosmooth or custom split normals are used as those are
* complicated to support on GPU, and should really be separate workflows. */
if (BKE_subsurf_modifier_use_custom_loop_normals(smd, mesh)) {
return false;
}
ModifierData *md = modifier_get_last_enabled_for_mode(scene, ob, required_mode);
if (md != (const ModifierData *)smd) {
return false;
}
return is_subdivision_evaluation_possible_on_gpu();
}
bool BKE_subsurf_modifier_has_gpu_subdiv(const Mesh *mesh)
{
SubsurfRuntimeData *runtime_data = mesh->runtime->subsurf_runtime_data;
return runtime_data && runtime_data->has_gpu_subdiv;
}
void (*BKE_subsurf_modifier_free_gpu_cache_cb)(Subdiv *subdiv) = nullptr;
Subdiv *BKE_subsurf_modifier_subdiv_descriptor_ensure(SubsurfRuntimeData *runtime_data,
const Mesh *mesh,
const bool for_draw_code)
{
if (for_draw_code) {
runtime_data->used_gpu = 2; /* countdown in frames */
return runtime_data->subdiv_gpu = BKE_subdiv_update_from_mesh(
runtime_data->subdiv_gpu, &runtime_data->settings, mesh);
}
runtime_data->used_cpu = 2;
return runtime_data->subdiv_cpu = BKE_subdiv_update_from_mesh(
runtime_data->subdiv_cpu, &runtime_data->settings, mesh);
}
int BKE_subsurf_modifier_eval_required_mode(bool is_final_render, bool is_edit_mode)
{
if (is_final_render) {
return eModifierMode_Render;
}
return eModifierMode_Realtime | (is_edit_mode ? int(eModifierMode_Editmode) : 0);
}