blob: 49d8e3dcfb5cca388a526143cef99effd9756084 [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 Romanick3f9a73d2010-04-02 11:59:57 -070034add_variable(const char *name, enum ir_variable_mode mode,
35 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
62 /* Once the variable is created an initialized, add it to the symbol table
63 * and add the declaration to the IR stream.
64 */
65 instructions->push_tail(var);
66
Ian Romanick8bde4ce2010-03-19 11:57:24 -070067 symtab->add_variable(var->name, var);
Ian Romanickc77b2572010-04-07 16:59:46 -070068 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080069}
70
Ian Romanick3f9a73d2010-04-02 11:59:57 -070071
72static void
73add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
74 glsl_symbol_table *symtab)
75{
76 /* Create a new variable declaration from the description supplied by
77 * the caller.
78 */
79 const glsl_type *const type = symtab->get_type(proto->type);
80
81 assert(type != NULL);
82
83 add_variable(proto->name, proto->mode, type, instructions, symtab);
84}
85
86
Eric Anholt78fe3c92010-03-28 01:46:48 -070087static void
88generate_110_uniforms(exec_list *instructions,
89 glsl_symbol_table *symtab)
90{
91 for (unsigned i = 0
92 ; i < Elements(builtin_110_deprecated_uniforms)
93 ; i++) {
94 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
95 instructions, symtab);
96 }
97
Ian Romanick3eba5932010-04-26 14:59:32 -070098 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -070099 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
100 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
101 * FINISHME: 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 =
Ian Romanickcfb35362010-06-07 19:10:33 -0700104 glsl_type::get_array_instance(glsl_type::mat4_type, 4);
Ian Romanick3eba5932010-04-26 14:59:32 -0700105
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
Ian Romanickcfb35362010-06-07 19:10:33 -0700152 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
153 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
154 * FINISHME: for now.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800155 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700156 const glsl_type *const vec4_array_type =
Ian Romanickcfb35362010-06-07 19:10:33 -0700157 glsl_type::get_array_instance(glsl_type::vec4_type, 4);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700158
159 add_variable("gl_TexCoord", ir_var_out, vec4_array_type, instructions,
160 symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800161}
162
163
164static void
165generate_120_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700166 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800167{
168 /* GLSL version 1.20 did not add any built-in variables in the vertex
169 * shader.
170 */
171 generate_110_vs_variables(instructions, symtab);
172}
173
174
175static void
176generate_130_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700177 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800178{
179 generate_120_vs_variables(instructions, symtab);
180
181 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
182 add_builtin_variable(& builtin_130_vs_variables[i],
183 instructions, symtab);
184 }
185
Eric Anholt271e1992010-04-02 23:47:06 -0700186 /* FINISHME: The size of this array is implementation dependent based on
187 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800188 */
Eric Anholt271e1992010-04-02 23:47:06 -0700189 const glsl_type *const clip_distance_array_type =
190 glsl_type::get_array_instance(glsl_type::float_type, 8);
191 add_variable("gl_ClipDistance", ir_var_out, clip_distance_array_type,
192 instructions, symtab);
193
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800194}
195
196
197static void
198initialize_vs_variables(exec_list *instructions,
199 struct _mesa_glsl_parse_state *state)
200{
201
202 switch (state->language_version) {
203 case 110:
204 generate_110_vs_variables(instructions, state->symbols);
205 break;
206 case 120:
207 generate_120_vs_variables(instructions, state->symbols);
208 break;
209 case 130:
210 generate_130_vs_variables(instructions, state->symbols);
211 break;
212 }
213}
214
Eric Anholtb3f743a2010-03-25 14:48:25 -0700215static void
216generate_110_fs_variables(exec_list *instructions,
217 glsl_symbol_table *symtab)
218{
219 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
220 add_builtin_variable(& builtin_core_fs_variables[i],
221 instructions, symtab);
222 }
223
Eric Anholt0f09aea2010-03-27 12:48:57 -0700224 for (unsigned i = 0
225 ; i < Elements(builtin_110_deprecated_fs_variables)
226 ; i++) {
227 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
228 instructions, symtab);
229 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700230 generate_110_uniforms(instructions, symtab);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700231
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700232 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700233 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
234 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
235 * FINISHME: for now.
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700236 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700237 const glsl_type *const vec4_array_type =
Ian Romanickcfb35362010-06-07 19:10:33 -0700238 glsl_type::get_array_instance(glsl_type::vec4_type, 4);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700239
240 add_variable("gl_TexCoord", ir_var_in, vec4_array_type, instructions,
241 symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700242}
243
Ian Romanickc77b2572010-04-07 16:59:46 -0700244
245static void
246generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
247 glsl_symbol_table *symtab, bool warn)
248{
249 /* FINISHME: The size of this array is implementation dependent based on the
250 * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be
251 * FINISHME: at least 1, so hard-code 1 for now.
252 */
Ian Romanickc77b2572010-04-07 16:59:46 -0700253 const glsl_type *const vec4_array_type =
Eric Anholtec9e7382010-04-22 09:47:27 -0700254 glsl_type::get_array_instance(glsl_type::vec4_type, 1);
Ian Romanickc77b2572010-04-07 16:59:46 -0700255
256 ir_variable *const fd =
257 add_variable("gl_FragData", ir_var_out, vec4_array_type, instructions,
258 symtab);
259
260 if (warn)
261 fd->warn_extension = "GL_ARB_draw_buffers";
262}
263
264
Eric Anholtb3f743a2010-03-25 14:48:25 -0700265static void
266generate_120_fs_variables(exec_list *instructions,
267 glsl_symbol_table *symtab)
268{
Eric Anholtb3f743a2010-03-25 14:48:25 -0700269 generate_110_fs_variables(instructions, symtab);
Ian Romanickc77b2572010-04-07 16:59:46 -0700270 generate_ARB_draw_buffers_fs_variables(instructions, symtab, false);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700271}
272
273static void
274generate_130_fs_variables(exec_list *instructions,
275 glsl_symbol_table *symtab)
276{
277 generate_120_fs_variables(instructions, symtab);
278
Ian Romanick8645a952010-04-07 16:47:44 -0700279 /* FINISHME: The size of this array is implementation dependent based on
280 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Eric Anholtb3f743a2010-03-25 14:48:25 -0700281 */
Ian Romanick8645a952010-04-07 16:47:44 -0700282 const glsl_type *const clip_distance_array_type =
283 glsl_type::get_array_instance(glsl_type::float_type, 8);
284 add_variable("gl_ClipDistance", ir_var_in, clip_distance_array_type,
285 instructions, symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700286}
287
288static void
289initialize_fs_variables(exec_list *instructions,
290 struct _mesa_glsl_parse_state *state)
291{
292
293 switch (state->language_version) {
294 case 110:
295 generate_110_fs_variables(instructions, state->symbols);
296 break;
297 case 120:
298 generate_120_fs_variables(instructions, state->symbols);
299 break;
300 case 130:
301 generate_130_fs_variables(instructions, state->symbols);
302 break;
303 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700304
305
306 /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
307 * can basically ignore any extension settings for it.
308 */
309 if (state->language_version < 120) {
310 if (state->ARB_draw_buffers_enable) {
311 generate_ARB_draw_buffers_fs_variables(instructions, state->symbols,
312 state->ARB_draw_buffers_warn);
313 }
314 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700315}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800316
317void
318_mesa_glsl_initialize_variables(exec_list *instructions,
319 struct _mesa_glsl_parse_state *state)
320{
321 switch (state->target) {
322 case vertex_shader:
323 initialize_vs_variables(instructions, state);
324 break;
325 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700326 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800327 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700328 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800329 break;
Eric Anholt81f49a72010-04-29 17:57:28 -0700330 case ir_shader:
331 fprintf(stderr, "ir reader has no builtin variables");
332 exit(1);
333 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800334 }
335}