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