Merge remote branch 'origin/master' into nv50-compiler
[mesa.git] / src / glsl / ir_variable.cpp
1 /*
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 "ir.h"
25 #include "glsl_parser_extras.h"
26 #include "glsl_symbol_table.h"
27 #include "builtin_variables.h"
28
29 static void generate_ARB_draw_buffers_variables(exec_list *,
30 struct _mesa_glsl_parse_state *,
31 bool, _mesa_glsl_parser_targets);
32
33 static ir_variable *
34 add_variable(const char *name, enum ir_variable_mode mode, int slot,
35 const glsl_type *type, exec_list *instructions,
36 glsl_symbol_table *symtab)
37 {
38 ir_variable *var = new(symtab) ir_variable(type, name, mode);
39
40 switch (var->mode) {
41 case ir_var_auto:
42 case ir_var_in:
43 case ir_var_uniform:
44 var->read_only = true;
45 break;
46 case ir_var_inout:
47 case ir_var_out:
48 break;
49 default:
50 assert(0);
51 break;
52 }
53
54 var->location = slot;
55
56 /* Once the variable is created an initialized, add it to the symbol table
57 * and add the declaration to the IR stream.
58 */
59 instructions->push_tail(var);
60
61 symtab->add_variable(var->name, var);
62 return var;
63 }
64
65 static ir_variable *
66 add_uniform(exec_list *instructions,
67 struct _mesa_glsl_parse_state *state,
68 const char *name, const glsl_type *type)
69 {
70 return add_variable(name, ir_var_uniform, -1, type, instructions,
71 state->symbols);
72 }
73
74 static void
75 add_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
85 add_variable(proto->name, proto->mode, proto->slot, type, instructions,
86 symtab);
87 }
88
89 static void
90 add_builtin_constant(exec_list *instructions,
91 struct _mesa_glsl_parse_state *state,
92 const char *name, int value)
93 {
94 ir_variable *const var = add_variable(name, ir_var_auto,
95 -1, glsl_type::int_type,
96 instructions, state->symbols);
97 var->constant_value = new(var) ir_constant(value);
98 }
99
100 static void
101 generate_110_uniforms(exec_list *instructions,
102 struct _mesa_glsl_parse_state *state)
103 {
104 for (unsigned i = 0
105 ; i < Elements(builtin_110_deprecated_uniforms)
106 ; i++) {
107 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
108 instructions, state->symbols);
109 }
110
111 add_builtin_constant(instructions, state, "gl_MaxLights",
112 state->Const.MaxLights);
113 add_builtin_constant(instructions, state, "gl_MaxClipPlanes",
114 state->Const.MaxClipPlanes);
115 add_builtin_constant(instructions, state, "gl_MaxTextureUnits",
116 state->Const.MaxTextureUnits);
117 add_builtin_constant(instructions, state, "gl_MaxTextureCoords",
118 state->Const.MaxTextureCoords);
119 add_builtin_constant(instructions, state, "gl_MaxVertexAttribs",
120 state->Const.MaxVertexAttribs);
121 add_builtin_constant(instructions, state, "gl_MaxVertexUniformComponents",
122 state->Const.MaxVertexUniformComponents);
123 add_builtin_constant(instructions, state, "gl_MaxVaryingFloats",
124 state->Const.MaxVaryingFloats);
125 add_builtin_constant(instructions, state, "gl_MaxVertexTextureImageUnits",
126 state->Const.MaxVertexTextureImageUnits);
127 add_builtin_constant(instructions, state, "gl_MaxCombinedTextureImageUnits",
128 state->Const.MaxCombinedTextureImageUnits);
129 add_builtin_constant(instructions, state, "gl_MaxTextureImageUnits",
130 state->Const.MaxTextureImageUnits);
131 add_builtin_constant(instructions, state, "gl_MaxFragmentUniformComponents",
132 state->Const.MaxFragmentUniformComponents);
133
134 const glsl_type *const mat4_array_type =
135 glsl_type::get_array_instance(glsl_type::mat4_type,
136 state->Const.MaxTextureCoords);
137
138 add_uniform(instructions, state, "gl_TextureMatrix", mat4_array_type);
139
140 add_uniform(instructions, state, "gl_DepthRange",
141 state->symbols->get_type("gl_DepthRangeParameters"));
142
143 add_uniform(instructions, state, "gl_ClipPlane",
144 glsl_type::get_array_instance(glsl_type::vec4_type,
145 state->Const.MaxClipPlanes));
146 add_uniform(instructions, state, "gl_Point",
147 state->symbols->get_type("gl_PointParameters"));
148
149 const glsl_type *const material_parameters_type =
150 state->symbols->get_type("gl_MaterialParameters");
151 add_uniform(instructions, state, "gl_FrontMaterial", material_parameters_type);
152 add_uniform(instructions, state, "gl_BackMaterial", material_parameters_type);
153
154 const glsl_type *const light_source_array_type =
155 glsl_type::get_array_instance(state->symbols->get_type("gl_LightSourceParameters"), state->Const.MaxLights);
156
157 add_uniform(instructions, state, "gl_LightSource", light_source_array_type);
158
159 const glsl_type *const light_model_products_type =
160 state->symbols->get_type("gl_LightModelProducts");
161 add_uniform(instructions, state, "gl_FrontLightModelProduct",
162 light_model_products_type);
163 add_uniform(instructions, state, "gl_BackLightModelProduct",
164 light_model_products_type);
165
166 const glsl_type *const light_products_type =
167 glsl_type::get_array_instance(state->symbols->get_type("gl_LightProducts"),
168 state->Const.MaxLights);
169 add_uniform(instructions, state, "gl_FrontLightProduct", light_products_type);
170 add_uniform(instructions, state, "gl_BackLightProduct", light_products_type);
171
172 add_uniform(instructions, state, "gl_TextureEnvColor",
173 glsl_type::get_array_instance(glsl_type::vec4_type,
174 state->Const.MaxTextureUnits));
175
176 const glsl_type *const texcoords_vec4 =
177 glsl_type::get_array_instance(glsl_type::vec4_type,
178 state->Const.MaxTextureCoords);
179 add_uniform(instructions, state, "gl_EyePlaneS", texcoords_vec4);
180 add_uniform(instructions, state, "gl_EyePlaneT", texcoords_vec4);
181 add_uniform(instructions, state, "gl_EyePlaneR", texcoords_vec4);
182 add_uniform(instructions, state, "gl_EyePlaneQ", texcoords_vec4);
183 add_uniform(instructions, state, "gl_ObjectPlaneS", texcoords_vec4);
184 add_uniform(instructions, state, "gl_ObjectPlaneT", texcoords_vec4);
185 add_uniform(instructions, state, "gl_ObjectPlaneR", texcoords_vec4);
186 add_uniform(instructions, state, "gl_ObjectPlaneQ", texcoords_vec4);
187
188 add_uniform(instructions, state, "gl_Fog",
189 state->symbols->get_type("gl_FogParameters"));
190 }
191
192 static void
193 generate_110_vs_variables(exec_list *instructions,
194 struct _mesa_glsl_parse_state *state)
195 {
196 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
197 add_builtin_variable(& builtin_core_vs_variables[i],
198 instructions, state->symbols);
199 }
200
201 for (unsigned i = 0
202 ; i < Elements(builtin_110_deprecated_vs_variables)
203 ; i++) {
204 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
205 instructions, state->symbols);
206 }
207 generate_110_uniforms(instructions, state);
208
209 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
210 *
211 * "As with all arrays, indices used to subscript gl_TexCoord must
212 * either be an integral constant expressions, or this array must be
213 * re-declared by the shader with a size. The size can be at most
214 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
215 * implementation in preserving varying resources."
216 */
217 const glsl_type *const vec4_array_type =
218 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
219
220 add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
221 instructions, state->symbols);
222
223 generate_ARB_draw_buffers_variables(instructions, state, false,
224 vertex_shader);
225 }
226
227
228 static void
229 generate_120_vs_variables(exec_list *instructions,
230 struct _mesa_glsl_parse_state *state)
231 {
232 /* GLSL version 1.20 did not add any built-in variables in the vertex
233 * shader.
234 */
235 generate_110_vs_variables(instructions, state);
236 }
237
238
239 static void
240 generate_130_vs_variables(exec_list *instructions,
241 struct _mesa_glsl_parse_state *state)
242 {
243 generate_120_vs_variables(instructions, state);
244
245 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
246 add_builtin_variable(& builtin_130_vs_variables[i],
247 instructions, state->symbols);
248 }
249
250 const glsl_type *const clip_distance_array_type =
251 glsl_type::get_array_instance(glsl_type::float_type,
252 state->Const.MaxClipPlanes);
253
254 /* FINISHME: gl_ClipDistance needs a real location assigned. */
255 add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
256 instructions, state->symbols);
257
258 }
259
260
261 static void
262 initialize_vs_variables(exec_list *instructions,
263 struct _mesa_glsl_parse_state *state)
264 {
265
266 switch (state->language_version) {
267 case 110:
268 generate_110_vs_variables(instructions, state);
269 break;
270 case 120:
271 generate_120_vs_variables(instructions, state);
272 break;
273 case 130:
274 generate_130_vs_variables(instructions, state);
275 break;
276 }
277 }
278
279 static void
280 generate_110_fs_variables(exec_list *instructions,
281 struct _mesa_glsl_parse_state *state)
282 {
283 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
284 add_builtin_variable(& builtin_core_fs_variables[i],
285 instructions, state->symbols);
286 }
287
288 for (unsigned i = 0
289 ; i < Elements(builtin_110_deprecated_fs_variables)
290 ; i++) {
291 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
292 instructions, state->symbols);
293 }
294 generate_110_uniforms(instructions, state);
295
296 /* From page 54 (page 60 of the PDF) of the GLSL 1.20 spec:
297 *
298 * "As with all arrays, indices used to subscript gl_TexCoord must
299 * either be an integral constant expressions, or this array must be
300 * re-declared by the shader with a size. The size can be at most
301 * gl_MaxTextureCoords. Using indexes close to 0 may aid the
302 * implementation in preserving varying resources."
303 */
304 const glsl_type *const vec4_array_type =
305 glsl_type::get_array_instance(glsl_type::vec4_type, 0);
306
307 add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
308 instructions, state->symbols);
309
310 generate_ARB_draw_buffers_variables(instructions, state, false,
311 fragment_shader);
312 }
313
314
315 static void
316 generate_ARB_draw_buffers_variables(exec_list *instructions,
317 struct _mesa_glsl_parse_state *state,
318 bool warn, _mesa_glsl_parser_targets target)
319 {
320 /* gl_MaxDrawBuffers is available in all shader stages.
321 */
322 ir_variable *const mdb =
323 add_variable("gl_MaxDrawBuffers", ir_var_auto, -1,
324 glsl_type::int_type, instructions, state->symbols);
325
326 if (warn)
327 mdb->warn_extension = "GL_ARB_draw_buffers";
328
329 mdb->constant_value = new(mdb)
330 ir_constant(int(state->Const.MaxDrawBuffers));
331
332
333 /* gl_FragData is only available in the fragment shader.
334 */
335 if (target == fragment_shader) {
336 const glsl_type *const vec4_array_type =
337 glsl_type::get_array_instance(glsl_type::vec4_type,
338 state->Const.MaxDrawBuffers);
339
340 ir_variable *const fd =
341 add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
342 vec4_array_type, instructions, state->symbols);
343
344 if (warn)
345 fd->warn_extension = "GL_ARB_draw_buffers";
346 }
347 }
348
349
350 static void
351 generate_120_fs_variables(exec_list *instructions,
352 struct _mesa_glsl_parse_state *state)
353 {
354 generate_110_fs_variables(instructions, state);
355
356 for (unsigned i = 0
357 ; i < Elements(builtin_120_fs_variables)
358 ; i++) {
359 add_builtin_variable(& builtin_120_fs_variables[i],
360 instructions, state->symbols);
361 }
362 }
363
364 static void
365 generate_130_fs_variables(exec_list *instructions,
366 struct _mesa_glsl_parse_state *state)
367 {
368 generate_120_fs_variables(instructions, state);
369
370 const glsl_type *const clip_distance_array_type =
371 glsl_type::get_array_instance(glsl_type::float_type,
372 state->Const.MaxClipPlanes);
373
374 /* FINISHME: gl_ClipDistance needs a real location assigned. */
375 add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
376 instructions, state->symbols);
377 }
378
379 static void
380 initialize_fs_variables(exec_list *instructions,
381 struct _mesa_glsl_parse_state *state)
382 {
383
384 switch (state->language_version) {
385 case 110:
386 generate_110_fs_variables(instructions, state);
387 break;
388 case 120:
389 generate_120_fs_variables(instructions, state);
390 break;
391 case 130:
392 generate_130_fs_variables(instructions, state);
393 break;
394 }
395 }
396
397 void
398 _mesa_glsl_initialize_variables(exec_list *instructions,
399 struct _mesa_glsl_parse_state *state)
400 {
401 switch (state->target) {
402 case vertex_shader:
403 initialize_vs_variables(instructions, state);
404 break;
405 case geometry_shader:
406 break;
407 case fragment_shader:
408 initialize_fs_variables(instructions, state);
409 break;
410 case ir_shader:
411 fprintf(stderr, "ir reader has no builtin variables");
412 exit(1);
413 break;
414 }
415 }