Blender V4.5
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};
55
57
58/* -------------------------------------------------------------------- */
61
63{
64 if (g_presets_3d.format.attr_len == 0) {
70 }
71 return g_presets_3d.format;
72}
73
75{
76 if (g_presets_2d.format.attr_len == 0) {
81 format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
82 }
83 return g_presets_2d.format;
84}
85
87 GPUVertBufRaw *nor_step,
88 float lat,
89 float lon)
90{
91 float pos[3];
92 pos[0] = sinf(lat) * cosf(lon);
93 pos[1] = cosf(lat);
94 pos[2] = sinf(lat) * sinf(lon);
95 copy_v3_v3(static_cast<float *>(GPU_vertbuf_raw_step(pos_step)), pos);
96 copy_v3_v3(static_cast<float *>(GPU_vertbuf_raw_step(nor_step)), pos);
97}
98blender::gpu::Batch *GPU_batch_preset_sphere(int lod)
99{
100 BLI_assert(lod >= 0 && lod <= 2);
102
103 if (lod == 0) {
104 return g_presets_3d.batch.sphere_low;
105 }
106 if (lod == 1) {
107 return g_presets_3d.batch.sphere_med;
108 }
109
110 return g_presets_3d.batch.sphere_high;
111}
112
113blender::gpu::Batch *GPU_batch_preset_sphere_wire(int lod)
114{
115 BLI_assert(lod >= 0 && lod <= 1);
117
118 if (lod == 0) {
119 return g_presets_3d.batch.sphere_wire_low;
120 }
121
122 return g_presets_3d.batch.sphere_wire_med;
123}
124
126
127/* -------------------------------------------------------------------- */
130
131static blender::gpu::Batch *gpu_batch_sphere(int lat_res, int lon_res)
132{
133 const float lon_inc = 2 * M_PI / lon_res;
134 const float lat_inc = M_PI / lat_res;
135 float lon, lat;
136
138 const uint vbo_len = (lat_res - 1) * lon_res * 6;
139 GPU_vertbuf_data_alloc(*vbo, vbo_len);
140
141 GPUVertBufRaw pos_step, nor_step;
142 GPU_vertbuf_attr_get_raw_data(vbo, g_presets_3d.attr_id.pos, &pos_step);
143 GPU_vertbuf_attr_get_raw_data(vbo, g_presets_3d.attr_id.nor, &nor_step);
144
145 lon = 0.0f;
146 for (int i = 0; i < lon_res; i++, lon += lon_inc) {
147 lat = 0.0f;
148 for (int j = 0; j < lat_res; j++, lat += lat_inc) {
149 if (j != lat_res - 1) { /* Pole */
150 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon + lon_inc);
151 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon);
152 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat, lon);
153 }
154
155 if (j != 0) { /* Pole */
156 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat, lon + lon_inc);
157 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon + lon_inc);
158 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat, lon);
159 }
160 }
161 }
162
163 BLI_assert(vbo_len == GPU_vertbuf_raw_used(&pos_step));
164 BLI_assert(vbo_len == GPU_vertbuf_raw_used(&nor_step));
165
167}
168
169static blender::gpu::Batch *batch_sphere_wire(int lat_res, int lon_res)
170{
171 const float lon_inc = 2 * M_PI / lon_res;
172 const float lat_inc = M_PI / lat_res;
173 float lon, lat;
174
176 const uint vbo_len = (lat_res * lon_res * 2) + ((lat_res - 1) * lon_res * 2);
177 GPU_vertbuf_data_alloc(*vbo, vbo_len);
178
179 GPUVertBufRaw pos_step, nor_step;
180 GPU_vertbuf_attr_get_raw_data(vbo, g_presets_3d.attr_id.pos, &pos_step);
181 GPU_vertbuf_attr_get_raw_data(vbo, g_presets_3d.attr_id.nor, &nor_step);
182
183 lon = 0.0f;
184 for (int i = 0; i < lon_res; i++, lon += lon_inc) {
185 lat = 0.0f;
186 for (int j = 0; j < lat_res; j++, lat += lat_inc) {
187 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon);
188 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat, lon);
189
190 if (j != lat_res - 1) { /* Pole */
191 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon + lon_inc);
192 batch_sphere_lat_lon_vert(&pos_step, &nor_step, lat + lat_inc, lon);
193 }
194 }
195 }
196
197 BLI_assert(vbo_len == GPU_vertbuf_raw_used(&pos_step));
198 BLI_assert(vbo_len == GPU_vertbuf_raw_used(&nor_step));
199
201}
202
203blender::gpu::Batch *GPU_batch_preset_quad()
204{
205 if (!g_presets_2d.batch.quad) {
207 GPU_vertbuf_data_alloc(*vbo, 4);
208
209 float pos_data[4][2] = {{0.0f, 0.0f}, {0.0f, 1.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}};
210 GPU_vertbuf_attr_fill(vbo, g_presets_2d.attr_id.pos, pos_data);
211 /* Don't fill the color. */
212
215
217 }
218 return g_presets_2d.batch.quad;
219}
220
222
223/* -------------------------------------------------------------------- */
226
228{
230
231 /* Hard coded resolution */
232 g_presets_3d.batch.sphere_low = gpu_batch_sphere(8, 16);
234
235 g_presets_3d.batch.sphere_med = gpu_batch_sphere(16, 10);
237
238 g_presets_3d.batch.sphere_high = gpu_batch_sphere(32, 24);
239 gpu_batch_presets_register(g_presets_3d.batch.sphere_high);
240
241 g_presets_3d.batch.sphere_wire_low = batch_sphere_wire(6, 8);
242 gpu_batch_presets_register(g_presets_3d.batch.sphere_wire_low);
243
244 g_presets_3d.batch.sphere_wire_med = batch_sphere_wire(8, 16);
245 gpu_batch_presets_register(g_presets_3d.batch.sphere_wire_med);
246}
247
248void gpu_batch_presets_register(blender::gpu::Batch *preset_batch)
249{
253}
254
255bool gpu_batch_presets_unregister(blender::gpu::Batch *preset_batch)
256{
259 if (preset_batch == link->data) {
262 MEM_freeN(link);
263 return true;
264 }
265 }
267 return false;
268}
269
271{
272 while (LinkData *link = static_cast<LinkData *>(BLI_pophead(&presets_list))) {
273 blender::gpu::Batch *preset = static_cast<blender::gpu::Batch *>(link->data);
274 GPU_batch_discard(preset);
275 MEM_freeN(link);
276 }
277
278 /* Reset pointers to null for subsequent initializations after tear-down. */
279 g_presets_2d = {{nullptr}};
280 g_presets_3d = {{nullptr}};
281 presets_list = {nullptr, nullptr};
282
284}
285
#define BLI_assert(a)
Definition BLI_assert.h:46
LinkData * BLI_genericNodeN(void *data)
Definition listbase.cc:922
#define LISTBASE_FOREACH_BACKWARD(type, var, list)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:131
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
blender::gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, blender::gpu::VertBuf *vertex_buf, blender::gpu::IndexBuf *index_buf, eGPUBatchFlag owns_flag)
Definition gpu_batch.cc:51
void GPU_batch_discard(blender::gpu::Batch *batch)
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:41
@ GPU_PRIM_LINES
@ GPU_PRIM_TRI_STRIP
@ GPU_PRIM_TRIS
void GPU_vertbuf_attr_get_raw_data(blender::gpu::VertBuf *, uint a_idx, GPUVertBufRaw *access)
GPU_INLINE void * GPU_vertbuf_raw_step(GPUVertBufRaw *a)
#define GPU_vertbuf_create_with_format(format)
void GPU_vertbuf_attr_fill(blender::gpu::VertBuf *, uint a_idx, const void *data)
GPU_INLINE uint GPU_vertbuf_raw_used(const GPUVertBufRaw *a)
void GPU_vertbuf_data_alloc(blender::gpu::VertBuf &verts, uint v_len)
@ GPU_FETCH_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, blender::StringRef name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
Read Guarded memory(de)allocation.
#define sinf(x)
#define cosf(x)
static blender::gpu::Batch * batch_sphere_wire(int lat_res, int lon_res)
blender::gpu::Batch * sphere_high
static ListBase presets_list
blender::gpu::Batch * GPU_batch_preset_quad()
static GPUVertFormat & preset_3d_format()
uint pos
static GPUVertFormat & preset_2d_format()
bool gpu_batch_presets_unregister(blender::gpu::Batch *preset_batch)
void gpu_batch_presets_register(blender::gpu::Batch *preset_batch)
ThreadMutex mutex
static struct @064345207361167251075330302113175271221317160336 g_presets_3d
void gpu_batch_presets_init()
static void batch_sphere_lat_lon_vert(GPUVertBufRaw *pos_step, GPUVertBufRaw *nor_step, float lat, float lon)
struct @064345207361167251075330302113175271221317160336::@201157344026354305110036153026103256267276205234 attr_id
blender::gpu::Batch * sphere_wire_low
struct @064345207361167251075330302113175271221317160336::@113254110077376341056327177062323111323010325277 batch
static blender::gpu::Batch * gpu_batch_sphere(int lat_res, int lon_res)
blender::gpu::Batch * quad
blender::gpu::Batch * sphere_low
uint nor
static struct @336165176165246216155013102301355235345260256275 g_presets_2d
uint col
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
i
Definition text_draw.cc:230