summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Arceri <tarceri@itsqueeze.com>2022-02-01 12:57:56 +1100
committerMarge Bot <emma+marge@anholt.net>2022-05-16 03:33:18 +0000
commit23ea24e11fcf54af0912a39401b4f2fc9cabb8c3 (patch)
tree30ce60a85aae8fbfd95d0fcb8e319590acdf068d
parent5d57bd0345bc8f4bbbfd563079247755fa762417 (diff)
glsl/mesa: move parse_program_resource_name() to common linker_util code
This will be shared by a new NIR varying linking pass in following patches but probably fits better here anyway considering its also used by shader_query.cpp Reviewed-by: Marek Olšák <marek.olsak@amd.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/15731>
-rw-r--r--src/compiler/glsl/link_varyings.cpp5
-rw-r--r--src/compiler/glsl/linker.cpp63
-rw-r--r--src/compiler/glsl/linker_util.cpp63
-rw-r--r--src/compiler/glsl/linker_util.h4
-rw-r--r--src/compiler/glsl/program.h4
-rw-r--r--src/mesa/main/shader_query.cpp6
6 files changed, 73 insertions, 72 deletions
diff --git a/src/compiler/glsl/link_varyings.cpp b/src/compiler/glsl/link_varyings.cpp
index 51c97271e53..b7f7c688d06 100644
--- a/src/compiler/glsl/link_varyings.cpp
+++ b/src/compiler/glsl/link_varyings.cpp
@@ -36,6 +36,7 @@
#include "glsl_parser_extras.h"
#include "ir_optimization.h"
#include "linker.h"
+#include "linker_util.h"
#include "link_varyings.h"
#include "main/macros.h"
#include "util/hash_table.h"
@@ -1030,8 +1031,8 @@ tfeedback_decl::init(const struct gl_constants *consts,
/* Parse a declaration. */
const char *base_name_end;
- long subscript = parse_program_resource_name(input, strlen(input),
- &base_name_end);
+ long subscript = link_util_parse_program_resource_name(input, strlen(input),
+ &base_name_end);
this->var_name = ralloc_strndup(mem_ctx, input, base_name_end - input);
if (this->var_name == NULL) {
_mesa_error_no_memory(__func__);
diff --git a/src/compiler/glsl/linker.cpp b/src/compiler/glsl/linker.cpp
index f0ba5c39666..160e3bbf593 100644
--- a/src/compiler/glsl/linker.cpp
+++ b/src/compiler/glsl/linker.cpp
@@ -512,69 +512,6 @@ linker_warning(gl_shader_program *prog, const char *fmt, ...)
}
-/**
- * Given a string identifying a program resource, break it into a base name
- * and an optional array index in square brackets.
- *
- * If an array index is present, \c out_base_name_end is set to point to the
- * "[" that precedes the array index, and the array index itself is returned
- * as a long.
- *
- * If no array index is present (or if the array index is negative or
- * mal-formed), \c out_base_name_end, is set to point to the null terminator
- * at the end of the input string, and -1 is returned.
- *
- * Only the final array index is parsed; if the string contains other array
- * indices (or structure field accesses), they are left in the base name.
- *
- * No attempt is made to check that the base name is properly formed;
- * typically the caller will look up the base name in a hash table, so
- * ill-formed base names simply turn into hash table lookup failures.
- */
-long
-parse_program_resource_name(const GLchar *name,
- const size_t len,
- const GLchar **out_base_name_end)
-{
- /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
- *
- * "When an integer array element or block instance number is part of
- * the name string, it will be specified in decimal form without a "+"
- * or "-" sign or any extra leading zeroes. Additionally, the name
- * string will not include white space anywhere in the string."
- */
-
- *out_base_name_end = name + len;
-
- if (len == 0 || name[len-1] != ']')
- return -1;
-
- /* Walk backwards over the string looking for a non-digit character. This
- * had better be the opening bracket for an array index.
- *
- * Initially, i specifies the location of the ']'. Since the string may
- * contain only the ']' charcater, walk backwards very carefully.
- */
- unsigned i;
- for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
- /* empty */ ;
-
- if ((i == 0) || name[i-1] != '[')
- return -1;
-
- long array_index = strtol(&name[i], NULL, 10);
- if (array_index < 0)
- return -1;
-
- /* Check for leading zero */
- if (name[i] == '0' && name[i+1] != ']')
- return -1;
-
- *out_base_name_end = name + (i - 1);
- return array_index;
-}
-
-
void
link_invalidate_variable_locations(exec_list *ir)
{
diff --git a/src/compiler/glsl/linker_util.cpp b/src/compiler/glsl/linker_util.cpp
index a3b7fe9132c..b9b67555c00 100644
--- a/src/compiler/glsl/linker_util.cpp
+++ b/src/compiler/glsl/linker_util.cpp
@@ -21,6 +21,8 @@
* IN THE SOFTWARE.
*
*/
+#include <ctype.h>
+
#include "glsl_types.h"
#include "linker_util.h"
#include "util/bitscan.h"
@@ -29,6 +31,67 @@
#include "main/shader_types.h"
#include "main/consts_exts.h"
+/**
+ * Given a string identifying a program resource, break it into a base name
+ * and an optional array index in square brackets.
+ *
+ * If an array index is present, \c out_base_name_end is set to point to the
+ * "[" that precedes the array index, and the array index itself is returned
+ * as a long.
+ *
+ * If no array index is present (or if the array index is negative or
+ * mal-formed), \c out_base_name_end, is set to point to the null terminator
+ * at the end of the input string, and -1 is returned.
+ *
+ * Only the final array index is parsed; if the string contains other array
+ * indices (or structure field accesses), they are left in the base name.
+ *
+ * No attempt is made to check that the base name is properly formed;
+ * typically the caller will look up the base name in a hash table, so
+ * ill-formed base names simply turn into hash table lookup failures.
+ */
+long
+link_util_parse_program_resource_name(const GLchar *name, const size_t len,
+ const GLchar **out_base_name_end)
+{
+ /* Section 7.3.1 ("Program Interfaces") of the OpenGL 4.3 spec says:
+ *
+ * "When an integer array element or block instance number is part of
+ * the name string, it will be specified in decimal form without a "+"
+ * or "-" sign or any extra leading zeroes. Additionally, the name
+ * string will not include white space anywhere in the string."
+ */
+
+ *out_base_name_end = name + len;
+
+ if (len == 0 || name[len-1] != ']')
+ return -1;
+
+ /* Walk backwards over the string looking for a non-digit character. This
+ * had better be the opening bracket for an array index.
+ *
+ * Initially, i specifies the location of the ']'. Since the string may
+ * contain only the ']' charcater, walk backwards very carefully.
+ */
+ unsigned i;
+ for (i = len - 1; (i > 0) && isdigit(name[i-1]); --i)
+ /* empty */ ;
+
+ if ((i == 0) || name[i-1] != '[')
+ return -1;
+
+ long array_index = strtol(&name[i], NULL, 10);
+ if (array_index < 0)
+ return -1;
+
+ /* Check for leading zero */
+ if (name[i] == '0' && name[i+1] != ']')
+ return -1;
+
+ *out_base_name_end = name + (i - 1);
+ return array_index;
+}
+
/* Utility methods shared between the GLSL IR and the NIR */
/* From the OpenGL 4.6 specification, 7.3.1.1 Naming Active Resources:
diff --git a/src/compiler/glsl/linker_util.h b/src/compiler/glsl/linker_util.h
index 55a4afad778..dcd8a5af2d9 100644
--- a/src/compiler/glsl/linker_util.h
+++ b/src/compiler/glsl/linker_util.h
@@ -71,6 +71,10 @@ linker_error(struct gl_shader_program *prog, const char *fmt, ...);
void
linker_warning(struct gl_shader_program *prog, const char *fmt, ...);
+long
+link_util_parse_program_resource_name(const GLchar *name, const size_t len,
+ const GLchar **out_base_name_end);
+
bool
link_util_should_add_buffer_variable(struct gl_shader_program *prog,
struct gl_uniform_storage *uniform,
diff --git a/src/compiler/glsl/program.h b/src/compiler/glsl/program.h
index 919ee92f3a6..7eea90d409f 100644
--- a/src/compiler/glsl/program.h
+++ b/src/compiler/glsl/program.h
@@ -48,8 +48,4 @@ extern void
build_program_resource_list(const struct gl_constants *consts,
struct gl_shader_program *shProg);
-extern long
-parse_program_resource_name(const GLchar *name, const size_t len,
- const GLchar **out_base_name_end);
-
#endif /* GLSL_PROGRAM_H */
diff --git a/src/mesa/main/shader_query.cpp b/src/mesa/main/shader_query.cpp
index de86bf144bc..88de7139825 100644
--- a/src/mesa/main/shader_query.cpp
+++ b/src/mesa/main/shader_query.cpp
@@ -35,7 +35,7 @@
#include "main/uniforms.h"
#include "compiler/glsl/glsl_symbol_table.h"
#include "compiler/glsl/ir.h"
-#include "compiler/glsl/program.h"
+#include "compiler/glsl/linker_util.h"
#include "compiler/glsl/string_to_uint_map.h"
#include "util/mesa-sha1.h"
#include "c99_alloca.h"
@@ -619,7 +619,7 @@ valid_array_index(const GLchar *name, int len, unsigned *array_index)
long idx = 0;
const GLchar *out_base_name_end;
- idx = parse_program_resource_name(name, len, &out_base_name_end);
+ idx = link_util_parse_program_resource_name(name, len, &out_base_name_end);
if (idx < 0)
return false;
@@ -641,7 +641,7 @@ search_resource_hash(struct gl_shader_program *shProg,
return NULL;
const char *base_name_end;
- long index = parse_program_resource_name(name, len, &base_name_end);
+ long index = link_util_parse_program_resource_name(name, len, &base_name_end);
char *name_copy;
/* If dealing with array, we need to get the basename. */