Debian Codegraph @

thirdparty/spirv-cross/spirv_glsl.hpp

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