blob: 15a4a92f628897a277d1296b7575fc937259d921 [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 Romanickc77b2572010-04-07 16:59:46 -070033static ir_variable *
Ian Romanicked0626e2010-06-21 11:42:57 -070034add_variable(const char *name, enum ir_variable_mode mode, int slot,
Ian Romanick3f9a73d2010-04-02 11:59:57 -070035 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{
Carl Worth1660a292010-06-23 18:11:51 -070038 ir_variable *var = new(symtab) 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
Ian Romanicked0626e2010-06-21 11:42:57 -070062 var->location = slot;
63
Ian Romanickadfb0cd2010-03-10 10:43:16 -080064 /* Once the variable is created an initialized, add it to the symbol table
65 * and add the declaration to the IR stream.
66 */
67 instructions->push_tail(var);
68
Ian Romanick8bde4ce2010-03-19 11:57:24 -070069 symtab->add_variable(var->name, var);
Ian Romanickc77b2572010-04-07 16:59:46 -070070 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080071}
72
Ian Romanick3f9a73d2010-04-02 11:59:57 -070073
74static void
75add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
76 glsl_symbol_table *symtab)
77{
78 /* Create a new variable declaration from the description supplied by
79 * the caller.
80 */
81 const glsl_type *const type = symtab->get_type(proto->type);
82
83 assert(type != NULL);
84
Ian Romanicked0626e2010-06-21 11:42:57 -070085 add_variable(proto->name, proto->mode, proto->slot, type, instructions,
86 symtab);
Ian Romanick3f9a73d2010-04-02 11:59:57 -070087}
88
89
Eric Anholt78fe3c92010-03-28 01:46:48 -070090static void
91generate_110_uniforms(exec_list *instructions,
92 glsl_symbol_table *symtab)
93{
94 for (unsigned i = 0
95 ; i < Elements(builtin_110_deprecated_uniforms)
96 ; i++) {
97 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
98 instructions, symtab);
99 }
100
Ian Romanick3eba5932010-04-26 14:59:32 -0700101 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700102 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
103 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
104 * FINISHME: for now.
Eric Anholt78fe3c92010-03-28 01:46:48 -0700105 */
Ian Romanick3eba5932010-04-26 14:59:32 -0700106 const glsl_type *const mat4_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700107 glsl_type::get_array_instance(symtab, glsl_type::mat4_type, 4);
Ian Romanick3eba5932010-04-26 14:59:32 -0700108
Ian Romanicked0626e2010-06-21 11:42:57 -0700109 add_variable("gl_TextureMatrix", ir_var_uniform, -1, mat4_array_type,
Ian Romanick3eba5932010-04-26 14:59:32 -0700110 instructions, symtab);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700111
112 /* FINISHME: Add support for gl_DepthRangeParameters */
113 /* FINISHME: Add support for gl_ClipPlane[] */
114 /* FINISHME: Add support for gl_PointParameters */
115
116 /* FINISHME: Add support for gl_MaterialParameters
117 * FINISHME: (glFrontMaterial, glBackMaterial)
118 */
119
Eric Anholtaa579432010-05-19 14:09:04 -0700120 /* FINISHME: The size of this array is implementation dependent based on the
121 * FINISHME: value of GL_MAX_TEXTURE_LIGHTS. GL_MAX_TEXTURE_LIGHTS must be
122 * FINISHME: at least 8, so hard-code 8 for now.
123 */
124 const glsl_type *const light_source_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700125 glsl_type::get_array_instance(symtab,
126 symtab->get_type("gl_LightSourceParameters"), 8);
Eric Anholtaa579432010-05-19 14:09:04 -0700127
Ian Romanicked0626e2010-06-21 11:42:57 -0700128 add_variable("gl_LightSource", ir_var_uniform, -1, light_source_array_type,
Eric Anholtaa579432010-05-19 14:09:04 -0700129 instructions, symtab);
130
Eric Anholt78fe3c92010-03-28 01:46:48 -0700131 /* FINISHME: Add support for gl_LightModel */
132 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
133 /* FINISHME: Add support for gl_TextureEnvColor[] */
134 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
135 /* FINISHME: Add support for gl_Fog */
136}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800137
138static void
139generate_110_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700140 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800141{
142 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
143 add_builtin_variable(& builtin_core_vs_variables[i],
144 instructions, symtab);
145 }
146
147 for (unsigned i = 0
148 ; i < Elements(builtin_110_deprecated_vs_variables)
149 ; i++) {
150 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
151 instructions, symtab);
152 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700153 generate_110_uniforms(instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800154
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700155 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700156 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
157 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
158 * FINISHME: for now.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800159 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700160 const glsl_type *const vec4_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700161 glsl_type::get_array_instance(symtab, glsl_type::vec4_type, 4);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700162
Ian Romanicked0626e2010-06-21 11:42:57 -0700163 add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
164 instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800165}
166
167
168static void
169generate_120_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700170 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800171{
172 /* GLSL version 1.20 did not add any built-in variables in the vertex
173 * shader.
174 */
175 generate_110_vs_variables(instructions, symtab);
176}
177
178
179static void
180generate_130_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700181 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800182{
Carl Worth12c41152010-06-18 17:52:59 -0700183 void *ctx = symtab;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800184 generate_120_vs_variables(instructions, symtab);
185
186 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
187 add_builtin_variable(& builtin_130_vs_variables[i],
188 instructions, symtab);
189 }
190
Eric Anholt271e1992010-04-02 23:47:06 -0700191 /* FINISHME: The size of this array is implementation dependent based on
192 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800193 */
Eric Anholt271e1992010-04-02 23:47:06 -0700194 const glsl_type *const clip_distance_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700195 glsl_type::get_array_instance(ctx, glsl_type::float_type, 8);
Ian Romanicked0626e2010-06-21 11:42:57 -0700196
197 /* FINISHME: gl_ClipDistance needs a real location assigned. */
198 add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
Eric Anholt271e1992010-04-02 23:47:06 -0700199 instructions, symtab);
200
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800201}
202
203
204static void
205initialize_vs_variables(exec_list *instructions,
206 struct _mesa_glsl_parse_state *state)
207{
208
209 switch (state->language_version) {
210 case 110:
211 generate_110_vs_variables(instructions, state->symbols);
212 break;
213 case 120:
214 generate_120_vs_variables(instructions, state->symbols);
215 break;
216 case 130:
217 generate_130_vs_variables(instructions, state->symbols);
218 break;
219 }
220}
221
Eric Anholtb3f743a2010-03-25 14:48:25 -0700222static void
223generate_110_fs_variables(exec_list *instructions,
224 glsl_symbol_table *symtab)
225{
226 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
227 add_builtin_variable(& builtin_core_fs_variables[i],
228 instructions, symtab);
229 }
230
Eric Anholt0f09aea2010-03-27 12:48:57 -0700231 for (unsigned i = 0
232 ; i < Elements(builtin_110_deprecated_fs_variables)
233 ; i++) {
234 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
235 instructions, symtab);
236 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700237 generate_110_uniforms(instructions, symtab);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700238
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700239 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700240 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
241 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
242 * FINISHME: for now.
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700243 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700244 const glsl_type *const vec4_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700245 glsl_type::get_array_instance(symtab, glsl_type::vec4_type, 4);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700246
Ian Romanicked0626e2010-06-21 11:42:57 -0700247 add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
248 instructions, symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700249}
250
Ian Romanickc77b2572010-04-07 16:59:46 -0700251
252static void
253generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
254 glsl_symbol_table *symtab, bool warn)
255{
256 /* FINISHME: The size of this array is implementation dependent based on the
257 * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be
258 * FINISHME: at least 1, so hard-code 1 for now.
259 */
Ian Romanickc77b2572010-04-07 16:59:46 -0700260 const glsl_type *const vec4_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700261 glsl_type::get_array_instance(symtab, glsl_type::vec4_type, 1);
Ian Romanickc77b2572010-04-07 16:59:46 -0700262
263 ir_variable *const fd =
Ian Romanicked0626e2010-06-21 11:42:57 -0700264 add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
265 vec4_array_type, instructions, symtab);
Ian Romanickc77b2572010-04-07 16:59:46 -0700266
267 if (warn)
268 fd->warn_extension = "GL_ARB_draw_buffers";
269}
270
271
Eric Anholtb3f743a2010-03-25 14:48:25 -0700272static void
273generate_120_fs_variables(exec_list *instructions,
274 glsl_symbol_table *symtab)
275{
Eric Anholtb3f743a2010-03-25 14:48:25 -0700276 generate_110_fs_variables(instructions, symtab);
Ian Romanickc77b2572010-04-07 16:59:46 -0700277 generate_ARB_draw_buffers_fs_variables(instructions, symtab, false);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700278}
279
280static void
281generate_130_fs_variables(exec_list *instructions,
282 glsl_symbol_table *symtab)
283{
Carl Worth12c41152010-06-18 17:52:59 -0700284 void *ctx = symtab;
Eric Anholtb3f743a2010-03-25 14:48:25 -0700285 generate_120_fs_variables(instructions, symtab);
286
Ian Romanick8645a952010-04-07 16:47:44 -0700287 /* FINISHME: The size of this array is implementation dependent based on
288 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Eric Anholtb3f743a2010-03-25 14:48:25 -0700289 */
Ian Romanick8645a952010-04-07 16:47:44 -0700290 const glsl_type *const clip_distance_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700291 glsl_type::get_array_instance(ctx, glsl_type::float_type, 8);
Ian Romanicked0626e2010-06-21 11:42:57 -0700292
293 /* FINISHME: gl_ClipDistance needs a real location assigned. */
294 add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
Ian Romanick8645a952010-04-07 16:47:44 -0700295 instructions, symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700296}
297
298static void
299initialize_fs_variables(exec_list *instructions,
300 struct _mesa_glsl_parse_state *state)
301{
302
303 switch (state->language_version) {
304 case 110:
305 generate_110_fs_variables(instructions, state->symbols);
306 break;
307 case 120:
308 generate_120_fs_variables(instructions, state->symbols);
309 break;
310 case 130:
311 generate_130_fs_variables(instructions, state->symbols);
312 break;
313 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700314
315
316 /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
317 * can basically ignore any extension settings for it.
318 */
319 if (state->language_version < 120) {
320 if (state->ARB_draw_buffers_enable) {
321 generate_ARB_draw_buffers_fs_variables(instructions, state->symbols,
322 state->ARB_draw_buffers_warn);
323 }
324 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700325}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800326
327void
328_mesa_glsl_initialize_variables(exec_list *instructions,
329 struct _mesa_glsl_parse_state *state)
330{
331 switch (state->target) {
332 case vertex_shader:
333 initialize_vs_variables(instructions, state);
334 break;
335 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700336 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800337 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700338 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800339 break;
Eric Anholt81f49a72010-04-29 17:57:28 -0700340 case ir_shader:
341 fprintf(stderr, "ir reader has no builtin variables");
342 exit(1);
343 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800344 }
345}