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