<div dir="ltr">On 11 October 2013 11:18, Ian Romanick <span dir="ltr"><<a href="mailto:idr@freedesktop.org" target="_blank">idr@freedesktop.org</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Ian Romanick <<a href="mailto:ian.d.romanick@intel.com">ian.d.romanick@intel.com</a>><br>
<br>
Checks that the variables generated meet certain criteria.<br>
<br>
 - Vertex shader inputs have an explicit location.<br>
<br>
 - Vertex shader outputs have an explicit location.<br>
<br>
 - Fragment shader-only varying locations are not used.<br>
<br>
 - Vertex shader uniforms and system values don't have an explicit<br>
   location.<br>
<br>
 - Vertex shader constants don't have an explicit location and are<br>
   read-only.<br>
<br>
 - No other kinds of vertex variables exist.<br>
<br>
It does not verify that an specific variables exist.<br>
<br>
Signed-off-by: Ian Romanick <<a href="mailto:ian.d.romanick@intel.com">ian.d.romanick@intel.com</a>><br>
---<br>
 src/glsl/Makefile.am                     |   1 +<br>
 src/glsl/tests/builtin_variable_test.cpp | 235 +++++++++++++++++++++++++++++++<br>
 2 files changed, 236 insertions(+)<br>
 create mode 100644 src/glsl/tests/builtin_variable_test.cpp<br>
<br>
diff --git a/src/glsl/Makefile.am b/src/glsl/Makefile.am<br>
index f43f49d..80949fb 100644<br>
--- a/src/glsl/Makefile.am<br>
+++ b/src/glsl/Makefile.am<br>
@@ -59,6 +59,7 @@ tests_general_ir_test_SOURCES =               \<br>
        $(top_srcdir)/src/mesa/program/prog_hash_table.c\<br>
        $(top_srcdir)/src/mesa/program/symbol_table.c   \<br>
        $(GLSL_SRCDIR)/standalone_scaffolding.cpp \<br>
+       tests/builtin_variable_test.cpp                 \<br>
        tests/general_ir_test.cpp<br>
 tests_general_ir_test_CFLAGS =                         \<br>
        $(PTHREAD_CFLAGS)<br>
