blob: 1b4b742ac4c875b3546d29d6762e09f657812a82 [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
34add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070035 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080036{
37 /* Create a new variable declaration from the description supplied by
38 * the caller.
39 */
Ian Romanick8bde4ce2010-03-19 11:57:24 -070040 const glsl_type *const type = symtab->get_type(proto->type);
Ian Romanickadfb0cd2010-03-10 10:43:16 -080041
42 assert(type != NULL);
43
44 ir_variable *var = new ir_variable(type, proto->name);
45
46 var->mode = proto->mode;
47 if (var->mode != ir_var_out)
48 var->read_only = true;
49
50
51 /* Once the variable is created an initialized, add it to the symbol table
52 * and add the declaration to the IR stream.
53 */
54 instructions->push_tail(var);
55
Ian Romanick8bde4ce2010-03-19 11:57:24 -070056 symtab->add_variable(var->name, var);
Ian Romanickadfb0cd2010-03-10 10:43:16 -080057}
58
Eric Anholt78fe3c92010-03-28 01:46:48 -070059static void
60generate_110_uniforms(exec_list *instructions,
61 glsl_symbol_table *symtab)
62{
63 for (unsigned i = 0
64 ; i < Elements(builtin_110_deprecated_uniforms)
65 ; i++) {
66 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
67 instructions, symtab);
68 }
69
70 /* FINISHME: Add support for gl_TextureMatrix[]. The size of this array is
71 * FINISHME: implementation dependent based on the value of
72 * FINISHME: GL_MAX_TEXTURE_COORDS.
73 */
74
75 /* FINISHME: Add support for gl_DepthRangeParameters */
76 /* FINISHME: Add support for gl_ClipPlane[] */
77 /* FINISHME: Add support for gl_PointParameters */
78
79 /* FINISHME: Add support for gl_MaterialParameters
80 * FINISHME: (glFrontMaterial, glBackMaterial)
81 */
82
83 /* FINISHME: Add support for gl_LightSource[] */
84 /* FINISHME: Add support for gl_LightModel */
85 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
86 /* FINISHME: Add support for gl_TextureEnvColor[] */
87 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
88 /* FINISHME: Add support for gl_Fog */
89}
Ian Romanickadfb0cd2010-03-10 10:43:16 -080090
91static void
92generate_110_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070093 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080094{
95 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
96 add_builtin_variable(& builtin_core_vs_variables[i],
97 instructions, symtab);
98 }
99
100 for (unsigned i = 0
101 ; i < Elements(builtin_110_deprecated_vs_variables)
102 ; i++) {
103 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
104 instructions, symtab);
105 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700106 generate_110_uniforms(instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800107
108 /* FINISHME: Add support fo gl_TexCoord. The size of this array is
109 * FINISHME: implementation dependent based on the value of
110 * FINISHME: GL_MAX_TEXTURE_COORDS.
111 */
112}
113
114
115static void
116generate_120_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700117 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800118{
119 /* GLSL version 1.20 did not add any built-in variables in the vertex
120 * shader.
121 */
122 generate_110_vs_variables(instructions, symtab);
123}
124
125
126static void
127generate_130_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700128 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800129{
130 generate_120_vs_variables(instructions, symtab);
131
132 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
133 add_builtin_variable(& builtin_130_vs_variables[i],
134 instructions, symtab);
135 }
136
137 /* FINISHME: Add support fo gl_ClipDistance. The size of this array is
138 * FINISHME: implementation dependent based on the value of
139 * FINISHME: GL_MAX_CLIP_DISTANCES.
140 */
141}
142
143
144static void
145initialize_vs_variables(exec_list *instructions,
146 struct _mesa_glsl_parse_state *state)
147{
148
149 switch (state->language_version) {
150 case 110:
151 generate_110_vs_variables(instructions, state->symbols);
152 break;
153 case 120:
154 generate_120_vs_variables(instructions, state->symbols);
155 break;
156 case 130:
157 generate_130_vs_variables(instructions, state->symbols);
158 break;
159 }
160}
161
Eric Anholtb3f743a2010-03-25 14:48:25 -0700162static void
163generate_110_fs_variables(exec_list *instructions,
164 glsl_symbol_table *symtab)
165{
166 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
167 add_builtin_variable(& builtin_core_fs_variables[i],
168 instructions, symtab);
169 }
170
Eric Anholt0f09aea2010-03-27 12:48:57 -0700171 /* FINISHME: Add support for gl_TexCoord[] */
172 for (unsigned i = 0
173 ; i < Elements(builtin_110_deprecated_fs_variables)
174 ; i++) {
175 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
176 instructions, symtab);
177 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700178 generate_110_uniforms(instructions, symtab);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700179
Eric Anholtb3f743a2010-03-25 14:48:25 -0700180 /* FINISHME: Add support for gl_FragData[GL_MAX_DRAW_BUFFERS]. */
181}
182
183static void
184generate_120_fs_variables(exec_list *instructions,
185 glsl_symbol_table *symtab)
186{
187 /* GLSL version 1.20 did not add any built-in variables in the fragment
188 * shader.
189 */
190 generate_110_fs_variables(instructions, symtab);
191}
192
193static void
194generate_130_fs_variables(exec_list *instructions,
195 glsl_symbol_table *symtab)
196{
197 generate_120_fs_variables(instructions, symtab);
198
199 /* FINISHME: Add support fo gl_ClipDistance. The size of this array is
200 * FINISHME: implementation dependent based on the value of
201 * FINISHME: GL_MAX_CLIP_DISTANCES.
202 */
203}
204
205static void
206initialize_fs_variables(exec_list *instructions,
207 struct _mesa_glsl_parse_state *state)
208{
209
210 switch (state->language_version) {
211 case 110:
212 generate_110_fs_variables(instructions, state->symbols);
213 break;
214 case 120:
215 generate_120_fs_variables(instructions, state->symbols);
216 break;
217 case 130:
218 generate_130_fs_variables(instructions, state->symbols);
219 break;
220 }
221}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800222
223void
224_mesa_glsl_initialize_variables(exec_list *instructions,
225 struct _mesa_glsl_parse_state *state)
226{
227 switch (state->target) {
228 case vertex_shader:
229 initialize_vs_variables(instructions, state);
230 break;
231 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700232 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800233 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700234 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800235 break;
236 }
237}