Mesa (master): mesa: Pull ir_to_mesa' s sampler number fetcher out to shared code.

Eric Anholt anholt at kemper.freedesktop.org
Tue Sep 28 18:38:08 UTC 2010


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

Author: Eric Anholt <eric at anholt.net>
Date:   Tue Sep 28 10:43:56 2010 -0700

mesa: Pull ir_to_mesa's sampler number fetcher out to shared code.

---

 src/mesa/program/ir_to_mesa.cpp |   90 +------------------------
 src/mesa/program/sampler.cpp    |  140 +++++++++++++++++++++++++++++++++++++++
 src/mesa/program/sampler.h      |   29 ++++++++
 src/mesa/sources.mak            |    3 +-
 4 files changed, 175 insertions(+), 87 deletions(-)

diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp
index 93b6c30..aa2add4 100644
--- a/src/mesa/program/ir_to_mesa.cpp
+++ b/src/mesa/program/ir_to_mesa.cpp
@@ -54,6 +54,7 @@ extern "C" {
 #include "program/program.h"
 #include "program/prog_uniform.h"
 #include "program/prog_parameter.h"
+#include "program/sampler.h"
 }
 
 static int swizzle_for_size(int size);
@@ -274,8 +275,6 @@ public:
    GLboolean try_emit_mad(ir_expression *ir,
 			  int mul_operand);
 
-   int get_sampler_uniform_value(ir_dereference *deref);
-
    void *mem_ctx;
 };
 
@@ -1894,89 +1893,6 @@ ir_to_mesa_visitor::visit(ir_call *ir)
    this->result = entry->return_reg;
 }
 
