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