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