8d8b4d89bc407a9692081fc283553d12c86fea13
[mesa.git] / src / mesa / shader / program_parser.h
1 /*
2 * Copyright © 2009 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 #pragma once
24
25 #include "main/config.h"
26
27 #ifndef MTYPES_H
28 struct __GLcontextRec;
29 typedef struct __GLcontextRec GLcontext;
30 #endif
31
32 enum asm_type {
33 at_none,
34 at_address,
35 at_attrib,
36 at_param,
37 at_temp,
38 at_output,
39 };
40
41 /**
42 * \note
43 * Objects of this type are allocated as the object plus the name of the
44 * symbol. That is, malloc(sizeof(struct asm_symbol) + strlen(name) + 1).
45 * Alternately, asm_symbol::name could be moved to the bottom of the structure
46 * and declared as 'char name[0];'.
47 */
48 struct asm_symbol {
49 struct asm_symbol *next; /**< List linkage for freeing. */
50 const char *name;
51 enum asm_type type;
52 unsigned attrib_binding;
53 unsigned output_binding; /**< Output / result register number. */
54
55 /**
56 * One of PROGRAM_STATE_VAR, PROGRAM_LOCAL_PARAM, or PROGRAM_ENV_PARAM.
57 */
58 unsigned param_binding_type;
59
60 /**
61 * Offset into the program_parameter_list where the tokens representing our
62 * bound state (or constants) start.
63 */
64 unsigned param_binding_begin;
65
66 /* This is how many entries in the the program_parameter_list we take up
67 * with our state tokens or constants. Note that this is _not_ the same as
68 * the number of param registers we eventually use.
69 */
70 unsigned param_binding_length;
71
72 /**
73 * Index of the temp register assigned to this variable.
74 */
75 unsigned temp_binding;
76
77 /**
78 * Flag whether or not a PARAM is an array
79 */
80 unsigned param_is_array:1;
81
82
83 /**
84 * Flag whether or not a PARAM array is accessed indirectly
85 */
86 unsigned param_accessed_indirectly:1;
87
88
89 /**
90 * \brief Is first pass of parameter layout done with this variable?
91 *
92 * The parameter layout routine operates in two passes. This flag tracks
93 * whether or not the first pass has handled this variable.
94 *
95 * \sa _mesa_layout_parameters
96 */
97 unsigned pass1_done:1;
98 };
99
100
101 struct asm_vector {
102 unsigned count;
103 float data[4];
104 };
105
106
107 struct asm_swizzle_mask {
108 unsigned swizzle:12;
109 unsigned mask:4;
110 };
111
112
113 struct asm_src_register {
114 struct prog_src_register Base;
115
116 /**
117 * Symbol associated with indirect access to parameter arrays.
118 *
119 * If \c Base::RelAddr is 1, this will point to the symbol for the parameter
120 * that is being dereferenced. Further, \c Base::Index will be the offset
121 * from the address register being used.
122 */
123 struct asm_symbol *Symbol;
124 };
125
126
127 struct asm_instruction {
128 struct prog_instruction Base;
129 struct asm_instruction *next;
130 struct asm_src_register SrcReg[3];
131 };
132
133
134 struct asm_parser_state {
135 GLcontext *ctx;
136 struct gl_program *prog;
137
138 /**
139 * Per-program target limits
140 */
141 struct gl_program_constants *limits;
142
143 struct _mesa_symbol_table *st;
144
145 /**
146 * Linked list of symbols
147 *
148 * This list is \b only used when cleaning up compiler state and freeing
149 * memory.
150 */
151 struct asm_symbol *sym;
152
153 /**
154 * State for the lexer.
155 */
156 void *scanner;
157
158 /**
159 * Linked list of instructions generated during parsing.
160 */
161 /*@{*/
162 struct asm_instruction *inst_head;
163 struct asm_instruction *inst_tail;
164 /*@}*/
165
166
167 /**
168 * Buffer to hold strings transfered from the lexer to the parser
169 */
170 /*@{*/
171 char *string_dumpster; /**< String data transfered. */
172 size_t dumpster_size; /**< Total size, in bytes, of the buffer. */
173 /*@}*/
174
175
176 /**
177 * Selected limits copied from gl_constants
178 *
179 * These are limits from the GL context, but various bits in the program
180 * must be validated against these values.
181 */
182 /*@{*/
183 unsigned MaxTextureCoordUnits;
184 unsigned MaxTextureImageUnits;
185 unsigned MaxTextureUnits;
186 unsigned MaxClipPlanes;
187 unsigned MaxLights;
188 unsigned MaxProgramMatrices;
189 /*@}*/
190
191 /**
192 * Value to use in state vector accessors for environment and local
193 * parameters
194 */
195 unsigned state_param_enum;
196
197
198 /**
199 * Input attributes bound to specific names
200 *
201 * This is only needed so that errors can be properly produced when
202 * multiple ATTRIB statements bind illegal combinations of vertex
203 * attributes.
204 */
205 unsigned InputsBound;
206
207 enum {
208 invalid_mode = 0,
209 ARB_vertex,
210 ARB_fragment
211 } mode;
212
213 struct {
214 unsigned PositionInvariant:1;
215 unsigned Fog:2;
216 unsigned PrecisionHint:2;
217 unsigned DrawBuffers:1;
218 unsigned Shadow:1;
219 unsigned TexRect:1;
220 unsigned TexArray:1;
221 } option;
222
223 struct {
224 unsigned UsesKill:1;
225 } fragment;
226 };
227
228 #define OPTION_NONE 0
229 #define OPTION_FOG_EXP 1
230 #define OPTION_FOG_EXP2 2
231 #define OPTION_FOG_LINEAR 3
232 #define OPTION_NICEST 1
233 #define OPTION_FASTEST 2
234
235 typedef struct YYLTYPE {
236 int first_line;
237 int first_column;
238 int last_line;
239 int last_column;
240 int position;
241 } YYLTYPE;
242
243 #define YYLTYPE_IS_DECLARED 1
244 #define YYLTYPE_IS_TRIVIAL 1
245
246
247 extern GLboolean _mesa_parse_arb_program(GLcontext *ctx, GLenum target,
248 const GLubyte *str, GLsizei len, struct asm_parser_state *state);
249
250
251
252 /* From program_lexer.l. */
253 extern void _mesa_program_lexer_dtor(void *scanner);
254
255 extern void _mesa_program_lexer_ctor(void **scanner,
256 struct asm_parser_state *state, const char *string, size_t len);
257
258
259 /**
260 *\name From program_parse_extra.c
261 */
262 /*@{*/
263
264 /**
265 * Parses and processes an option string to an ARB vertex program
266 *
267 * \return
268 * Non-zero on success, zero on failure.
269 */
270 extern int _mesa_ARBvp_parse_option(struct asm_parser_state *state,
271 const char *option);
272
273 /**
274 * Parses and processes an option string to an ARB fragment program
275 *
276 * \return
277 * Non-zero on success, zero on failure.
278 */
279 extern int _mesa_ARBfp_parse_option(struct asm_parser_state *state,
280 const char *option);
281
282 /*@}*/