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