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