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