blob: 0c0d1278a41466e16b2a2527dd64d889ab595cbe [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
Ian Romanickcfb35362010-06-07 19:10:33 -0700100 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
101 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
102 * FINISHME: for now.
Eric Anholt78fe3c92010-03-28 01:46:48 -0700103 */
Ian Romanick3eba5932010-04-26 14:59:32 -0700104 const glsl_type *const mat4_array_type =
Ian Romanickcfb35362010-06-07 19:10:33 -0700105 glsl_type::get_array_instance(glsl_type::mat4_type, 4);
Ian Romanick3eba5932010-04-26 14:59:32 -0700106
107 add_variable("gl_TextureMatrix", ir_var_uniform, mat4_array_type,
108 instructions, symtab);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700109
110 /* FINISHME: Add support for gl_DepthRangeParameters */
111 /* FINISHME: Add support for gl_ClipPlane[] */
112 /* FINISHME: Add support for gl_PointParameters */
113
114 /* FINISHME: Add support for gl_MaterialParameters
115 * FINISHME: (glFrontMaterial, glBackMaterial)
116 */
117
Eric Anholtaa579432010-05-19 14:09:04 -0700118 /* FINISHME: The size of this array is implementation dependent based on the
119 * FINISHME: value of GL_MAX_TEXTURE_LIGHTS. GL_MAX_TEXTURE_LIGHTS must be
120 * FINISHME: at least 8, so hard-code 8 for now.
121 */
122 const glsl_type *const light_source_array_type =
123 glsl_type::get_array_instance(symtab->get_type("gl_LightSourceParameters"), 8);
124
125 add_variable("gl_LightSource", ir_var_uniform, light_source_array_type,
126 instructions, symtab);
127
Eric Anholt78fe3c92010-03-28 01:46:48 -0700128 /* FINISHME: Add support for gl_LightModel */
129 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
130 /* FINISHME: Add support for gl_TextureEnvColor[] */
131 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
132 /* FINISHME: Add support for gl_Fog */
133}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800134
135static void
136generate_110_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700137 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800138{
139 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
140 add_builtin_variable(& builtin_core_vs_variables[i],
141 instructions, symtab);
142 }
143
144 for (unsigned i = 0
145 ; i < Elements(builtin_110_deprecated_vs_variables)
146 ; i++) {
147 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
148 instructions, symtab);
149 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700150 generate_110_uniforms(instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800151
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700152 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700153 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
154 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
155 * FINISHME: for now.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800156 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700157 const glsl_type *const vec4_array_type =
Ian Romanickcfb35362010-06-07 19:10:33 -0700158 glsl_type::get_array_instance(glsl_type::vec4_type, 4);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700159
160 add_variable("gl_TexCoord", ir_var_out, vec4_array_type, instructions,
161 symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800162}
163
164
165static void
166generate_120_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700167 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800168{
169 /* GLSL version 1.20 did not add any built-in variables in the vertex
170 * shader.
171 */
172 generate_110_vs_variables(instructions, symtab);
173}
174
175
176static void
177generate_130_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700178 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800179{
180 generate_120_vs_variables(instructions, symtab);
181
182 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
183 add_builtin_variable(& builtin_130_vs_variables[i],
184 instructions, symtab);
185 }
186
Eric Anholt271e1992010-04-02 23:47:06 -0700187 /* FINISHME: The size of this array is implementation dependent based on
188 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800189 */
Eric Anholt271e1992010-04-02 23:47:06 -0700190 const glsl_type *const clip_distance_array_type =
191 glsl_type::get_array_instance(glsl_type::float_type, 8);
192 add_variable("gl_ClipDistance", ir_var_out, clip_distance_array_type,
193 instructions, symtab);
194
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800195}
196
197
198static void
199initialize_vs_variables(exec_list *instructions,
200 struct _mesa_glsl_parse_state *state)
201{
202
203 switch (state->language_version) {
204 case 110:
205 generate_110_vs_variables(instructions, state->symbols);
206 break;
207 case 120:
208 generate_120_vs_variables(instructions, state->symbols);
209 break;
210 case 130:
211 generate_130_vs_variables(instructions, state->symbols);
212 break;
213 }
214}
215
Eric Anholtb3f743a2010-03-25 14:48:25 -0700216static void
217generate_110_fs_variables(exec_list *instructions,
218 glsl_symbol_table *symtab)
219{
220 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
221 add_builtin_variable(& builtin_core_fs_variables[i],
222 instructions, symtab);
223 }
224
Eric Anholt0f09aea2010-03-27 12:48:57 -0700225 for (unsigned i = 0
226 ; i < Elements(builtin_110_deprecated_fs_variables)
227 ; i++) {
228 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
229 instructions, symtab);
230 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700231 generate_110_uniforms(instructions, symtab);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700232
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700233 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700234 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
235 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
236 * FINISHME: for now.
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700237 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700238 const glsl_type *const vec4_array_type =
Ian Romanickcfb35362010-06-07 19:10:33 -0700239 glsl_type::get_array_instance(glsl_type::vec4_type, 4);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700240
241 add_variable("gl_TexCoord", ir_var_in, vec4_array_type, instructions,
242 symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700243}
244
Ian Romanickc77b2572010-04-07 16:59:46 -0700245
246static void
247generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
248 glsl_symbol_table *symtab, bool warn)
249{
250 /* FINISHME: The size of this array is implementation dependent based on the
251 * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be
252 * FINISHME: at least 1, so hard-code 1 for now.
253 */
Ian Romanickc77b2572010-04-07 16:59:46 -0700254 const glsl_type *const vec4_array_type =
Eric Anholtec9e7382010-04-22 09:47:27 -0700255 glsl_type::get_array_instance(glsl_type::vec4_type, 1);
Ian Romanickc77b2572010-04-07 16:59:46 -0700256
257 ir_variable *const fd =
258 add_variable("gl_FragData", ir_var_out, vec4_array_type, instructions,
259 symtab);
260
261 if (warn)
262 fd->warn_extension = "GL_ARB_draw_buffers";
263}
264
265
Eric Anholtb3f743a2010-03-25 14:48:25 -0700266static void
267generate_120_fs_variables(exec_list *instructions,
268 glsl_symbol_table *symtab)
269{
Eric Anholtb3f743a2010-03-25 14:48:25 -0700270 generate_110_fs_variables(instructions, symtab);
Ian Romanickc77b2572010-04-07 16:59:46 -0700271 generate_ARB_draw_buffers_fs_variables(instructions, symtab, false);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700272}
273
274static void
275generate_130_fs_variables(exec_list *instructions,
276 glsl_symbol_table *symtab)
277{
278 generate_120_fs_variables(instructions, symtab);
279
Ian Romanick8645a952010-04-07 16:47:44 -0700280 /* FINISHME: The size of this array is implementation dependent based on
281 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Eric Anholtb3f743a2010-03-25 14:48:25 -0700282 */
Ian Romanick8645a952010-04-07 16:47:44 -0700283 const glsl_type *const clip_distance_array_type =
284 glsl_type::get_array_instance(glsl_type::float_type, 8);
285 add_variable("gl_ClipDistance", ir_var_in, clip_distance_array_type,
286 instructions, symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700287}
288
289static void
290initialize_fs_variables(exec_list *instructions,
291 struct _mesa_glsl_parse_state *state)
292{
293
294 switch (state->language_version) {
295 case 110:
296 generate_110_fs_variables(instructions, state->symbols);
297 break;
298 case 120:
299 generate_120_fs_variables(instructions, state->symbols);
300 break;
301 case 130:
302 generate_130_fs_variables(instructions, state->symbols);
303 break;
304 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700305
306
307 /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
308 * can basically ignore any extension settings for it.
309 */
310 if (state->language_version < 120) {
311 if (state->ARB_draw_buffers_enable) {
312 generate_ARB_draw_buffers_fs_variables(instructions, state->symbols,
313 state->ARB_draw_buffers_warn);
314 }
315 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700316}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800317
318void
319_mesa_glsl_initialize_variables(exec_list *instructions,
320 struct _mesa_glsl_parse_state *state)
321{
322 switch (state->target) {
323 case vertex_shader:
324 initialize_vs_variables(instructions, state);
325 break;
326 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700327 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800328 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700329 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800330 break;
Eric Anholt81f49a72010-04-29 17:57:28 -0700331 case ir_shader:
332 fprintf(stderr, "ir reader has no builtin variables");
333 exit(1);
334 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800335 }
336}