glsl_parser_extras.h revision 551bdf25bc4e57bea51c54da7e31c44c507e6c9f
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
37enum _mesa_glsl_parser_targets {
38   vertex_shader,
39   geometry_shader,
40   fragment_shader
41};
42
43struct gl_context;
44
45struct 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
59struct _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    * Default uniform layout qualifiers tracked during parsing.
96    * Currently affects uniform blocks and uniform buffer variables in
97    * those blocks.
98    */
99   class ast_type_qualifier *default_uniform_qualifier;
100
101   /**
102    * Printable list of GLSL versions supported by the current context
103    *
104    * \note
105    * This string should probably be generated per-context instead of per
106    * invokation of the compiler.  This should be changed when the method of
107    * tracking supported GLSL versions changes.
108    */
109   const char *supported_version_string;
110
111   /**
112    * Implementation defined limits that affect built-in variables, etc.
113    *
114    * \sa struct gl_constants (in mtypes.h)
115    */
116   struct {
117      /* 1.10 */
118      unsigned MaxLights;
119      unsigned MaxClipPlanes;
120      unsigned MaxTextureUnits;
121      unsigned MaxTextureCoords;
122      unsigned MaxVertexAttribs;
123      unsigned MaxVertexUniformComponents;
124      unsigned MaxVaryingFloats;
125      unsigned MaxVertexTextureImageUnits;
126      unsigned MaxCombinedTextureImageUnits;
127      unsigned MaxTextureImageUnits;
128      unsigned MaxFragmentUniformComponents;
129
130      /* ARB_draw_buffers */
131      unsigned MaxDrawBuffers;
132   } Const;
133
134   /**
135    * During AST to IR conversion, pointer to current IR function
136    *
137    * Will be \c NULL whenever the AST to IR conversion is not inside a
138    * function definition.
139    */
140   class ir_function_signature *current_function;
141
142   /**
143    * During AST to IR conversion, pointer to the toplevel IR
144    * instruction list being generated.
145    */
146   exec_list *toplevel_ir;
147
148   /** Have we found a return statement in this function? */
149   bool found_return;
150
151   /** Was there an error during compilation? */
152   bool error;
153
154   /**
155    * Are all shader inputs / outputs invariant?
156    *
157    * This is set when the 'STDGL invariant(all)' pragma is used.
158    */
159   bool all_invariant;
160
161   /** Loop or switch statement containing the current instructions. */
162   class ast_iteration_statement *loop_nesting_ast;
163
164   struct glsl_switch_state switch_state;
165
166   /** List of structures defined in user code. */
167   const glsl_type **user_structures;
168   unsigned num_user_structures;
169
170   char *info_log;
171
172   /**
173    * \name Enable bits for GLSL extensions
174    */
175   /*@{*/
176   bool ARB_draw_buffers_enable;
177   bool ARB_draw_buffers_warn;
178   bool ARB_draw_instanced_enable;
179   bool ARB_draw_instanced_warn;
180   bool ARB_explicit_attrib_location_enable;
181   bool ARB_explicit_attrib_location_warn;
182   bool ARB_fragment_coord_conventions_enable;
183   bool ARB_fragment_coord_conventions_warn;
184   bool ARB_texture_rectangle_enable;
185   bool ARB_texture_rectangle_warn;
186   bool EXT_texture_array_enable;
187   bool EXT_texture_array_warn;
188   bool ARB_shader_texture_lod_enable;
189   bool ARB_shader_texture_lod_warn;
190   bool ARB_shader_stencil_export_enable;
191   bool ARB_shader_stencil_export_warn;
192   bool AMD_conservative_depth_enable;
193   bool AMD_conservative_depth_warn;
194   bool ARB_conservative_depth_enable;
195   bool ARB_conservative_depth_warn;
196   bool AMD_shader_stencil_export_enable;
197   bool AMD_shader_stencil_export_warn;
198   bool OES_texture_3D_enable;
199   bool OES_texture_3D_warn;
200   bool OES_EGL_image_external_enable;
201   bool OES_EGL_image_external_warn;
202   bool ARB_shader_bit_encoding_enable;
203   bool ARB_shader_bit_encoding_warn;
204   bool ARB_uniform_buffer_object_enable;
205   bool ARB_uniform_buffer_object_warn;
206   /*@}*/
207
208   /** Extensions supported by the OpenGL implementation. */
209   const struct gl_extensions *extensions;
210
211   /** Shaders containing built-in functions that are used for linking. */
212   struct gl_shader *builtins_to_link[16];
213   unsigned num_builtins_to_link;
214};
215
216typedef struct YYLTYPE {
217   int first_line;
218   int first_column;
219   int last_line;
220   int last_column;
221   unsigned source;
222} YYLTYPE;
223# define YYLTYPE_IS_DECLARED 1
224# define YYLTYPE_IS_TRIVIAL 1
225
226# define YYLLOC_DEFAULT(Current, Rhs, N)			\
227do {								\
228   if (N)							\
229   {								\
230      (Current).first_line   = YYRHSLOC(Rhs, 1).first_line;	\
231      (Current).first_column = YYRHSLOC(Rhs, 1).first_column;	\
232      (Current).last_line    = YYRHSLOC(Rhs, N).last_line;	\
233      (Current).last_column  = YYRHSLOC(Rhs, N).last_column;	\
234   }								\
235   else								\
236   {								\
237      (Current).first_line   = (Current).last_line =		\
238	 YYRHSLOC(Rhs, 0).last_line;				\
239      (Current).first_column = (Current).last_column =		\
240	 YYRHSLOC(Rhs, 0).last_column;				\
241   }								\
242   (Current).source = 0;					\
243} while (0)
244
245extern void _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
246			     const char *fmt, ...);
247
248/**
249 * Emit a warning to the shader log
250 *
251 * \sa _mesa_glsl_error
252 */
253extern void _mesa_glsl_warning(const YYLTYPE *locp,
254			       _mesa_glsl_parse_state *state,
255			       const char *fmt, ...);
256
257extern void _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state,
258				  const char *string);
259
260extern void _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state);
261
262union YYSTYPE;
263extern int _mesa_glsl_lex(union YYSTYPE *yylval, YYLTYPE *yylloc,
264			  void *scanner);
265
266extern int _mesa_glsl_parse(struct _mesa_glsl_parse_state *);
267
268/**
269 * Process elements of the #extension directive
270 *
271 * \return
272 * If \c name and \c behavior are valid, \c true is returned.  Otherwise
273 * \c false is returned.
274 */
275extern bool _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
276					 const char *behavior,
277					 YYLTYPE *behavior_locp,
278					 _mesa_glsl_parse_state *state);
279
280/**
281 * Get the textual name of the specified shader target
282 */
283extern const char *
284_mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target);
285
286
287#endif /* __cplusplus */
288
289
290/*
291 * These definitions apply to C and C++
292 */
293#ifdef __cplusplus
294extern "C" {
295#endif
296
297extern int preprocess(void *ctx, const char **shader, char **info_log,
298                      const struct gl_extensions *extensions, int api);
299
300extern void _mesa_destroy_shader_compiler(void);
301extern void _mesa_destroy_shader_compiler_caches(void);
302
303#ifdef __cplusplus
304}
305#endif
306
307
308#endif /* GLSL_PARSER_EXTRAS_H */
309