From cadef79a52cbe3d70488ef5fd70c805a872b17f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Thu, 5 Apr 2018 15:36:38 +0200 Subject: 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. --- source/blender/gpu/intern/gpu_batch_presets.c | 62 +++++++++++++++++---------- 1 file changed, 39 insertions(+), 23 deletions(-) (limited to 'source/blender/gpu/intern') diff --git a/source/blender/gpu/intern/gpu_batch_presets.c b/source/blender/gpu/intern/gpu_batch_presets.c index 144c4a33787..10cbd16490b 100644 --- a/source/blender/gpu/intern/gpu_batch_presets.c +++ b/source/blender/gpu/intern/gpu_batch_presets.c @@ -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); + } } -- cgit v1.2.3