Blender V5.0
extract_mesh_ibo_tris.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_editmesh.hh"
10
11#include "BLI_math_geom.h"
12
13#include "GPU_index_buffer.hh"
14
15#include "extract_mesh.hh"
16
17#include "draw_subdivision.hh"
18
19namespace blender::draw {
20
22 const SortedFaceData &face_sorted)
23{
24 const Span<int3> corner_tris = mr.mesh->corner_tris();
25 if (!face_sorted.face_tri_offsets) {
26 /* There are no hidden faces and no reordering is necessary to group triangles with the same
27 * material. The corner indices from #Mesh::corner_tris() can be copied directly to the GPU. */
28 BLI_assert(face_sorted.visible_tris_num == corner_tris.size());
30 corner_tris.cast<uint32_t>().data(),
31 corner_tris.size(),
32 0,
33 mr.corners_num,
34 false));
35 }
36
37 const OffsetIndices faces = mr.faces;
38 const Span<bool> hide_poly = mr.hide_poly;
39
40 GPUIndexBufBuilder builder;
43
44 const Span<int> face_tri_offsets = face_sorted.face_tri_offsets->as_span();
45 threading::parallel_for(faces.index_range(), 2048, [&](const IndexRange range) {
46 for (const int face : range) {
47 if (!hide_poly.is_empty() && hide_poly[face]) {
48 continue;
49 }
50 const IndexRange mesh_range = bke::mesh::face_triangles_range(faces, face);
51 const Span<uint3> mesh_tris = corner_tris.slice(mesh_range).cast<uint3>();
52 MutableSpan<uint3> ibo_tris = data.slice(face_tri_offsets[face], mesh_tris.size());
53 ibo_tris.copy_from(mesh_tris);
54 }
55 });
56
57 return gpu::IndexBufPtr(GPU_indexbuf_build_ex(&builder, 0, mr.corners_num, false));
58}
59
61 const SortedFaceData &face_sorted)
62{
63 GPUIndexBufBuilder builder;
66
67 BMesh &bm = *mr.bm;
69 const Span<int> face_tri_offsets = *face_sorted.face_tri_offsets;
70 threading::parallel_for(IndexRange(bm.totface), 1024, [&](const IndexRange range) {
71 for (const int face_index : range) {
72 const BMFace &face = *BM_face_at_index(&bm, face_index);
73 if (BM_elem_flag_test(&face, BM_ELEM_HIDDEN)) {
74 continue;
75 }
76 const int corner_index = BM_elem_index_get(BM_FACE_FIRST_LOOP(&face));
77 const IndexRange bm_tris(poly_to_tri_count(face_index, corner_index),
78 bke::mesh::face_triangles_num(face.len));
79 const IndexRange ibo_tris(face_tri_offsets[face_index], bm_tris.size());
80 for (const int i : bm_tris.index_range()) {
81 data[ibo_tris[i]] = uint3(BM_elem_index_get(looptris[bm_tris[i]][0]),
82 BM_elem_index_get(looptris[bm_tris[i]][1]),
83 BM_elem_index_get(looptris[bm_tris[i]][2]));
84 }
85 }
86 });
87
88 return gpu::IndexBufPtr(GPU_indexbuf_build_ex(&builder, 0, bm.totloop, false));
89}
90
92 gpu::IndexBuf &tris_ibo,
94{
95 /* Create ibo sub-ranges. Always do this to avoid error when the standard surface batch
96 * is created before the surfaces-per-material. */
97 int mat_start = 0;
98 for (const int i : face_sorted.tris_num_by_material.index_range()) {
99 /* These IBOs have not been queried yet but we create them just in case they are needed
100 * later since they are not tracked by mesh_buffer_cache_create_requested(). */
101
102 const int mat_tri_len = face_sorted.tris_num_by_material[i];
103 /* Multiply by 3 because these are triangle indices. */
104 const int start = mat_start * 3;
105 const int len = mat_tri_len * 3;
106 ibos[i] = gpu::IndexBufPtr(GPU_indexbuf_create_subrange(&tris_ibo, start, len));
107 mat_start += mat_tri_len;
108 }
109}
110
112{
114 return extract_tris_mesh(mr, face_sorted);
115 }
116 return extract_tris_bmesh(mr, face_sorted);
117}
118
120{
121 /* Initialize the index buffer, it was already allocated, it will be filled on the device. */
124
125 if (!cache.tris_per_mat.is_empty()) {
126 for (int i = 0; i < cache.mat_len; i++) {
127 /* Multiply by 6 since we have 2 triangles per quad. */
128 const int start = subdiv_cache.mat_start[i] * 6;
129 const int len = (subdiv_cache.mat_end[i] - subdiv_cache.mat_start[i]) * 6;
131 GPU_indexbuf_create_subrange(ibo.get(), start, len));
132 }
133 }
134
135 draw_subdiv_build_tris_buffer(subdiv_cache, ibo.get(), cache.mat_len);
136 return ibo;
137}
138
139} // namespace blender::draw
#define BLI_assert(a)
Definition BLI_assert.h:46
blender::MutableSpan< uint32_t > GPU_indexbuf_get_data(GPUIndexBufBuilder *)
blender::gpu::IndexBuf * GPU_indexbuf_build_on_device(uint index_len)
void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len)
blender::gpu::IndexBuf * GPU_indexbuf_build_ex(GPUIndexBufBuilder *builder, uint index_min, uint index_max, bool uses_restart_indices)
blender::gpu::IndexBuf * GPU_indexbuf_create_subrange(blender::gpu::IndexBuf *elem_src, uint start, uint length)
blender::gpu::IndexBuf * GPU_indexbuf_build_from_memory(GPUPrimType prim_type, const uint32_t *data, int32_t data_len, int32_t index_min, int32_t index_max, bool uses_restart_indices)
@ GPU_PRIM_TRIS
BMesh const char void * data
BMesh * bm
IndexRange index_range() const
Definition BLI_array.hh:360
bool is_empty() const
Definition BLI_array.hh:264
Span< NewT > constexpr cast() const
Definition BLI_span.hh:418
constexpr int64_t size() const
Definition BLI_span.hh:252
Extraction of Mesh data into VBO to feed to GPU.
static char faces[256]
void draw_subdiv_build_tris_buffer(const DRWSubdivCache &cache, gpu::IndexBuf *subdiv_tris, const int material_count)
gpu::IndexBufPtr extract_tris_subdiv(const DRWSubdivCache &subdiv_cache, MeshBatchCache &cache)
static gpu::IndexBufPtr extract_tris_bmesh(const MeshRenderData &mr, const SortedFaceData &face_sorted)
gpu::IndexBufPtr extract_tris(const MeshRenderData &mr, const SortedFaceData &face_sorted)
static gpu::IndexBufPtr extract_tris_mesh(const MeshRenderData &mr, const SortedFaceData &face_sorted)
void create_material_subranges(const SortedFaceData &face_sorted, gpu::IndexBuf &tris_ibo, MutableSpan< gpu::IndexBufPtr > ibos)
std::unique_ptr< IndexBuf, IndexBufDeleter > IndexBufPtr
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:93
VecBase< uint32_t, 3 > uint3
blender::Array< std::array< BMLoop *, 3 > > looptris
Array< gpu::IndexBufPtr > tris_per_mat
OffsetIndices< int > faces
std::optional< Array< int > > face_tri_offsets
i
Definition text_draw.cc:230
uint len