blob: e298a0e06b50100b8eaae560b88ed23f2f16ec83 [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
Eric Anholtac95f2f2010-06-22 10:38:52 -070024#include "ir.h"
Ian Romanickadfb0cd2010-03-10 10:43:16 -080025#include "glsl_parser_extras.h"
Ian Romanick8bde4ce2010-03-19 11:57:24 -070026#include "glsl_symbol_table.h"
Ian Romanickadfb0cd2010-03-10 10:43:16 -080027#include "builtin_variables.h"
28
29#ifndef Elements
30#define Elements(x) (sizeof(x)/sizeof(*(x)))
31#endif
32
Ian Romanick9c4b1f22010-06-29 15:10:09 -070033static void generate_ARB_draw_buffers_fs_variables(exec_list *,
34 struct _mesa_glsl_parse_state *, bool);
35
Ian Romanickc77b2572010-04-07 16:59:46 -070036static ir_variable *
Ian Romanicked0626e2010-06-21 11:42:57 -070037add_variable(const char *name, enum ir_variable_mode mode, int slot,
Ian Romanick3f9a73d2010-04-02 11:59:57 -070038 const glsl_type *type, exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070039 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080040{
Carl Worth1660a292010-06-23 18:11:51 -070041 ir_variable *var = new(symtab) ir_variable(type, name);
Ian Romanickadfb0cd2010-03-10 10:43:16 -080042
Ian Romanick3f9a73d2010-04-02 11:59:57 -070043 var->mode = mode;
Eric Anholt71df19f2010-04-19 11:10:37 -070044 switch (var->mode) {
Ian Romanicke2f84f02010-06-29 15:19:11 -070045 case ir_var_auto:
46 var->read_only = true;
47 break;
Eric Anholt71df19f2010-04-19 11:10:37 -070048 case ir_var_in:
49 var->shader_in = true;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080050 var->read_only = true;
Eric Anholt71df19f2010-04-19 11:10:37 -070051 break;
52 case ir_var_inout:
53 var->shader_in = true;
54 var->shader_out = true;
Ian Romanickff236fa2010-04-21 15:08:08 -070055 break;
Eric Anholt71df19f2010-04-19 11:10:37 -070056 case ir_var_out:
57 var->shader_out = true;
58 break;
59 case ir_var_uniform:
60 var->shader_in = true;
61 var->read_only = true;
62 break;
63 default:
64 assert(0);
65 break;
66 }
Ian Romanickadfb0cd2010-03-10 10:43:16 -080067
Ian Romanicked0626e2010-06-21 11:42:57 -070068 var->location = slot;
69
Ian Romanickadfb0cd2010-03-10 10:43:16 -080070 /* Once the variable is created an initialized, add it to the symbol table
71 * and add the declaration to the IR stream.
72 */
73 instructions->push_tail(var);
74
Ian Romanick8bde4ce2010-03-19 11:57:24 -070075 symtab->add_variable(var->name, var);
Ian Romanickc77b2572010-04-07 16:59:46 -070076 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080077}
78
Ian Romanick3f9a73d2010-04-02 11:59:57 -070079
80static void
81add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
82 glsl_symbol_table *symtab)
83{
84 /* Create a new variable declaration from the description supplied by
85 * the caller.
86 */
87 const glsl_type *const type = symtab->get_type(proto->type);
88
89 assert(type != NULL);
90
Ian Romanicked0626e2010-06-21 11:42:57 -070091 add_variable(proto->name, proto->mode, proto->slot, type, instructions,
92 symtab);
Ian Romanick3f9a73d2010-04-02 11:59:57 -070093}
94
95
Eric Anholt78fe3c92010-03-28 01:46:48 -070096static void
97generate_110_uniforms(exec_list *instructions,
98 glsl_symbol_table *symtab)
99{
100 for (unsigned i = 0
101 ; i < Elements(builtin_110_deprecated_uniforms)
102 ; i++) {
103 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
104 instructions, symtab);
105 }
106
Ian Romanick3eba5932010-04-26 14:59:32 -0700107 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700108 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
109 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
110 * FINISHME: for now.
Eric Anholt78fe3c92010-03-28 01:46:48 -0700111 */
Ian Romanick3eba5932010-04-26 14:59:32 -0700112 const glsl_type *const mat4_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700113 glsl_type::get_array_instance(symtab, glsl_type::mat4_type, 4);
Ian Romanick3eba5932010-04-26 14:59:32 -0700114
Ian Romanicked0626e2010-06-21 11:42:57 -0700115 add_variable("gl_TextureMatrix", ir_var_uniform, -1, mat4_array_type,
Ian Romanick3eba5932010-04-26 14:59:32 -0700116 instructions, symtab);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700117
118 /* FINISHME: Add support for gl_DepthRangeParameters */
119 /* FINISHME: Add support for gl_ClipPlane[] */
120 /* FINISHME: Add support for gl_PointParameters */
121
122 /* FINISHME: Add support for gl_MaterialParameters
123 * FINISHME: (glFrontMaterial, glBackMaterial)
124 */
125
Eric Anholtaa579432010-05-19 14:09:04 -0700126 /* FINISHME: The size of this array is implementation dependent based on the
127 * FINISHME: value of GL_MAX_TEXTURE_LIGHTS. GL_MAX_TEXTURE_LIGHTS must be
128 * FINISHME: at least 8, so hard-code 8 for now.
129 */
130 const glsl_type *const light_source_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700131 glsl_type::get_array_instance(symtab,
132 symtab->get_type("gl_LightSourceParameters"), 8);
Eric Anholtaa579432010-05-19 14:09:04 -0700133
Ian Romanicked0626e2010-06-21 11:42:57 -0700134 add_variable("gl_LightSource", ir_var_uniform, -1, light_source_array_type,
Eric Anholtaa579432010-05-19 14:09:04 -0700135 instructions, symtab);
136
Eric Anholt78fe3c92010-03-28 01:46:48 -0700137 /* FINISHME: Add support for gl_LightModel */
138 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
139 /* FINISHME: Add support for gl_TextureEnvColor[] */
140 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
141 /* FINISHME: Add support for gl_Fog */
142}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800143
144static void
145generate_110_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 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
149 add_builtin_variable(& builtin_core_vs_variables[i],
150 instructions, symtab);
151 }
152
153 for (unsigned i = 0
154 ; i < Elements(builtin_110_deprecated_vs_variables)
155 ; i++) {
156 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
157 instructions, symtab);
158 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700159 generate_110_uniforms(instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800160
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700161 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700162 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
163 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
164 * FINISHME: for now.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800165 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700166 const glsl_type *const vec4_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700167 glsl_type::get_array_instance(symtab, glsl_type::vec4_type, 4);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700168
Ian Romanicked0626e2010-06-21 11:42:57 -0700169 add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
170 instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800171}
172
173
174static void
175generate_120_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700176 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800177{
178 /* GLSL version 1.20 did not add any built-in variables in the vertex
179 * shader.
180 */
181 generate_110_vs_variables(instructions, symtab);
182}
183
184
185static void
186generate_130_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700187 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800188{
Carl Worth12c41152010-06-18 17:52:59 -0700189 void *ctx = symtab;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800190 generate_120_vs_variables(instructions, symtab);
191
192 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
193 add_builtin_variable(& builtin_130_vs_variables[i],
194 instructions, symtab);
195 }
196
Eric Anholt271e1992010-04-02 23:47:06 -0700197 /* FINISHME: The size of this array is implementation dependent based on
198 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800199 */
Eric Anholt271e1992010-04-02 23:47:06 -0700200 const glsl_type *const clip_distance_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700201 glsl_type::get_array_instance(ctx, glsl_type::float_type, 8);
Ian Romanicked0626e2010-06-21 11:42:57 -0700202
203 /* FINISHME: gl_ClipDistance needs a real location assigned. */
204 add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
Eric Anholt271e1992010-04-02 23:47:06 -0700205 instructions, symtab);
206
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800207}
208
209
210static void
211initialize_vs_variables(exec_list *instructions,
212 struct _mesa_glsl_parse_state *state)
213{
214
215 switch (state->language_version) {
216 case 110:
217 generate_110_vs_variables(instructions, state->symbols);
218 break;
219 case 120:
220 generate_120_vs_variables(instructions, state->symbols);
221 break;
222 case 130:
223 generate_130_vs_variables(instructions, state->symbols);
224 break;
225 }
226}
227
Eric Anholtb3f743a2010-03-25 14:48:25 -0700228static void
229generate_110_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700230 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700231{
232 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
233 add_builtin_variable(& builtin_core_fs_variables[i],
Ian Romanick5e18b052010-06-29 14:21:05 -0700234 instructions, state->symbols);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700235 }
236
Eric Anholt0f09aea2010-03-27 12:48:57 -0700237 for (unsigned i = 0
238 ; i < Elements(builtin_110_deprecated_fs_variables)
239 ; i++) {
240 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
Ian Romanick5e18b052010-06-29 14:21:05 -0700241 instructions, state->symbols);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700242 }
Ian Romanick5e18b052010-06-29 14:21:05 -0700243 generate_110_uniforms(instructions, state->symbols);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700244
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700245 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700246 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
247 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
248 * FINISHME: for now.
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700249 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700250 const glsl_type *const vec4_array_type =
Ian Romanick5e18b052010-06-29 14:21:05 -0700251 glsl_type::get_array_instance(state->symbols, glsl_type::vec4_type, 4);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700252
Ian Romanicked0626e2010-06-21 11:42:57 -0700253 add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
Ian Romanick5e18b052010-06-29 14:21:05 -0700254 instructions, state->symbols);
Ian Romanick9c4b1f22010-06-29 15:10:09 -0700255
256 generate_ARB_draw_buffers_fs_variables(instructions, state, false);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700257}
258
Ian Romanickc77b2572010-04-07 16:59:46 -0700259
260static void
261generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700262 struct _mesa_glsl_parse_state *state,
263 bool warn)
Ian Romanickc77b2572010-04-07 16:59:46 -0700264{
Ian Romanicke2f84f02010-06-29 15:19:11 -0700265 assert(state->Const.MaxDrawBuffers >= 1);
266
267 ir_variable *const mdb =
268 add_variable("gl_MaxDrawBuffers", ir_var_auto, -1,
269 glsl_type::int_type, instructions, state->symbols);
270
271 if (warn)
272 mdb->warn_extension = "GL_ARB_draw_buffers";
273
274 mdb->constant_value = new(mdb)
275 ir_constant(int(state->Const.MaxDrawBuffers));
276
Ian Romanickc77b2572010-04-07 16:59:46 -0700277 const glsl_type *const vec4_array_type =
Ian Romanick5e18b052010-06-29 14:21:05 -0700278 glsl_type::get_array_instance(state->symbols, glsl_type::vec4_type,
279 state->Const.MaxDrawBuffers);
Ian Romanickc77b2572010-04-07 16:59:46 -0700280
281 ir_variable *const fd =
Ian Romanicked0626e2010-06-21 11:42:57 -0700282 add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
Ian Romanick5e18b052010-06-29 14:21:05 -0700283 vec4_array_type, instructions, state->symbols);
Ian Romanickc77b2572010-04-07 16:59:46 -0700284
285 if (warn)
286 fd->warn_extension = "GL_ARB_draw_buffers";
287}
288
289
Eric Anholtb3f743a2010-03-25 14:48:25 -0700290static void
291generate_120_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700292 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700293{
Ian Romanick5e18b052010-06-29 14:21:05 -0700294 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700295}
296
297static void
298generate_130_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700299 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700300{
Ian Romanick5e18b052010-06-29 14:21:05 -0700301 void *ctx = state->symbols;
302 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700303
Ian Romanick8645a952010-04-07 16:47:44 -0700304 /* FINISHME: The size of this array is implementation dependent based on
305 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Eric Anholtb3f743a2010-03-25 14:48:25 -0700306 */
Ian Romanick8645a952010-04-07 16:47:44 -0700307 const glsl_type *const clip_distance_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700308 glsl_type::get_array_instance(ctx, glsl_type::float_type, 8);
Ian Romanicked0626e2010-06-21 11:42:57 -0700309
310 /* FINISHME: gl_ClipDistance needs a real location assigned. */
311 add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
Ian Romanick5e18b052010-06-29 14:21:05 -0700312 instructions, state->symbols);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700313}
314
315static void
316initialize_fs_variables(exec_list *instructions,
317 struct _mesa_glsl_parse_state *state)
318{
319
320 switch (state->language_version) {
321 case 110:
Ian Romanick5e18b052010-06-29 14:21:05 -0700322 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700323 break;
324 case 120:
Ian Romanick5e18b052010-06-29 14:21:05 -0700325 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700326 break;
327 case 130:
Ian Romanick5e18b052010-06-29 14:21:05 -0700328 generate_130_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700329 break;
330 }
331}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800332
333void
334_mesa_glsl_initialize_variables(exec_list *instructions,
335 struct _mesa_glsl_parse_state *state)
336{
337 switch (state->target) {
338 case vertex_shader:
339 initialize_vs_variables(instructions, state);
340 break;
341 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700342 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800343 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700344 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800345 break;
Eric Anholt81f49a72010-04-29 17:57:28 -0700346 case ir_shader:
347 fprintf(stderr, "ir reader has no builtin variables");
348 exit(1);
349 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800350 }
351}