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