Blender V5.3
gpu_batch_presets.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_listbase.hh"
10#include "BLI_math_vector_c.hh"
12#include "BLI_threads.hh"
13
14#include "MEM_guardedalloc.h"
15
16#include "GPU_batch.hh"
17#include "GPU_batch_presets.hh" /* Own include. */
18
19namespace blender {
20
21/* -------------------------------------------------------------------- */
24
25/* Struct to store 3D Batches and their format */
26static struct {
27 struct {
28 gpu::Batch *sphere_high;
29 gpu::Batch *sphere_med;
30 gpu::Batch *sphere_low;
31 gpu::Batch *sphere_wire_low;
32 gpu::Batch *sphere_wire_med;
33 } batch;
34
36
37 struct {
38 uint pos, nor;
39 } attr_id;
40
42} g_presets_3d = {{nullptr}};
43
44static struct {
45 struct {
46 gpu::Batch *quad;
47 } batch;
48
50
51 struct {
52 uint pos, col;
53 } attr_id;
54} g_presets_2d = {{nullptr}};
55
56static ListBaseT<LinkData> presets_list = {nullptr, nullptr};
57static ListBaseT<LinkData> buffer_list = {nullptr, nullptr};
58
60
61/* -------------------------------------------------------------------- */
64
66{
67 if (g_presets_3d.format.attr_len == 0) {
68 GPUVertFormat *format = &g_presets_3d.format;
70 format, "pos", gpu::VertAttrType::SFLOAT_32_32_32);
72 format, "nor", gpu::VertAttrType::SFLOAT_32_32_32);
73 }
74 return g_presets_3d.format;
75}
76
78{
79 if (g_presets_2d.format.attr_len == 0) {
80 GPUVertFormat *format = &g_presets_2d.format;
82 format, "pos", gpu::VertAttrType::SFLOAT_32_32);
84 format, "color", gpu::VertAttrType::SFLOAT_32_32_32_32);
85 }
86 return g_presets_2d.format;
87}
88
90 GPUVertBufRaw *nor_step,
91 float lat,
92 float lon)
93{
94 float pos[3];
95 pos[0] = sinf(lat) * cosf(lon);
96 pos[1] = cosf(lat);
97 pos[2] = sinf(lat) * sinf(lon);
98 copy_v3_v3(static_cast<float *>(GPU_vertbuf_raw_step(pos_step)), pos);
99 copy_v3_v3(static_cast<float *>(GPU_vertbuf_raw_step(nor_step)), pos);
100}
101gpu::Batch *GPU_batch_preset_sphere(int lod)
102{
103 BLI_assert(lod >= 0 && lod <= 2);
105
106 if (lod == 0) {
107 return g_presets_3d.batch.sphere_low;
108 }
109 if (lod == 1) {
110 return g_presets_3d.batch.sphere_med;
111 }
112
113 return g_presets_3d.batch.sphere_high;
114}
115
117{
118 BLI_assert(lod >= 0 && lod <= 1);
120
121 if (lod == 0) {
122 return g_presets_3d.batch.sphere_wire_low;
123 }
124
125 return g_presets_3d.batch.sphere_wire_med;
126}
127
129
130/* -------------------------------------------------------------------- */
133
134static gpu::Batch *gpu_batch_sphere(int lat_res, int lon_res)
135{
136 const float lon_inc = 2 * M_PI / lon_res;
137 const float lat_inc = M_PI / lat_res;
138 float lon, lat;
139
140 gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(preset_3d_format());
141 const uint vbo_len = (lat_res - 1) * lon_res * 6;
142 GPU_vertbuf_data_alloc(*vbo, vbo_len);
143
144 GPUVertBufRaw pos_step, nor_step;
145 GPU_vertbuf_attr_get_raw_data(vbo, g_presets_3d.attr_id.pos, &pos_step);
146 GPU_vertbuf_attr_get_raw_data(vbo, g_presets_3d.attr_id.nor, &nor_step);
147
148 lon = 0.0f;
149 for (int i = 0; i < lon_res; i++, lon += lon_inc) {
150 lat = 0.0f;
151 for (int j = 0; j < lat_res; j++, lat += lat_inc) {
152 if (j != lat_res - 1) { /* Pole */
153 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon + lon_inc);
154 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon);
155 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat, lon);
156 }
157
158 if (j != 0) { /* Pole */
159 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat, lon + lon_inc);
160 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon + lon_inc);
161 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat, lon);
162 }
163 }
164 }
165
166 BLI_assert(vbo_len == GPU_vertbuf_raw_used(&pos_step));
167 BLI_assert(vbo_len == GPU_vertbuf_raw_used(&nor_step));
168
170}
171
172static gpu::Batch *batch_sphere_wire(int lat_res, int lon_res)
173{
174 const float lon_inc = 2 * M_PI / lon_res;
175 const float lat_inc = M_PI / lat_res;
176 float lon, lat;
177
178 gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(preset_3d_format());
179 const uint vbo_len = (lat_res * lon_res * 2) + ((lat_res - 1) * lon_res * 2);
180 GPU_vertbuf_data_alloc(*vbo, vbo_len);
181
182 GPUVertBufRaw pos_step, nor_step;
183 GPU_vertbuf_attr_get_raw_data(vbo, g_presets_3d.attr_id.pos, &pos_step);
184 GPU_vertbuf_attr_get_raw_data(vbo, g_presets_3d.attr_id.nor, &nor_step);
185
186 lon = 0.0f;
187 for (int i = 0; i < lon_res; i++, lon += lon_inc) {
188 lat = 0.0f;
189 for (int j = 0; j < lat_res; j++, lat += lat_inc) {
190 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon);
191 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat, lon);
192
193 if (j != lat_res - 1) { /* Pole */
194 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon + lon_inc);
195 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon);
196 }
197 }
198 }
199
200 BLI_assert(vbo_len == GPU_vertbuf_raw_used(&pos_step));
201 BLI_assert(vbo_len == GPU_vertbuf_raw_used(&nor_step));
202
204}
205
207{
208 if (!g_presets_2d.batch.quad) {
209 gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(preset_2d_format());
210 GPU_vertbuf_data_alloc(*vbo, 4);
211
212 float pos_data[4][2] = {{0.0f, 0.0f}, {0.0f, 1.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}};
213 GPU_vertbuf_attr_fill(vbo, g_presets_2d.attr_id.pos, pos_data);
214 /* Don't fill the color. */
215
218
220 }
221 return g_presets_2d.batch.quad;
222}
223
225
226/* -------------------------------------------------------------------- */
229
231{
233
234 /* Hard coded resolution */
235 g_presets_3d.batch.sphere_low = gpu_batch_sphere(8, 16);
237
238 g_presets_3d.batch.sphere_med = gpu_batch_sphere(16, 10);
240
241 g_presets_3d.batch.sphere_high = gpu_batch_sphere(32, 24);
242 gpu_batch_presets_register(g_presets_3d.batch.sphere_high);
243
244 g_presets_3d.batch.sphere_wire_low = batch_sphere_wire(6, 8);
245 gpu_batch_presets_register(g_presets_3d.batch.sphere_wire_low);
246
247 g_presets_3d.batch.sphere_wire_med = batch_sphere_wire(8, 16);
248 gpu_batch_presets_register(g_presets_3d.batch.sphere_wire_med);
249}
250
251void gpu_batch_presets_register(gpu::Batch *preset_batch)
252{
256}
257
264
266{
267 while (LinkData *link = static_cast<LinkData *>(BLI_pophead(&presets_list))) {
268 gpu::Batch *preset = static_cast<gpu::Batch *>(link->data);
269 GPU_batch_discard(preset);
270 MEM_delete(link);
271 }
272
273 while (LinkData *link = static_cast<LinkData *>(BLI_pophead(&buffer_list))) {
274 gpu::StorageBuf *preset = static_cast<gpu::StorageBuf *>(link->data);
275 GPU_storagebuf_free(preset);
276 MEM_delete(link);
277 }
278
279 /* Reset pointers to null for subsequent initializations after tear-down. */
280 g_presets_2d = {{nullptr}};
281 g_presets_3d = {{nullptr}};
282 presets_list = {nullptr, nullptr};
283
285}
286
288
289} // namespace blender
#define M_PI
Read Blender memory allocation and freeing.
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
GPU_INLINE uint GPU_vertbuf_raw_used(const GPUVertBufRaw *a)
static GPUVertFormat & preset_2d_format()
gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, gpu::VertBuf *vertex_buf, gpu::IndexBuf *index_buf, GPUBatchFlag owns_flag)
Definition gpu_batch.cc:53
static struct blender::@254231171003143263230320320040336246336164304242 g_presets_2d
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void BLI_mutex_unlock(ThreadMutex *mutex)
Definition threads.cc:353
@ GPU_PRIM_TRI_STRIP
void GPU_batch_discard(gpu::Batch *batch)
static ListBaseT< LinkData > presets_list
GPU_INLINE void * GPU_vertbuf_raw_step(GPUVertBufRaw *a)
void gpu_batch_storage_buffer_register(gpu::StorageBuf *preset_buffer)
static GPUVertFormat & preset_3d_format()
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:259
gpu::Batch * gpu_batch_sphere(int lat_res, int lon_res) ATTR_WARN_UNUSED_RESULT
void BLI_mutex_lock(ThreadMutex *mutex)
Definition threads.cc:348
void gpu_batch_presets_init()
LinkData * BLI_genericNodeN(void *data)
Definition listbase.cc:936
int BLI_thread_is_main()
Definition threads.cc:184
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:44
void gpu_batch_presets_register(gpu::Batch *preset_batch)
static struct blender::@227276277241050127303241112136216361116007216362 g_presets_3d
void GPU_vertbuf_attr_get_raw_data(gpu::VertBuf *, uint a_idx, GPUVertBufRaw *access)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void BLI_mutex_end(ThreadMutex *mutex)
Definition threads.cc:363
static gpu::Batch * batch_sphere_wire(int lat_res, int lon_res)
gpu::Batch * GPU_batch_preset_quad()
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void GPU_vertbuf_attr_fill(gpu::VertBuf *, uint a_idx, const void *data)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
pthread_mutex_t ThreadMutex
void gpu_batch_presets_exit()
gpu::Batch * GPU_batch_preset_sphere(int lod) ATTR_WARN_UNUSED_RESULT
void GPU_storagebuf_free(gpu::StorageBuf *ssbo)
gpu::Batch * GPU_batch_preset_sphere_wire(int lod) ATTR_WARN_UNUSED_RESULT
static void batch_sphere_lat_lon_vert(GPUVertBufRaw *pos_step, GPUVertBufRaw *nor_step, float lat, float lon)
void BLI_mutex_init(ThreadMutex *mutex)
Definition threads.cc:343
static ListBaseT< LinkData > buffer_list
#define sinf
#define cosf
struct blender::@254231171003143263230320320040336246336164304242::@330354346005035103023170064231341273375322222226 attr_id
struct blender::@254231171003143263230320320040336246336164304242::@236350330101024374063302317031045074307346333131 batch
struct blender::@227276277241050127303241112136216361116007216362::@311015206042074154152027234324206265260124165235 attr_id
struct blender::@227276277241050127303241112136216361116007216362::@133302374126343015340100247116332212135374332263 batch