blob: 6e466fa6d1eff46f4ac350393663bd3122d43a31 [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) {
45 case ir_var_in:
46 var->shader_in = true;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080047 var->read_only = true;
Eric Anholt71df19f2010-04-19 11:10:37 -070048 break;
49 case ir_var_inout:
50 var->shader_in = true;
51 var->shader_out = true;
Ian Romanickff236fa2010-04-21 15:08:08 -070052 break;
Eric Anholt71df19f2010-04-19 11:10:37 -070053 case ir_var_out:
54 var->shader_out = true;
55 break;
56 case ir_var_uniform:
57 var->shader_in = true;
58 var->read_only = true;
59 break;
60 default:
61 assert(0);
62 break;
63 }
Ian Romanickadfb0cd2010-03-10 10:43:16 -080064
Ian Romanicked0626e2010-06-21 11:42:57 -070065 var->location = slot;
66
Ian Romanickadfb0cd2010-03-10 10:43:16 -080067 /* Once the variable is created an initialized, add it to the symbol table
68 * and add the declaration to the IR stream.
69 */
70 instructions->push_tail(var);
71
Ian Romanick8bde4ce2010-03-19 11:57:24 -070072 symtab->add_variable(var->name, var);
Ian Romanickc77b2572010-04-07 16:59:46 -070073 return var;
Ian Romanickadfb0cd2010-03-10 10:43:16 -080074}
75
Ian Romanick3f9a73d2010-04-02 11:59:57 -070076
77static void
78add_builtin_variable(const builtin_variable *proto, exec_list *instructions,
79 glsl_symbol_table *symtab)
80{
81 /* Create a new variable declaration from the description supplied by
82 * the caller.
83 */
84 const glsl_type *const type = symtab->get_type(proto->type);
85
86 assert(type != NULL);
87
Ian Romanicked0626e2010-06-21 11:42:57 -070088 add_variable(proto->name, proto->mode, proto->slot, type, instructions,
89 symtab);
Ian Romanick3f9a73d2010-04-02 11:59:57 -070090}
91
92
Eric Anholt78fe3c92010-03-28 01:46:48 -070093static void
94generate_110_uniforms(exec_list *instructions,
95 glsl_symbol_table *symtab)
96{
97 for (unsigned i = 0
98 ; i < Elements(builtin_110_deprecated_uniforms)
99 ; i++) {
100 add_builtin_variable(& builtin_110_deprecated_uniforms[i],
101 instructions, symtab);
102 }
103
Ian Romanick3eba5932010-04-26 14:59:32 -0700104 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700105 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
106 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
107 * FINISHME: for now.
Eric Anholt78fe3c92010-03-28 01:46:48 -0700108 */
Ian Romanick3eba5932010-04-26 14:59:32 -0700109 const glsl_type *const mat4_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700110 glsl_type::get_array_instance(symtab, glsl_type::mat4_type, 4);
Ian Romanick3eba5932010-04-26 14:59:32 -0700111
Ian Romanicked0626e2010-06-21 11:42:57 -0700112 add_variable("gl_TextureMatrix", ir_var_uniform, -1, mat4_array_type,
Ian Romanick3eba5932010-04-26 14:59:32 -0700113 instructions, symtab);
Eric Anholt78fe3c92010-03-28 01:46:48 -0700114
115 /* FINISHME: Add support for gl_DepthRangeParameters */
116 /* FINISHME: Add support for gl_ClipPlane[] */
117 /* FINISHME: Add support for gl_PointParameters */
118
119 /* FINISHME: Add support for gl_MaterialParameters
120 * FINISHME: (glFrontMaterial, glBackMaterial)
121 */
122
Eric Anholtaa579432010-05-19 14:09:04 -0700123 /* FINISHME: The size of this array is implementation dependent based on the
124 * FINISHME: value of GL_MAX_TEXTURE_LIGHTS. GL_MAX_TEXTURE_LIGHTS must be
125 * FINISHME: at least 8, so hard-code 8 for now.
126 */
127 const glsl_type *const light_source_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700128 glsl_type::get_array_instance(symtab,
129 symtab->get_type("gl_LightSourceParameters"), 8);
Eric Anholtaa579432010-05-19 14:09:04 -0700130
Ian Romanicked0626e2010-06-21 11:42:57 -0700131 add_variable("gl_LightSource", ir_var_uniform, -1, light_source_array_type,
Eric Anholtaa579432010-05-19 14:09:04 -0700132 instructions, symtab);
133
Eric Anholt78fe3c92010-03-28 01:46:48 -0700134 /* FINISHME: Add support for gl_LightModel */
135 /* FINISHME: Add support for gl_FrontLightProduct[], gl_BackLightProduct[] */
136 /* FINISHME: Add support for gl_TextureEnvColor[] */
137 /* FINISHME: Add support for gl_ObjectPlane*[], gl_EyePlane*[] */
138 /* FINISHME: Add support for gl_Fog */
139}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800140
141static void
142generate_110_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700143 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800144{
145 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
146 add_builtin_variable(& builtin_core_vs_variables[i],
147 instructions, symtab);
148 }
149
150 for (unsigned i = 0
151 ; i < Elements(builtin_110_deprecated_vs_variables)
152 ; i++) {
153 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
154 instructions, symtab);
155 }
Eric Anholt78fe3c92010-03-28 01:46:48 -0700156 generate_110_uniforms(instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800157
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700158 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700159 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
160 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
161 * FINISHME: for now.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800162 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700163 const glsl_type *const vec4_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700164 glsl_type::get_array_instance(symtab, glsl_type::vec4_type, 4);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700165
Ian Romanicked0626e2010-06-21 11:42:57 -0700166 add_variable("gl_TexCoord", ir_var_out, VERT_RESULT_TEX0, vec4_array_type,
167 instructions, symtab);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800168}
169
170
171static void
172generate_120_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700173 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800174{
175 /* GLSL version 1.20 did not add any built-in variables in the vertex
176 * shader.
177 */
178 generate_110_vs_variables(instructions, symtab);
179}
180
181
182static void
183generate_130_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700184 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800185{
Carl Worth12c41152010-06-18 17:52:59 -0700186 void *ctx = symtab;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800187 generate_120_vs_variables(instructions, symtab);
188
189 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
190 add_builtin_variable(& builtin_130_vs_variables[i],
191 instructions, symtab);
192 }
193
Eric Anholt271e1992010-04-02 23:47:06 -0700194 /* FINISHME: The size of this array is implementation dependent based on
195 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800196 */
Eric Anholt271e1992010-04-02 23:47:06 -0700197 const glsl_type *const clip_distance_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700198 glsl_type::get_array_instance(ctx, glsl_type::float_type, 8);
Ian Romanicked0626e2010-06-21 11:42:57 -0700199
200 /* FINISHME: gl_ClipDistance needs a real location assigned. */
201 add_variable("gl_ClipDistance", ir_var_out, -1, clip_distance_array_type,
Eric Anholt271e1992010-04-02 23:47:06 -0700202 instructions, symtab);
203
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800204}
205
206
207static void
208initialize_vs_variables(exec_list *instructions,
209 struct _mesa_glsl_parse_state *state)
210{
211
212 switch (state->language_version) {
213 case 110:
214 generate_110_vs_variables(instructions, state->symbols);
215 break;
216 case 120:
217 generate_120_vs_variables(instructions, state->symbols);
218 break;
219 case 130:
220 generate_130_vs_variables(instructions, state->symbols);
221 break;
222 }
223}
224
Eric Anholtb3f743a2010-03-25 14:48:25 -0700225static void
226generate_110_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700227 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700228{
229 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
230 add_builtin_variable(& builtin_core_fs_variables[i],
Ian Romanick5e18b052010-06-29 14:21:05 -0700231 instructions, state->symbols);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700232 }
233
Eric Anholt0f09aea2010-03-27 12:48:57 -0700234 for (unsigned i = 0
235 ; i < Elements(builtin_110_deprecated_fs_variables)
236 ; i++) {
237 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
Ian Romanick5e18b052010-06-29 14:21:05 -0700238 instructions, state->symbols);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700239 }
Ian Romanick5e18b052010-06-29 14:21:05 -0700240 generate_110_uniforms(instructions, state->symbols);
Eric Anholt0f09aea2010-03-27 12:48:57 -0700241
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700242 /* FINISHME: The size of this array is implementation dependent based on the
Ian Romanickcfb35362010-06-07 19:10:33 -0700243 * FINISHME: value of GL_MAX_TEXTURE_COORDS. Every platform that supports
244 * FINISHME: GLSL sets GL_MAX_TEXTURE_COORDS to at least 4, so hard-code 4
245 * FINISHME: for now.
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700246 */
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700247 const glsl_type *const vec4_array_type =
Ian Romanick5e18b052010-06-29 14:21:05 -0700248 glsl_type::get_array_instance(state->symbols, glsl_type::vec4_type, 4);
Ian Romanick3f9a73d2010-04-02 11:59:57 -0700249
Ian Romanicked0626e2010-06-21 11:42:57 -0700250 add_variable("gl_TexCoord", ir_var_in, FRAG_ATTRIB_TEX0, vec4_array_type,
Ian Romanick5e18b052010-06-29 14:21:05 -0700251 instructions, state->symbols);
Ian Romanick9c4b1f22010-06-29 15:10:09 -0700252
253 generate_ARB_draw_buffers_fs_variables(instructions, state, false);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700254}
255
Ian Romanickc77b2572010-04-07 16:59:46 -0700256
257static void
258generate_ARB_draw_buffers_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700259 struct _mesa_glsl_parse_state *state,
260 bool warn)
Ian Romanickc77b2572010-04-07 16:59:46 -0700261{
Ian Romanickc77b2572010-04-07 16:59:46 -0700262 const glsl_type *const vec4_array_type =
Ian Romanick5e18b052010-06-29 14:21:05 -0700263 glsl_type::get_array_instance(state->symbols, glsl_type::vec4_type,
264 state->Const.MaxDrawBuffers);
Ian Romanickc77b2572010-04-07 16:59:46 -0700265
266 ir_variable *const fd =
Ian Romanicked0626e2010-06-21 11:42:57 -0700267 add_variable("gl_FragData", ir_var_out, FRAG_RESULT_DATA0,
Ian Romanick5e18b052010-06-29 14:21:05 -0700268 vec4_array_type, instructions, state->symbols);
Ian Romanickc77b2572010-04-07 16:59:46 -0700269
270 if (warn)
271 fd->warn_extension = "GL_ARB_draw_buffers";
272}
273
274
Eric Anholtb3f743a2010-03-25 14:48:25 -0700275static void
276generate_120_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700277 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700278{
Ian Romanick5e18b052010-06-29 14:21:05 -0700279 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700280}
281
282static void
283generate_130_fs_variables(exec_list *instructions,
Ian Romanick5e18b052010-06-29 14:21:05 -0700284 struct _mesa_glsl_parse_state *state)
Eric Anholtb3f743a2010-03-25 14:48:25 -0700285{
Ian Romanick5e18b052010-06-29 14:21:05 -0700286 void *ctx = state->symbols;
287 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700288
Ian Romanick8645a952010-04-07 16:47:44 -0700289 /* FINISHME: The size of this array is implementation dependent based on
290 * FINISHME: the value of GL_MAX_CLIP_DISTANCES.
Eric Anholtb3f743a2010-03-25 14:48:25 -0700291 */
Ian Romanick8645a952010-04-07 16:47:44 -0700292 const glsl_type *const clip_distance_array_type =
Carl Worth12c41152010-06-18 17:52:59 -0700293 glsl_type::get_array_instance(ctx, glsl_type::float_type, 8);
Ian Romanicked0626e2010-06-21 11:42:57 -0700294
295 /* FINISHME: gl_ClipDistance needs a real location assigned. */
296 add_variable("gl_ClipDistance", ir_var_in, -1, clip_distance_array_type,
Ian Romanick5e18b052010-06-29 14:21:05 -0700297 instructions, state->symbols);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700298}
299
300static void
301initialize_fs_variables(exec_list *instructions,
302 struct _mesa_glsl_parse_state *state)
303{
304
305 switch (state->language_version) {
306 case 110:
Ian Romanick5e18b052010-06-29 14:21:05 -0700307 generate_110_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700308 break;
309 case 120:
Ian Romanick5e18b052010-06-29 14:21:05 -0700310 generate_120_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700311 break;
312 case 130:
Ian Romanick5e18b052010-06-29 14:21:05 -0700313 generate_130_fs_variables(instructions, state);
Eric Anholtb3f743a2010-03-25 14:48:25 -0700314 break;
315 }
316}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800317
318void
319_mesa_glsl_initialize_variables(exec_list *instructions,
320 struct _mesa_glsl_parse_state *state)
321{
322 switch (state->target) {
323 case vertex_shader:
324 initialize_vs_variables(instructions, state);
325 break;
326 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700327 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800328 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700329 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800330 break;
Eric Anholt81f49a72010-04-29 17:57:28 -0700331 case ir_shader:
332 fprintf(stderr, "ir reader has no builtin variables");
333 exit(1);
334 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800335 }
336}