glsl: add a mechanism to allow #extension directives in the middle of shaders
[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 struct gl_context;
38
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;
45
46 /** Used to set condition if 'default' label should be chosen. */
47 ir_variable *run_default;
48
49 /** Table of constant values already used in case labels */
50 struct hash_table *labels_ht;
51 class ast_case_label *previous_default;
52
53 bool is_switch_innermost; // if switch stmt is closest to break, ...
54 };
55
56 const char *
57 glsl_compute_version_string(void *mem_ctx, bool is_es, unsigned version);
58
59 typedef struct YYLTYPE {
60 int first_line;
61 int first_column;
62 int last_line;
63 int last_column;
64 unsigned source;
65 } YYLTYPE;
66 # define YYLTYPE_IS_DECLARED 1
67 # define YYLTYPE_IS_TRIVIAL 1
68
69 extern void _mesa_glsl_error(YYLTYPE *locp, _mesa_glsl_parse_state *state,
70 const char *fmt, ...);
71
72
73 struct _mesa_glsl_parse_state {
74 _mesa_glsl_parse_state(struct gl_context *_ctx, gl_shader_stage stage,
75 void *mem_ctx);
76
77 DECLARE_RALLOC_CXX_OPERATORS(_mesa_glsl_parse_state);
78
79 /**
80 * Generate a string representing the GLSL version currently being compiled
81 * (useful for error messages).
82 */
83 const char *get_version_string()
84 {
85 return glsl_compute_version_string(this, this->es_shader,
86 this->language_version);
87 }
88
89 /**
90 * Determine whether the current GLSL version is sufficiently high to
91 * support a certain feature.
92 *
93 * \param required_glsl_version is the desktop GLSL version that is
94 * required to support the feature, or 0 if no version of desktop GLSL
95 * supports the feature.
96 *
97 * \param required_glsl_es_version is the GLSL ES version that is required
98 * to support the feature, or 0 if no version of GLSL ES suports the
99 * feature.
100 */
101 bool is_version(unsigned required_glsl_version,
102 unsigned required_glsl_es_version) const
103 {
104 unsigned required_version = this->es_shader ?
105 required_glsl_es_version : required_glsl_version;
106 return required_version != 0
107 && this->language_version >= required_version;
108 }
109
110 bool check_version(unsigned required_glsl_version,
111 unsigned required_glsl_es_version,
112 YYLTYPE *locp, const char *fmt, ...) PRINTFLIKE(5, 6);
113
114 bool check_precision_qualifiers_allowed(YYLTYPE *locp)
115 {
116 return check_version(130, 100, locp,
117 "precision qualifiers are forbidden");
118 }
119
120 bool check_bitwise_operations_allowed(YYLTYPE *locp)
121 {
122 return check_version(130, 300, locp, "bit-wise operations are forbidden");
123 }
124
125 bool check_explicit_attrib_stream_allowed(YYLTYPE *locp)
126 {
127 if (!this->has_explicit_attrib_stream()) {
128 const char *const requirement = "GL_ARB_gpu_shader5 extension or GLSL 400";
129
130 _mesa_glsl_error(locp, this, "explicit stream requires %s",
131 requirement);
132 return false;
133 }
134
135 return true;
136 }
137
138 bool check_explicit_attrib_location_allowed(YYLTYPE *locp,
139 const ir_variable *var)
140 {
141 if (!this->has_explicit_attrib_location()) {
142 const char *const requirement = this->es_shader
143 ? "GLSL ES 300"
144 : "GL_ARB_explicit_attrib_location extension or GLSL 330";
145
146 _mesa_glsl_error(locp, this, "%s explicit location requires %s",
147 mode_string(var), requirement);
148 return false;
149 }
150
151 return true;
152 }
153
154 bool check_separate_shader_objects_allowed(YYLTYPE *locp,
155 const ir_variable *var)
156 {
157 if (!this->has_separate_shader_objects()) {
158 const char *const requirement = this->es_shader
159 ? "GL_EXT_separate_shader_objects extension"
160 : "GL_ARB_separate_shader_objects extension or GLSL 420";
161
162 _mesa_glsl_error(locp, this, "%s explicit location requires %s",
163 mode_string(var), requirement);
164 return false;
165 }
166
167 return true;
168 }
169
170 bool check_explicit_uniform_location_allowed(YYLTYPE *locp,
171 const ir_variable *)
172 {
173 if (!this->has_explicit_attrib_location() ||
174 !this->ARB_explicit_uniform_location_enable) {
175 _mesa_glsl_error(locp, this,
176 "uniform explicit location requires "
177 "GL_ARB_explicit_uniform_location and either "
178 "GL_ARB_explicit_attrib_location or GLSL 330.");
179 return false;
180 }
181
182 return true;
183 }
184
185 bool has_explicit_attrib_stream() const
186 {
187 return ARB_gpu_shader5_enable || is_version(400, 0);
188 }
189
190 bool has_explicit_attrib_location() const
191 {
192 return ARB_explicit_attrib_location_enable || is_version(330, 300);
193 }
194
195 bool has_uniform_buffer_objects() const
196 {
197 return ARB_uniform_buffer_object_enable || is_version(140, 300);
198 }
199
200 bool has_separate_shader_objects() const
201 {
202 return ARB_separate_shader_objects_enable || is_version(410, 0)
203 || EXT_separate_shader_objects_enable;
204 }
205
206 void process_version_directive(YYLTYPE *locp, int version,
207 const char *ident);
208
209 struct gl_context *const ctx;
210 void *scanner;
211 exec_list translation_unit;
212 glsl_symbol_table *symbols;
213
214 unsigned num_supported_versions;
215 struct {
216 unsigned ver;
217 bool es;
218 } supported_versions[12];
219
220 bool es_shader;
221 unsigned language_version;
222 gl_shader_stage stage;
223
224 /**
225 * Number of nested struct_specifier levels
226 *
227 * Outside a struct_specifier, this is zero.
228 */
229 unsigned struct_specifier_depth;
230
231 /**
232 * Default uniform layout qualifiers tracked during parsing.
233 * Currently affects uniform blocks and uniform buffer variables in
234 * those blocks.
235 */
236 struct ast_type_qualifier *default_uniform_qualifier;
237
238 /**
239 * Variables to track different cases if a fragment shader redeclares
240 * built-in variable gl_FragCoord.
241 *
242 * Note: These values are computed at ast_to_hir time rather than at parse
243 * time.
244 */
245 bool fs_redeclares_gl_fragcoord;
246 bool fs_origin_upper_left;
247 bool fs_pixel_center_integer;
248 bool fs_redeclares_gl_fragcoord_with_no_layout_qualifiers;
249
250 /**
251 * True if a geometry shader input primitive type was specified using a
252 * layout directive.
253 *
254 * Note: this value is computed at ast_to_hir time rather than at parse
255 * time.
256 */
257 bool gs_input_prim_type_specified;
258
259 /** Input layout qualifiers from GLSL 1.50. (geometry shader controls)*/
260 struct ast_type_qualifier *in_qualifier;
261
262 /**
263 * True if a compute shader input local size was specified using a layout
264 * directive.
265 *
266 * Note: this value is computed at ast_to_hir time rather than at parse
267 * time.
268 */
269 bool cs_input_local_size_specified;
270
271 /**
272 * If cs_input_local_size_specified is true, the local size that was
273 * specified. Otherwise ignored.
274 */
275 unsigned cs_input_local_size[3];
276
277 /** Output layout qualifiers from GLSL 1.50. (geometry shader controls)*/
278 struct ast_type_qualifier *out_qualifier;
279
280 /**
281 * Printable list of GLSL versions supported by the current context
282 *
283 * \note
284 * This string should probably be generated per-context instead of per
285 * invokation of the compiler. This should be changed when the method of
286 * tracking supported GLSL versions changes.
287 */
288 const char *supported_version_string;
289
290 /**
291 * Implementation defined limits that affect built-in variables, etc.
292 *
293 * \sa struct gl_constants (in mtypes.h)
294 */
295 struct {
296 /* 1.10 */
297 unsigned MaxLights;
298 unsigned MaxClipPlanes;
299 unsigned MaxTextureUnits;
300 unsigned MaxTextureCoords;
301 unsigned MaxVertexAttribs;
302 unsigned MaxVertexUniformComponents;
303 unsigned MaxVertexTextureImageUnits;
304 unsigned MaxCombinedTextureImageUnits;
305 unsigned MaxTextureImageUnits;
306 unsigned MaxFragmentUniformComponents;
307
308 /* ARB_draw_buffers */
309 unsigned MaxDrawBuffers;
310
311 /* 3.00 ES */
312 int MinProgramTexelOffset;
313 int MaxProgramTexelOffset;
314
315 /* 1.50 */
316 unsigned MaxVertexOutputComponents;
317 unsigned MaxGeometryInputComponents;
318 unsigned MaxGeometryOutputComponents;
319 unsigned MaxFragmentInputComponents;
320 unsigned MaxGeometryTextureImageUnits;
321 unsigned MaxGeometryOutputVertices;
322 unsigned MaxGeometryTotalOutputComponents;
323 unsigned MaxGeometryUniformComponents;
324
325 /* ARB_shader_atomic_counters */
326 unsigned MaxVertexAtomicCounters;
327 unsigned MaxGeometryAtomicCounters;
328 unsigned MaxFragmentAtomicCounters;
329 unsigned MaxCombinedAtomicCounters;
330 unsigned MaxAtomicBufferBindings;
331
332 /* ARB_compute_shader */
333 unsigned MaxComputeWorkGroupCount[3];
334 unsigned MaxComputeWorkGroupSize[3];
335
336 /* ARB_shader_image_load_store */
337 unsigned MaxImageUnits;
338 unsigned MaxCombinedImageUnitsAndFragmentOutputs;
339 unsigned MaxImageSamples;
340 unsigned MaxVertexImageUniforms;
341 unsigned MaxGeometryImageUniforms;
342 unsigned MaxFragmentImageUniforms;
343 unsigned MaxCombinedImageUniforms;
344 } Const;
345
346 /**
347 * During AST to IR conversion, pointer to current IR function
348 *
349 * Will be \c NULL whenever the AST to IR conversion is not inside a
350 * function definition.
351 */
352 class ir_function_signature *current_function;
353
354 /**
355 * During AST to IR conversion, pointer to the toplevel IR
356 * instruction list being generated.
357 */
358 exec_list *toplevel_ir;
359
360 /** Have we found a return statement in this function? */
361 bool found_return;
362
363 /** Was there an error during compilation? */
364 bool error;
365
366 /**
367 * Are all shader inputs / outputs invariant?
368 *
369 * This is set when the 'STDGL invariant(all)' pragma is used.
370 */
371 bool all_invariant;
372
373 /** Loop or switch statement containing the current instructions. */
374 class ast_iteration_statement *loop_nesting_ast;
375
376 struct glsl_switch_state switch_state;
377
378 /** List of structures defined in user code. */
379 const glsl_type **user_structures;
380 unsigned num_user_structures;
381
382 char *info_log;
383
384 /**
385 * \name Enable bits for GLSL extensions
386 */
387 /*@{*/
388 /* ARB extensions go here, sorted alphabetically.
389 */
390 bool ARB_arrays_of_arrays_enable;
391 bool ARB_arrays_of_arrays_warn;
392 bool ARB_compute_shader_enable;
393 bool ARB_compute_shader_warn;
394 bool ARB_conservative_depth_enable;
395 bool ARB_conservative_depth_warn;
396 bool ARB_draw_buffers_enable;
397 bool ARB_draw_buffers_warn;
398 bool ARB_draw_instanced_enable;
399 bool ARB_draw_instanced_warn;
400 bool ARB_explicit_attrib_location_enable;
401 bool ARB_explicit_attrib_location_warn;
402 bool ARB_explicit_uniform_location_enable;
403 bool ARB_explicit_uniform_location_warn;
404 bool ARB_fragment_coord_conventions_enable;
405 bool ARB_fragment_coord_conventions_warn;
406 bool ARB_fragment_layer_viewport_enable;
407 bool ARB_fragment_layer_viewport_warn;
408 bool ARB_gpu_shader5_enable;
409 bool ARB_gpu_shader5_warn;
410 bool ARB_sample_shading_enable;
411 bool ARB_sample_shading_warn;
412 bool ARB_separate_shader_objects_enable;
413 bool ARB_separate_shader_objects_warn;
414 bool ARB_shader_atomic_counters_enable;
415 bool ARB_shader_atomic_counters_warn;
416 bool ARB_shader_bit_encoding_enable;
417 bool ARB_shader_bit_encoding_warn;
418 bool ARB_shader_image_load_store_enable;
419 bool ARB_shader_image_load_store_warn;
420 bool ARB_shader_stencil_export_enable;
421 bool ARB_shader_stencil_export_warn;
422 bool ARB_shader_texture_lod_enable;
423 bool ARB_shader_texture_lod_warn;
424 bool ARB_shading_language_420pack_enable;
425 bool ARB_shading_language_420pack_warn;
426 bool ARB_shading_language_packing_enable;
427 bool ARB_shading_language_packing_warn;
428 bool ARB_texture_cube_map_array_enable;
429 bool ARB_texture_cube_map_array_warn;
430 bool ARB_texture_gather_enable;
431 bool ARB_texture_gather_warn;
432 bool ARB_texture_multisample_enable;
433 bool ARB_texture_multisample_warn;
434 bool ARB_texture_query_levels_enable;
435 bool ARB_texture_query_levels_warn;
436 bool ARB_texture_query_lod_enable;
437 bool ARB_texture_query_lod_warn;
438 bool ARB_texture_rectangle_enable;
439 bool ARB_texture_rectangle_warn;
440 bool ARB_uniform_buffer_object_enable;
441 bool ARB_uniform_buffer_object_warn;
442 bool ARB_viewport_array_enable;
443 bool ARB_viewport_array_warn;
444
445 /* KHR extensions go here, sorted alphabetically.
446 */
447
448 /* OES extensions go here, sorted alphabetically.
449 */
450 bool OES_EGL_image_external_enable;
451 bool OES_EGL_image_external_warn;
452 bool OES_standard_derivatives_enable;
453 bool OES_standard_derivatives_warn;
454 bool OES_texture_3D_enable;
455 bool OES_texture_3D_warn;
456
457 /* All other extensions go here, sorted alphabetically.
458 */
459 bool AMD_conservative_depth_enable;
460 bool AMD_conservative_depth_warn;
461 bool AMD_shader_stencil_export_enable;
462 bool AMD_shader_stencil_export_warn;
463 bool AMD_shader_trinary_minmax_enable;
464 bool AMD_shader_trinary_minmax_warn;
465 bool AMD_vertex_shader_layer_enable;
466 bool AMD_vertex_shader_layer_warn;
467 bool AMD_vertex_shader_viewport_index_enable;
468 bool AMD_vertex_shader_viewport_index_warn;
469 bool EXT_separate_shader_objects_enable;
470 bool EXT_separate_shader_objects_warn;
471 bool EXT_shader_integer_mix_enable;
472 bool EXT_shader_integer_mix_warn;
473 bool EXT_texture_array_enable;
474 bool EXT_texture_array_warn;
475 /*@}*/
476
477 /** Extensions supported by the OpenGL implementation. */
478 const struct gl_extensions *extensions;
479
480 bool uses_builtin_functions;
481 bool fs_uses_gl_fragcoord;
482
483 /**
484 * For geometry shaders, size of the most recently seen input declaration
485 * that was a sized array, or 0 if no sized input array declarations have
486 * been seen.
487 *
488 * Unused for other shader types.
489 */
490 unsigned gs_input_size;
491
492 bool early_fragment_tests;
493
494 /** Atomic counter offsets by binding */
495 unsigned atomic_counter_offsets[MAX_COMBINED_ATOMIC_BUFFERS];
496
497 bool allow_extension_directive_midshader;
498 };
499
500 # define YYLLOC_DEFAULT(Current, Rhs, N) \
501 do { \
502 if (N) \
503 { \
504 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
505 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
506 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
507 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
508 } \
509 else \
510 { \
511 (Current).first_line = (Current).last_line = \
512 YYRHSLOC(Rhs, 0).last_line; \
513 (Current).first_column = (Current).last_column = \
514 YYRHSLOC(Rhs, 0).last_column; \
515 } \
516 (Current).source = 0; \
517 } while (0)
518
519 /**
520 * Emit a warning to the shader log
521 *
522 * \sa _mesa_glsl_error
523 */
524 extern void _mesa_glsl_warning(const YYLTYPE *locp,
525 _mesa_glsl_parse_state *state,
526 const char *fmt, ...);
527
528 extern void _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state,
529 const char *string);
530
531 extern void _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state);
532
533 union YYSTYPE;
534 extern int _mesa_glsl_lexer_lex(union YYSTYPE *yylval, YYLTYPE *yylloc,
535 void *scanner);
536
537 extern int _mesa_glsl_parse(struct _mesa_glsl_parse_state *);
538
539 /**
540 * Process elements of the #extension directive
541 *
542 * \return
543 * If \c name and \c behavior are valid, \c true is returned. Otherwise
544 * \c false is returned.
545 */
546 extern bool _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
547 const char *behavior,
548 YYLTYPE *behavior_locp,
549 _mesa_glsl_parse_state *state);
550
551 #endif /* __cplusplus */
552
553
554 /*
555 * These definitions apply to C and C++
556 */
557 #ifdef __cplusplus
558 extern "C" {
559 #endif
560
561 /**
562 * Get the textual name of the specified shader stage (which is a
563 * gl_shader_stage).
564 */
565 extern const char *
566 _mesa_shader_stage_to_string(unsigned stage);
567
568 extern int glcpp_preprocess(void *ctx, const char **shader, char **info_log,
569 const struct gl_extensions *extensions, struct gl_context *gl_ctx);
570
571 extern void _mesa_destroy_shader_compiler(void);
572 extern void _mesa_destroy_shader_compiler_caches(void);
573
574 #ifdef __cplusplus
575 }
576 #endif
577
578
579 #endif /* GLSL_PARSER_EXTRAS_H */