blob: 2c2b57a71146eb741e1b6f338ff693f26192b7af [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
117 /* FINISHME: Add support for gl_LightSource[] */
118 /* FINISHME: Add support for gl_LightModel */
119 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
120 /* FINISHME: Add support for gl_TextureEnvColor[] */
121 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
122 /* FINISHME: Add support for gl_Fog */
123}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800124
125static void
126generate_110_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700127 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800128{
129 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
130 add_builtin_variable(& builtin_core_vs_variables[i],
131 instructions, symtab);
132 }
133
134 for (unsigned i = 0
135 ; i < Elements(builtin_110_deprecated_vs_variables)
136 ; i++) {
137 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
138 instructions, symtab);
139 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700140 generate_110_uniforms(instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800141
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700142 /* FINISHME: The size of this array is implementation dependent based on the
143 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
144 * FINISHME: at least 2, so hard-code 2 for now.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800145 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700146 const glsl_type *const vec4_array_type =
Eric Anholtec9e7382010-04-22 09:47:27 -0700147 glsl_type::get_array_instance(glsl_type::vec4_type, 2);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700148
149 add_variable("gl_TexCoord", ir_var_out, vec4_array_type, instructions,
150 symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800151}
152
153
154static void
155generate_120_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700156 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800157{
158 /* GLSL version 1.20 did not add any built-in variables in the vertex
159 * shader.
160 */
161 generate_110_vs_variables(instructions, symtab);
162}
163
164
165static void
166generate_130_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 generate_120_vs_variables(instructions, symtab);
170
171 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
172 add_builtin_variable(& builtin_130_vs_variables[i],
173 instructions, symtab);
174 }
175
Eric Anholt271e1992010-04-02 23:47:06 -0700176 /* FINISHME: The size of this array is implementation dependent based on
177 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800178 */
Eric Anholt271e1992010-04-02 23:47:06 -0700179 const glsl_type *const clip_distance_array_type =
180 glsl_type::get_array_instance(glsl_type::float_type, 8);
181 add_variable("gl_ClipDistance", ir_var_out, clip_distance_array_type,
182 instructions, symtab);
183
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800184}
185
186
187static void
188initialize_vs_variables(exec_list *instructions,
189 struct _mesa_glsl_parse_state *state)
190{
191
192 switch (state->language_version) {
193 case 110:
194 generate_110_vs_variables(instructions, state->symbols);
195 break;
196 case 120:
197 generate_120_vs_variables(instructions, state->symbols);
198 break;
199 case 130:
200 generate_130_vs_variables(instructions, state->symbols);
201 break;
202 }
203}
204
Eric Anholtb3f743a2010-03-25 14:48:25 -0700205static void
206generate_110_fs_variables(exec_list *instructions,
207 glsl_symbol_table *symtab)
208{
209 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
210 add_builtin_variable(& builtin_core_fs_variables[i],
211 instructions, symtab);
212 }
213
Eric Anholt0f09aea2010-03-27 12:48:57 -0700214 for (unsigned i = 0
215 ; i < Elements(builtin_110_deprecated_fs_variables)
216 ; i++) {
217 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
218 instructions, symtab);
219 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700220 generate_110_uniforms(instructions, symtab);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700221
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700222 /* FINISHME: The size of this array is implementation dependent based on the
223 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
224 * FINISHME: at least 2, so hard-code 2 for now.
225 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700226 const glsl_type *const vec4_array_type =
Eric Anholtec9e7382010-04-22 09:47:27 -0700227 glsl_type::get_array_instance(glsl_type::vec4_type, 2);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700228
229 add_variable("gl_TexCoord", ir_var_in, vec4_array_type, instructions,
230 symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700231}
232
Ian Romanickc77b2572010-04-07 16:59:46 -0700233
234static void
235generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
236 glsl_symbol_table *symtab, bool warn)
237{
238 /* FINISHME: The size of this array is implementation dependent based on the
239 * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be
240 * FINISHME: at least 1, so hard-code 1 for now.
241 */
Ian Romanickc77b2572010-04-07 16:59:46 -0700242 const glsl_type *const vec4_array_type =
Eric Anholtec9e7382010-04-22 09:47:27 -0700243 glsl_type::get_array_instance(glsl_type::vec4_type, 1);
Ian Romanickc77b2572010-04-07 16:59:46 -0700244
245 ir_variable *const fd =
246 add_variable("gl_FragData", ir_var_out, vec4_array_type, instructions,
247 symtab);
248
249 if (warn)
250 fd->warn_extension = "GL_ARB_draw_buffers";
251}
252
253
Eric Anholtb3f743a2010-03-25 14:48:25 -0700254static void
255generate_120_fs_variables(exec_list *instructions,
256 glsl_symbol_table *symtab)
257{
Eric Anholtb3f743a2010-03-25 14:48:25 -0700258 generate_110_fs_variables(instructions, symtab);
Ian Romanickc77b2572010-04-07 16:59:46 -0700259 generate_ARB_draw_buffers_fs_variables(instructions, symtab, false);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700260}
261
262static void
263generate_130_fs_variables(exec_list *instructions,
264 glsl_symbol_table *symtab)
265{
266 generate_120_fs_variables(instructions, symtab);
267
Ian Romanick8645a952010-04-07 16:47:44 -0700268 /* FINISHME: The size of this array is implementation dependent based on
269 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Eric Anholtb3f743a2010-03-25 14:48:25 -0700270 */
Ian Romanick8645a952010-04-07 16:47:44 -0700271 const glsl_type *const clip_distance_array_type =
272 glsl_type::get_array_instance(glsl_type::float_type, 8);
273 add_variable("gl_ClipDistance", ir_var_in, clip_distance_array_type,
274 instructions, symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700275}
276
277static void
278initialize_fs_variables(exec_list *instructions,
279 struct _mesa_glsl_parse_state *state)
280{
281
282 switch (state->language_version) {
283 case 110:
284 generate_110_fs_variables(instructions, state->symbols);
285 break;
286 case 120:
287 generate_120_fs_variables(instructions, state->symbols);
288 break;
289 case 130:
290 generate_130_fs_variables(instructions, state->symbols);
291 break;
292 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700293
294
295 /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
296 * can basically ignore any extension settings for it.
297 */
298 if (state->language_version < 120) {
299 if (state->ARB_draw_buffers_enable) {
300 generate_ARB_draw_buffers_fs_variables(instructions, state->symbols,
301 state->ARB_draw_buffers_warn);
302 }
303 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700304}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800305
306void
307_mesa_glsl_initialize_variables(exec_list *instructions,
308 struct _mesa_glsl_parse_state *state)
309{
310 switch (state->target) {
311 case vertex_shader:
312 initialize_vs_variables(instructions, state);
313 break;
314 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700315 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800316 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700317 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800318 break;
Eric Anholt81f49a72010-04-29 17:57:28 -0700319 case ir_shader:
320 fprintf(stderr, "ir reader has no builtin variables");
321 exit(1);
322 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800323 }
324}