GPUBatch: Change preset managment system.

Now use a list of preset batches with a function to add new ones to this
list.

This removes the need of new functions all over the place to reset/exit.
This commit is contained in:
Clément Foucault 2018-04-05 15:36:38 +02:00
parent d48597eb54
commit cadef79a52
4 changed files with 41 additions and 43 deletions

View File

@ -1168,8 +1168,4 @@ void UI_tooltip_free(struct bContext *C, struct bScreen *sc, struct ARegion *ar)
int UI_calc_float_precision(int prec, double value);
/* UI Batches managment */
void UI_widget_batch_preset_reset(void);
void UI_widget_batch_preset_exit(void);
#endif /* __UI_INTERFACE_H__ */

View File

@ -378,6 +378,7 @@ static Gwn_Batch *ui_batch_roundbox_get(int tria)
roundbox_batch_add_tria(&vflag_step, tria, last_data);
}
g_ui_batch_cache.roundbox[tria] = GWN_batch_create_ex(GWN_PRIM_TRI_STRIP, vbo, NULL, GWN_BATCH_OWNS_VBO);
gpu_batch_presets_register(g_ui_batch_cache.roundbox[tria]);
}
return g_ui_batch_cache.roundbox[tria];
}
@ -387,22 +388,6 @@ static Gwn_Batch *ui_batch_roundbox_get(int tria)
#undef EMBOSS
#undef NO_AA
void UI_widget_batch_preset_reset(void)
{
for (int i = 0; i < ROUNDBOX_TRIA_MAX; ++i) {
if (g_ui_batch_cache.roundbox[i]) {
gwn_batch_vao_cache_clear(g_ui_batch_cache.roundbox[i]);
}
}
}
void UI_widget_batch_preset_exit(void)
{
for (int i = 0; i < ROUNDBOX_TRIA_MAX; ++i) {
GWN_BATCH_DISCARD_SAFE(g_ui_batch_cache.roundbox[i]);
}
}
/* ************************************************* */
void ui_draw_anti_tria(float x1, float y1, float x2, float y2, float x3, float y3,

View File

@ -67,6 +67,7 @@ Gwn_Batch *GPU_batch_preset_sphere(int lod) ATTR_WARN_UNUSED_RESULT;
Gwn_Batch *GPU_batch_preset_sphere_wire(int lod) ATTR_WARN_UNUSED_RESULT;
void gpu_batch_presets_init(void);
void gpu_batch_presets_register(Gwn_Batch *preset_batch);
void gpu_batch_presets_reset(void);
void gpu_batch_presets_exit(void);

View File

@ -32,6 +32,8 @@
#include "BLI_utildefines.h"
#include "BLI_math.h"
#include "BLI_threads.h"
#include "BLI_listbase.h"
#include "MEM_guardedalloc.h"
#include "UI_interface.h"
@ -55,12 +57,22 @@ static struct {
} attr_id;
} g_presets_3d = {{0}};
/* We may want 2D presets later. */
static ListBase presets_list = {NULL, NULL};
/* -------------------------------------------------------------------- */
/** \name 3D Primitives
* \{ */
static Gwn_VertFormat *preset_3D_format(void)
{
if (g_presets_3d.format.attrib_ct == 0) {
Gwn_VertFormat *format = &g_presets_3d.format;
g_presets_3d.attr_id.pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
g_presets_3d.attr_id.nor = GWN_vertformat_attr_add(format, "nor", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
}
return &g_presets_3d.format;
}
static void batch_sphere_lat_lon_vert(
Gwn_VertBufRaw *pos_step, Gwn_VertBufRaw *nor_step,
float lat, float lon)
@ -80,7 +92,7 @@ Gwn_Batch *gpu_batch_sphere(int lat_res, int lon_res)
const float lat_inc = M_PI / lat_res;
float lon, lat;
Gwn_VertBuf *vbo = GWN_vertbuf_create_with_format(&g_presets_3d.format);
Gwn_VertBuf *vbo = GWN_vertbuf_create_with_format(preset_3D_format());
const uint vbo_len = (lat_res - 1) * lon_res * 6;
GWN_vertbuf_data_alloc(vbo, vbo_len);
@ -118,7 +130,7 @@ static Gwn_Batch *batch_sphere_wire(int lat_res, int lon_res)
const float lat_inc = M_PI / lat_res;
float lon, lat;
Gwn_VertBuf *vbo = GWN_vertbuf_create_with_format(&g_presets_3d.format);
Gwn_VertBuf *vbo = GWN_vertbuf_create_with_format(preset_3D_format());
const uint vbo_len = (lat_res * lon_res * 2) + ((lat_res - 1) * lon_res * 2);
GWN_vertbuf_data_alloc(vbo, vbo_len);
@ -180,41 +192,45 @@ Gwn_Batch *GPU_batch_preset_sphere_wire(int lod)
void gpu_batch_presets_init(void)
{
if (g_presets_3d.format.attrib_ct == 0) {
Gwn_VertFormat *format = &g_presets_3d.format;
g_presets_3d.attr_id.pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
g_presets_3d.attr_id.nor = GWN_vertformat_attr_add(format, "nor", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
}
/* Hard coded resolution */
g_presets_3d.batch.sphere_low = gpu_batch_sphere(8, 16);
gpu_batch_presets_register(g_presets_3d.batch.sphere_low);
g_presets_3d.batch.sphere_med = gpu_batch_sphere(16, 10);
gpu_batch_presets_register(g_presets_3d.batch.sphere_med);
g_presets_3d.batch.sphere_high = gpu_batch_sphere(32, 24);
gpu_batch_presets_register(g_presets_3d.batch.sphere_high);
g_presets_3d.batch.sphere_wire_low = batch_sphere_wire(6, 8);
gpu_batch_presets_register(g_presets_3d.batch.sphere_wire_low);
g_presets_3d.batch.sphere_wire_med = batch_sphere_wire(8, 16);
gpu_batch_presets_register(g_presets_3d.batch.sphere_wire_med);
}
void gpu_batch_presets_register(Gwn_Batch *preset_batch)
{
BLI_addtail(&presets_list, BLI_genericNodeN(preset_batch));
}
void gpu_batch_presets_reset(void)
{
/* Reset vao caches for these every time we switch opengl context.
* This way they will draw correctly for each window. */
gwn_batch_vao_cache_clear(g_presets_3d.batch.sphere_low);
gwn_batch_vao_cache_clear(g_presets_3d.batch.sphere_med);
gwn_batch_vao_cache_clear(g_presets_3d.batch.sphere_high);
gwn_batch_vao_cache_clear(g_presets_3d.batch.sphere_wire_low);
gwn_batch_vao_cache_clear(g_presets_3d.batch.sphere_wire_med);
UI_widget_batch_preset_reset();
LinkData *link = presets_list.first;
for (link = presets_list.first; link; link = link->next) {
Gwn_Batch *preset = link->data;
gwn_batch_vao_cache_clear(preset);
}
}
void gpu_batch_presets_exit(void)
{
GWN_batch_discard(g_presets_3d.batch.sphere_low);
GWN_batch_discard(g_presets_3d.batch.sphere_med);
GWN_batch_discard(g_presets_3d.batch.sphere_high);
GWN_batch_discard(g_presets_3d.batch.sphere_wire_low);
GWN_batch_discard(g_presets_3d.batch.sphere_wire_med);
UI_widget_batch_preset_exit();
LinkData *link;
while ((link = BLI_pophead(&presets_list))) {
Gwn_Batch *preset = link->data;
GWN_batch_discard(preset);
MEM_freeN(link);
}
}