Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glsl_parser_extras.h 18KB

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