blob: 0edb19e28fbedb78b148574dbb2a5ea509f8f991 [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
154 /* FINISHME: Add support fo gl_ClipDistance. The size of this array is
155 * FINISHME: implementation dependent based on the value of
156 * FINISHME: GL_MAX_CLIP_DISTANCES.
157 */
158}
159
160
161static void
162initialize_vs_variables(exec_list *instructions,
163 struct _mesa_glsl_parse_state *state)
164{
165
166 switch (state->language_version) {
167 case 110:
168 generate_110_vs_variables(instructions, state->symbols);
169 break;
170 case 120:
171 generate_120_vs_variables(instructions, state->symbols);
172 break;
173 case 130:
174 generate_130_vs_variables(instructions, state->symbols);
175 break;
176 }
177}
178
Eric Anholtb3f743a2010-03-25 14:48:25 -0700179static void
180generate_110_fs_variables(exec_list *instructions,
181 glsl_symbol_table *symtab)
182{
183 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
184 add_builtin_variable(& builtin_core_fs_variables[i],
185 instructions, symtab);
186 }
187
Eric Anholt0f09aea2010-03-27 12:48:57 -0700188 for (unsigned i = 0
189 ; i < Elements(builtin_110_deprecated_fs_variables)
190 ; i++) {
191 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
192 instructions, symtab);
193 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700194 generate_110_uniforms(instructions, symtab);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700195
Eric Anholtb3f743a2010-03-25 14:48:25 -0700196 /* FINISHME: Add support for gl_FragData[GL_MAX_DRAW_BUFFERS]. */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700197
198 /* FINISHME: The size of this array is implementation dependent based on the
199 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
200 * FINISHME: at least 2, so hard-code 2 for now.
201 */
202 const glsl_type *const vec4_type =
203 glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 0);
204 const glsl_type *const vec4_array_type =
205 glsl_type::get_array_instance(vec4_type, 2);
206
207 add_variable("gl_TexCoord", ir_var_in, vec4_array_type, instructions,
208 symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700209}
210
211static void
212generate_120_fs_variables(exec_list *instructions,
213 glsl_symbol_table *symtab)
214{
215 /* GLSL version 1.20 did not add any built-in variables in the fragment
216 * shader.
217 */
218 generate_110_fs_variables(instructions, symtab);
219}
220
221static void
222generate_130_fs_variables(exec_list *instructions,
223 glsl_symbol_table *symtab)
224{
225 generate_120_fs_variables(instructions, symtab);
226
227 /* FINISHME: Add support fo gl_ClipDistance. The size of this array is
228 * FINISHME: implementation dependent based on the value of
229 * FINISHME: GL_MAX_CLIP_DISTANCES.
230 */
231}
232
233static void
234initialize_fs_variables(exec_list *instructions,
235 struct _mesa_glsl_parse_state *state)
236{
237
238 switch (state->language_version) {
239 case 110:
240 generate_110_fs_variables(instructions, state->symbols);
241 break;
242 case 120:
243 generate_120_fs_variables(instructions, state->symbols);
244 break;
245 case 130:
246 generate_130_fs_variables(instructions, state->symbols);
247 break;
248 }
249}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800250
251void
252_mesa_glsl_initialize_variables(exec_list *instructions,
253 struct _mesa_glsl_parse_state *state)
254{
255 switch (state->target) {
256 case vertex_shader:
257 initialize_vs_variables(instructions, state);
258 break;
259 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700260 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800261 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700262 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800263 break;
264 }
265}