blob: a2c0803e55a44de7e590682712303cd73f5b62a6 [file] [log] [blame]
Ian Romanickadfb0cd2010-03-10 10:43:16 -08001/*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include "glsl_parser_extras.h"
Ian Romanick8bde4ce2010-03-19 11:57:24 -070025#include "glsl_symbol_table.h"
Ian Romanickadfb0cd2010-03-10 10:43:16 -080026#include "ir.h"
27#include "builtin_variables.h"
28
29#ifndef Elements
30#define Elements(x) (sizeof(x)/sizeof(*(x)))
31#endif
32
Ian Romanickc77b2572010-04-07 16:59:46 -070033static ir_variable *
Ian Romanick3f9a73d2010-04-02 11:59:57 -070034add_variable(const char *name, enum ir_variable_mode mode,
35 const glsl_type *type, exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070036 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080037{
Ian Romanick3f9a73d2010-04-02 11:59:57 -070038 ir_variable *var = new ir_variable(type, name);
Ian Romanickadfb0cd2010-03-10 10:43:16 -080039
Ian Romanick3f9a73d2010-04-02 11:59:57 -070040 var->mode = mode;
Eric Anholt71df19f2010-04-19 11:10:37 -070041 switch (var->mode) {
42 case ir_var_in:
43 var->shader_in = true;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080044 var->read_only = true;
Eric Anholt71df19f2010-04-19 11:10:37 -070045 break;
46 case ir_var_inout:
47 var->shader_in = true;
48 var->shader_out = true;
Ian Romanickff236fa2010-04-21 15:08:08 -070049 break;
Eric Anholt71df19f2010-04-19 11:10:37 -070050 case ir_var_out:
51 var->shader_out = true;
52 break;
53 case ir_var_uniform:
54 var->shader_in = true;
55 var->read_only = true;
56 break;
57 default:
58 assert(0);
59 break;
60 }
Ian Romanickadfb0cd2010-03-10 10:43:16 -080061
62 /* Once the variable is created an initialized, add it to the symbol table
63 * and add the declaration to the IR stream.
64 */
65 instructions->push_tail(var);
66
Ian Romanick8bde4ce2010-03-19 11:57:24 -070067 symtab->add_variable(var->name, var);
Ian Romanickc77b2572010-04-07 16:59:46 -070068 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080069}
70
Ian Romanick3f9a73d2010-04-02 11:59:57 -070071
72static void
73add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
74 glsl_symbol_table *symtab)
75{
76 /* Create a new variable declaration from the description supplied by
77 * the caller.
78 */
79 const glsl_type *const type = symtab->get_type(proto->type);
80
81 assert(type != NULL);
82
83 add_variable(proto->name, proto->mode, type, instructions, symtab);
84}
85
86
Eric Anholt78fe3c92010-03-28 01:46:48 -070087static void
88generate_110_uniforms(exec_list *instructions,
89 glsl_symbol_table *symtab)
90{
91 for (unsigned i = 0
92 ; i < Elements(builtin_110_deprecated_uniforms)
93 ; i++) {
94 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
95 instructions, symtab);
96 }
97
98 /* FINISHME: Add support for gl_TextureMatrix[]. The size of this array is
99 * FINISHME: implementation dependent based on the value of
100 * FINISHME: GL_MAX_TEXTURE_COORDS.
101 */
102
103 /* FINISHME: Add support for gl_DepthRangeParameters */
104 /* FINISHME: Add support for gl_ClipPlane[] */
105 /* FINISHME: Add support for gl_PointParameters */
106
107 /* FINISHME: Add support for gl_MaterialParameters
108 * FINISHME: (glFrontMaterial, glBackMaterial)
109 */
110
111 /* FINISHME: Add support for gl_LightSource[] */
112 /* FINISHME: Add support for gl_LightModel */
113 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
114 /* FINISHME: Add support for gl_TextureEnvColor[] */
115 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
116 /* FINISHME: Add support for gl_Fog */
117}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800118
119static void
120generate_110_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700121 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800122{
123 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
124 add_builtin_variable(& builtin_core_vs_variables[i],
125 instructions, symtab);
126 }
127
128 for (unsigned i = 0
129 ; i < Elements(builtin_110_deprecated_vs_variables)
130 ; i++) {
131 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
132 instructions, symtab);
133 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700134 generate_110_uniforms(instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800135
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700136 /* FINISHME: The size of this array is implementation dependent based on the
137 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
138 * FINISHME: at least 2, so hard-code 2 for now.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800139 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700140 const glsl_type *const vec4_type =
Ian Romanick1b3f47f2010-04-07 16:09:47 -0700141 glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700142 const glsl_type *const vec4_array_type =
143 glsl_type::get_array_instance(vec4_type, 2);
144
145 add_variable("gl_TexCoord", ir_var_out, vec4_array_type, instructions,
146 symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800147}
148
149
150static void
151generate_120_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700152 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800153{
154 /* GLSL version 1.20 did not add any built-in variables in the vertex
155 * shader.
156 */
157 generate_110_vs_variables(instructions, symtab);
158}
159
160
161static void
162generate_130_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700163 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800164{
165 generate_120_vs_variables(instructions, symtab);
166
167 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
168 add_builtin_variable(& builtin_130_vs_variables[i],
169 instructions, symtab);
170 }
171
Eric Anholt271e1992010-04-02 23:47:06 -0700172 /* FINISHME: The size of this array is implementation dependent based on
173 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800174 */
Eric Anholt271e1992010-04-02 23:47:06 -0700175 const glsl_type *const clip_distance_array_type =
176 glsl_type::get_array_instance(glsl_type::float_type, 8);
177 add_variable("gl_ClipDistance", ir_var_out, clip_distance_array_type,
178 instructions, symtab);
179
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800180}
181
182
183static void
184initialize_vs_variables(exec_list *instructions,
185 struct _mesa_glsl_parse_state *state)
186{
187
188 switch (state->language_version) {
189 case 110:
190 generate_110_vs_variables(instructions, state->symbols);
191 break;
192 case 120:
193 generate_120_vs_variables(instructions, state->symbols);
194 break;
195 case 130:
196 generate_130_vs_variables(instructions, state->symbols);
197 break;
198 }
199}
200
Eric Anholtb3f743a2010-03-25 14:48:25 -0700201static void
202generate_110_fs_variables(exec_list *instructions,
203 glsl_symbol_table *symtab)
204{
205 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
206 add_builtin_variable(& builtin_core_fs_variables[i],
207 instructions, symtab);
208 }
209
Eric Anholt0f09aea2010-03-27 12:48:57 -0700210 for (unsigned i = 0
211 ; i < Elements(builtin_110_deprecated_fs_variables)
212 ; i++) {
213 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
214 instructions, symtab);
215 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700216 generate_110_uniforms(instructions, symtab);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700217
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700218 /* FINISHME: The size of this array is implementation dependent based on the
219 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
220 * FINISHME: at least 2, so hard-code 2 for now.
221 */
222 const glsl_type *const vec4_type =
Ian Romanick1b3f47f2010-04-07 16:09:47 -0700223 glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700224 const glsl_type *const vec4_array_type =
225 glsl_type::get_array_instance(vec4_type, 2);
226
227 add_variable("gl_TexCoord", ir_var_in, vec4_array_type, instructions,
228 symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700229}
230
Ian Romanickc77b2572010-04-07 16:59:46 -0700231
232static void
233generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
234 glsl_symbol_table *symtab, bool warn)
235{
236 /* FINISHME: The size of this array is implementation dependent based on the
237 * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be
238 * FINISHME: at least 1, so hard-code 1 for now.
239 */
240 const glsl_type *const vec4_type =
241 glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
242 const glsl_type *const vec4_array_type =
243 glsl_type::get_array_instance(vec4_type, 1);
244
245 ir_variable *const fd =
246 add_variable("gl_FragData", ir_var_out, vec4_array_type, instructions,
247 symtab);
248
249 if (warn)
250 fd->warn_extension = "GL_ARB_draw_buffers";
251}
252
253
Eric Anholtb3f743a2010-03-25 14:48:25 -0700254static void
255generate_120_fs_variables(exec_list *instructions,
256 glsl_symbol_table *symtab)
257{
Eric Anholtb3f743a2010-03-25 14:48:25 -0700258 generate_110_fs_variables(instructions, symtab);
Ian Romanickc77b2572010-04-07 16:59:46 -0700259 generate_ARB_draw_buffers_fs_variables(instructions, symtab, false);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700260}
261
262static void
263generate_130_fs_variables(exec_list *instructions,
264 glsl_symbol_table *symtab)
265{
266 generate_120_fs_variables(instructions, symtab);
267
Ian Romanick8645a952010-04-07 16:47:44 -0700268 /* FINISHME: The size of this array is implementation dependent based on
269 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Eric Anholtb3f743a2010-03-25 14:48:25 -0700270 */
Ian Romanick8645a952010-04-07 16:47:44 -0700271 const glsl_type *const clip_distance_array_type =
272 glsl_type::get_array_instance(glsl_type::float_type, 8);
273 add_variable("gl_ClipDistance", ir_var_in, clip_distance_array_type,
274 instructions, symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700275}
276
277static void
278initialize_fs_variables(exec_list *instructions,
279 struct _mesa_glsl_parse_state *state)
280{
281
282 switch (state->language_version) {
283 case 110:
284 generate_110_fs_variables(instructions, state->symbols);
285 break;
286 case 120:
287 generate_120_fs_variables(instructions, state->symbols);
288 break;
289 case 130:
290 generate_130_fs_variables(instructions, state->symbols);
291 break;
292 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700293
294
295 /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
296 * can basically ignore any extension settings for it.
297 */
298 if (state->language_version < 120) {
299 if (state->ARB_draw_buffers_enable) {
300 generate_ARB_draw_buffers_fs_variables(instructions, state->symbols,
301 state->ARB_draw_buffers_warn);
302 }
303 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700304}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800305
306void
307_mesa_glsl_initialize_variables(exec_list *instructions,
308 struct _mesa_glsl_parse_state *state)
309{
310 switch (state->target) {
311 case vertex_shader:
312 initialize_vs_variables(instructions, state);
313 break;
314 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700315 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800316 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700317 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800318 break;
319 }
320}