2010-04-11 22:12:30 +00:00
|
|
|
/*
|
2011-10-23 17:52:20 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2005 by the Blender Foundation.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s): Daniel Dunbar
|
|
|
|
* Ton Roosendaal,
|
|
|
|
* Ben Batt,
|
|
|
|
* Brecht Van Lommel,
|
|
|
|
* Campbell Barton
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
*/
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2011-02-25 13:57:17 +00:00
|
|
|
/** \file blender/modifiers/intern/MOD_subsurf.c
|
|
|
|
* \ingroup modifiers
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-03-09 01:25:59 +00:00
|
|
|
#include <stddef.h>
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2010-04-12 00:36:50 +00:00
|
|
|
#include "DNA_object_types.h"
|
2018-07-17 18:09:18 +02:00
|
|
|
#include "DNA_scene_types.h"
|
|
|
|
#include "DNA_mesh_types.h"
|
2015-08-25 13:24:02 +02:00
|
|
|
|
|
|
|
#ifdef WITH_OPENSUBDIV
|
|
|
|
# include "DNA_userdef_types.h"
|
|
|
|
#endif
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2011-01-07 18:36:47 +00:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
#include "BKE_cdderivedmesh.h"
|
|
|
|
#include "BKE_scene.h"
|
2018-07-17 18:09:18 +02:00
|
|
|
#include "BKE_subdiv.h"
|
2010-04-11 22:12:30 +00:00
|
|
|
#include "BKE_subsurf.h"
|
|
|
|
|
2017-04-06 15:37:46 +02:00
|
|
|
#include "DEG_depsgraph.h"
|
2017-04-06 16:34:38 +02:00
|
|
|
#include "DEG_depsgraph_query.h"
|
2017-04-06 15:37:46 +02:00
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
#include "MOD_modifiertypes.h"
|
|
|
|
|
2013-01-24 02:14:39 +00:00
|
|
|
#include "intern/CCGSubSurf.h"
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2018-07-31 11:17:51 +02:00
|
|
|
// #define USE_OPENSUBDIV
|
2018-07-17 18:09:18 +02:00
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
static void initData(ModifierData *md)
|
|
|
|
{
|
2012-05-06 13:38:33 +00:00
|
|
|
SubsurfModifierData *smd = (SubsurfModifierData *) md;
|
2010-04-11 22:12:30 +00:00
|
|
|
|
|
|
|
smd->levels = 1;
|
|
|
|
smd->renderLevels = 2;
|
|
|
|
smd->flags |= eSubsurfModifierFlag_SubsurfUv;
|
|
|
|
}
|
|
|
|
|
2018-07-04 12:45:30 +02:00
|
|
|
static void copyData(const ModifierData *md, ModifierData *target, const int flag)
|
2010-04-11 22:12:30 +00:00
|
|
|
{
|
2013-12-22 04:35:52 +11:00
|
|
|
#if 0
|
2018-05-08 15:04:10 +02:00
|
|
|
const SubsurfModifierData *smd = (const SubsurfModifierData *) md;
|
2013-12-22 04:35:52 +11:00
|
|
|
#endif
|
2012-05-06 13:38:33 +00:00
|
|
|
SubsurfModifierData *tsmd = (SubsurfModifierData *) target;
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2018-07-04 12:45:30 +02:00
|
|
|
modifier_copyData_generic(md, target, flag);
|
2013-12-22 04:35:52 +11:00
|
|
|
|
|
|
|
tsmd->emCache = tsmd->mCache = NULL;
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void freeData(ModifierData *md)
|
|
|
|
{
|
2012-05-06 13:38:33 +00:00
|
|
|
SubsurfModifierData *smd = (SubsurfModifierData *) md;
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2012-03-24 06:24:53 +00:00
|
|
|
if (smd->mCache) {
|
2010-04-11 22:12:30 +00:00
|
|
|
ccgSubSurf_free(smd->mCache);
|
2018-05-08 14:21:02 +02:00
|
|
|
smd->mCache = NULL;
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
2012-03-24 06:24:53 +00:00
|
|
|
if (smd->emCache) {
|
2010-04-11 22:12:30 +00:00
|
|
|
ccgSubSurf_free(smd->emCache);
|
2018-05-08 14:21:02 +02:00
|
|
|
smd->emCache = NULL;
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:14:08 +02:00
|
|
|
static bool isDisabled(const Scene *scene, ModifierData *md, bool useRenderParams)
|
2010-04-11 22:12:30 +00:00
|
|
|
{
|
2012-05-06 13:38:33 +00:00
|
|
|
SubsurfModifierData *smd = (SubsurfModifierData *) md;
|
|
|
|
int levels = (useRenderParams) ? smd->renderLevels : smd->levels;
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2018-06-21 17:54:12 +02:00
|
|
|
return get_render_subsurf_level(&scene->r, levels, useRenderParams != 0) == 0;
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 08:04:56 +02:00
|
|
|
static DerivedMesh *applyModifier(
|
2018-05-12 08:21:07 +02:00
|
|
|
ModifierData *md, const ModifierEvalContext *ctx,
|
|
|
|
DerivedMesh *derivedData)
|
2010-04-11 22:12:30 +00:00
|
|
|
{
|
2012-05-06 13:38:33 +00:00
|
|
|
SubsurfModifierData *smd = (SubsurfModifierData *) md;
|
2018-06-18 11:51:02 +02:00
|
|
|
struct Scene *scene = DEG_get_evaluated_scene(ctx->depsgraph);
|
2012-05-10 20:31:55 +00:00
|
|
|
SubsurfFlags subsurf_flags = 0;
|
2010-04-11 22:12:30 +00:00
|
|
|
DerivedMesh *result;
|
2018-05-01 17:33:04 +02:00
|
|
|
const bool useRenderParams = (ctx->flag & MOD_APPLY_RENDER) != 0;
|
|
|
|
const bool isFinalCalc = (ctx->flag & MOD_APPLY_USECACHE) != 0;
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2015-07-20 16:08:06 +02:00
|
|
|
#ifdef WITH_OPENSUBDIV
|
2018-05-01 17:33:04 +02:00
|
|
|
const bool allow_gpu = (ctx->flag & MOD_APPLY_ALLOW_GPU) != 0;
|
2015-07-20 16:08:06 +02:00
|
|
|
#endif
|
2015-10-15 00:17:54 +05:00
|
|
|
bool do_cddm_convert = useRenderParams || !isFinalCalc;
|
2015-07-20 16:08:06 +02:00
|
|
|
|
2012-05-11 08:05:47 +00:00
|
|
|
if (useRenderParams)
|
2012-05-10 20:31:55 +00:00
|
|
|
subsurf_flags |= SUBSURF_USE_RENDER_PARAMS;
|
2012-05-11 08:05:47 +00:00
|
|
|
if (isFinalCalc)
|
2012-05-10 20:31:55 +00:00
|
|
|
subsurf_flags |= SUBSURF_IS_FINAL_CALC;
|
2018-05-01 17:33:04 +02:00
|
|
|
if (ctx->object->mode & OB_MODE_EDIT)
|
2012-05-10 20:31:55 +00:00
|
|
|
subsurf_flags |= SUBSURF_IN_EDIT_MODE;
|
2015-07-20 16:08:06 +02:00
|
|
|
|
|
|
|
#ifdef WITH_OPENSUBDIV
|
|
|
|
/* TODO(sergey): Not entirely correct, modifiers on top of subsurf
|
|
|
|
* could be disabled.
|
|
|
|
*/
|
2015-08-03 17:30:44 +02:00
|
|
|
if (md->next == NULL &&
|
|
|
|
allow_gpu &&
|
|
|
|
do_cddm_convert == false &&
|
|
|
|
smd->use_opensubdiv)
|
|
|
|
{
|
2015-08-25 13:23:06 +02:00
|
|
|
if (U.opensubdiv_compute_type == USER_OPENSUBDIV_COMPUTE_NONE) {
|
|
|
|
modifier_setError(md, "OpenSubdiv is disabled in User Preferences");
|
|
|
|
}
|
2018-05-01 17:33:04 +02:00
|
|
|
else if ((ctx->object->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT)) != 0) {
|
2016-09-02 15:15:42 +02:00
|
|
|
modifier_setError(md, "OpenSubdiv is not supported in paint modes");
|
|
|
|
}
|
2018-05-01 17:33:04 +02:00
|
|
|
else if ((DEG_get_eval_flags_for_id(ctx->depsgraph, &ctx->object->id) & DAG_EVAL_NEED_CPU) == 0) {
|
2015-08-03 15:57:22 +02:00
|
|
|
subsurf_flags |= SUBSURF_USE_GPU_BACKEND;
|
2015-10-15 00:17:54 +05:00
|
|
|
do_cddm_convert = false;
|
2015-08-03 15:57:22 +02:00
|
|
|
}
|
2015-08-04 12:23:53 +02:00
|
|
|
else {
|
|
|
|
modifier_setError(md, "OpenSubdiv is disabled due to dependencies");
|
|
|
|
}
|
2015-07-20 16:08:06 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-06-18 11:51:02 +02:00
|
|
|
result = subsurf_make_derived_from_derived(derivedData, smd, scene, NULL, subsurf_flags);
|
2013-02-26 14:32:53 +00:00
|
|
|
result->cd_flag = derivedData->cd_flag;
|
2015-07-20 16:08:06 +02:00
|
|
|
|
2017-05-11 15:57:19 +02:00
|
|
|
{
|
2012-05-06 13:38:33 +00:00
|
|
|
DerivedMesh *cddm = CDDM_copy(result);
|
2010-04-11 22:12:30 +00:00
|
|
|
result->release(result);
|
2012-05-06 13:38:33 +00:00
|
|
|
result = cddm;
|
2010-04-11 22:12:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-19 11:47:55 +02:00
|
|
|
#ifndef WITH_OPESUBDIV
|
2017-05-11 16:15:26 +02:00
|
|
|
(void) do_cddm_convert;
|
|
|
|
#endif
|
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-08-16 12:45:11 +10:00
|
|
|
static DerivedMesh *applyModifierEM(
|
2018-05-01 17:33:04 +02:00
|
|
|
ModifierData *md, const ModifierEvalContext *ctx,
|
|
|
|
struct BMEditMesh *UNUSED(editData),
|
|
|
|
DerivedMesh *derivedData)
|
2010-04-11 22:12:30 +00:00
|
|
|
{
|
2012-05-06 13:38:33 +00:00
|
|
|
SubsurfModifierData *smd = (SubsurfModifierData *) md;
|
2018-06-18 11:51:02 +02:00
|
|
|
struct Scene *scene = DEG_get_evaluated_scene(ctx->depsgraph);
|
2010-04-11 22:12:30 +00:00
|
|
|
DerivedMesh *result;
|
2013-09-12 10:41:00 +00:00
|
|
|
/* 'orco' using editmode flags would cause cache to be used twice in editbmesh_calc_modifiers */
|
2018-05-01 17:33:04 +02:00
|
|
|
SubsurfFlags ss_flags = (ctx->flag & MOD_APPLY_ORCO) ? 0 : (SUBSURF_FOR_EDIT_MODE | SUBSURF_IN_EDIT_MODE);
|
2015-07-20 16:08:06 +02:00
|
|
|
#ifdef WITH_OPENSUBDIV
|
2018-05-01 17:33:04 +02:00
|
|
|
const bool allow_gpu = (ctx->flag & MOD_APPLY_ALLOW_GPU) != 0;
|
2015-08-05 14:51:08 +02:00
|
|
|
if (md->next == NULL && allow_gpu && smd->use_opensubdiv) {
|
2015-09-16 23:00:50 +05:00
|
|
|
modifier_setError(md, "OpenSubdiv is not supported in edit mode");
|
2015-07-20 16:08:06 +02:00
|
|
|
}
|
|
|
|
#endif
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2018-06-18 11:51:02 +02:00
|
|
|
result = subsurf_make_derived_from_derived(derivedData, smd, scene, NULL, ss_flags);
|
2018-07-17 18:09:18 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_OPENSUBDIV
|
|
|
|
static int subdiv_levels_for_modifier_get(const SubsurfModifierData *smd,
|
|
|
|
const ModifierEvalContext *ctx)
|
|
|
|
{
|
|
|
|
Scene *scene = DEG_get_evaluated_scene(ctx->depsgraph);
|
|
|
|
const bool use_render_params = (ctx->flag & MOD_APPLY_RENDER);
|
|
|
|
const int requested_levels = (use_render_params) ? smd->renderLevels
|
|
|
|
: smd->levels;
|
|
|
|
return get_render_subsurf_level(&scene->r,
|
|
|
|
requested_levels,
|
|
|
|
use_render_params);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void subdiv_settings_init(SubdivSettings *settings,
|
|
|
|
const SubsurfModifierData *smd,
|
|
|
|
const ModifierEvalContext *ctx)
|
|
|
|
{
|
|
|
|
settings->is_simple = (smd->subdivType == SUBSURF_TYPE_SIMPLE);
|
|
|
|
settings->is_adaptive = !settings->is_simple;
|
|
|
|
settings->level = subdiv_levels_for_modifier_get(smd, ctx);
|
|
|
|
settings->fvar_linear_interpolation =
|
|
|
|
(smd->flags & eSubsurfModifierFlag_SubsurfUv)
|
|
|
|
? SUBDIV_FVAR_LINEAR_INTERPOLATION_CORNERS_ONLY
|
|
|
|
: SUBDIV_FVAR_LINEAR_INTERPOLATION_ALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void subdiv_mesh_settings_init(SubdivToMeshSettings *settings,
|
|
|
|
const SubdivSettings *subdiv_settings)
|
|
|
|
{
|
|
|
|
settings->resolution = (1 << subdiv_settings->level) + 1;
|
|
|
|
}
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2018-07-17 18:09:18 +02:00
|
|
|
static Mesh *applyModifier_subdiv(ModifierData *md,
|
|
|
|
const ModifierEvalContext *ctx,
|
|
|
|
Mesh *mesh)
|
|
|
|
{
|
|
|
|
Mesh *result = mesh;
|
|
|
|
SubsurfModifierData *smd = (SubsurfModifierData *) md;
|
|
|
|
SubdivSettings subdiv_settings;
|
|
|
|
subdiv_settings_init(&subdiv_settings, smd, ctx);
|
|
|
|
if (subdiv_settings.level == 0) {
|
|
|
|
/* NOTE: Shouldn't really happen, is supposed to be catched by
|
|
|
|
* isDisabled() callback.
|
|
|
|
*/
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/* TODO(sergey): Try to re-use subdiv when possible. */
|
|
|
|
Subdiv *subdiv = BKE_subdiv_new_from_mesh(&subdiv_settings, mesh);
|
|
|
|
if (subdiv == NULL) {
|
2018-08-01 15:43:57 +02:00
|
|
|
/* Happens on bad topology, ut also on empty input mesh. */
|
2018-07-17 18:09:18 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
SubdivToMeshSettings mesh_settings;
|
|
|
|
subdiv_mesh_settings_init(&mesh_settings, &subdiv_settings);
|
|
|
|
result = BKE_subdiv_to_mesh(subdiv, &mesh_settings, mesh);
|
|
|
|
/* TODO(sergey): Cache subdiv somehow. */
|
2018-07-19 16:27:18 +02:00
|
|
|
// BKE_subdiv_stats_print(&subdiv->stats);
|
2018-07-17 18:09:18 +02:00
|
|
|
BKE_subdiv_free(subdiv);
|
2010-04-11 22:12:30 +00:00
|
|
|
return result;
|
|
|
|
}
|
2018-07-17 18:09:18 +02:00
|
|
|
#endif
|
2010-04-11 22:12:30 +00:00
|
|
|
|
2015-08-05 19:19:05 +02:00
|
|
|
static bool dependsOnNormals(ModifierData *md)
|
|
|
|
{
|
|
|
|
#ifdef WITH_OPENSUBDIV
|
|
|
|
SubsurfModifierData *smd = (SubsurfModifierData *) md;
|
|
|
|
if (smd->use_opensubdiv && md->next == NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-06 12:34:31 +10:00
|
|
|
#else
|
|
|
|
UNUSED_VARS(md);
|
2015-08-05 19:19:05 +02:00
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-11 22:12:30 +00:00
|
|
|
|
|
|
|
ModifierTypeInfo modifierType_Subsurf = {
|
|
|
|
/* name */ "Subsurf",
|
|
|
|
/* structName */ "SubsurfModifierData",
|
|
|
|
/* structSize */ sizeof(SubsurfModifierData),
|
|
|
|
/* type */ eModifierTypeType_Constructive,
|
2012-05-06 13:38:33 +00:00
|
|
|
/* flags */ eModifierTypeFlag_AcceptsMesh |
|
|
|
|
eModifierTypeFlag_SupportsMapping |
|
|
|
|
eModifierTypeFlag_SupportsEditmode |
|
|
|
|
eModifierTypeFlag_EnableInEditmode |
|
|
|
|
eModifierTypeFlag_AcceptsCVs,
|
2010-04-11 22:12:30 +00:00
|
|
|
|
|
|
|
/* copyData */ copyData,
|
2018-04-18 15:45:54 +02:00
|
|
|
|
|
|
|
/* deformVerts_DM */ NULL,
|
|
|
|
/* deformMatrices_DM */ NULL,
|
|
|
|
/* deformVertsEM_DM */ NULL,
|
|
|
|
/* deformMatricesEM_DM*/NULL,
|
|
|
|
/* applyModifier_DM */ applyModifier,
|
|
|
|
/* applyModifierEM_DM */applyModifierEM,
|
|
|
|
|
2011-03-05 10:29:10 +00:00
|
|
|
/* deformVerts */ NULL,
|
|
|
|
/* deformMatrices */ NULL,
|
|
|
|
/* deformVertsEM */ NULL,
|
|
|
|
/* deformMatricesEM */ NULL,
|
2018-07-17 18:09:18 +02:00
|
|
|
#ifdef USE_OPENSUBDIV
|
|
|
|
/* applyModifier */ applyModifier_subdiv,
|
|
|
|
#else
|
2018-04-18 15:45:54 +02:00
|
|
|
/* applyModifier */ NULL,
|
2018-07-17 18:09:18 +02:00
|
|
|
#endif
|
2018-04-18 15:45:54 +02:00
|
|
|
/* applyModifierEM */ NULL,
|
|
|
|
|
2010-04-11 22:12:30 +00:00
|
|
|
/* initData */ initData,
|
2011-03-05 10:29:10 +00:00
|
|
|
/* requiredDataMask */ NULL,
|
2010-04-11 22:12:30 +00:00
|
|
|
/* freeData */ freeData,
|
|
|
|
/* isDisabled */ isDisabled,
|
Depsgraph: New dependency graph integration commit
This commit integrates the work done so far on the new dependency graph system,
where goal was to replace legacy depsgraph with the new one, supporting loads of
neat features like:
- More granular dependency relation nature, which solves issues with fake cycles
in the dependencies.
- Move towards all-animatable, by better integration of drivers into the system.
- Lay down some basis for upcoming copy-on-write, overrides and so on.
The new system is living side-by-side with the previous one and disabled by
default, so nothing will become suddenly broken. The way to enable new depsgraph
is to pass `--new-depsgraph` command line argument.
It's a bit early to consider the system production-ready, there are some TODOs
and issues were discovered during the merge period, they'll be addressed ASAP.
But it's important to merge, because it's the only way to attract artists to
really start testing this system.
There are number of assorted documents related on the design of the new system:
* http://wiki.blender.org/index.php/User:Aligorith/GSoC2013_Depsgraph#Design_Documents
* http://wiki.blender.org/index.php/User:Nazg-gul/DependencyGraph
There are also some user-related information online:
* http://code.blender.org/2015/02/blender-dependency-graph-branch-for-users/
* http://code.blender.org/2015/03/more-dependency-graph-tricks/
Kudos to everyone who was involved into the project:
- Joshua "Aligorith" Leung -- design specification, initial code
- Lukas "lukas_t" Toenne -- integrating code into blender, with further fixes
- Sergey "Sergey" "Sharybin" -- some mocking around, trying to wrap up the
project and so
- Bassam "slikdigit" Kurdali -- stressing the new system, reporting all the
issues and recording/writing documentation.
- Everyone else who i forgot to mention here :)
2015-05-12 15:05:57 +05:00
|
|
|
/* updateDepsgraph */ NULL,
|
2011-03-05 10:29:10 +00:00
|
|
|
/* dependsOnTime */ NULL,
|
2015-08-05 19:19:05 +02:00
|
|
|
/* dependsOnNormals */ dependsOnNormals,
|
2011-03-05 10:29:10 +00:00
|
|
|
/* foreachObjectLink */ NULL,
|
|
|
|
/* foreachIDLink */ NULL,
|
2011-08-12 18:11:22 +00:00
|
|
|
/* foreachTexLink */ NULL,
|
2010-04-11 22:12:30 +00:00
|
|
|
};
|