Blender V5.3
extract_mesh_corner_gpu.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_math_base_c.hh"
10
11#include "GPU_capabilities.hh"
12#include "GPU_compute.hh"
13#include "GPU_shader.hh"
14#include "GPU_state.hh"
15
16#include "draw_defines.hh"
17#include "draw_shader.hh"
18
19#include "extract_mesh.hh"
20
21namespace blender::draw {
22
23template<typename GetIndex>
25{
27 MutableSpan<int> data = buf->data<int>();
28 threading::parallel_for(IndexRange(bm.totface), 2048, [&](const IndexRange range) {
29 for (const int face_index : range) {
30 const BMFace &face = *BM_face_at_index(&const_cast<BMesh &>(bm), face_index);
31 const BMLoop *loop = BM_FACE_FIRST_LOOP(&face);
32 for ([[maybe_unused]] const int i : IndexRange(face.len)) {
33 data[BM_elem_index_get(loop)] = get_index(*loop);
34 loop = loop->next;
35 }
36 }
37 });
38 return buf;
39}
40
42{
43 if (!cache.corner_verts) {
46 }
47 else {
49 *mr.bm, [](const BMLoop &loop) { return BM_elem_index_get(loop.v); });
50 }
51 }
52 return *cache.corner_verts;
53}
54
56{
57 if (!cache.corner_edges) {
60 }
61 else {
63 *mr.bm, [](const BMLoop &loop) { return BM_elem_index_get(loop.e); });
64 }
65 }
66 return *cache.corner_edges;
67}
68
70{
71 if (cache.face_offsets) {
72 return *cache.face_offsets;
73 }
75 cache.face_offsets = gpu::VertBuf::from_span(mr.faces.data().cast<uint>());
76 return *cache.face_offsets;
77 }
78 const BMesh &bm = *mr.bm;
80 MutableSpan<int> data = cache.face_offsets->data<int>();
81 threading::parallel_for(IndexRange(bm.totface), 4096, [&](const IndexRange range) {
82 for (const int face_index : range) {
83 const BMFace &face = *BM_face_at_index(&const_cast<BMesh &>(bm), face_index);
84 data[face_index] = BM_elem_index_get(BM_FACE_FIRST_LOOP(&face));
85 }
86 });
88 return *cache.face_offsets;
89}
90
91static void dispatch_batched(gpu::Shader *shader, const int size)
92{
93 const int64_t max_batch_size = int64_t(GPU_max_work_group_count(0)) *
95 int start = 0;
96 while (start < size) {
97 const int batch_size = std::min(int64_t(size - start), max_batch_size);
98 GPU_shader_uniform_1i(shader, "start", start);
99 GPU_shader_uniform_1i(shader, "count", batch_size);
101 start += batch_size;
102 }
103}
104
105static int element_size(const gpu::VertBuf &src, const gpu::VertBuf &dst)
106{
107 const int size = GPU_vertbuf_get_format(&dst)->stride;
108 BLI_assert(GPU_vertbuf_get_format(&src)->stride == size);
110 return size;
111}
112
128
130 MeshBufferCache &cache,
131 gpu::VertBuf &src,
132 gpu::VertBuf &dst)
133{
134 if (mr.corners_num == 0) {
135 return;
136 }
138 gather_to_corner_gpu(mr, src, corner_verts_ensure(mr, cache), dst);
139}
140
142 MeshBufferCache &cache,
143 gpu::VertBuf &src,
144 gpu::VertBuf &dst)
145{
146 if (mr.corners_num == 0) {
147 return;
148 }
150 gather_to_corner_gpu(mr, src, corner_edges_ensure(mr, cache), dst);
151}
152
172
173} // namespace blender::draw
#define UNUSED_VARS_NDEBUG(...)
constexpr IndexRange()=default
static VertBufPtr from_size(const int size, GPUUsageType usage=GPU_USAGE_STATIC)
static VertBufPtr from_span(const Span< T > data)
#define DRW_MESH_TO_CORNER_GROUP_SIZE
Extraction of Mesh data into VBO to feed to GPU.
void scatter_face_to_corner_gpu(const MeshRenderData &mr, MeshBufferCache &cache, gpu::VertBuf &src, gpu::VertBuf &dst)
static gpu::VertBuf & face_offsets_ensure(const MeshRenderData &mr, MeshBufferCache &cache)
static int element_size(const gpu::VertBuf &src, const gpu::VertBuf &dst)
static gpu::VertBuf & corner_edges_ensure(const MeshRenderData &mr, MeshBufferCache &cache)
void gather_edge_to_corner_gpu(const MeshRenderData &mr, MeshBufferCache &cache, gpu::VertBuf &src, gpu::VertBuf &dst)
static void dispatch_batched(gpu::Shader *shader, const int size)
static void gather_to_corner_gpu(const MeshRenderData &mr, gpu::VertBuf &src, gpu::VertBuf &indices, gpu::VertBuf &dst)
void gather_vert_to_corner_gpu(const MeshRenderData &mr, MeshBufferCache &cache, gpu::VertBuf &src, gpu::VertBuf &dst)
static gpu::VertBuf & corner_verts_ensure(const MeshRenderData &mr, MeshBufferCache &cache)
static gpu::VertBufPtr bm_corner_indices_create(const BMesh &bm, const GetIndex get_index)
std::unique_ptr< gpu::VertBuf, gpu::VertBufDeleter > VertBufPtr
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:69
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
unsigned int uint
Definition BKE_text.h:55
gpu::Shader * DRW_shader_mesh_scatter_faces_get(const int element_size)
const GPUVertFormat * GPU_vertbuf_get_format(const gpu::VertBuf *verts)
void GPU_compute_dispatch(gpu::Shader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_vertbuf_bind_as_ssbo(gpu::VertBuf *verts, int binding)
gpu::Shader * DRW_shader_mesh_gather_get(const int element_size)
int GPU_max_work_group_count(int index)
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
BMesh const char void * data
void GPU_shader_unbind()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
@ GPU_BARRIER_VERTEX_ATTRIB_ARRAY
Definition GPU_state.hh:53
@ GPU_BARRIER_SHADER_STORAGE
Definition GPU_state.hh:51
MINLINE uint divide_ceil_u(uint a, uint b)
uint GPU_vertbuf_get_vertex_len(const gpu::VertBuf *verts)
OffsetIndices< int > faces
uint get_index(uint i)