blob: a0b66b777028f76408f695a6de3230811953b511 [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
Eric Anholtac95f2f2010-06-22 10:38:52 -070024#include "ir.h"
Ian Romanickadfb0cd2010-03-10 10:43:16 -080025#include "glsl_parser_extras.h"
Ian Romanick8bde4ce2010-03-19 11:57:24 -070026#include "glsl_symbol_table.h"
Ian Romanickadfb0cd2010-03-10 10:43:16 -080027#include "builtin_variables.h"
28
29#ifndef Elements
30#define Elements(x) (sizeof(x)/sizeof(*(x)))
31#endif
32
Ian Romanick22971e92010-06-29 15:29:56 -070033static void generate_ARB_draw_buffers_variables(exec_list *,
34 struct _mesa_glsl_parse_state *,
35 bool, _mesa_glsl_parser_targets);
Ian Romanick9c4b1f22010-06-29 15:10:09 -070036
Ian Romanickc77b2572010-04-07 16:59:46 -070037static ir_variable *
Ian Romanicked0626e2010-06-21 11:42:57 -070038add_variable(const char *name, enum ir_variable_mode mode, int slot,
Ian Romanick3f9a73d2010-04-02 11:59:57 -070039 const glsl_type *type, exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070040 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080041{
Carl Worth1660a292010-06-23 18:11:51 -070042 ir_variable *var = new(symtab) ir_variable(type, name);
Ian Romanickadfb0cd2010-03-10 10:43:16 -080043
Ian Romanick3f9a73d2010-04-02 11:59:57 -070044 var->mode = mode;
Eric Anholt71df19f2010-04-19 11:10:37 -070045 switch (var->mode) {
Ian Romanicke2f84f02010-06-29 15:19:11 -070046 case ir_var_auto:
47 var->read_only = true;
48 break;
Eric Anholt71df19f2010-04-19 11:10:37 -070049 case ir_var_in:
50 var->shader_in = true;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080051 var->read_only = true;
Eric Anholt71df19f2010-04-19 11:10:37 -070052 break;
53 case ir_var_inout:
54 var->shader_in = true;
55 var->shader_out = true;
Ian Romanickff236fa2010-04-21 15:08:08 -070056 break;
Eric Anholt71df19f2010-04-19 11:10:37 -070057 case ir_var_out:
58 var->shader_out = true;
59 break;
60 case ir_var_uniform:
61 var->shader_in = true;
62 var->read_only = true;
63 break;
64 default:
65 assert(0);
66 break;
67 }
Ian Romanickadfb0cd2010-03-10 10:43:16 -080068
Ian Romanicked0626e2010-06-21 11:42:57 -070069 var->location = slot;
70
Ian Romanickadfb0cd2010-03-10 10:43:16 -080071 /* Once the variable is created an initialized, add it to the symbol table
72 * and add the declaration to the IR stream.
73 */
74 instructions->push_tail(var);
75
Ian Romanick8bde4ce2010-03-19 11:57:24 -070076 symtab->add_variable(var->name, var);
Ian Romanickc77b2572010-04-07 16:59:46 -070077 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080078}
79
Ian Romanick3f9a73d2010-04-02 11:59:57 -070080
81static void
82add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
83 glsl_symbol_table *symtab)
84{
85 /* Create a new variable declaration from the description supplied by
86 * the caller.
87 */
88 const glsl_type *const type = symtab->get_type(proto->type);
89
90 assert(type != NULL);
91
Ian Romanicked0626e2010-06-21 11:42:57 -070092 add_variable(proto->name, proto->mode, proto->slot, type, instructions,
93 symtab);
Ian Romanick3f9a73d2010-04-02 11:59:57 -070094}
95
96
Eric Anholt78fe3c92010-03-28 01:46:48 -070097static void
98generate_110_uniforms(exec_list *instructions,
Ian Romanick127308b2010-07-01 13:30:50 -070099 struct _mesa_glsl_parse_state *state)
Eric Anholt78fe3c92010-03-28 01:46:48 -0700100{
101 for (unsigned i = 0
102 ; i < Elements(builtin_110_deprecated_uniforms)
103 ; i++) {
104 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
Ian Romanick127308b2010-07-01 13:30:50 -0700105 instructions, state->symbols);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700106 }
107
Ian Romanick127308b2010-07-01 13:30:50 -0700108 ir_variable *const mtc = add_variable("gl_MaxTextureCoords", ir_var_auto,
109 -1, glsl_type::int_type,
110 instructions, state->symbols);
111 mtc->constant_value = new(mtc)
112 ir_constant(int(state->Const.MaxTextureCoords));
113
114
Ian Romanick3eba5932010-04-26 14:59:32 -0700115 const glsl_type *const mat4_array_type =
Ian Romanick127308b2010-07-01 13:30:50 -0700116 glsl_type::get_array_instance(state->symbols, glsl_type::mat4_type,
117 state->Const.MaxTextureCoords);
Ian Romanick3eba5932010-04-26 14:59:32 -0700118
Ian Romanicked0626e2010-06-21 11:42:57 -0700119 add_variable("gl_TextureMatrix", ir_var_uniform, -1, mat4_array_type,
Ian Romanick127308b2010-07-01 13:30:50 -0700120 instructions, state->symbols);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700121
122 /* FINISHME: Add support for gl_DepthRangeParameters */
123 /* FINISHME: Add support for gl_ClipPlane[] */
124 /* FINISHME: Add support for gl_PointParameters */
125
126 /* FINISHME: Add support for gl_MaterialParameters
127 * FINISHME: (glFrontMaterial, glBackMaterial)
128 */
129
Eric Anholtaa579432010-05-19 14:09:04 -0700130 /* FINISHME: The size of this array is implementation dependent based on the
131 * FINISHME: value of GL_MAX_TEXTURE_LIGHTS. GL_MAX_TEXTURE_LIGHTS must be
132 * FINISHME: at least 8, so hard-code 8 for now.
133 */
134 const glsl_type *const light_source_array_type =
Ian Romanick127308b2010-07-01 13:30:50 -0700135 glsl_type::get_array_instance(state->symbols,
136 state->symbols->get_type("gl_LightSourceParameters"), 8);
Eric Anholtaa579432010-05-19 14:09:04 -0700137
Ian Romanicked0626e2010-06-21 11:42:57 -0700138 add_variable("gl_LightSource", ir_var_uniform, -1, light_source_array_type,
Ian Romanick127308b2010-07-01 13:30:50 -0700139 instructions, state->symbols);
Eric Anholtaa579432010-05-19 14:09:04 -0700140
Eric Anholt78fe3c92010-03-28 01:46:48 -0700141 /* FINISHME: Add support for gl_LightModel */
142 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
143 /* FINISHME: Add support for gl_TextureEnvColor[] */
144 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
145 /* FINISHME: Add support for gl_Fog */
146}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800147
148static void
149generate_110_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700150 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800151{
152 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
153 add_builtin_variable(& builtin_core_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700154 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800155 }
156
157 for (unsigned i = 0
158 ; i < Elements(builtin_110_deprecated_vs_variables)
159 ; i++) {
160 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700161 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800162 }
Ian Romanick127308b2010-07-01 13:30:50 -0700163 generate_110_uniforms(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800164
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700165 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
166 *
167 * "As with all arrays, indices used to subscript gl_TexCoord must
168 * either be an integral constant expressions, or this array must be
169 * re-declared by the shader with a size. The size can be at most
170 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
171 * implementation in preserving varying resources."
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800172 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700173 const glsl_type *const vec4_array_type =
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700174 glsl_type::get_array_instance(state->symbols, glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700175
Ian Romanicked0626e2010-06-21 11:42:57 -0700176 add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700177 instructions, state->symbols);
178
179 generate_ARB_draw_buffers_variables(instructions, state, false,
180 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800181}
182
183
184static void
185generate_120_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700186 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800187{
188 /* GLSL version 1.20 did not add any built-in variables in the vertex
189 * shader.
190 */
Ian Romanick22971e92010-06-29 15:29:56 -0700191 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800192}
193
194
195static void
196generate_130_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700197 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800198{
Ian Romanick22971e92010-06-29 15:29:56 -0700199 void *ctx = state->symbols;
200 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800201
202 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
203 add_builtin_variable(& builtin_130_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700204 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800205 }
206
Eric Anholt271e1992010-04-02 23:47:06 -0700207 /* FINISHME: The size of this array is implementation dependent based on
208 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800209 */
Eric Anholt271e1992010-04-02 23:47:06 -0700210 const glsl_type *const clip_distance_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700211 glsl_type::get_array_instance(ctx, glsl_type::float_type, 8);
Ian Romanicked0626e2010-06-21 11:42:57 -0700212
213 /* FINISHME: gl_ClipDistance needs a real location assigned. */
214 add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700215 instructions, state->symbols);
Eric Anholt271e1992010-04-02 23:47:06 -0700216
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800217}
218
219
220static void
221initialize_vs_variables(exec_list *instructions,
222 struct _mesa_glsl_parse_state *state)
223{
224
225 switch (state->language_version) {
226 case 110:
Ian Romanick22971e92010-06-29 15:29:56 -0700227 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800228 break;
229 case 120:
Ian Romanick22971e92010-06-29 15:29:56 -0700230 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800231 break;
232 case 130:
Ian Romanick22971e92010-06-29 15:29:56 -0700233 generate_130_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800234 break;
235 }
236}
237
Eric Anholtb3f743a2010-03-25 14:48:25 -0700238static void
239generate_110_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700240 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700241{
242 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
243 add_builtin_variable(& builtin_core_fs_variables[i],
Ian Romanick5e18b052010-06-29 14:21:05 -0700244 instructions, state->symbols);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700245 }
246
Eric Anholt0f09aea2010-03-27 12:48:57 -0700247 for (unsigned i = 0
248 ; i < Elements(builtin_110_deprecated_fs_variables)
249 ; i++) {
250 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
Ian Romanick5e18b052010-06-29 14:21:05 -0700251 instructions, state->symbols);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700252 }
Ian Romanick127308b2010-07-01 13:30:50 -0700253 generate_110_uniforms(instructions, state);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700254
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700255 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
256 *
257 * "As with all arrays, indices used to subscript gl_TexCoord must
258 * either be an integral constant expressions, or this array must be
259 * re-declared by the shader with a size. The size can be at most
260 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
261 * implementation in preserving varying resources."
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700262 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700263 const glsl_type *const vec4_array_type =
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700264 glsl_type::get_array_instance(state->symbols, glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700265
Ian Romanicked0626e2010-06-21 11:42:57 -0700266 add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
Ian Romanick5e18b052010-06-29 14:21:05 -0700267 instructions, state->symbols);
Ian Romanick9c4b1f22010-06-29 15:10:09 -0700268
Ian Romanick22971e92010-06-29 15:29:56 -0700269 generate_ARB_draw_buffers_variables(instructions, state, false,
270 fragment_shader);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700271}
272
Ian Romanickc77b2572010-04-07 16:59:46 -0700273
274static void
Ian Romanick22971e92010-06-29 15:29:56 -0700275generate_ARB_draw_buffers_variables(exec_list *instructions,
276 struct _mesa_glsl_parse_state *state,
277 bool warn, _mesa_glsl_parser_targets target)
Ian Romanickc77b2572010-04-07 16:59:46 -0700278{
Ian Romanick22971e92010-06-29 15:29:56 -0700279 /* gl_MaxDrawBuffers is available in all shader stages.
280 */
Ian Romanicke2f84f02010-06-29 15:19:11 -0700281 ir_variable *const mdb =
282 add_variable("gl_MaxDrawBuffers", ir_var_auto, -1,
283 glsl_type::int_type, instructions, state->symbols);
284
285 if (warn)
286 mdb->warn_extension = "GL_ARB_draw_buffers";
287
288 mdb->constant_value = new(mdb)
289 ir_constant(int(state->Const.MaxDrawBuffers));
290
Ian Romanickc77b2572010-04-07 16:59:46 -0700291
Ian Romanick22971e92010-06-29 15:29:56 -0700292 /* gl_FragData is only available in the fragment shader.
293 */
294 if (target == fragment_shader) {
295 const glsl_type *const vec4_array_type =
296 glsl_type::get_array_instance(state->symbols, glsl_type::vec4_type,
297 state->Const.MaxDrawBuffers);
Ian Romanickc77b2572010-04-07 16:59:46 -0700298
Ian Romanick22971e92010-06-29 15:29:56 -0700299 ir_variable *const fd =
300 add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
301 vec4_array_type, instructions, state->symbols);
302
303 if (warn)
304 fd->warn_extension = "GL_ARB_draw_buffers";
305 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700306}
307
308
Eric Anholtb3f743a2010-03-25 14:48:25 -0700309static void
310generate_120_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700311 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700312{
Ian Romanick5e18b052010-06-29 14:21:05 -0700313 generate_110_fs_variables(instructions, state);
Eric Anholt152b55e2010-07-07 19:45:22 -0700314
315 for (unsigned i = 0
316 ; i < Elements(builtin_120_fs_variables)
317 ; i++) {
318 add_builtin_variable(& builtin_120_fs_variables[i],
319 instructions, state->symbols);
320 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700321}
322
323static void
324generate_130_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700325 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700326{
Ian Romanick5e18b052010-06-29 14:21:05 -0700327 void *ctx = state->symbols;
328 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700329
Ian Romanick8645a952010-04-07 16:47:44 -0700330 /* FINISHME: The size of this array is implementation dependent based on
331 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Eric Anholtb3f743a2010-03-25 14:48:25 -0700332 */
Ian Romanick8645a952010-04-07 16:47:44 -0700333 const glsl_type *const clip_distance_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700334 glsl_type::get_array_instance(ctx, glsl_type::float_type, 8);
Ian Romanicked0626e2010-06-21 11:42:57 -0700335
336 /* FINISHME: gl_ClipDistance needs a real location assigned. */
337 add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
Ian Romanick5e18b052010-06-29 14:21:05 -0700338 instructions, state->symbols);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700339}
340
341static void
342initialize_fs_variables(exec_list *instructions,
343 struct _mesa_glsl_parse_state *state)
344{
345
346 switch (state->language_version) {
347 case 110:
Ian Romanick5e18b052010-06-29 14:21:05 -0700348 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700349 break;
350 case 120:
Ian Romanick5e18b052010-06-29 14:21:05 -0700351 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700352 break;
353 case 130:
Ian Romanick5e18b052010-06-29 14:21:05 -0700354 generate_130_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700355 break;
356 }
357}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800358
359void
360_mesa_glsl_initialize_variables(exec_list *instructions,
361 struct _mesa_glsl_parse_state *state)
362{
363 switch (state->target) {
364 case vertex_shader:
365 initialize_vs_variables(instructions, state);
366 break;
367 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700368 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800369 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700370 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800371 break;
Eric Anholt81f49a72010-04-29 17:57:28 -0700372 case ir_shader:
373 fprintf(stderr, "ir reader has no builtin variables");
374 exit(1);
375 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800376 }
377}