blob: efebe9199fad9cddb8aaf7decfd4195af384f4ed [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{
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
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 =
Ian Romanickcfb35362010-06-07 19:10:33 -0700107 glsl_type::get_array_instance(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 =
125 glsl_type::get_array_instance(symtab->get_type("gl_LightSourceParameters"), 8);
126
Ian Romanicked0626e2010-06-21 11:42:57 -0700127 add_variable("gl_LightSource", ir_var_uniform, -1, light_source_array_type,
Eric Anholtaa579432010-05-19 14:09:04 -0700128 instructions, symtab);
129
Eric Anholt78fe3c92010-03-28 01:46:48 -0700130 /* FINISHME: Add support for gl_LightModel */
131 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
132 /* FINISHME: Add support for gl_TextureEnvColor[] */
133 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
134 /* FINISHME: Add support for gl_Fog */
135}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800136
137static void
138generate_110_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700139 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800140{
141 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
142 add_builtin_variable(& builtin_core_vs_variables[i],
143 instructions, symtab);
144 }
145
146 for (unsigned i = 0
147 ; i < Elements(builtin_110_deprecated_vs_variables)
148 ; i++) {
149 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
150 instructions, symtab);
151 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700152 generate_110_uniforms(instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800153
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700154 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700155 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
156 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
157 * FINISHME: for now.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800158 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700159 const glsl_type *const vec4_array_type =
Ian Romanickcfb35362010-06-07 19:10:33 -0700160 glsl_type::get_array_instance(glsl_type::vec4_type, 4);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700161
Ian Romanicked0626e2010-06-21 11:42:57 -0700162 add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
163 instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800164}
165
166
167static void
168generate_120_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700169 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800170{
171 /* GLSL version 1.20 did not add any built-in variables in the vertex
172 * shader.
173 */
174 generate_110_vs_variables(instructions, symtab);
175}
176
177
178static void
179generate_130_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700180 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800181{
182 generate_120_vs_variables(instructions, symtab);
183
184 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
185 add_builtin_variable(& builtin_130_vs_variables[i],
186 instructions, symtab);
187 }
188
Eric Anholt271e1992010-04-02 23:47:06 -0700189 /* FINISHME: The size of this array is implementation dependent based on
190 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800191 */
Eric Anholt271e1992010-04-02 23:47:06 -0700192 const glsl_type *const clip_distance_array_type =
193 glsl_type::get_array_instance(glsl_type::float_type, 8);
Ian Romanicked0626e2010-06-21 11:42:57 -0700194
195 /* FINISHME: gl_ClipDistance needs a real location assigned. */
196 add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
Eric Anholt271e1992010-04-02 23:47:06 -0700197 instructions, symtab);
198
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800199}
200
201
202static void
203initialize_vs_variables(exec_list *instructions,
204 struct _mesa_glsl_parse_state *state)
205{
206
207 switch (state->language_version) {
208 case 110:
209 generate_110_vs_variables(instructions, state->symbols);
210 break;
211 case 120:
212 generate_120_vs_variables(instructions, state->symbols);
213 break;
214 case 130:
215 generate_130_vs_variables(instructions, state->symbols);
216 break;
217 }
218}
219
Eric Anholtb3f743a2010-03-25 14:48:25 -0700220static void
221generate_110_fs_variables(exec_list *instructions,
222 glsl_symbol_table *symtab)
223{
224 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
225 add_builtin_variable(& builtin_core_fs_variables[i],
226 instructions, symtab);
227 }
228
Eric Anholt0f09aea2010-03-27 12:48:57 -0700229 for (unsigned i = 0
230 ; i < Elements(builtin_110_deprecated_fs_variables)
231 ; i++) {
232 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
233 instructions, symtab);
234 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700235 generate_110_uniforms(instructions, symtab);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700236
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700237 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700238 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
239 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
240 * FINISHME: for now.
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700241 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700242 const glsl_type *const vec4_array_type =
Ian Romanickcfb35362010-06-07 19:10:33 -0700243 glsl_type::get_array_instance(glsl_type::vec4_type, 4);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700244
Ian Romanicked0626e2010-06-21 11:42:57 -0700245 add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
246 instructions, symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700247}
248
Ian Romanickc77b2572010-04-07 16:59:46 -0700249
250static void
251generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
252 glsl_symbol_table *symtab, bool warn)
253{
254 /* FINISHME: The size of this array is implementation dependent based on the
255 * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be
256 * FINISHME: at least 1, so hard-code 1 for now.
257 */
Ian Romanickc77b2572010-04-07 16:59:46 -0700258 const glsl_type *const vec4_array_type =
Eric Anholtec9e7382010-04-22 09:47:27 -0700259 glsl_type::get_array_instance(glsl_type::vec4_type, 1);
Ian Romanickc77b2572010-04-07 16:59:46 -0700260
261 ir_variable *const fd =
Ian Romanicked0626e2010-06-21 11:42:57 -0700262 add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
263 vec4_array_type, instructions, symtab);
Ian Romanickc77b2572010-04-07 16:59:46 -0700264
265 if (warn)
266 fd->warn_extension = "GL_ARB_draw_buffers";
267}
268
269
Eric Anholtb3f743a2010-03-25 14:48:25 -0700270static void
271generate_120_fs_variables(exec_list *instructions,
272 glsl_symbol_table *symtab)
273{
Eric Anholtb3f743a2010-03-25 14:48:25 -0700274 generate_110_fs_variables(instructions, symtab);
Ian Romanickc77b2572010-04-07 16:59:46 -0700275 generate_ARB_draw_buffers_fs_variables(instructions, symtab, false);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700276}
277
278static void
279generate_130_fs_variables(exec_list *instructions,
280 glsl_symbol_table *symtab)
281{
282 generate_120_fs_variables(instructions, symtab);
283
Ian Romanick8645a952010-04-07 16:47:44 -0700284 /* FINISHME: The size of this array is implementation dependent based on
285 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Eric Anholtb3f743a2010-03-25 14:48:25 -0700286 */
Ian Romanick8645a952010-04-07 16:47:44 -0700287 const glsl_type *const clip_distance_array_type =
288 glsl_type::get_array_instance(glsl_type::float_type, 8);
Ian Romanicked0626e2010-06-21 11:42:57 -0700289
290 /* FINISHME: gl_ClipDistance needs a real location assigned. */
291 add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
Ian Romanick8645a952010-04-07 16:47:44 -0700292 instructions, symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700293}
294
295static void
296initialize_fs_variables(exec_list *instructions,
297 struct _mesa_glsl_parse_state *state)
298{
299
300 switch (state->language_version) {
301 case 110:
302 generate_110_fs_variables(instructions, state->symbols);
303 break;
304 case 120:
305 generate_120_fs_variables(instructions, state->symbols);
306 break;
307 case 130:
308 generate_130_fs_variables(instructions, state->symbols);
309 break;
310 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700311
312
313 /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
314 * can basically ignore any extension settings for it.
315 */
316 if (state->language_version < 120) {
317 if (state->ARB_draw_buffers_enable) {
318 generate_ARB_draw_buffers_fs_variables(instructions, state->symbols,
319 state->ARB_draw_buffers_warn);
320 }
321 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700322}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800323
324void
325_mesa_glsl_initialize_variables(exec_list *instructions,
326 struct _mesa_glsl_parse_state *state)
327{
328 switch (state->target) {
329 case vertex_shader:
330 initialize_vs_variables(instructions, state);
331 break;
332 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700333 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800334 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700335 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800336 break;
Eric Anholt81f49a72010-04-29 17:57:28 -0700337 case ir_shader:
338 fprintf(stderr, "ir reader has no builtin variables");
339 exit(1);
340 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800341 }
342}