[Bf-blender-cvs] [fd061f61c79] master: GPUShaderInterface: Reduce creation time on some drivers.

Clément Foucault noreply at git.blender.org
Thu Jun 4 15:28:49 CEST 2020


Commit: fd061f61c79ecb2d16d61711af4cc777b536bd8e
Author: Clément Foucault
Date:   Thu Jun 4 15:28:35 2020 +0200
Branches: master
https://developer.blender.org/rBfd061f61c79ecb2d16d61711af4cc777b536bd8e

GPUShaderInterface: Reduce creation time on some drivers.

Querying GL_UNIFORM_BLOCK_INDEX seems to be a problem on apple drivers.

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

M	source/blender/gpu/intern/gpu_shader_interface.c

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

diff --git a/source/blender/gpu/intern/gpu_shader_interface.c b/source/blender/gpu/intern/gpu_shader_interface.c
index 0da4067484c..9d9f98c6bb0 100644
--- a/source/blender/gpu/intern/gpu_shader_interface.c
+++ b/source/blender/gpu/intern/gpu_shader_interface.c
@@ -25,6 +25,7 @@
 
 #include "BKE_global.h"
 
+#include "BLI_bitmap.h"
 #include "BLI_math_base.h"
 
 #include "MEM_guardedalloc.h"
@@ -39,7 +40,6 @@
 #include <string.h>
 
 #define DEBUG_SHADER_INTERFACE 0
-#define DEBUG_SHADER_UNIFORMS 0
 
 #if DEBUG_SHADER_INTERFACE
 #  include <stdio.h>
@@ -263,25 +263,26 @@ GPUShaderInterface *GPU_shaderinterface_create(int32_t program)
 
   /* GL_ACTIVE_UNIFORMS lied to us! Remove the UBO uniforms from the total before
    * allocating the uniform array. */
-  GLint *uniforms_block_index = MEM_mallocN(sizeof(GLint) * active_uniform_len, __func__);
-  if (uniform_len > 0) {
-    GLuint *indices = MEM_mallocN(sizeof(GLuint) * active_uniform_len, __func__);
-    for (uint i = 0; i < uniform_len; i++) {
-      indices[i] = i;
-    }
-
-    glGetActiveUniformsiv(
-        program, uniform_len, indices, GL_UNIFORM_BLOCK_INDEX, uniforms_block_index);
-
-    MEM_freeN(indices);
-
-    for (int i = 0; i < active_uniform_len; i++) {
-      /* If GL_UNIFORM_BLOCK_INDEX is not -1 it means the uniform belongs to a UBO. */
-      if (uniforms_block_index[i] != -1) {
-        uniform_len--;
-      }
+  GLint max_ubo_uni_len = 0;
+  for (int i = 0; i < ubo_len; i++) {
+    GLint ubo_uni_len;
+    glGetActiveUniformBlockiv(program, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &ubo_uni_len);
+    max_ubo_uni_len = max_ii(max_ubo_uni_len, ubo_uni_len);
+    uniform_len -= ubo_uni_len;
+  }
+  /* Bit set to true if uniform comes from a uniform block. */
+  BLI_bitmap *uniforms_from_blocks = BLI_BITMAP_NEW(active_uniform_len, __func__);
+  /* Set uniforms from block for exclusion. */
+  GLint *ubo_uni_ids = MEM_mallocN(sizeof(GLint) * max_ubo_uni_len, __func__);
+  for (int i = 0; i < ubo_len; i++) {
+    GLint ubo_uni_len;
+    glGetActiveUniformBlockiv(program, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &ubo_uni_len);
+    glGetActiveUniformBlockiv(program, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, ubo_uni_ids);
+    for (int u = 0; u < ubo_uni_len; u++) {
+      BLI_BITMAP_ENABLE(uniforms_from_blocks, ubo_uni_ids[u]);
     }
   }
+  MEM_freeN(ubo_uni_ids);
 
   uint32_t name_buffer_offset = 0;
   const uint32_t name_buffer_len = attr_len * max_attr_name_len + ubo_len * max_ubo_name_len +
@@ -346,8 +347,7 @@ GPUShaderInterface *GPU_shaderinterface_create(int32_t program)
 
   /* Uniforms */
   for (int i = 0, idx = 0, sampler = 0; i < active_uniform_len; i++) {
-    /* If GL_UNIFORM_BLOCK_INDEX is not -1 it means the uniform belongs to a UBO. */
-    if (uniforms_block_index[i] != -1) {
+    if (BLI_BITMAP_TEST(uniforms_from_blocks, i)) {
       continue;
     }
     char *name = shaderface->name_buffer + name_buffer_offset;
@@ -381,7 +381,7 @@ GPUShaderInterface *GPU_shaderinterface_create(int32_t program)
   shaderface->batches = MEM_callocN(shaderface->batches_len * sizeof(GPUBatch *),
                                     "GPUShaderInterface batches");
 
-  MEM_freeN(uniforms_block_index);
+  MEM_freeN(uniforms_from_blocks);
   MEM_freeN(inputs_tmp);
 
   /* Resize name buffer to save some memory. */



More information about the Bf-blender-cvs mailing list