blob: f8584c96810e365b410feed0919e82a3973e70c6 [file] [log] [blame]
Eric Anholta3289322010-09-28 10:43:56 -07001/*
2 * Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
3 * Copyright (C) 2008 VMware, Inc. All Rights Reserved.
4 * Copyright © 2010 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
25
Chad Versace223568f2010-11-01 14:23:53 -070026#include "ir.h"
27#include "glsl_types.h"
28#include "ir_visitor.h"
Ian Romanicka9f25162011-09-22 16:56:58 -070029#include "../glsl/program.h"
Ian Romanick71990962011-10-18 16:01:49 -070030#include "ir_uniform.h"
Eric Anholta3289322010-09-28 10:43:56 -070031
Eric Anholta3289322010-09-28 10:43:56 -070032#include "main/compiler.h"
33#include "main/mtypes.h"
Brian Paul6dac4552014-12-15 17:05:13 -070034#include "program/hash_table.h"
Eric Anholta3289322010-09-28 10:43:56 -070035#include "program/prog_parameter.h"
Marek Olšákd4a06d72013-05-13 15:46:49 +020036#include "program/program.h"
Brian Paul6dac4552014-12-15 17:05:13 -070037
Eric Anholta3289322010-09-28 10:43:56 -070038
Eric Anholta3289322010-09-28 10:43:56 -070039class get_sampler_name : public ir_hierarchical_visitor
40{
41public:
42 get_sampler_name(ir_dereference *last,
43 struct gl_shader_program *shader_program)
44 {
Kenneth Graunked3073f52011-01-21 14:32:31 -080045 this->mem_ctx = ralloc_context(NULL);
Eric Anholta3289322010-09-28 10:43:56 -070046 this->shader_program = shader_program;
47 this->name = NULL;
48 this->offset = 0;
49 this->last = last;
50 }
51
52 ~get_sampler_name()
53 {
Kenneth Graunked3073f52011-01-21 14:32:31 -080054 ralloc_free(this->mem_ctx);
Eric Anholta3289322010-09-28 10:43:56 -070055 }
56
57 virtual ir_visitor_status visit(ir_dereference_variable *ir)
58 {
59 this->name = ir->var->name;
60 return visit_continue;
61 }
62
63 virtual ir_visitor_status visit_leave(ir_dereference_record *ir)
64 {
Kenneth Graunked3073f52011-01-21 14:32:31 -080065 this->name = ralloc_asprintf(mem_ctx, "%s.%s", name, ir->field);
Eric Anholta3289322010-09-28 10:43:56 -070066 return visit_continue;
67 }
68
69 virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
70 {
71 ir_constant *index = ir->array_index->as_constant();
72 int i;
73
74 if (index) {
75 i = index->value.i[0];
76 } else {
77 /* GLSL 1.10 and 1.20 allowed variable sampler array indices,
78 * while GLSL 1.30 requires that the array indices be
79 * constant integer expressions. We don't expect any driver
80 * to actually work with a really variable array index, so
81 * all that would work would be an unrolled loop counter that ends
82 * up being constant above.
83 */
Kenneth Graunked3073f52011-01-21 14:32:31 -080084 ralloc_strcat(&shader_program->InfoLog,
85 "warning: Variable sampler array index unsupported.\n"
86 "This feature of the language was removed in GLSL 1.20 "
87 "and is unlikely to be supported for 1.10 in Mesa.\n");
Eric Anholta3289322010-09-28 10:43:56 -070088 i = 0;
89 }
90 if (ir != last) {
Kenneth Graunked3073f52011-01-21 14:32:31 -080091 this->name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
Eric Anholta3289322010-09-28 10:43:56 -070092 } else {
93 offset = i;
94 }
95 return visit_continue;
96 }
97
98 struct gl_shader_program *shader_program;
99 const char *name;
100 void *mem_ctx;
101 int offset;
102 ir_dereference *last;
103};
104
Marek Olšákd4a06d72013-05-13 15:46:49 +0200105
Brian Paul6dac4552014-12-15 17:05:13 -0700106int
Eric Anholta3289322010-09-28 10:43:56 -0700107_mesa_get_sampler_uniform_value(class ir_dereference *sampler,
108 struct gl_shader_program *shader_program,
109 const struct gl_program *prog)
110{
111 get_sampler_name getname(sampler, shader_program);
112
Paul Berry665b8d72014-01-07 10:11:39 -0800113 GLuint shader = _mesa_program_enum_to_shader_stage(prog->Target);
Marek Olšákd4a06d72013-05-13 15:46:49 +0200114
Eric Anholta3289322010-09-28 10:43:56 -0700115 sampler->accept(&getname);
116
Ian Romanick71990962011-10-18 16:01:49 -0700117 unsigned location;
118 if (!shader_program->UniformHash->get(location, getname.name)) {
Ian Romanicka9f25162011-09-22 16:56:58 -0700119 linker_error(shader_program,
120 "failed to find sampler named %s.\n", getname.name);
Eric Anholta3289322010-09-28 10:43:56 -0700121 return 0;
122 }
123
Marek Olšákd4a06d72013-05-13 15:46:49 +0200124 if (!shader_program->UniformStorage[location].sampler[shader].active) {
125 assert(0 && "cannot return a sampler");
126 linker_error(shader_program,
127 "cannot return a sampler named %s, because it is not "
128 "used in this shader stage. This is a driver bug.\n",
129 getname.name);
130 return 0;
131 }
132
133 return shader_program->UniformStorage[location].sampler[shader].index +
134 getname.offset;
Eric Anholta3289322010-09-28 10:43:56 -0700135}
Chris Forbes3b48f6a2014-08-03 19:55:55 +1200136
137
Brian Paul6dac4552014-12-15 17:05:13 -0700138class ir_rvalue *
Chris Forbes3b48f6a2014-08-03 19:55:55 +1200139_mesa_get_sampler_array_nonconst_index(class ir_dereference *sampler)
140{
141 ir_dereference_array *deref_arr = sampler->as_dereference_array();
142 if (!deref_arr || deref_arr->array_index->as_constant())
143 return NULL;
144
145 return deref_arr->array_index;
146}