Commit: 41abbc271c58cdbd24a4e919c102e6bd1c7b64b3
Author: Clément Foucault
Date:   Fri Mar 9 23:50:30 2018 +0100
Branches: blender2.8
https://developer.blender.org/rB41abbc271c58cdbd24a4e919c102e6bd1c7b64b3

DRW: Change UBOs binding logic.

Use the same logic than textures. Also reset bindings only on shader changes.

===================================================================

M       source/blender/draw/intern/draw_manager.c
M       source/blender/draw/intern/draw_manager.h
M       source/blender/draw/intern/draw_manager_exec.c
M       source/blender/gpu/intern/gpu_extensions.c

===================================================================

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;
diff --git a/source/blender/gpu/intern/gpu_extensions.c 
b/source/blender/gpu/intern/gpu_extensions.c
index 6bf330179d3..73e86c1b391 100644
--- a/source/blender/gpu/intern/gpu_extensions.c
+++ b/source/blender/gpu/intern/gpu_extensions.c
@@ -149,7 +149,7 @@ void gpu_extensions_init(void)
        else
                GG.max_anisotropy = 1.0f;
 
-       glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, &GG.maxubobinds);
+       glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_BLOCKS, &GG.maxubobinds);
        glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &GG.maxubosize);
 
 #ifndef NDEBUG

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to