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