Mesa (main): mesa: preparse [ and [0] in gl_resource_name and use it in shader_query.cpp

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Oct 29 12:03:49 UTC 2021


Module: Mesa
Branch: main
Commit: aad903c3f5357697adb4bee82399904d3e78d992
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=aad903c3f5357697adb4bee82399904d3e78d992

Author: Marek Olšák <marek.olsak at amd.com>
Date:   Fri Oct 22 21:02:42 2021 -0400

mesa: preparse [ and [0] in gl_resource_name and use it in shader_query.cpp

strrchr is very expensive here.

Reviewed-by: Timothy Arceri <tarceri at itsqueeze.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/13507>

---

 src/compiler/glsl/ir_uniform.h |  2 ++
 src/compiler/glsl/linker.cpp   | 12 ++++++++++++
 src/mesa/main/shader_query.cpp | 14 +++++---------
 3 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/src/compiler/glsl/ir_uniform.h b/src/compiler/glsl/ir_uniform.h
index 8f91bb0a285..57f77fd3d1b 100644
--- a/src/compiler/glsl/ir_uniform.h
+++ b/src/compiler/glsl/ir_uniform.h
@@ -92,6 +92,8 @@ struct gl_resource_name
 {
    char *string;
    int length;              /* strlen(string) or 0 */
+   int last_square_bracket; /* (strrchr(name, '[') - name) or -1 */
+   bool suffix_is_zero_square_bracketed; /* suffix is [0] */
 };
 
 struct gl_uniform_storage {
diff --git a/src/compiler/glsl/linker.cpp b/src/compiler/glsl/linker.cpp
index 44a710d7ffa..add7887d5c0 100644
--- a/src/compiler/glsl/linker.cpp
+++ b/src/compiler/glsl/linker.cpp
@@ -5043,7 +5043,19 @@ resource_name_updated(struct gl_resource_name *name)
 {
    if (name->string) {
       name->length = strlen(name->string);
+
+      const char *last_square_bracket = strrchr(name->string, '[');
+      if (last_square_bracket) {
+         name->last_square_bracket = last_square_bracket - name->string;
+         name->suffix_is_zero_square_bracketed =
+            strcmp(last_square_bracket, "[0]") == 0;
+      } else {
+         name->last_square_bracket = -1;
+         name->suffix_is_zero_square_bracketed = false;
+      }
    } else {
       name->length = 0;
+      name->last_square_bracket = -1;
+      name->suffix_is_zero_square_bracketed = false;
    }
 }
diff --git a/src/mesa/main/shader_query.cpp b/src/mesa/main/shader_query.cpp
index c89c0094bdc..efa46bef990 100644
--- a/src/mesa/main/shader_query.cpp
+++ b/src/mesa/main/shader_query.cpp
@@ -694,10 +694,8 @@ _mesa_program_resource_find_name(struct gl_shader_program *shProg,
       if (!_mesa_program_get_resource_name(res, &rname))
          continue;
 
-      int length_without_array_index = rname.length;
-      const char *rname_last_square_bracket = strrchr(rname.string, '[');
       bool found = false;
-      bool rname_has_array_index_zero = false;
+
       /* From ARB_program_interface_query spec:
        *
        * "uint GetProgramResourceIndex(uint program, enum programInterface,
@@ -723,12 +721,10 @@ _mesa_program_resource_find_name(struct gl_shader_program *shProg,
        * array's index is zero and the resulting string length is the same
        * than the provided name's length.
        */
-      if (rname_last_square_bracket) {
-         length_without_array_index -= rname_last_square_bracket - rname.string;
-         rname_has_array_index_zero =
-            (strcmp(rname_last_square_bracket, "[0]") == 0) &&
-            (length_without_array_index == len);
-      }
+      int length_without_array_index =
+         rname.last_square_bracket >= 0 ? rname.last_square_bracket : rname.length;
+      bool rname_has_array_index_zero = rname.suffix_is_zero_square_bracketed &&
+                                        rname.last_square_bracket == len;
 
       if (len >= rname.length && strncmp(rname.string, name, rname.length) == 0)
          found = true;



More information about the mesa-commit mailing list