-class get_sampler_name : public ir_hierarchical_visitor
-{
-public:
-   get_sampler_name(ir_to_mesa_visitor *mesa, ir_dereference *last)
-   {
-      this->mem_ctx = mesa->mem_ctx;
-      this->mesa = mesa;
-      this->name = NULL;
-      this->offset = 0;
-      this->last = last;
-   }
-
-   virtual ir_visitor_status visit(ir_dereference_variable *ir)
-   {
-      this->name = ir->var->name;
-      return visit_continue;
-   }
-
-   virtual ir_visitor_status visit_leave(ir_dereference_record *ir)
-   {
-      this->name = talloc_asprintf(mem_ctx, "%s.%s", name, ir->field);
-      return visit_continue;
-   }
-
-   virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
-   {
-      ir_constant *index = ir->array_index->as_constant();
-      int i;
-
-      if (index) {
-	 i = index->value.i[0];
-      } else {
-	 /* GLSL 1.10 and 1.20 allowed variable sampler array indices,
-	  * while GLSL 1.30 requires that the array indices be
-	  * constant integer expressions.  We don't expect any driver
-	  * to actually work with a really variable array index, so
-	  * all that would work would be an unrolled loop counter that ends
-	  * up being constant above.
-	  */
-	 mesa->shader_program->InfoLog =
-	    talloc_asprintf_append(mesa->shader_program->InfoLog,
-				   "warning: Variable sampler array index "
-				   "unsupported.\nThis feature of the language "
-				   "was removed in GLSL 1.20 and is unlikely "
-				   "to be supported for 1.10 in Mesa.\n");
-	 i = 0;
-      }
-      if (ir != last) {
-	 this->name = talloc_asprintf(mem_ctx, "%s[%d]", name, i);
-      } else {
-	 offset = i;
-      }
-      return visit_continue;
-   }
-
-   ir_to_mesa_visitor *mesa;
-   const char *name;
-   void *mem_ctx;
-   int offset;
-   ir_dereference *last;
-};
-
-int
-ir_to_mesa_visitor::get_sampler_uniform_value(ir_dereference *sampler)
-{
-   get_sampler_name getname(this, sampler);
-
-   sampler->accept(&getname);
-
-   GLint index = _mesa_lookup_parameter_index(prog->Parameters, -1,
-					      getname.name);
-
-   if (index < 0) {
-      fail_link(this->shader_program,
-		"failed to find sampler named %s.\n", getname.name);
-      return 0;
-   }
-
-   index += getname.offset;
-
-   return this->prog->Parameters->ParameterValues[index][0];
-}
-
 void
 ir_to_mesa_visitor::visit(ir_texture *ir)
 {
@@ -2076,7 +1992,9 @@ ir_to_mesa_visitor::visit(ir_texture *ir)
    if (ir->shadow_comparitor)
       inst->tex_shadow = GL_TRUE;
 
-   inst->sampler = get_sampler_uniform_value(ir->sampler);
+   inst->sampler = _mesa_get_sampler_uniform_value(ir->sampler,
+						   this->shader_program,
+						   this->prog);
 
    const glsl_type *sampler_type = ir->sampler->type;
 
diff --git a/src/mesa/program/sampler.cpp b/src/mesa/program/sampler.cpp
new file mode 100644
index 0000000..0e58aef
--- /dev/null
+++ b/src/mesa/program/sampler.cpp
@@ -0,0 +1,140 @@
+/*
+ * Copyright (C) 2005-2007  Brian Paul   All Rights Reserved.
+ * Copyright (C) 2008  VMware, Inc.   All Rights Reserved.
+ * Copyright © 2010 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stdio.h>
+
+extern "C" {
+#include "main/compiler.h"
+#include "main/mtypes.h"
+#include "program/prog_parameter.h"
+#include "ir.h"
+#include "ir_visitor.h"
+#include "glsl_types.h"
+}
+
+static void fail_link(struct gl_shader_program *prog, const char *fmt, ...) PRINTFLIKE(2, 3);
+
+static void fail_link(struct gl_shader_program *prog, const char *fmt, ...)
+{
+   va_list args;
+   va_start(args, fmt);
+   prog->InfoLog = talloc_vasprintf_append(prog->InfoLog, fmt, args);
+   va_end(args);
+
+   prog->LinkStatus = GL_FALSE;
+}
+
+class get_sampler_name : public ir_hierarchical_visitor
+{
+public:
+   get_sampler_name(ir_dereference *last,
+		    struct gl_shader_program *shader_program)
+   {
+      this->mem_ctx = talloc_new(NULL);
+      this->shader_program = shader_program;
+      this->name = NULL;
+      this->offset = 0;
+      this->last = last;
+   }
+
+   ~get_sampler_name()
+   {
+      talloc_free(this->mem_ctx);
+   }
+
+   virtual ir_visitor_status visit(ir_dereference_variable *ir)
+   {
+      this->name = ir->var->name;
+      return visit_continue;
+   }
+
+   virtual ir_visitor_status visit_leave(ir_dereference_record *ir)
+   {
+      this->name = talloc_asprintf(mem_ctx, "%s.%s", name, ir->field);
+      return visit_continue;
+   }
+
+   virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
+   {
+      ir_constant *index = ir->array_index->as_constant();
+      int i;
+
+      if (index) {
+	 i = index->value.i[0];
+      } else {
+	 /* GLSL 1.10 and 1.20 allowed variable sampler array indices,
+	  * while GLSL 1.30 requires that the array indices be
+	  * constant integer expressions.  We don't expect any driver
+	  * to actually work with a really variable array index, so
+	  * all that would work would be an unrolled loop counter that ends
+	  * up being constant above.
+	  */
+	 shader_program->InfoLog =
+	    talloc_asprintf_append(shader_program->InfoLog,
+				   "warning: Variable sampler array index "
+				   "unsupported.\nThis feature of the language "
+				   "was removed in GLSL 1.20 and is unlikely "
+				   "to be supported for 1.10 in Mesa.\n");
+	 i = 0;
+      }
+      if (ir != last) {
+	 this->name = talloc_asprintf(mem_ctx, "%s[%d]", name, i);
+      } else {
+	 offset = i;
+      }
+      return visit_continue;
+   }
+
+   struct gl_shader_program *shader_program;
+   const char *name;
+   void *mem_ctx;
+   int offset;
+   ir_dereference *last;
+};
+
+extern "C" {
+int
+_mesa_get_sampler_uniform_value(class ir_dereference *sampler,
+				struct gl_shader_program *shader_program,
+				const struct gl_program *prog)
+{
+   get_sampler_name getname(sampler, shader_program);
+
+   sampler->accept(&getname);
+
+   GLint index = _mesa_lookup_parameter_index(prog->Parameters, -1,
+					      getname.name);
+
+   if (index < 0) {
+      fail_link(shader_program,
+		"failed to find sampler named %s.\n", getname.name);
+      return 0;
+   }
+
+   index += getname.offset;
+
+   return prog->Parameters->ParameterValues[index][0];
+}
+}
diff --git a/src/mesa/program/sampler.h b/src/mesa/program/sampler.h
new file mode 100644
index 0000000..22467e9
--- /dev/null
+++ b/src/mesa/program/sampler.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2005-2007  Brian Paul   All Rights Reserved.
+ * Copyright (C) 2008  VMware, Inc.   All Rights Reserved.
+ * Copyright © 2010 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+int
+_mesa_get_sampler_uniform_value(class ir_dereference *sampler,
+				struct gl_shader_program *shader_program,
+				const struct gl_program *prog);
diff --git a/src/mesa/sources.mak b/src/mesa/sources.mak
index 6f6598d..b2be5ba 100644
--- a/src/mesa/sources.mak
+++ b/src/mesa/sources.mak
@@ -250,7 +250,8 @@ PROGRAM_SOURCES = \
 	program/symbol_table.c
 
 SHADER_CXX_SOURCES = \
-	program/ir_to_mesa.cpp
+	program/ir_to_mesa.cpp \
+	program/sampler.cpp
 
 ASM_C_SOURCES =	\
 	x86/common_x86.c \




More information about the mesa-commit mailing list