Add definitions for 1.10 built-in uniforms for ff state.
[mesa.git] / ir_variable.cpp
1 /*
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"
25 #include "glsl_symbol_table.h"
26 #include "ir.h"
27 #include "builtin_variables.h"
28
29 #ifndef Elements
30 #define Elements(x) (sizeof(x)/sizeof(*(x)))
31 #endif
32
33 static void
34 add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
35 glsl_symbol_table *symtab)
36 {
37 /* Create a new variable declaration from the description supplied by
38 * the caller.
39 */
40 const glsl_type *const type = symtab->get_type(proto->type);
41
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
56 symtab->add_variable(var->name, var);
57 }
58
59 static void
60 generate_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 }
90
91 static void
92 generate_110_vs_variables(exec_list *instructions,
93 glsl_symbol_table *symtab)
94 {
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 }
106 generate_110_uniforms(instructions, symtab);
107
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
115 static void
116 generate_120_vs_variables(exec_list *instructions,
117 glsl_symbol_table *symtab)
118 {
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
126 static void
127 generate_130_vs_variables(exec_list *instructions,
128 glsl_symbol_table *symtab)
129 {
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
144 static void
145 initialize_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
162 static void
163 generate_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
171 /* 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 }
178 generate_110_uniforms(instructions, symtab);
179
180 /* FINISHME: Add support for gl_FragData[GL_MAX_DRAW_BUFFERS]. */
181 }
182
183 static void
184 generate_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
193 static void
194 generate_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
205 static void
206 initialize_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 }
222
223 void
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:
232 break;
233 case fragment_shader:
234 initialize_fs_variables(instructions, state);
235 break;
236 }
237 }