Merge branch 'glsl-to-tgsi'
[mesa.git] / src / mesa / program / sampler.cpp
1 /*
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
26 #include "ir.h"
27 #include "glsl_types.h"
28 #include "ir_visitor.h"
29
30 extern "C" {
31 #include "main/compiler.h"
32 #include "main/mtypes.h"
33 #include "program/prog_parameter.h"
34 }
35
36 static void fail_link(struct gl_shader_program *prog, const char *fmt, ...) PRINTFLIKE(2, 3);
37
38 static void fail_link(struct gl_shader_program *prog, const char *fmt, ...)
39 {
40 va_list args;
41 va_start(args, fmt);
42 ralloc_vasprintf_append(&prog->InfoLog, fmt, args);
43 va_end(args);
44
45 prog->LinkStatus = GL_FALSE;
46 }
47
48 class get_sampler_name : public ir_hierarchical_visitor
49 {
50 public:
51 get_sampler_name(ir_dereference *last,
52 struct gl_shader_program *shader_program)
53 {
54 this->mem_ctx = ralloc_context(NULL);
55 this->shader_program = shader_program;
56 this->name = NULL;
57 this->offset = 0;
58 this->last = last;
59 }
60
61 ~get_sampler_name()
62 {
63 ralloc_free(this->mem_ctx);
64 }
65
66 virtual ir_visitor_status visit(ir_dereference_variable *ir)
67 {
68 this->name = ir->var->name;
69 return visit_continue;
70 }
71
72 virtual ir_visitor_status visit_leave(ir_dereference_record *ir)
73 {
74 this->name = ralloc_asprintf(mem_ctx, "%s.%s", name, ir->field);
75 return visit_continue;
76 }
77
78 virtual ir_visitor_status visit_leave(ir_dereference_array *ir)
79 {
80 ir_constant *index = ir->array_index->as_constant();
81 int i;
82
83 if (index) {
84 i = index->value.i[0];
85 } else {
86 /* GLSL 1.10 and 1.20 allowed variable sampler array indices,
87 * while GLSL 1.30 requires that the array indices be
88 * constant integer expressions. We don't expect any driver
89 * to actually work with a really variable array index, so
90 * all that would work would be an unrolled loop counter that ends
91 * up being constant above.
92 */
93 ralloc_strcat(&shader_program->InfoLog,
94 "warning: Variable sampler array index unsupported.\n"
95 "This feature of the language was removed in GLSL 1.20 "
96 "and is unlikely to be supported for 1.10 in Mesa.\n");
97 i = 0;
98 }
99 if (ir != last) {
100 this->name = ralloc_asprintf(mem_ctx, "%s[%d]", name, i);
101 } else {
102 offset = i;
103 }
104 return visit_continue;
105 }
106
107 struct gl_shader_program *shader_program;
108 const char *name;
109 void *mem_ctx;
110 int offset;
111 ir_dereference *last;
112 };
113
114 extern "C" {
115 int
116 _mesa_get_sampler_uniform_value(class ir_dereference *sampler,
117 struct gl_shader_program *shader_program,
118 const struct gl_program *prog)
119 {
120 get_sampler_name getname(sampler, shader_program);
121
122 sampler->accept(&getname);
123
124 GLint index = _mesa_lookup_parameter_index(prog->Parameters, -1,
125 getname.name);
126
127 if (index < 0) {
128 fail_link(shader_program,
129 "failed to find sampler named %s.\n", getname.name);
130 return 0;
131 }
132
133 index += getname.offset;
134
135 return prog->Parameters->ParameterValues[index][0].f;
136 }
137 }