diff options
author | Kévin Dietrich <kevin.dietrich@mailoo.org> | 2021-12-27 18:34:47 +0300 |
---|---|---|
committer | Kévin Dietrich <kevin.dietrich@mailoo.org> | 2021-12-27 18:35:54 +0300 |
commit | eed45d2a239a2a18a2420ba15dfb55e0f8dc5630 (patch) | |
tree | aa55ce966caa8e28db4853d7d755003ed249805b /source/blender/draw/intern/draw_cache_extract_mesh.cc | |
parent | 31e120ef4997583332aa9b5af93521e7e666e9f3 (diff) |
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
Diffstat (limited to 'source/blender/draw/intern/draw_cache_extract_mesh.cc')
-rw-r--r-- | source/blender/draw/intern/draw_cache_extract_mesh.cc | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/source/blender/draw/intern/draw_cache_extract_mesh.cc b/source/blender/draw/intern/draw_cache_extract_mesh.cc index 485b803310c..383a3b05b67 100644 --- a/source/blender/draw/intern/draw_cache_extract_mesh.cc +++ b/source/blender/draw/intern/draw_cache_extract_mesh.cc @@ -42,6 +42,7 @@ #include "draw_cache_extract.h" #include "draw_cache_inline.h" +#include "draw_subdivision.h" #include "mesh_extractors/extract_mesh.h" @@ -783,6 +784,99 @@ static void mesh_buffer_cache_create_requested(struct TaskGraph *task_graph, /** \} */ +/* ---------------------------------------------------------------------- */ +/** \name Subdivision Extract Loop + * \{ */ + +static void mesh_buffer_cache_create_requested_subdiv(MeshBatchCache *cache, + MeshBufferCache *mbc, + DRWSubdivCache *subdiv_cache, + const ToolSettings *ts) +{ + /* Create an array containing all the extractors that needs to be executed. */ + ExtractorRunDatas extractors; + + MeshBufferList *mbuflist = &mbc->buff; + +#define EXTRACT_ADD_REQUESTED(type, name) \ + do { \ + if (DRW_##type##_requested(mbuflist->type.name)) { \ + const MeshExtract *extractor = &extract_##name; \ + extractors.append(extractor); \ + } \ + } while (0) + + /* The order in which extractors are added to the list matters somewhat, as some buffers are + * reused when building others. */ + EXTRACT_ADD_REQUESTED(ibo, tris); + EXTRACT_ADD_REQUESTED(vbo, pos_nor); + EXTRACT_ADD_REQUESTED(vbo, lnor); + for (int i = 0; i < GPU_MAX_ATTR; i++) { + EXTRACT_ADD_REQUESTED(vbo, attr[i]); + } + + /* We use only one extractor for face dots, as the work is done in a single compute shader. */ + if (DRW_vbo_requested(mbuflist->vbo.fdots_nor) || DRW_vbo_requested(mbuflist->vbo.fdots_pos) || + DRW_ibo_requested(mbuflist->ibo.fdots)) { + extractors.append(&extract_fdots_pos); + } + + EXTRACT_ADD_REQUESTED(ibo, lines); + EXTRACT_ADD_REQUESTED(ibo, edituv_points); + EXTRACT_ADD_REQUESTED(ibo, edituv_tris); + EXTRACT_ADD_REQUESTED(ibo, edituv_lines); + EXTRACT_ADD_REQUESTED(vbo, vert_idx); + EXTRACT_ADD_REQUESTED(vbo, edge_idx); + EXTRACT_ADD_REQUESTED(vbo, poly_idx); + EXTRACT_ADD_REQUESTED(vbo, edge_fac); + EXTRACT_ADD_REQUESTED(ibo, points); + EXTRACT_ADD_REQUESTED(vbo, edit_data); + EXTRACT_ADD_REQUESTED(vbo, edituv_data); + /* Make sure UVs are computed before edituv stuffs. */ + EXTRACT_ADD_REQUESTED(vbo, uv); + EXTRACT_ADD_REQUESTED(vbo, edituv_stretch_area); + EXTRACT_ADD_REQUESTED(vbo, edituv_stretch_angle); + EXTRACT_ADD_REQUESTED(ibo, lines_adjacency); + EXTRACT_ADD_REQUESTED(vbo, vcol); + EXTRACT_ADD_REQUESTED(vbo, weights); + EXTRACT_ADD_REQUESTED(vbo, sculpt_data); + +#undef EXTRACT_ADD_REQUESTED + + if (extractors.is_empty()) { + return; + } + + MeshRenderData mr; + draw_subdiv_init_mesh_render_data(subdiv_cache, &mr, ts); + mesh_render_data_update_loose_geom(&mr, mbc, MR_ITER_LEDGE | MR_ITER_LVERT, MR_DATA_LOOSE_GEOM); + + void *data_stack = MEM_mallocN(extractors.data_size_total(), __func__); + uint32_t data_offset = 0; + for (const ExtractorRunData &run_data : extractors) { + const MeshExtract *extractor = run_data.extractor; + void *buffer = mesh_extract_buffer_get(extractor, mbuflist); + void *data = POINTER_OFFSET(data_stack, data_offset); + + extractor->init_subdiv(subdiv_cache, &mr, cache, buffer, data); + + if (extractor->iter_subdiv) { + extractor->iter_subdiv(subdiv_cache, &mr, data); + } + + if (extractor->iter_loose_geom_subdiv) { + extractor->iter_loose_geom_subdiv(subdiv_cache, &mr, &mbc->loose_geom, buffer, data); + } + + if (extractor->finish_subdiv) { + extractor->finish_subdiv(subdiv_cache, buffer, data); + } + } + MEM_freeN(data_stack); +} + +/** \} */ + } // namespace blender::draw extern "C" { @@ -818,4 +912,12 @@ void mesh_buffer_cache_create_requested(struct TaskGraph *task_graph, use_hide); } +void mesh_buffer_cache_create_requested_subdiv(MeshBatchCache *cache, + MeshBufferCache *mbc, + DRWSubdivCache *subdiv_cache, + const ToolSettings *ts) +{ + blender::draw::mesh_buffer_cache_create_requested_subdiv(cache, mbc, subdiv_cache, ts); +} + } // extern "C" |