blob: 76a528ca2bea49c22b1955230d78ca65db2aa0f9 [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
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;
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 Romanickc77b2572010-04-07 16:59:46 -070051 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080052}
53
Ian Romanick3f9a73d2010-04-02 11:59:57 -070054
55static void
56add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
57 glsl_symbol_table *symtab)
58{
59 /* Create a new variable declaration from the description supplied by
60 * the caller.
61 */
62 const glsl_type *const type = symtab->get_type(proto->type);
63
64 assert(type != NULL);
65
66 add_variable(proto->name, proto->mode, type, instructions, symtab);
67}
68
69
Eric Anholt78fe3c92010-03-28 01:46:48 -070070static void
71generate_110_uniforms(exec_list *instructions,
72 glsl_symbol_table *symtab)
73{
74 for (unsigned i = 0
75 ; i < Elements(builtin_110_deprecated_uniforms)
76 ; i++) {
77 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
78 instructions, symtab);
79 }
80
81 /* FINISHME: Add support for gl_TextureMatrix[]. The size of this array is
82 * FINISHME: implementation dependent based on the value of
83 * FINISHME: GL_MAX_TEXTURE_COORDS.
84 */
85
86 /* FINISHME: Add support for gl_DepthRangeParameters */
87 /* FINISHME: Add support for gl_ClipPlane[] */
88 /* FINISHME: Add support for gl_PointParameters */
89
90 /* FINISHME: Add support for gl_MaterialParameters
91 * FINISHME: (glFrontMaterial, glBackMaterial)
92 */
93
94 /* FINISHME: Add support for gl_LightSource[] */
95 /* FINISHME: Add support for gl_LightModel */
96 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
97 /* FINISHME: Add support for gl_TextureEnvColor[] */
98 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
99 /* FINISHME: Add support for gl_Fog */
100}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800101
102static void
103generate_110_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700104 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800105{
106 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
107 add_builtin_variable(& builtin_core_vs_variables[i],
108 instructions, symtab);
109 }
110
111 for (unsigned i = 0
112 ; i < Elements(builtin_110_deprecated_vs_variables)
113 ; i++) {
114 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
115 instructions, symtab);
116 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700117 generate_110_uniforms(instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800118
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700119 /* FINISHME: The size of this array is implementation dependent based on the
120 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
121 * FINISHME: at least 2, so hard-code 2 for now.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800122 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700123 const glsl_type *const vec4_type =
Ian Romanick1b3f47f2010-04-07 16:09:47 -0700124 glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700125 const glsl_type *const vec4_array_type =
126 glsl_type::get_array_instance(vec4_type, 2);
127
128 add_variable("gl_TexCoord", ir_var_out, vec4_array_type, instructions,
129 symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800130}
131
132
133static void
134generate_120_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700135 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800136{
137 /* GLSL version 1.20 did not add any built-in variables in the vertex
138 * shader.
139 */
140 generate_110_vs_variables(instructions, symtab);
141}
142
143
144static void
145generate_130_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700146 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800147{
148 generate_120_vs_variables(instructions, symtab);
149
150 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
151 add_builtin_variable(& builtin_130_vs_variables[i],
152 instructions, symtab);
153 }
154
Eric Anholt271e1992010-04-02 23:47:06 -0700155 /* FINISHME: The size of this array is implementation dependent based on
156 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800157 */
Eric Anholt271e1992010-04-02 23:47:06 -0700158 const glsl_type *const clip_distance_array_type =
159 glsl_type::get_array_instance(glsl_type::float_type, 8);
160 add_variable("gl_ClipDistance", ir_var_out, clip_distance_array_type,
161 instructions, symtab);
162
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800163}
164
165
166static void
167initialize_vs_variables(exec_list *instructions,
168 struct _mesa_glsl_parse_state *state)
169{
170
171 switch (state->language_version) {
172 case 110:
173 generate_110_vs_variables(instructions, state->symbols);
174 break;
175 case 120:
176 generate_120_vs_variables(instructions, state->symbols);
177 break;
178 case 130:
179 generate_130_vs_variables(instructions, state->symbols);
180 break;
181 }
182}
183
Eric Anholtb3f743a2010-03-25 14:48:25 -0700184static void
185generate_110_fs_variables(exec_list *instructions,
186 glsl_symbol_table *symtab)
187{
188 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
189 add_builtin_variable(& builtin_core_fs_variables[i],
190 instructions, symtab);
191 }
192
Eric Anholt0f09aea2010-03-27 12:48:57 -0700193 for (unsigned i = 0
194 ; i < Elements(builtin_110_deprecated_fs_variables)
195 ; i++) {
196 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
197 instructions, symtab);
198 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700199 generate_110_uniforms(instructions, symtab);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700200
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700201 /* FINISHME: The size of this array is implementation dependent based on the
202 * FINISHME: value of GL_MAX_TEXTURE_COORDS. GL_MAX_TEXTURE_COORDS must be
203 * FINISHME: at least 2, so hard-code 2 for now.
204 */
205 const glsl_type *const vec4_type =
Ian Romanick1b3f47f2010-04-07 16:09:47 -0700206 glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700207 const glsl_type *const vec4_array_type =
208 glsl_type::get_array_instance(vec4_type, 2);
209
210 add_variable("gl_TexCoord", ir_var_in, vec4_array_type, instructions,
211 symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700212}
213
Ian Romanickc77b2572010-04-07 16:59:46 -0700214
215static void
216generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
217 glsl_symbol_table *symtab, bool warn)
218{
219 /* FINISHME: The size of this array is implementation dependent based on the
220 * FINISHME: value of GL_MAX_DRAW_BUFFERS. GL_MAX_DRAW_BUFFERS must be
221 * FINISHME: at least 1, so hard-code 1 for now.
222 */
223 const glsl_type *const vec4_type =
224 glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
225 const glsl_type *const vec4_array_type =
226 glsl_type::get_array_instance(vec4_type, 1);
227
228 ir_variable *const fd =
229 add_variable("gl_FragData", ir_var_out, vec4_array_type, instructions,
230 symtab);
231
232 if (warn)
233 fd->warn_extension = "GL_ARB_draw_buffers";
234}
235
236
Eric Anholtb3f743a2010-03-25 14:48:25 -0700237static void
238generate_120_fs_variables(exec_list *instructions,
239 glsl_symbol_table *symtab)
240{
Eric Anholtb3f743a2010-03-25 14:48:25 -0700241 generate_110_fs_variables(instructions, symtab);
Ian Romanickc77b2572010-04-07 16:59:46 -0700242 generate_ARB_draw_buffers_fs_variables(instructions, symtab, false);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700243}
244
245static void
246generate_130_fs_variables(exec_list *instructions,
247 glsl_symbol_table *symtab)
248{
249 generate_120_fs_variables(instructions, symtab);
250
Ian Romanick8645a952010-04-07 16:47:44 -0700251 /* FINISHME: The size of this array is implementation dependent based on
252 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Eric Anholtb3f743a2010-03-25 14:48:25 -0700253 */
Ian Romanick8645a952010-04-07 16:47:44 -0700254 const glsl_type *const clip_distance_array_type =
255 glsl_type::get_array_instance(glsl_type::float_type, 8);
256 add_variable("gl_ClipDistance", ir_var_in, clip_distance_array_type,
257 instructions, symtab);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700258}
259
260static void
261initialize_fs_variables(exec_list *instructions,
262 struct _mesa_glsl_parse_state *state)
263{
264
265 switch (state->language_version) {
266 case 110:
267 generate_110_fs_variables(instructions, state->symbols);
268 break;
269 case 120:
270 generate_120_fs_variables(instructions, state->symbols);
271 break;
272 case 130:
273 generate_130_fs_variables(instructions, state->symbols);
274 break;
275 }
Ian Romanickc77b2572010-04-07 16:59:46 -0700276
277
278 /* Since GL_ARB_draw_buffers is included in GLSL 1.20 and later, we
279 * can basically ignore any extension settings for it.
280 */
281 if (state->language_version < 120) {
282 if (state->ARB_draw_buffers_enable) {
283 generate_ARB_draw_buffers_fs_variables(instructions, state->symbols,
284 state->ARB_draw_buffers_warn);
285 }
286 }
Eric Anholtb3f743a2010-03-25 14:48:25 -0700287}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800288
289void
290_mesa_glsl_initialize_variables(exec_list *instructions,
291 struct _mesa_glsl_parse_state *state)
292{
293 switch (state->target) {
294 case vertex_shader:
295 initialize_vs_variables(instructions, state);
296 break;
297 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700298 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800299 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700300 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800301 break;
302 }
303}