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