Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 1 | // Copyright 2018 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef sw_SpirvShader_hpp |
| 16 | #define sw_SpirvShader_hpp |
| 17 | |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 18 | #include "SamplerCore.hpp" |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 19 | #include "ShaderCore.hpp" |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 20 | #include "SpirvID.hpp" |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 21 | #include "Device/Config.hpp" |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 22 | #include "Device/Sampler.hpp" |
Ben Clayton | 25e06e0 | 2020-02-07 11:19:08 +0000 | [diff] [blame] | 23 | #include "System/Debug.hpp" |
Nicolas Capens | 68d9ad8 | 2020-04-02 05:22:04 -0400 | [diff] [blame] | 24 | #include "System/Math.hpp" |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 25 | #include "System/Types.hpp" |
Antonio Maiorano | 42fd159 | 2020-04-27 11:30:40 -0400 | [diff] [blame] | 26 | #include "Vulkan/VkConfig.hpp" |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 27 | #include "Vulkan/VkDescriptorSet.hpp" |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 28 | |
Nicolas Capens | 78cc4f4 | 2020-04-08 23:44:07 -0400 | [diff] [blame] | 29 | #define SPV_ENABLE_UTILITY_CODE |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 30 | #include <spirv/unified1/spirv.hpp> |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 31 | |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 32 | #include <array> |
Ben Clayton | c42b220 | 2019-05-22 11:18:14 +0100 | [diff] [blame] | 33 | #include <atomic> |
| 34 | #include <cstdint> |
Ben Clayton | 6fae32c | 2019-02-28 20:06:42 +0000 | [diff] [blame] | 35 | #include <cstring> |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 36 | #include <deque> |
Ben Clayton | 49d8158 | 2019-03-12 20:05:04 +0000 | [diff] [blame] | 37 | #include <functional> |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 38 | #include <memory> |
Ben Clayton | c42b220 | 2019-05-22 11:18:14 +0100 | [diff] [blame] | 39 | #include <string> |
| 40 | #include <type_traits> |
| 41 | #include <unordered_map> |
| 42 | #include <unordered_set> |
| 43 | #include <vector> |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 44 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 45 | #undef Yield // b/127920555 |
Nicolas Capens | d354537 | 2019-08-09 13:59:18 -0400 | [diff] [blame] | 46 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 47 | namespace vk { |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 48 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 49 | class PipelineLayout; |
| 50 | class ImageView; |
| 51 | class Sampler; |
| 52 | class RenderPass; |
| 53 | struct SampledImageDescriptor; |
Ben Clayton | 24ea515 | 2019-02-26 11:02:42 +0000 | [diff] [blame] | 54 | |
Ben Clayton | 7d0ce41 | 2019-12-03 13:26:31 +0000 | [diff] [blame] | 55 | namespace dbg { |
| 56 | class Context; |
| 57 | } // namespace dbg |
| 58 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 59 | } // namespace vk |
| 60 | |
| 61 | namespace sw { |
| 62 | |
| 63 | // Forward declarations. |
| 64 | class SpirvRoutine; |
| 65 | |
| 66 | // Incrementally constructed complex bundle of rvalues |
| 67 | // Effectively a restricted vector, supporting only: |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 68 | // - allocation to a (runtime-known) fixed component count |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 69 | // - in-place construction of elements |
| 70 | // - const operator[] |
| 71 | class Intermediate |
| 72 | { |
| 73 | public: |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 74 | Intermediate(uint32_t componentCount) |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 75 | : componentCount(componentCount) |
| 76 | , scalar(new rr::Value *[componentCount]) |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 77 | { |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 78 | for(auto i = 0u; i < componentCount; i++) { scalar[i] = nullptr; } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | ~Intermediate() |
| 82 | { |
| 83 | delete[] scalar; |
| 84 | } |
| 85 | |
Ben Clayton | fc951cd | 2019-05-15 17:16:56 +0100 | [diff] [blame] | 86 | // TypeHint is used as a hint for rr::PrintValue::Ty<sw::Intermediate> to |
| 87 | // decide the format used to print the intermediate data. |
| 88 | enum class TypeHint |
| 89 | { |
| 90 | Float, |
| 91 | Int, |
| 92 | UInt |
| 93 | }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 94 | |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 95 | void move(uint32_t i, RValue<SIMD::Float> &&scalar) { emplace(i, scalar.value(), TypeHint::Float); } |
| 96 | void move(uint32_t i, RValue<SIMD::Int> &&scalar) { emplace(i, scalar.value(), TypeHint::Int); } |
| 97 | void move(uint32_t i, RValue<SIMD::UInt> &&scalar) { emplace(i, scalar.value(), TypeHint::UInt); } |
Ben Clayton | fc951cd | 2019-05-15 17:16:56 +0100 | [diff] [blame] | 98 | |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 99 | void move(uint32_t i, const RValue<SIMD::Float> &scalar) { emplace(i, scalar.value(), TypeHint::Float); } |
| 100 | void move(uint32_t i, const RValue<SIMD::Int> &scalar) { emplace(i, scalar.value(), TypeHint::Int); } |
| 101 | void move(uint32_t i, const RValue<SIMD::UInt> &scalar) { emplace(i, scalar.value(), TypeHint::UInt); } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 102 | |
| 103 | // Value retrieval functions. |
| 104 | RValue<SIMD::Float> Float(uint32_t i) const |
| 105 | { |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 106 | ASSERT(i < componentCount); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 107 | ASSERT(scalar[i] != nullptr); |
| 108 | return As<SIMD::Float>(scalar[i]); // TODO(b/128539387): RValue<SIMD::Float>(scalar) |
| 109 | } |
| 110 | |
| 111 | RValue<SIMD::Int> Int(uint32_t i) const |
| 112 | { |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 113 | ASSERT(i < componentCount); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 114 | ASSERT(scalar[i] != nullptr); |
| 115 | return As<SIMD::Int>(scalar[i]); // TODO(b/128539387): RValue<SIMD::Int>(scalar) |
| 116 | } |
| 117 | |
| 118 | RValue<SIMD::UInt> UInt(uint32_t i) const |
| 119 | { |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 120 | ASSERT(i < componentCount); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 121 | ASSERT(scalar[i] != nullptr); |
| 122 | return As<SIMD::UInt>(scalar[i]); // TODO(b/128539387): RValue<SIMD::UInt>(scalar) |
| 123 | } |
| 124 | |
| 125 | // No copy/move construction or assignment |
| 126 | Intermediate(Intermediate const &) = delete; |
| 127 | Intermediate(Intermediate &&) = delete; |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 128 | Intermediate &operator=(Intermediate const &) = delete; |
| 129 | Intermediate &operator=(Intermediate &&) = delete; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 130 | |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 131 | const uint32_t componentCount; |
| 132 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 133 | private: |
Ben Clayton | fc951cd | 2019-05-15 17:16:56 +0100 | [diff] [blame] | 134 | void emplace(uint32_t i, rr::Value *value, TypeHint type) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 135 | { |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 136 | ASSERT(i < componentCount); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 137 | ASSERT(scalar[i] == nullptr); |
| 138 | scalar[i] = value; |
Ben Clayton | fc951cd | 2019-05-15 17:16:56 +0100 | [diff] [blame] | 139 | RR_PRINT_ONLY(typeHint = type;) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | rr::Value **const scalar; |
Ben Clayton | fc951cd | 2019-05-15 17:16:56 +0100 | [diff] [blame] | 143 | |
| 144 | #ifdef ENABLE_RR_PRINT |
| 145 | friend struct rr::PrintValue::Ty<sw::Intermediate>; |
| 146 | TypeHint typeHint = TypeHint::Float; |
| 147 | #endif // ENABLE_RR_PRINT |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | class SpirvShader |
| 151 | { |
| 152 | public: |
| 153 | using InsnStore = std::vector<uint32_t>; |
| 154 | InsnStore insns; |
| 155 | |
Nicolas Capens | 6c11cf2 | 2020-03-19 15:21:13 -0400 | [diff] [blame] | 156 | using ImageSampler = void(void *texture, void *uvsIn, void *texelOut, void *constants); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 157 | |
| 158 | enum class YieldResult |
| 159 | { |
| 160 | ControlBarrier, |
| 161 | }; |
| 162 | |
Nicolas Capens | 78cc4f4 | 2020-04-08 23:44:07 -0400 | [diff] [blame] | 163 | class Type; |
| 164 | class Object; |
| 165 | |
| 166 | // Pseudo-iterator over SPIRV instructions, designed to support range-based-for. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 167 | class InsnIterator |
| 168 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 169 | public: |
Nicolas Capens | 78cc4f4 | 2020-04-08 23:44:07 -0400 | [diff] [blame] | 170 | InsnIterator(InsnIterator const &other) = default; |
| 171 | |
| 172 | InsnIterator() = default; |
| 173 | |
| 174 | explicit InsnIterator(InsnStore::const_iterator iter) |
| 175 | : iter{ iter } |
| 176 | { |
| 177 | } |
| 178 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 179 | spv::Op opcode() const |
| 180 | { |
| 181 | return static_cast<spv::Op>(*iter & spv::OpCodeMask); |
| 182 | } |
| 183 | |
| 184 | uint32_t wordCount() const |
| 185 | { |
| 186 | return *iter >> spv::WordCountShift; |
| 187 | } |
| 188 | |
| 189 | uint32_t word(uint32_t n) const |
| 190 | { |
| 191 | ASSERT(n < wordCount()); |
| 192 | return iter[n]; |
| 193 | } |
| 194 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 195 | uint32_t const *wordPointer(uint32_t n) const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 196 | { |
| 197 | ASSERT(n < wordCount()); |
| 198 | return &iter[n]; |
| 199 | } |
| 200 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 201 | const char *string(uint32_t n) const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 202 | { |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 203 | return reinterpret_cast<const char *>(wordPointer(n)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 204 | } |
| 205 | |
Nicolas Capens | 78cc4f4 | 2020-04-08 23:44:07 -0400 | [diff] [blame] | 206 | bool hasResultAndType() const |
| 207 | { |
| 208 | bool hasResult = false, hasResultType = false; |
| 209 | spv::HasResultAndType(opcode(), &hasResult, &hasResultType); |
| 210 | |
| 211 | return hasResultType; |
| 212 | } |
| 213 | |
| 214 | SpirvID<Type> resultTypeId() const |
| 215 | { |
| 216 | ASSERT(hasResultAndType()); |
| 217 | return word(1); |
| 218 | } |
| 219 | |
| 220 | SpirvID<Object> resultId() const |
| 221 | { |
| 222 | ASSERT(hasResultAndType()); |
| 223 | return word(2); |
| 224 | } |
| 225 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 226 | bool operator==(InsnIterator const &other) const |
| 227 | { |
| 228 | return iter == other.iter; |
| 229 | } |
| 230 | |
| 231 | bool operator!=(InsnIterator const &other) const |
| 232 | { |
| 233 | return iter != other.iter; |
| 234 | } |
| 235 | |
| 236 | InsnIterator operator*() const |
| 237 | { |
| 238 | return *this; |
| 239 | } |
| 240 | |
| 241 | InsnIterator &operator++() |
| 242 | { |
| 243 | iter += wordCount(); |
| 244 | return *this; |
| 245 | } |
| 246 | |
| 247 | InsnIterator const operator++(int) |
| 248 | { |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 249 | InsnIterator ret{ *this }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 250 | iter += wordCount(); |
| 251 | return ret; |
| 252 | } |
| 253 | |
Nicolas Capens | 78cc4f4 | 2020-04-08 23:44:07 -0400 | [diff] [blame] | 254 | private: |
| 255 | InsnStore::const_iterator iter; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | /* range-based-for interface */ |
| 259 | InsnIterator begin() const |
| 260 | { |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 261 | return InsnIterator{ insns.cbegin() + 5 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | InsnIterator end() const |
| 265 | { |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 266 | return InsnIterator{ insns.cend() }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | class Type |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 270 | { |
| 271 | public: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 272 | using ID = SpirvID<Type>; |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 273 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 274 | spv::Op opcode() const { return definition.opcode(); } |
| 275 | |
| 276 | InsnIterator definition; |
| 277 | spv::StorageClass storageClass = static_cast<spv::StorageClass>(-1); |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 278 | uint32_t componentCount = 0; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 279 | bool isBuiltInBlock = false; |
| 280 | |
| 281 | // Inner element type for pointers, arrays, vectors and matrices. |
| 282 | ID element; |
| 283 | }; |
| 284 | |
| 285 | class Object |
| 286 | { |
| 287 | public: |
| 288 | using ID = SpirvID<Object>; |
| 289 | |
| 290 | spv::Op opcode() const { return definition.opcode(); } |
Nicolas Capens | 78cc4f4 | 2020-04-08 23:44:07 -0400 | [diff] [blame] | 291 | Type::ID typeId() const { return definition.resultTypeId(); } |
| 292 | Object::ID id() const { return definition.resultId(); } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 293 | |
| 294 | InsnIterator definition; |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 295 | std::vector<uint32_t> constantValue; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 296 | |
| 297 | enum class Kind |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 298 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 299 | // Invalid default kind. |
| 300 | // If we get left with an object in this state, the module was |
| 301 | // broken. |
| 302 | Unknown, |
| 303 | |
| 304 | // TODO: Better document this kind. |
| 305 | // A shader interface variable pointer. |
| 306 | // Pointer with uniform address across all lanes. |
| 307 | // Pointer held by SpirvRoutine::pointers |
| 308 | InterfaceVariable, |
| 309 | |
| 310 | // Constant value held by Object::constantValue. |
| 311 | Constant, |
| 312 | |
| 313 | // Value held by SpirvRoutine::intermediates. |
| 314 | Intermediate, |
| 315 | |
| 316 | // Pointer held by SpirvRoutine::pointers |
| 317 | Pointer, |
| 318 | |
| 319 | // A pointer to a vk::DescriptorSet*. |
| 320 | // Pointer held by SpirvRoutine::pointers. |
| 321 | DescriptorSet, |
| 322 | }; |
| 323 | |
| 324 | Kind kind = Kind::Unknown; |
| 325 | }; |
| 326 | |
| 327 | // Block is an interval of SPIR-V instructions, starting with the |
| 328 | // opening OpLabel, and ending with a termination instruction. |
| 329 | class Block |
| 330 | { |
| 331 | public: |
| 332 | using ID = SpirvID<Block>; |
| 333 | using Set = std::unordered_set<ID>; |
| 334 | |
| 335 | // Edge represents the graph edge between two blocks. |
| 336 | struct Edge |
| 337 | { |
| 338 | ID from; |
| 339 | ID to; |
| 340 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 341 | bool operator==(const Edge &other) const { return from == other.from && to == other.to; } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 342 | |
| 343 | struct Hash |
| 344 | { |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 345 | std::size_t operator()(const Edge &edge) const noexcept |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 346 | { |
| 347 | return std::hash<uint32_t>()(edge.from.value() * 31 + edge.to.value()); |
| 348 | } |
| 349 | }; |
| 350 | }; |
| 351 | |
| 352 | Block() = default; |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 353 | Block(const Block &other) = default; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 354 | explicit Block(InsnIterator begin, InsnIterator end); |
| 355 | |
| 356 | /* range-based-for interface */ |
| 357 | inline InsnIterator begin() const { return begin_; } |
| 358 | inline InsnIterator end() const { return end_; } |
| 359 | |
| 360 | enum Kind |
| 361 | { |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 362 | Simple, // OpBranch or other simple terminator. |
| 363 | StructuredBranchConditional, // OpSelectionMerge + OpBranchConditional |
| 364 | UnstructuredBranchConditional, // OpBranchConditional |
| 365 | StructuredSwitch, // OpSelectionMerge + OpSwitch |
| 366 | UnstructuredSwitch, // OpSwitch |
| 367 | Loop, // OpLoopMerge + [OpBranchConditional | OpBranch] |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | Kind kind = Simple; |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 371 | InsnIterator mergeInstruction; // Structured control flow merge instruction. |
| 372 | InsnIterator branchInstruction; // Branch instruction. |
| 373 | ID mergeBlock; // Structured flow merge block. |
| 374 | ID continueTarget; // Loop continue block. |
| 375 | Set ins; // Blocks that branch into this block. |
| 376 | Set outs; // Blocks that this block branches to. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 377 | bool isLoopMerge = false; |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 378 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 379 | private: |
| 380 | InsnIterator begin_; |
| 381 | InsnIterator end_; |
| 382 | }; |
| 383 | |
| 384 | class Function |
| 385 | { |
| 386 | public: |
| 387 | using ID = SpirvID<Function>; |
| 388 | |
| 389 | // Walks all reachable the blocks starting from id adding them to |
| 390 | // reachable. |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 391 | void TraverseReachableBlocks(Block::ID id, Block::Set &reachable) const; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 392 | |
| 393 | // AssignBlockFields() performs the following for all reachable blocks: |
| 394 | // * Assigns Block::ins with the identifiers of all blocks that contain |
| 395 | // this block in their Block::outs. |
| 396 | // * Sets Block::isLoopMerge to true if the block is the merge of a |
| 397 | // another loop block. |
| 398 | void AssignBlockFields(); |
| 399 | |
| 400 | // ForeachBlockDependency calls f with each dependency of the given |
| 401 | // block. A dependency is an incoming block that is not a loop-back |
| 402 | // edge. |
| 403 | void ForeachBlockDependency(Block::ID blockId, std::function<void(Block::ID)> f) const; |
| 404 | |
| 405 | // ExistsPath returns true if there's a direct or indirect flow from |
| 406 | // the 'from' block to the 'to' block that does not pass through |
| 407 | // notPassingThrough. |
| 408 | bool ExistsPath(Block::ID from, Block::ID to, Block::ID notPassingThrough) const; |
| 409 | |
| 410 | Block const &getBlock(Block::ID id) const |
| 411 | { |
| 412 | auto it = blocks.find(id); |
| 413 | ASSERT_MSG(it != blocks.end(), "Unknown block %d", id.value()); |
| 414 | return it->second; |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 415 | } |
| 416 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 417 | Block::ID entry; // function entry point block. |
| 418 | HandleMap<Block> blocks; // blocks belonging to this function. |
| 419 | Type::ID type; // type of the function. |
| 420 | Type::ID result; // return type. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 421 | }; |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 422 | |
Ben Clayton | 9c8823a | 2020-01-08 12:07:30 +0000 | [diff] [blame] | 423 | using String = std::string; |
| 424 | using StringID = SpirvID<std::string>; |
| 425 | |
Ben Clayton | b36dbbe | 2020-01-08 12:18:43 +0000 | [diff] [blame] | 426 | class Extension |
| 427 | { |
| 428 | public: |
| 429 | using ID = SpirvID<Extension>; |
| 430 | |
| 431 | enum Name |
| 432 | { |
| 433 | Unknown, |
| 434 | GLSLstd450, |
Ben Clayton | cd55f05 | 2020-01-14 11:56:00 +0000 | [diff] [blame] | 435 | OpenCLDebugInfo100 |
Ben Clayton | b36dbbe | 2020-01-08 12:18:43 +0000 | [diff] [blame] | 436 | }; |
| 437 | |
| 438 | Name name; |
| 439 | }; |
| 440 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 441 | struct TypeOrObject |
| 442 | {}; // Dummy struct to represent a Type or Object. |
Ben Clayton | 093be46 | 2019-03-08 08:37:24 +0000 | [diff] [blame] | 443 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 444 | // TypeOrObjectID is an identifier that represents a Type or an Object, |
| 445 | // and supports implicit casting to and from Type::ID or Object::ID. |
| 446 | class TypeOrObjectID : public SpirvID<TypeOrObject> |
| 447 | { |
| 448 | public: |
| 449 | using Hash = std::hash<SpirvID<TypeOrObject>>; |
| 450 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 451 | inline TypeOrObjectID(uint32_t id) |
| 452 | : SpirvID(id) |
| 453 | {} |
| 454 | inline TypeOrObjectID(Type::ID id) |
| 455 | : SpirvID(id.value()) |
| 456 | {} |
| 457 | inline TypeOrObjectID(Object::ID id) |
| 458 | : SpirvID(id.value()) |
| 459 | {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 460 | inline operator Type::ID() const { return Type::ID(value()); } |
| 461 | inline operator Object::ID() const { return Object::ID(value()); } |
| 462 | }; |
| 463 | |
| 464 | // OpImageSample variants |
| 465 | enum Variant |
| 466 | { |
| 467 | None, // No Dref or Proj. Also used by OpImageFetch and OpImageQueryLod. |
| 468 | Dref, |
| 469 | Proj, |
| 470 | ProjDref, |
| 471 | VARIANT_LAST = ProjDref |
| 472 | }; |
| 473 | |
| 474 | // Compact representation of image instruction parameters that is passed to the |
| 475 | // trampoline function for retrieving/generating the corresponding sampling routine. |
| 476 | struct ImageInstruction |
| 477 | { |
| 478 | ImageInstruction(Variant variant, SamplerMethod samplerMethod) |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 479 | : parameters(0) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 480 | { |
| 481 | this->variant = variant; |
| 482 | this->samplerMethod = samplerMethod; |
| 483 | } |
| 484 | |
| 485 | // Unmarshal from raw 32-bit data |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 486 | ImageInstruction(uint32_t parameters) |
| 487 | : parameters(parameters) |
| 488 | {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 489 | |
| 490 | SamplerFunction getSamplerFunction() const |
| 491 | { |
| 492 | return { static_cast<SamplerMethod>(samplerMethod), offset != 0, sample != 0 }; |
| 493 | } |
| 494 | |
| 495 | bool isDref() const |
| 496 | { |
| 497 | return (variant == Dref) || (variant == ProjDref); |
| 498 | } |
| 499 | |
| 500 | bool isProj() const |
| 501 | { |
| 502 | return (variant == Proj) || (variant == ProjDref); |
| 503 | } |
| 504 | |
| 505 | union |
| 506 | { |
| 507 | struct |
| 508 | { |
| 509 | uint32_t variant : BITS(VARIANT_LAST); |
| 510 | uint32_t samplerMethod : BITS(SAMPLER_METHOD_LAST); |
| 511 | uint32_t gatherComponent : 2; |
| 512 | |
| 513 | // Parameters are passed to the sampling routine in this order: |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 514 | uint32_t coordinates : 3; // 1-4 (does not contain projection component) |
| 515 | // uint32_t dref : 1; // Indicated by Variant::ProjDref|Dref |
| 516 | // uint32_t lodOrBias : 1; // Indicated by SamplerMethod::Lod|Bias|Fetch |
| 517 | uint32_t grad : 2; // 0-3 components (for each of dx / dy) |
| 518 | uint32_t offset : 2; // 0-3 components |
| 519 | uint32_t sample : 1; // 0-1 scalar integer |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 520 | }; |
| 521 | |
| 522 | uint32_t parameters; |
| 523 | }; |
| 524 | }; |
| 525 | |
| 526 | static_assert(sizeof(ImageInstruction) == sizeof(uint32_t), "ImageInstruction must be 32-bit"); |
| 527 | |
| 528 | // This method is for retrieving an ID that uniquely identifies the |
| 529 | // shader entry point represented by this object. |
| 530 | uint64_t getSerialID() const |
| 531 | { |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 532 | return ((uint64_t)entryPoint.value() << 32) | codeSerialID; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | SpirvShader(uint32_t codeSerialID, |
| 536 | VkShaderStageFlagBits stage, |
| 537 | const char *entryPointName, |
| 538 | InsnStore const &insns, |
| 539 | const vk::RenderPass *renderPass, |
| 540 | uint32_t subpassIndex, |
Ben Clayton | 7d0ce41 | 2019-12-03 13:26:31 +0000 | [diff] [blame] | 541 | bool robustBufferAccess, |
| 542 | const std::shared_ptr<vk::dbg::Context> &dbgctx); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 543 | |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 544 | ~SpirvShader(); |
| 545 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 546 | struct Modes |
| 547 | { |
| 548 | bool EarlyFragmentTests : 1; |
| 549 | bool DepthReplacing : 1; |
| 550 | bool DepthGreater : 1; |
| 551 | bool DepthLess : 1; |
| 552 | bool DepthUnchanged : 1; |
| 553 | bool ContainsKill : 1; |
| 554 | bool ContainsControlBarriers : 1; |
| 555 | bool NeedsCentroid : 1; |
| 556 | |
| 557 | // Compute workgroup dimensions |
Nicolas Capens | 57eb48a | 2020-05-15 17:07:07 -0400 | [diff] [blame] | 558 | int WorkgroupSizeX = 1; |
| 559 | int WorkgroupSizeY = 1; |
| 560 | int WorkgroupSizeZ = 1; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 561 | }; |
| 562 | |
| 563 | Modes const &getModes() const |
| 564 | { |
| 565 | return modes; |
| 566 | } |
| 567 | |
| 568 | struct Capabilities |
| 569 | { |
| 570 | bool Matrix : 1; |
| 571 | bool Shader : 1; |
| 572 | bool ClipDistance : 1; |
| 573 | bool CullDistance : 1; |
| 574 | bool InputAttachment : 1; |
| 575 | bool Sampled1D : 1; |
| 576 | bool Image1D : 1; |
| 577 | bool ImageCubeArray : 1; |
| 578 | bool SampledBuffer : 1; |
| 579 | bool SampledCubeArray : 1; |
| 580 | bool ImageBuffer : 1; |
| 581 | bool StorageImageExtendedFormats : 1; |
| 582 | bool ImageQuery : 1; |
| 583 | bool DerivativeControl : 1; |
| 584 | bool GroupNonUniform : 1; |
| 585 | bool GroupNonUniformVote : 1; |
| 586 | bool GroupNonUniformBallot : 1; |
| 587 | bool GroupNonUniformShuffle : 1; |
| 588 | bool GroupNonUniformShuffleRelative : 1; |
| 589 | bool GroupNonUniformArithmetic : 1; |
| 590 | bool DeviceGroup : 1; |
| 591 | bool MultiView : 1; |
Alexis Hetu | 1ee36c9 | 2020-02-20 14:07:26 -0500 | [diff] [blame] | 592 | bool StencilExportEXT : 1; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 593 | }; |
| 594 | |
| 595 | Capabilities const &getUsedCapabilities() const |
| 596 | { |
| 597 | return capabilities; |
| 598 | } |
| 599 | |
| 600 | // getNumOutputClipDistances() returns the number of ClipDistances |
| 601 | // outputted by this shader. |
| 602 | unsigned int getNumOutputClipDistances() const |
| 603 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 604 | if(getUsedCapabilities().ClipDistance) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 605 | { |
| 606 | auto it = outputBuiltins.find(spv::BuiltInClipDistance); |
| 607 | if(it != outputBuiltins.end()) |
| 608 | { |
| 609 | return it->second.SizeInComponents; |
| 610 | } |
| 611 | } |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | // getNumOutputCullDistances() returns the number of CullDistances |
| 616 | // outputted by this shader. |
| 617 | unsigned int getNumOutputCullDistances() const |
| 618 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 619 | if(getUsedCapabilities().CullDistance) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 620 | { |
| 621 | auto it = outputBuiltins.find(spv::BuiltInCullDistance); |
| 622 | if(it != outputBuiltins.end()) |
| 623 | { |
| 624 | return it->second.SizeInComponents; |
| 625 | } |
| 626 | } |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | enum AttribType : unsigned char |
| 631 | { |
| 632 | ATTRIBTYPE_FLOAT, |
| 633 | ATTRIBTYPE_INT, |
| 634 | ATTRIBTYPE_UINT, |
| 635 | ATTRIBTYPE_UNUSED, |
| 636 | |
| 637 | ATTRIBTYPE_LAST = ATTRIBTYPE_UINT |
| 638 | }; |
| 639 | |
| 640 | bool hasBuiltinInput(spv::BuiltIn b) const |
| 641 | { |
| 642 | return inputBuiltins.find(b) != inputBuiltins.end(); |
| 643 | } |
| 644 | |
| 645 | bool hasBuiltinOutput(spv::BuiltIn b) const |
| 646 | { |
| 647 | return outputBuiltins.find(b) != outputBuiltins.end(); |
| 648 | } |
| 649 | |
| 650 | struct Decorations |
| 651 | { |
| 652 | int32_t Location = -1; |
| 653 | int32_t Component = 0; |
| 654 | spv::BuiltIn BuiltIn = static_cast<spv::BuiltIn>(-1); |
| 655 | int32_t Offset = -1; |
| 656 | int32_t ArrayStride = -1; |
| 657 | int32_t MatrixStride = 1; |
| 658 | |
| 659 | bool HasLocation : 1; |
| 660 | bool HasComponent : 1; |
| 661 | bool HasBuiltIn : 1; |
| 662 | bool HasOffset : 1; |
| 663 | bool HasArrayStride : 1; |
| 664 | bool HasMatrixStride : 1; |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 665 | bool HasRowMajor : 1; // whether RowMajor bit is valid. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 666 | |
| 667 | bool Flat : 1; |
| 668 | bool Centroid : 1; |
| 669 | bool NoPerspective : 1; |
| 670 | bool Block : 1; |
| 671 | bool BufferBlock : 1; |
| 672 | bool RelaxedPrecision : 1; |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 673 | bool RowMajor : 1; // RowMajor if true; ColMajor if false |
| 674 | bool InsideMatrix : 1; // pseudo-decoration for whether we're inside a matrix. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 675 | |
| 676 | Decorations() |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 677 | : Location{ -1 } |
| 678 | , Component{ 0 } |
| 679 | , BuiltIn{ static_cast<spv::BuiltIn>(-1) } |
| 680 | , Offset{ -1 } |
| 681 | , ArrayStride{ -1 } |
| 682 | , MatrixStride{ -1 } |
| 683 | , HasLocation{ false } |
| 684 | , HasComponent{ false } |
| 685 | , HasBuiltIn{ false } |
| 686 | , HasOffset{ false } |
| 687 | , HasArrayStride{ false } |
| 688 | , HasMatrixStride{ false } |
| 689 | , HasRowMajor{ false } |
| 690 | , Flat{ false } |
| 691 | , Centroid{ false } |
| 692 | , NoPerspective{ false } |
| 693 | , Block{ false } |
| 694 | , BufferBlock{ false } |
| 695 | , RelaxedPrecision{ false } |
| 696 | , RowMajor{ false } |
| 697 | , InsideMatrix{ false } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 698 | { |
| 699 | } |
| 700 | |
| 701 | Decorations(Decorations const &) = default; |
| 702 | |
| 703 | void Apply(Decorations const &src); |
| 704 | |
| 705 | void Apply(spv::Decoration decoration, uint32_t arg); |
| 706 | }; |
| 707 | |
| 708 | std::unordered_map<TypeOrObjectID, Decorations, TypeOrObjectID::Hash> decorations; |
| 709 | std::unordered_map<Type::ID, std::vector<Decorations>> memberDecorations; |
| 710 | |
| 711 | struct DescriptorDecorations |
| 712 | { |
| 713 | int32_t DescriptorSet = -1; |
| 714 | int32_t Binding = -1; |
| 715 | int32_t InputAttachmentIndex = -1; |
| 716 | |
| 717 | void Apply(DescriptorDecorations const &src); |
| 718 | }; |
| 719 | |
| 720 | std::unordered_map<Object::ID, DescriptorDecorations> descriptorDecorations; |
| 721 | std::vector<VkFormat> inputAttachmentFormats; |
| 722 | |
| 723 | struct InterfaceComponent |
| 724 | { |
| 725 | AttribType Type; |
| 726 | |
| 727 | union |
| 728 | { |
| 729 | struct |
| 730 | { |
| 731 | bool Flat : 1; |
| 732 | bool Centroid : 1; |
| 733 | bool NoPerspective : 1; |
| 734 | }; |
| 735 | |
| 736 | uint8_t DecorationBits; |
| 737 | }; |
| 738 | |
| 739 | InterfaceComponent() |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 740 | : Type{ ATTRIBTYPE_UNUSED } |
| 741 | , DecorationBits{ 0 } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 742 | { |
| 743 | } |
| 744 | }; |
| 745 | |
| 746 | struct BuiltinMapping |
| 747 | { |
| 748 | Object::ID Id; |
| 749 | uint32_t FirstComponent; |
| 750 | uint32_t SizeInComponents; |
| 751 | }; |
| 752 | |
| 753 | struct WorkgroupMemory |
| 754 | { |
| 755 | // allocates a new variable of size bytes with the given identifier. |
| 756 | inline void allocate(Object::ID id, uint32_t size) |
| 757 | { |
| 758 | uint32_t offset = totalSize; |
| 759 | auto it = offsets.emplace(id, offset); |
| 760 | ASSERT_MSG(it.second, "WorkgroupMemory already has an allocation for object %d", int(id.value())); |
| 761 | totalSize += size; |
| 762 | } |
| 763 | // returns the byte offset of the variable with the given identifier. |
| 764 | inline uint32_t offsetOf(Object::ID id) const |
| 765 | { |
| 766 | auto it = offsets.find(id); |
| 767 | ASSERT_MSG(it != offsets.end(), "WorkgroupMemory has no allocation for object %d", int(id.value())); |
| 768 | return it->second; |
| 769 | } |
| 770 | // returns the total allocated size in bytes. |
| 771 | inline uint32_t size() const { return totalSize; } |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 772 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 773 | private: |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 774 | uint32_t totalSize = 0; // in bytes |
| 775 | std::unordered_map<Object::ID, uint32_t> offsets; // in bytes |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 776 | }; |
| 777 | |
| 778 | std::vector<InterfaceComponent> inputs; |
| 779 | std::vector<InterfaceComponent> outputs; |
| 780 | |
| 781 | void emitProlog(SpirvRoutine *routine) const; |
| 782 | void emit(SpirvRoutine *routine, RValue<SIMD::Int> const &activeLaneMask, RValue<SIMD::Int> const &storesAndAtomicsMask, const vk::DescriptorSet::Bindings &descriptorSets) const; |
| 783 | void emitEpilog(SpirvRoutine *routine) const; |
| 784 | |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 785 | bool containsImageWrite() const { return imageWriteEmitted; } |
| 786 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 787 | using BuiltInHash = std::hash<std::underlying_type<spv::BuiltIn>::type>; |
| 788 | std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> inputBuiltins; |
| 789 | std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> outputBuiltins; |
| 790 | WorkgroupMemory workgroupMemory; |
| 791 | |
| 792 | private: |
| 793 | const uint32_t codeSerialID; |
| 794 | Modes modes = {}; |
| 795 | Capabilities capabilities = {}; |
| 796 | HandleMap<Type> types; |
| 797 | HandleMap<Object> defs; |
| 798 | HandleMap<Function> functions; |
Ben Clayton | 9c8823a | 2020-01-08 12:07:30 +0000 | [diff] [blame] | 799 | std::unordered_map<StringID, String> strings; |
Ben Clayton | 8842c8f | 2020-01-13 16:57:48 +0000 | [diff] [blame] | 800 | HandleMap<Extension> extensionsByID; |
| 801 | std::unordered_set<Extension::Name> extensionsImported; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 802 | Function::ID entryPoint; |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 803 | mutable bool imageWriteEmitted = false; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 804 | |
| 805 | const bool robustBufferAccess = true; |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 806 | spv::ExecutionModel executionModel = spv::ExecutionModelMax; // Invalid prior to OpEntryPoint parsing. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 807 | |
| 808 | // DeclareType creates a Type for the given OpTypeX instruction, storing |
| 809 | // it into the types map. It is called from the analysis pass (constructor). |
| 810 | void DeclareType(InsnIterator insn); |
| 811 | |
| 812 | void ProcessExecutionMode(InsnIterator it); |
| 813 | |
| 814 | uint32_t ComputeTypeSize(InsnIterator insn); |
| 815 | void ApplyDecorationsForId(Decorations *d, TypeOrObjectID id) const; |
| 816 | void ApplyDecorationsForIdMember(Decorations *d, Type::ID id, uint32_t member) const; |
| 817 | void ApplyDecorationsForAccessChain(Decorations *d, DescriptorDecorations *dd, Object::ID baseId, uint32_t numIndexes, uint32_t const *indexIds) const; |
| 818 | |
| 819 | // Creates an Object for the instruction's result in 'defs'. |
| 820 | void DefineResult(const InsnIterator &insn); |
| 821 | |
Ben Clayton | cd55f05 | 2020-01-14 11:56:00 +0000 | [diff] [blame] | 822 | // Processes the OpenCL.Debug.100 instruction for the initial definition |
| 823 | // pass of the SPIR-V. |
| 824 | void DefineOpenCLDebugInfo100(const InsnIterator &insn); |
| 825 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 826 | // Returns true if data in the given storage class is word-interleaved |
| 827 | // by each SIMD vector lane, otherwise data is stored linerally. |
| 828 | // |
| 829 | // Each lane addresses a single word, picked by a base pointer and an |
| 830 | // integer offset. |
| 831 | // |
| 832 | // A word is currently 32 bits (single float, int32_t, uint32_t). |
| 833 | // A lane is a single element of a SIMD vector register. |
| 834 | // |
| 835 | // Storage interleaved by lane - (IsStorageInterleavedByLane() == true): |
| 836 | // --------------------------------------------------------------------- |
| 837 | // |
| 838 | // Address = PtrBase + sizeof(Word) * (SIMD::Width * LaneOffset + LaneIndex) |
| 839 | // |
| 840 | // Assuming SIMD::Width == 4: |
| 841 | // |
| 842 | // Lane[0] | Lane[1] | Lane[2] | Lane[3] |
| 843 | // ===========+===========+===========+========== |
| 844 | // LaneOffset=0: | Word[0] | Word[1] | Word[2] | Word[3] |
| 845 | // ---------------+-----------+-----------+-----------+---------- |
| 846 | // LaneOffset=1: | Word[4] | Word[5] | Word[6] | Word[7] |
| 847 | // ---------------+-----------+-----------+-----------+---------- |
| 848 | // LaneOffset=2: | Word[8] | Word[9] | Word[a] | Word[b] |
| 849 | // ---------------+-----------+-----------+-----------+---------- |
| 850 | // LaneOffset=3: | Word[c] | Word[d] | Word[e] | Word[f] |
| 851 | // |
| 852 | // |
| 853 | // Linear storage - (IsStorageInterleavedByLane() == false): |
| 854 | // --------------------------------------------------------- |
| 855 | // |
| 856 | // Address = PtrBase + sizeof(Word) * LaneOffset |
| 857 | // |
| 858 | // Lane[0] | Lane[1] | Lane[2] | Lane[3] |
| 859 | // ===========+===========+===========+========== |
| 860 | // LaneOffset=0: | Word[0] | Word[0] | Word[0] | Word[0] |
| 861 | // ---------------+-----------+-----------+-----------+---------- |
| 862 | // LaneOffset=1: | Word[1] | Word[1] | Word[1] | Word[1] |
| 863 | // ---------------+-----------+-----------+-----------+---------- |
| 864 | // LaneOffset=2: | Word[2] | Word[2] | Word[2] | Word[2] |
| 865 | // ---------------+-----------+-----------+-----------+---------- |
| 866 | // LaneOffset=3: | Word[3] | Word[3] | Word[3] | Word[3] |
| 867 | // |
| 868 | static bool IsStorageInterleavedByLane(spv::StorageClass storageClass); |
| 869 | static bool IsExplicitLayout(spv::StorageClass storageClass); |
| 870 | |
| 871 | static sw::SIMD::Pointer InterleaveByLane(sw::SIMD::Pointer p); |
| 872 | |
| 873 | // Output storage buffers and images should not be affected by helper invocations |
| 874 | static bool StoresInHelperInvocation(spv::StorageClass storageClass); |
| 875 | |
| 876 | using InterfaceVisitor = std::function<void(Decorations const, AttribType)>; |
| 877 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 878 | void VisitInterface(Object::ID id, const InterfaceVisitor &v) const; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 879 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 880 | int VisitInterfaceInner(Type::ID id, Decorations d, const InterfaceVisitor &v) const; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 881 | |
| 882 | // MemoryElement describes a scalar element within a structure, and is |
| 883 | // used by the callback function of VisitMemoryObject(). |
| 884 | struct MemoryElement |
| 885 | { |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 886 | uint32_t index; // index of the scalar element |
| 887 | uint32_t offset; // offset (in bytes) from the base of the object |
| 888 | const Type &type; // element type |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 889 | }; |
| 890 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 891 | using MemoryVisitor = std::function<void(const MemoryElement &)>; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 892 | |
| 893 | // VisitMemoryObject() walks a type tree in an explicitly laid out |
| 894 | // storage class, calling the MemoryVisitor for each scalar element |
| 895 | // within the |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 896 | void VisitMemoryObject(Object::ID id, const MemoryVisitor &v) const; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 897 | |
| 898 | // VisitMemoryObjectInner() is internally called by VisitMemoryObject() |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 899 | void VisitMemoryObjectInner(Type::ID id, Decorations d, uint32_t &index, uint32_t offset, const MemoryVisitor &v) const; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 900 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 901 | Object &CreateConstant(InsnIterator it); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 902 | |
| 903 | void ProcessInterfaceVariable(Object &object); |
| 904 | |
| 905 | // EmitState holds control-flow state for the emit() pass. |
| 906 | class EmitState |
| 907 | { |
| 908 | public: |
| 909 | EmitState(SpirvRoutine *routine, |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 910 | Function::ID function, |
| 911 | RValue<SIMD::Int> activeLaneMask, |
| 912 | RValue<SIMD::Int> storesAndAtomicsMask, |
| 913 | const vk::DescriptorSet::Bindings &descriptorSets, |
| 914 | bool robustBufferAccess, |
| 915 | spv::ExecutionModel executionModel) |
| 916 | : routine(routine) |
| 917 | , function(function) |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 918 | , activeLaneMaskValue(activeLaneMask.value()) |
| 919 | , storesAndAtomicsMaskValue(storesAndAtomicsMask.value()) |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 920 | , descriptorSets(descriptorSets) |
| 921 | , robustBufferAccess(robustBufferAccess) |
| 922 | , executionModel(executionModel) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 923 | { |
| 924 | ASSERT(executionModelToStage(executionModel) != VkShaderStageFlagBits(0)); // Must parse OpEntryPoint before emitting. |
| 925 | } |
| 926 | |
Ben Clayton | 7483e56 | 2020-04-24 23:00:40 +0100 | [diff] [blame] | 927 | // Returns the mask describing the active lanes as updated by dynamic |
| 928 | // control flow. Active lanes include helper invocations, used for |
| 929 | // calculating fragment derivitives, which must not perform memory |
| 930 | // stores or atomic writes. |
| 931 | // |
| 932 | // Use activeStoresAndAtomicsMask() to consider both control flow and |
| 933 | // lanes which are permitted to perform memory stores and atomic |
| 934 | // operations |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 935 | RValue<SIMD::Int> activeLaneMask() const |
| 936 | { |
| 937 | ASSERT(activeLaneMaskValue != nullptr); |
| 938 | return RValue<SIMD::Int>(activeLaneMaskValue); |
| 939 | } |
| 940 | |
Ben Clayton | 7483e56 | 2020-04-24 23:00:40 +0100 | [diff] [blame] | 941 | // Returns the immutable lane mask that describes which lanes are |
| 942 | // permitted to perform memory stores and atomic operations. |
| 943 | // Note that unlike activeStoresAndAtomicsMask() this mask *does not* |
| 944 | // consider lanes that have been made inactive due to control flow. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 945 | RValue<SIMD::Int> storesAndAtomicsMask() const |
| 946 | { |
| 947 | ASSERT(storesAndAtomicsMaskValue != nullptr); |
| 948 | return RValue<SIMD::Int>(storesAndAtomicsMaskValue); |
| 949 | } |
| 950 | |
Ben Clayton | 7483e56 | 2020-04-24 23:00:40 +0100 | [diff] [blame] | 951 | // Returns a lane mask that describes which lanes are permitted to |
| 952 | // perform memory stores and atomic operations, considering lanes that |
| 953 | // may have been made inactive due to control flow. |
| 954 | RValue<SIMD::Int> activeStoresAndAtomicsMask() const |
| 955 | { |
| 956 | return activeLaneMask() & storesAndAtomicsMask(); |
| 957 | } |
| 958 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 959 | // Add a new active lane mask edge from the current block to out. |
| 960 | // The edge mask value will be (mask AND activeLaneMaskValue). |
| 961 | // If multiple active lane masks are added for the same edge, then |
| 962 | // they will be ORed together. |
| 963 | void addOutputActiveLaneMaskEdge(Block::ID out, RValue<SIMD::Int> mask); |
| 964 | |
| 965 | // Add a new active lane mask for the edge from -> to. |
| 966 | // If multiple active lane masks are added for the same edge, then |
| 967 | // they will be ORed together. |
| 968 | void addActiveLaneMaskEdge(Block::ID from, Block::ID to, RValue<SIMD::Int> mask); |
| 969 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 970 | SpirvRoutine *routine = nullptr; // The current routine being built. |
| 971 | Function::ID function; // The current function being built. |
| 972 | Block::ID block; // The current block being built. |
| 973 | rr::Value *activeLaneMaskValue = nullptr; // The current active lane mask. |
| 974 | rr::Value *storesAndAtomicsMaskValue = nullptr; // The current atomics mask. |
| 975 | Block::Set visited; // Blocks already built. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 976 | std::unordered_map<Block::Edge, RValue<SIMD::Int>, Block::Edge::Hash> edgeActiveLaneMasks; |
| 977 | std::deque<Block::ID> *pending; |
| 978 | |
| 979 | const vk::DescriptorSet::Bindings &descriptorSets; |
| 980 | |
| 981 | OutOfBoundsBehavior getOutOfBoundsBehavior(spv::StorageClass storageClass) const; |
| 982 | |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 983 | Intermediate &createIntermediate(Object::ID id, uint32_t componentCount) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 984 | { |
| 985 | auto it = intermediates.emplace(std::piecewise_construct, |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 986 | std::forward_as_tuple(id), |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 987 | std::forward_as_tuple(componentCount)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 988 | ASSERT_MSG(it.second, "Intermediate %d created twice", id.value()); |
| 989 | return it.first->second; |
| 990 | } |
| 991 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 992 | Intermediate const &getIntermediate(Object::ID id) const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 993 | { |
| 994 | auto it = intermediates.find(id); |
| 995 | ASSERT_MSG(it != intermediates.end(), "Unknown intermediate %d", id.value()); |
| 996 | return it->second; |
| 997 | } |
| 998 | |
| 999 | void createPointer(Object::ID id, SIMD::Pointer ptr) |
| 1000 | { |
| 1001 | bool added = pointers.emplace(id, ptr).second; |
| 1002 | ASSERT_MSG(added, "Pointer %d created twice", id.value()); |
| 1003 | } |
| 1004 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1005 | SIMD::Pointer const &getPointer(Object::ID id) const |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1006 | { |
| 1007 | auto it = pointers.find(id); |
| 1008 | ASSERT_MSG(it != pointers.end(), "Unknown pointer %d", id.value()); |
| 1009 | return it->second; |
| 1010 | } |
| 1011 | |
| 1012 | private: |
| 1013 | std::unordered_map<Object::ID, Intermediate> intermediates; |
| 1014 | std::unordered_map<Object::ID, SIMD::Pointer> pointers; |
| 1015 | |
| 1016 | const bool robustBufferAccess = true; // Emit robustBufferAccess safe code. |
| 1017 | const spv::ExecutionModel executionModel = spv::ExecutionModelMax; |
| 1018 | }; |
| 1019 | |
| 1020 | // EmitResult is an enumerator of result values from the Emit functions. |
| 1021 | enum class EmitResult |
| 1022 | { |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1023 | Continue, // No termination instructions. |
| 1024 | Terminator, // Reached a termination instruction. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1025 | }; |
| 1026 | |
| 1027 | // Generic wrapper over either per-lane intermediate value, or a constant. |
| 1028 | // Constants are transparently widened to per-lane values in operator[]. |
| 1029 | // This is appropriate in most cases -- if we're not going to do something |
| 1030 | // significantly different based on whether the value is uniform across lanes. |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 1031 | class Operand |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1032 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1033 | public: |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 1034 | Operand(const SpirvShader *shader, const EmitState *state, SpirvShader::Object::ID objectId); |
Nicolas Capens | 20220a0 | 2020-04-09 02:48:16 -0400 | [diff] [blame] | 1035 | Operand(const Intermediate &value); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1036 | |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 1037 | RValue<SIMD::Float> Float(uint32_t i) const |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 1038 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1039 | if(intermediate) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1040 | { |
| 1041 | return intermediate->Float(i); |
| 1042 | } |
| 1043 | |
| 1044 | // Constructing a constant SIMD::Float is not guaranteed to preserve the data's exact |
| 1045 | // bit pattern, but SPIR-V provides 32-bit words representing "the bit pattern for the constant". |
| 1046 | // Thus we must first construct an integer constant, and bitcast to float. |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 1047 | return As<SIMD::Float>(SIMD::UInt(constant[i])); |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 1048 | } |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 1049 | |
| 1050 | RValue<SIMD::Int> Int(uint32_t i) const |
| 1051 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1052 | if(intermediate) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1053 | { |
| 1054 | return intermediate->Int(i); |
| 1055 | } |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 1056 | |
| 1057 | return SIMD::Int(constant[i]); |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | RValue<SIMD::UInt> UInt(uint32_t i) const |
| 1061 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1062 | if(intermediate) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1063 | { |
| 1064 | return intermediate->UInt(i); |
| 1065 | } |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 1066 | |
| 1067 | return SIMD::UInt(constant[i]); |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 1068 | } |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 1069 | |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 1070 | private: |
Ben Clayton | fc951cd | 2019-05-15 17:16:56 +0100 | [diff] [blame] | 1071 | RR_PRINT_ONLY(friend struct rr::PrintValue::Ty<Operand>;) |
| 1072 | |
Nicolas Capens | 2f4b603 | 2020-04-09 02:01:50 -0400 | [diff] [blame] | 1073 | // Delegate constructor |
| 1074 | Operand(const EmitState *state, const Object &object); |
| 1075 | |
| 1076 | const uint32_t *constant; |
| 1077 | const Intermediate *intermediate; |
| 1078 | |
| 1079 | public: |
| 1080 | const uint32_t componentCount; |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 1081 | }; |
| 1082 | |
Ben Clayton | fc951cd | 2019-05-15 17:16:56 +0100 | [diff] [blame] | 1083 | RR_PRINT_ONLY(friend struct rr::PrintValue::Ty<Operand>;) |
| 1084 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1085 | Type const &getType(Type::ID id) const |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 1086 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1087 | auto it = types.find(id); |
| 1088 | ASSERT_MSG(it != types.end(), "Unknown type %d", id.value()); |
| 1089 | return it->second; |
| 1090 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 1091 | |
Nicolas Capens | 78cc4f4 | 2020-04-08 23:44:07 -0400 | [diff] [blame] | 1092 | Type const &getType(const Object &object) const |
| 1093 | { |
| 1094 | return getType(object.typeId()); |
| 1095 | } |
| 1096 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1097 | Object const &getObject(Object::ID id) const |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1098 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1099 | auto it = defs.find(id); |
| 1100 | ASSERT_MSG(it != defs.end(), "Unknown object %d", id.value()); |
| 1101 | return it->second; |
| 1102 | } |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 1103 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1104 | Function const &getFunction(Function::ID id) const |
| 1105 | { |
| 1106 | auto it = functions.find(id); |
| 1107 | ASSERT_MSG(it != functions.end(), "Unknown function %d", id.value()); |
| 1108 | return it->second; |
| 1109 | } |
Ben Clayton | 24ea515 | 2019-02-26 11:02:42 +0000 | [diff] [blame] | 1110 | |
Ben Clayton | 9c8823a | 2020-01-08 12:07:30 +0000 | [diff] [blame] | 1111 | String const &getString(StringID id) const |
| 1112 | { |
| 1113 | auto it = strings.find(id); |
| 1114 | ASSERT_MSG(it != strings.end(), "Unknown string %d", id.value()); |
| 1115 | return it->second; |
| 1116 | } |
| 1117 | |
Ben Clayton | b36dbbe | 2020-01-08 12:18:43 +0000 | [diff] [blame] | 1118 | Extension const &getExtension(Extension::ID id) const |
| 1119 | { |
Ben Clayton | 8842c8f | 2020-01-13 16:57:48 +0000 | [diff] [blame] | 1120 | auto it = extensionsByID.find(id); |
| 1121 | ASSERT_MSG(it != extensionsByID.end(), "Unknown extension %d", id.value()); |
Ben Clayton | b36dbbe | 2020-01-08 12:18:43 +0000 | [diff] [blame] | 1122 | return it->second; |
| 1123 | } |
| 1124 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1125 | // Returns a SIMD::Pointer to the underlying data for the given pointer |
| 1126 | // object. |
| 1127 | // Handles objects of the following kinds: |
Nicolas Capens | 479d143 | 2020-01-31 11:19:21 -0500 | [diff] [blame] | 1128 | // - DescriptorSet |
| 1129 | // - Pointer |
| 1130 | // - InterfaceVariable |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1131 | // Calling GetPointerToData with objects of any other kind will assert. |
Nicolas Capens | 479d143 | 2020-01-31 11:19:21 -0500 | [diff] [blame] | 1132 | SIMD::Pointer GetPointerToData(Object::ID id, Int arrayIndex, EmitState const *state) const; |
Ben Clayton | 204a410 | 2019-07-31 13:17:47 +0100 | [diff] [blame] | 1133 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1134 | SIMD::Pointer WalkExplicitLayoutAccessChain(Object::ID id, uint32_t numIndexes, uint32_t const *indexIds, EmitState const *state) const; |
| 1135 | SIMD::Pointer WalkAccessChain(Object::ID id, uint32_t numIndexes, uint32_t const *indexIds, EmitState const *state) const; |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 1136 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1137 | // Returns the *component* offset in the literal for the given access chain. |
| 1138 | uint32_t WalkLiteralAccessChain(Type::ID id, uint32_t numIndexes, uint32_t const *indexes) const; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1139 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1140 | // Lookup the active lane mask for the edge from -> to. |
| 1141 | // If from is unreachable, then a mask of all zeros is returned. |
| 1142 | // Asserts if from is reachable and the edge does not exist. |
| 1143 | RValue<SIMD::Int> GetActiveLaneMaskEdge(EmitState *state, Block::ID from, Block::ID to) const; |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 1144 | |
Ben Clayton | 8a7067d | 2020-01-08 12:30:06 +0000 | [diff] [blame] | 1145 | // Updates the current active lane mask. |
| 1146 | void SetActiveLaneMask(RValue<SIMD::Int> mask, EmitState *state) const; |
| 1147 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1148 | // Emit all the unvisited blocks (except for ignore) in DFS order, |
| 1149 | // starting with id. |
| 1150 | void EmitBlocks(Block::ID id, EmitState *state, Block::ID ignore = 0) const; |
| 1151 | void EmitNonLoop(EmitState *state) const; |
| 1152 | void EmitLoop(EmitState *state) const; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1153 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1154 | void EmitInstructions(InsnIterator begin, InsnIterator end, EmitState *state) const; |
| 1155 | EmitResult EmitInstruction(InsnIterator insn, EmitState *state) const; |
Ben Clayton | ea3cd1b | 2019-06-19 21:43:55 +0100 | [diff] [blame] | 1156 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1157 | // Emit pass instructions: |
| 1158 | EmitResult EmitVariable(InsnIterator insn, EmitState *state) const; |
| 1159 | EmitResult EmitLoad(InsnIterator insn, EmitState *state) const; |
| 1160 | EmitResult EmitStore(InsnIterator insn, EmitState *state) const; |
| 1161 | EmitResult EmitAccessChain(InsnIterator insn, EmitState *state) const; |
| 1162 | EmitResult EmitCompositeConstruct(InsnIterator insn, EmitState *state) const; |
| 1163 | EmitResult EmitCompositeInsert(InsnIterator insn, EmitState *state) const; |
| 1164 | EmitResult EmitCompositeExtract(InsnIterator insn, EmitState *state) const; |
| 1165 | EmitResult EmitVectorShuffle(InsnIterator insn, EmitState *state) const; |
| 1166 | EmitResult EmitVectorTimesScalar(InsnIterator insn, EmitState *state) const; |
| 1167 | EmitResult EmitMatrixTimesVector(InsnIterator insn, EmitState *state) const; |
| 1168 | EmitResult EmitVectorTimesMatrix(InsnIterator insn, EmitState *state) const; |
| 1169 | EmitResult EmitMatrixTimesMatrix(InsnIterator insn, EmitState *state) const; |
| 1170 | EmitResult EmitOuterProduct(InsnIterator insn, EmitState *state) const; |
| 1171 | EmitResult EmitTranspose(InsnIterator insn, EmitState *state) const; |
| 1172 | EmitResult EmitVectorExtractDynamic(InsnIterator insn, EmitState *state) const; |
| 1173 | EmitResult EmitVectorInsertDynamic(InsnIterator insn, EmitState *state) const; |
| 1174 | EmitResult EmitUnaryOp(InsnIterator insn, EmitState *state) const; |
| 1175 | EmitResult EmitBinaryOp(InsnIterator insn, EmitState *state) const; |
| 1176 | EmitResult EmitDot(InsnIterator insn, EmitState *state) const; |
| 1177 | EmitResult EmitSelect(InsnIterator insn, EmitState *state) const; |
| 1178 | EmitResult EmitExtendedInstruction(InsnIterator insn, EmitState *state) const; |
Ben Clayton | b36dbbe | 2020-01-08 12:18:43 +0000 | [diff] [blame] | 1179 | EmitResult EmitExtGLSLstd450(InsnIterator insn, EmitState *state) const; |
Ben Clayton | cd55f05 | 2020-01-14 11:56:00 +0000 | [diff] [blame] | 1180 | EmitResult EmitOpenCLDebugInfo100(InsnIterator insn, EmitState *state) const; |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 1181 | EmitResult EmitLine(InsnIterator insn, EmitState *state) const; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1182 | EmitResult EmitAny(InsnIterator insn, EmitState *state) const; |
| 1183 | EmitResult EmitAll(InsnIterator insn, EmitState *state) const; |
| 1184 | EmitResult EmitBranch(InsnIterator insn, EmitState *state) const; |
| 1185 | EmitResult EmitBranchConditional(InsnIterator insn, EmitState *state) const; |
| 1186 | EmitResult EmitSwitch(InsnIterator insn, EmitState *state) const; |
| 1187 | EmitResult EmitUnreachable(InsnIterator insn, EmitState *state) const; |
| 1188 | EmitResult EmitReturn(InsnIterator insn, EmitState *state) const; |
| 1189 | EmitResult EmitKill(InsnIterator insn, EmitState *state) const; |
| 1190 | EmitResult EmitFunctionCall(InsnIterator insn, EmitState *state) const; |
| 1191 | EmitResult EmitPhi(InsnIterator insn, EmitState *state) const; |
| 1192 | EmitResult EmitImageSampleImplicitLod(Variant variant, InsnIterator insn, EmitState *state) const; |
| 1193 | EmitResult EmitImageSampleExplicitLod(Variant variant, InsnIterator insn, EmitState *state) const; |
| 1194 | EmitResult EmitImageGather(Variant variant, InsnIterator insn, EmitState *state) const; |
| 1195 | EmitResult EmitImageFetch(InsnIterator insn, EmitState *state) const; |
| 1196 | EmitResult EmitImageSample(ImageInstruction instruction, InsnIterator insn, EmitState *state) const; |
| 1197 | EmitResult EmitImageQuerySizeLod(InsnIterator insn, EmitState *state) const; |
| 1198 | EmitResult EmitImageQuerySize(InsnIterator insn, EmitState *state) const; |
| 1199 | EmitResult EmitImageQueryLod(InsnIterator insn, EmitState *state) const; |
| 1200 | EmitResult EmitImageQueryLevels(InsnIterator insn, EmitState *state) const; |
| 1201 | EmitResult EmitImageQuerySamples(InsnIterator insn, EmitState *state) const; |
| 1202 | EmitResult EmitImageRead(InsnIterator insn, EmitState *state) const; |
| 1203 | EmitResult EmitImageWrite(InsnIterator insn, EmitState *state) const; |
| 1204 | EmitResult EmitImageTexelPointer(InsnIterator insn, EmitState *state) const; |
| 1205 | EmitResult EmitAtomicOp(InsnIterator insn, EmitState *state) const; |
| 1206 | EmitResult EmitAtomicCompareExchange(InsnIterator insn, EmitState *state) const; |
| 1207 | EmitResult EmitSampledImageCombineOrSplit(InsnIterator insn, EmitState *state) const; |
| 1208 | EmitResult EmitCopyObject(InsnIterator insn, EmitState *state) const; |
| 1209 | EmitResult EmitCopyMemory(InsnIterator insn, EmitState *state) const; |
| 1210 | EmitResult EmitControlBarrier(InsnIterator insn, EmitState *state) const; |
| 1211 | EmitResult EmitMemoryBarrier(InsnIterator insn, EmitState *state) const; |
| 1212 | EmitResult EmitGroupNonUniform(InsnIterator insn, EmitState *state) const; |
| 1213 | EmitResult EmitArrayLength(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 30ee92e | 2019-08-13 14:21:44 +0100 | [diff] [blame] | 1214 | |
Nicolas Capens | 9b83ddb | 2020-04-06 15:54:03 -0400 | [diff] [blame] | 1215 | // Emits code to sample an image, regardless of whether any SIMD lanes are active. |
| 1216 | void EmitImageSampleUnconditional(Array<SIMD::Float> &out, ImageInstruction instruction, InsnIterator insn, EmitState *state) const; |
| 1217 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1218 | void GetImageDimensions(EmitState const *state, Type const &resultTy, Object::ID imageId, Object::ID lodId, Intermediate &dst) const; |
Nicolas Capens | 4a65df1 | 2020-05-07 10:06:46 -0400 | [diff] [blame] | 1219 | SIMD::Pointer GetTexelAddress(EmitState const *state, Pointer<Byte> imageBase, Int imageSizeInBytes, Operand const &coordinate, Type const &imageType, Pointer<Byte> descriptor, int texelSize, Object::ID sampleId, bool useStencilAspect, OutOfBoundsBehavior outOfBoundsBehavior) const; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1220 | uint32_t GetConstScalarInt(Object::ID id) const; |
| 1221 | void EvalSpecConstantOp(InsnIterator insn); |
| 1222 | void EvalSpecConstantUnaryOp(InsnIterator insn); |
| 1223 | void EvalSpecConstantBinaryOp(InsnIterator insn); |
Ben Clayton | 30ee92e | 2019-08-13 14:21:44 +0100 | [diff] [blame] | 1224 | |
Nicolas Capens | 0b77aa5 | 2020-04-09 02:48:16 -0400 | [diff] [blame] | 1225 | // Helper for implementing OpStore, which doesn't take an InsnIterator so it |
| 1226 | // can also store independent operands. |
| 1227 | void Store(Object::ID pointerId, const Operand &value, bool atomic, std::memory_order memoryOrder, EmitState *state) const; |
| 1228 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1229 | // LoadPhi loads the phi values from the alloca storage and places the |
| 1230 | // load values into the intermediate with the phi's result id. |
| 1231 | void LoadPhi(InsnIterator insn, EmitState *state) const; |
Ben Clayton | ea3cd1b | 2019-06-19 21:43:55 +0100 | [diff] [blame] | 1232 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1233 | // StorePhi updates the phi's alloca storage value using the incoming |
| 1234 | // values from blocks that are both in the OpPhi instruction and in |
| 1235 | // filter. |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1236 | void StorePhi(Block::ID blockID, InsnIterator insn, EmitState *state, std::unordered_set<SpirvShader::Block::ID> const &filter) const; |
Ben Clayton | ea3cd1b | 2019-06-19 21:43:55 +0100 | [diff] [blame] | 1237 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1238 | // Emits a rr::Fence for the given MemorySemanticsMask. |
| 1239 | void Fence(spv::MemorySemanticsMask semantics) const; |
| 1240 | |
| 1241 | // Helper for calling rr::Yield with res cast to an rr::Int. |
| 1242 | void Yield(YieldResult res) const; |
| 1243 | |
Ben Clayton | 0d6791c | 2020-04-22 21:55:27 +0100 | [diff] [blame] | 1244 | // WriteCFGGraphVizDotFile() writes a graphviz dot file of the shader's |
| 1245 | // control flow to the given file path. |
| 1246 | void WriteCFGGraphVizDotFile(const char *path) const; |
| 1247 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1248 | // OpcodeName() returns the name of the opcode op. |
| 1249 | // If NDEBUG is defined, then OpcodeName() will only return the numerical code. |
| 1250 | static std::string OpcodeName(spv::Op op); |
| 1251 | static std::memory_order MemoryOrder(spv::MemorySemanticsMask memorySemantics); |
| 1252 | |
Ben Clayton | 54d16b8 | 2020-02-03 15:32:06 +0000 | [diff] [blame] | 1253 | // IsStatement() returns true if the given opcode actually performs |
| 1254 | // work (as opposed to declaring a type, defining a function start / end, |
| 1255 | // etc). |
| 1256 | static bool IsStatement(spv::Op op); |
| 1257 | |
Nicolas Capens | 78cc4f4 | 2020-04-08 23:44:07 -0400 | [diff] [blame] | 1258 | // HasTypeAndResult() returns true if the given opcode's instruction |
| 1259 | // has a result type ID and result ID, i.e. defines an Object. |
| 1260 | static bool HasTypeAndResult(spv::Op op); |
| 1261 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1262 | // Helper as we often need to take dot products as part of doing other things. |
Nicolas Capens | e6f65d9 | 2020-04-08 21:55:43 -0400 | [diff] [blame] | 1263 | SIMD::Float Dot(unsigned numComponents, Operand const &x, Operand const &y) const; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1264 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1265 | // Splits x into a floating-point significand in the range [0.5, 1.0) |
| 1266 | // and an integral exponent of two, such that: |
| 1267 | // x = significand * 2^exponent |
| 1268 | // Returns the pair <significand, exponent> |
| 1269 | std::pair<SIMD::Float, SIMD::Int> Frexp(RValue<SIMD::Float> val) const; |
| 1270 | |
| 1271 | static ImageSampler *getImageSampler(uint32_t instruction, vk::SampledImageDescriptor const *imageDescriptor, const vk::Sampler *sampler); |
| 1272 | static std::shared_ptr<rr::Routine> emitSamplerRoutine(ImageInstruction instruction, const Sampler &samplerState); |
| 1273 | |
| 1274 | // TODO(b/129523279): Eliminate conversion and use vk::Sampler members directly. |
| 1275 | static sw::FilterType convertFilterMode(const vk::Sampler *sampler); |
| 1276 | static sw::MipmapType convertMipmapMode(const vk::Sampler *sampler); |
| 1277 | static sw::AddressingMode convertAddressingMode(int coordinateIndex, const vk::Sampler *sampler, VkImageViewType imageViewType); |
| 1278 | |
| 1279 | // Returns 0 when invalid. |
| 1280 | static VkShaderStageFlagBits executionModelToStage(spv::ExecutionModel model); |
| 1281 | |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 1282 | // Debugger API functions. When ENABLE_VK_DEBUGGER is not defined, these |
| 1283 | // are all no-ops. |
| 1284 | |
| 1285 | // dbgInit() initializes the debugger code generation. |
| 1286 | // All other dbgXXX() functions are no-op until this is called. |
| 1287 | void dbgInit(const std::shared_ptr<vk::dbg::Context> &dbgctx); |
| 1288 | |
| 1289 | // dbgTerm() terminates the debugger code generation. |
| 1290 | void dbgTerm(); |
| 1291 | |
| 1292 | // dbgCreateFile() generates a synthetic file containing the disassembly |
| 1293 | // of the SPIR-V shader. This is the file displayed in the debug |
| 1294 | // session. |
| 1295 | void dbgCreateFile(); |
| 1296 | |
| 1297 | // dbgBeginEmit() sets up the debugging state for the shader. |
| 1298 | void dbgBeginEmit(EmitState *state) const; |
| 1299 | |
| 1300 | // dbgEndEmit() tears down the debugging state for the shader. |
| 1301 | void dbgEndEmit(EmitState *state) const; |
| 1302 | |
| 1303 | // dbgBeginEmitInstruction() updates the current debugger location for |
| 1304 | // the given instruction. |
| 1305 | void dbgBeginEmitInstruction(InsnIterator insn, EmitState *state) const; |
| 1306 | |
| 1307 | // dbgEndEmitInstruction() creates any new debugger variables for the |
| 1308 | // instruction that just completed. |
| 1309 | void dbgEndEmitInstruction(InsnIterator insn, EmitState *state) const; |
| 1310 | |
| 1311 | // dbgExposeIntermediate() exposes the intermediate with the given ID to |
| 1312 | // the debugger. |
| 1313 | void dbgExposeIntermediate(Object::ID id, EmitState *state) const; |
| 1314 | |
| 1315 | // dbgUpdateActiveLaneMask() updates the active lane masks to the |
| 1316 | // debugger. |
| 1317 | void dbgUpdateActiveLaneMask(RValue<SIMD::Int> mask, EmitState *state) const; |
| 1318 | |
| 1319 | // dbgDeclareResult() associates resultId as the result of the given |
| 1320 | // instruction. |
| 1321 | void dbgDeclareResult(const InsnIterator &insn, Object::ID resultId) const; |
| 1322 | |
| 1323 | // Impl holds forward declaration structs and pointers to state for the |
| 1324 | // private implementations in the corresponding SpirvShaderXXX.cpp files. |
Ben Clayton | 0771f9b | 2020-01-08 12:00:25 +0000 | [diff] [blame] | 1325 | // This allows access to the private members of the SpirvShader, without |
| 1326 | // littering the header with implementation details. |
| 1327 | struct Impl |
| 1328 | { |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 1329 | struct Debugger; |
Ben Clayton | 0771f9b | 2020-01-08 12:00:25 +0000 | [diff] [blame] | 1330 | struct Group; |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 1331 | Debugger *debugger = nullptr; |
Ben Clayton | 0771f9b | 2020-01-08 12:00:25 +0000 | [diff] [blame] | 1332 | }; |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 1333 | Impl impl; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1334 | }; |
| 1335 | |
| 1336 | class SpirvRoutine |
| 1337 | { |
| 1338 | public: |
| 1339 | SpirvRoutine(vk::PipelineLayout const *pipelineLayout); |
| 1340 | |
| 1341 | using Variable = Array<SIMD::Float>; |
| 1342 | |
| 1343 | struct SamplerCache |
| 1344 | { |
| 1345 | Pointer<Byte> imageDescriptor = nullptr; |
| 1346 | Pointer<Byte> sampler; |
| 1347 | Pointer<Byte> function; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1348 | }; |
| 1349 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1350 | vk::PipelineLayout const *const pipelineLayout; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1351 | |
| 1352 | std::unordered_map<SpirvShader::Object::ID, Variable> variables; |
| 1353 | std::unordered_map<SpirvShader::Object::ID, SamplerCache> samplerCache; |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1354 | Variable inputs = Variable{ MAX_INTERFACE_COMPONENTS }; |
| 1355 | Variable outputs = Variable{ MAX_INTERFACE_COMPONENTS }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1356 | |
| 1357 | Pointer<Byte> workgroupMemory; |
| 1358 | Pointer<Pointer<Byte>> descriptorSets; |
| 1359 | Pointer<Int> descriptorDynamicOffsets; |
| 1360 | Pointer<Byte> pushConstants; |
| 1361 | Pointer<Byte> constants; |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1362 | Int killMask = Int{ 0 }; |
Ben Clayton | 5beaef9 | 2019-12-03 12:23:35 +0000 | [diff] [blame] | 1363 | |
| 1364 | // Shader invocation state. |
| 1365 | // Not all of these variables are used for every type of shader, and some |
| 1366 | // are only used when debugging. See b/146486064 for more information. |
| 1367 | // Give careful consideration to the runtime performance loss before adding |
| 1368 | // more state here. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1369 | SIMD::Int windowSpacePosition[2]; |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1370 | Int viewID; // slice offset into input attachments for multiview, even if the shader doesn't use ViewIndex |
Ben Clayton | 5beaef9 | 2019-12-03 12:23:35 +0000 | [diff] [blame] | 1371 | Int instanceID; |
| 1372 | SIMD::Int vertexIndex; |
| 1373 | std::array<SIMD::Float, 4> fragCoord; |
| 1374 | std::array<SIMD::Float, 4> pointCoord; |
| 1375 | SIMD::Int helperInvocation; |
| 1376 | Int4 numWorkgroups; |
| 1377 | Int4 workgroupID; |
| 1378 | Int4 workgroupSize; |
| 1379 | Int subgroupsPerWorkgroup; |
| 1380 | Int invocationsPerSubgroup; |
| 1381 | Int subgroupIndex; |
| 1382 | SIMD::Int localInvocationIndex; |
| 1383 | std::array<SIMD::Int, 3> localInvocationID; |
| 1384 | std::array<SIMD::Int, 3> globalInvocationID; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1385 | |
Ben Clayton | b0ca2a8 | 2020-01-08 13:00:57 +0000 | [diff] [blame] | 1386 | Pointer<Byte> dbgState; // Pointer to a debugger state. |
| 1387 | |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 1388 | void createVariable(SpirvShader::Object::ID id, uint32_t componentCount) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1389 | { |
Nicolas Capens | ff9f9b5 | 2020-04-14 00:46:38 -0400 | [diff] [blame] | 1390 | bool added = variables.emplace(id, Variable(componentCount)).second; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1391 | ASSERT_MSG(added, "Variable %d created twice", id.value()); |
| 1392 | } |
| 1393 | |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1394 | Variable &getVariable(SpirvShader::Object::ID id) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1395 | { |
| 1396 | auto it = variables.find(id); |
| 1397 | ASSERT_MSG(it != variables.end(), "Unknown variables %d", id.value()); |
| 1398 | return it->second; |
| 1399 | } |
| 1400 | |
| 1401 | // setImmutableInputBuiltins() sets all the immutable input builtins, |
| 1402 | // common for all shader types. |
| 1403 | void setImmutableInputBuiltins(SpirvShader const *shader); |
| 1404 | |
| 1405 | // setInputBuiltin() calls f() with the builtin and value if the shader |
| 1406 | // uses the input builtin, otherwise the call is a no-op. |
| 1407 | // F is a function with the signature: |
| 1408 | // void(const SpirvShader::BuiltinMapping& builtin, Array<SIMD::Float>& value) |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1409 | template<typename F> |
| 1410 | inline void setInputBuiltin(SpirvShader const *shader, spv::BuiltIn id, F &&f) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1411 | { |
| 1412 | auto it = shader->inputBuiltins.find(id); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 1413 | if(it != shader->inputBuiltins.end()) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1414 | { |
Ben Clayton | bc1c067 | 2019-12-17 20:37:37 +0000 | [diff] [blame] | 1415 | const auto &builtin = it->second; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1416 | f(builtin, getVariable(builtin.Id)); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | private: |
| 1421 | // The phis are only accessible to SpirvShader as they are only used and |
| 1422 | // exist between calls to SpirvShader::emitProlog() and |
| 1423 | // SpirvShader::emitEpilog(). |
| 1424 | friend class SpirvShader; |
| 1425 | |
| 1426 | std::unordered_map<SpirvShader::Object::ID, Variable> phis; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1427 | }; |
| 1428 | |
| 1429 | } // namespace sw |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 1430 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 1431 | #endif // sw_SpirvShader_hpp |