glsl: Support the 'invariant(all)' pragma
[mesa.git] / src / glsl / glsl_parser_extras.h
1 /*
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 #pragma once
25 #ifndef GLSL_PARSER_EXTRAS_H
26 #define GLSL_PARSER_EXTRAS_H
27
28 /*
29 * Most of the definitions here only apply to C++
30 */
31 #ifdef __cplusplus
32
33
34 #include <cstdlib>
35 #include "glsl_symbol_table.h"
36
37 enum _mesa_glsl_parser_targets {
38 vertex_shader,
39 geometry_shader,
40 fragment_shader
41 };
42
43 struct gl_context;
44
45 struct _mesa_glsl_parse_state {
46 _mesa_glsl_parse_state(struct gl_context *ctx, GLenum target,
47 void *mem_ctx);
48
49 /* Callers of this talloc-based new need not call delete. It's
50 * easier to just talloc_free 'ctx' (or any of its ancestors). */
51 static void* operator new(size_t size, void *ctx)
52 {
53 void *mem = talloc_zero_size(ctx, size);
54 assert(mem != NULL);
55
56 return mem;
57 }
58
59 /* If the user *does* call delete, that's OK, we will just
60 * talloc_free in that case. */
61 static void operator delete(void *mem)
62 {
63 talloc_free(mem);
64 }
65
66 void *scanner;
67 exec_list translation_unit;
68 glsl_symbol_table *symbols;
69
70 bool es_shader;
71 unsigned language_version;
72 enum _mesa_glsl_parser_targets target;
73
74 /**
75 * Implementation defined limits that affect built-in variables, etc.
76 *
77 * \sa struct gl_constants (in mtypes.h)
78 */
79 struct {
80 /* 1.10 */
81 unsigned MaxLights;
82 unsigned MaxClipPlanes;
83 unsigned MaxTextureUnits;
84 unsigned MaxTextureCoords;
85 unsigned MaxVertexAttribs;
86 unsigned MaxVertexUniformComponents;
87 unsigned MaxVaryingFloats;
88 unsigned MaxVertexTextureImageUnits;
89 unsigned MaxCombinedTextureImageUnits;
90 unsigned MaxTextureImageUnits;
91 unsigned MaxFragmentUniformComponents;
92
93 /* ARB_draw_buffers */
94 unsigned MaxDrawBuffers;
95 } Const;
96
97 /**
98 * During AST to IR conversion, pointer to current IR function
99 *
100 * Will be \c NULL whenever the AST to IR conversion is not inside a
101 * function definition.
102 */
103 class ir_function_signature *current_function;
104
105 /** Have we found a return statement in this function? */
106 bool found_return;
107
108 /** Was there an error during compilation? */
109 bool error;
110
111 /**
112 * Are all shader inputs / outputs invariant?
113 *
114 * This is set when the 'STDGL invariant(all)' pragma is used.
115 */
116 bool all_invariant;
117
118 /** Loop or switch statement containing the current instructions. */
119 class ir_instruction *loop_or_switch_nesting;
120 class ast_iteration_statement *loop_or_switch_nesting_ast;
121
122 /** List of structures defined in user code. */
123 const glsl_type **user_structures;
124 unsigned num_user_structures;
125
126 char *info_log;
127
128 /**
129 * \name Enable bits for GLSL extensions
130 */
131 /*@{*/
132 unsigned ARB_draw_buffers_enable:1;
133 unsigned ARB_draw_buffers_warn:1;
134 unsigned ARB_explicit_attrib_location_enable:1;
135 unsigned ARB_explicit_attrib_location_warn:1;
136 unsigned ARB_fragment_coord_conventions_enable:1;
137 unsigned ARB_fragment_coord_conventions_warn:1;
138 unsigned ARB_texture_rectangle_enable:1;
139 unsigned ARB_texture_rectangle_warn:1;
140 unsigned EXT_texture_array_enable:1;
141 unsigned EXT_texture_array_warn:1;
142 unsigned ARB_shader_stencil_export_enable:1;
143 unsigned ARB_shader_stencil_export_warn:1;
144 /*@}*/
145
146 /** Extensions supported by the OpenGL implementation. */
147 const struct gl_extensions *extensions;
148
149 /** Shaders containing built-in functions that are used for linking. */
150 struct gl_shader *builtins_to_link[16];
151 unsigned num_builtins_to_link;
152 };
153
154 typedef struct YYLTYPE {
155 int first_line;
156 int first_column;
157 int last_line;
158 int last_column;
159 unsigned source;
160 } YYLTYPE;
161 # define YYLTYPE_IS_DECLARED 1
162 # define YYLTYPE_IS_TRIVIAL 1
163
164 # define YYLLOC_DEFAULT(Current, Rhs, N) \
165 do { \
166 if (N) \
167 { \
168 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
169 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
170 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
171 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
172 } \
173 else \
174 { \
175 (Current).first_line = (Current).last_line = \
176 YYRHSLOC(Rhs, 0).last_line; \
177 (Current).first_column = (Current).last_column = \
178 YYRHSLOC(Rhs, 0).last_column; \
179 } \
180 (Current).source = 0; \
181 } while (0)
182
183 extern void _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
184 const char *fmt, ...);
185
186 /**
187 * Emit a warning to the shader log
188 *
189 * \sa _mesa_glsl_error
190 */
191 extern void _mesa_glsl_warning(const YYLTYPE *locp,
192 _mesa_glsl_parse_state *state,
193 const char *fmt, ...);
194
195 extern void _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state,
196 const char *string);
197
198 extern void _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state);
199
200 union YYSTYPE;
201 extern int _mesa_glsl_lex(union YYSTYPE *yylval, YYLTYPE *yylloc,
202 void *scanner);
203
204 extern int _mesa_glsl_parse(struct _mesa_glsl_parse_state *);
205
206 /**
207 * Process elements of the #extension directive
208 *
209 * \return
210 * If \c name and \c behavior are valid, \c true is returned. Otherwise
211 * \c false is returned.
212 */
213 extern bool _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
214 const char *behavior,
215 YYLTYPE *behavior_locp,
216 _mesa_glsl_parse_state *state);
217
218 /**
219 * Get the textual name of the specified shader target
220 */
221 extern const char *
222 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target);
223
224
225 #endif /* __cplusplus */
226
227
228 /*
229 * These definitions apply to C and C++
230 */
231 #ifdef __cplusplus
232 extern "C" {
233 #endif
234
235 extern int preprocess(void *ctx, const char **shader, char **info_log,
236 const struct gl_extensions *extensions, int api);
237
238 extern void _mesa_destroy_shader_compiler();
239 extern void _mesa_destroy_shader_compiler_caches();
240
241 #ifdef __cplusplus
242 }
243 #endif
244
245
246 #endif /* GLSL_PARSER_EXTRAS_H */