glsl: add texture gather changes
[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 typedef struct YYLTYPE {
63 int first_line;
64 int first_column;
65 int last_line;
66 int last_column;
67 unsigned source;
68 } YYLTYPE;
69 # define YYLTYPE_IS_DECLARED 1
70 # define YYLTYPE_IS_TRIVIAL 1
71
72 struct _mesa_glsl_parse_state {
73 _mesa_glsl_parse_state(struct gl_context *_ctx, GLenum target,
74 void *mem_ctx);
75
76 DECLARE_RALLOC_CXX_OPERATORS(_mesa_glsl_parse_state);
77
78 /**
79 * Generate a string representing the GLSL version currently being compiled
80 * (useful for error messages).
81 */
82 const char *get_version_string()
83 {
84 return glsl_compute_version_string(this, this->es_shader,
85 this->language_version);
86 }
87
88 /**
89 * Determine whether the current GLSL version is sufficiently high to
90 * support a certain feature.
91 *
92 * \param required_glsl_version is the desktop GLSL version that is
93 * required to support the feature, or 0 if no version of desktop GLSL
94 * supports the feature.
95 *
96 * \param required_glsl_es_version is the GLSL ES version that is required
97 * to support the feature, or 0 if no version of GLSL ES suports the
98 * feature.
99 */
100 bool is_version(unsigned required_glsl_version,
101 unsigned required_glsl_es_version) const
102 {
103 unsigned required_version = this->es_shader ?
104 required_glsl_es_version : required_glsl_version;
105 return required_version != 0
106 && this->language_version >= required_version;
107 }
108
109 bool check_version(unsigned required_glsl_version,
110 unsigned required_glsl_es_version,
111 YYLTYPE *locp, const char *fmt, ...) PRINTFLIKE(5, 6);
112
113 bool check_precision_qualifiers_allowed(YYLTYPE *locp)
114 {
115 return check_version(130, 100, locp,
116 "precision qualifiers are forbidden");
117 }
118
119 bool check_bitwise_operations_allowed(YYLTYPE *locp)
120 {
121 return check_version(130, 300, locp, "bit-wise operations are forbidden");
122 }
123
124 bool has_explicit_attrib_location() const
125 {
126 return ARB_explicit_attrib_location_enable || is_version(330, 300);
127 }
128
129 bool has_uniform_buffer_objects() const
130 {
131 return ARB_uniform_buffer_object_enable || is_version(140, 300);
132 }
133
134 void process_version_directive(YYLTYPE *locp, int version,
135 const char *ident);
136
137 struct gl_context *const ctx;
138 void *scanner;
139 exec_list translation_unit;
140 glsl_symbol_table *symbols;
141
142 unsigned num_uniform_blocks;
143 unsigned uniform_block_array_size;
144 struct gl_uniform_block *uniform_blocks;
145
146 unsigned num_supported_versions;
147 struct {
148 unsigned ver;
149 bool es;
150 } supported_versions[12];
151
152 bool es_shader;
153 unsigned language_version;
154 enum _mesa_glsl_parser_targets target;
155
156 /**
157 * Number of nested struct_specifier levels
158 *
159 * Outside a struct_specifer, this is zero.
160 */
161 unsigned struct_specifier_depth;
162
163 /**
164 * Default uniform layout qualifiers tracked during parsing.
165 * Currently affects uniform blocks and uniform buffer variables in
166 * those blocks.
167 */
168 struct ast_type_qualifier *default_uniform_qualifier;
169
170 /**
171 * True if a geometry shader input primitive type was specified using a
172 * layout directive.
173 *
174 * Note: this value is computed at ast_to_hir time rather than at parse
175 * time.
176 */
177 bool gs_input_prim_type_specified;
178
179 /**
180 * If gs_input_prim_type_specified is true, the primitive type that was
181 * specified. Otherwise ignored.
182 */
183 GLenum gs_input_prim_type;
184
185 /** Output layout qualifiers from GLSL 1.50. (geometry shader controls)*/
186 struct ast_type_qualifier *out_qualifier;
187
188 /**
189 * Printable list of GLSL versions supported by the current context
190 *
191 * \note
192 * This string should probably be generated per-context instead of per
193 * invokation of the compiler. This should be changed when the method of
194 * tracking supported GLSL versions changes.
195 */
196 const char *supported_version_string;
197
198 /**
199 * Implementation defined limits that affect built-in variables, etc.
200 *
201 * \sa struct gl_constants (in mtypes.h)
202 */
203 struct {
204 /* 1.10 */
205 unsigned MaxLights;
206 unsigned MaxClipPlanes;
207 unsigned MaxTextureUnits;
208 unsigned MaxTextureCoords;
209 unsigned MaxVertexAttribs;
210 unsigned MaxVertexUniformComponents;
211 unsigned MaxVaryingFloats;
212 unsigned MaxVertexTextureImageUnits;
213 unsigned MaxCombinedTextureImageUnits;
214 unsigned MaxTextureImageUnits;
215 unsigned MaxFragmentUniformComponents;
216
217 /* ARB_draw_buffers */
218 unsigned MaxDrawBuffers;
219
220 /* 3.00 ES */
221 int MinProgramTexelOffset;
222 int MaxProgramTexelOffset;
223 } Const;
224
225 /**
226 * During AST to IR conversion, pointer to current IR function
227 *
228 * Will be \c NULL whenever the AST to IR conversion is not inside a
229 * function definition.
230 */
231 class ir_function_signature *current_function;
232
233 /**
234 * During AST to IR conversion, pointer to the toplevel IR
235 * instruction list being generated.
236 */
237 exec_list *toplevel_ir;
238
239 /** Have we found a return statement in this function? */
240 bool found_return;
241
242 /** Was there an error during compilation? */
243 bool error;
244
245 /**
246 * Are all shader inputs / outputs invariant?
247 *
248 * This is set when the 'STDGL invariant(all)' pragma is used.
249 */
250 bool all_invariant;
251
252 /** Loop or switch statement containing the current instructions. */
253 class ast_iteration_statement *loop_nesting_ast;
254
255 struct glsl_switch_state switch_state;
256
257 /** List of structures defined in user code. */
258 const glsl_type **user_structures;
259 unsigned num_user_structures;
260
261 char *info_log;
262
263 /**
264 * \name Enable bits for GLSL extensions
265 */
266 /*@{*/
267 bool ARB_draw_buffers_enable;
268 bool ARB_draw_buffers_warn;
269 bool ARB_draw_instanced_enable;
270 bool ARB_draw_instanced_warn;
271 bool ARB_explicit_attrib_location_enable;
272 bool ARB_explicit_attrib_location_warn;
273 bool ARB_fragment_coord_conventions_enable;
274 bool ARB_fragment_coord_conventions_warn;
275 bool ARB_texture_rectangle_enable;
276 bool ARB_texture_rectangle_warn;
277 bool ARB_texture_gather_enable;
278 bool ARB_texture_gather_warn;
279 bool EXT_texture_array_enable;
280 bool EXT_texture_array_warn;
281 bool ARB_shader_texture_lod_enable;
282 bool ARB_shader_texture_lod_warn;
283 bool ARB_shader_stencil_export_enable;
284 bool ARB_shader_stencil_export_warn;
285 bool AMD_conservative_depth_enable;
286 bool AMD_conservative_depth_warn;
287 bool ARB_conservative_depth_enable;
288 bool ARB_conservative_depth_warn;
289 bool AMD_shader_stencil_export_enable;
290 bool AMD_shader_stencil_export_warn;
291 bool OES_texture_3D_enable;
292 bool OES_texture_3D_warn;
293 bool OES_EGL_image_external_enable;
294 bool OES_EGL_image_external_warn;
295 bool ARB_shader_bit_encoding_enable;
296 bool ARB_shader_bit_encoding_warn;
297 bool ARB_uniform_buffer_object_enable;
298 bool ARB_uniform_buffer_object_warn;
299 bool OES_standard_derivatives_enable;
300 bool OES_standard_derivatives_warn;
301 bool ARB_texture_cube_map_array_enable;
302 bool ARB_texture_cube_map_array_warn;
303 bool ARB_shading_language_packing_enable;
304 bool ARB_shading_language_packing_warn;
305 bool ARB_texture_multisample_enable;
306 bool ARB_texture_multisample_warn;
307 bool ARB_texture_query_lod_enable;
308 bool ARB_texture_query_lod_warn;
309 bool ARB_gpu_shader5_enable;
310 bool ARB_gpu_shader5_warn;
311 bool AMD_vertex_shader_layer_enable;
312 bool AMD_vertex_shader_layer_warn;
313 bool ARB_shading_language_420pack_enable;
314 bool ARB_shading_language_420pack_warn;
315 bool EXT_shader_integer_mix_enable;
316 bool EXT_shader_integer_mix_warn;
317 /*@}*/
318
319 /** Extensions supported by the OpenGL implementation. */
320 const struct gl_extensions *extensions;
321
322 /** Shaders containing built-in functions that are used for linking. */
323 struct gl_shader *builtins_to_link[16];
324 unsigned num_builtins_to_link;
325
326 /**
327 * For geometry shaders, size of the most recently seen input declaration
328 * that was a sized array, or 0 if no sized input array declarations have
329 * been seen.
330 *
331 * Unused for other shader types.
332 */
333 unsigned gs_input_size;
334 };
335
336 # define YYLLOC_DEFAULT(Current, Rhs, N) \
337 do { \
338 if (N) \
339 { \
340 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
341 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
342 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
343 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
344 } \
345 else \
346 { \
347 (Current).first_line = (Current).last_line = \
348 YYRHSLOC(Rhs, 0).last_line; \
349 (Current).first_column = (Current).last_column = \
350 YYRHSLOC(Rhs, 0).last_column; \
351 } \
352 (Current).source = 0; \
353 } while (0)
354
355 extern void _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
356 const char *fmt, ...);
357
358 /**
359 * Emit a warning to the shader log
360 *
361 * \sa _mesa_glsl_error
362 */
363 extern void _mesa_glsl_warning(const YYLTYPE *locp,
364 _mesa_glsl_parse_state *state,
365 const char *fmt, ...);
366
367 extern void _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state,
368 const char *string);
369
370 extern void _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state);
371
372 union YYSTYPE;
373 extern int _mesa_glsl_lexer_lex(union YYSTYPE *yylval, YYLTYPE *yylloc,
374 void *scanner);
375
376 extern int _mesa_glsl_parse(struct _mesa_glsl_parse_state *);
377
378 /**
379 * Process elements of the #extension directive
380 *
381 * \return
382 * If \c name and \c behavior are valid, \c true is returned. Otherwise
383 * \c false is returned.
384 */
385 extern bool _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
386 const char *behavior,
387 YYLTYPE *behavior_locp,
388 _mesa_glsl_parse_state *state);
389
390 /**
391 * Get the textual name of the specified shader target
392 */
393 extern const char *
394 _mesa_glsl_shader_target_name(enum _mesa_glsl_parser_targets target);
395
396
397 #endif /* __cplusplus */
398
399
400 /*
401 * These definitions apply to C and C++
402 */
403 #ifdef __cplusplus
404 extern "C" {
405 #endif
406
407 extern const char *
408 _mesa_glsl_shader_target_name(GLenum type);
409
410 extern int glcpp_preprocess(void *ctx, const char **shader, char **info_log,
411 const struct gl_extensions *extensions, struct gl_context *gl_ctx);
412
413 extern void _mesa_destroy_shader_compiler(void);
414 extern void _mesa_destroy_shader_compiler_caches(void);
415
416 #ifdef __cplusplus
417 }
418 #endif
419
420
421 #endif /* GLSL_PARSER_EXTRAS_H */