blob: df8e4c3ba829e8085c040fbae88260038dac3958 [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
59
60static void
61generate_110_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070062 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080063{
64 for (unsigned i = 0; i < Elements(builtin_core_vs_variables); i++) {
65 add_builtin_variable(& builtin_core_vs_variables[i],
66 instructions, symtab);
67 }
68
69 for (unsigned i = 0
70 ; i < Elements(builtin_110_deprecated_vs_variables)
71 ; i++) {
72 add_builtin_variable(& builtin_110_deprecated_vs_variables[i],
73 instructions, symtab);
74 }
75
76 /* FINISHME: Add support fo gl_TexCoord. The size of this array is
77 * FINISHME: implementation dependent based on the value of
78 * FINISHME: GL_MAX_TEXTURE_COORDS.
79 */
80}
81
82
83static void
84generate_120_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070085 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080086{
87 /* GLSL version 1.20 did not add any built-in variables in the vertex
88 * shader.
89 */
90 generate_110_vs_variables(instructions, symtab);
91}
92
93
94static void
95generate_130_vs_variables(exec_list *instructions,
Ian Romanick8bde4ce2010-03-19 11:57:24 -070096 glsl_symbol_table *symtab)
Ian Romanickadfb0cd2010-03-10 10:43:16 -080097{
98 generate_120_vs_variables(instructions, symtab);
99
100 for (unsigned i = 0; i < Elements(builtin_130_vs_variables); i++) {
101 add_builtin_variable(& builtin_130_vs_variables[i],
102 instructions, symtab);
103 }
104
105 /* FINISHME: Add support fo gl_ClipDistance. The size of this array is
106 * FINISHME: implementation dependent based on the value of
107 * FINISHME: GL_MAX_CLIP_DISTANCES.
108 */
109}
110
111
112static void
113initialize_vs_variables(exec_list *instructions,
114 struct _mesa_glsl_parse_state *state)
115{
116
117 switch (state->language_version) {
118 case 110:
119 generate_110_vs_variables(instructions, state->symbols);
120 break;
121 case 120:
122 generate_120_vs_variables(instructions, state->symbols);
123 break;
124 case 130:
125 generate_130_vs_variables(instructions, state->symbols);
126 break;
127 }
128}
129
Eric Anholtb3f743a2010-03-25 14:48:25 -0700130static void
131generate_110_fs_variables(exec_list *instructions,
132 glsl_symbol_table *symtab)
133{
134 for (unsigned i = 0; i < Elements(builtin_core_fs_variables); i++) {
135 add_builtin_variable(& builtin_core_fs_variables[i],
136 instructions, symtab);
137 }
138
Eric Anholt0f09aea2010-03-27 12:48:57 -0700139 /* FINISHME: Add support for gl_TexCoord[] */
140 for (unsigned i = 0
141 ; i < Elements(builtin_110_deprecated_fs_variables)
142 ; i++) {
143 add_builtin_variable(& builtin_110_deprecated_fs_variables[i],
144 instructions, symtab);
145 }
146
Eric Anholtb3f743a2010-03-25 14:48:25 -0700147 /* FINISHME: Add support for gl_FragData[GL_MAX_DRAW_BUFFERS]. */
148}
149
150static void
151generate_120_fs_variables(exec_list *instructions,
152 glsl_symbol_table *symtab)
153{
154 /* GLSL version 1.20 did not add any built-in variables in the fragment
155 * shader.
156 */
157 generate_110_fs_variables(instructions, symtab);
158}
159
160static void
161generate_130_fs_variables(exec_list *instructions,
162 glsl_symbol_table *symtab)
163{
164 generate_120_fs_variables(instructions, symtab);
165
166 /* FINISHME: Add support fo gl_ClipDistance. The size of this array is
167 * FINISHME: implementation dependent based on the value of
168 * FINISHME: GL_MAX_CLIP_DISTANCES.
169 */
170}
171
172static void
173initialize_fs_variables(exec_list *instructions,
174 struct _mesa_glsl_parse_state *state)
175{
176
177 switch (state->language_version) {
178 case 110:
179 generate_110_fs_variables(instructions, state->symbols);
180 break;
181 case 120:
182 generate_120_fs_variables(instructions, state->symbols);
183 break;
184 case 130:
185 generate_130_fs_variables(instructions, state->symbols);
186 break;
187 }
188}
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800189
190void
191_mesa_glsl_initialize_variables(exec_list *instructions,
192 struct _mesa_glsl_parse_state *state)
193{
194 switch (state->target) {
195 case vertex_shader:
196 initialize_vs_variables(instructions, state);
197 break;
198 case geometry_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700199 break;
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800200 case fragment_shader:
Eric Anholtb3f743a2010-03-25 14:48:25 -0700201 initialize_fs_variables(instructions, state);
Ian Romanickadfb0cd2010-03-10 10:43:16 -0800202 break;
203 }
204}