diff options
author | Clément Foucault <foucault.clem@gmail.com> | 2018-03-10 01:50:30 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2018-03-10 04:18:25 +0300 |
commit | 41abbc271c58cdbd24a4e919c102e6bd1c7b64b3 (patch) | |
tree | 5a58e94486b95db96049c5af4ccdee890ebafdaf /source/blender/draw | |
parent | dfd8a52cd2e83b350e75cf69ee96d347e921ce26 (diff) |
DRW: Change UBOs binding logic.
Use the same logic than textures. Also reset bindings only on shader changes.
Diffstat (limited to 'source/blender/draw')
-rw-r--r-- | source/blender/draw/intern/draw_manager.c | 8 | ||||
-rw-r--r-- | source/blender/draw/intern/draw_manager.h | 2 | ||||
-rw-r--r-- | source/blender/draw/intern/draw_manager_exec.c | 36 |
3 files changed, 37 insertions, 9 deletions
diff --git a/source/blender/draw/intern/draw_manager.c b/source/blender/draw/intern/draw_manager.c index 703aad2ec39..2f8cf04f476 100644 --- a/source/blender/draw/intern/draw_manager.c +++ b/source/blender/draw/intern/draw_manager.c @@ -438,6 +438,12 @@ static void drw_viewport_var_init(void) if (DST.RST.bound_tex_slots == NULL) { DST.RST.bound_tex_slots = MEM_callocN(sizeof(bool) * GPU_max_textures(), "Bound Texture Slots"); } + if (DST.RST.bound_ubos == NULL) { + DST.RST.bound_ubos = MEM_callocN(sizeof(GPUUniformBuffer *) * GPU_max_ubo_binds(), "Bound GPUUniformBuffer refs"); + } + if (DST.RST.bound_ubo_slots == NULL) { + DST.RST.bound_ubo_slots = MEM_callocN(sizeof(bool) * GPU_max_textures(), "Bound Ubo Slots"); + } if (view_ubo == NULL) { view_ubo = DRW_uniformbuffer_create(sizeof(ViewUboStorage), NULL); @@ -1948,6 +1954,8 @@ void DRW_engines_free(void) MEM_SAFE_FREE(DST.RST.bound_texs); MEM_SAFE_FREE(DST.RST.bound_tex_slots); + MEM_SAFE_FREE(DST.RST.bound_ubos); + MEM_SAFE_FREE(DST.RST.bound_ubo_slots); DRW_opengl_context_disable(); diff --git a/source/blender/draw/intern/draw_manager.h b/source/blender/draw/intern/draw_manager.h index 689b19a81ff..bde6a4ab24d 100644 --- a/source/blender/draw/intern/draw_manager.h +++ b/source/blender/draw/intern/draw_manager.h @@ -336,6 +336,8 @@ typedef struct DRWManager { GPUTexture **bound_texs; bool *bound_tex_slots; int bind_tex_inc; + GPUUniformBuffer **bound_ubos; + bool *bound_ubo_slots; int bind_ubo_inc; } RST; } DRWManager; diff --git a/source/blender/draw/intern/draw_manager_exec.c b/source/blender/draw/intern/draw_manager_exec.c index f53631e7e54..19b55190e6c 100644 --- a/source/blender/draw/intern/draw_manager_exec.c +++ b/source/blender/draw/intern/draw_manager_exec.c @@ -729,11 +729,20 @@ static void bind_texture(GPUTexture *tex) static void bind_ubo(GPUUniformBuffer *ubo) { - if (DST.RST.bind_ubo_inc < GPU_max_ubo_binds()) { - GPU_uniformbuffer_bind(ubo, DST.RST.bind_ubo_inc); - DST.RST.bind_ubo_inc++; - } - else { + int bind_num = GPU_uniformbuffer_bindpoint(ubo); + if (bind_num == -1) { + for (int i = 0; i < GPU_max_ubo_binds(); ++i) { + DST.RST.bind_ubo_inc = (DST.RST.bind_ubo_inc + 1) % GPU_max_ubo_binds(); + if (DST.RST.bound_ubo_slots[DST.RST.bind_ubo_inc] == false) { + if (DST.RST.bound_ubos[DST.RST.bind_ubo_inc] != NULL) { + GPU_uniformbuffer_unbind(DST.RST.bound_ubos[DST.RST.bind_ubo_inc]); + } + GPU_uniformbuffer_bind(ubo, DST.RST.bind_ubo_inc); + DST.RST.bound_ubos[DST.RST.bind_ubo_inc] = ubo; + DST.RST.bound_ubo_slots[DST.RST.bind_ubo_inc] = true; + return; + } + } /* This is not depending on user input. * It is our responsability to make sure there enough slots. */ BLI_assert(0 && "Not enough ubo slots! This should not happen!\n"); @@ -741,6 +750,7 @@ static void bind_ubo(GPUUniformBuffer *ubo) /* printf so user can report bad behaviour */ printf("Not enough ubo slots! This should not happen!\n"); } + DST.RST.bound_ubo_slots[bind_num] = true; } static void release_texture_slots(void) @@ -750,7 +760,7 @@ static void release_texture_slots(void) static void release_ubo_slots(void) { - DST.RST.bind_ubo_inc = 0; + memset(DST.RST.bound_ubo_slots, 0x0, sizeof(bool) * GPU_max_textures()); } static void draw_shgroup(DRWShadingGroup *shgroup, DRWState pass_state) @@ -766,10 +776,10 @@ static void draw_shgroup(DRWShadingGroup *shgroup, DRWState pass_state) if (DST.shader) GPU_shader_unbind(); GPU_shader_bind(shgroup->shader); DST.shader = shgroup->shader; - } - release_texture_slots(); - release_ubo_slots(); + release_texture_slots(); + release_ubo_slots(); + } drw_state_set((pass_state & shgroup->state_extra_disable) | shgroup->state_extra); drw_stencil_set(shgroup->stencil_mask); @@ -1000,6 +1010,14 @@ static void drw_draw_pass_ex(DRWPass *pass, DRWShadingGroup *start_group, DRWSha } } + /* Clear Bound Ubos */ + for (int i = 0; i < GPU_max_ubo_binds(); i++) { + if (DST.RST.bound_ubos[i] != NULL) { + GPU_uniformbuffer_unbind(DST.RST.bound_ubos[i]); + DST.RST.bound_ubos[i] = NULL; + } + } + if (DST.shader) { GPU_shader_unbind(); DST.shader = NULL; |