diff --git a/src/glsl/tests/builtin_variable_test.cpp b/src/glsl/tests/builtin_variable_test.cpp<br>
new file mode 100644<br>
index 0000000..c4711ab<br>
--- /dev/null<br>
+++ b/src/glsl/tests/builtin_variable_test.cpp<br>
@@ -0,0 +1,235 @@<br>
+/*<br>
+ * Copyright © 2013 Intel Corporation<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice (including the next<br>
+ * paragraph) shall be included in all copies or substantial portions of the<br>
+ * Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER<br>
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING<br>
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER<br>
+ * DEALINGS IN THE SOFTWARE.<br>
+ */<br>
+#include <gtest/gtest.h><br>
+#include "standalone_scaffolding.h"<br>
+#include "main/compiler.h"<br>
+#include "main/mtypes.h"<br>
+#include "main/macros.h"<br>
+#include "ralloc.h"<br>
+#include "ir.h"<br>
+#include "glsl_parser_extras.h"<br>
+#include "glsl_symbol_table.h"<br>
+<br>
+class common_builtin : public ::testing::Test {<br>
+public:<br>
+   common_builtin(GLenum shader_type)<br>
+      : shader_type(shader_type)<br>
+   {<br>
+      /* empty */<br>
+   }<br>
+<br>
+   virtual void SetUp();<br>
+   virtual void TearDown();<br>
+<br>
+   void string_starts_with_prefix(const char *str, const char *prefix);<br>
+   void names_start_with_gl();<br>
+   void uniforms_and_system_values_dont_have_explicit_location();<br>
+   void constants_are_constant();<br>
+   void no_invalid_variable_modes();<br>
+<br>
+   GLenum shader_type;<br>
+   struct _mesa_glsl_parse_state *state;<br>
+   struct gl_shader *shader;<br>
+   void *mem_ctx;<br>
+   gl_context ctx;<br>
+   exec_list ir;<br>
+};<br>
+<br>
+void<br>
+common_builtin::SetUp()<br>
+{<br>
+   this->mem_ctx = ralloc_context(NULL);<br>
+   this->ir.make_empty();<br>
+<br>
+   initialize_context_to_defaults(&this->ctx, API_OPENGL_COMPAT);<br>
+<br>
+   this->shader = rzalloc(this->mem_ctx, gl_shader);<br>
+   this->shader->Type = this->shader_type;<br>
+<br>
+   this->state =<br>
+      new(mem_ctx) _mesa_glsl_parse_state(&this->ctx, this->shader->Type,<br>
+                                          this->shader);<br>
+<br>
+   _mesa_glsl_initialize_types(this->state);<br>
+   _mesa_glsl_initialize_variables(&this->ir, this->state);<br>
+}<br>
+<br>
+void<br>
+common_builtin::TearDown()<br>
+{<br>
+   ralloc_free(this->mem_ctx);<br>
+   this->mem_ctx = NULL;<br>
+}<br>
+<br>
+void<br>
+common_builtin::string_starts_with_prefix(const char *str, const char *prefix)<br>
+{<br>
+   const size_t len = strlen(prefix);<br>
+   char *const name_prefix = new char[len];<br></blockquote><div><br></div><div>This needs to be new char[len + 1], since you assign to its len'th element below.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

+<br>
+   strncpy(name_prefix, str, len);<br>
+   name_prefix[len] = '\0';<br>
+   EXPECT_STREQ(prefix, name_prefix) << "Bad name " << str;<br>
+}<br>
+<br>
+void<br>
+common_builtin::names_start_with_gl()<br>
+{<br>
+   foreach_list(node, &this->ir) {<br>
+      ir_variable *const var = ((ir_instruction *) node)->as_variable();<br>
+<br>
+      string_starts_with_prefix(var->name, "gl_");<br>
+   }<br>
+}<br>
+<br>
+void<br>
+common_builtin::uniforms_and_system_values_dont_have_explicit_location()<br>
+{<br>
+   foreach_list(node, &this->ir) {<br>
+      ir_variable *const var = ((ir_instruction *) node)->as_variable();<br>
+<br>
+      if (var->mode != ir_var_uniform && var->mode != ir_var_system_value)<br>
+         continue;<br>
+<br>
+      EXPECT_FALSE(var->explicit_location);<br>
+      EXPECT_EQ(-1, var->location);<br>
+   }<br>
+}<br>
+<br>
+void<br>
+common_builtin::constants_are_constant()<br>
+{<br>
+   foreach_list(node, &this->ir) {<br>
+      ir_variable *const var = ((ir_instruction *) node)->as_variable();<br>
+<br>
+      if (var->mode != ir_var_auto)<br>
+         continue;<br>
+<br>
+      EXPECT_FALSE(var->explicit_location);<br>
+      EXPECT_EQ(-1, var->location);<br>
+      EXPECT_TRUE(var->read_only);<br>
+   }<br>
+}<br>
+<br>
+void<br>
+common_builtin::no_invalid_variable_modes()<br>
+{<br>
+   foreach_list(node, &this->ir) {<br>
+      ir_variable *const var = ((ir_instruction *) node)->as_variable();<br>
+<br>
+      switch (var->mode) {<br>
+      case ir_var_auto:<br>
+      case ir_var_uniform:<br>
+      case ir_var_shader_in:<br>
+      case ir_var_shader_out:<br>
+      case ir_var_system_value:<br>
+         break;<br>
+<br>
+      /* Invalid modes that we know. */<br>
+      case ir_var_function_in:<br>
+      case ir_var_function_out:<br>
+      case ir_var_function_inout:<br>
+      case ir_var_const_in:<br>
+      case ir_var_temporary:<br>
+         EXPECT_NE(ir_var_function_in, var->mode);<br>
+         EXPECT_NE(ir_var_function_out, var->mode);<br>
+         EXPECT_NE(ir_var_function_inout, var->mode);<br>
+         EXPECT_NE(ir_var_const_in, var->mode);<br>
+         EXPECT_NE(ir_var_temporary, var->mode);<br>
+         break;<br></blockquote><div><br></div><div>This block seems really strange to me.  All of these invalid modes are adequately covered by the default case, which is easier to read.<br><br></div><div>With those two things fixed, this patch is:<br>
<br>Reviewed-by: Paul Berry <<a href="mailto:stereotype441@gmail.com">stereotype441@gmail.com</a>><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

+<br>
+      /* Invalid modes that we don't know. */<br>
+      default:<br>
+         EXPECT_TRUE(false) << "invalid mode " << int(var->mode);<br>
+         break;<br>
+      }<br>
+   }<br>
+}<br>
+<br>
+/************************************************************/<br>
+<br>
+class vertex_builtin : public common_builtin {<br>
+public:<br>
+   vertex_builtin()<br>
+      : common_builtin(GL_VERTEX_SHADER)<br>
+   {<br>
+      /* empty */<br>
+   }<br>
+};<br>
+<br>
+TEST_F(vertex_builtin, names_start_with_gl)<br>
+{<br>
+   common_builtin::names_start_with_gl();<br>
+}<br>
+<br>
+TEST_F(vertex_builtin, inputs_have_explicit_location)<br>
+{<br>
+   foreach_list(node, &this->ir) {<br>
+      ir_variable *const var = ((ir_instruction *) node)->as_variable();<br>
+<br>
+      if (var->mode != ir_var_shader_in)<br>
+         continue;<br>
+<br>
+      EXPECT_TRUE(var->explicit_location);<br>
+      EXPECT_NE(-1, var->location);<br>
+      EXPECT_GT(VERT_ATTRIB_GENERIC0, var->location);<br>
+      EXPECT_EQ(0u, var->location_frac);<br>
+   }<br>
+}<br>
+<br>
+TEST_F(vertex_builtin, outputs_have_explicit_location)<br>
+{<br>
+   foreach_list(node, &this->ir) {<br>
+      ir_variable *const var = ((ir_instruction *) node)->as_variable();<br>
+<br>
+      if (var->mode != ir_var_shader_out)<br>
+         continue;<br>
+<br>
+      EXPECT_TRUE(var->explicit_location);<br>
+      EXPECT_NE(-1, var->location);<br>
+      EXPECT_GT(VARYING_SLOT_VAR0, var->location);<br>
+      EXPECT_EQ(0u, var->location_frac);<br>
+<br>
+      /* Several varyings only exist in the fragment shader.  Be sure that no<br>
+       * outputs with these locations exist.<br>
+       */<br>
+      EXPECT_NE(VARYING_SLOT_PNTC, var->location);<br>
+      EXPECT_NE(VARYING_SLOT_FACE, var->location);<br>
+      EXPECT_NE(VARYING_SLOT_PRIMITIVE_ID, var->location);<br>
+   }<br>
+}<br>
+<br>
+TEST_F(vertex_builtin, uniforms_and_system_values_dont_have_explicit_location)<br>
+{<br>
+   common_builtin::uniforms_and_system_values_dont_have_explicit_location();<br>
+}<br>
+<br>
+TEST_F(vertex_builtin, constants_are_constant)<br>
+{<br>
+   common_builtin::constants_are_constant();<br>
+}<br>
+<br>
+TEST_F(vertex_builtin, no_invalid_variable_modes)<br>
+{<br>
+   common_builtin::no_invalid_variable_modes();<br>
+}<br>
<span class="HOEnZb"><font color="#888888">--<br>
1.8.1.4<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org">mesa-dev@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div>