blob: d43809ef9c28d025d3be2591e8ddfda70dab0f05 [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,
99 glsl_symbol_table *symtab)
100{
101 for (unsigned i = 0
102 ; i < Elements(builtin_110_deprecated_uniforms)
103 ; i++) {
104 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
105 instructions, symtab);
106 }
107
Ian Romanick3eba5932010-04-26 14:59:32 -0700108 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700109 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
110 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
111 * FINISHME: for now.
Eric Anholt78fe3c92010-03-28 01:46:48 -0700112 */
Ian Romanick3eba5932010-04-26 14:59:32 -0700113 const glsl_type *const mat4_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700114 glsl_type::get_array_instance(symtab, glsl_type::mat4_type, 4);
Ian Romanick3eba5932010-04-26 14:59:32 -0700115
Ian Romanicked0626e2010-06-21 11:42:57 -0700116 add_variable("gl_TextureMatrix", ir_var_uniform, -1, mat4_array_type,
Ian Romanick3eba5932010-04-26 14:59:32 -0700117 instructions, symtab);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700118
119 /* FINISHME: Add support for gl_DepthRangeParameters */
120 /* FINISHME: Add support for gl_ClipPlane[] */
121 /* FINISHME: Add support for gl_PointParameters */
122
123 /* FINISHME: Add support for gl_MaterialParameters
124 * FINISHME: (glFrontMaterial, glBackMaterial)
125 */
126
Eric Anholtaa579432010-05-19 14:09:04 -0700127 /* FINISHME: The size of this array is implementation dependent based on the
128 * FINISHME: value of GL_MAX_TEXTURE_LIGHTS. GL_MAX_TEXTURE_LIGHTS must be
129 * FINISHME: at least 8, so hard-code 8 for now.
130 */
131 const glsl_type *const light_source_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700132 glsl_type::get_array_instance(symtab,
133 symtab->get_type("gl_LightSourceParameters"), 8);
Eric Anholtaa579432010-05-19 14:09:04 -0700134
Ian Romanicked0626e2010-06-21 11:42:57 -0700135 add_variable("gl_LightSource", ir_var_uniform, -1, light_source_array_type,
Eric Anholtaa579432010-05-19 14:09:04 -0700136 instructions, symtab);
137
Eric Anholt78fe3c92010-03-28 01:46:48 -0700138 /* FINISHME: Add support for gl_LightModel */
139 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
140 /* FINISHME: Add support for gl_TextureEnvColor[] */
141 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
142 /* FINISHME: Add support for gl_Fog */
143}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800144
145static void
146generate_110_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700147 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800148{
149 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
150 add_builtin_variable(& builtin_core_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700151 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800152 }
153
154 for (unsigned i = 0
155 ; i < Elements(builtin_110_deprecated_vs_variables)
156 ; i++) {
157 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700158 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800159 }
Ian Romanick22971e92010-06-29 15:29:56 -0700160 generate_110_uniforms(instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800161
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700162 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
163 *
164 * "As with all arrays, indices used to subscript gl_TexCoord must
165 * either be an integral constant expressions, or this array must be
166 * re-declared by the shader with a size. The size can be at most
167 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
168 * implementation in preserving varying resources."
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800169 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700170 const glsl_type *const vec4_array_type =
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700171 glsl_type::get_array_instance(state->symbols, glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700172
Ian Romanicked0626e2010-06-21 11:42:57 -0700173 add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700174 instructions, state->symbols);
175
176 generate_ARB_draw_buffers_variables(instructions, state, false,
177 vertex_shader);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800178}
179
180
181static void
182generate_120_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700183 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800184{
185 /* GLSL version 1.20 did not add any built-in variables in the vertex
186 * shader.
187 */
Ian Romanick22971e92010-06-29 15:29:56 -0700188 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800189}
190
191
192static void
193generate_130_vs_variables(exec_list *instructions,
Ian Romanick22971e92010-06-29 15:29:56 -0700194 struct _mesa_glsl_parse_state *state)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800195{
Ian Romanick22971e92010-06-29 15:29:56 -0700196 void *ctx = state->symbols;
197 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800198
199 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
200 add_builtin_variable(& builtin_130_vs_variables[i],
Ian Romanick22971e92010-06-29 15:29:56 -0700201 instructions, state->symbols);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800202 }
203
Eric Anholt271e1992010-04-02 23:47:06 -0700204 /* FINISHME: The size of this array is implementation dependent based on
205 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800206 */
Eric Anholt271e1992010-04-02 23:47:06 -0700207 const glsl_type *const clip_distance_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700208 glsl_type::get_array_instance(ctx, glsl_type::float_type, 8);
Ian Romanicked0626e2010-06-21 11:42:57 -0700209
210 /* FINISHME: gl_ClipDistance needs a real location assigned. */
211 add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
Ian Romanick22971e92010-06-29 15:29:56 -0700212 instructions, state->symbols);
Eric Anholt271e1992010-04-02 23:47:06 -0700213
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800214}
215
216
217static void
218initialize_vs_variables(exec_list *instructions,
219 struct _mesa_glsl_parse_state *state)
220{
221
222 switch (state->language_version) {
223 case 110:
Ian Romanick22971e92010-06-29 15:29:56 -0700224 generate_110_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800225 break;
226 case 120:
Ian Romanick22971e92010-06-29 15:29:56 -0700227 generate_120_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800228 break;
229 case 130:
Ian Romanick22971e92010-06-29 15:29:56 -0700230 generate_130_vs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800231 break;
232 }
233}
234
Eric Anholtb3f743a2010-03-25 14:48:25 -0700235static void
236generate_110_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700237 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700238{
239 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
240 add_builtin_variable(& builtin_core_fs_variables[i],
Ian Romanick5e18b052010-06-29 14:21:05 -0700241 instructions, state->symbols);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700242 }
243
Eric Anholt0f09aea2010-03-27 12:48:57 -0700244 for (unsigned i = 0
245 ; i < Elements(builtin_110_deprecated_fs_variables)
246 ; i++) {
247 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
Ian Romanick5e18b052010-06-29 14:21:05 -0700248 instructions, state->symbols);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700249 }
Ian Romanick5e18b052010-06-29 14:21:05 -0700250 generate_110_uniforms(instructions, state->symbols);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700251
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700252 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
253 *
254 * "As with all arrays, indices used to subscript gl_TexCoord must
255 * either be an integral constant expressions, or this array must be
256 * re-declared by the shader with a size. The size can be at most
257 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
258 * implementation in preserving varying resources."
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700259 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700260 const glsl_type *const vec4_array_type =
Ian Romanickcd00d5b2010-07-01 13:17:54 -0700261 glsl_type::get_array_instance(state->symbols, glsl_type::vec4_type, 0);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700262
Ian Romanicked0626e2010-06-21 11:42:57 -0700263 add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
Ian Romanick5e18b052010-06-29 14:21:05 -0700264 instructions, state->symbols);
Ian Romanick9c4b1f22010-06-29 15:10:09 -0700265
Ian Romanick22971e92010-06-29 15:29:56 -0700266 generate_ARB_draw_buffers_variables(instructions, state, false,
267 fragment_shader);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700268}
269
Ian Romanickc77b2572010-04-07 16:59:46 -0700270
271static void
Ian Romanick22971e92010-06-29 15:29:56 -0700272generate_ARB_draw_buffers_variables(exec_list *instructions,
273 struct _mesa_glsl_parse_state *state,
274 bool warn, _mesa_glsl_parser_targets target)
Ian Romanickc77b2572010-04-07 16:59:46 -0700275{
Ian Romanick22971e92010-06-29 15:29:56 -0700276 /* gl_MaxDrawBuffers is available in all shader stages.
277 */
Ian Romanicke2f84f02010-06-29 15:19:11 -0700278 ir_variable *const mdb =
279 add_variable("gl_MaxDrawBuffers", ir_var_auto, -1,
280 glsl_type::int_type, instructions, state->symbols);
281
282 if (warn)
283 mdb->warn_extension = "GL_ARB_draw_buffers";
284
285 mdb->constant_value = new(mdb)
286 ir_constant(int(state->Const.MaxDrawBuffers));
287
Ian Romanickc77b2572010-04-07 16:59:46 -0700288
Ian Romanick22971e92010-06-29 15:29:56 -0700289 /* gl_FragData is only available in the fragment shader.
290 */
291 if (target == fragment_shader) {
292 const glsl_type *const vec4_array_type =
293 glsl_type::get_array_instance(state->symbols, glsl_type::vec4_type,
294 state->Const.MaxDrawBuffers);
Ian Romanickc77b2572010-04-07 16:59:46 -0700295
Ian Romanick22971e92010-06-29 15:29:56 -0700296 ir_variable *const fd =
297 add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
298 vec4_array_type, instructions, state->symbols);
299
300 if (warn)
301 fd->warn_extension = "GL_ARB_draw_buffers";
302 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700303}
304
305
Eric Anholtb3f743a2010-03-25 14:48:25 -0700306static void
307generate_120_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700308 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700309{
Ian Romanick5e18b052010-06-29 14:21:05 -0700310 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700311}
312
313static void
314generate_130_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700315 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700316{
Ian Romanick5e18b052010-06-29 14:21:05 -0700317 void *ctx = state->symbols;
318 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700319
Ian Romanick8645a952010-04-07 16:47:44 -0700320 /* FINISHME: The size of this array is implementation dependent based on
321 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Eric Anholtb3f743a2010-03-25 14:48:25 -0700322 */
Ian Romanick8645a952010-04-07 16:47:44 -0700323 const glsl_type *const clip_distance_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700324 glsl_type::get_array_instance(ctx, glsl_type::float_type, 8);
Ian Romanicked0626e2010-06-21 11:42:57 -0700325
326 /* FINISHME: gl_ClipDistance needs a real location assigned. */
327 add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
Ian Romanick5e18b052010-06-29 14:21:05 -0700328 instructions, state->symbols);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700329}
330
331static void
332initialize_fs_variables(exec_list *instructions,
333 struct _mesa_glsl_parse_state *state)
334{
335
336 switch (state->language_version) {
337 case 110:
Ian Romanick5e18b052010-06-29 14:21:05 -0700338 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700339 break;
340 case 120:
Ian Romanick5e18b052010-06-29 14:21:05 -0700341 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700342 break;
343 case 130:
Ian Romanick5e18b052010-06-29 14:21:05 -0700344 generate_130_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700345 break;
346 }
347}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800348
349void
350_mesa_glsl_initialize_variables(exec_list *instructions,
351 struct _mesa_glsl_parse_state *state)
352{
353 switch (state->target) {
354 case vertex_shader:
355 initialize_vs_variables(instructions, state);
356 break;
357 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700358 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800359 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700360 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800361 break;
Eric Anholt81f49a72010-04-29 17:57:28 -0700362 case ir_shader:
363 fprintf(stderr, "ir reader has no builtin variables");
364 exit(1);
365 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800366 }
367}