blob: af8ad2c3182e149e809dfb9bff31e3d0d24b0a15 [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 Anholt81f49a72010-04-29 17:57:28 -070024#include <stdio.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 "ir.h"
28#include "builtin_variables.h"
29
30#ifndef Elements
31#define Elements(x) (sizeof(x)/sizeof(*(x)))
32#endif
33
Ian Romanickc77b2572010-04-07 16:59:46 -070034static ir_variable *
Ian Romanick3f9a73d2010-04-02 11:59:57 -070035add_variable(const char *name, enum ir_variable_mode mode,
36 const glsl_type *type, exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070037 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080038{
Ian Romanick3f9a73d2010-04-02 11:59:57 -070039 ir_variable *var = new ir_variable(type, name);
Ian Romanickadfb0cd2010-03-10 10:43:16 -080040
Ian Romanick3f9a73d2010-04-02 11:59:57 -070041 var->mode = mode;
Eric Anholt71df19f2010-04-19 11:10:37 -070042 switch (var->mode) {
43 case ir_var_in:
44 var->shader_in = true;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080045 var->read_only = true;
Eric Anholt71df19f2010-04-19 11:10:37 -070046 break;
47 case ir_var_inout:
48 var->shader_in = true;
49 var->shader_out = true;
Ian Romanickff236fa2010-04-21 15:08:08 -070050 break;
Eric Anholt71df19f2010-04-19 11:10:37 -070051 case ir_var_out:
52 var->shader_out = true;
53 break;
54 case ir_var_uniform:
55 var->shader_in = true;
56 var->read_only = true;
57 break;
58 default:
59 assert(0);
60 break;
61 }
Ian Romanickadfb0cd2010-03-10 10:43:16 -080062
63 /* Once the variable is created an initialized, add it to the symbol table
64 * and add the declaration to the IR stream.
65 */
66 instructions->push_tail(var);
67
Ian Romanick8bde4ce2010-03-19 11:57:24 -070068 symtab->add_variable(var->name, var);
Ian Romanickc77b2572010-04-07 16:59:46 -070069 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080070}
71
Ian Romanick3f9a73d2010-04-02 11:59:57 -070072
73static void
74add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
75 glsl_symbol_table *symtab)
76{
77 /* Create a new variable declaration from the description supplied by
78 * the caller.
79 */
80 const glsl_type *const type = symtab->get_type(proto->type);
81
82 assert(type != NULL);
83
84 add_variable(proto->name, proto->mode, type, instructions, symtab);
85}
86
87
Eric Anholt78fe3c92010-03-28 01:46:48 -070088static void
89generate_110_uniforms(exec_list *instructions,
90 glsl_symbol_table *symtab)
91{
92 for (unsigned i = 0
93 ; i < Elements(builtin_110_deprecated_uniforms)
94 ; i++) {
95 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
96 instructions, symtab);
97 }
98
Ian Romanick3eba5932010-04-26 14:59:32 -070099 /* FINISHME: The size of this array is implementation dependent based on the
100 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
101 * FINISHME: at least 2, so hard-code 2 for now.
Eric Anholt78fe3c92010-03-28 01:46:48 -0700102 */
Ian Romanick3eba5932010-04-26 14:59:32 -0700103 const glsl_type *const mat4_array_type =
104 glsl_type::get_array_instance(glsl_type::mat4_type, 2);
105
106 add_variable("gl_TextureMatrix", ir_var_uniform, mat4_array_type,
107 instructions, symtab);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700108
109 /* FINISHME: Add support for gl_DepthRangeParameters */
110 /* FINISHME: Add support for gl_ClipPlane[] */
111 /* FINISHME: Add support for gl_PointParameters */
112
113 /* FINISHME: Add support for gl_MaterialParameters
114 * FINISHME: (glFrontMaterial, glBackMaterial)
115 */
116
Eric Anholtaa579432010-05-19 14:09:04 -0700117 /* FINISHME: The size of this array is implementation dependent based on the
118 * FINISHME: value of GL_MAX_TEXTURE_LIGHTS. GL_MAX_TEXTURE_LIGHTS must be
119 * FINISHME: at least 8, so hard-code 8 for now.
120 */
121 const glsl_type *const light_source_array_type =
122 glsl_type::get_array_instance(symtab->get_type("gl_LightSourceParameters"), 8);
123
124 add_variable("gl_LightSource", ir_var_uniform, light_source_array_type,
125 instructions, symtab);
126
Eric Anholt78fe3c92010-03-28 01:46:48 -0700127 /* FINISHME: Add support for gl_LightModel */
128 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
129 /* FINISHME: Add support for gl_TextureEnvColor[] */
130 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
131 /* FINISHME: Add support for gl_Fog */
132}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800133
134static void
135generate_110_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700136 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800137{
138 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
139 add_builtin_variable(& builtin_core_vs_variables[i],
140 instructions, symtab);
141 }
142
143 for (unsigned i = 0
144 ; i < Elements(builtin_110_deprecated_vs_variables)
145 ; i++) {
146 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
147 instructions, symtab);
148 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700149 generate_110_uniforms(instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800150
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700151 /* FINISHME: The size of this array is implementation dependent based on the
152 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
153 * FINISHME: at least 2, so hard-code 2 for now.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800154 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700155 const glsl_type *const vec4_array_type =
Eric Anholtec9e7382010-04-22 09:47:27 -0700156 glsl_type::get_array_instance(glsl_type::vec4_type, 2);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700157
158 add_variable("gl_TexCoord", ir_var_out, vec4_array_type, instructions,
159 symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800160}
161
162
163static void
164generate_120_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700165 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800166{
167 /* GLSL version 1.20 did not add any built-in variables in the vertex
168 * shader.
169 */
170 generate_110_vs_variables(instructions, symtab);
171}
172
173
174static void
175generate_130_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700176 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800177{
178 generate_120_vs_variables(instructions, symtab);
179
180 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
181 add_builtin_variable(& builtin_130_vs_variables[i],
182 instructions, symtab);
183 }
184
Eric Anholt271e1992010-04-02 23:47:06 -0700185 /* FINISHME: The size of this array is implementation dependent based on
186 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800187 */
Eric Anholt271e1992010-04-02 23:47:06 -0700188 const glsl_type *const clip_distance_array_type =
189 glsl_type::get_array_instance(glsl_type::float_type, 8);
190 add_variable("gl_ClipDistance", ir_var_out, clip_distance_array_type,
191 instructions, symtab);
192
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800193}
194
195
196static void
197initialize_vs_variables(exec_list *instructions,
198 struct _mesa_glsl_parse_state *state)
199{
200
201 switch (state->language_version) {
202 case 110:
203 generate_110_vs_variables(instructions, state->symbols);
204 break;
205 case 120:
206 generate_120_vs_variables(instructions, state->symbols);
207 break;
208 case 130:
209 generate_130_vs_variables(instructions, state->symbols);
210 break;
211 }
212}
213
Eric Anholtb3f743a2010-03-25 14:48:25 -0700214static void
215generate_110_fs_variables(exec_list *instructions,
216 glsl_symbol_table *symtab)
217{
218 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
219 add_builtin_variable(& builtin_core_fs_variables[i],
220 instructions, symtab);
221 }
222
Eric Anholt0f09aea2010-03-27 12:48:57 -0700223 for (unsigned i = 0
224 ; i < Elements(builtin_110_deprecated_fs_variables)
225 ; i++) {
226 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
227 instructions, symtab);
228 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700229 generate_110_uniforms(instructions, symtab);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700230
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700231 /* FINISHME: The size of this array is implementation dependent based on the
232 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
233 * FINISHME: at least 2, so hard-code 2 for now.
234 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700235 const glsl_type *const vec4_array_type =
Eric Anholtec9e7382010-04-22 09:47:27 -0700236 glsl_type::get_array_instance(glsl_type::vec4_type, 2);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700237
238 add_variable("gl_TexCoord", ir_var_in, vec4_array_type, instructions,
239 symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700240}
241
Ian Romanickc77b2572010-04-07 16:59:46 -0700242
243static void
244generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
245 glsl_symbol_table *symtab, bool warn)
246{
247 /* FINISHME: The size of this array is implementation dependent based on the
248 * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be
249 * FINISHME: at least 1, so hard-code 1 for now.
250 */
Ian Romanickc77b2572010-04-07 16:59:46 -0700251 const glsl_type *const vec4_array_type =
Eric Anholtec9e7382010-04-22 09:47:27 -0700252 glsl_type::get_array_instance(glsl_type::vec4_type, 1);
Ian Romanickc77b2572010-04-07 16:59:46 -0700253
254 ir_variable *const fd =
255 add_variable("gl_FragData", ir_var_out, vec4_array_type, instructions,
256 symtab);
257
258 if (warn)
259 fd->warn_extension = "GL_ARB_draw_buffers";
260}
261
262
Eric Anholtb3f743a2010-03-25 14:48:25 -0700263static void
264generate_120_fs_variables(exec_list *instructions,
265 glsl_symbol_table *symtab)
266{
Eric Anholtb3f743a2010-03-25 14:48:25 -0700267 generate_110_fs_variables(instructions, symtab);
Ian Romanickc77b2572010-04-07 16:59:46 -0700268 generate_ARB_draw_buffers_fs_variables(instructions, symtab, false);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700269}
270
271static void
272generate_130_fs_variables(exec_list *instructions,
273 glsl_symbol_table *symtab)
274{
275 generate_120_fs_variables(instructions, symtab);
276
Ian Romanick8645a952010-04-07 16:47:44 -0700277 /* FINISHME: The size of this array is implementation dependent based on
278 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Eric Anholtb3f743a2010-03-25 14:48:25 -0700279 */
Ian Romanick8645a952010-04-07 16:47:44 -0700280 const glsl_type *const clip_distance_array_type =
281 glsl_type::get_array_instance(glsl_type::float_type, 8);
282 add_variable("gl_ClipDistance", ir_var_in, clip_distance_array_type,
283 instructions, symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700284}
285
286static void
287initialize_fs_variables(exec_list *instructions,
288 struct _mesa_glsl_parse_state *state)
289{
290
291 switch (state->language_version) {
292 case 110:
293 generate_110_fs_variables(instructions, state->symbols);
294 break;
295 case 120:
296 generate_120_fs_variables(instructions, state->symbols);
297 break;
298 case 130:
299 generate_130_fs_variables(instructions, state->symbols);
300 break;
301 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700302
303
304 /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
305 * can basically ignore any extension settings for it.
306 */
307 if (state->language_version < 120) {
308 if (state->ARB_draw_buffers_enable) {
309 generate_ARB_draw_buffers_fs_variables(instructions, state->symbols,
310 state->ARB_draw_buffers_warn);
311 }
312 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700313}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800314
315void
316_mesa_glsl_initialize_variables(exec_list *instructions,
317 struct _mesa_glsl_parse_state *state)
318{
319 switch (state->target) {
320 case vertex_shader:
321 initialize_vs_variables(instructions, state);
322 break;
323 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700324 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800325 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700326 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800327 break;
Eric Anholt81f49a72010-04-29 17:57:28 -0700328 case ir_shader:
329 fprintf(stderr, "ir reader has no builtin variables");
330 exit(1);
331 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800332 }
333}