Blender V5.0
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.h"
10#include "BLI_math_vector.h"
12#include "BLI_threads.h"
13
14#include "MEM_guardedalloc.h"
15
16#include "GPU_batch.hh"
17#include "GPU_batch_presets.hh" /* Own include. */
18
19/* -------------------------------------------------------------------- */
22
23/* Struct to store 3D Batches and their format */
24static struct {
25 struct {
26 blender::gpu::Batch *sphere_high;
27 blender::gpu::Batch *sphere_med;
28 blender::gpu::Batch *sphere_low;
29 blender::gpu::Batch *sphere_wire_low;
30 blender::gpu::Batch *sphere_wire_med;
32
34
35 struct {
38
40} g_presets_3d = {{nullptr}};
41
42static struct {
43 struct {
44 blender::gpu::Batch *quad;
45 } batch;
46
48
49 struct {
51 } attr_id;
52} g_presets_2d = {{nullptr}};
53
54static ListBase presets_list = {nullptr, nullptr};
55static ListBase buffer_list = {nullptr, nullptr};
56
58
59/* -------------------------------------------------------------------- */
62
64{
65 if (g_presets_3d.format.attr_len == 0) {
68 format, "pos", blender::gpu::VertAttrType::SFLOAT_32_32_32);
70 format, "nor", blender::gpu::VertAttrType::SFLOAT_32_32_32);
71 }
72 return g_presets_3d.format;
73}
74
76{
77 if (g_presets_2d.format.attr_len == 0) {
80 format, "pos", blender::gpu::VertAttrType::SFLOAT_32_32);
82 format, "color", blender::gpu::VertAttrType::SFLOAT_32_32_32_32);
83 }
84 return g_presets_2d.format;
85}
86
88 GPUVertBufRaw *nor_step,
89 float lat,
90 float lon)
91{
92 float pos[3];
93 pos[0] = sinf(lat) * cosf(lon);
94 pos[1] = cosf(lat);
95 pos[2] = sinf(lat) * sinf(lon);
96 copy_v3_v3(static_cast<float *>(GPU_vertbuf_raw_step(pos_step)), pos);
97 copy_v3_v3(static_cast<float *>(GPU_vertbuf_raw_step(nor_step)), pos);
98}
99blender::gpu::Batch *GPU_batch_preset_sphere(int lod)
100{
101 BLI_assert(lod >= 0 && lod <= 2);
103
104 if (lod == 0) {
105 return g_presets_3d.batch.sphere_low;
106 }
107 if (lod == 1) {
108 return g_presets_3d.batch.sphere_med;
109 }
110
111 return g_presets_3d.batch.sphere_high;
112}
113
114blender::gpu::Batch *GPU_batch_preset_sphere_wire(int lod)
115{
116 BLI_assert(lod >= 0 && lod <= 1);
118
119 if (lod == 0) {
120 return g_presets_3d.batch.sphere_wire_low;
121 }
122
123 return g_presets_3d.batch.sphere_wire_med;
124}
125
127
128/* -------------------------------------------------------------------- */
131
132static blender::gpu::Batch *gpu_batch_sphere(int lat_res, int lon_res)
133{
134 const float lon_inc = 2 * M_PI / lon_res;
135 const float lat_inc = M_PI / lat_res;
136 float lon, lat;
137
139 const uint vbo_len = (lat_res - 1) * lon_res * 6;
140 GPU_vertbuf_data_alloc(*vbo, vbo_len);
141
142 GPUVertBufRaw pos_step, nor_step;
143 GPU_vertbuf_attr_get_raw_data(vbo, g_presets_3d.attr_id.pos, &pos_step);
144 GPU_vertbuf_attr_get_raw_data(vbo, g_presets_3d.attr_id.nor, &nor_step);
145
146 lon = 0.0f;
147 for (int i = 0; i < lon_res; i++, lon += lon_inc) {
148 lat = 0.0f;
149 for (int j = 0; j < lat_res; j++, lat += lat_inc) {
150 if (j != lat_res - 1) { /* Pole */
151 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon + lon_inc);
152 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon);
153 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat, lon);
154 }
155
156 if (j != 0) { /* Pole */
157 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat, lon + lon_inc);
158 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon + lon_inc);
159 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat, lon);
160 }
161 }
162 }
163
164 BLI_assert(vbo_len == GPU_vertbuf_raw_used(&pos_step));
165 BLI_assert(vbo_len == GPU_vertbuf_raw_used(&nor_step));
166
168}
169
170static blender::gpu::Batch *batch_sphere_wire(int lat_res, int lon_res)
171{
172 const float lon_inc = 2 * M_PI / lon_res;
173 const float lat_inc = M_PI / lat_res;
174 float lon, lat;
175
177 const uint vbo_len = (lat_res * lon_res * 2) + ((lat_res - 1) * lon_res * 2);
178 GPU_vertbuf_data_alloc(*vbo, vbo_len);
179
180 GPUVertBufRaw pos_step, nor_step;
181 GPU_vertbuf_attr_get_raw_data(vbo, g_presets_3d.attr_id.pos, &pos_step);
182 GPU_vertbuf_attr_get_raw_data(vbo, g_presets_3d.attr_id.nor, &nor_step);
183
184 lon = 0.0f;
185 for (int i = 0; i < lon_res; i++, lon += lon_inc) {
186 lat = 0.0f;
187 for (int j = 0; j < lat_res; j++, lat += lat_inc) {
188 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon);
189 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat, lon);
190
191 if (j != lat_res - 1) { /* Pole */
192 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon + lon_inc);
193 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon);
194 }
195 }
196 }
197
198 BLI_assert(vbo_len == GPU_vertbuf_raw_used(&pos_step));
199 BLI_assert(vbo_len == GPU_vertbuf_raw_used(&nor_step));
200
202}
203
204blender::gpu::Batch *GPU_batch_preset_quad()
205{
206 if (!g_presets_2d.batch.quad) {
208 GPU_vertbuf_data_alloc(*vbo, 4);
209
210 float pos_data[4][2] = {{0.0f, 0.0f}, {0.0f, 1.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}};
211 GPU_vertbuf_attr_fill(vbo, g_presets_2d.attr_id.pos, pos_data);
212 /* Don't fill the color. */
213
216
218 }
219 return g_presets_2d.batch.quad;
220}
221
223
224/* -------------------------------------------------------------------- */
227
229{
231
232 /* Hard coded resolution */
233 g_presets_3d.batch.sphere_low = gpu_batch_sphere(8, 16);
235
236 g_presets_3d.batch.sphere_med = gpu_batch_sphere(16, 10);
238
239 g_presets_3d.batch.sphere_high = gpu_batch_sphere(32, 24);
240 gpu_batch_presets_register(g_presets_3d.batch.sphere_high);
241
242 g_presets_3d.batch.sphere_wire_low = batch_sphere_wire(6, 8);
243 gpu_batch_presets_register(g_presets_3d.batch.sphere_wire_low);
244
245 g_presets_3d.batch.sphere_wire_med = batch_sphere_wire(8, 16);
246 gpu_batch_presets_register(g_presets_3d.batch.sphere_wire_med);
247}
248
249void gpu_batch_presets_register(blender::gpu::Batch *preset_batch)
250{
254}
255
262
264{
265 while (LinkData *link = static_cast<LinkData *>(BLI_pophead(&presets_list))) {
266 blender::gpu::Batch *preset = static_cast<blender::gpu::Batch *>(link->data);
267 GPU_batch_discard(preset);
268 MEM_freeN(link);
269 }
270
271 while (LinkData *link = static_cast<LinkData *>(BLI_pophead(&buffer_list))) {
272 blender::gpu::StorageBuf *preset = static_cast<blender::gpu::StorageBuf *>(link->data);
273 GPU_storagebuf_free(preset);
274 MEM_freeN(link);
275 }
276
277 /* Reset pointers to null for subsequent initializations after tear-down. */
278 g_presets_2d = {{nullptr}};
279 g_presets_3d = {{nullptr}};
280 presets_list = {nullptr, nullptr};
281
283}
284
#define BLI_assert(a)
Definition BLI_assert.h:46
LinkData * BLI_genericNodeN(void *data)
Definition listbase.cc:922
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:252
#define M_PI
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
void BLI_mutex_end(ThreadMutex *mutex)
Definition threads.cc:360
void BLI_mutex_init(ThreadMutex *mutex)
Definition threads.cc:340
int BLI_thread_is_main(void)
Definition threads.cc:179
void BLI_mutex_lock(ThreadMutex *mutex)
Definition threads.cc:345
void BLI_mutex_unlock(ThreadMutex *mutex)
Definition threads.cc:350
pthread_mutex_t ThreadMutex
Definition BLI_threads.h:79
void GPU_batch_discard(blender::gpu::Batch *batch)
blender::gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, blender::gpu::VertBuf *vertex_buf, blender::gpu::IndexBuf *index_buf, GPUBatchFlag owns_flag)
Definition gpu_batch.cc:51
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:42
@ GPU_PRIM_LINES
@ GPU_PRIM_TRI_STRIP
@ GPU_PRIM_TRIS
void GPU_storagebuf_free(blender::gpu::StorageBuf *ssbo)
void GPU_vertbuf_attr_get_raw_data(blender::gpu::VertBuf *, uint a_idx, GPUVertBufRaw *access)
GPU_INLINE void * GPU_vertbuf_raw_step(GPUVertBufRaw *a)
void GPU_vertbuf_attr_fill(blender::gpu::VertBuf *, uint a_idx, const void *data)
static blender::gpu::VertBuf * GPU_vertbuf_create_with_format(const GPUVertFormat &format)
GPU_INLINE uint GPU_vertbuf_raw_used(const GPUVertBufRaw *a)
void GPU_vertbuf_data_alloc(blender::gpu::VertBuf &verts, uint v_len)
uint GPU_vertformat_attr_add(GPUVertFormat *format, blender::StringRef name, blender::gpu::VertAttrType type)
Read Guarded memory(de)allocation.
static blender::gpu::Batch * batch_sphere_wire(int lat_res, int lon_res)
blender::gpu::Batch * sphere_high
struct @021025263243242147216143265077100330027142264337::@225245033123204053237120173316075113304004012000 batch
static ListBase presets_list
blender::gpu::Batch * GPU_batch_preset_quad()
static GPUVertFormat & preset_3d_format()
uint pos
static GPUVertFormat & preset_2d_format()
void gpu_batch_presets_register(blender::gpu::Batch *preset_batch)
ThreadMutex mutex
void gpu_batch_presets_init()
static void batch_sphere_lat_lon_vert(GPUVertBufRaw *pos_step, GPUVertBufRaw *nor_step, float lat, float lon)
blender::gpu::Batch * sphere_wire_low
static ListBase buffer_list
void gpu_batch_storage_buffer_register(blender::gpu::StorageBuf *preset_buffer)
static blender::gpu::Batch * gpu_batch_sphere(int lat_res, int lon_res)
static struct @021025263243242147216143265077100330027142264337 g_presets_3d
static struct @041311352076166317212073256040344052312232023253 g_presets_2d
blender::gpu::Batch * quad
blender::gpu::Batch * sphere_low
uint nor
uint col
struct @021025263243242147216143265077100330027142264337::@240232116316110053135047106323056371161236243121 attr_id
blender::gpu::Batch * sphere_wire_med
blender::gpu::Batch * GPU_batch_preset_sphere_wire(int lod)
blender::gpu::Batch * GPU_batch_preset_sphere(int lod)
void gpu_batch_presets_exit()
blender::gpu::Batch * sphere_med
format
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
#define sinf
#define cosf
i
Definition text_draw.cc:230