| 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_HPP |
| 25 | #define SPIRV_CROSS_HPP |
| 26 | |
| 27 | #ifndef SPV_ENABLE_UTILITY_CODE |
| 28 | #define SPV_ENABLE_UTILITY_CODE |
| 29 | #endif |
| 30 | #include "spirv.hpp" |
| 31 | #include "spirv_cfg.hpp" |
| 32 | #include "spirv_cross_parsed_ir.hpp" |
| 33 | |
| 34 | namespace SPIRV_CROSS_NAMESPACE |
| 35 | { |
| 36 | struct Resource |
| 37 | { |
| 38 | // Resources are identified with their SPIR-V ID. |
| 39 | // This is the ID of the OpVariable. |
| 40 | ID id; |
| 41 | |
| 42 | // The type ID of the variable which includes arrays and all type modifications. |
| 43 | // This type ID is not suitable for parsing OpMemberDecoration of a struct and other decorations in general |
| 44 | // since these modifications typically happen on the base_type_id. |
| 45 | TypeID type_id; |
| 46 | |
| 47 | // The base type of the declared resource. |
| 48 | // This type is the base type which ignores pointers and arrays of the type_id. |
| 49 | // This is mostly useful to parse decorations of the underlying type. |
| 50 | // base_type_id can also be obtained with get_type(get_type(type_id).self). |
| 51 | TypeID base_type_id; |
| 52 | |
| 53 | // The declared name (OpName) of the resource. |
| 54 | // For Buffer blocks, the name actually reflects the externally |
| 55 | // visible Block name. |
| 56 | // |
| 57 | // This name can be retrieved again by using either |
| 58 | // get_name(id) or get_name(base_type_id) depending if it's a buffer block or not. |
| 59 | // |
| 60 | // This name can be an empty string in which case get_fallback_name(id) can be |
| 61 | // used which obtains a suitable fallback identifier for an ID. |
| 62 | std::string name; |
| 63 | }; |
| 64 | |
| 65 | struct BuiltInResource |
| 66 | { |
| 67 | // This is mostly here to support reflection of builtins such as Position/PointSize/CullDistance/ClipDistance. |
| 68 | // This needs to be different from Resource since we can collect builtins from blocks. |
| 69 | // A builtin present here does not necessarily mean it's considered an active builtin, |
| 70 | // since variable ID "activeness" is only tracked on OpVariable level, not Block members. |
| 71 | // For that, update_active_builtins() -> has_active_builtin() can be used to further refine the reflection. |
| 72 | spv::BuiltIn builtin; |
| 73 | |
| 74 | // This is the actual value type of the builtin. |
| 75 | // Typically float4, float, array<float, N> for the gl_PerVertex builtins. |
| 76 | // If the builtin is a control point, the control point array type will be stripped away here as appropriate. |
| 77 | TypeID value_type_id; |
| 78 | |
| 79 | // This refers to the base resource which contains the builtin. |
| 80 | // If resource is a Block, it can hold multiple builtins, or it might not be a block. |
| 81 | // For advanced reflection scenarios, all information in builtin/value_type_id can be deduced, |
| 82 | // it's just more convenient this way. |
| 83 | Resource resource; |
| 84 | }; |
| 85 | |
| 86 | struct ShaderResources |
| 87 | { |
| 88 | SmallVector<Resource> uniform_buffers; |
| 89 | SmallVector<Resource> storage_buffers; |
| 90 | SmallVector<Resource> stage_inputs; |
| 91 | SmallVector<Resource> stage_outputs; |
| 92 | SmallVector<Resource> subpass_inputs; |
| 93 | SmallVector<Resource> storage_images; |
| 94 | SmallVector<Resource> sampled_images; |
| 95 | SmallVector<Resource> atomic_counters; |
| 96 | SmallVector<Resource> acceleration_structures; |
| 97 | SmallVector<Resource> gl_plain_uniforms; |
| 98 | |
| 99 | // There can only be one push constant block, |
| 100 | // but keep the vector in case this restriction is lifted in the future. |
| 101 | SmallVector<Resource> push_constant_buffers; |
| 102 | |
| 103 | SmallVector<Resource> shader_record_buffers; |
| 104 | |
| 105 | // For Vulkan GLSL and HLSL source, |
| 106 | // these correspond to separate texture2D and samplers respectively. |
| 107 | SmallVector<Resource> separate_images; |
| 108 | SmallVector<Resource> separate_samplers; |
| 109 | |
| 110 | SmallVector<BuiltInResource> builtin_inputs; |
| 111 | SmallVector<BuiltInResource> builtin_outputs; |
| 112 | }; |
| 113 | |
| 114 | struct CombinedImageSampler |
| 115 | { |
| 116 | // The ID of the sampler2D variable. |
| 117 | VariableID combined_id; |
| 118 | // The ID of the texture2D variable. |
| 119 | VariableID image_id; |
| 120 | // The ID of the sampler variable. |
| 121 | VariableID sampler_id; |
| 122 | }; |
| 123 | |
| 124 | struct SpecializationConstant |
| 125 | { |
| 126 | // The ID of the specialization constant. |
| 127 | ConstantID id; |
| 128 | // The constant ID of the constant, used in Vulkan during pipeline creation. |
| 129 | uint32_t constant_id; |
| 130 | }; |
| 131 | |
| 132 | struct BufferRange |
| 133 | { |
| 134 | unsigned index; |
| 135 | size_t offset; |
| 136 | size_t range; |
| 137 | }; |
| 138 | |
| 139 | enum BufferPackingStandard |
| 140 | { |
| 141 | BufferPackingStd140, |
| 142 | BufferPackingStd430, |
| 143 | BufferPackingStd140EnhancedLayout, |
| 144 | BufferPackingStd430EnhancedLayout, |
| 145 | BufferPackingHLSLCbuffer, |
| 146 | BufferPackingHLSLCbufferPackOffset, |
| 147 | BufferPackingScalar, |
| 148 | BufferPackingScalarEnhancedLayout |
| 149 | }; |
| 150 | |
| 151 | struct EntryPoint |
| 152 | { |
| 153 | std::string name; |
| 154 | spv::ExecutionModel execution_model; |
| 155 | }; |
| 156 | |
| 157 | class Compiler |
| 158 | { |
| 159 | public: |
| 160 | friend class CFG; |
| 161 | friend class DominatorBuilder; |
| 162 | |
| 163 | // The constructor takes a buffer of SPIR-V words and parses it. |
| 164 | // It will create its own parser, parse the SPIR-V and move the parsed IR |
| 165 | // as if you had called the constructors taking ParsedIR directly. |
| 166 | explicit Compiler(std::vector<uint32_t> ir); |
| 167 | Compiler(const uint32_t *ir, size_t word_count); |
| 168 | |
| 169 | // This is more modular. We can also consume a ParsedIR structure directly, either as a move, or copy. |
| 170 | // With copy, we can reuse the same parsed IR for multiple Compiler instances. |
| 171 | explicit Compiler(const ParsedIR &ir); |
| 172 | explicit Compiler(ParsedIR &&ir); |
| 173 | |
| 174 | virtual ~Compiler() = default; |
| 175 | |
| 176 | // After parsing, API users can modify the SPIR-V via reflection and call this |
| 177 | // to disassemble the SPIR-V into the desired langauage. |
| 178 | // Sub-classes actually implement this. |
| 179 | virtual std::string compile(); |
| 180 | |
| 181 | // Gets the identifier (OpName) of an ID. If not defined, an empty string will be returned. |
| 182 | const std::string &get_name(ID id) const; |
| 183 | |
| 184 | // Applies a decoration to an ID. Effectively injects OpDecorate. |
| 185 | void set_decoration(ID id, spv::Decoration decoration, uint32_t argument = 0); |
| 186 | void set_decoration_string(ID id, spv::Decoration decoration, const std::string &argument); |
| 187 | |
| 188 | // Overrides the identifier OpName of an ID. |
| 189 | // Identifiers beginning with underscores or identifiers which contain double underscores |
| 190 | // are reserved by the implementation. |
| 191 | void set_name(ID id, const std::string &name); |
| 192 | |
| 193 | // Gets a bitmask for the decorations which are applied to ID. |
| 194 | // I.e. (1ull << spv::DecorationFoo) | (1ull << spv::DecorationBar) |
| 195 | const Bitset &get_decoration_bitset(ID id) const; |
| 196 | |
| 197 | // Returns whether the decoration has been applied to the ID. |
| 198 | bool has_decoration(ID id, spv::Decoration decoration) const; |
| 199 | |
| 200 | // Gets the value for decorations which take arguments. |
| 201 | // If the decoration is a boolean (i.e. spv::DecorationNonWritable), |
| 202 | // 1 will be returned. |
| 203 | // If decoration doesn't exist or decoration is not recognized, |
| 204 | // 0 will be returned. |
| 205 | uint32_t get_decoration(ID id, spv::Decoration decoration) const; |
| 206 | const std::string &get_decoration_string(ID id, spv::Decoration decoration) const; |
| 207 | |
| 208 | // Removes the decoration for an ID. |
| 209 | void unset_decoration(ID id, spv::Decoration decoration); |
| 210 | |
| 211 | // Gets the SPIR-V type associated with ID. |
| 212 | // Mostly used with Resource::type_id and Resource::base_type_id to parse the underlying type of a resource. |
| 213 | const SPIRType &get_type(TypeID id) const; |
| 214 | |
| 215 | // Gets the SPIR-V type of a variable. |
| 216 | const SPIRType &get_type_from_variable(VariableID id) const; |
| 217 | |
| 218 | // Gets the underlying storage class for an OpVariable. |
| 219 | spv::StorageClass get_storage_class(VariableID id) const; |
| 220 | |
| 221 | // If get_name() is an empty string, get the fallback name which will be used |
| 222 | // instead in the disassembled source. |
| 223 | virtual const std::string get_fallback_name(ID id) const; |
| 224 | |
| 225 | // If get_name() of a Block struct is an empty string, get the fallback name. |
| 226 | // This needs to be per-variable as multiple variables can use the same block type. |
| 227 | virtual const std::string get_block_fallback_name(VariableID id) const; |
| 228 | |
| 229 | // Given an OpTypeStruct in ID, obtain the identifier for member number "index". |
| 230 | // This may be an empty string. |
| 231 | const std::string &get_member_name(TypeID id, uint32_t index) const; |
| 232 | |
| 233 | // Given an OpTypeStruct in ID, obtain the OpMemberDecoration for member number "index". |
| 234 | uint32_t get_member_decoration(TypeID id, uint32_t index, spv::Decoration decoration) const; |
| 235 | const std::string &get_member_decoration_string(TypeID id, uint32_t index, spv::Decoration decoration) const; |
| 236 | |
| 237 | // Sets the member identifier for OpTypeStruct ID, member number "index". |
| 238 | void set_member_name(TypeID id, uint32_t index, const std::string &name); |
| 239 | |
| 240 | // Returns the qualified member identifier for OpTypeStruct ID, member number "index", |
| 241 | // or an empty string if no qualified alias exists |
| 242 | const std::string &get_member_qualified_name(TypeID type_id, uint32_t index) const; |
| 243 | |
| 244 | // Gets the decoration mask for a member of a struct, similar to get_decoration_mask. |
| 245 | const Bitset &get_member_decoration_bitset(TypeID id, uint32_t index) const; |
| 246 | |
| 247 | // Returns whether the decoration has been applied to a member of a struct. |
| 248 | bool has_member_decoration(TypeID id, uint32_t index, spv::Decoration decoration) const; |
| 249 | |
| 250 | // Similar to set_decoration, but for struct members. |
| 251 | void set_member_decoration(TypeID id, uint32_t index, spv::Decoration decoration, uint32_t argument = 0); |
| 252 | void set_member_decoration_string(TypeID id, uint32_t index, spv::Decoration decoration, |
| 253 | const std::string &argument); |
| 254 | |
| 255 | // Unsets a member decoration, similar to unset_decoration. |
| 256 | void unset_member_decoration(TypeID id, uint32_t index, spv::Decoration decoration); |
| 257 | |
| 258 | // Gets the fallback name for a member, similar to get_fallback_name. |
| 259 | virtual const std::string get_fallback_member_name(uint32_t index) const |
| 260 | { |
| 261 | return join(ts: "_" , ts&: index); |
| 262 | } |
| 263 | |
| 264 | // Returns a vector of which members of a struct are potentially in use by a |
| 265 | // SPIR-V shader. The granularity of this analysis is per-member of a struct. |
| 266 | // This can be used for Buffer (UBO), BufferBlock/StorageBuffer (SSBO) and PushConstant blocks. |
| 267 | // ID is the Resource::id obtained from get_shader_resources(). |
| 268 | SmallVector<BufferRange> get_active_buffer_ranges(VariableID id) const; |
| 269 | |
| 270 | // Returns the effective size of a buffer block. |
| 271 | size_t get_declared_struct_size(const SPIRType &struct_type) const; |
| 272 | |
| 273 | // Returns the effective size of a buffer block, with a given array size |
| 274 | // for a runtime array. |
| 275 | // SSBOs are typically declared as runtime arrays. get_declared_struct_size() will return 0 for the size. |
| 276 | // This is not very helpful for applications which might need to know the array stride of its last member. |
| 277 | // This can be done through the API, but it is not very intuitive how to accomplish this, so here we provide a helper function |
| 278 | // to query the size of the buffer, assuming that the last member has a certain size. |
| 279 | // If the buffer does not contain a runtime array, array_size is ignored, and the function will behave as |
| 280 | // get_declared_struct_size(). |
| 281 | // To get the array stride of the last member, something like: |
| 282 | // get_declared_struct_size_runtime_array(type, 1) - get_declared_struct_size_runtime_array(type, 0) will work. |
| 283 | size_t get_declared_struct_size_runtime_array(const SPIRType &struct_type, size_t array_size) const; |
| 284 | |
| 285 | // Returns the effective size of a buffer block struct member. |
| 286 | size_t get_declared_struct_member_size(const SPIRType &struct_type, uint32_t index) const; |
| 287 | |
| 288 | // Returns a set of all global variables which are statically accessed |
| 289 | // by the control flow graph from the current entry point. |
| 290 | // Only variables which change the interface for a shader are returned, that is, |
| 291 | // variables with storage class of Input, Output, Uniform, UniformConstant, PushConstant and AtomicCounter |
| 292 | // storage classes are returned. |
| 293 | // |
| 294 | // To use the returned set as the filter for which variables are used during compilation, |
| 295 | // this set can be moved to set_enabled_interface_variables(). |
| 296 | std::unordered_set<VariableID> get_active_interface_variables() const; |
| 297 | |
| 298 | // Sets the interface variables which are used during compilation. |
| 299 | // By default, all variables are used. |
| 300 | // Once set, compile() will only consider the set in active_variables. |
| 301 | void set_enabled_interface_variables(std::unordered_set<VariableID> active_variables); |
| 302 | |
| 303 | // Query shader resources, use ids with reflection interface to modify or query binding points, etc. |
| 304 | ShaderResources get_shader_resources() const; |
| 305 | |
| 306 | // Query shader resources, but only return the variables which are part of active_variables. |
| 307 | // E.g.: get_shader_resources(get_active_variables()) to only return the variables which are statically |
| 308 | // accessed. |
| 309 | ShaderResources get_shader_resources(const std::unordered_set<VariableID> &active_variables) const; |
| 310 | |
| 311 | // Remapped variables are considered built-in variables and a backend will |
| 312 | // not emit a declaration for this variable. |
| 313 | // This is mostly useful for making use of builtins which are dependent on extensions. |
| 314 | void set_remapped_variable_state(VariableID id, bool remap_enable); |
| 315 | bool get_remapped_variable_state(VariableID id) const; |
| 316 | |
| 317 | // For subpassInput variables which are remapped to plain variables, |
| 318 | // the number of components in the remapped |
| 319 | // variable must be specified as the backing type of subpass inputs are opaque. |
| 320 | void set_subpass_input_remapped_components(VariableID id, uint32_t components); |
| 321 | uint32_t get_subpass_input_remapped_components(VariableID id) const; |
| 322 | |
| 323 | // All operations work on the current entry point. |
| 324 | // Entry points can be swapped out with set_entry_point(). |
| 325 | // Entry points should be set right after the constructor completes as some reflection functions traverse the graph from the entry point. |
| 326 | // Resource reflection also depends on the entry point. |
| 327 | // By default, the current entry point is set to the first OpEntryPoint which appears in the SPIR-V module. |
| 328 | |
| 329 | // Some shader languages restrict the names that can be given to entry points, and the |
| 330 | // corresponding backend will automatically rename an entry point name, during the call |
| 331 | // to compile() if it is illegal. For example, the common entry point name main() is |
| 332 | // illegal in MSL, and is renamed to an alternate name by the MSL backend. |
| 333 | // Given the original entry point name contained in the SPIR-V, this function returns |
| 334 | // the name, as updated by the backend during the call to compile(). If the name is not |
| 335 | // illegal, and has not been renamed, or if this function is called before compile(), |
| 336 | // this function will simply return the same name. |
| 337 | |
| 338 | // New variants of entry point query and reflection. |
| 339 | // Names for entry points in the SPIR-V module may alias if they belong to different execution models. |
| 340 | // To disambiguate, we must pass along with the entry point names the execution model. |
| 341 | SmallVector<EntryPoint> get_entry_points_and_stages() const; |
| 342 | void set_entry_point(const std::string &entry, spv::ExecutionModel execution_model); |
| 343 | |
| 344 | // Renames an entry point from old_name to new_name. |
| 345 | // If old_name is currently selected as the current entry point, it will continue to be the current entry point, |
| 346 | // albeit with a new name. |
| 347 | // get_entry_points() is essentially invalidated at this point. |
| 348 | void rename_entry_point(const std::string &old_name, const std::string &new_name, |
| 349 | spv::ExecutionModel execution_model); |
| 350 | const SPIREntryPoint &get_entry_point(const std::string &name, spv::ExecutionModel execution_model) const; |
| 351 | SPIREntryPoint &get_entry_point(const std::string &name, spv::ExecutionModel execution_model); |
| 352 | const std::string &get_cleansed_entry_point_name(const std::string &name, |
| 353 | spv::ExecutionModel execution_model) const; |
| 354 | |
| 355 | // Traverses all reachable opcodes and sets active_builtins to a bitmask of all builtin variables which are accessed in the shader. |
| 356 | void update_active_builtins(); |
| 357 | bool has_active_builtin(spv::BuiltIn builtin, spv::StorageClass storage) const; |
| 358 | |
| 359 | // Query and modify OpExecutionMode. |
| 360 | const Bitset &get_execution_mode_bitset() const; |
| 361 | |
| 362 | void unset_execution_mode(spv::ExecutionMode mode); |
| 363 | void set_execution_mode(spv::ExecutionMode mode, uint32_t arg0 = 0, uint32_t arg1 = 0, uint32_t arg2 = 0); |
| 364 | |
| 365 | // Gets argument for an execution mode (LocalSize, Invocations, OutputVertices). |
| 366 | // For LocalSize or LocalSizeId, the index argument is used to select the dimension (X = 0, Y = 1, Z = 2). |
| 367 | // For execution modes which do not have arguments, 0 is returned. |
| 368 | // LocalSizeId query returns an ID. If LocalSizeId execution mode is not used, it returns 0. |
| 369 | // LocalSize always returns a literal. If execution mode is LocalSizeId, |
| 370 | // the literal (spec constant or not) is still returned. |
| 371 | uint32_t get_execution_mode_argument(spv::ExecutionMode mode, uint32_t index = 0) const; |
| 372 | spv::ExecutionModel get_execution_model() const; |
| 373 | |
| 374 | bool is_tessellation_shader() const; |
| 375 | bool is_tessellating_triangles() const; |
| 376 | |
| 377 | // In SPIR-V, the compute work group size can be represented by a constant vector, in which case |
| 378 | // the LocalSize execution mode is ignored. |
| 379 | // |
| 380 | // This constant vector can be a constant vector, specialization constant vector, or partly specialized constant vector. |
| 381 | // To modify and query work group dimensions which are specialization constants, SPIRConstant values must be modified |
| 382 | // directly via get_constant() rather than using LocalSize directly. This function will return which constants should be modified. |
| 383 | // |
| 384 | // To modify dimensions which are *not* specialization constants, set_execution_mode should be used directly. |
| 385 | // Arguments to set_execution_mode which are specialization constants are effectively ignored during compilation. |
| 386 | // NOTE: This is somewhat different from how SPIR-V works. In SPIR-V, the constant vector will completely replace LocalSize, |
| 387 | // while in this interface, LocalSize is only ignored for specialization constants. |
| 388 | // |
| 389 | // The specialization constant will be written to x, y and z arguments. |
| 390 | // If the component is not a specialization constant, a zeroed out struct will be written. |
| 391 | // The return value is the constant ID of the builtin WorkGroupSize, but this is not expected to be useful |
| 392 | // for most use cases. |
| 393 | // If LocalSizeId is used, there is no uvec3 value representing the workgroup size, so the return value is 0, |
| 394 | // but x, y and z are written as normal if the components are specialization constants. |
| 395 | uint32_t get_work_group_size_specialization_constants(SpecializationConstant &x, SpecializationConstant &y, |
| 396 | SpecializationConstant &z) const; |
| 397 | |
| 398 | // Analyzes all OpImageFetch (texelFetch) opcodes and checks if there are instances where |
| 399 | // said instruction is used without a combined image sampler. |
| 400 | // GLSL targets do not support the use of texelFetch without a sampler. |
| 401 | // To workaround this, we must inject a dummy sampler which can be used to form a sampler2D at the call-site of |
| 402 | // texelFetch as necessary. |
| 403 | // |
| 404 | // This must be called before build_combined_image_samplers(). |
| 405 | // build_combined_image_samplers() may refer to the ID returned by this method if the returned ID is non-zero. |
| 406 | // The return value will be the ID of a sampler object if a dummy sampler is necessary, or 0 if no sampler object |
| 407 | // is required. |
| 408 | // |
| 409 | // If the returned ID is non-zero, it can be decorated with set/bindings as desired before calling compile(). |
| 410 | // Calling this function also invalidates get_active_interface_variables(), so this should be called |
| 411 | // before that function. |
| 412 | VariableID build_dummy_sampler_for_combined_images(); |
| 413 | |
| 414 | // Analyzes all separate image and samplers used from the currently selected entry point, |
| 415 | // and re-routes them all to a combined image sampler instead. |
| 416 | // This is required to "support" separate image samplers in targets which do not natively support |
| 417 | // this feature, like GLSL/ESSL. |
| 418 | // |
| 419 | // This must be called before compile() if such remapping is desired. |
| 420 | // This call will add new sampled images to the SPIR-V, |
| 421 | // so it will appear in reflection if get_shader_resources() is called after build_combined_image_samplers. |
| 422 | // |
| 423 | // If any image/sampler remapping was found, no separate image/samplers will appear in the decompiled output, |
| 424 | // but will still appear in reflection. |
| 425 | // |
| 426 | // The resulting samplers will be void of any decorations like name, descriptor sets and binding points, |
| 427 | // so this can be added before compile() if desired. |
| 428 | // |
| 429 | // Combined image samplers originating from this set are always considered active variables. |
| 430 | // Arrays of separate samplers are not supported, but arrays of separate images are supported. |
| 431 | // Array of images + sampler -> Array of combined image samplers. |
| 432 | void build_combined_image_samplers(); |
| 433 | |
| 434 | // Gets a remapping for the combined image samplers. |
| 435 | const SmallVector<CombinedImageSampler> &get_combined_image_samplers() const |
| 436 | { |
| 437 | return combined_image_samplers; |
| 438 | } |
| 439 | |
| 440 | // Set a new variable type remap callback. |
| 441 | // The type remapping is designed to allow global interface variable to assume more special types. |
| 442 | // A typical example here is to remap sampler2D into samplerExternalOES, which currently isn't supported |
| 443 | // directly by SPIR-V. |
| 444 | // |
| 445 | // In compile() while emitting code, |
| 446 | // for every variable that is declared, including function parameters, the callback will be called |
| 447 | // and the API user has a chance to change the textual representation of the type used to declare the variable. |
| 448 | // The API user can detect special patterns in names to guide the remapping. |
| 449 | void set_variable_type_remap_callback(VariableTypeRemapCallback cb) |
| 450 | { |
| 451 | variable_remap_callback = std::move(cb); |
| 452 | } |
| 453 | |
| 454 | // API for querying which specialization constants exist. |
| 455 | // To modify a specialization constant before compile(), use get_constant(constant.id), |
| 456 | // then update constants directly in the SPIRConstant data structure. |
| 457 | // For composite types, the subconstants can be iterated over and modified. |
| 458 | // constant_type is the SPIRType for the specialization constant, |
| 459 | // which can be queried to determine which fields in the unions should be poked at. |
| 460 | SmallVector<SpecializationConstant> get_specialization_constants() const; |
| 461 | SPIRConstant &get_constant(ConstantID id); |
| 462 | const SPIRConstant &get_constant(ConstantID id) const; |
| 463 | |
| 464 | uint32_t get_current_id_bound() const |
| 465 | { |
| 466 | return uint32_t(ir.ids.size()); |
| 467 | } |
| 468 | |
| 469 | // API for querying buffer objects. |
| 470 | // The type passed in here should be the base type of a resource, i.e. |
| 471 | // get_type(resource.base_type_id) |
| 472 | // as decorations are set in the basic Block type. |
| 473 | // The type passed in here must have these decorations set, or an exception is raised. |
| 474 | // Only UBOs and SSBOs or sub-structs which are part of these buffer types will have these decorations set. |
| 475 | uint32_t type_struct_member_offset(const SPIRType &type, uint32_t index) const; |
| 476 | uint32_t type_struct_member_array_stride(const SPIRType &type, uint32_t index) const; |
| 477 | uint32_t type_struct_member_matrix_stride(const SPIRType &type, uint32_t index) const; |
| 478 | |
| 479 | // Gets the offset in SPIR-V words (uint32_t) for a decoration which was originally declared in the SPIR-V binary. |
| 480 | // The offset will point to one or more uint32_t literals which can be modified in-place before using the SPIR-V binary. |
| 481 | // Note that adding or removing decorations using the reflection API will not change the behavior of this function. |
| 482 | // If the decoration was declared, sets the word_offset to an offset into the provided SPIR-V binary buffer and returns true, |
| 483 | // otherwise, returns false. |
| 484 | // If the decoration does not have any value attached to it (e.g. DecorationRelaxedPrecision), this function will also return false. |
| 485 | bool get_binary_offset_for_decoration(VariableID id, spv::Decoration decoration, uint32_t &word_offset) const; |
| 486 | |
| 487 | // HLSL counter buffer reflection interface. |
| 488 | // Append/Consume/Increment/Decrement in HLSL is implemented as two "neighbor" buffer objects where |
| 489 | // one buffer implements the storage, and a single buffer containing just a lone "int" implements the counter. |
| 490 | // To SPIR-V these will be exposed as two separate buffers, but glslang HLSL frontend emits a special indentifier |
| 491 | // which lets us link the two buffers together. |
| 492 | |
| 493 | // Queries if a variable ID is a counter buffer which "belongs" to a regular buffer object. |
| 494 | |
| 495 | // If SPV_GOOGLE_hlsl_functionality1 is used, this can be used even with a stripped SPIR-V module. |
| 496 | // Otherwise, this query is purely based on OpName identifiers as found in the SPIR-V module, and will |
| 497 | // only return true if OpSource was reported HLSL. |
| 498 | // To rely on this functionality, ensure that the SPIR-V module is not stripped. |
| 499 | |
| 500 | bool buffer_is_hlsl_counter_buffer(VariableID id) const; |
| 501 | |
| 502 | // Queries if a buffer object has a neighbor "counter" buffer. |
| 503 | // If so, the ID of that counter buffer will be returned in counter_id. |
| 504 | // If SPV_GOOGLE_hlsl_functionality1 is used, this can be used even with a stripped SPIR-V module. |
| 505 | // Otherwise, this query is purely based on OpName identifiers as found in the SPIR-V module, and will |
| 506 | // only return true if OpSource was reported HLSL. |
| 507 | // To rely on this functionality, ensure that the SPIR-V module is not stripped. |
| 508 | bool buffer_get_hlsl_counter_buffer(VariableID id, uint32_t &counter_id) const; |
| 509 | |
| 510 | // Gets the list of all SPIR-V Capabilities which were declared in the SPIR-V module. |
| 511 | const SmallVector<spv::Capability> &get_declared_capabilities() const; |
| 512 | |
| 513 | // Gets the list of all SPIR-V extensions which were declared in the SPIR-V module. |
| 514 | const SmallVector<std::string> &get_declared_extensions() const; |
| 515 | |
| 516 | // When declaring buffer blocks in GLSL, the name declared in the GLSL source |
| 517 | // might not be the same as the name declared in the SPIR-V module due to naming conflicts. |
| 518 | // In this case, SPIRV-Cross needs to find a fallback-name, and it might only |
| 519 | // be possible to know this name after compiling to GLSL. |
| 520 | // This is particularly important for HLSL input and UAVs which tends to reuse the same block type |
| 521 | // for multiple distinct blocks. For these cases it is not possible to modify the name of the type itself |
| 522 | // because it might be unique. Instead, you can use this interface to check after compilation which |
| 523 | // name was actually used if your input SPIR-V tends to have this problem. |
| 524 | // For other names like remapped names for variables, etc, it's generally enough to query the name of the variables |
| 525 | // after compiling, block names are an exception to this rule. |
| 526 | // ID is the name of a variable as returned by Resource::id, and must be a variable with a Block-like type. |
| 527 | // |
| 528 | // This also applies to HLSL cbuffers. |
| 529 | std::string get_remapped_declared_block_name(VariableID id) const; |
| 530 | |
| 531 | // For buffer block variables, get the decorations for that variable. |
| 532 | // Sometimes, decorations for buffer blocks are found in member decorations instead |
| 533 | // of direct decorations on the variable itself. |
| 534 | // The most common use here is to check if a buffer is readonly or writeonly. |
| 535 | Bitset get_buffer_block_flags(VariableID id) const; |
| 536 | |
| 537 | // Returns whether the position output is invariant |
| 538 | bool is_position_invariant() const |
| 539 | { |
| 540 | return position_invariant; |
| 541 | } |
| 542 | |
| 543 | protected: |
| 544 | const uint32_t *stream(const Instruction &instr) const |
| 545 | { |
| 546 | // If we're not going to use any arguments, just return nullptr. |
| 547 | // We want to avoid case where we return an out of range pointer |
| 548 | // that trips debug assertions on some platforms. |
| 549 | if (!instr.length) |
| 550 | return nullptr; |
| 551 | |
| 552 | if (instr.is_embedded()) |
| 553 | { |
| 554 | auto &embedded = static_cast<const EmbeddedInstruction &>(instr); |
| 555 | assert(embedded.ops.size() == instr.length); |
| 556 | return embedded.ops.data(); |
| 557 | } |
| 558 | else |
| 559 | { |
| 560 | if (instr.offset + instr.length > ir.spirv.size()) |
| 561 | SPIRV_CROSS_THROW("Compiler::stream() out of range." ); |
| 562 | return &ir.spirv[instr.offset]; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | uint32_t *stream_mutable(const Instruction &instr) const |
| 567 | { |
| 568 | return const_cast<uint32_t *>(stream(instr)); |
| 569 | } |
| 570 | |
| 571 | ParsedIR ir; |
| 572 | // Marks variables which have global scope and variables which can alias with other variables |
| 573 | // (SSBO, image load store, etc) |
| 574 | SmallVector<uint32_t> global_variables; |
| 575 | SmallVector<uint32_t> aliased_variables; |
| 576 | |
| 577 | SPIRFunction *current_function = nullptr; |
| 578 | SPIRBlock *current_block = nullptr; |
| 579 | uint32_t current_loop_level = 0; |
| 580 | std::unordered_set<VariableID> active_interface_variables; |
| 581 | bool check_active_interface_variables = false; |
| 582 | |
| 583 | void add_loop_level(); |
| 584 | |
| 585 | void set_initializers(SPIRExpression &e) |
| 586 | { |
| 587 | e.emitted_loop_level = current_loop_level; |
| 588 | } |
| 589 | |
| 590 | template <typename T> |
| 591 | void set_initializers(const T &) |
| 592 | { |
| 593 | } |
| 594 | |
| 595 | // If our IDs are out of range here as part of opcodes, throw instead of |
| 596 | // undefined behavior. |
| 597 | template <typename T, typename... P> |
| 598 | T &set(uint32_t id, P &&... args) |
| 599 | { |
| 600 | ir.add_typed_id(type: static_cast<Types>(T::type), id); |
| 601 | auto &var = variant_set<T>(ir.ids[id], std::forward<P>(args)...); |
| 602 | var.self = id; |
| 603 | set_initializers(var); |
| 604 | return var; |
| 605 | } |
| 606 | |
| 607 | template <typename T> |
| 608 | T &get(uint32_t id) |
| 609 | { |
| 610 | return variant_get<T>(ir.ids[id]); |
| 611 | } |
| 612 | |
| 613 | template <typename T> |
| 614 | T *maybe_get(uint32_t id) |
| 615 | { |
| 616 | if (id >= ir.ids.size()) |
| 617 | return nullptr; |
| 618 | else if (ir.ids[id].get_type() == static_cast<Types>(T::type)) |
| 619 | return &get<T>(id); |
| 620 | else |
| 621 | return nullptr; |
| 622 | } |
| 623 | |
| 624 | template <typename T> |
| 625 | const T &get(uint32_t id) const |
| 626 | { |
| 627 | return variant_get<T>(ir.ids[id]); |
| 628 | } |
| 629 | |
| 630 | template <typename T> |
| 631 | const T *maybe_get(uint32_t id) const |
| 632 | { |
| 633 | if (id >= ir.ids.size()) |
| 634 | return nullptr; |
| 635 | else if (ir.ids[id].get_type() == static_cast<Types>(T::type)) |
| 636 | return &get<T>(id); |
| 637 | else |
| 638 | return nullptr; |
| 639 | } |
| 640 | |
| 641 | // Gets the id of SPIR-V type underlying the given type_id, which might be a pointer. |
| 642 | uint32_t get_pointee_type_id(uint32_t type_id) const; |
| 643 | |
| 644 | // Gets the SPIR-V type underlying the given type, which might be a pointer. |
| 645 | const SPIRType &get_pointee_type(const SPIRType &type) const; |
| 646 | |
| 647 | // Gets the SPIR-V type underlying the given type_id, which might be a pointer. |
| 648 | const SPIRType &get_pointee_type(uint32_t type_id) const; |
| 649 | |
| 650 | // Gets the ID of the SPIR-V type underlying a variable. |
| 651 | uint32_t get_variable_data_type_id(const SPIRVariable &var) const; |
| 652 | |
| 653 | // Gets the SPIR-V type underlying a variable. |
| 654 | SPIRType &get_variable_data_type(const SPIRVariable &var); |
| 655 | |
| 656 | // Gets the SPIR-V type underlying a variable. |
| 657 | const SPIRType &get_variable_data_type(const SPIRVariable &var) const; |
| 658 | |
| 659 | // Gets the SPIR-V element type underlying an array variable. |
| 660 | SPIRType &get_variable_element_type(const SPIRVariable &var); |
| 661 | |
| 662 | // Gets the SPIR-V element type underlying an array variable. |
| 663 | const SPIRType &get_variable_element_type(const SPIRVariable &var) const; |
| 664 | |
| 665 | // Sets the qualified member identifier for OpTypeStruct ID, member number "index". |
| 666 | void set_member_qualified_name(uint32_t type_id, uint32_t index, const std::string &name); |
| 667 | void set_qualified_name(uint32_t id, const std::string &name); |
| 668 | |
| 669 | // Returns if the given type refers to a sampled image. |
| 670 | bool is_sampled_image_type(const SPIRType &type); |
| 671 | |
| 672 | const SPIREntryPoint &get_entry_point() const; |
| 673 | SPIREntryPoint &get_entry_point(); |
| 674 | static bool is_tessellation_shader(spv::ExecutionModel model); |
| 675 | |
| 676 | virtual std::string to_name(uint32_t id, bool allow_alias = true) const; |
| 677 | bool is_builtin_variable(const SPIRVariable &var) const; |
| 678 | bool is_builtin_type(const SPIRType &type) const; |
| 679 | bool is_hidden_variable(const SPIRVariable &var, bool include_builtins = false) const; |
| 680 | bool is_immutable(uint32_t id) const; |
| 681 | bool is_member_builtin(const SPIRType &type, uint32_t index, spv::BuiltIn *builtin) const; |
| 682 | bool is_scalar(const SPIRType &type) const; |
| 683 | bool is_vector(const SPIRType &type) const; |
| 684 | bool is_matrix(const SPIRType &type) const; |
| 685 | bool is_array(const SPIRType &type) const; |
| 686 | bool is_pointer(const SPIRType &type) const; |
| 687 | bool is_physical_pointer(const SPIRType &type) const; |
| 688 | bool is_physical_pointer_to_buffer_block(const SPIRType &type) const; |
| 689 | static bool is_runtime_size_array(const SPIRType &type); |
| 690 | uint32_t expression_type_id(uint32_t id) const; |
| 691 | const SPIRType &expression_type(uint32_t id) const; |
| 692 | bool expression_is_lvalue(uint32_t id) const; |
| 693 | bool variable_storage_is_aliased(const SPIRVariable &var); |
| 694 | SPIRVariable *maybe_get_backing_variable(uint32_t chain); |
| 695 | |
| 696 | void register_read(uint32_t expr, uint32_t chain, bool forwarded); |
| 697 | void register_write(uint32_t chain); |
| 698 | |
| 699 | inline bool is_continue(uint32_t next) const |
| 700 | { |
| 701 | return (ir.block_meta[next] & ParsedIR::BLOCK_META_CONTINUE_BIT) != 0; |
| 702 | } |
| 703 | |
| 704 | inline bool is_single_block_loop(uint32_t next) const |
| 705 | { |
| 706 | auto &block = get<SPIRBlock>(id: next); |
| 707 | return block.merge == SPIRBlock::MergeLoop && block.continue_block == ID(next); |
| 708 | } |
| 709 | |
| 710 | inline bool is_break(uint32_t next) const |
| 711 | { |
| 712 | return (ir.block_meta[next] & |
| 713 | (ParsedIR::BLOCK_META_LOOP_MERGE_BIT | ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT)) != 0; |
| 714 | } |
| 715 | |
| 716 | inline bool is_loop_break(uint32_t next) const |
| 717 | { |
| 718 | return (ir.block_meta[next] & ParsedIR::BLOCK_META_LOOP_MERGE_BIT) != 0; |
| 719 | } |
| 720 | |
| 721 | inline bool is_conditional(uint32_t next) const |
| 722 | { |
| 723 | return (ir.block_meta[next] & |
| 724 | (ParsedIR::BLOCK_META_SELECTION_MERGE_BIT | ParsedIR::BLOCK_META_MULTISELECT_MERGE_BIT)) != 0; |
| 725 | } |
| 726 | |
| 727 | // Dependency tracking for temporaries read from variables. |
| 728 | void flush_dependees(SPIRVariable &var); |
| 729 | void flush_all_active_variables(); |
| 730 | void flush_control_dependent_expressions(uint32_t block); |
| 731 | void flush_all_atomic_capable_variables(); |
| 732 | void flush_all_aliased_variables(); |
| 733 | void register_global_read_dependencies(const SPIRBlock &func, uint32_t id); |
| 734 | void register_global_read_dependencies(const SPIRFunction &func, uint32_t id); |
| 735 | std::unordered_set<uint32_t> invalid_expressions; |
| 736 | |
| 737 | void update_name_cache(std::unordered_set<std::string> &cache, std::string &name); |
| 738 | |
| 739 | // A variant which takes two sets of names. The secondary is only used to verify there are no collisions, |
| 740 | // but the set is not updated when we have found a new name. |
| 741 | // Used primarily when adding block interface names. |
| 742 | void update_name_cache(std::unordered_set<std::string> &cache_primary, |
| 743 | const std::unordered_set<std::string> &cache_secondary, std::string &name); |
| 744 | |
| 745 | bool function_is_pure(const SPIRFunction &func); |
| 746 | bool block_is_pure(const SPIRBlock &block); |
| 747 | bool function_is_control_dependent(const SPIRFunction &func); |
| 748 | bool block_is_control_dependent(const SPIRBlock &block); |
| 749 | |
| 750 | bool execution_is_branchless(const SPIRBlock &from, const SPIRBlock &to) const; |
| 751 | bool execution_is_direct_branch(const SPIRBlock &from, const SPIRBlock &to) const; |
| 752 | bool execution_is_noop(const SPIRBlock &from, const SPIRBlock &to) const; |
| 753 | SPIRBlock::ContinueBlockType continue_block_type(const SPIRBlock &continue_block) const; |
| 754 | |
| 755 | void force_recompile(); |
| 756 | void force_recompile_guarantee_forward_progress(); |
| 757 | void clear_force_recompile(); |
| 758 | bool is_forcing_recompilation() const; |
| 759 | bool is_force_recompile = false; |
| 760 | bool is_force_recompile_forward_progress = false; |
| 761 | |
| 762 | bool block_is_noop(const SPIRBlock &block) const; |
| 763 | bool block_is_loop_candidate(const SPIRBlock &block, SPIRBlock::Method method) const; |
| 764 | |
| 765 | bool types_are_logically_equivalent(const SPIRType &a, const SPIRType &b) const; |
| 766 | void inherit_expression_dependencies(uint32_t dst, uint32_t source); |
| 767 | void add_implied_read_expression(SPIRExpression &e, uint32_t source); |
| 768 | void add_implied_read_expression(SPIRAccessChain &e, uint32_t source); |
| 769 | void add_active_interface_variable(uint32_t var_id); |
| 770 | |
| 771 | // For proper multiple entry point support, allow querying if an Input or Output |
| 772 | // variable is part of that entry points interface. |
| 773 | bool interface_variable_exists_in_entry_point(uint32_t id) const; |
| 774 | |
| 775 | SmallVector<CombinedImageSampler> combined_image_samplers; |
| 776 | |
| 777 | void remap_variable_type_name(const SPIRType &type, const std::string &var_name, std::string &type_name) const |
| 778 | { |
| 779 | if (variable_remap_callback) |
| 780 | variable_remap_callback(type, var_name, type_name); |
| 781 | } |
| 782 | |
| 783 | void set_ir(const ParsedIR &parsed); |
| 784 | void set_ir(ParsedIR &&parsed); |
| 785 | void parse_fixup(); |
| 786 | |
| 787 | // Used internally to implement various traversals for queries. |
| 788 | struct OpcodeHandler |
| 789 | { |
| 790 | virtual ~OpcodeHandler() = default; |
| 791 | |
| 792 | // Return true if traversal should continue. |
| 793 | // If false, traversal will end immediately. |
| 794 | virtual bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) = 0; |
| 795 | virtual bool handle_terminator(const SPIRBlock &) |
| 796 | { |
| 797 | return true; |
| 798 | } |
| 799 | |
| 800 | virtual bool follow_function_call(const SPIRFunction &) |
| 801 | { |
| 802 | return true; |
| 803 | } |
| 804 | |
| 805 | virtual void set_current_block(const SPIRBlock &) |
| 806 | { |
| 807 | } |
| 808 | |
| 809 | // Called after returning from a function or when entering a block, |
| 810 | // can be called multiple times per block, |
| 811 | // while set_current_block is only called on block entry. |
| 812 | virtual void rearm_current_block(const SPIRBlock &) |
| 813 | { |
| 814 | } |
| 815 | |
| 816 | virtual bool begin_function_scope(const uint32_t *, uint32_t) |
| 817 | { |
| 818 | return true; |
| 819 | } |
| 820 | |
| 821 | virtual bool end_function_scope(const uint32_t *, uint32_t) |
| 822 | { |
| 823 | return true; |
| 824 | } |
| 825 | }; |
| 826 | |
| 827 | struct BufferAccessHandler : OpcodeHandler |
| 828 | { |
| 829 | BufferAccessHandler(const Compiler &compiler_, SmallVector<BufferRange> &ranges_, uint32_t id_) |
| 830 | : compiler(compiler_) |
| 831 | , ranges(ranges_) |
| 832 | , id(id_) |
| 833 | { |
| 834 | } |
| 835 | |
| 836 | bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override; |
| 837 | |
| 838 | const Compiler &compiler; |
| 839 | SmallVector<BufferRange> &ranges; |
| 840 | uint32_t id; |
| 841 | |
| 842 | std::unordered_set<uint32_t> seen; |
| 843 | }; |
| 844 | |
| 845 | struct InterfaceVariableAccessHandler : OpcodeHandler |
| 846 | { |
| 847 | InterfaceVariableAccessHandler(const Compiler &compiler_, std::unordered_set<VariableID> &variables_) |
| 848 | : compiler(compiler_) |
| 849 | , variables(variables_) |
| 850 | { |
| 851 | } |
| 852 | |
| 853 | bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override; |
| 854 | |
| 855 | const Compiler &compiler; |
| 856 | std::unordered_set<VariableID> &variables; |
| 857 | }; |
| 858 | |
| 859 | struct CombinedImageSamplerHandler : OpcodeHandler |
| 860 | { |
| 861 | CombinedImageSamplerHandler(Compiler &compiler_) |
| 862 | : compiler(compiler_) |
| 863 | { |
| 864 | } |
| 865 | bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override; |
| 866 | bool begin_function_scope(const uint32_t *args, uint32_t length) override; |
| 867 | bool end_function_scope(const uint32_t *args, uint32_t length) override; |
| 868 | |
| 869 | Compiler &compiler; |
| 870 | |
| 871 | // Each function in the call stack needs its own remapping for parameters so we can deduce which global variable each texture/sampler the parameter is statically bound to. |
| 872 | std::stack<std::unordered_map<uint32_t, uint32_t>> parameter_remapping; |
| 873 | std::stack<SPIRFunction *> functions; |
| 874 | |
| 875 | uint32_t remap_parameter(uint32_t id); |
| 876 | void push_remap_parameters(const SPIRFunction &func, const uint32_t *args, uint32_t length); |
| 877 | void pop_remap_parameters(); |
| 878 | void register_combined_image_sampler(SPIRFunction &caller, VariableID combined_id, VariableID texture_id, |
| 879 | VariableID sampler_id, bool depth); |
| 880 | }; |
| 881 | |
| 882 | struct DummySamplerForCombinedImageHandler : OpcodeHandler |
| 883 | { |
| 884 | DummySamplerForCombinedImageHandler(Compiler &compiler_) |
| 885 | : compiler(compiler_) |
| 886 | { |
| 887 | } |
| 888 | bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override; |
| 889 | |
| 890 | Compiler &compiler; |
| 891 | bool need_dummy_sampler = false; |
| 892 | }; |
| 893 | |
| 894 | struct ActiveBuiltinHandler : OpcodeHandler |
| 895 | { |
| 896 | ActiveBuiltinHandler(Compiler &compiler_) |
| 897 | : compiler(compiler_) |
| 898 | { |
| 899 | } |
| 900 | |
| 901 | bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override; |
| 902 | Compiler &compiler; |
| 903 | |
| 904 | void handle_builtin(const SPIRType &type, spv::BuiltIn builtin, const Bitset &decoration_flags); |
| 905 | void add_if_builtin(uint32_t id); |
| 906 | void add_if_builtin_or_block(uint32_t id); |
| 907 | void add_if_builtin(uint32_t id, bool allow_blocks); |
| 908 | }; |
| 909 | |
| 910 | bool traverse_all_reachable_opcodes(const SPIRBlock &block, OpcodeHandler &handler) const; |
| 911 | bool traverse_all_reachable_opcodes(const SPIRFunction &block, OpcodeHandler &handler) const; |
| 912 | // This must be an ordered data structure so we always pick the same type aliases. |
| 913 | SmallVector<uint32_t> global_struct_cache; |
| 914 | |
| 915 | ShaderResources get_shader_resources(const std::unordered_set<VariableID> *active_variables) const; |
| 916 | |
| 917 | VariableTypeRemapCallback variable_remap_callback; |
| 918 | |
| 919 | bool get_common_basic_type(const SPIRType &type, SPIRType::BaseType &base_type); |
| 920 | |
| 921 | std::unordered_set<uint32_t> forced_temporaries; |
| 922 | std::unordered_set<uint32_t> forwarded_temporaries; |
| 923 | std::unordered_set<uint32_t> suppressed_usage_tracking; |
| 924 | std::unordered_set<uint32_t> hoisted_temporaries; |
| 925 | std::unordered_set<uint32_t> forced_invariant_temporaries; |
| 926 | |
| 927 | Bitset active_input_builtins; |
| 928 | Bitset active_output_builtins; |
| 929 | uint32_t clip_distance_count = 0; |
| 930 | uint32_t cull_distance_count = 0; |
| 931 | bool position_invariant = false; |
| 932 | |
| 933 | void analyze_parameter_preservation( |
| 934 | SPIRFunction &entry, const CFG &cfg, |
| 935 | const std::unordered_map<uint32_t, std::unordered_set<uint32_t>> &variable_to_blocks, |
| 936 | const std::unordered_map<uint32_t, std::unordered_set<uint32_t>> &complete_write_blocks); |
| 937 | |
| 938 | // If a variable ID or parameter ID is found in this set, a sampler is actually a shadow/comparison sampler. |
| 939 | // SPIR-V does not support this distinction, so we must keep track of this information outside the type system. |
| 940 | // There might be unrelated IDs found in this set which do not correspond to actual variables. |
| 941 | // This set should only be queried for the existence of samplers which are already known to be variables or parameter IDs. |
| 942 | // Similar is implemented for images, as well as if subpass inputs are needed. |
| 943 | std::unordered_set<uint32_t> comparison_ids; |
| 944 | bool need_subpass_input = false; |
| 945 | bool need_subpass_input_ms = false; |
| 946 | |
| 947 | // In certain backends, we will need to use a dummy sampler to be able to emit code. |
| 948 | // GLSL does not support texelFetch on texture2D objects, but SPIR-V does, |
| 949 | // so we need to workaround by having the application inject a dummy sampler. |
| 950 | uint32_t dummy_sampler_id = 0; |
| 951 | |
| 952 | void analyze_image_and_sampler_usage(); |
| 953 | |
| 954 | struct CombinedImageSamplerDrefHandler : OpcodeHandler |
| 955 | { |
| 956 | CombinedImageSamplerDrefHandler(Compiler &compiler_) |
| 957 | : compiler(compiler_) |
| 958 | { |
| 959 | } |
| 960 | bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override; |
| 961 | |
| 962 | Compiler &compiler; |
| 963 | std::unordered_set<uint32_t> dref_combined_samplers; |
| 964 | }; |
| 965 | |
| 966 | struct CombinedImageSamplerUsageHandler : OpcodeHandler |
| 967 | { |
| 968 | CombinedImageSamplerUsageHandler(Compiler &compiler_, |
| 969 | const std::unordered_set<uint32_t> &dref_combined_samplers_) |
| 970 | : compiler(compiler_) |
| 971 | , dref_combined_samplers(dref_combined_samplers_) |
| 972 | { |
| 973 | } |
| 974 | |
| 975 | bool begin_function_scope(const uint32_t *args, uint32_t length) override; |
| 976 | bool handle(spv::Op opcode, const uint32_t *args, uint32_t length) override; |
| 977 | Compiler &compiler; |
| 978 | const std::unordered_set<uint32_t> &dref_combined_samplers; |
| 979 | |
| 980 | std::unordered_map<uint32_t, std::unordered_set<uint32_t>> dependency_hierarchy; |
| 981 | std::unordered_set<uint32_t> comparison_ids; |
| 982 | |
| 983 | void add_hierarchy_to_comparison_ids(uint32_t ids); |
| 984 | bool need_subpass_input = false; |
| 985 | bool need_subpass_input_ms = false; |
| 986 | void add_dependency(uint32_t dst, uint32_t src); |
| 987 | }; |
| 988 | |
| 989 | void build_function_control_flow_graphs_and_analyze(); |
| 990 | std::unordered_map<uint32_t, std::unique_ptr<CFG>> function_cfgs; |
| 991 | const CFG &get_cfg_for_current_function() const; |
| 992 | const CFG &get_cfg_for_function(uint32_t id) const; |
| 993 | |
| 994 | struct CFGBuilder : OpcodeHandler |
| 995 | { |
| 996 | explicit CFGBuilder(Compiler &compiler_); |
| 997 | |
| 998 | bool follow_function_call(const SPIRFunction &func) override; |
| 999 | bool handle(spv::Op op, const uint32_t *args, uint32_t length) override; |
| 1000 | Compiler &compiler; |
| 1001 | std::unordered_map<uint32_t, std::unique_ptr<CFG>> function_cfgs; |
| 1002 | }; |
| 1003 | |
| 1004 | struct AnalyzeVariableScopeAccessHandler : OpcodeHandler |
| 1005 | { |
| 1006 | AnalyzeVariableScopeAccessHandler(Compiler &compiler_, SPIRFunction &entry_); |
| 1007 | |
| 1008 | bool follow_function_call(const SPIRFunction &) override; |
| 1009 | void set_current_block(const SPIRBlock &block) override; |
| 1010 | |
| 1011 | void notify_variable_access(uint32_t id, uint32_t block); |
| 1012 | bool id_is_phi_variable(uint32_t id) const; |
| 1013 | bool id_is_potential_temporary(uint32_t id) const; |
| 1014 | bool handle(spv::Op op, const uint32_t *args, uint32_t length) override; |
| 1015 | bool handle_terminator(const SPIRBlock &block) override; |
| 1016 | |
| 1017 | Compiler &compiler; |
| 1018 | SPIRFunction &entry; |
| 1019 | std::unordered_map<uint32_t, std::unordered_set<uint32_t>> accessed_variables_to_block; |
| 1020 | std::unordered_map<uint32_t, std::unordered_set<uint32_t>> accessed_temporaries_to_block; |
| 1021 | std::unordered_map<uint32_t, uint32_t> result_id_to_type; |
| 1022 | std::unordered_map<uint32_t, std::unordered_set<uint32_t>> complete_write_variables_to_block; |
| 1023 | std::unordered_map<uint32_t, std::unordered_set<uint32_t>> partial_write_variables_to_block; |
| 1024 | std::unordered_set<uint32_t> access_chain_expressions; |
| 1025 | // Access chains used in multiple blocks mean hoisting all the variables used to construct the access chain as not all backends can use pointers. |
| 1026 | // This is also relevant when forwarding opaque objects since we cannot lower these to temporaries. |
| 1027 | std::unordered_map<uint32_t, std::unordered_set<uint32_t>> rvalue_forward_children; |
| 1028 | const SPIRBlock *current_block = nullptr; |
| 1029 | }; |
| 1030 | |
| 1031 | struct StaticExpressionAccessHandler : OpcodeHandler |
| 1032 | { |
| 1033 | StaticExpressionAccessHandler(Compiler &compiler_, uint32_t variable_id_); |
| 1034 | bool follow_function_call(const SPIRFunction &) override; |
| 1035 | bool handle(spv::Op op, const uint32_t *args, uint32_t length) override; |
| 1036 | |
| 1037 | Compiler &compiler; |
| 1038 | uint32_t variable_id; |
| 1039 | uint32_t static_expression = 0; |
| 1040 | uint32_t write_count = 0; |
| 1041 | }; |
| 1042 | |
| 1043 | struct PhysicalBlockMeta |
| 1044 | { |
| 1045 | uint32_t alignment = 0; |
| 1046 | }; |
| 1047 | |
| 1048 | struct PhysicalStorageBufferPointerHandler : OpcodeHandler |
| 1049 | { |
| 1050 | explicit PhysicalStorageBufferPointerHandler(Compiler &compiler_); |
| 1051 | bool handle(spv::Op op, const uint32_t *args, uint32_t length) override; |
| 1052 | Compiler &compiler; |
| 1053 | |
| 1054 | std::unordered_set<uint32_t> non_block_types; |
| 1055 | std::unordered_map<uint32_t, PhysicalBlockMeta> physical_block_type_meta; |
| 1056 | std::unordered_map<uint32_t, PhysicalBlockMeta *> access_chain_to_physical_block; |
| 1057 | |
| 1058 | void mark_aligned_access(uint32_t id, const uint32_t *args, uint32_t length); |
| 1059 | PhysicalBlockMeta *find_block_meta(uint32_t id) const; |
| 1060 | bool type_is_bda_block_entry(uint32_t type_id) const; |
| 1061 | void setup_meta_chain(uint32_t type_id, uint32_t var_id); |
| 1062 | uint32_t get_minimum_scalar_alignment(const SPIRType &type) const; |
| 1063 | void analyze_non_block_types_from_block(const SPIRType &type); |
| 1064 | uint32_t get_base_non_block_type_id(uint32_t type_id) const; |
| 1065 | }; |
| 1066 | void analyze_non_block_pointer_types(); |
| 1067 | SmallVector<uint32_t> physical_storage_non_block_pointer_types; |
| 1068 | std::unordered_map<uint32_t, PhysicalBlockMeta> physical_storage_type_to_alignment; |
| 1069 | |
| 1070 | void analyze_variable_scope(SPIRFunction &function, AnalyzeVariableScopeAccessHandler &handler); |
| 1071 | void find_function_local_luts(SPIRFunction &function, const AnalyzeVariableScopeAccessHandler &handler, |
| 1072 | bool single_function); |
| 1073 | bool may_read_undefined_variable_in_block(const SPIRBlock &block, uint32_t var); |
| 1074 | |
| 1075 | // Finds all resources that are written to from inside the critical section, if present. |
| 1076 | // The critical section is delimited by OpBeginInvocationInterlockEXT and |
| 1077 | // OpEndInvocationInterlockEXT instructions. In MSL and HLSL, any resources written |
| 1078 | // while inside the critical section must be placed in a raster order group. |
| 1079 | struct InterlockedResourceAccessHandler : OpcodeHandler |
| 1080 | { |
| 1081 | InterlockedResourceAccessHandler(Compiler &compiler_, uint32_t entry_point_id) |
| 1082 | : compiler(compiler_) |
| 1083 | { |
| 1084 | call_stack.push_back(t: entry_point_id); |
| 1085 | } |
| 1086 | |
| 1087 | bool handle(spv::Op op, const uint32_t *args, uint32_t length) override; |
| 1088 | bool begin_function_scope(const uint32_t *args, uint32_t length) override; |
| 1089 | bool end_function_scope(const uint32_t *args, uint32_t length) override; |
| 1090 | |
| 1091 | Compiler &compiler; |
| 1092 | bool in_crit_sec = false; |
| 1093 | |
| 1094 | uint32_t interlock_function_id = 0; |
| 1095 | bool split_function_case = false; |
| 1096 | bool control_flow_interlock = false; |
| 1097 | bool use_critical_section = false; |
| 1098 | bool call_stack_is_interlocked = false; |
| 1099 | SmallVector<uint32_t> call_stack; |
| 1100 | |
| 1101 | void access_potential_resource(uint32_t id); |
| 1102 | }; |
| 1103 | |
| 1104 | struct InterlockedResourceAccessPrepassHandler : OpcodeHandler |
| 1105 | { |
| 1106 | InterlockedResourceAccessPrepassHandler(Compiler &compiler_, uint32_t entry_point_id) |
| 1107 | : compiler(compiler_) |
| 1108 | { |
| 1109 | call_stack.push_back(t: entry_point_id); |
| 1110 | } |
| 1111 | |
| 1112 | void rearm_current_block(const SPIRBlock &block) override; |
| 1113 | bool handle(spv::Op op, const uint32_t *args, uint32_t length) override; |
| 1114 | bool begin_function_scope(const uint32_t *args, uint32_t length) override; |
| 1115 | bool end_function_scope(const uint32_t *args, uint32_t length) override; |
| 1116 | |
| 1117 | Compiler &compiler; |
| 1118 | uint32_t interlock_function_id = 0; |
| 1119 | uint32_t current_block_id = 0; |
| 1120 | bool split_function_case = false; |
| 1121 | bool control_flow_interlock = false; |
| 1122 | SmallVector<uint32_t> call_stack; |
| 1123 | }; |
| 1124 | |
| 1125 | void analyze_interlocked_resource_usage(); |
| 1126 | // The set of all resources written while inside the critical section, if present. |
| 1127 | std::unordered_set<uint32_t> interlocked_resources; |
| 1128 | bool interlocked_is_complex = false; |
| 1129 | |
| 1130 | void make_constant_null(uint32_t id, uint32_t type); |
| 1131 | |
| 1132 | std::unordered_map<uint32_t, std::string> declared_block_names; |
| 1133 | |
| 1134 | bool instruction_to_result_type(uint32_t &result_type, uint32_t &result_id, spv::Op op, const uint32_t *args, |
| 1135 | uint32_t length); |
| 1136 | |
| 1137 | Bitset combined_decoration_for_member(const SPIRType &type, uint32_t index) const; |
| 1138 | static bool is_desktop_only_format(spv::ImageFormat format); |
| 1139 | |
| 1140 | bool is_depth_image(const SPIRType &type, uint32_t id) const; |
| 1141 | |
| 1142 | void set_extended_decoration(uint32_t id, ExtendedDecorations decoration, uint32_t value = 0); |
| 1143 | uint32_t get_extended_decoration(uint32_t id, ExtendedDecorations decoration) const; |
| 1144 | bool has_extended_decoration(uint32_t id, ExtendedDecorations decoration) const; |
| 1145 | void unset_extended_decoration(uint32_t id, ExtendedDecorations decoration); |
| 1146 | |
| 1147 | void set_extended_member_decoration(uint32_t type, uint32_t index, ExtendedDecorations decoration, |
| 1148 | uint32_t value = 0); |
| 1149 | uint32_t get_extended_member_decoration(uint32_t type, uint32_t index, ExtendedDecorations decoration) const; |
| 1150 | bool has_extended_member_decoration(uint32_t type, uint32_t index, ExtendedDecorations decoration) const; |
| 1151 | void unset_extended_member_decoration(uint32_t type, uint32_t index, ExtendedDecorations decoration); |
| 1152 | |
| 1153 | bool check_internal_recursion(const SPIRType &type, std::unordered_set<uint32_t> &checked_ids); |
| 1154 | bool type_contains_recursion(const SPIRType &type); |
| 1155 | bool type_is_array_of_pointers(const SPIRType &type) const; |
| 1156 | bool type_is_block_like(const SPIRType &type) const; |
| 1157 | bool type_is_top_level_block(const SPIRType &type) const; |
| 1158 | bool type_is_opaque_value(const SPIRType &type) const; |
| 1159 | |
| 1160 | bool reflection_ssbo_instance_name_is_significant() const; |
| 1161 | std::string get_remapped_declared_block_name(uint32_t id, bool fallback_prefer_instance_name) const; |
| 1162 | |
| 1163 | bool flush_phi_required(BlockID from, BlockID to) const; |
| 1164 | |
| 1165 | uint32_t evaluate_spec_constant_u32(const SPIRConstantOp &spec) const; |
| 1166 | uint32_t evaluate_constant_u32(uint32_t id) const; |
| 1167 | |
| 1168 | bool is_vertex_like_shader() const; |
| 1169 | |
| 1170 | // Get the correct case list for the OpSwitch, since it can be either a |
| 1171 | // 32 bit wide condition or a 64 bit, but the type is not embedded in the |
| 1172 | // instruction itself. |
| 1173 | const SmallVector<SPIRBlock::Case> &get_case_list(const SPIRBlock &block) const; |
| 1174 | |
| 1175 | private: |
| 1176 | // Used only to implement the old deprecated get_entry_point() interface. |
| 1177 | const SPIREntryPoint &get_first_entry_point(const std::string &name) const; |
| 1178 | SPIREntryPoint &get_first_entry_point(const std::string &name); |
| 1179 | }; |
| 1180 | } // namespace SPIRV_CROSS_NAMESPACE |
| 1181 | |
| 1182 | #endif |
| 1183 | |