blob: 29f3fc5a632b27a9bbb472c5cd34d2bca540268b [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
24#include "glsl_parser_extras.h"
Ian Romanick8bde4ce2010-03-19 11:57:24 -070025#include "glsl_symbol_table.h"
Ian Romanickadfb0cd2010-03-10 10:43:16 -080026#include "ir.h"
27#include "builtin_variables.h"
28
29#ifndef Elements
30#define Elements(x) (sizeof(x)/sizeof(*(x)))
31#endif
32
33static void
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;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080041 if (var->mode != ir_var_out)
42 var->read_only = true;
43
44
45 /* Once the variable is created an initialized, add it to the symbol table
46 * and add the declaration to the IR stream.
47 */
48 instructions->push_tail(var);
49
Ian Romanick8bde4ce2010-03-19 11:57:24 -070050 symtab->add_variable(var->name, var);
Ian Romanickadfb0cd2010-03-10 10:43:16 -080051}
52
Ian Romanick3f9a73d2010-04-02 11:59:57 -070053
54static void
55add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
56 glsl_symbol_table *symtab)
57{
58 /* Create a new variable declaration from the description supplied by
59 * the caller.
60 */
61 const glsl_type *const type = symtab->get_type(proto->type);
62
63 assert(type != NULL);
64
65 add_variable(proto->name, proto->mode, type, instructions, symtab);
66}
67
68
Eric Anholt78fe3c92010-03-28 01:46:48 -070069static void
70generate_110_uniforms(exec_list *instructions,
71 glsl_symbol_table *symtab)
72{
73 for (unsigned i = 0
74 ; i < Elements(builtin_110_deprecated_uniforms)
75 ; i++) {
76 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
77 instructions, symtab);
78 }
79
80 /* FINISHME: Add support for gl_TextureMatrix[]. The size of this array is
81 * FINISHME: implementation dependent based on the value of
82 * FINISHME: GL_MAX_TEXTURE_COORDS.
83 */
84
85 /* FINISHME: Add support for gl_DepthRangeParameters */
86 /* FINISHME: Add support for gl_ClipPlane[] */
87 /* FINISHME: Add support for gl_PointParameters */
88
89 /* FINISHME: Add support for gl_MaterialParameters
90 * FINISHME: (glFrontMaterial, glBackMaterial)
91 */
92
93 /* FINISHME: Add support for gl_LightSource[] */
94 /* FINISHME: Add support for gl_LightModel */
95 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
96 /* FINISHME: Add support for gl_TextureEnvColor[] */
97 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
98 /* FINISHME: Add support for gl_Fog */
99}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800100
101static void
102generate_110_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700103 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800104{
105 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
106 add_builtin_variable(& builtin_core_vs_variables[i],
107 instructions, symtab);
108 }
109
110 for (unsigned i = 0
111 ; i < Elements(builtin_110_deprecated_vs_variables)
112 ; i++) {
113 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
114 instructions, symtab);
115 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700116 generate_110_uniforms(instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800117
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700118 /* FINISHME: The size of this array is implementation dependent based on the
119 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
120 * FINISHME: at least 2, so hard-code 2 for now.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800121 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700122 const glsl_type *const vec4_type =
123 glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 0);
124 const glsl_type *const vec4_array_type =
125 glsl_type::get_array_instance(vec4_type, 2);
126
127 add_variable("gl_TexCoord", ir_var_out, vec4_array_type, instructions,
128 symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800129}
130
131
132static void
133generate_120_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700134 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800135{
136 /* GLSL version 1.20 did not add any built-in variables in the vertex
137 * shader.
138 */
139 generate_110_vs_variables(instructions, symtab);
140}
141
142
143static void
144generate_130_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700145 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800146{
147 generate_120_vs_variables(instructions, symtab);
148
149 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
150 add_builtin_variable(& builtin_130_vs_variables[i],
151 instructions, symtab);
152 }
153
Eric Anholt271e1992010-04-02 23:47:06 -0700154 /* FINISHME: The size of this array is implementation dependent based on
155 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800156 */
Eric Anholt271e1992010-04-02 23:47:06 -0700157 const glsl_type *const clip_distance_array_type =
158 glsl_type::get_array_instance(glsl_type::float_type, 8);
159 add_variable("gl_ClipDistance", ir_var_out, clip_distance_array_type,
160 instructions, symtab);
161
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800162}
163
164
165static void
166initialize_vs_variables(exec_list *instructions,
167 struct _mesa_glsl_parse_state *state)
168{
169
170 switch (state->language_version) {
171 case 110:
172 generate_110_vs_variables(instructions, state->symbols);
173 break;
174 case 120:
175 generate_120_vs_variables(instructions, state->symbols);
176 break;
177 case 130:
178 generate_130_vs_variables(instructions, state->symbols);
179 break;
180 }
181}
182
Eric Anholtb3f743a2010-03-25 14:48:25 -0700183static void
184generate_110_fs_variables(exec_list *instructions,
185 glsl_symbol_table *symtab)
186{
187 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
188 add_builtin_variable(& builtin_core_fs_variables[i],
189 instructions, symtab);
190 }
191
Eric Anholt0f09aea2010-03-27 12:48:57 -0700192 for (unsigned i = 0
193 ; i < Elements(builtin_110_deprecated_fs_variables)
194 ; i++) {
195 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
196 instructions, symtab);
197 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700198 generate_110_uniforms(instructions, symtab);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700199
Eric Anholtb3f743a2010-03-25 14:48:25 -0700200 /* FINISHME: Add support for gl_FragData[GL_MAX_DRAW_BUFFERS]. */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700201
202 /* FINISHME: The size of this array is implementation dependent based on the
203 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
204 * FINISHME: at least 2, so hard-code 2 for now.
205 */
206 const glsl_type *const vec4_type =
207 glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 0);
208 const glsl_type *const vec4_array_type =
209 glsl_type::get_array_instance(vec4_type, 2);
210
211 add_variable("gl_TexCoord", ir_var_in, vec4_array_type, instructions,
212 symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700213}
214
215static void
216generate_120_fs_variables(exec_list *instructions,
217 glsl_symbol_table *symtab)
218{
219 /* GLSL version 1.20 did not add any built-in variables in the fragment
220 * shader.
221 */
222 generate_110_fs_variables(instructions, symtab);
223}
224
225static void
226generate_130_fs_variables(exec_list *instructions,
227 glsl_symbol_table *symtab)
228{
229 generate_120_fs_variables(instructions, symtab);
230
Ian Romanick8645a952010-04-07 16:47:44 -0700231 /* FINISHME: The size of this array is implementation dependent based on
232 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Eric Anholtb3f743a2010-03-25 14:48:25 -0700233 */
Ian Romanick8645a952010-04-07 16:47:44 -0700234 const glsl_type *const clip_distance_array_type =
235 glsl_type::get_array_instance(glsl_type::float_type, 8);
236 add_variable("gl_ClipDistance", ir_var_in, clip_distance_array_type,
237 instructions, symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700238}
239
240static void
241initialize_fs_variables(exec_list *instructions,
242 struct _mesa_glsl_parse_state *state)
243{
244
245 switch (state->language_version) {
246 case 110:
247 generate_110_fs_variables(instructions, state->symbols);
248 break;
249 case 120:
250 generate_120_fs_variables(instructions, state->symbols);
251 break;
252 case 130:
253 generate_130_fs_variables(instructions, state->symbols);
254 break;
255 }
256}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800257
258void
259_mesa_glsl_initialize_variables(exec_list *instructions,
260 struct _mesa_glsl_parse_state *state)
261{
262 switch (state->target) {
263 case vertex_shader:
264 initialize_vs_variables(instructions, state);
265 break;
266 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700267 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800268 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700269 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800270 break;
271 }
272}