1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright (C) 2005-2007  Brian Paul   All Rights Reserved.
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright (C) 2008  VMware, Inc.   All Rights Reserved.
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright © 2010 Intel Corporation
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the "Software"),
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * to deal in the Software without restriction, including without limitation
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * and/or sell copies of the Software, and to permit persons to whom the
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software is furnished to do so, subject to the following conditions:
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the next
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * paragraph) shall be included in all copies or substantial portions of the
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software.
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * DEALINGS IN THE SOFTWARE.
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir.h"
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "glsl_types.h"
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir_visitor.h"
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "../glsl/program.h"
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "program/hash_table.h"
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir_uniform.h"
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgextern "C" {
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/compiler.h"
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/mtypes.h"
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "program/prog_parameter.h"
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass get_sampler_name : public ir_hierarchical_visitor
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgpublic:
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   get_sampler_name(ir_dereference *last,
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		    struct gl_shader_program *shader_program)
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->mem_ctx = ralloc_context(NULL);
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->shader_program = shader_program;
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->name = NULL;
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->offset = 0;
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->last = last;
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ~get_sampler_name()
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ralloc_free(this->mem_ctx);
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   virtual ir_visitor_status visit(ir_dereference_variable *ir)
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->name = ir->var->name;
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return visit_continue;
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   virtual ir_visitor_status visit_leave(ir_dereference_record *ir)
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->name = ralloc_asprintf(mem_ctx, "%s.%s", name, ir->field);
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return visit_continue;
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ir_constant *index = ir->array_index->as_constant();
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      int i;
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (index) {
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 i = index->value.i[0];
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      } else {
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 /* GLSL 1.10 and 1.20 allowed variable sampler array indices,
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  * while GLSL 1.30 requires that the array indices be
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  * constant integer expressions.  We don't expect any driver
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  * to actually work with a really variable array index, so
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  * all that would work would be an unrolled loop counter that ends
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  * up being constant above.
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  */
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 ralloc_strcat(&shader_program->InfoLog,
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		       "warning: Variable sampler array index unsupported.\n"
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		       "This feature of the language was removed in GLSL 1.20 "
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		       "and is unlikely to be supported for 1.10 in Mesa.\n");
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 i = 0;
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (ir != last) {
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 this->name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      } else {
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 offset = i;
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return visit_continue;
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct gl_shader_program *shader_program;
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const char *name;
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void *mem_ctx;
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   int offset;
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_dereference *last;
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgextern "C" {
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgint
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org_mesa_get_sampler_uniform_value(class ir_dereference *sampler,
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				struct gl_shader_program *shader_program,
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org				const struct gl_program *prog)
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   get_sampler_name getname(sampler, shader_program);
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   sampler->accept(&getname);
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned location;
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!shader_program->UniformHash->get(location, getname.name)) {
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      linker_error(shader_program,
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		   "failed to find sampler named %s.\n", getname.name);
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return 0;
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return shader_program->UniformStorage[location].sampler + getname.offset;
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
125