2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 #ifndef GLSL_PARSER_EXTRAS_H
26 #define GLSL_PARSER_EXTRAS_H
29 * Most of the definitions here only apply to C++
35 #include "glsl_symbol_table.h"
39 struct glsl_switch_state
{
40 /** Temporary variables needed for switch statement. */
41 ir_variable
*test_var
;
42 ir_variable
*is_fallthru_var
;
43 ir_variable
*is_break_var
;
44 class ast_switch_statement
*switch_nesting_ast
;
46 /** Table of constant values already used in case labels */
47 struct hash_table
*labels_ht
;
48 class ast_case_label
*previous_default
;
50 bool is_switch_innermost
; // if switch stmt is closest to break, ...
54 glsl_compute_version_string(void *mem_ctx
, bool is_es
, unsigned version
);
56 typedef struct YYLTYPE
{
63 # define YYLTYPE_IS_DECLARED 1
64 # define YYLTYPE_IS_TRIVIAL 1
66 extern void _mesa_glsl_error(YYLTYPE
*locp
, _mesa_glsl_parse_state
*state
,
67 const char *fmt
, ...);
70 struct _mesa_glsl_parse_state
{
71 _mesa_glsl_parse_state(struct gl_context
*_ctx
, gl_shader_stage stage
,
74 DECLARE_RALLOC_CXX_OPERATORS(_mesa_glsl_parse_state
);
77 * Generate a string representing the GLSL version currently being compiled
78 * (useful for error messages).
80 const char *get_version_string()
82 return glsl_compute_version_string(this, this->es_shader
,
83 this->language_version
);
87 * Determine whether the current GLSL version is sufficiently high to
88 * support a certain feature.
90 * \param required_glsl_version is the desktop GLSL version that is
91 * required to support the feature, or 0 if no version of desktop GLSL
92 * supports the feature.
94 * \param required_glsl_es_version is the GLSL ES version that is required
95 * to support the feature, or 0 if no version of GLSL ES suports the
98 bool is_version(unsigned required_glsl_version
,
99 unsigned required_glsl_es_version
) const
101 unsigned required_version
= this->es_shader
?
102 required_glsl_es_version
: required_glsl_version
;
103 return required_version
!= 0
104 && this->language_version
>= required_version
;
107 bool check_version(unsigned required_glsl_version
,
108 unsigned required_glsl_es_version
,
109 YYLTYPE
*locp
, const char *fmt
, ...) PRINTFLIKE(5, 6);
111 bool check_precision_qualifiers_allowed(YYLTYPE
*locp
)
113 return check_version(130, 100, locp
,
114 "precision qualifiers are forbidden");
117 bool check_bitwise_operations_allowed(YYLTYPE
*locp
)
119 return check_version(130, 300, locp
, "bit-wise operations are forbidden");
122 bool check_explicit_attrib_location_allowed(YYLTYPE
*locp
,
123 const ir_variable
*var
)
125 if (!this->has_explicit_attrib_location()) {
126 const char *const requirement
= this->es_shader
128 : "GL_ARB_explicit_attrib_location extension or GLSL 330";
130 _mesa_glsl_error(locp
, this, "%s explicit location requires %s",
131 mode_string(var
), requirement
);
138 bool has_explicit_attrib_location() const
140 return ARB_explicit_attrib_location_enable
|| is_version(330, 300);
143 bool has_uniform_buffer_objects() const
145 return ARB_uniform_buffer_object_enable
|| is_version(140, 300);
148 void process_version_directive(YYLTYPE
*locp
, int version
,
151 struct gl_context
*const ctx
;
153 exec_list translation_unit
;
154 glsl_symbol_table
*symbols
;
156 unsigned num_uniform_blocks
;
157 unsigned uniform_block_array_size
;
158 struct gl_uniform_block
*uniform_blocks
;
160 unsigned num_supported_versions
;
164 } supported_versions
[12];
167 unsigned language_version
;
168 gl_shader_stage stage
;
171 * Number of nested struct_specifier levels
173 * Outside a struct_specifer, this is zero.
175 unsigned struct_specifier_depth
;
178 * Default uniform layout qualifiers tracked during parsing.
179 * Currently affects uniform blocks and uniform buffer variables in
182 struct ast_type_qualifier
*default_uniform_qualifier
;
185 * True if a geometry shader input primitive type was specified using a
188 * Note: this value is computed at ast_to_hir time rather than at parse
191 bool gs_input_prim_type_specified
;
194 * If gs_input_prim_type_specified is true, the primitive type that was
195 * specified. Otherwise ignored.
197 GLenum gs_input_prim_type
;
200 * True if a compute shader input local size was specified using a layout
203 * Note: this value is computed at ast_to_hir time rather than at parse
206 bool cs_input_local_size_specified
;
209 * If cs_input_local_size_specified is true, the local size that was
210 * specified. Otherwise ignored.
212 unsigned cs_input_local_size
[3];
214 /** Output layout qualifiers from GLSL 1.50. (geometry shader controls)*/
215 struct ast_type_qualifier
*out_qualifier
;
218 * Printable list of GLSL versions supported by the current context
221 * This string should probably be generated per-context instead of per
222 * invokation of the compiler. This should be changed when the method of
223 * tracking supported GLSL versions changes.
225 const char *supported_version_string
;
228 * Implementation defined limits that affect built-in variables, etc.
230 * \sa struct gl_constants (in mtypes.h)
235 unsigned MaxClipPlanes
;
236 unsigned MaxTextureUnits
;
237 unsigned MaxTextureCoords
;
238 unsigned MaxVertexAttribs
;
239 unsigned MaxVertexUniformComponents
;
240 unsigned MaxVertexTextureImageUnits
;
241 unsigned MaxCombinedTextureImageUnits
;
242 unsigned MaxTextureImageUnits
;
243 unsigned MaxFragmentUniformComponents
;
245 /* ARB_draw_buffers */
246 unsigned MaxDrawBuffers
;
249 int MinProgramTexelOffset
;
250 int MaxProgramTexelOffset
;
253 unsigned MaxVertexOutputComponents
;
254 unsigned MaxGeometryInputComponents
;
255 unsigned MaxGeometryOutputComponents
;
256 unsigned MaxFragmentInputComponents
;
257 unsigned MaxGeometryTextureImageUnits
;
258 unsigned MaxGeometryOutputVertices
;
259 unsigned MaxGeometryTotalOutputComponents
;
260 unsigned MaxGeometryUniformComponents
;
262 /* ARB_shader_atomic_counters */
263 unsigned MaxVertexAtomicCounters
;
264 unsigned MaxGeometryAtomicCounters
;
265 unsigned MaxFragmentAtomicCounters
;
266 unsigned MaxCombinedAtomicCounters
;
267 unsigned MaxAtomicBufferBindings
;
269 /* ARB_compute_shader */
270 unsigned MaxComputeWorkGroupCount
[3];
271 unsigned MaxComputeWorkGroupSize
[3];
273 /* ARB_shader_image_load_store */
274 unsigned MaxImageUnits
;
275 unsigned MaxCombinedImageUnitsAndFragmentOutputs
;
276 unsigned MaxImageSamples
;
277 unsigned MaxVertexImageUniforms
;
278 unsigned MaxGeometryImageUniforms
;
279 unsigned MaxFragmentImageUniforms
;
280 unsigned MaxCombinedImageUniforms
;
284 * During AST to IR conversion, pointer to current IR function
286 * Will be \c NULL whenever the AST to IR conversion is not inside a
287 * function definition.
289 class ir_function_signature
*current_function
;
292 * During AST to IR conversion, pointer to the toplevel IR
293 * instruction list being generated.
295 exec_list
*toplevel_ir
;
297 /** Have we found a return statement in this function? */
300 /** Was there an error during compilation? */
304 * Are all shader inputs / outputs invariant?
306 * This is set when the 'STDGL invariant(all)' pragma is used.
310 /** Loop or switch statement containing the current instructions. */
311 class ast_iteration_statement
*loop_nesting_ast
;
313 struct glsl_switch_state switch_state
;
315 /** List of structures defined in user code. */
316 const glsl_type
**user_structures
;
317 unsigned num_user_structures
;
322 * \name Enable bits for GLSL extensions
325 bool ARB_arrays_of_arrays_enable
;
326 bool ARB_arrays_of_arrays_warn
;
327 bool ARB_draw_buffers_enable
;
328 bool ARB_draw_buffers_warn
;
329 bool ARB_draw_instanced_enable
;
330 bool ARB_draw_instanced_warn
;
331 bool ARB_explicit_attrib_location_enable
;
332 bool ARB_explicit_attrib_location_warn
;
333 bool ARB_fragment_coord_conventions_enable
;
334 bool ARB_fragment_coord_conventions_warn
;
335 bool ARB_texture_rectangle_enable
;
336 bool ARB_texture_rectangle_warn
;
337 bool ARB_texture_gather_enable
;
338 bool ARB_texture_gather_warn
;
339 bool EXT_texture_array_enable
;
340 bool EXT_texture_array_warn
;
341 bool ARB_shader_texture_lod_enable
;
342 bool ARB_shader_texture_lod_warn
;
343 bool ARB_shader_stencil_export_enable
;
344 bool ARB_shader_stencil_export_warn
;
345 bool AMD_conservative_depth_enable
;
346 bool AMD_conservative_depth_warn
;
347 bool ARB_conservative_depth_enable
;
348 bool ARB_conservative_depth_warn
;
349 bool AMD_shader_stencil_export_enable
;
350 bool AMD_shader_stencil_export_warn
;
351 bool OES_texture_3D_enable
;
352 bool OES_texture_3D_warn
;
353 bool OES_EGL_image_external_enable
;
354 bool OES_EGL_image_external_warn
;
355 bool ARB_shader_bit_encoding_enable
;
356 bool ARB_shader_bit_encoding_warn
;
357 bool ARB_uniform_buffer_object_enable
;
358 bool ARB_uniform_buffer_object_warn
;
359 bool OES_standard_derivatives_enable
;
360 bool OES_standard_derivatives_warn
;
361 bool ARB_texture_cube_map_array_enable
;
362 bool ARB_texture_cube_map_array_warn
;
363 bool ARB_shading_language_packing_enable
;
364 bool ARB_shading_language_packing_warn
;
365 bool ARB_texture_multisample_enable
;
366 bool ARB_texture_multisample_warn
;
367 bool ARB_texture_query_levels_enable
;
368 bool ARB_texture_query_levels_warn
;
369 bool ARB_texture_query_lod_enable
;
370 bool ARB_texture_query_lod_warn
;
371 bool ARB_gpu_shader5_enable
;
372 bool ARB_gpu_shader5_warn
;
373 bool AMD_vertex_shader_layer_enable
;
374 bool AMD_vertex_shader_layer_warn
;
375 bool ARB_shading_language_420pack_enable
;
376 bool ARB_shading_language_420pack_warn
;
377 bool ARB_sample_shading_enable
;
378 bool ARB_sample_shading_warn
;
379 bool EXT_shader_integer_mix_enable
;
380 bool EXT_shader_integer_mix_warn
;
381 bool ARB_shader_atomic_counters_enable
;
382 bool ARB_shader_atomic_counters_warn
;
383 bool AMD_shader_trinary_minmax_enable
;
384 bool AMD_shader_trinary_minmax_warn
;
385 bool ARB_viewport_array_enable
;
386 bool ARB_viewport_array_warn
;
387 bool ARB_compute_shader_enable
;
388 bool ARB_compute_shader_warn
;
389 bool ARB_shader_image_load_store_enable
;
390 bool ARB_shader_image_load_store_warn
;
393 /** Extensions supported by the OpenGL implementation. */
394 const struct gl_extensions
*extensions
;
396 bool uses_builtin_functions
;
399 * For geometry shaders, size of the most recently seen input declaration
400 * that was a sized array, or 0 if no sized input array declarations have
403 * Unused for other shader types.
405 unsigned gs_input_size
;
407 bool early_fragment_tests
;
409 /** Atomic counter offsets by binding */
410 unsigned atomic_counter_offsets
[MAX_COMBINED_ATOMIC_BUFFERS
];
413 # define YYLLOC_DEFAULT(Current, Rhs, N) \
417 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
418 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
419 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
420 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
424 (Current).first_line = (Current).last_line = \
425 YYRHSLOC(Rhs, 0).last_line; \
426 (Current).first_column = (Current).last_column = \
427 YYRHSLOC(Rhs, 0).last_column; \
429 (Current).source = 0; \
433 * Emit a warning to the shader log
435 * \sa _mesa_glsl_error
437 extern void _mesa_glsl_warning(const YYLTYPE
*locp
,
438 _mesa_glsl_parse_state
*state
,
439 const char *fmt
, ...);
441 extern void _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state
*state
,
444 extern void _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state
*state
);
447 extern int _mesa_glsl_lexer_lex(union YYSTYPE
*yylval
, YYLTYPE
*yylloc
,
450 extern int _mesa_glsl_parse(struct _mesa_glsl_parse_state
*);
453 * Process elements of the #extension directive
456 * If \c name and \c behavior are valid, \c true is returned. Otherwise
457 * \c false is returned.
459 extern bool _mesa_glsl_process_extension(const char *name
, YYLTYPE
*name_locp
,
460 const char *behavior
,
461 YYLTYPE
*behavior_locp
,
462 _mesa_glsl_parse_state
*state
);
464 #endif /* __cplusplus */
468 * These definitions apply to C and C++
475 * Get the textual name of the specified shader stage (which is a
479 _mesa_shader_stage_to_string(unsigned stage
);
481 extern int glcpp_preprocess(void *ctx
, const char **shader
, char **info_log
,
482 const struct gl_extensions
*extensions
, struct gl_context
*gl_ctx
);
484 extern void _mesa_destroy_shader_compiler(void);
485 extern void _mesa_destroy_shader_compiler_caches(void);
492 #endif /* GLSL_PARSER_EXTRAS_H */