2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-05-25 20:16:42 +10:00
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
|
|
|
|
*
|
|
|
|
|
* The primary purpose of this API is to avoid unnecessary mesh conversion for the final
|
|
|
|
|
* output of a modified mesh.
|
|
|
|
|
*
|
|
|
|
|
* This API handles the case when the modifier stack outputs a mesh which does not have
|
2023-07-24 22:06:55 +02:00
|
|
|
* #Mesh data (#Mesh::faces(), corner verts, corner edges, edges, etc).
|
2020-05-25 20:16:42 +10:00
|
|
|
* Currently this is used so the resulting mesh can have #BMEditMesh data,
|
|
|
|
|
* postponing the converting until it's needed or avoiding conversion entirely
|
|
|
|
|
* which can be an expensive operation.
|
|
|
|
|
* Once converted, the meshes type changes to #ME_WRAPPER_TYPE_MDATA,
|
|
|
|
|
* although the edit mesh is not cleared.
|
|
|
|
|
*
|
|
|
|
|
* This API exposes functions that abstract over the different kinds of internal data,
|
|
|
|
|
* as well as supporting converting the mesh into regular mesh.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
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
|
|
|
#include "DNA_modifier_types.h"
|
2020-05-25 20:16:42 +10:00
|
|
|
#include "DNA_object_types.h"
|
|
|
|
|
|
|
|
|
|
#include "BLI_ghash.h"
|
Cleanup: reduce amount of math-related includes
Using ClangBuildAnalyzer on the whole Blender build, it was pointing
out that BLI_math.h is the heaviest "header hub" (i.e. non tiny file
that is included a lot).
However, there's very little (actually zero) source files in Blender
that need "all the math" (base, colors, vectors, matrices,
quaternions, intersection, interpolation, statistics, solvers and
time). A common use case is source files needing just vectors, or
just vectors & matrices, or just colors etc. Actually, 181 files
were including the whole math thing without needing it at all.
This change removes BLI_math.h completely, and instead in all the
places that need it, includes BLI_math_vector.h or BLI_math_color.h
and so on.
Change from that:
- BLI_math_color.h was included 1399 times -> now 408 (took 114.0sec
to parse -> now 36.3sec)
- BLI_simd.h 1403 -> 418 (109.7sec -> 34.9sec).
Full rebuild of Blender (Apple M1, Xcode, RelWithDebInfo) is not
affected much (342sec -> 334sec). Most of benefit would be when
someone's changing BLI_simd.h or BLI_math_color.h or similar files,
that now there's 3x fewer files result in a recompile.
Pull Request #110944
2023-08-09 11:39:20 +03:00
|
|
|
#include "BLI_math_matrix.h"
|
|
|
|
|
#include "BLI_math_vector.h"
|
2023-06-16 08:08:18 -04:00
|
|
|
#include "BLI_math_vector.hh"
|
2022-04-13 17:51:05 -05:00
|
|
|
#include "BLI_task.hh"
|
2020-07-29 17:36:27 +02:00
|
|
|
#include "BLI_threads.h"
|
2020-05-25 20:16:42 +10:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_DerivedMesh.hh"
|
|
|
|
|
#include "BKE_editmesh.hh"
|
2023-07-07 08:19:52 -04:00
|
|
|
#include "BKE_editmesh_cache.hh"
|
2024-01-15 12:44:04 -05:00
|
|
|
#include "BKE_lib_id.hh"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_mesh_runtime.hh"
|
|
|
|
|
#include "BKE_mesh_wrapper.hh"
|
2023-11-14 09:30:40 +01:00
|
|
|
#include "BKE_modifier.hh"
|
2023-10-09 23:41:53 +02:00
|
|
|
#include "BKE_object.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_subdiv.hh"
|
2023-02-23 17:02:01 -05:00
|
|
|
#include "BKE_subdiv_mesh.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_subdiv_modifier.hh"
|
2020-05-25 20:16:42 +10:00
|
|
|
|
2023-09-22 03:18:17 +02:00
|
|
|
#include "DEG_depsgraph.hh"
|
|
|
|
|
#include "DEG_depsgraph_query.hh"
|
2020-05-25 20:16:42 +10:00
|
|
|
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
using blender::float3;
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
using blender::Span;
|
|
|
|
|
|
2023-07-07 13:07:15 +02:00
|
|
|
Mesh *BKE_mesh_wrapper_from_editmesh(BMEditMesh *em,
|
|
|
|
|
const CustomData_MeshMasks *cd_mask_extra,
|
|
|
|
|
const Mesh *me_settings)
|
2020-05-25 20:16:42 +10:00
|
|
|
{
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(BKE_id_new_nomain(ID_ME, nullptr));
|
|
|
|
|
BKE_mesh_copy_parameters_for_eval(mesh, me_settings);
|
|
|
|
|
BKE_mesh_runtime_ensure_edit_data(mesh);
|
2020-05-25 20:16:42 +10:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
mesh->runtime->wrapper_type = ME_WRAPPER_TYPE_BMESH;
|
2020-05-25 20:16:42 +10:00
|
|
|
if (cd_mask_extra) {
|
2023-12-08 16:40:06 -05:00
|
|
|
mesh->runtime->cd_mask_extra = *cd_mask_extra;
|
2020-05-25 20:16:42 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Use edit-mesh directly where possible. */
|
2023-12-08 16:40:06 -05:00
|
|
|
mesh->runtime->is_original_bmesh = true;
|
2021-09-02 15:39:52 +10:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
mesh->edit_mesh = static_cast<BMEditMesh *>(MEM_dupallocN(em));
|
|
|
|
|
mesh->edit_mesh->is_shallow_copy = true;
|
2020-05-25 20:16:42 +10:00
|
|
|
|
2022-04-13 18:02:21 -05:00
|
|
|
/* Make sure we crash if these are ever used. */
|
2023-12-04 15:13:06 +01:00
|
|
|
#ifndef NDEBUG
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->verts_num = INT_MAX;
|
|
|
|
|
mesh->edges_num = INT_MAX;
|
2023-12-08 16:40:06 -05:00
|
|
|
mesh->faces_num = INT_MAX;
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->corners_num = INT_MAX;
|
2020-05-25 20:16:42 +10:00
|
|
|
#else
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->verts_num = 0;
|
|
|
|
|
mesh->edges_num = 0;
|
2023-12-08 16:40:06 -05:00
|
|
|
mesh->faces_num = 0;
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->corners_num = 0;
|
2020-05-25 20:16:42 +10:00
|
|
|
#endif
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
return mesh;
|
2020-05-25 20:16:42 +10:00
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
void BKE_mesh_wrapper_ensure_mdata(Mesh *mesh)
|
2021-06-22 18:17:48 +02:00
|
|
|
{
|
2023-12-08 16:40:06 -05:00
|
|
|
std::lock_guard lock{mesh->runtime->eval_mutex};
|
|
|
|
|
if (mesh->runtime->wrapper_type == ME_WRAPPER_TYPE_MDATA) {
|
2021-06-22 18:17:48 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Must isolate multithreaded tasks while holding a mutex lock. */
|
2022-04-13 17:51:05 -05:00
|
|
|
blender::threading::isolate_task([&]() {
|
2023-12-08 16:40:06 -05:00
|
|
|
switch (static_cast<eMeshWrapperType>(mesh->runtime->wrapper_type)) {
|
2022-04-13 17:51:05 -05:00
|
|
|
case ME_WRAPPER_TYPE_MDATA:
|
|
|
|
|
case ME_WRAPPER_TYPE_SUBD: {
|
|
|
|
|
break; /* Quiet warning. */
|
|
|
|
|
}
|
|
|
|
|
case ME_WRAPPER_TYPE_BMESH: {
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->verts_num = 0;
|
|
|
|
|
mesh->edges_num = 0;
|
2023-12-08 16:40:06 -05:00
|
|
|
mesh->faces_num = 0;
|
2023-12-20 02:21:48 +01:00
|
|
|
mesh->corners_num = 0;
|
2022-04-13 17:51:05 -05:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
BLI_assert(mesh->edit_mesh != nullptr);
|
|
|
|
|
BLI_assert(mesh->runtime->edit_data != nullptr);
|
2022-04-13 17:51:05 -05:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
BMEditMesh *em = mesh->edit_mesh;
|
2024-01-24 20:38:46 +01:00
|
|
|
BM_mesh_bm_to_me_for_eval(*em->bm, *mesh, &mesh->runtime->cd_mask_extra);
|
2022-04-13 17:51:05 -05:00
|
|
|
|
2023-07-26 00:08:31 -04:00
|
|
|
/* Adding original index layers here assumes that all BMesh Mesh wrappers are created from
|
2022-04-13 17:51:05 -05:00
|
|
|
* original edit mode meshes (the only case where adding original indices makes sense).
|
2023-07-26 00:08:31 -04:00
|
|
|
* If that assumption is broken, the layers might be incorrect because they might not
|
2022-04-13 17:51:05 -05:00
|
|
|
* actually be "original".
|
|
|
|
|
*
|
|
|
|
|
* There is also a performance aspect, where this also assumes that original indices are
|
2023-07-26 00:08:31 -04:00
|
|
|
* always needed when converting a BMesh to a mesh with the mesh wrapper system. That might
|
|
|
|
|
* be wrong, but it's not harmful. */
|
2023-12-08 16:40:06 -05:00
|
|
|
BKE_mesh_ensure_default_orig_index_customdata_no_check(mesh);
|
2022-04-13 17:51:05 -05:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
blender::bke::EditMeshData &edit_data = *mesh->runtime->edit_data;
|
2023-12-04 15:22:00 -05:00
|
|
|
if (!edit_data.vertexCos.is_empty()) {
|
2023-12-08 16:40:06 -05:00
|
|
|
mesh->vert_positions_for_write().copy_from(edit_data.vertexCos);
|
|
|
|
|
mesh->runtime->is_original_bmesh = false;
|
2022-04-13 17:51:05 -05:00
|
|
|
}
|
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
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
if (mesh->runtime->wrapper_type_finalize) {
|
|
|
|
|
BKE_mesh_wrapper_deferred_finalize_mdata(mesh);
|
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
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
mesh->runtime->edit_data.reset();
|
2022-04-13 17:51:05 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 17:25:13 +02:00
|
|
|
/* Keep type assignment last, so that read-only access only uses the mdata code paths after all
|
|
|
|
|
* the underlying data has been initialized. */
|
2023-12-08 16:40:06 -05:00
|
|
|
mesh->runtime->wrapper_type = ME_WRAPPER_TYPE_MDATA;
|
2022-04-13 17:51:05 -05:00
|
|
|
});
|
2020-05-25 20:16:42 +10:00
|
|
|
}
|
|
|
|
|
|
2020-06-10 15:52:08 +10:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Mesh Coordinate Access
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2023-07-07 13:07:15 +02:00
|
|
|
const float (*BKE_mesh_wrapper_vert_coords(const Mesh *mesh))[3]
|
|
|
|
|
{
|
|
|
|
|
switch (mesh->runtime->wrapper_type) {
|
|
|
|
|
case ME_WRAPPER_TYPE_BMESH:
|
2023-07-12 17:09:11 -04:00
|
|
|
if (mesh->runtime->edit_data->vertexCos.is_empty()) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2023-07-10 19:49:54 +02:00
|
|
|
return reinterpret_cast<const float(*)[3]>(mesh->runtime->edit_data->vertexCos.data());
|
2023-07-07 13:07:15 +02:00
|
|
|
case ME_WRAPPER_TYPE_MDATA:
|
|
|
|
|
case ME_WRAPPER_TYPE_SUBD:
|
|
|
|
|
return reinterpret_cast<const float(*)[3]>(mesh->vert_positions().data());
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
const float (*BKE_mesh_wrapper_face_normals(Mesh *mesh))[3]
|
2023-07-07 13:07:15 +02:00
|
|
|
{
|
|
|
|
|
switch (mesh->runtime->wrapper_type) {
|
|
|
|
|
case ME_WRAPPER_TYPE_BMESH:
|
2023-12-04 15:22:00 -05:00
|
|
|
BKE_editmesh_cache_ensure_face_normals(*mesh->edit_mesh, *mesh->runtime->edit_data);
|
2023-07-24 22:06:55 +02:00
|
|
|
if (mesh->runtime->edit_data->faceNos.is_empty()) {
|
2023-07-12 17:09:11 -04:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
2023-07-24 22:06:55 +02:00
|
|
|
return reinterpret_cast<const float(*)[3]>(mesh->runtime->edit_data->faceNos.data());
|
2023-07-07 13:07:15 +02:00
|
|
|
case ME_WRAPPER_TYPE_MDATA:
|
|
|
|
|
case ME_WRAPPER_TYPE_SUBD:
|
2023-07-24 22:06:55 +02:00
|
|
|
return reinterpret_cast<const float(*)[3]>(mesh->face_normals().data());
|
2023-07-07 13:07:15 +02:00
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_mesh_wrapper_tag_positions_changed(Mesh *mesh)
|
|
|
|
|
{
|
|
|
|
|
switch (mesh->runtime->wrapper_type) {
|
|
|
|
|
case ME_WRAPPER_TYPE_BMESH:
|
|
|
|
|
if (mesh->runtime->edit_data) {
|
2023-07-10 19:49:54 +02:00
|
|
|
mesh->runtime->edit_data->vertexNos = {};
|
2023-07-24 22:06:55 +02:00
|
|
|
mesh->runtime->edit_data->faceCos = {};
|
|
|
|
|
mesh->runtime->edit_data->faceNos = {};
|
2023-07-07 13:07:15 +02:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case ME_WRAPPER_TYPE_MDATA:
|
|
|
|
|
case ME_WRAPPER_TYPE_SUBD:
|
2023-12-12 15:38:42 -05:00
|
|
|
mesh->tag_positions_changed();
|
2023-07-07 13:07:15 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
void BKE_mesh_wrapper_vert_coords_copy(const Mesh *mesh, blender::MutableSpan<float3> positions)
|
2020-06-10 15:52:08 +10:00
|
|
|
{
|
2023-12-08 16:40:06 -05:00
|
|
|
switch (mesh->runtime->wrapper_type) {
|
2020-06-10 15:52:08 +10:00
|
|
|
case ME_WRAPPER_TYPE_BMESH: {
|
2023-12-08 16:40:06 -05:00
|
|
|
BMesh *bm = mesh->edit_mesh->bm;
|
|
|
|
|
const blender::bke::EditMeshData &edit_data = *mesh->runtime->edit_data;
|
2023-12-04 15:22:00 -05:00
|
|
|
if (!edit_data.vertexCos.is_empty()) {
|
|
|
|
|
positions.copy_from(edit_data.vertexCos);
|
2020-06-10 15:52:08 +10:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
BMIter iter;
|
|
|
|
|
BMVert *v;
|
|
|
|
|
int i;
|
|
|
|
|
BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
|
2023-11-16 18:29:52 +01:00
|
|
|
copy_v3_v3(positions[i], v->co);
|
2020-06-10 15:52:08 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
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
|
|
|
case ME_WRAPPER_TYPE_MDATA:
|
|
|
|
|
case ME_WRAPPER_TYPE_SUBD: {
|
2023-12-08 16:40:06 -05:00
|
|
|
positions.copy_from(mesh->vert_positions());
|
2020-06-10 15:52:08 +10:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-24 12:38:08 +11:00
|
|
|
BLI_assert_unreachable();
|
2020-06-10 15:52:08 +10:00
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
void BKE_mesh_wrapper_vert_coords_copy_with_mat4(const Mesh *mesh,
|
2020-06-10 15:52:08 +10:00
|
|
|
float (*vert_coords)[3],
|
|
|
|
|
int vert_coords_len,
|
|
|
|
|
const float mat[4][4])
|
|
|
|
|
{
|
2023-12-08 16:40:06 -05:00
|
|
|
switch (mesh->runtime->wrapper_type) {
|
2020-06-10 15:52:08 +10:00
|
|
|
case ME_WRAPPER_TYPE_BMESH: {
|
2023-12-08 16:40:06 -05:00
|
|
|
BMesh *bm = mesh->edit_mesh->bm;
|
2020-06-10 15:52:08 +10:00
|
|
|
BLI_assert(vert_coords_len == bm->totvert);
|
2023-12-08 16:40:06 -05:00
|
|
|
const blender::bke::EditMeshData &edit_data = *mesh->runtime->edit_data;
|
2023-12-04 15:22:00 -05:00
|
|
|
if (!edit_data.vertexCos.is_empty()) {
|
2020-06-10 15:52:08 +10:00
|
|
|
for (int i = 0; i < vert_coords_len; i++) {
|
2023-12-04 15:22:00 -05:00
|
|
|
mul_v3_m4v3(vert_coords[i], mat, edit_data.vertexCos[i]);
|
2020-06-10 15:52:08 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
BMIter iter;
|
|
|
|
|
BMVert *v;
|
|
|
|
|
int i;
|
|
|
|
|
BM_ITER_MESH_INDEX (v, &iter, bm, BM_VERTS_OF_MESH, i) {
|
|
|
|
|
mul_v3_m4v3(vert_coords[i], mat, v->co);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
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
|
|
|
case ME_WRAPPER_TYPE_MDATA:
|
|
|
|
|
case ME_WRAPPER_TYPE_SUBD: {
|
2023-12-20 02:21:48 +01:00
|
|
|
BLI_assert(vert_coords_len == mesh->verts_num);
|
2023-12-08 16:40:06 -05:00
|
|
|
const Span<float3> positions = mesh->vert_positions();
|
2020-06-10 15:52:08 +10:00
|
|
|
for (int i = 0; i < vert_coords_len; i++) {
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
mul_v3_m4v3(vert_coords[i], mat, positions[i]);
|
2020-06-10 15:52:08 +10:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-24 12:38:08 +11:00
|
|
|
BLI_assert_unreachable();
|
2020-06-10 15:52:08 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Mesh Array Length Access
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
int BKE_mesh_wrapper_vert_len(const Mesh *mesh)
|
2020-06-10 15:52:08 +10:00
|
|
|
{
|
2023-12-08 16:40:06 -05:00
|
|
|
switch (mesh->runtime->wrapper_type) {
|
2020-06-10 15:52:08 +10:00
|
|
|
case ME_WRAPPER_TYPE_BMESH:
|
2023-12-08 16:40:06 -05:00
|
|
|
return mesh->edit_mesh->bm->totvert;
|
2020-06-10 15:52:08 +10:00
|
|
|
case ME_WRAPPER_TYPE_MDATA:
|
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
|
|
|
case ME_WRAPPER_TYPE_SUBD:
|
2023-12-20 02:21:48 +01:00
|
|
|
return mesh->verts_num;
|
2020-06-10 15:52:08 +10:00
|
|
|
}
|
2021-03-24 12:38:08 +11:00
|
|
|
BLI_assert_unreachable();
|
2020-06-10 15:52:08 +10:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
int BKE_mesh_wrapper_edge_len(const Mesh *mesh)
|
2020-06-10 15:52:08 +10:00
|
|
|
{
|
2023-12-08 16:40:06 -05:00
|
|
|
switch (mesh->runtime->wrapper_type) {
|
2020-06-10 15:52:08 +10:00
|
|
|
case ME_WRAPPER_TYPE_BMESH:
|
2023-12-08 16:40:06 -05:00
|
|
|
return mesh->edit_mesh->bm->totedge;
|
2020-06-10 15:52:08 +10:00
|
|
|
case ME_WRAPPER_TYPE_MDATA:
|
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
|
|
|
case ME_WRAPPER_TYPE_SUBD:
|
2023-12-20 02:21:48 +01:00
|
|
|
return mesh->edges_num;
|
2020-06-10 15:52:08 +10:00
|
|
|
}
|
2021-03-24 12:38:08 +11:00
|
|
|
BLI_assert_unreachable();
|
2020-06-10 15:52:08 +10:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
int BKE_mesh_wrapper_loop_len(const Mesh *mesh)
|
2020-06-10 15:52:08 +10:00
|
|
|
{
|
2023-12-08 16:40:06 -05:00
|
|
|
switch (mesh->runtime->wrapper_type) {
|
2020-06-10 15:52:08 +10:00
|
|
|
case ME_WRAPPER_TYPE_BMESH:
|
2023-12-08 16:40:06 -05:00
|
|
|
return mesh->edit_mesh->bm->totloop;
|
2020-06-10 15:52:08 +10:00
|
|
|
case ME_WRAPPER_TYPE_MDATA:
|
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
|
|
|
case ME_WRAPPER_TYPE_SUBD:
|
2023-12-20 02:21:48 +01:00
|
|
|
return mesh->corners_num;
|
2020-06-10 15:52:08 +10:00
|
|
|
}
|
2021-03-24 12:38:08 +11:00
|
|
|
BLI_assert_unreachable();
|
2020-06-10 15:52:08 +10:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
int BKE_mesh_wrapper_face_len(const Mesh *mesh)
|
2020-06-10 15:52:08 +10:00
|
|
|
{
|
2023-12-08 16:40:06 -05:00
|
|
|
switch (mesh->runtime->wrapper_type) {
|
2020-06-10 15:52:08 +10:00
|
|
|
case ME_WRAPPER_TYPE_BMESH:
|
2023-12-08 16:40:06 -05:00
|
|
|
return mesh->edit_mesh->bm->totface;
|
2020-06-10 15:52:08 +10:00
|
|
|
case ME_WRAPPER_TYPE_MDATA:
|
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
|
|
|
case ME_WRAPPER_TYPE_SUBD:
|
2023-12-08 16:40:06 -05:00
|
|
|
return mesh->faces_num;
|
2020-06-10 15:52:08 +10:00
|
|
|
}
|
2021-03-24 12:38:08 +11:00
|
|
|
BLI_assert_unreachable();
|
2020-06-10 15:52:08 +10:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
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
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name CPU Subdivision Evaluation
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
static Mesh *mesh_wrapper_ensure_subdivision(Mesh *mesh)
|
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
|
|
|
{
|
2023-12-08 16:40:06 -05:00
|
|
|
SubsurfRuntimeData *runtime_data = (SubsurfRuntimeData *)mesh->runtime->subsurf_runtime_data;
|
2022-06-13 15:21:05 +02:00
|
|
|
if (runtime_data == nullptr || runtime_data->settings.level == 0) {
|
2023-12-08 16:40:06 -05:00
|
|
|
return mesh;
|
2022-06-13 15:21:05 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-11 03:34:16 +01:00
|
|
|
/* Initialize the settings before ensuring the descriptor as this is checked to decide whether
|
|
|
|
|
* subdivision is needed at all, and checking the descriptor status might involve checking if the
|
|
|
|
|
* data is out-of-date, which is a very expensive operation. */
|
|
|
|
|
SubdivToMeshSettings mesh_settings;
|
2022-06-13 15:21:05 +02:00
|
|
|
mesh_settings.resolution = runtime_data->resolution;
|
|
|
|
|
mesh_settings.use_optimal_display = runtime_data->use_optimal_display;
|
2022-01-11 03:34:16 +01:00
|
|
|
|
|
|
|
|
if (mesh_settings.resolution < 3) {
|
2023-12-08 16:40:06 -05:00
|
|
|
return mesh;
|
2022-01-11 03:34:16 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
Subdiv *subdiv = BKE_subsurf_modifier_subdiv_descriptor_ensure(runtime_data, mesh, false);
|
2022-04-13 18:02:21 -05:00
|
|
|
if (subdiv == nullptr) {
|
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
|
|
|
/* Happens on bad topology, but also on empty input mesh. */
|
2023-12-08 16:40:06 -05:00
|
|
|
return mesh;
|
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
|
|
|
}
|
2022-06-24 19:03:05 +02:00
|
|
|
const bool use_clnors = runtime_data->use_loop_normals;
|
2022-04-27 14:03:03 +02:00
|
|
|
if (use_clnors) {
|
|
|
|
|
/* If custom normals are present and the option is turned on calculate the split
|
|
|
|
|
* normals and clear flag so the normals get interpolated to the result mesh. */
|
2023-12-20 02:21:48 +01:00
|
|
|
void *data = CustomData_add_layer(
|
2023-12-19 20:38:59 -05:00
|
|
|
&mesh->corner_data, CD_NORMAL, CD_CONSTRUCT, mesh->corners_num);
|
2023-12-08 16:40:06 -05:00
|
|
|
memcpy(data, mesh->corner_normals().data(), mesh->corner_normals().size_in_bytes());
|
2022-04-27 14:03:03 +02:00
|
|
|
}
|
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
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *subdiv_mesh = BKE_subdiv_to_mesh(subdiv, &mesh_settings, mesh);
|
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
|
|
|
|
2022-04-27 14:03:03 +02:00
|
|
|
if (use_clnors) {
|
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
|
|
|
BKE_mesh_set_custom_normals(subdiv_mesh,
|
|
|
|
|
static_cast<float(*)[3]>(CustomData_get_layer_for_write(
|
2023-12-19 20:38:59 -05:00
|
|
|
&subdiv_mesh->corner_data, CD_NORMAL, mesh->corners_num)));
|
|
|
|
|
CustomData_free_layers(&subdiv_mesh->corner_data, CD_NORMAL, mesh->corners_num);
|
2022-04-27 14:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
2023-02-11 13:19:18 +11:00
|
|
|
if (!ELEM(subdiv, runtime_data->subdiv_cpu, runtime_data->subdiv_gpu)) {
|
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
|
|
|
BKE_subdiv_free(subdiv);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
if (subdiv_mesh != mesh) {
|
|
|
|
|
if (mesh->runtime->mesh_eval != nullptr) {
|
|
|
|
|
BKE_id_free(nullptr, mesh->runtime->mesh_eval);
|
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
|
|
|
}
|
2023-12-08 16:40:06 -05:00
|
|
|
mesh->runtime->mesh_eval = subdiv_mesh;
|
|
|
|
|
mesh->runtime->wrapper_type = ME_WRAPPER_TYPE_SUBD;
|
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
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
return mesh->runtime->mesh_eval;
|
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
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *BKE_mesh_wrapper_ensure_subdivision(Mesh *mesh)
|
2022-02-11 12:32:38 +01:00
|
|
|
{
|
2023-12-08 16:40:06 -05:00
|
|
|
std::lock_guard lock{mesh->runtime->eval_mutex};
|
2022-02-11 12:32:38 +01:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
if (mesh->runtime->wrapper_type == ME_WRAPPER_TYPE_SUBD) {
|
|
|
|
|
return mesh->runtime->mesh_eval;
|
2022-02-11 12:32:38 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-13 17:51:05 -05:00
|
|
|
Mesh *result;
|
2022-02-11 12:32:38 +01:00
|
|
|
|
|
|
|
|
/* Must isolate multithreaded tasks while holding a mutex lock. */
|
2023-12-08 16:40:06 -05:00
|
|
|
blender::threading::isolate_task([&]() { result = mesh_wrapper_ensure_subdivision(mesh); });
|
2022-02-11 12:32:38 +01:00
|
|
|
|
2022-04-13 17:51:05 -05:00
|
|
|
return result;
|
2022-02-11 12:32:38 +01:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
/** \} */
|