35d1e3aee26ea90ebf665aaffb8ce66a0013a2d6
[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 <stdlib.h>
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 glsl_switch_state {
46 /** Temporary variables needed for switch statement. */
47 ir_variable *test_var;
48 ir_variable *is_fallthru_var;
49 ir_variable *is_break_var;
50 class ast_switch_statement *switch_nesting_ast;
51 bool is_switch_innermost; // if switch stmt is closest to break, ...
52 };
53
54 struct _mesa_glsl_parse_state {
55 _mesa_glsl_parse_state(struct gl_context *ctx, GLenum target,
56 void *mem_ctx);
57
58 /* Callers of this ralloc-based new need not call delete. It's
59 * easier to just ralloc_free 'ctx' (or any of its ancestors). */
60 static void* operator new(size_t size, void *ctx)
61 {
62 void *mem = rzalloc_size(ctx, size);
63 assert(mem != NULL);
64
65 return mem;
66 }
67
68 /* If the user *does* call delete, that's OK, we will just
69 * ralloc_free in that case. */
70 static void operator delete(void *mem)
71 {
72 ralloc_free(mem);
73 }
74
75 void *scanner;
76 exec_list translation_unit;
77 glsl_symbol_table *symbols;
78
79 bool es_shader;
80 unsigned language_version;
81 const char *version_string;
82 enum _mesa_glsl_parser_targets target;
83
84 /**
85 * Printable list of GLSL versions supported by the current context
86 *
87 * \note
88 * This string should probably be generated per-context instead of per
89 * invokation of the compiler. This should be changed when the method of
90 * tracking supported GLSL versions changes.
91 */
92 const char *supported_version_string;
93
94 /**
95 * Implementation defined limits that affect built-in variables, etc.
96 *
97 * \sa struct gl_constants (in mtypes.h)
98 */
99 struct {
100 /* 1.10 */
101 unsigned MaxLights;
102 unsigned MaxClipPlanes;
103 unsigned MaxTextureUnits;
104 unsigned MaxTextureCoords;
105 unsigned MaxVertexAttribs;
106 unsigned MaxVertexUniformComponents;
107 unsigned MaxVaryingFloats;
108 unsigned MaxVertexTextureImageUnits;
109 unsigned MaxCombinedTextureImageUnits;
110 unsigned MaxTextureImageUnits;
111 unsigned MaxFragmentUniformComponents;
112
113 /* ARB_draw_buffers */
114 unsigned MaxDrawBuffers;
115
116 /**
117 * Set of GLSL versions supported by the current context
118 *
119 * Knowing that version X is supported doesn't mean that versions before
120 * X are also supported. Version 1.00 is only supported in an ES2
121 * context or when GL_ARB_ES2_compatibility is supported. In an OpenGL
122 * 3.0 "forward compatible" context, GLSL 1.10 and 1.20 are \b not
123 * supported.
124 */
125 /*@{*/
126 unsigned GLSL_100ES:1;
127 unsigned GLSL_110:1;
128 unsigned GLSL_120:1;
129 unsigned GLSL_130:1;
130 /*@}*/
131 } Const;
132
133 /**
134 * During AST to IR conversion, pointer to current IR function
135 *
136 * Will be \c NULL whenever the AST to IR conversion is not inside a
137 * function definition.
138 */
139 class ir_function_signature *current_function;
140
141 /**
142 * During AST to IR conversion, pointer to the toplevel IR
143 * instruction list being generated.
144 */
145 exec_list *toplevel_ir;
146
147 /** Have we found a return statement in this function? */
148 bool found_return;
149
150 /** Was there an error during compilation? */
151 bool error;
152
153 /**
154 * Are all shader inputs / outputs invariant?
155 *
156 * This is set when the 'STDGL invariant(all)' pragma is used.
157 */
158 bool all_invariant;
159
160 /** Loop or switch statement containing the current instructions. */
161 class ast_iteration_statement *loop_nesting_ast;
162
163 struct glsl_switch_state switch_state;
164
165 /** List of structures defined in user code. */
166 const glsl_type **user_structures;
167 unsigned num_user_structures;
168
169 char *info_log;
170
171 /**
172 * \name Enable bits for GLSL extensions
173 */
174 /*@{*/
175 bool ARB_draw_buffers_enable;
176 bool ARB_draw_buffers_warn;
177 bool ARB_draw_instanced_enable;
178 bool ARB_draw_instanced_warn;
179 bool ARB_explicit_attrib_location_enable;
180 bool ARB_explicit_attrib_location_warn;
181 bool ARB_fragment_coord_conventions_enable;
182 bool ARB_fragment_coord_conventions_warn;
183 bool ARB_texture_rectangle_enable;
184 bool ARB_texture_rectangle_warn;
185 bool EXT_texture_array_enable;
186 bool EXT_texture_array_warn;
187 bool ARB_shader_texture_lod_enable;
188 bool ARB_shader_texture_lod_warn;
189 bool ARB_shader_stencil_export_enable;
190 bool ARB_shader_stencil_export_warn;
191 bool AMD_conservative_depth_enable;
192 bool AMD_conservative_depth_warn;
193 bool ARB_conservative_depth_enable;
194 bool ARB_conservative_depth_warn;
195 bool AMD_shader_stencil_export_enable;
196 bool AMD_shader_stencil_export_warn;
197 bool OES_texture_3D_enable;
198 bool OES_texture_3D_warn;
199 bool OES_EGL_image_external_enable;
200 bool OES_EGL_image_external_warn;
201 /*@}*/
202
203 /** Extensions supported by the OpenGL implementation. */
204 const struct gl_extensions *extensions;
205
206 /** Shaders containing built-in functions that are used for linking. */
207 struct gl_shader *builtins_to_link[16];
208 unsigned num_builtins_to_link;
209 };
210
211 typedef struct YYLTYPE {
212 int first_line;
213 int first_column;
214 int last_line;
215 int last_column;
216 unsigned source;
217 } YYLTYPE;
218 # define YYLTYPE_IS_DECLARED 1
219 # define YYLTYPE_IS_TRIVIAL 1
220
221 # define YYLLOC_DEFAULT(Current, Rhs, N) \
222 do { \
223 if (N) \
224 { \
225 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
226 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
227 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
228 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
229 } \
230 else \
231 { \
232 (Current).first_line = (Current).last_line = \
233 YYRHSLOC(Rhs, 0).last_line; \
234 (Current).first_column = (Current).last_column = \
235 YYRHSLOC(Rhs, 0).last_column; \
236 } \
237 (Current).source = 0; \
238 } while (0)
239
240 extern void _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
241 const char *fmt, ...);
242
243 /**
244 * Emit a warning to the shader log
245 *
246 * \sa _mesa_glsl_error
247 */
248 extern void _mesa_glsl_warning(const YYLTYPE *locp,
249 _mesa_glsl_parse_state *state,
250 const char *fmt, ...);
251
252 extern void _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state,
253 const char *string);
254
255 extern void _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state);
256
257 union YYSTYPE;
258 extern int _mesa_glsl_lex(union YYSTYPE *yylval, YYLTYPE *yylloc,
259 void *scanner);
260
261 extern int _mesa_glsl_parse(struct _mesa_glsl_parse_state *);
262
263 /**
264 * Process elements of the #extension directive
265 *
266 * \return
267 * If \c name and \c behavior are valid, \c true is returned. Otherwise
268 * \c false is returned.
269 */
270 extern bool _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
271 const char *behavior,
272 YYLTYPE *behavior_locp,
273 _mesa_glsl_parse_state *state);
274
275 /**
276 * Get the textual name of the specified shader target
277 */
278 extern const char *
279 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target);
280
281
282 #endif /* __cplusplus */
283
284
285 /*
286 * These definitions apply to C and C++
287 */
288 #ifdef __cplusplus
289 extern "C" {
290 #endif
291
292 extern int preprocess(void *ctx, const char **shader, char **info_log,
293 const struct gl_extensions *extensions, int api);
294
295 extern void _mesa_destroy_shader_compiler(void);
296 extern void _mesa_destroy_shader_compiler_caches(void);
297
298 #ifdef __cplusplus
299 }
300 #endif
301
302
303 #endif /* GLSL_PARSER_EXTRAS_H */