[Bf-blender-cvs] [6a3f2b30d20] master: Fix T72467: Crash when using many (>64) images in a shader

Lukas Stockner noreply at git.blender.org
Mon Dec 16 16:09:24 CET 2019


Commit: 6a3f2b30d206df23120cd212132adea821b6c20e
Author: Lukas Stockner
Date:   Mon Dec 16 15:49:19 2019 +0100
Branches: master
https://developer.blender.org/rB6a3f2b30d206df23120cd212132adea821b6c20e

Fix T72467: Crash when using many (>64) images in a shader

Previously this limit was rather high, but with UDIMs it's fairly easy
to reach this many images. Even though this exceeds the texture limit
on most hardware as far as I can tell, it should at least not crash.

The old code uses a fixed array which overflows eventually, this fix
replaces the array with a GSet.

Reviewed By: fclem

Differential Revision: https://developer.blender.org/D6416

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

M	source/blender/blenlib/BLI_ghash.h
M	source/blender/blenlib/intern/BLI_ghash_utils.c
M	source/blender/gpu/intern/gpu_codegen.c

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

diff --git a/source/blender/blenlib/BLI_ghash.h b/source/blender/blenlib/BLI_ghash.h
index f2c0bf1e6f1..eb926c51ba9 100644
--- a/source/blender/blenlib/BLI_ghash.h
+++ b/source/blender/blenlib/BLI_ghash.h
@@ -406,6 +406,9 @@ GSet *BLI_gset_str_new(const char *info);
 GSet *BLI_gset_pair_new_ex(const char *info, const unsigned int nentries_reserve)
     ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
 GSet *BLI_gset_pair_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
+GSet *BLI_gset_int_new_ex(const char *info,
+                          const unsigned int nentries_reserve) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
+GSet *BLI_gset_int_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT;
 
 /** \} */
 
diff --git a/source/blender/blenlib/intern/BLI_ghash_utils.c b/source/blender/blenlib/intern/BLI_ghash_utils.c
index 1c6484acbdf..63559da5bd7 100644
--- a/source/blender/blenlib/intern/BLI_ghash_utils.c
+++ b/source/blender/blenlib/intern/BLI_ghash_utils.c
@@ -278,4 +278,13 @@ GSet *BLI_gset_pair_new(const char *info)
   return BLI_gset_pair_new_ex(info, 0);
 }
 
+GSet *BLI_gset_int_new_ex(const char *info, const uint nentries_reserve)
+{
+  return BLI_gset_new_ex(BLI_ghashutil_inthash_p, BLI_ghashutil_intcmp, info, nentries_reserve);
+}
+GSet *BLI_gset_int_new(const char *info)
+{
+  return BLI_gset_int_new_ex(info, 0);
+}
+
 /** \} */
diff --git a/source/blender/gpu/intern/gpu_codegen.c b/source/blender/gpu/intern/gpu_codegen.c
index 23ea9a62ef8..ca804a26acd 100644
--- a/source/blender/gpu/intern/gpu_codegen.c
+++ b/source/blender/gpu/intern/gpu_codegen.c
@@ -2131,8 +2131,10 @@ GPUPass *GPU_generate_pass(GPUMaterial *material,
 static int count_active_texture_sampler(GPUShader *shader, char *source)
 {
   char *code = source;
-  int samplers_id[64]; /* Remember this is per stage. */
-  int sampler_len = 0;
+
+  /* Remember this is per stage. */
+  GSet *sampler_ids = BLI_gset_int_new(__func__);
+  int num_samplers = 0;
 
   while ((code = strstr(code, "uniform "))) {
     /* Move past "uniform". */
@@ -2167,22 +2169,16 @@ static int count_active_texture_sampler(GPUShader *shader, char *source)
           continue;
         }
         /* Catch duplicates. */
-        bool is_duplicate = false;
-        for (int i = 0; i < sampler_len; i++) {
-          if (samplers_id[i] == id) {
-            is_duplicate = true;
-          }
-        }
-
-        if (!is_duplicate) {
-          samplers_id[sampler_len] = id;
-          sampler_len++;
+        if (BLI_gset_add(sampler_ids, POINTER_FROM_INT(id))) {
+          num_samplers++;
         }
       }
     }
   }
 
-  return sampler_len;
+  BLI_gset_free(sampler_ids, NULL);
+
+  return num_samplers;
 }
 
 static bool gpu_pass_shader_validate(GPUPass *pass, GPUShader *shader)



More information about the Bf-blender-cvs mailing list