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