spirv_glsl.hpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /*
  2. * Copyright 2015-2021 Arm Limited
  3. * SPDX-License-Identifier: Apache-2.0 OR MIT
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. * At your option, you may choose to accept this material under either:
  19. * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or
  20. * 2. The MIT License, found at <http://opensource.org/licenses/MIT>.
  21. */
  22. #ifndef SPIRV_CROSS_GLSL_HPP
  23. #define SPIRV_CROSS_GLSL_HPP
  24. #include "GLSL.std.450.h"
  25. #include "spirv_cross.hpp"
  26. #include <unordered_map>
  27. #include <unordered_set>
  28. #include <utility>
  29. namespace SPIRV_CROSS_NAMESPACE
  30. {
  31. using namespace SPIRV_CROSS_SPV_HEADER_NAMESPACE;
  32. struct GlslConstantNameMapping;
  33. enum PlsFormat
  34. {
  35. PlsNone = 0,
  36. PlsR11FG11FB10F,
  37. PlsR32F,
  38. PlsRG16F,
  39. PlsRGB10A2,
  40. PlsRGBA8,
  41. PlsRG16,
  42. PlsRGBA8I,
  43. PlsRG16I,
  44. PlsRGB10A2UI,
  45. PlsRGBA8UI,
  46. PlsRG16UI,
  47. PlsR32UI
  48. };
  49. struct PlsRemap
  50. {
  51. uint32_t id;
  52. PlsFormat format;
  53. };
  54. enum AccessChainFlagBits
  55. {
  56. ACCESS_CHAIN_INDEX_IS_LITERAL_BIT = 1 << 0,
  57. ACCESS_CHAIN_CHAIN_ONLY_BIT = 1 << 1,
  58. ACCESS_CHAIN_PTR_CHAIN_BIT = 1 << 2,
  59. ACCESS_CHAIN_SKIP_REGISTER_EXPRESSION_READ_BIT = 1 << 3,
  60. ACCESS_CHAIN_LITERAL_MSB_FORCE_ID = 1 << 4,
  61. ACCESS_CHAIN_FLATTEN_ALL_MEMBERS_BIT = 1 << 5,
  62. ACCESS_CHAIN_FORCE_COMPOSITE_BIT = 1 << 6,
  63. ACCESS_CHAIN_PTR_CHAIN_POINTER_ARITH_BIT = 1 << 7,
  64. ACCESS_CHAIN_PTR_CHAIN_CAST_TO_SCALAR_BIT = 1 << 8
  65. };
  66. typedef uint32_t AccessChainFlags;
  67. class CompilerGLSL : public Compiler
  68. {
  69. public:
  70. struct Options
  71. {
  72. // The shading language version. Corresponds to #version $VALUE.
  73. uint32_t version = 450;
  74. // Emit the OpenGL ES shading language instead of desktop OpenGL.
  75. bool es = false;
  76. // Debug option to always emit temporary variables for all expressions.
  77. bool force_temporary = false;
  78. // Debug option, can be increased in an attempt to workaround SPIRV-Cross bugs temporarily.
  79. // If this limit has to be increased, it points to an implementation bug.
  80. // In certain scenarios, the maximum number of debug iterations may increase beyond this limit
  81. // as long as we can prove we're making certain kinds of forward progress.
  82. uint32_t force_recompile_max_debug_iterations = 3;
  83. // If true, Vulkan GLSL features are used instead of GL-compatible features.
  84. // Mostly useful for debugging SPIR-V files.
  85. bool vulkan_semantics = false;
  86. // If true, gl_PerVertex is explicitly redeclared in vertex, geometry and tessellation shaders.
  87. // The members of gl_PerVertex is determined by which built-ins are declared by the shader.
  88. // This option is ignored in ES versions, as redeclaration in ES is not required, and it depends on a different extension
  89. // (EXT_shader_io_blocks) which makes things a bit more fuzzy.
  90. bool separate_shader_objects = false;
  91. // Flattens multidimensional arrays, e.g. float foo[a][b][c] into single-dimensional arrays,
  92. // e.g. float foo[a * b * c].
  93. // This function does not change the actual SPIRType of any object.
  94. // Only the generated code, including declarations of interface variables are changed to be single array dimension.
  95. bool flatten_multidimensional_arrays = false;
  96. // For older desktop GLSL targets than version 420, the
  97. // GL_ARB_shading_language_420pack extensions is used to be able to support
  98. // layout(binding) on UBOs and samplers.
  99. // If disabled on older targets, binding decorations will be stripped.
  100. bool enable_420pack_extension = true;
  101. // In non-Vulkan GLSL, emit push constant blocks as UBOs rather than plain uniforms.
  102. bool emit_push_constant_as_uniform_buffer = false;
  103. // Always emit uniform blocks as plain uniforms, regardless of the GLSL version, even when UBOs are supported.
  104. // Does not apply to shader storage or push constant blocks.
  105. bool emit_uniform_buffer_as_plain_uniforms = false;
  106. // Emit OpLine directives if present in the module.
  107. // May not correspond exactly to original source, but should be a good approximation.
  108. bool emit_line_directives = false;
  109. // In cases where readonly/writeonly decoration are not used at all,
  110. // we try to deduce which qualifier(s) we should actually used, since actually emitting
  111. // read-write decoration is very rare, and older glslang/HLSL compilers tend to just emit readwrite as a matter of fact.
  112. // The default (true) is to enable automatic deduction for these cases, but if you trust the decorations set
  113. // by the SPIR-V, it's recommended to set this to false.
  114. bool enable_storage_image_qualifier_deduction = true;
  115. // On some targets (WebGPU), uninitialized variables are banned.
  116. // If this is enabled, all variables (temporaries, Private, Function)
  117. // which would otherwise be uninitialized will now be initialized to 0 instead.
  118. bool force_zero_initialized_variables = false;
  119. // In GLSL, force use of I/O block flattening, similar to
  120. // what happens on legacy GLSL targets for blocks and structs.
  121. bool force_flattened_io_blocks = false;
  122. // For opcodes where we have to perform explicit additional nan checks, very ugly code is generated.
  123. // If we opt-in, ignore these requirements.
  124. // In opcodes like NClamp/NMin/NMax and FP compare, ignore NaN behavior.
  125. // Use FClamp/FMin/FMax semantics for clamps and lets implementation choose ordered or unordered
  126. // compares.
  127. bool relax_nan_checks = false;
  128. // Loading row-major matrices from UBOs on older AMD Windows OpenGL drivers is problematic.
  129. // To load these types correctly, we must generate a wrapper. them in a dummy function which only purpose is to
  130. // ensure row_major decoration is actually respected.
  131. // This workaround may cause significant performance degeneration on some Android devices.
  132. bool enable_row_major_load_workaround = true;
  133. // If non-zero, controls layout(num_views = N) in; in GL_OVR_multiview2.
  134. uint32_t ovr_multiview_view_count = 0;
  135. enum Precision
  136. {
  137. DontCare,
  138. Lowp,
  139. Mediump,
  140. Highp
  141. };
  142. struct VertexOptions
  143. {
  144. // "Vertex-like shader" here is any shader stage that can write BuiltInPosition.
  145. // GLSL: In vertex-like shaders, rewrite [0, w] depth (Vulkan/D3D style) to [-w, w] depth (GL style).
  146. // MSL: In vertex-like shaders, rewrite [-w, w] depth (GL style) to [0, w] depth.
  147. // HLSL: In vertex-like shaders, rewrite [-w, w] depth (GL style) to [0, w] depth.
  148. bool fixup_clipspace = false;
  149. // In vertex-like shaders, inverts gl_Position.y or equivalent.
  150. bool flip_vert_y = false;
  151. // GLSL only, for HLSL version of this option, see CompilerHLSL.
  152. // If true, the backend will assume that InstanceIndex will need to apply
  153. // a base instance offset. Set to false if you know you will never use base instance
  154. // functionality as it might remove some internal uniforms.
  155. bool support_nonzero_base_instance = true;
  156. } vertex;
  157. struct FragmentOptions
  158. {
  159. // Add precision mediump float in ES targets when emitting GLES source.
  160. // Add precision highp int in ES targets when emitting GLES source.
  161. Precision default_float_precision = Mediump;
  162. Precision default_int_precision = Highp;
  163. } fragment;
  164. };
  165. void remap_pixel_local_storage(std::vector<PlsRemap> inputs, std::vector<PlsRemap> outputs)
  166. {
  167. pls_inputs = std::move(inputs);
  168. pls_outputs = std::move(outputs);
  169. remap_pls_variables();
  170. }
  171. // Redirect a subpassInput reading from input_attachment_index to instead load its value from
  172. // the color attachment at location = color_location. Requires ESSL.
  173. // If coherent, uses GL_EXT_shader_framebuffer_fetch, if not, uses noncoherent variant.
  174. void remap_ext_framebuffer_fetch(uint32_t input_attachment_index, uint32_t color_location, bool coherent);
  175. explicit CompilerGLSL(std::vector<uint32_t> spirv_)
  176. : Compiler(std::move(spirv_))
  177. {
  178. init();
  179. }
  180. CompilerGLSL(const uint32_t *ir_, size_t word_count)
  181. : Compiler(ir_, word_count)
  182. {
  183. init();
  184. }
  185. explicit CompilerGLSL(const ParsedIR &ir_)
  186. : Compiler(ir_)
  187. {
  188. init();
  189. }
  190. explicit CompilerGLSL(ParsedIR &&ir_)
  191. : Compiler(std::move(ir_))
  192. {
  193. init();
  194. }
  195. const Options &get_common_options() const
  196. {
  197. return options;
  198. }
  199. void set_common_options(const Options &opts)
  200. {
  201. options = opts;
  202. }
  203. std::string compile() override;
  204. // Returns the current string held in the conversion buffer. Useful for
  205. // capturing what has been converted so far when compile() throws an error.
  206. std::string get_partial_source();
  207. // Adds a line to be added right after #version in GLSL backend.
  208. // This is useful for enabling custom extensions which are outside the scope of SPIRV-Cross.
  209. // This can be combined with variable remapping.
  210. // A new-line will be added.
  211. //
  212. // While add_header_line() is a more generic way of adding arbitrary text to the header
  213. // of a GLSL file, require_extension() should be used when adding extensions since it will
  214. // avoid creating collisions with SPIRV-Cross generated extensions.
  215. //
  216. // Code added via add_header_line() is typically backend-specific.
  217. void add_header_line(const std::string &str);
  218. // Adds an extension which is required to run this shader, e.g.
  219. // require_extension("GL_KHR_my_extension");
  220. void require_extension(const std::string &ext);
  221. // Returns the list of required extensions. After compilation this will contains any other
  222. // extensions that the compiler used automatically, in addition to the user specified ones.
  223. const SmallVector<std::string> &get_required_extensions() const;
  224. // Legacy GLSL compatibility method.
  225. // Takes a uniform or push constant variable and flattens it into a (i|u)vec4 array[N]; array instead.
  226. // For this to work, all types in the block must be the same basic type, e.g. mixing vec2 and vec4 is fine, but
  227. // mixing int and float is not.
  228. // The name of the uniform array will be the same as the interface block name.
  229. void flatten_buffer_block(VariableID id);
  230. // After compilation, query if a variable ID was used as a depth resource.
  231. // This is meaningful for MSL since descriptor types depend on this knowledge.
  232. // Cases which return true:
  233. // - Images which are declared with depth = 1 image type.
  234. // - Samplers which are statically used at least once with Dref opcodes.
  235. // - Images which are statically used at least once with Dref opcodes.
  236. bool variable_is_depth_or_compare(VariableID id) const;
  237. // If a shader output is active in this stage, but inactive in a subsequent stage,
  238. // this can be signalled here. This can be used to work around certain cross-stage matching problems
  239. // which plagues MSL and HLSL in certain scenarios.
  240. // An output which matches one of these will not be emitted in stage output interfaces, but rather treated as a private
  241. // variable.
  242. // This option is only meaningful for MSL and HLSL, since GLSL matches by location directly.
  243. // Masking builtins only takes effect if the builtin in question is part of the stage output interface.
  244. void mask_stage_output_by_location(uint32_t location, uint32_t component);
  245. void mask_stage_output_by_builtin(BuiltIn builtin);
  246. // Allow to control how to format float literals in the output.
  247. // Set to "nullptr" to use the default "convert_to_string" function.
  248. // This handle is not owned by SPIRV-Cross and must remain valid until compile() has been called.
  249. void set_float_formatter(FloatFormatter *formatter)
  250. {
  251. float_formatter = formatter;
  252. }
  253. // Returns the macro name corresponding to constant id
  254. std::string constant_value_macro_name(uint32_t id) const;
  255. protected:
  256. struct ShaderSubgroupSupportHelper
  257. {
  258. // lower enum value = greater priority
  259. enum Candidate
  260. {
  261. KHR_shader_subgroup_ballot,
  262. KHR_shader_subgroup_basic,
  263. KHR_shader_subgroup_vote,
  264. KHR_shader_subgroup_arithmetic,
  265. NV_gpu_shader_5,
  266. NV_shader_thread_group,
  267. NV_shader_thread_shuffle,
  268. ARB_shader_ballot,
  269. ARB_shader_group_vote,
  270. AMD_gcn_shader,
  271. CandidateCount
  272. };
  273. static const char *get_extension_name(Candidate c);
  274. static SmallVector<std::string> get_extra_required_extension_names(Candidate c);
  275. static const char *get_extra_required_extension_predicate(Candidate c);
  276. enum Feature
  277. {
  278. SubgroupMask = 0,
  279. SubgroupSize = 1,
  280. SubgroupInvocationID = 2,
  281. SubgroupID = 3,
  282. NumSubgroups = 4,
  283. SubgroupBroadcast_First = 5,
  284. SubgroupBallotFindLSB_MSB = 6,
  285. SubgroupAll_Any_AllEqualBool = 7,
  286. SubgroupAllEqualT = 8,
  287. SubgroupElect = 9,
  288. SubgroupBarrier = 10,
  289. SubgroupMemBarrier = 11,
  290. SubgroupBallot = 12,
  291. SubgroupInverseBallot_InclBitCount_ExclBitCout = 13,
  292. SubgroupBallotBitExtract = 14,
  293. SubgroupBallotBitCount = 15,
  294. SubgroupArithmeticIAddReduce = 16,
  295. SubgroupArithmeticIAddExclusiveScan = 17,
  296. SubgroupArithmeticIAddInclusiveScan = 18,
  297. SubgroupArithmeticFAddReduce = 19,
  298. SubgroupArithmeticFAddExclusiveScan = 20,
  299. SubgroupArithmeticFAddInclusiveScan = 21,
  300. SubgroupArithmeticIMulReduce = 22,
  301. SubgroupArithmeticIMulExclusiveScan = 23,
  302. SubgroupArithmeticIMulInclusiveScan = 24,
  303. SubgroupArithmeticFMulReduce = 25,
  304. SubgroupArithmeticFMulExclusiveScan = 26,
  305. SubgroupArithmeticFMulInclusiveScan = 27,
  306. FeatureCount
  307. };
  308. using FeatureMask = uint32_t;
  309. static_assert(sizeof(FeatureMask) * 8u >= FeatureCount, "Mask type needs more bits.");
  310. using CandidateVector = SmallVector<Candidate, CandidateCount>;
  311. using FeatureVector = SmallVector<Feature>;
  312. static FeatureVector get_feature_dependencies(Feature feature);
  313. static FeatureMask get_feature_dependency_mask(Feature feature);
  314. static bool can_feature_be_implemented_without_extensions(Feature feature);
  315. static Candidate get_KHR_extension_for_feature(Feature feature);
  316. struct Result
  317. {
  318. Result();
  319. uint32_t weights[CandidateCount];
  320. };
  321. void request_feature(Feature feature);
  322. bool is_feature_requested(Feature feature) const;
  323. Result resolve() const;
  324. static CandidateVector get_candidates_for_feature(Feature ft, const Result &r);
  325. private:
  326. static CandidateVector get_candidates_for_feature(Feature ft);
  327. static FeatureMask build_mask(const SmallVector<Feature> &features);
  328. FeatureMask feature_mask = 0;
  329. };
  330. // TODO remove this function when all subgroup ops are supported (or make it always return true)
  331. static bool is_supported_subgroup_op_in_opengl(Op op, const uint32_t *ops);
  332. void reset(uint32_t iteration_count);
  333. void emit_function(SPIRFunction &func, const Bitset &return_flags);
  334. bool has_extension(const std::string &ext) const;
  335. void require_extension_internal(const std::string &ext);
  336. // Virtualize methods which need to be overridden by subclass targets like C++ and such.
  337. virtual void emit_function_prototype(SPIRFunction &func, const Bitset &return_flags);
  338. SPIRBlock *current_emitting_block = nullptr;
  339. SmallVector<SPIRBlock *> current_emitting_switch_stack;
  340. bool current_emitting_switch_fallthrough = false;
  341. virtual void emit_instruction(const Instruction &instr);
  342. struct TemporaryCopy
  343. {
  344. uint32_t dst_id;
  345. uint32_t src_id;
  346. };
  347. TemporaryCopy handle_instruction_precision(const Instruction &instr);
  348. void emit_block_instructions(SPIRBlock &block);
  349. void emit_block_instructions_with_masked_debug(SPIRBlock &block);
  350. // For relax_nan_checks.
  351. GLSLstd450 get_remapped_glsl_op(GLSLstd450 std450_op) const;
  352. Op get_remapped_spirv_op(Op op) const;
  353. virtual void emit_glsl_op(uint32_t result_type, uint32_t result_id, uint32_t op, const uint32_t *args,
  354. uint32_t count);
  355. virtual void emit_spv_amd_shader_ballot_op(uint32_t result_type, uint32_t result_id, uint32_t op,
  356. const uint32_t *args, uint32_t count);
  357. virtual void emit_spv_amd_shader_explicit_vertex_parameter_op(uint32_t result_type, uint32_t result_id, uint32_t op,
  358. const uint32_t *args, uint32_t count);
  359. virtual void emit_spv_amd_shader_trinary_minmax_op(uint32_t result_type, uint32_t result_id, uint32_t op,
  360. const uint32_t *args, uint32_t count);
  361. virtual void emit_spv_amd_gcn_shader_op(uint32_t result_type, uint32_t result_id, uint32_t op, const uint32_t *args,
  362. uint32_t count);
  363. void emit_non_semantic_shader_debug_info(uint32_t result_type, uint32_t result_id, uint32_t op,
  364. const uint32_t *args, uint32_t count);
  365. virtual void emit_header();
  366. void emit_line_directive(uint32_t file_id, uint32_t line_literal);
  367. void build_workgroup_size(SmallVector<std::string> &arguments, const SpecializationConstant &x,
  368. const SpecializationConstant &y, const SpecializationConstant &z);
  369. void request_subgroup_feature(ShaderSubgroupSupportHelper::Feature feature);
  370. virtual void emit_sampled_image_op(uint32_t result_type, uint32_t result_id, uint32_t image_id, uint32_t samp_id);
  371. virtual void emit_texture_op(const Instruction &i, bool sparse);
  372. virtual std::string to_texture_op(const Instruction &i, bool sparse, bool *forward,
  373. SmallVector<uint32_t> &inherited_expressions);
  374. virtual void emit_subgroup_op(const Instruction &i);
  375. virtual std::string type_to_glsl(const SPIRType &type, uint32_t id = 0);
  376. virtual std::string builtin_to_glsl(BuiltIn builtin, StorageClass storage);
  377. virtual void emit_struct_member(const SPIRType &type, uint32_t member_type_id, uint32_t index,
  378. const std::string &qualifier = "", uint32_t base_offset = 0);
  379. virtual void emit_struct_padding_target(const SPIRType &type);
  380. virtual std::string image_type_glsl(const SPIRType &type, uint32_t id = 0, bool member = false);
  381. std::string constant_expression(const SPIRConstant &c,
  382. bool inside_block_like_struct_scope = false,
  383. bool inside_struct_scope = false);
  384. virtual std::string constant_op_expression(const SPIRConstantOp &cop);
  385. virtual std::string constant_expression_vector(const SPIRConstant &c, uint32_t vector);
  386. virtual void emit_fixup();
  387. virtual std::string variable_decl(const SPIRType &type, const std::string &name, uint32_t id = 0);
  388. virtual bool variable_decl_is_remapped_storage(const SPIRVariable &var, StorageClass storage) const;
  389. virtual std::string to_func_call_arg(const SPIRFunction::Parameter &arg, uint32_t id);
  390. virtual void emit_workgroup_initialization(const SPIRVariable &var);
  391. struct TextureFunctionBaseArguments
  392. {
  393. // GCC 4.8 workarounds, it doesn't understand '{}' constructor here, use explicit default constructor.
  394. TextureFunctionBaseArguments() = default;
  395. VariableID img = 0;
  396. const SPIRType *imgtype = nullptr;
  397. bool is_fetch = false, is_gather = false, is_proj = false;
  398. };
  399. struct TextureFunctionNameArguments
  400. {
  401. // GCC 4.8 workarounds, it doesn't understand '{}' constructor here, use explicit default constructor.
  402. TextureFunctionNameArguments() = default;
  403. TextureFunctionBaseArguments base;
  404. bool has_array_offsets = false, has_offset = false, has_grad = false;
  405. bool has_dref = false, is_sparse_feedback = false, has_min_lod = false;
  406. uint32_t lod = 0;
  407. };
  408. virtual std::string to_function_name(const TextureFunctionNameArguments &args);
  409. struct TextureFunctionArguments
  410. {
  411. // GCC 4.8 workarounds, it doesn't understand '{}' constructor here, use explicit default constructor.
  412. TextureFunctionArguments() = default;
  413. TextureFunctionBaseArguments base;
  414. uint32_t coord = 0, coord_components = 0, dref = 0;
  415. uint32_t grad_x = 0, grad_y = 0, lod = 0, offset = 0;
  416. uint32_t bias = 0, component = 0, sample = 0, sparse_texel = 0, min_lod = 0;
  417. bool nonuniform_expression = false, has_array_offsets = false;
  418. };
  419. virtual std::string to_function_args(const TextureFunctionArguments &args, bool *p_forward);
  420. void emit_sparse_feedback_temporaries(uint32_t result_type_id, uint32_t id, uint32_t &feedback_id,
  421. uint32_t &texel_id);
  422. uint32_t get_sparse_feedback_texel_id(uint32_t id) const;
  423. virtual void emit_buffer_block(const SPIRVariable &type);
  424. virtual void emit_push_constant_block(const SPIRVariable &var);
  425. virtual void emit_uniform(const SPIRVariable &var);
  426. virtual std::string unpack_expression_type(std::string expr_str, const SPIRType &type, uint32_t physical_type_id,
  427. bool packed_type, bool row_major);
  428. virtual bool builtin_translates_to_nonarray(BuiltIn builtin) const;
  429. virtual bool is_user_type_structured(uint32_t id) const;
  430. void emit_copy_logical_type(uint32_t lhs_id, uint32_t lhs_type_id, uint32_t rhs_id, uint32_t rhs_type_id,
  431. SmallVector<uint32_t> chain);
  432. StringStream<> buffer;
  433. template <typename T>
  434. inline void statement_inner(T &&t)
  435. {
  436. buffer << std::forward<T>(t);
  437. statement_count++;
  438. }
  439. template <typename T, typename... Ts>
  440. inline void statement_inner(T &&t, Ts &&... ts)
  441. {
  442. buffer << std::forward<T>(t);
  443. statement_count++;
  444. statement_inner(std::forward<Ts>(ts)...);
  445. }
  446. template <typename... Ts>
  447. inline void statement(Ts &&... ts)
  448. {
  449. if (is_forcing_recompilation())
  450. {
  451. // Do not bother emitting code while force_recompile is active.
  452. // We will compile again.
  453. statement_count++;
  454. return;
  455. }
  456. if (redirect_statement)
  457. {
  458. redirect_statement->push_back(join(std::forward<Ts>(ts)...));
  459. statement_count++;
  460. }
  461. else
  462. {
  463. for (uint32_t i = 0; i < indent; i++)
  464. buffer << " ";
  465. statement_inner(std::forward<Ts>(ts)...);
  466. buffer << '\n';
  467. }
  468. }
  469. template <typename... Ts>
  470. inline void statement_no_indent(Ts &&... ts)
  471. {
  472. auto old_indent = indent;
  473. indent = 0;
  474. statement(std::forward<Ts>(ts)...);
  475. indent = old_indent;
  476. }
  477. // Used for implementing continue blocks where
  478. // we want to obtain a list of statements we can merge
  479. // on a single line separated by comma.
  480. SmallVector<std::string> *redirect_statement = nullptr;
  481. const SPIRBlock *current_continue_block = nullptr;
  482. bool block_temporary_hoisting = false;
  483. bool block_debug_directives = false;
  484. void begin_scope();
  485. void end_scope();
  486. void end_scope(const std::string &trailer);
  487. void end_scope_decl();
  488. void end_scope_decl(const std::string &decl);
  489. Options options;
  490. // Allow Metal to use the array<T> template to make arrays a value type
  491. virtual std::string type_to_array_glsl(const SPIRType &type, uint32_t variable_id);
  492. std::string to_array_size(const SPIRType &type, uint32_t index);
  493. uint32_t to_array_size_literal(const SPIRType &type, uint32_t index) const;
  494. uint32_t to_array_size_literal(const SPIRType &type) const;
  495. virtual std::string variable_decl(const SPIRVariable &variable); // Threadgroup arrays can't have a wrapper type
  496. std::string variable_decl_function_local(SPIRVariable &variable);
  497. void add_local_variable_name(uint32_t id);
  498. void add_resource_name(uint32_t id);
  499. void add_member_name(SPIRType &type, uint32_t name);
  500. void add_function_overload(const SPIRFunction &func);
  501. virtual bool is_non_native_row_major_matrix(uint32_t id);
  502. virtual bool member_is_non_native_row_major_matrix(const SPIRType &type, uint32_t index);
  503. bool member_is_remapped_physical_type(const SPIRType &type, uint32_t index) const;
  504. bool member_is_packed_physical_type(const SPIRType &type, uint32_t index) const;
  505. virtual std::string convert_row_major_matrix(std::string exp_str, const SPIRType &exp_type,
  506. uint32_t physical_type_id, bool is_packed,
  507. bool relaxed = false);
  508. std::unordered_set<std::string> local_variable_names;
  509. std::unordered_set<std::string> resource_names;
  510. std::unordered_set<std::string> block_input_names;
  511. std::unordered_set<std::string> block_output_names;
  512. std::unordered_set<std::string> block_ubo_names;
  513. std::unordered_set<std::string> block_ssbo_names;
  514. std::unordered_set<std::string> block_names; // A union of all block_*_names.
  515. std::unordered_map<std::string, std::unordered_set<uint64_t>> function_overloads;
  516. std::unordered_map<uint32_t, std::string> preserved_aliases;
  517. void preserve_alias_on_reset(uint32_t id);
  518. void reset_name_caches();
  519. bool processing_entry_point = false;
  520. // Can be overriden by subclass backends for trivial things which
  521. // shouldn't need polymorphism.
  522. struct BackendVariations
  523. {
  524. std::string discard_literal = "discard";
  525. std::string demote_literal = "demote";
  526. std::string null_pointer_literal = "";
  527. bool float_literal_suffix = false;
  528. bool double_literal_suffix = true;
  529. bool uint32_t_literal_suffix = true;
  530. bool long_long_literal_suffix = false;
  531. const char *basic_int_type = "int";
  532. const char *basic_uint_type = "uint";
  533. const char *basic_int8_type = "int8_t";
  534. const char *basic_uint8_type = "uint8_t";
  535. const char *basic_int16_type = "int16_t";
  536. const char *basic_uint16_type = "uint16_t";
  537. const char *int16_t_literal_suffix = "s";
  538. const char *uint16_t_literal_suffix = "us";
  539. const char *nonuniform_qualifier = "nonuniformEXT";
  540. const char *boolean_mix_function = "mix";
  541. const char *printf_function = "debugPrintfEXT";
  542. std::string constant_null_initializer = "";
  543. SPIRType::BaseType boolean_in_struct_remapped_type = SPIRType::Boolean;
  544. bool swizzle_is_function = false;
  545. bool shared_is_implied = false;
  546. bool unsized_array_supported = true;
  547. bool explicit_struct_type = false;
  548. bool use_initializer_list = false;
  549. bool use_typed_initializer_list = false;
  550. bool requires_matching_array_initializer = false;
  551. bool can_declare_struct_inline = true;
  552. bool can_declare_arrays_inline = true;
  553. bool native_row_major_matrix = true;
  554. bool use_constructor_splatting = true;
  555. bool allow_precision_qualifiers = false;
  556. bool can_swizzle_scalar = false;
  557. bool force_gl_in_out_block = false;
  558. bool force_merged_mesh_block = false;
  559. bool can_return_array = true;
  560. bool allow_truncated_access_chain = false;
  561. bool supports_extensions = false;
  562. bool supports_empty_struct = false;
  563. bool array_is_value_type = true;
  564. bool array_is_value_type_in_buffer_blocks = true;
  565. bool comparison_image_samples_scalar = false;
  566. bool native_pointers = false;
  567. bool support_small_type_sampling_result = false;
  568. bool support_case_fallthrough = true;
  569. bool use_array_constructor = false;
  570. bool needs_row_major_load_workaround = false;
  571. bool support_pointer_to_pointer = false;
  572. bool support_precise_qualifier = false;
  573. bool support_64bit_switch = false;
  574. bool workgroup_size_is_hidden = false;
  575. bool requires_relaxed_precision_analysis = false;
  576. bool implicit_c_integer_promotion_rules = false;
  577. bool supports_spec_constant_array_size = true;
  578. } backend;
  579. void emit_struct(SPIRType &type);
  580. void emit_resources();
  581. void emit_extension_workarounds(ExecutionModel model);
  582. void emit_subgroup_arithmetic_workaround(const std::string &func, Op op, GroupOperation group_op);
  583. void emit_polyfills(uint32_t polyfills, bool relaxed);
  584. void emit_buffer_block_native(const SPIRVariable &var);
  585. void emit_buffer_reference_block(uint32_t type_id, bool forward_declaration);
  586. void emit_buffer_block_legacy(const SPIRVariable &var);
  587. void emit_buffer_block_flattened(const SPIRVariable &type);
  588. void fixup_implicit_builtin_block_names(ExecutionModel model);
  589. void emit_declared_builtin_block(StorageClass storage, ExecutionModel model);
  590. bool should_force_emit_builtin_block(StorageClass storage);
  591. void emit_push_constant_block_vulkan(const SPIRVariable &var);
  592. void emit_push_constant_block_glsl(const SPIRVariable &var);
  593. void emit_interface_block(const SPIRVariable &type);
  594. void emit_flattened_io_block(const SPIRVariable &var, const char *qual);
  595. void emit_flattened_io_block_struct(const std::string &basename, const SPIRType &type, const char *qual,
  596. const SmallVector<uint32_t> &indices);
  597. void emit_flattened_io_block_member(const std::string &basename, const SPIRType &type, const char *qual,
  598. const SmallVector<uint32_t> &indices);
  599. void emit_block_chain(SPIRBlock &block);
  600. BlockID emit_block_chain_inner(SPIRBlock &block);
  601. void emit_block_chain_cleanup(SPIRBlock &block);
  602. void emit_hoisted_temporaries(SmallVector<std::pair<TypeID, ID>> &temporaries);
  603. int get_constant_mapping_to_workgroup_component(const SPIRConstant &constant) const;
  604. void emit_constant(const SPIRConstant &constant);
  605. void emit_specialization_constant_op(const SPIRConstantOp &constant);
  606. std::string emit_continue_block(uint32_t continue_block, bool follow_true_block, bool follow_false_block);
  607. bool attempt_emit_loop_header(SPIRBlock &block, SPIRBlock::Method method);
  608. void branch(BlockID from, BlockID to);
  609. void branch_to_continue(BlockID from, BlockID to);
  610. void branch(BlockID from, uint32_t cond, BlockID true_block, BlockID false_block);
  611. void flush_phi(BlockID from, BlockID to);
  612. void flush_variable_declaration(uint32_t id);
  613. void flush_undeclared_variables(SPIRBlock &block);
  614. void emit_variable_temporary_copies(const SPIRVariable &var);
  615. bool should_dereference(uint32_t id);
  616. bool should_dereference_caller_param(uint32_t id);
  617. bool should_forward(uint32_t id) const;
  618. bool should_suppress_usage_tracking(uint32_t id) const;
  619. void emit_mix_op(uint32_t result_type, uint32_t id, uint32_t left, uint32_t right, uint32_t lerp);
  620. void emit_nminmax_op(uint32_t result_type, uint32_t id, uint32_t op0, uint32_t op1, GLSLstd450 op);
  621. void emit_emulated_ahyper_op(uint32_t result_type, uint32_t result_id, uint32_t op0, GLSLstd450 op);
  622. bool to_trivial_mix_op(const SPIRType &type, std::string &op, uint32_t left, uint32_t right, uint32_t lerp);
  623. void emit_quaternary_func_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, uint32_t op2,
  624. uint32_t op3, const char *op);
  625. void emit_trinary_func_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, uint32_t op2,
  626. const char *op);
  627. void emit_binary_func_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, const char *op);
  628. void emit_atomic_func_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, const char *op);
  629. void emit_atomic_func_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, uint32_t op2, const char *op);
  630. void emit_unary_func_op_cast(uint32_t result_type, uint32_t result_id, uint32_t op0, const char *op,
  631. SPIRType::BaseType input_type, SPIRType::BaseType expected_result_type);
  632. void emit_binary_func_op_cast(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, const char *op,
  633. SPIRType::BaseType input_type, bool skip_cast_if_equal_type);
  634. void emit_binary_func_op_cast_clustered(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1,
  635. const char *op, SPIRType::BaseType input_type);
  636. void emit_trinary_func_op_cast(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, uint32_t op2,
  637. const char *op, SPIRType::BaseType input_type);
  638. void emit_trinary_func_op_bitextract(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1,
  639. uint32_t op2, const char *op, SPIRType::BaseType expected_result_type,
  640. SPIRType::BaseType input_type0, SPIRType::BaseType input_type1,
  641. SPIRType::BaseType input_type2);
  642. void emit_bitfield_insert_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, uint32_t op2,
  643. uint32_t op3, const char *op, SPIRType::BaseType offset_count_type);
  644. void emit_unary_func_op(uint32_t result_type, uint32_t result_id, uint32_t op0, const char *op);
  645. void emit_unrolled_unary_op(uint32_t result_type, uint32_t result_id, uint32_t operand, const char *op);
  646. void emit_binary_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, const char *op);
  647. void emit_unrolled_binary_op(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, const char *op,
  648. bool negate, SPIRType::BaseType expected_type);
  649. void emit_binary_op_cast(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1, const char *op,
  650. SPIRType::BaseType input_type, bool skip_cast_if_equal_type, bool implicit_integer_promotion);
  651. SPIRType binary_op_bitcast_helper(std::string &cast_op0, std::string &cast_op1, SPIRType::BaseType &input_type,
  652. uint32_t op0, uint32_t op1, bool skip_cast_if_equal_type);
  653. virtual bool emit_complex_bitcast(uint32_t result_type, uint32_t id, uint32_t op0);
  654. std::string to_ternary_expression(const SPIRType &result_type, uint32_t select, uint32_t true_value,
  655. uint32_t false_value);
  656. void emit_unary_op(uint32_t result_type, uint32_t result_id, uint32_t op0, const char *op);
  657. void emit_unary_op_cast(uint32_t result_type, uint32_t result_id, uint32_t op0, const char *op);
  658. virtual void emit_mesh_tasks(SPIRBlock &block);
  659. bool expression_is_forwarded(uint32_t id) const;
  660. bool expression_suppresses_usage_tracking(uint32_t id) const;
  661. bool expression_read_implies_multiple_reads(uint32_t id) const;
  662. SPIRExpression &emit_op(uint32_t result_type, uint32_t result_id, const std::string &rhs, bool forward_rhs,
  663. bool suppress_usage_tracking = false);
  664. void emit_transposed_op(uint32_t result_type, uint32_t result_id, const std::string &rhs, bool forward_rhs);
  665. void access_chain_internal_append_index(std::string &expr, uint32_t base, const SPIRType *type,
  666. AccessChainFlags flags, bool &access_chain_is_arrayed, uint32_t index);
  667. std::string access_chain_internal(uint32_t base, const uint32_t *indices, uint32_t count, AccessChainFlags flags,
  668. AccessChainMeta *meta);
  669. // Only meaningful on backends with physical pointer support ala MSL.
  670. // Relevant for PtrAccessChain / BDA.
  671. virtual uint32_t get_physical_type_stride(const SPIRType &type) const;
  672. StorageClass get_expression_effective_storage_class(uint32_t ptr);
  673. virtual bool access_chain_needs_stage_io_builtin_translation(uint32_t base);
  674. virtual bool check_physical_type_cast(std::string &expr, const SPIRType *type, uint32_t physical_type);
  675. virtual bool prepare_access_chain_for_scalar_access(std::string &expr, const SPIRType &type,
  676. StorageClass storage, bool &is_packed);
  677. std::string access_chain(uint32_t base, const uint32_t *indices, uint32_t count, const SPIRType &target_type,
  678. AccessChainMeta *meta = nullptr, bool ptr_chain = false);
  679. std::string flattened_access_chain(uint32_t base, const uint32_t *indices, uint32_t count,
  680. const SPIRType &target_type, uint32_t offset, uint32_t matrix_stride,
  681. uint32_t array_stride, bool need_transpose);
  682. std::string flattened_access_chain_struct(uint32_t base, const uint32_t *indices, uint32_t count,
  683. const SPIRType &target_type, uint32_t offset);
  684. std::string flattened_access_chain_matrix(uint32_t base, const uint32_t *indices, uint32_t count,
  685. const SPIRType &target_type, uint32_t offset, uint32_t matrix_stride,
  686. bool need_transpose);
  687. std::string flattened_access_chain_vector(uint32_t base, const uint32_t *indices, uint32_t count,
  688. const SPIRType &target_type, uint32_t offset, uint32_t matrix_stride,
  689. bool need_transpose);
  690. std::pair<std::string, uint32_t> flattened_access_chain_offset(const SPIRType &basetype, const uint32_t *indices,
  691. uint32_t count, uint32_t offset,
  692. uint32_t word_stride, bool *need_transpose = nullptr,
  693. uint32_t *matrix_stride = nullptr,
  694. uint32_t *array_stride = nullptr,
  695. bool ptr_chain = false);
  696. const char *index_to_swizzle(uint32_t index);
  697. std::string remap_swizzle(const SPIRType &result_type, uint32_t input_components, const std::string &expr);
  698. std::string declare_temporary(uint32_t type, uint32_t id);
  699. bool can_declare_inline_temporary(uint32_t id) const;
  700. void emit_uninitialized_temporary(uint32_t type, uint32_t id);
  701. SPIRExpression &emit_uninitialized_temporary_expression(uint32_t type, uint32_t id);
  702. virtual void append_global_func_args(const SPIRFunction &func, uint32_t index, SmallVector<std::string> &arglist);
  703. std::string to_non_uniform_aware_expression(uint32_t id);
  704. std::string to_atomic_ptr_expression(uint32_t id);
  705. std::string to_pretty_expression_if_int_constant(
  706. uint32_t id,
  707. const GlslConstantNameMapping *mapping_start, const GlslConstantNameMapping *mapping_end,
  708. bool register_expression_read = true);
  709. std::string to_expression(uint32_t id, bool register_expression_read = true);
  710. std::string to_composite_constructor_expression(const SPIRType &parent_type, uint32_t id, bool block_like_type);
  711. std::string to_rerolled_array_expression(const SPIRType &parent_type, const std::string &expr, const SPIRType &type);
  712. std::string to_enclosed_expression(uint32_t id, bool register_expression_read = true);
  713. std::string to_unpacked_expression(uint32_t id, bool register_expression_read = true);
  714. std::string to_unpacked_row_major_matrix_expression(uint32_t id);
  715. std::string to_enclosed_unpacked_expression(uint32_t id, bool register_expression_read = true);
  716. std::string to_dereferenced_expression(uint32_t id, bool register_expression_read = true);
  717. std::string to_pointer_expression(uint32_t id, bool register_expression_read = true);
  718. std::string to_enclosed_pointer_expression(uint32_t id, bool register_expression_read = true);
  719. std::string to_extract_component_expression(uint32_t id, uint32_t index);
  720. std::string to_extract_constant_composite_expression(uint32_t result_type, const SPIRConstant &c,
  721. const uint32_t *chain, uint32_t length);
  722. static bool needs_enclose_expression(const std::string &expr);
  723. std::string enclose_expression(const std::string &expr);
  724. std::string dereference_expression(const SPIRType &expression_type, const std::string &expr);
  725. std::string address_of_expression(const std::string &expr);
  726. void strip_enclosed_expression(std::string &expr);
  727. std::string to_member_name(const SPIRType &type, uint32_t index);
  728. virtual std::string to_member_reference(uint32_t base, const SPIRType &type, uint32_t index, bool ptr_chain_is_resolved);
  729. std::string to_multi_member_reference(const SPIRType &type, const SmallVector<uint32_t> &indices);
  730. std::string type_to_glsl_constructor(const SPIRType &type);
  731. std::string argument_decl(const SPIRFunction::Parameter &arg);
  732. virtual std::string to_qualifiers_glsl(uint32_t id);
  733. void fixup_io_block_patch_primitive_qualifiers(const SPIRVariable &var);
  734. void emit_output_variable_initializer(const SPIRVariable &var);
  735. std::string to_precision_qualifiers_glsl(uint32_t id);
  736. virtual const char *to_storage_qualifiers_glsl(const SPIRVariable &var);
  737. std::string flags_to_qualifiers_glsl(const SPIRType &type, uint32_t id, const Bitset &flags);
  738. const char *format_to_glsl(ImageFormat format);
  739. virtual std::string layout_for_member(const SPIRType &type, uint32_t index);
  740. virtual std::string to_interpolation_qualifiers(const Bitset &flags);
  741. std::string layout_for_variable(const SPIRVariable &variable);
  742. std::string to_combined_image_sampler(VariableID image_id, VariableID samp_id);
  743. virtual bool skip_argument(uint32_t id) const;
  744. virtual bool emit_array_copy(const char *expr, uint32_t lhs_id, uint32_t rhs_id,
  745. StorageClass lhs_storage, StorageClass rhs_storage);
  746. virtual void emit_block_hints(const SPIRBlock &block);
  747. virtual std::string to_initializer_expression(const SPIRVariable &var);
  748. virtual std::string to_zero_initialized_expression(uint32_t type_id);
  749. bool type_can_zero_initialize(const SPIRType &type) const;
  750. bool buffer_is_packing_standard(const SPIRType &type, BufferPackingStandard packing,
  751. uint32_t *failed_index = nullptr, uint32_t start_offset = 0,
  752. uint32_t end_offset = ~(0u));
  753. std::string buffer_to_packing_standard(const SPIRType &type,
  754. bool support_std430_without_scalar_layout,
  755. bool support_enhanced_layouts);
  756. uint32_t type_to_packed_base_size(const SPIRType &type, BufferPackingStandard packing);
  757. uint32_t type_to_packed_alignment(const SPIRType &type, const Bitset &flags, BufferPackingStandard packing);
  758. uint32_t type_to_packed_array_stride(const SPIRType &type, const Bitset &flags, BufferPackingStandard packing);
  759. uint32_t type_to_packed_size(const SPIRType &type, const Bitset &flags, BufferPackingStandard packing);
  760. uint32_t type_to_location_count(const SPIRType &type) const;
  761. std::string bitcast_glsl(const SPIRType &result_type, uint32_t arg);
  762. virtual std::string bitcast_glsl_op(const SPIRType &result_type, const SPIRType &argument_type);
  763. std::string bitcast_expression(SPIRType::BaseType target_type, uint32_t arg);
  764. std::string bitcast_expression(const SPIRType &target_type, SPIRType::BaseType expr_type, const std::string &expr);
  765. std::string build_composite_combiner(uint32_t result_type, const uint32_t *elems, uint32_t length);
  766. bool remove_duplicate_swizzle(std::string &op);
  767. bool remove_unity_swizzle(uint32_t base, std::string &op);
  768. // Can modify flags to remote readonly/writeonly if image type
  769. // and force recompile.
  770. bool check_atomic_image(uint32_t id);
  771. virtual void replace_illegal_names();
  772. void replace_illegal_names(const std::unordered_set<std::string> &keywords);
  773. virtual void emit_entry_point_declarations();
  774. void replace_fragment_output(SPIRVariable &var);
  775. void replace_fragment_outputs();
  776. std::string legacy_tex_op(const std::string &op, const SPIRType &imgtype, uint32_t id);
  777. void forward_relaxed_precision(uint32_t dst_id, const uint32_t *args, uint32_t length);
  778. void analyze_precision_requirements(uint32_t type_id, uint32_t dst_id, uint32_t *args, uint32_t length);
  779. Options::Precision analyze_expression_precision(const uint32_t *args, uint32_t length) const;
  780. uint32_t indent = 0;
  781. std::unordered_set<uint32_t> emitted_functions;
  782. // Ensure that we declare phi-variable copies even if the original declaration isn't deferred
  783. std::unordered_set<uint32_t> flushed_phi_variables;
  784. std::unordered_set<uint32_t> flattened_buffer_blocks;
  785. std::unordered_map<uint32_t, bool> flattened_structs;
  786. ShaderSubgroupSupportHelper shader_subgroup_supporter;
  787. std::string load_flattened_struct(const std::string &basename, const SPIRType &type);
  788. std::string to_flattened_struct_member(const std::string &basename, const SPIRType &type, uint32_t index);
  789. void store_flattened_struct(uint32_t lhs_id, uint32_t value);
  790. void store_flattened_struct(const std::string &basename, uint32_t rhs, const SPIRType &type,
  791. const SmallVector<uint32_t> &indices);
  792. std::string to_flattened_access_chain_expression(uint32_t id);
  793. // Usage tracking. If a temporary is used more than once, use the temporary instead to
  794. // avoid AST explosion when SPIRV is generated with pure SSA and doesn't write stuff to variables.
  795. std::unordered_map<uint32_t, uint32_t> expression_usage_counts;
  796. void track_expression_read(uint32_t id);
  797. SmallVector<std::string> forced_extensions;
  798. SmallVector<std::string> header_lines;
  799. // Used when expressions emit extra opcodes with their own unique IDs,
  800. // and we need to reuse the IDs across recompilation loops.
  801. // Currently used by NMin/Max/Clamp implementations.
  802. std::unordered_map<uint32_t, uint32_t> extra_sub_expressions;
  803. SmallVector<TypeID> workaround_ubo_load_overload_types;
  804. void request_workaround_wrapper_overload(TypeID id);
  805. void rewrite_load_for_wrapped_row_major(std::string &expr, TypeID loaded_type, ID ptr);
  806. uint32_t statement_count = 0;
  807. inline bool is_legacy() const
  808. {
  809. return (options.es && options.version < 300) || (!options.es && options.version < 130);
  810. }
  811. inline bool is_legacy_es() const
  812. {
  813. return options.es && options.version < 300;
  814. }
  815. inline bool is_legacy_desktop() const
  816. {
  817. return !options.es && options.version < 130;
  818. }
  819. enum Polyfill : uint32_t
  820. {
  821. PolyfillTranspose2x2 = 1 << 0,
  822. PolyfillTranspose3x3 = 1 << 1,
  823. PolyfillTranspose4x4 = 1 << 2,
  824. PolyfillDeterminant2x2 = 1 << 3,
  825. PolyfillDeterminant3x3 = 1 << 4,
  826. PolyfillDeterminant4x4 = 1 << 5,
  827. PolyfillMatrixInverse2x2 = 1 << 6,
  828. PolyfillMatrixInverse3x3 = 1 << 7,
  829. PolyfillMatrixInverse4x4 = 1 << 8,
  830. PolyfillNMin16 = 1 << 9,
  831. PolyfillNMin32 = 1 << 10,
  832. PolyfillNMin64 = 1 << 11,
  833. PolyfillNMax16 = 1 << 12,
  834. PolyfillNMax32 = 1 << 13,
  835. PolyfillNMax64 = 1 << 14,
  836. PolyfillNClamp16 = 1 << 15,
  837. PolyfillNClamp32 = 1 << 16,
  838. PolyfillNClamp64 = 1 << 17,
  839. };
  840. uint32_t required_polyfills = 0;
  841. uint32_t required_polyfills_relaxed = 0;
  842. void require_polyfill(Polyfill polyfill, bool relaxed);
  843. bool ray_tracing_is_khr = false;
  844. bool barycentric_is_nv = false;
  845. void ray_tracing_khr_fixup_locations();
  846. bool args_will_forward(uint32_t id, const uint32_t *args, uint32_t num_args, bool pure);
  847. void register_call_out_argument(uint32_t id);
  848. void register_impure_function_call();
  849. void register_control_dependent_expression(uint32_t expr);
  850. // GL_EXT_shader_pixel_local_storage support.
  851. std::vector<PlsRemap> pls_inputs;
  852. std::vector<PlsRemap> pls_outputs;
  853. std::string pls_decl(const PlsRemap &variable);
  854. const char *to_pls_qualifiers_glsl(const SPIRVariable &variable);
  855. void emit_pls();
  856. void remap_pls_variables();
  857. // GL_EXT_shader_framebuffer_fetch support.
  858. std::vector<std::pair<uint32_t, uint32_t>> subpass_to_framebuffer_fetch_attachment;
  859. std::vector<std::pair<uint32_t, bool>> inout_color_attachments;
  860. bool location_is_framebuffer_fetch(uint32_t location) const;
  861. bool location_is_non_coherent_framebuffer_fetch(uint32_t location) const;
  862. bool subpass_input_is_framebuffer_fetch(uint32_t id) const;
  863. void emit_inout_fragment_outputs_copy_to_subpass_inputs();
  864. const SPIRVariable *find_subpass_input_by_attachment_index(uint32_t index) const;
  865. const SPIRVariable *find_color_output_by_location(uint32_t location) const;
  866. // A variant which takes two sets of name. The secondary is only used to verify there are no collisions,
  867. // but the set is not updated when we have found a new name.
  868. // Used primarily when adding block interface names.
  869. void add_variable(std::unordered_set<std::string> &variables_primary,
  870. const std::unordered_set<std::string> &variables_secondary, std::string &name);
  871. void check_function_call_constraints(const uint32_t *args, uint32_t length);
  872. void handle_invalid_expression(uint32_t id);
  873. void force_temporary_and_recompile(uint32_t id);
  874. void find_static_extensions();
  875. uint32_t consume_temporary_in_precision_context(uint32_t type_id, uint32_t id, Options::Precision precision);
  876. std::unordered_map<uint32_t, uint32_t> temporary_to_mirror_precision_alias;
  877. std::unordered_set<uint32_t> composite_insert_overwritten;
  878. std::unordered_set<uint32_t> block_composite_insert_overwrite;
  879. std::string emit_for_loop_initializers(const SPIRBlock &block);
  880. void emit_while_loop_initializers(const SPIRBlock &block);
  881. bool for_loop_initializers_are_same_type(const SPIRBlock &block);
  882. bool optimize_read_modify_write(const SPIRType &type, const std::string &lhs, const std::string &rhs);
  883. void fixup_image_load_store_access();
  884. bool type_is_empty(const SPIRType &type);
  885. bool can_use_io_location(StorageClass storage, bool block);
  886. const Instruction *get_next_instruction_in_block(const Instruction &instr);
  887. static uint32_t mask_relevant_memory_semantics(uint32_t semantics);
  888. std::string convert_floate4m3_to_string(const SPIRConstant &value, uint32_t col, uint32_t row);
  889. std::string convert_floate5m2_to_string(const SPIRConstant &value, uint32_t col, uint32_t row);
  890. std::string convert_half_to_string(const SPIRConstant &value, uint32_t col, uint32_t row);
  891. std::string convert_float_to_string(const SPIRConstant &value, uint32_t col, uint32_t row);
  892. std::string convert_double_to_string(const SPIRConstant &value, uint32_t col, uint32_t row);
  893. std::string convert_separate_image_to_expression(uint32_t id);
  894. // Builtins in GLSL are always specific signedness, but the SPIR-V can declare them
  895. // as either unsigned or signed.
  896. // Sometimes we will need to automatically perform casts on load and store to make this work.
  897. virtual SPIRType::BaseType get_builtin_basetype(BuiltIn builtin, SPIRType::BaseType default_type);
  898. virtual void cast_to_variable_store(uint32_t target_id, std::string &expr, const SPIRType &expr_type);
  899. virtual void cast_from_variable_load(uint32_t source_id, std::string &expr, const SPIRType &expr_type);
  900. void unroll_array_from_complex_load(uint32_t target_id, uint32_t source_id, std::string &expr);
  901. bool unroll_array_to_complex_store(uint32_t target_id, uint32_t source_id);
  902. void convert_non_uniform_expression(std::string &expr, uint32_t ptr_id);
  903. void handle_store_to_invariant_variable(uint32_t store_id, uint32_t value_id);
  904. void disallow_forwarding_in_expression_chain(const SPIRExpression &expr);
  905. bool expression_is_constant_null(uint32_t id) const;
  906. bool expression_is_non_value_type_array(uint32_t ptr);
  907. virtual void emit_store_statement(uint32_t lhs_expression, uint32_t rhs_expression);
  908. uint32_t get_integer_width_for_instruction(const Instruction &instr) const;
  909. uint32_t get_integer_width_for_glsl_instruction(GLSLstd450 op, const uint32_t *arguments, uint32_t length) const;
  910. bool variable_is_lut(const SPIRVariable &var) const;
  911. char current_locale_radix_character = '.';
  912. void fixup_type_alias();
  913. void reorder_type_alias();
  914. void fixup_anonymous_struct_names();
  915. void fixup_anonymous_struct_names(std::unordered_set<uint32_t> &visited, const SPIRType &type);
  916. static const char *vector_swizzle(int vecsize, int index);
  917. bool is_stage_output_location_masked(uint32_t location, uint32_t component) const;
  918. bool is_stage_output_builtin_masked(BuiltIn builtin) const;
  919. bool is_stage_output_variable_masked(const SPIRVariable &var) const;
  920. bool is_stage_output_block_member_masked(const SPIRVariable &var, uint32_t index, bool strip_array) const;
  921. bool is_per_primitive_variable(const SPIRVariable &var) const;
  922. uint32_t get_accumulated_member_location(const SPIRVariable &var, uint32_t mbr_idx, bool strip_array) const;
  923. uint32_t get_declared_member_location(const SPIRVariable &var, uint32_t mbr_idx, bool strip_array) const;
  924. std::unordered_set<LocationComponentPair, InternalHasher> masked_output_locations;
  925. std::unordered_set<uint32_t> masked_output_builtins;
  926. FloatFormatter *float_formatter = nullptr;
  927. std::string format_float(float value) const;
  928. std::string format_double(double value) const;
  929. uint32_t get_fp_fast_math_flags_for_op(uint32_t result_type, uint32_t id) const;
  930. bool has_legacy_nocontract(uint32_t result_type, uint32_t id) const;
  931. private:
  932. void init();
  933. SmallVector<ConstantID> get_composite_constant_ids(ConstantID const_id);
  934. void fill_composite_constant(SPIRConstant &constant, TypeID type_id, const SmallVector<ConstantID> &initializers);
  935. void set_composite_constant(ConstantID const_id, TypeID type_id, const SmallVector<ConstantID> &initializers);
  936. TypeID get_composite_member_type(TypeID type_id, uint32_t member_idx);
  937. std::unordered_map<uint32_t, SmallVector<ConstantID>> const_composite_insert_ids;
  938. };
  939. } // namespace SPIRV_CROSS_NAMESPACE
  940. #endif