glsl: convert GS input primitive to use ast_type_qualifier
[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 has_explicit_attrib_location() const
139 {
140 return ARB_explicit_attrib_location_enable || is_version(330, 300);
141 }
142
143 bool has_uniform_buffer_objects() const
144 {
145 return ARB_uniform_buffer_object_enable || is_version(140, 300);
146 }
147
148 void process_version_directive(YYLTYPE *locp, int version,
149 const char *ident);
150
151 struct gl_context *const ctx;
152 void *scanner;
153 exec_list translation_unit;
154 glsl_symbol_table *symbols;
155
156 unsigned num_uniform_blocks;
157 unsigned uniform_block_array_size;
158 struct gl_uniform_block *uniform_blocks;
159
160 unsigned num_supported_versions;
161 struct {
162 unsigned ver;
163 bool es;
164 } supported_versions[12];
165
166 bool es_shader;
167 unsigned language_version;
168 gl_shader_stage stage;
169
170 /**
171 * Number of nested struct_specifier levels
172 *
173 * Outside a struct_specifer, this is zero.
174 */
175 unsigned struct_specifier_depth;
176
177 /**
178 * Default uniform layout qualifiers tracked during parsing.
179 * Currently affects uniform blocks and uniform buffer variables in
180 * those blocks.
181 */
182 struct ast_type_qualifier *default_uniform_qualifier;
183
184 /**
185 * True if a geometry shader input primitive type was specified using a
186 * layout directive.
187 *
188 * Note: this value is computed at ast_to_hir time rather than at parse
189 * time.
190 */
191 bool gs_input_prim_type_specified;
192
193 /** Input layout qualifiers from GLSL 1.50. (geometry shader controls)*/
194 struct ast_type_qualifier *in_qualifier;
195
196 /**
197 * True if a compute shader input local size was specified using a layout
198 * directive.
199 *
200 * Note: this value is computed at ast_to_hir time rather than at parse
201 * time.
202 */
203 bool cs_input_local_size_specified;
204
205 /**
206 * If cs_input_local_size_specified is true, the local size that was
207 * specified. Otherwise ignored.
208 */
209 unsigned cs_input_local_size[3];
210
211 /** Output layout qualifiers from GLSL 1.50. (geometry shader controls)*/
212 struct ast_type_qualifier *out_qualifier;
213
214 /**
215 * Printable list of GLSL versions supported by the current context
216 *
217 * \note
218 * This string should probably be generated per-context instead of per
219 * invokation of the compiler. This should be changed when the method of
220 * tracking supported GLSL versions changes.
221 */
222 const char *supported_version_string;
223
224 /**
225 * Implementation defined limits that affect built-in variables, etc.
226 *
227 * \sa struct gl_constants (in mtypes.h)
228 */
229 struct {
230 /* 1.10 */
231 unsigned MaxLights;
232 unsigned MaxClipPlanes;
233 unsigned MaxTextureUnits;
234 unsigned MaxTextureCoords;
235 unsigned MaxVertexAttribs;
236 unsigned MaxVertexUniformComponents;
237 unsigned MaxVertexTextureImageUnits;
238 unsigned MaxCombinedTextureImageUnits;
239 unsigned MaxTextureImageUnits;
240 unsigned MaxFragmentUniformComponents;
241
242 /* ARB_draw_buffers */
243 unsigned MaxDrawBuffers;
244
245 /* 3.00 ES */
246 int MinProgramTexelOffset;
247 int MaxProgramTexelOffset;
248
249 /* 1.50 */
250 unsigned MaxVertexOutputComponents;
251 unsigned MaxGeometryInputComponents;
252 unsigned MaxGeometryOutputComponents;
253 unsigned MaxFragmentInputComponents;
254 unsigned MaxGeometryTextureImageUnits;
255 unsigned MaxGeometryOutputVertices;
256 unsigned MaxGeometryTotalOutputComponents;
257 unsigned MaxGeometryUniformComponents;
258
259 /* ARB_shader_atomic_counters */
260 unsigned MaxVertexAtomicCounters;
261 unsigned MaxGeometryAtomicCounters;
262 unsigned MaxFragmentAtomicCounters;
263 unsigned MaxCombinedAtomicCounters;
264 unsigned MaxAtomicBufferBindings;
265
266 /* ARB_compute_shader */
267 unsigned MaxComputeWorkGroupCount[3];
268 unsigned MaxComputeWorkGroupSize[3];
269
270 /* ARB_shader_image_load_store */
271 unsigned MaxImageUnits;
272 unsigned MaxCombinedImageUnitsAndFragmentOutputs;
273 unsigned MaxImageSamples;
274 unsigned MaxVertexImageUniforms;
275 unsigned MaxGeometryImageUniforms;
276 unsigned MaxFragmentImageUniforms;
277 unsigned MaxCombinedImageUniforms;
278 } Const;
279
280 /**
281 * During AST to IR conversion, pointer to current IR function
282 *
283 * Will be \c NULL whenever the AST to IR conversion is not inside a
284 * function definition.
285 */
286 class ir_function_signature *current_function;
287
288 /**
289 * During AST to IR conversion, pointer to the toplevel IR
290 * instruction list being generated.
291 */
292 exec_list *toplevel_ir;
293
294 /** Have we found a return statement in this function? */
295 bool found_return;
296
297 /** Was there an error during compilation? */
298 bool error;
299
300 /**
301 * Are all shader inputs / outputs invariant?
302 *
303 * This is set when the 'STDGL invariant(all)' pragma is used.
304 */
305 bool all_invariant;
306
307 /** Loop or switch statement containing the current instructions. */
308 class ast_iteration_statement *loop_nesting_ast;
309
310 struct glsl_switch_state switch_state;
311
312 /** List of structures defined in user code. */
313 const glsl_type **user_structures;
314 unsigned num_user_structures;
315
316 char *info_log;
317
318 /**
319 * \name Enable bits for GLSL extensions
320 */
321 /*@{*/
322 bool ARB_arrays_of_arrays_enable;
323 bool ARB_arrays_of_arrays_warn;
324 bool ARB_draw_buffers_enable;
325 bool ARB_draw_buffers_warn;
326 bool ARB_draw_instanced_enable;
327 bool ARB_draw_instanced_warn;
328 bool ARB_explicit_attrib_location_enable;
329 bool ARB_explicit_attrib_location_warn;
330 bool ARB_fragment_coord_conventions_enable;
331 bool ARB_fragment_coord_conventions_warn;
332 bool ARB_texture_rectangle_enable;
333 bool ARB_texture_rectangle_warn;
334 bool ARB_texture_gather_enable;
335 bool ARB_texture_gather_warn;
336 bool EXT_texture_array_enable;
337 bool EXT_texture_array_warn;
338 bool ARB_shader_texture_lod_enable;
339 bool ARB_shader_texture_lod_warn;
340 bool ARB_shader_stencil_export_enable;
341 bool ARB_shader_stencil_export_warn;
342 bool AMD_conservative_depth_enable;
343 bool AMD_conservative_depth_warn;
344 bool ARB_conservative_depth_enable;
345 bool ARB_conservative_depth_warn;
346 bool AMD_shader_stencil_export_enable;
347 bool AMD_shader_stencil_export_warn;
348 bool OES_texture_3D_enable;
349 bool OES_texture_3D_warn;
350 bool OES_EGL_image_external_enable;
351 bool OES_EGL_image_external_warn;
352 bool ARB_shader_bit_encoding_enable;
353 bool ARB_shader_bit_encoding_warn;
354 bool ARB_uniform_buffer_object_enable;
355 bool ARB_uniform_buffer_object_warn;
356 bool OES_standard_derivatives_enable;
357 bool OES_standard_derivatives_warn;
358 bool ARB_texture_cube_map_array_enable;
359 bool ARB_texture_cube_map_array_warn;
360 bool ARB_shading_language_packing_enable;
361 bool ARB_shading_language_packing_warn;
362 bool ARB_texture_multisample_enable;
363 bool ARB_texture_multisample_warn;
364 bool ARB_texture_query_levels_enable;
365 bool ARB_texture_query_levels_warn;
366 bool ARB_texture_query_lod_enable;
367 bool ARB_texture_query_lod_warn;
368 bool ARB_gpu_shader5_enable;
369 bool ARB_gpu_shader5_warn;
370 bool AMD_vertex_shader_layer_enable;
371 bool AMD_vertex_shader_layer_warn;
372 bool ARB_shading_language_420pack_enable;
373 bool ARB_shading_language_420pack_warn;
374 bool ARB_sample_shading_enable;
375 bool ARB_sample_shading_warn;
376 bool EXT_shader_integer_mix_enable;
377 bool EXT_shader_integer_mix_warn;
378 bool ARB_shader_atomic_counters_enable;
379 bool ARB_shader_atomic_counters_warn;
380 bool AMD_shader_trinary_minmax_enable;
381 bool AMD_shader_trinary_minmax_warn;
382 bool ARB_viewport_array_enable;
383 bool ARB_viewport_array_warn;
384 bool ARB_compute_shader_enable;
385 bool ARB_compute_shader_warn;
386 bool ARB_shader_image_load_store_enable;
387 bool ARB_shader_image_load_store_warn;
388 /*@}*/
389
390 /** Extensions supported by the OpenGL implementation. */
391 const struct gl_extensions *extensions;
392
393 bool uses_builtin_functions;
394
395 /**
396 * For geometry shaders, size of the most recently seen input declaration
397 * that was a sized array, or 0 if no sized input array declarations have
398 * been seen.
399 *
400 * Unused for other shader types.
401 */
402 unsigned gs_input_size;
403
404 bool early_fragment_tests;
405
406 /** Atomic counter offsets by binding */
407 unsigned atomic_counter_offsets[MAX_COMBINED_ATOMIC_BUFFERS];
408 };
409
410 # define YYLLOC_DEFAULT(Current, Rhs, N) \
411 do { \
412 if (N) \
413 { \
414 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
415 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
416 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
417 (Current).last_column = YYRHSLOC(Rhs, N).last_column; \
418 } \
419 else \
420 { \
421 (Current).first_line = (Current).last_line = \
422 YYRHSLOC(Rhs, 0).last_line; \
423 (Current).first_column = (Current).last_column = \
424 YYRHSLOC(Rhs, 0).last_column; \
425 } \
426 (Current).source = 0; \
427 } while (0)
428
429 /**
430 * Emit a warning to the shader log
431 *
432 * \sa _mesa_glsl_error
433 */
434 extern void _mesa_glsl_warning(const YYLTYPE *locp,
435 _mesa_glsl_parse_state *state,
436 const char *fmt, ...);
437
438 extern void _mesa_glsl_lexer_ctor(struct _mesa_glsl_parse_state *state,
439 const char *string);
440
441 extern void _mesa_glsl_lexer_dtor(struct _mesa_glsl_parse_state *state);
442
443 union YYSTYPE;
444 extern int _mesa_glsl_lexer_lex(union YYSTYPE *yylval, YYLTYPE *yylloc,
445 void *scanner);
446
447 extern int _mesa_glsl_parse(struct _mesa_glsl_parse_state *);
448
449 /**
450 * Process elements of the #extension directive
451 *
452 * \return
453 * If \c name and \c behavior are valid, \c true is returned. Otherwise
454 * \c false is returned.
455 */
456 extern bool _mesa_glsl_process_extension(const char *name, YYLTYPE *name_locp,
457 const char *behavior,
458 YYLTYPE *behavior_locp,
459 _mesa_glsl_parse_state *state);
460
461 #endif /* __cplusplus */
462
463
464 /*
465 * These definitions apply to C and C++
466 */
467 #ifdef __cplusplus
468 extern "C" {
469 #endif
470
471 /**
472 * Get the textual name of the specified shader stage (which is a
473 * gl_shader_stage).
474 */
475 extern const char *
476 _mesa_shader_stage_to_string(unsigned stage);
477
478 extern int glcpp_preprocess(void *ctx, const char **shader, char **info_log,
479 const struct gl_extensions *extensions, struct gl_context *gl_ctx);
480
481 extern void _mesa_destroy_shader_compiler(void);
482 extern void _mesa_destroy_shader_compiler_caches(void);
483
484 #ifdef __cplusplus
485 }
486 #endif
487
488
489 #endif /* GLSL_PARSER_EXTRAS_H */