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 | |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 18 | #include "ShaderCore.hpp" |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 19 | #include "SamplerCore.hpp" |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 20 | #include "SpirvID.hpp" |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 21 | #include "System/Types.hpp" |
| 22 | #include "Vulkan/VkDebug.hpp" |
| 23 | #include "Vulkan/VkConfig.h" |
Nicolas Capens | 09591b8 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 24 | #include "Vulkan/VkDescriptorSet.hpp" |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 25 | #include "Common/Types.hpp" |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 26 | #include "Device/Config.hpp" |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 27 | #include "Device/Sampler.hpp" |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 28 | |
| 29 | #include <spirv/unified1/spirv.hpp> |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 30 | |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 31 | #include <array> |
Ben Clayton | 6fae32c | 2019-02-28 20:06:42 +0000 | [diff] [blame] | 32 | #include <cstring> |
Ben Clayton | 49d8158 | 2019-03-12 20:05:04 +0000 | [diff] [blame] | 33 | #include <functional> |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 34 | #include <string> |
| 35 | #include <vector> |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 36 | #include <unordered_set> |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 37 | #include <unordered_map> |
| 38 | #include <cstdint> |
Chris Forbes | ef4ab0f | 2019-01-18 08:11:03 -0800 | [diff] [blame] | 39 | #include <type_traits> |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 40 | #include <memory> |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 41 | #include <queue> |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 42 | |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 43 | namespace vk |
| 44 | { |
| 45 | class PipelineLayout; |
Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 46 | class ImageView; |
| 47 | class Sampler; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 48 | class RenderPass; |
Chris Forbes | 45f9a93 | 2019-05-08 13:30:38 -0700 | [diff] [blame] | 49 | struct SampledImageDescriptor; |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 50 | } // namespace vk |
| 51 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 52 | namespace sw |
| 53 | { |
Ben Clayton | 24ea515 | 2019-02-26 11:02:42 +0000 | [diff] [blame] | 54 | // Forward declarations. |
| 55 | class SpirvRoutine; |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 56 | class GenericValue; |
Ben Clayton | 24ea515 | 2019-02-26 11:02:42 +0000 | [diff] [blame] | 57 | |
| 58 | // SIMD contains types that represent multiple scalars packed into a single |
| 59 | // vector data type. Types in the SIMD namespace provide a semantic hint |
| 60 | // that the data should be treated as a per-execution-lane scalar instead of |
| 61 | // a typical euclidean-style vector type. |
| 62 | namespace SIMD |
| 63 | { |
| 64 | // Width is the number of per-lane scalars packed into each SIMD vector. |
| 65 | static constexpr int Width = 4; |
| 66 | |
| 67 | using Float = rr::Float4; |
| 68 | using Int = rr::Int4; |
Ben Clayton | dd1e37e | 2019-02-28 19:59:15 +0000 | [diff] [blame] | 69 | using UInt = rr::UInt4; |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 70 | |
| 71 | struct Pointer |
| 72 | { |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 73 | Pointer(rr::Pointer<Byte> base, rr::Int limit) |
| 74 | : base(base), limit(limit), dynamicOffsets(0), staticOffsets{}, hasDynamicOffsets(false) {} |
| 75 | Pointer(rr::Pointer<Byte> base, rr::Int limit, SIMD::Int offset) |
| 76 | : base(base), limit(limit), dynamicOffsets(offset), staticOffsets{}, hasDynamicOffsets(false) {} |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 77 | |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 78 | inline Pointer& operator += (Int i) |
| 79 | { |
| 80 | dynamicOffsets += i; |
| 81 | hasDynamicOffsets = true; |
| 82 | return *this; |
| 83 | } |
| 84 | |
| 85 | inline Pointer& operator *= (Int i) |
| 86 | { |
| 87 | dynamicOffsets = offsets() * i; |
| 88 | staticOffsets = {}; |
| 89 | hasDynamicOffsets = true; |
| 90 | return *this; |
| 91 | } |
| 92 | |
| 93 | inline Pointer operator + (SIMD::Int i) { Pointer p = *this; p += i; return p; } |
| 94 | inline Pointer operator * (SIMD::Int i) { Pointer p = *this; p *= i; return p; } |
| 95 | |
| 96 | inline Pointer& operator += (int i) |
| 97 | { |
| 98 | for (int el = 0; el < SIMD::Width; el++) { staticOffsets[el] += i; } |
| 99 | return *this; |
| 100 | } |
| 101 | |
| 102 | inline Pointer& operator *= (int i) |
| 103 | { |
| 104 | for (int el = 0; el < SIMD::Width; el++) { staticOffsets[el] *= i; } |
| 105 | if (hasDynamicOffsets) |
| 106 | { |
| 107 | dynamicOffsets *= SIMD::Int(i); |
| 108 | } |
| 109 | return *this; |
| 110 | } |
| 111 | |
| 112 | inline Pointer operator + (int i) { Pointer p = *this; p += i; return p; } |
| 113 | inline Pointer operator * (int i) { Pointer p = *this; p *= i; return p; } |
| 114 | |
| 115 | inline SIMD::Int offsets() const |
| 116 | { |
| 117 | static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4"); |
| 118 | return dynamicOffsets + SIMD::Int(staticOffsets[0], staticOffsets[1], staticOffsets[2], staticOffsets[3]); |
| 119 | } |
| 120 | |
| 121 | // Returns true if all offsets are sequential (N+0, N+1, N+2, N+3) |
| 122 | inline rr::Bool hasSequentialOffsets() const |
| 123 | { |
| 124 | if (hasDynamicOffsets) |
| 125 | { |
| 126 | auto o = offsets(); |
| 127 | static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4"); |
| 128 | return rr::SignMask(~CmpEQ(o.yzww, o + SIMD::Int(1, 2, 3, 0))) == 0; |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | for (int i = 1; i < SIMD::Width; i++) |
| 133 | { |
| 134 | if (staticOffsets[i-1] + 1 != staticOffsets[i]) { return false; } |
| 135 | } |
| 136 | return true; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Returns true if all offsets are equal (N, N, N, N) |
| 141 | inline rr::Bool hasEqualOffsets() const |
| 142 | { |
| 143 | if (hasDynamicOffsets) |
| 144 | { |
| 145 | auto o = offsets(); |
| 146 | static_assert(SIMD::Width == 4, "Expects SIMD::Width to be 4"); |
| 147 | return rr::SignMask(~CmpEQ(o, o.yzwx)) == 0; |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | for (int i = 1; i < SIMD::Width; i++) |
| 152 | { |
| 153 | if (staticOffsets[i-1] != staticOffsets[i]) { return false; } |
| 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | } |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 158 | |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 159 | // Base address for the pointer, common across all lanes. |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 160 | rr::Pointer<rr::Byte> base; |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 161 | |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 162 | // Upper (non-inclusive) limit for offsets from base. |
| 163 | rr::Int limit; |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 164 | |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 165 | // Per lane offsets from base. |
| 166 | SIMD::Int dynamicOffsets; // If hasDynamicOffsets is false, all dynamicOffsets are zero. |
| 167 | std::array<int32_t, SIMD::Width> staticOffsets; |
| 168 | |
| 169 | // True if all dynamicOffsets are zero. |
| 170 | bool hasDynamicOffsets; |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 171 | }; |
Ben Clayton | 9e4bc1b | 2019-04-16 16:52:02 -0400 | [diff] [blame] | 172 | |
| 173 | template <typename T> struct Element {}; |
| 174 | template <> struct Element<Float> { using type = rr::Float; }; |
| 175 | template <> struct Element<Int> { using type = rr::Int; }; |
| 176 | template <> struct Element<UInt> { using type = rr::UInt; }; |
| 177 | |
| 178 | template<typename T> |
| 179 | void Store(Pointer ptr, T val, Int mask, bool atomic = false, std::memory_order order = std::memory_order_relaxed); |
| 180 | |
| 181 | template<typename T> |
| 182 | void Store(Pointer ptr, RValue<T> val, Int mask, bool atomic = false, std::memory_order order = std::memory_order_relaxed) |
| 183 | { |
| 184 | Store(ptr, T(val), mask, atomic, order); |
| 185 | } |
| 186 | |
| 187 | template<typename T> |
| 188 | T Load(Pointer ptr, Int mask, bool atomic = false, std::memory_order order = std::memory_order_relaxed); |
Ben Clayton | 24ea515 | 2019-02-26 11:02:42 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 191 | // Incrementally constructed complex bundle of rvalues |
| 192 | // Effectively a restricted vector, supporting only: |
| 193 | // - allocation to a (runtime-known) fixed size |
| 194 | // - in-place construction of elements |
| 195 | // - const operator[] |
| 196 | class Intermediate |
| 197 | { |
| 198 | public: |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 199 | Intermediate(uint32_t size) : scalar(new rr::Value*[size]), size(size) { |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 200 | memset(scalar, 0, sizeof(rr::Value*) * size); |
Ben Clayton | 6fae32c | 2019-02-28 20:06:42 +0000 | [diff] [blame] | 201 | } |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 202 | |
| 203 | ~Intermediate() |
| 204 | { |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 205 | delete[] scalar; |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 208 | void move(uint32_t i, RValue<SIMD::Float> &&scalar) { emplace(i, scalar.value); } |
| 209 | void move(uint32_t i, RValue<SIMD::Int> &&scalar) { emplace(i, scalar.value); } |
| 210 | void move(uint32_t i, RValue<SIMD::UInt> &&scalar) { emplace(i, scalar.value); } |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 211 | |
Nicolas Capens | 80c796b | 2019-03-19 21:38:44 -0400 | [diff] [blame] | 212 | void move(uint32_t i, const RValue<SIMD::Float> &scalar) { emplace(i, scalar.value); } |
| 213 | void move(uint32_t i, const RValue<SIMD::Int> &scalar) { emplace(i, scalar.value); } |
| 214 | void move(uint32_t i, const RValue<SIMD::UInt> &scalar) { emplace(i, scalar.value); } |
Ben Clayton | 093be46 | 2019-03-08 08:37:24 +0000 | [diff] [blame] | 215 | |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 216 | // Value retrieval functions. |
| 217 | RValue<SIMD::Float> Float(uint32_t i) const |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 218 | { |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 219 | ASSERT(i < size); |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 220 | ASSERT(scalar[i] != nullptr); |
| 221 | return As<SIMD::Float>(scalar[i]); // TODO(b/128539387): RValue<SIMD::Float>(scalar) |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 222 | } |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 223 | |
| 224 | RValue<SIMD::Int> Int(uint32_t i) const |
| 225 | { |
| 226 | ASSERT(i < size); |
| 227 | ASSERT(scalar[i] != nullptr); |
| 228 | return As<SIMD::Int>(scalar[i]); // TODO(b/128539387): RValue<SIMD::Int>(scalar) |
| 229 | } |
| 230 | |
| 231 | RValue<SIMD::UInt> UInt(uint32_t i) const |
| 232 | { |
| 233 | ASSERT(i < size); |
| 234 | ASSERT(scalar[i] != nullptr); |
| 235 | return As<SIMD::UInt>(scalar[i]); // TODO(b/128539387): RValue<SIMD::UInt>(scalar) |
| 236 | } |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 237 | |
| 238 | // No copy/move construction or assignment |
| 239 | Intermediate(Intermediate const &) = delete; |
| 240 | Intermediate(Intermediate &&) = delete; |
| 241 | Intermediate & operator=(Intermediate const &) = delete; |
| 242 | Intermediate & operator=(Intermediate &&) = delete; |
| 243 | |
| 244 | private: |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 245 | void emplace(uint32_t i, rr::Value *value) |
| 246 | { |
| 247 | ASSERT(i < size); |
| 248 | ASSERT(scalar[i] == nullptr); |
| 249 | scalar[i] = value; |
| 250 | } |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 251 | |
Nicolas Capens | 5851ef4 | 2019-03-19 14:28:18 -0400 | [diff] [blame] | 252 | rr::Value **const scalar; |
Chris Forbes | e4ef5f7 | 2019-02-15 16:00:08 -0800 | [diff] [blame] | 253 | uint32_t size; |
| 254 | }; |
| 255 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 256 | class SpirvShader |
| 257 | { |
| 258 | public: |
| 259 | using InsnStore = std::vector<uint32_t>; |
| 260 | InsnStore insns; |
| 261 | |
Nicolas Capens | 97da782 | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 262 | using ImageSampler = void(void* texture, void *sampler, void* uvsIn, void* texelOut, void* constants); |
Nicolas Capens | 125dba0 | 2019-04-24 02:03:22 -0400 | [diff] [blame] | 263 | using GetImageSampler = ImageSampler*(const vk::ImageView *imageView, const vk::Sampler *sampler); |
| 264 | |
Ben Clayton | ecfeede | 2019-05-08 08:51:01 +0100 | [diff] [blame] | 265 | enum class YieldResult |
| 266 | { |
| 267 | ControlBarrier, |
| 268 | }; |
| 269 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 270 | /* Pseudo-iterator over SPIRV instructions, designed to support range-based-for. */ |
| 271 | class InsnIterator |
| 272 | { |
| 273 | InsnStore::const_iterator iter; |
| 274 | |
| 275 | public: |
| 276 | spv::Op opcode() const |
| 277 | { |
| 278 | return static_cast<spv::Op>(*iter & spv::OpCodeMask); |
| 279 | } |
| 280 | |
| 281 | uint32_t wordCount() const |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 282 | { |
| 283 | return *iter >> spv::WordCountShift; |
| 284 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 285 | |
| 286 | uint32_t word(uint32_t n) const |
| 287 | { |
| 288 | ASSERT(n < wordCount()); |
| 289 | return iter[n]; |
| 290 | } |
| 291 | |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 292 | uint32_t const * wordPointer(uint32_t n) const |
| 293 | { |
| 294 | ASSERT(n < wordCount()); |
| 295 | return &iter[n]; |
| 296 | } |
| 297 | |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 298 | const char* string(uint32_t n) const |
| 299 | { |
| 300 | return reinterpret_cast<const char*>(wordPointer(n)); |
| 301 | } |
| 302 | |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 303 | bool operator==(InsnIterator const &other) const |
| 304 | { |
| 305 | return iter == other.iter; |
| 306 | } |
| 307 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 308 | bool operator!=(InsnIterator const &other) const |
| 309 | { |
| 310 | return iter != other.iter; |
| 311 | } |
| 312 | |
| 313 | InsnIterator operator*() const |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 314 | { |
| 315 | return *this; |
| 316 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 317 | |
| 318 | InsnIterator &operator++() |
| 319 | { |
| 320 | iter += wordCount(); |
| 321 | return *this; |
| 322 | } |
| 323 | |
| 324 | InsnIterator const operator++(int) |
| 325 | { |
| 326 | InsnIterator ret{*this}; |
| 327 | iter += wordCount(); |
| 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | InsnIterator(InsnIterator const &other) = default; |
| 332 | |
| 333 | InsnIterator() = default; |
| 334 | |
| 335 | explicit InsnIterator(InsnStore::const_iterator iter) : iter{iter} |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 336 | { |
| 337 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | /* range-based-for interface */ |
| 341 | InsnIterator begin() const |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 342 | { |
| 343 | return InsnIterator{insns.cbegin() + 5}; |
| 344 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 345 | |
| 346 | InsnIterator end() const |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 347 | { |
| 348 | return InsnIterator{insns.cend()}; |
| 349 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 350 | |
Ben Clayton | e205d34 | 2019-02-20 10:22:09 +0000 | [diff] [blame] | 351 | class Type |
| 352 | { |
| 353 | public: |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 354 | using ID = SpirvID<Type>; |
| 355 | |
Nicolas Capens | 2909085 | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 356 | spv::Op opcode() const { return definition.opcode(); } |
| 357 | |
Ben Clayton | e205d34 | 2019-02-20 10:22:09 +0000 | [diff] [blame] | 358 | InsnIterator definition; |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 359 | spv::StorageClass storageClass = static_cast<spv::StorageClass>(-1); |
Ben Clayton | e205d34 | 2019-02-20 10:22:09 +0000 | [diff] [blame] | 360 | uint32_t sizeInComponents = 0; |
| 361 | bool isBuiltInBlock = false; |
Ben Clayton | 9a16248 | 2019-02-25 11:54:43 +0000 | [diff] [blame] | 362 | |
| 363 | // Inner element type for pointers, arrays, vectors and matrices. |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 364 | ID element; |
Ben Clayton | e205d34 | 2019-02-20 10:22:09 +0000 | [diff] [blame] | 365 | }; |
| 366 | |
Chris Forbes | 296aa25 | 2018-12-27 11:48:21 -0800 | [diff] [blame] | 367 | class Object |
| 368 | { |
| 369 | public: |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 370 | using ID = SpirvID<Object>; |
| 371 | |
Nicolas Capens | 2909085 | 2019-03-19 16:22:35 -0400 | [diff] [blame] | 372 | spv::Op opcode() const { return definition.opcode(); } |
| 373 | |
Chris Forbes | 296aa25 | 2018-12-27 11:48:21 -0800 | [diff] [blame] | 374 | InsnIterator definition; |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 375 | Type::ID type; |
Chris Forbes | 1ca8acd | 2019-02-20 13:00:54 -0800 | [diff] [blame] | 376 | std::unique_ptr<uint32_t[]> constantValue = nullptr; |
Chris Forbes | 296aa25 | 2018-12-27 11:48:21 -0800 | [diff] [blame] | 377 | |
| 378 | enum class Kind |
| 379 | { |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 380 | // Invalid default kind. |
| 381 | // If we get left with an object in this state, the module was |
| 382 | // broken. |
| 383 | Unknown, |
| 384 | |
| 385 | // TODO: Better document this kind. |
| 386 | // A shader interface variable pointer. |
| 387 | // Pointer with uniform address across all lanes. |
| 388 | // Pointer held by SpirvRoutine::pointers |
| 389 | InterfaceVariable, |
| 390 | |
| 391 | // Constant value held by Object::constantValue. |
| 392 | Constant, |
| 393 | |
| 394 | // Value held by SpirvRoutine::intermediates. |
| 395 | Intermediate, |
| 396 | |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 397 | // Pointer held by SpirvRoutine::pointers |
Ben Clayton | 1d514f3 | 2019-04-19 16:11:18 -0400 | [diff] [blame] | 398 | Pointer, |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 399 | |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 400 | // A pointer to a vk::DescriptorSet*. |
| 401 | // Pointer held by SpirvRoutine::pointers. |
| 402 | DescriptorSet, |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 403 | }; |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 404 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 405 | Kind kind = Kind::Unknown; |
Chris Forbes | 296aa25 | 2018-12-27 11:48:21 -0800 | [diff] [blame] | 406 | }; |
| 407 | |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 408 | // Block is an interval of SPIR-V instructions, starting with the |
| 409 | // opening OpLabel, and ending with a termination instruction. |
| 410 | class Block |
| 411 | { |
| 412 | public: |
| 413 | using ID = SpirvID<Block>; |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 414 | using Set = std::unordered_set<ID>; |
| 415 | |
| 416 | // Edge represents the graph edge between two blocks. |
| 417 | struct Edge |
| 418 | { |
| 419 | ID from; |
| 420 | ID to; |
| 421 | |
| 422 | bool operator == (const Edge& other) const { return from == other.from && to == other.to; } |
| 423 | |
| 424 | struct Hash |
| 425 | { |
| 426 | std::size_t operator()(const Edge& edge) const noexcept |
| 427 | { |
| 428 | return std::hash<uint32_t>()(edge.from.value() * 31 + edge.to.value()); |
| 429 | } |
| 430 | }; |
| 431 | }; |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 432 | |
| 433 | Block() = default; |
| 434 | Block(const Block& other) = default; |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 435 | explicit Block(InsnIterator begin, InsnIterator end); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 436 | |
| 437 | /* range-based-for interface */ |
| 438 | inline InsnIterator begin() const { return begin_; } |
| 439 | inline InsnIterator end() const { return end_; } |
| 440 | |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 441 | enum Kind |
| 442 | { |
| 443 | Simple, // OpBranch or other simple terminator. |
| 444 | StructuredBranchConditional, // OpSelectionMerge + OpBranchConditional |
| 445 | UnstructuredBranchConditional, // OpBranchConditional |
| 446 | StructuredSwitch, // OpSelectionMerge + OpSwitch |
| 447 | UnstructuredSwitch, // OpSwitch |
| 448 | Loop, // OpLoopMerge + [OpBranchConditional | OpBranch] |
| 449 | }; |
| 450 | |
| 451 | Kind kind; |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 452 | InsnIterator mergeInstruction; // Structured control flow merge instruction. |
| 453 | InsnIterator branchInstruction; // Branch instruction. |
Ben Clayton | 64f78f5 | 2019-03-21 17:21:06 +0000 | [diff] [blame] | 454 | ID mergeBlock; // Structured flow merge block. |
| 455 | ID continueTarget; // Loop continue block. |
| 456 | Set ins; // Blocks that branch into this block. |
| 457 | Set outs; // Blocks that this block branches to. |
| 458 | |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 459 | private: |
| 460 | InsnIterator begin_; |
| 461 | InsnIterator end_; |
| 462 | }; |
| 463 | |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 464 | struct TypeOrObject {}; // Dummy struct to represent a Type or Object. |
| 465 | |
| 466 | // TypeOrObjectID is an identifier that represents a Type or an Object, |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 467 | // and supports implicit casting to and from Type::ID or Object::ID. |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 468 | class TypeOrObjectID : public SpirvID<TypeOrObject> |
| 469 | { |
| 470 | public: |
| 471 | using Hash = std::hash<SpirvID<TypeOrObject>>; |
| 472 | |
| 473 | inline TypeOrObjectID(uint32_t id) : SpirvID(id) {} |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 474 | inline TypeOrObjectID(Type::ID id) : SpirvID(id.value()) {} |
| 475 | inline TypeOrObjectID(Object::ID id) : SpirvID(id.value()) {} |
| 476 | inline operator Type::ID() const { return Type::ID(value()); } |
| 477 | inline operator Object::ID() const { return Object::ID(value()); } |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 478 | }; |
| 479 | |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 480 | // OpImageSample variants |
| 481 | enum Variant |
| 482 | { |
| 483 | None, |
| 484 | Dref, |
| 485 | Proj, |
| 486 | ProjDref, |
| 487 | VARIANT_LAST = ProjDref |
| 488 | }; |
| 489 | |
Nicolas Capens | 7889633 | 2019-04-29 16:41:50 -0400 | [diff] [blame] | 490 | // Compact representation of image instruction parameters that is passed to the |
| 491 | // trampoline function for retrieving/generating the corresponding sampling routine. |
| 492 | struct ImageInstruction |
| 493 | { |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 494 | ImageInstruction(Variant variant, SamplerMethod samplerMethod) |
| 495 | : parameters(0) |
Nicolas Capens | 7889633 | 2019-04-29 16:41:50 -0400 | [diff] [blame] | 496 | { |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 497 | this->variant = variant; |
| 498 | this->samplerMethod = samplerMethod; |
Nicolas Capens | 7889633 | 2019-04-29 16:41:50 -0400 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // Unmarshal from raw 32-bit data |
| 502 | ImageInstruction(uint32_t parameters) : parameters(parameters) {} |
| 503 | |
Nicolas Capens | 022bd57 | 2019-04-29 23:45:25 -0400 | [diff] [blame] | 504 | SamplerFunction getSamplerFunction() const |
Nicolas Capens | 7889633 | 2019-04-29 16:41:50 -0400 | [diff] [blame] | 505 | { |
Nicolas Capens | 022bd57 | 2019-04-29 23:45:25 -0400 | [diff] [blame] | 506 | return { static_cast<SamplerMethod>(samplerMethod), static_cast<SamplerOption>(samplerOption) }; |
Nicolas Capens | 7889633 | 2019-04-29 16:41:50 -0400 | [diff] [blame] | 507 | } |
| 508 | |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 509 | bool isDref() const |
| 510 | { |
| 511 | return (variant == Dref) || (variant == ProjDref); |
| 512 | } |
| 513 | |
| 514 | bool isProj() const |
| 515 | { |
| 516 | return (variant == Proj) || (variant == ProjDref); |
| 517 | } |
| 518 | |
Nicolas Capens | 7889633 | 2019-04-29 16:41:50 -0400 | [diff] [blame] | 519 | union |
| 520 | { |
| 521 | struct |
| 522 | { |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 523 | uint32_t variant : BITS(VARIANT_LAST); |
Nicolas Capens | 7889633 | 2019-04-29 16:41:50 -0400 | [diff] [blame] | 524 | uint32_t samplerMethod : BITS(SAMPLER_METHOD_LAST); |
Nicolas Capens | 022bd57 | 2019-04-29 23:45:25 -0400 | [diff] [blame] | 525 | uint32_t samplerOption : BITS(SAMPLER_OPTION_LAST); |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 526 | |
| 527 | // Parameters are passed to the sampling routine in this order: |
| 528 | uint32_t coordinates : 3; // 1-4 (does not contain projection component) |
Nicolas Capens | e2535df | 2019-05-06 10:37:50 -0400 | [diff] [blame] | 529 | // uint32_t dref : 1; // Indicated by Variant::ProjDref|Dref |
Nicolas Capens | 4bade2e | 2019-04-30 16:21:06 -0400 | [diff] [blame] | 530 | // uint32_t lodOrBias : 1; // Indicated by SamplerMethod::Lod|Bias |
Nicolas Capens | 022bd57 | 2019-04-29 23:45:25 -0400 | [diff] [blame] | 531 | uint32_t gradComponents : 2; // 0-3 (for each of dx / dy) |
| 532 | uint32_t offsetComponents : 2; // 0-3 |
Nicolas Capens | 7889633 | 2019-04-29 16:41:50 -0400 | [diff] [blame] | 533 | }; |
| 534 | |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 535 | uint32_t parameters; |
Nicolas Capens | 7889633 | 2019-04-29 16:41:50 -0400 | [diff] [blame] | 536 | }; |
| 537 | }; |
| 538 | |
| 539 | static_assert(sizeof(ImageInstruction) == 4, "ImageInstruction must be 32-bit"); |
| 540 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 541 | int getSerialID() const |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 542 | { |
| 543 | return serialID; |
| 544 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 545 | |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 546 | SpirvShader(VkPipelineShaderStageCreateInfo const *createInfo, |
| 547 | InsnStore const &insns, |
| 548 | vk::RenderPass *renderPass, |
| 549 | uint32_t subpassIndex); |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 550 | |
| 551 | struct Modes |
| 552 | { |
| 553 | bool EarlyFragmentTests : 1; |
| 554 | bool DepthReplacing : 1; |
| 555 | bool DepthGreater : 1; |
| 556 | bool DepthLess : 1; |
| 557 | bool DepthUnchanged : 1; |
Chris Forbes | 8b0a281 | 2019-01-17 10:10:09 -0800 | [diff] [blame] | 558 | bool ContainsKill : 1; |
Ben Clayton | ecfeede | 2019-05-08 08:51:01 +0100 | [diff] [blame] | 559 | bool ContainsControlBarriers : 1; |
Chris Forbes | 93f70b3 | 2019-02-10 21:26:27 +0000 | [diff] [blame] | 560 | bool NeedsCentroid : 1; |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 561 | |
| 562 | // Compute workgroup dimensions |
Ben Clayton | 62758f5 | 2019-03-13 14:18:58 +0000 | [diff] [blame] | 563 | int WorkgroupSizeX = 1, WorkgroupSizeY = 1, WorkgroupSizeZ = 1; |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 564 | }; |
| 565 | |
| 566 | Modes const &getModes() const |
Chris Forbes | 4a979dc | 2019-01-17 09:36:46 -0800 | [diff] [blame] | 567 | { |
| 568 | return modes; |
| 569 | } |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 570 | |
Chris Forbes | 2e7f35b | 2019-01-17 09:51:39 -0800 | [diff] [blame] | 571 | enum AttribType : unsigned char |
| 572 | { |
| 573 | ATTRIBTYPE_FLOAT, |
| 574 | ATTRIBTYPE_INT, |
| 575 | ATTRIBTYPE_UINT, |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 576 | ATTRIBTYPE_UNUSED, |
Chris Forbes | 2e7f35b | 2019-01-17 09:51:39 -0800 | [diff] [blame] | 577 | |
| 578 | ATTRIBTYPE_LAST = ATTRIBTYPE_UINT |
| 579 | }; |
| 580 | |
Chris Forbes | 8b0a281 | 2019-01-17 10:10:09 -0800 | [diff] [blame] | 581 | bool hasBuiltinInput(spv::BuiltIn b) const |
| 582 | { |
| 583 | return inputBuiltins.find(b) != inputBuiltins.end(); |
| 584 | } |
| 585 | |
Chris Forbes | 71a1e01 | 2019-04-22 14:18:34 -0700 | [diff] [blame] | 586 | bool hasBuiltinOutput(spv::BuiltIn b) const |
| 587 | { |
| 588 | return outputBuiltins.find(b) != outputBuiltins.end(); |
| 589 | } |
| 590 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 591 | struct Decorations |
| 592 | { |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 593 | int32_t Location = -1; |
| 594 | int32_t Component = 0; |
| 595 | spv::BuiltIn BuiltIn = static_cast<spv::BuiltIn>(-1); |
| 596 | int32_t Offset = -1; |
| 597 | int32_t ArrayStride = -1; |
| 598 | int32_t MatrixStride = 1; |
| 599 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 600 | bool HasLocation : 1; |
| 601 | bool HasComponent : 1; |
| 602 | bool HasBuiltIn : 1; |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 603 | bool HasOffset : 1; |
| 604 | bool HasArrayStride : 1; |
| 605 | bool HasMatrixStride : 1; |
Chris Forbes | 1ba5ba7 | 2019-04-12 11:37:21 -0700 | [diff] [blame] | 606 | bool HasRowMajor : 1; // whether RowMajor bit is valid. |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 607 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 608 | bool Flat : 1; |
| 609 | bool Centroid : 1; |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 610 | bool NoPerspective : 1; |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 611 | bool Block : 1; |
| 612 | bool BufferBlock : 1; |
Ben Clayton | 8448cc5 | 2019-04-09 16:24:31 -0400 | [diff] [blame] | 613 | bool RelaxedPrecision : 1; |
Chris Forbes | 1ba5ba7 | 2019-04-12 11:37:21 -0700 | [diff] [blame] | 614 | bool RowMajor : 1; // RowMajor if true; ColMajor if false |
Chris Forbes | 98e6b96 | 2019-04-12 11:58:58 -0700 | [diff] [blame] | 615 | bool InsideMatrix : 1; // pseudo-decoration for whether we're inside a matrix. |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 616 | |
| 617 | Decorations() |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 618 | : Location{-1}, Component{0}, |
Ben Clayton | d073d8e | 2019-02-26 11:06:50 +0000 | [diff] [blame] | 619 | BuiltIn{static_cast<spv::BuiltIn>(-1)}, |
Chris Forbes | 6532107 | 2019-03-07 16:13:56 -0800 | [diff] [blame] | 620 | Offset{-1}, ArrayStride{-1}, MatrixStride{-1}, |
Ben Clayton | d073d8e | 2019-02-26 11:06:50 +0000 | [diff] [blame] | 621 | HasLocation{false}, HasComponent{false}, |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 622 | HasBuiltIn{false}, HasOffset{false}, |
| 623 | HasArrayStride{false}, HasMatrixStride{false}, |
Chris Forbes | 1ba5ba7 | 2019-04-12 11:37:21 -0700 | [diff] [blame] | 624 | HasRowMajor{false}, |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 625 | Flat{false}, Centroid{false}, NoPerspective{false}, |
| 626 | Block{false}, BufferBlock{false}, |
Chris Forbes | 98e6b96 | 2019-04-12 11:58:58 -0700 | [diff] [blame] | 627 | RelaxedPrecision{false}, RowMajor{false}, |
| 628 | InsideMatrix{false} |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 629 | { |
| 630 | } |
| 631 | |
| 632 | Decorations(Decorations const &) = default; |
| 633 | |
| 634 | void Apply(Decorations const &src); |
| 635 | |
| 636 | void Apply(spv::Decoration decoration, uint32_t arg); |
| 637 | }; |
| 638 | |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 639 | std::unordered_map<TypeOrObjectID, Decorations, TypeOrObjectID::Hash> decorations; |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 640 | std::unordered_map<Type::ID, std::vector<Decorations>> memberDecorations; |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 641 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 642 | struct DescriptorDecorations |
| 643 | { |
| 644 | int32_t DescriptorSet = -1; |
| 645 | int32_t Binding = -1; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 646 | int32_t InputAttachmentIndex = -1; |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 647 | |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 648 | void Apply(DescriptorDecorations const &src); |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 649 | }; |
| 650 | |
| 651 | std::unordered_map<Object::ID, DescriptorDecorations> descriptorDecorations; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 652 | std::vector<VkFormat> inputAttachmentFormats; |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 653 | |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 654 | struct InterfaceComponent |
| 655 | { |
| 656 | AttribType Type; |
| 657 | bool Flat : 1; |
| 658 | bool Centroid : 1; |
| 659 | bool NoPerspective : 1; |
| 660 | |
| 661 | InterfaceComponent() |
| 662 | : Type{ATTRIBTYPE_UNUSED}, Flat{false}, Centroid{false}, NoPerspective{false} |
| 663 | { |
| 664 | } |
| 665 | }; |
| 666 | |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 667 | struct BuiltinMapping |
| 668 | { |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 669 | Object::ID Id; |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 670 | uint32_t FirstComponent; |
| 671 | uint32_t SizeInComponents; |
| 672 | }; |
| 673 | |
Ben Clayton | ecd3848 | 2019-04-19 17:11:08 -0400 | [diff] [blame] | 674 | struct WorkgroupMemory |
| 675 | { |
| 676 | // allocates a new variable of size bytes with the given identifier. |
| 677 | inline void allocate(Object::ID id, uint32_t size) |
| 678 | { |
| 679 | uint32_t offset = totalSize; |
| 680 | auto it = offsets.emplace(id, offset); |
| 681 | ASSERT_MSG(it.second, "WorkgroupMemory already has an allocation for object %d", int(id.value())); |
| 682 | totalSize += size; |
| 683 | } |
| 684 | // returns the byte offset of the variable with the given identifier. |
| 685 | inline uint32_t offsetOf(Object::ID id) const |
| 686 | { |
| 687 | auto it = offsets.find(id); |
| 688 | ASSERT_MSG(it != offsets.end(), "WorkgroupMemory has no allocation for object %d", int(id.value())); |
| 689 | return it->second; |
| 690 | } |
| 691 | // returns the total allocated size in bytes. |
| 692 | inline uint32_t size() const { return totalSize; } |
| 693 | private: |
| 694 | uint32_t totalSize = 0; // in bytes |
| 695 | std::unordered_map<Object::ID, uint32_t> offsets; // in bytes |
| 696 | }; |
| 697 | |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 698 | std::vector<InterfaceComponent> inputs; |
| 699 | std::vector<InterfaceComponent> outputs; |
| 700 | |
Chris Forbes | c61271e | 2019-02-19 17:01:28 -0800 | [diff] [blame] | 701 | void emitProlog(SpirvRoutine *routine) const; |
Nicolas Capens | 09591b8 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 702 | void emit(SpirvRoutine *routine, RValue<SIMD::Int> const &activeLaneMask, const vk::DescriptorSet::Bindings &descriptorSets) const; |
Chris Forbes | c61271e | 2019-02-19 17:01:28 -0800 | [diff] [blame] | 703 | void emitEpilog(SpirvRoutine *routine) const; |
Chris Forbes | ef4ab0f | 2019-01-18 08:11:03 -0800 | [diff] [blame] | 704 | |
| 705 | using BuiltInHash = std::hash<std::underlying_type<spv::BuiltIn>::type>; |
| 706 | std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> inputBuiltins; |
| 707 | std::unordered_map<spv::BuiltIn, BuiltinMapping, BuiltInHash> outputBuiltins; |
Ben Clayton | ecd3848 | 2019-04-19 17:11:08 -0400 | [diff] [blame] | 708 | WorkgroupMemory workgroupMemory; |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 709 | |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 710 | Type const &getType(Type::ID id) const |
Chris Forbes | 840809a | 2019-01-14 14:30:20 -0800 | [diff] [blame] | 711 | { |
| 712 | auto it = types.find(id); |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 713 | ASSERT_MSG(it != types.end(), "Unknown type %d", id.value()); |
Chris Forbes | 840809a | 2019-01-14 14:30:20 -0800 | [diff] [blame] | 714 | return it->second; |
| 715 | } |
| 716 | |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 717 | Object const &getObject(Object::ID id) const |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 718 | { |
Chris Forbes | 1c65823 | 2019-02-01 17:12:25 -0800 | [diff] [blame] | 719 | auto it = defs.find(id); |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 720 | ASSERT_MSG(it != defs.end(), "Unknown object %d", id.value()); |
Chris Forbes | 1c65823 | 2019-02-01 17:12:25 -0800 | [diff] [blame] | 721 | return it->second; |
| 722 | } |
| 723 | |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 724 | Block const &getBlock(Block::ID id) const |
| 725 | { |
| 726 | auto it = blocks.find(id); |
Ben Clayton | af26cfe | 2019-03-21 17:32:44 +0000 | [diff] [blame] | 727 | ASSERT_MSG(it != blocks.end(), "Unknown block %d", id.value()); |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 728 | return it->second; |
| 729 | } |
| 730 | |
Chris Forbes | d5aed49 | 2019-02-02 15:18:52 -0800 | [diff] [blame] | 731 | private: |
| 732 | const int serialID; |
| 733 | static volatile int serialCounter; |
| 734 | Modes modes; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 735 | HandleMap<Type> types; |
| 736 | HandleMap<Object> defs; |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 737 | HandleMap<Block> blocks; |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 738 | Block::ID entryPointBlockId; // Block of the entry point function. |
Ben Clayton | 9b15661 | 2019-03-13 19:48:31 +0000 | [diff] [blame] | 739 | |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 740 | // Walks all reachable the blocks starting from id adding them to |
| 741 | // reachable. |
| 742 | void TraverseReachableBlocks(Block::ID id, Block::Set& reachable); |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 743 | |
| 744 | // Assigns Block::ins from Block::outs for every block. |
| 745 | void AssignBlockIns(); |
| 746 | |
Ben Clayton | 0bb83b8 | 2019-02-26 11:41:07 +0000 | [diff] [blame] | 747 | // DeclareType creates a Type for the given OpTypeX instruction, storing |
| 748 | // it into the types map. It is called from the analysis pass (constructor). |
| 749 | void DeclareType(InsnIterator insn); |
| 750 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 751 | void ProcessExecutionMode(InsnIterator it); |
Chris Forbes | 739a7fb | 2018-12-08 13:09:40 -0800 | [diff] [blame] | 752 | |
| 753 | uint32_t ComputeTypeSize(InsnIterator insn); |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 754 | void ApplyDecorationsForId(Decorations *d, TypeOrObjectID id) const; |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 755 | void ApplyDecorationsForIdMember(Decorations *d, Type::ID id, uint32_t member) const; |
Chris Forbes | 3610ded | 2019-04-22 18:12:13 -0700 | [diff] [blame] | 756 | void ApplyDecorationsForAccessChain(Decorations *d, DescriptorDecorations *dd, Object::ID baseId, uint32_t numIndexes, uint32_t const *indexIds) const; |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 757 | |
Nicolas Capens | 82eb22e | 2019-04-10 01:15:43 -0400 | [diff] [blame] | 758 | // Creates an Object for the instruction's result in 'defs'. |
| 759 | void DefineResult(const InsnIterator &insn); |
| 760 | |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 761 | // Returns true if data in the given storage class is word-interleaved |
Ben Clayton | ff1cede | 2019-03-08 08:23:34 +0000 | [diff] [blame] | 762 | // by each SIMD vector lane, otherwise data is stored linerally. |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 763 | // |
Ben Clayton | ff1cede | 2019-03-08 08:23:34 +0000 | [diff] [blame] | 764 | // Each lane addresses a single word, picked by a base pointer and an |
| 765 | // integer offset. |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 766 | // |
Ben Clayton | ff1cede | 2019-03-08 08:23:34 +0000 | [diff] [blame] | 767 | // A word is currently 32 bits (single float, int32_t, uint32_t). |
| 768 | // A lane is a single element of a SIMD vector register. |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 769 | // |
Ben Clayton | ff1cede | 2019-03-08 08:23:34 +0000 | [diff] [blame] | 770 | // Storage interleaved by lane - (IsStorageInterleavedByLane() == true): |
| 771 | // --------------------------------------------------------------------- |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 772 | // |
Ben Clayton | ff1cede | 2019-03-08 08:23:34 +0000 | [diff] [blame] | 773 | // Address = PtrBase + sizeof(Word) * (SIMD::Width * LaneOffset + LaneIndex) |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 774 | // |
Ben Clayton | ff1cede | 2019-03-08 08:23:34 +0000 | [diff] [blame] | 775 | // Assuming SIMD::Width == 4: |
| 776 | // |
| 777 | // Lane[0] | Lane[1] | Lane[2] | Lane[3] |
| 778 | // ===========+===========+===========+========== |
| 779 | // LaneOffset=0: | Word[0] | Word[1] | Word[2] | Word[3] |
| 780 | // ---------------+-----------+-----------+-----------+---------- |
| 781 | // LaneOffset=1: | Word[4] | Word[5] | Word[6] | Word[7] |
| 782 | // ---------------+-----------+-----------+-----------+---------- |
| 783 | // LaneOffset=2: | Word[8] | Word[9] | Word[a] | Word[b] |
| 784 | // ---------------+-----------+-----------+-----------+---------- |
| 785 | // LaneOffset=3: | Word[c] | Word[d] | Word[e] | Word[f] |
| 786 | // |
| 787 | // |
| 788 | // Linear storage - (IsStorageInterleavedByLane() == false): |
| 789 | // --------------------------------------------------------- |
| 790 | // |
| 791 | // Address = PtrBase + sizeof(Word) * LaneOffset |
| 792 | // |
| 793 | // Lane[0] | Lane[1] | Lane[2] | Lane[3] |
| 794 | // ===========+===========+===========+========== |
| 795 | // LaneOffset=0: | Word[0] | Word[0] | Word[0] | Word[0] |
| 796 | // ---------------+-----------+-----------+-----------+---------- |
| 797 | // LaneOffset=1: | Word[1] | Word[1] | Word[1] | Word[1] |
| 798 | // ---------------+-----------+-----------+-----------+---------- |
| 799 | // LaneOffset=2: | Word[2] | Word[2] | Word[2] | Word[2] |
| 800 | // ---------------+-----------+-----------+-----------+---------- |
| 801 | // LaneOffset=3: | Word[3] | Word[3] | Word[3] | Word[3] |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 802 | // |
| 803 | static bool IsStorageInterleavedByLane(spv::StorageClass storageClass); |
| 804 | |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 805 | template<typename F> |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 806 | int VisitInterfaceInner(Type::ID id, Decorations d, F f) const; |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 807 | |
Chris Forbes | b8fb08a | 2019-02-13 11:45:27 -0800 | [diff] [blame] | 808 | template<typename F> |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 809 | void VisitInterface(Object::ID id, F f) const; |
Chris Forbes | 5839dcf | 2018-12-10 19:02:58 -0800 | [diff] [blame] | 810 | |
Chris Forbes | e6419ad | 2019-04-11 12:23:10 -0700 | [diff] [blame] | 811 | template<typename F> |
| 812 | void VisitMemoryObject(Object::ID id, F f) const; |
| 813 | |
| 814 | template<typename F> |
| 815 | void VisitMemoryObjectInner(Type::ID id, Decorations d, uint32_t &index, uint32_t offset, F f) const; |
| 816 | |
Chris Forbes | 1ca8acd | 2019-02-20 13:00:54 -0800 | [diff] [blame] | 817 | Object& CreateConstant(InsnIterator it); |
Chris Forbes | bde3408 | 2018-12-28 12:03:10 -0800 | [diff] [blame] | 818 | |
Chris Forbes | 049ff38 | 2019-02-02 15:16:43 -0800 | [diff] [blame] | 819 | void ProcessInterfaceVariable(Object &object); |
Chris Forbes | 38f85b3 | 2019-02-12 20:10:05 +0000 | [diff] [blame] | 820 | |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 821 | // Returns a SIMD::Pointer to the underlying data for the given pointer |
| 822 | // object. |
| 823 | // Handles objects of the following kinds: |
Ben Clayton | 6b51134 | 2019-04-05 12:12:30 +0100 | [diff] [blame] | 824 | // • DescriptorSet |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 825 | // • DivergentPointer |
| 826 | // • InterfaceVariable |
| 827 | // • NonDivergentPointer |
| 828 | // Calling GetPointerToData with objects of any other kind will assert. |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 829 | SIMD::Pointer GetPointerToData(Object::ID id, int arrayIndex, SpirvRoutine *routine) const; |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 830 | |
Ben Clayton | 3d49738 | 2019-04-08 16:16:12 -0400 | [diff] [blame] | 831 | SIMD::Pointer WalkExplicitLayoutAccessChain(Object::ID id, uint32_t numIndexes, uint32_t const *indexIds, SpirvRoutine *routine) const; |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 832 | SIMD::Pointer WalkAccessChain(Object::ID id, uint32_t numIndexes, uint32_t const *indexIds, SpirvRoutine *routine) const; |
Ben Clayton | 97035bd | 2019-04-16 11:35:38 -0400 | [diff] [blame] | 833 | |
| 834 | // Returns the *component* offset in the literal for the given access chain. |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 835 | uint32_t WalkLiteralAccessChain(Type::ID id, uint32_t numIndexes, uint32_t const *indexes) const; |
Ben Clayton | d4e4c66 | 2019-02-26 11:54:34 +0000 | [diff] [blame] | 836 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 837 | // EmitState holds control-flow state for the emit() pass. |
| 838 | class EmitState |
| 839 | { |
| 840 | public: |
Nicolas Capens | 09591b8 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 841 | EmitState(SpirvRoutine *routine, RValue<SIMD::Int> activeLaneMask, const vk::DescriptorSet::Bindings &descriptorSets) |
| 842 | : routine(routine), |
| 843 | activeLaneMaskValue(activeLaneMask.value), |
| 844 | descriptorSets(descriptorSets) |
| 845 | { |
| 846 | } |
| 847 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 848 | RValue<SIMD::Int> activeLaneMask() const |
| 849 | { |
| 850 | ASSERT(activeLaneMaskValue != nullptr); |
| 851 | return RValue<SIMD::Int>(activeLaneMaskValue); |
| 852 | } |
| 853 | |
| 854 | void setActiveLaneMask(RValue<SIMD::Int> mask) |
| 855 | { |
| 856 | activeLaneMaskValue = mask.value; |
| 857 | } |
| 858 | |
| 859 | // Add a new active lane mask edge from the current block to out. |
| 860 | // The edge mask value will be (mask AND activeLaneMaskValue). |
| 861 | // If multiple active lane masks are added for the same edge, then |
| 862 | // they will be ORed together. |
| 863 | void addOutputActiveLaneMaskEdge(Block::ID out, RValue<SIMD::Int> mask); |
| 864 | |
| 865 | // Add a new active lane mask for the edge from -> to. |
| 866 | // If multiple active lane masks are added for the same edge, then |
| 867 | // they will be ORed together. |
| 868 | void addActiveLaneMaskEdge(Block::ID from, Block::ID to, RValue<SIMD::Int> mask); |
| 869 | |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 870 | SpirvRoutine *routine = nullptr; // The current routine being built. |
| 871 | rr::Value *activeLaneMaskValue = nullptr; // The current active lane mask. |
| 872 | Block::ID currentBlock; // The current block being built. |
| 873 | Block::Set visited; // Blocks already built. |
| 874 | std::unordered_map<Block::Edge, RValue<SIMD::Int>, Block::Edge::Hash> edgeActiveLaneMasks; |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 875 | std::queue<Block::ID> *pending; |
Nicolas Capens | 09591b8 | 2019-04-08 22:51:08 -0400 | [diff] [blame] | 876 | |
| 877 | const vk::DescriptorSet::Bindings &descriptorSets; |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 878 | }; |
| 879 | |
| 880 | // EmitResult is an enumerator of result values from the Emit functions. |
| 881 | enum class EmitResult |
| 882 | { |
| 883 | Continue, // No termination instructions. |
| 884 | Terminator, // Reached a termination instruction. |
| 885 | }; |
| 886 | |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 887 | // existsPath returns true if there's a direct or indirect flow from |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 888 | // the 'from' block to the 'to' block that does not pass through |
| 889 | // notPassingThrough. |
| 890 | bool existsPath(Block::ID from, Block::ID to, Block::ID notPassingThrough) const; |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 891 | |
Ben Clayton | fe3f013 | 2019-03-26 11:10:16 +0000 | [diff] [blame] | 892 | // Lookup the active lane mask for the edge from -> to. |
| 893 | // If from is unreachable, then a mask of all zeros is returned. |
| 894 | // Asserts if from is reachable and the edge does not exist. |
| 895 | RValue<SIMD::Int> GetActiveLaneMaskEdge(EmitState *state, Block::ID from, Block::ID to) const; |
| 896 | |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 897 | // Emit all the unvisited blocks (except for ignore) in BFS order, |
| 898 | // starting with id. |
| 899 | void EmitBlocks(Block::ID id, EmitState *state, Block::ID ignore = 0) const; |
| 900 | void EmitNonLoop(EmitState *state) const; |
Ben Clayton | e747b3c | 2019-03-21 19:35:15 +0000 | [diff] [blame] | 901 | void EmitLoop(EmitState *state) const; |
Ben Clayton | 513ed1d | 2019-03-28 16:07:00 +0000 | [diff] [blame] | 902 | |
| 903 | void EmitInstructions(InsnIterator begin, InsnIterator end, EmitState *state) const; |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 904 | EmitResult EmitInstruction(InsnIterator insn, EmitState *state) const; |
| 905 | |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 906 | // Emit pass instructions: |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 907 | EmitResult EmitVariable(InsnIterator insn, EmitState *state) const; |
| 908 | EmitResult EmitLoad(InsnIterator insn, EmitState *state) const; |
| 909 | EmitResult EmitStore(InsnIterator insn, EmitState *state) const; |
| 910 | EmitResult EmitAccessChain(InsnIterator insn, EmitState *state) const; |
| 911 | EmitResult EmitCompositeConstruct(InsnIterator insn, EmitState *state) const; |
| 912 | EmitResult EmitCompositeInsert(InsnIterator insn, EmitState *state) const; |
| 913 | EmitResult EmitCompositeExtract(InsnIterator insn, EmitState *state) const; |
| 914 | EmitResult EmitVectorShuffle(InsnIterator insn, EmitState *state) const; |
| 915 | EmitResult EmitVectorTimesScalar(InsnIterator insn, EmitState *state) const; |
Chris Forbes | 06f4ed7 | 2019-03-28 09:53:20 +1300 | [diff] [blame] | 916 | EmitResult EmitMatrixTimesVector(InsnIterator insn, EmitState *state) const; |
Chris Forbes | a563dd8 | 2019-03-28 10:32:55 +1300 | [diff] [blame] | 917 | EmitResult EmitVectorTimesMatrix(InsnIterator insn, EmitState *state) const; |
Chris Forbes | 51562f1 | 2019-03-28 19:08:39 -0700 | [diff] [blame] | 918 | EmitResult EmitMatrixTimesMatrix(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 3ee5299 | 2019-04-08 11:01:23 -0400 | [diff] [blame] | 919 | EmitResult EmitOuterProduct(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 620f708 | 2019-04-08 11:12:08 -0400 | [diff] [blame] | 920 | EmitResult EmitTranspose(InsnIterator insn, EmitState *state) const; |
Ben Clayton | c0cf68b | 2019-03-21 17:46:08 +0000 | [diff] [blame] | 921 | EmitResult EmitVectorExtractDynamic(InsnIterator insn, EmitState *state) const; |
| 922 | EmitResult EmitVectorInsertDynamic(InsnIterator insn, EmitState *state) const; |
| 923 | EmitResult EmitUnaryOp(InsnIterator insn, EmitState *state) const; |
| 924 | EmitResult EmitBinaryOp(InsnIterator insn, EmitState *state) const; |
| 925 | EmitResult EmitDot(InsnIterator insn, EmitState *state) const; |
| 926 | EmitResult EmitSelect(InsnIterator insn, EmitState *state) const; |
| 927 | EmitResult EmitExtendedInstruction(InsnIterator insn, EmitState *state) const; |
| 928 | EmitResult EmitAny(InsnIterator insn, EmitState *state) const; |
| 929 | EmitResult EmitAll(InsnIterator insn, EmitState *state) const; |
| 930 | EmitResult EmitBranch(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 931 | EmitResult EmitBranchConditional(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 213a8ce | 2019-03-21 18:57:23 +0000 | [diff] [blame] | 932 | EmitResult EmitSwitch(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 933 | EmitResult EmitUnreachable(InsnIterator insn, EmitState *state) const; |
| 934 | EmitResult EmitReturn(InsnIterator insn, EmitState *state) const; |
Chris Forbes | 97e9589 | 2019-04-02 13:37:37 +1300 | [diff] [blame] | 935 | EmitResult EmitKill(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 9fd02e0 | 2019-03-21 18:47:15 +0000 | [diff] [blame] | 936 | EmitResult EmitPhi(InsnIterator insn, EmitState *state) const; |
Nicolas Capens | 5b09dd1 | 2019-04-30 01:05:28 -0400 | [diff] [blame] | 937 | EmitResult EmitImageSampleImplicitLod(Variant variant, InsnIterator insn, EmitState *state) const; |
| 938 | EmitResult EmitImageSampleExplicitLod(Variant variant, InsnIterator insn, EmitState *state) const; |
Chris Forbes | cd63159 | 2019-04-27 10:37:18 -0700 | [diff] [blame] | 939 | EmitResult EmitImageFetch(InsnIterator insn, EmitState *state) const; |
Nicolas Capens | 7889633 | 2019-04-29 16:41:50 -0400 | [diff] [blame] | 940 | EmitResult EmitImageSample(ImageInstruction instruction, InsnIterator insn, EmitState *state) const; |
Chris Forbes | b0d00ea | 2019-04-17 20:24:20 -0700 | [diff] [blame] | 941 | EmitResult EmitImageQuerySize(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 0264d8e | 2019-05-08 15:39:40 +0100 | [diff] [blame] | 942 | EmitResult EmitImageQuerySizeLod(InsnIterator insn, EmitState *state) const; |
Ben Clayton | b4001ed | 2019-05-10 10:21:00 +0100 | [diff] [blame] | 943 | EmitResult EmitImageQueryLevels(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 2568cf7 | 2019-05-10 11:53:14 +0100 | [diff] [blame] | 944 | EmitResult EmitImageQuerySamples(InsnIterator insn, EmitState *state) const; |
Chris Forbes | 2f7f2ec | 2019-04-17 16:58:15 -0700 | [diff] [blame] | 945 | EmitResult EmitImageRead(InsnIterator insn, EmitState *state) const; |
Chris Forbes | 179f014 | 2019-04-17 20:24:44 -0700 | [diff] [blame] | 946 | EmitResult EmitImageWrite(InsnIterator insn, EmitState *state) const; |
Chris Forbes | b51f2c1 | 2019-04-18 11:01:30 -0700 | [diff] [blame] | 947 | EmitResult EmitImageTexelPointer(InsnIterator insn, EmitState *state) const; |
Chris Forbes | 1781393 | 2019-04-18 11:45:54 -0700 | [diff] [blame] | 948 | EmitResult EmitAtomicOp(InsnIterator insn, EmitState *state) const; |
Chris Forbes | a16238d | 2019-04-18 16:31:54 -0700 | [diff] [blame] | 949 | EmitResult EmitAtomicCompareExchange(InsnIterator insn, EmitState *state) const; |
Chris Forbes | fa82c34 | 2019-04-26 16:42:38 -0700 | [diff] [blame] | 950 | EmitResult EmitSampledImageCombineOrSplit(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 78abf37 | 2019-05-09 15:11:58 +0100 | [diff] [blame] | 951 | EmitResult EmitCopyObject(InsnIterator insn, EmitState *state) const; |
Ben Clayton | b5a4546 | 2019-04-30 19:21:29 +0100 | [diff] [blame] | 952 | EmitResult EmitCopyMemory(InsnIterator insn, EmitState *state) const; |
Ben Clayton | ecfeede | 2019-05-08 08:51:01 +0100 | [diff] [blame] | 953 | EmitResult EmitControlBarrier(InsnIterator insn, EmitState *state) const; |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 954 | EmitResult EmitMemoryBarrier(InsnIterator insn, EmitState *state) const; |
Ben Clayton | 32d4797 | 2019-04-19 17:08:15 -0400 | [diff] [blame] | 955 | EmitResult EmitGroupNonUniform(InsnIterator insn, EmitState *state) const; |
Ben Clayton | e4605da | 2019-05-09 16:24:01 +0100 | [diff] [blame] | 956 | EmitResult EmitArrayLength(InsnIterator insn, EmitState *state) const; |
Ben Clayton | dfc0f3b | 2019-02-26 12:19:48 +0000 | [diff] [blame] | 957 | |
Ben Clayton | 0264d8e | 2019-05-08 15:39:40 +0100 | [diff] [blame] | 958 | void GetImageDimensions(SpirvRoutine const *routine, Type const &resultTy, Object::ID imageId, Object::ID lodId, Intermediate &dst) const; |
| 959 | SIMD::Pointer GetTexelAddress(SpirvRoutine const *routine, SIMD::Pointer base, GenericValue const & coordinate, Type const & imageType, Pointer<Byte> descriptor, int texelSize, Object::ID sampleId, bool useStencilAspect) const; |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 960 | uint32_t GetConstScalarInt(Object::ID id) const; |
Chris Forbes | ea81ab7 | 2019-05-14 15:20:33 -0700 | [diff] [blame] | 961 | void EvalSpecConstantOp(InsnIterator insn); |
| 962 | void EvalSpecConstantUnaryOp(InsnIterator insn); |
| 963 | void EvalSpecConstantBinaryOp(InsnIterator insn); |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 964 | |
Ben Clayton | 69c3749 | 2019-05-13 17:31:16 +0100 | [diff] [blame] | 965 | // LoadPhi loads the phi values from the alloca storage and places the |
| 966 | // load values into the intermediate with the phi's result id. |
| 967 | void LoadPhi(InsnIterator insn, EmitState *state) const; |
| 968 | |
| 969 | // StorePhi updates the phi's alloca storage value using the incoming |
| 970 | // values from blocks that are both in the OpPhi instruction and in |
| 971 | // filter. |
| 972 | void StorePhi(InsnIterator insn, EmitState *state, std::unordered_set<SpirvShader::Block::ID> const& filter) const; |
| 973 | |
Ben Clayton | b16c586 | 2019-05-08 14:01:38 +0100 | [diff] [blame] | 974 | // Emits a rr::Fence for the given MemorySemanticsMask. |
| 975 | void Fence(spv::MemorySemanticsMask semantics) const; |
Chris Forbes | 89c37a4 | 2019-04-17 18:28:33 -0700 | [diff] [blame] | 976 | |
Ben Clayton | ecfeede | 2019-05-08 08:51:01 +0100 | [diff] [blame] | 977 | // Helper for calling rr::Yield with res cast to an rr::Int. |
| 978 | void Yield(YieldResult res) const; |
| 979 | |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 980 | // OpcodeName() returns the name of the opcode op. |
| 981 | // If NDEBUG is defined, then OpcodeName() will only return the numerical code. |
Ben Clayton | d4e4c66 | 2019-02-26 11:54:34 +0000 | [diff] [blame] | 982 | static std::string OpcodeName(spv::Op op); |
Nicolas Capens | 86509d9 | 2019-03-21 13:23:50 -0400 | [diff] [blame] | 983 | static std::memory_order MemoryOrder(spv::MemorySemanticsMask memorySemantics); |
Chris Forbes | 868ed90 | 2019-03-13 17:39:45 -0700 | [diff] [blame] | 984 | |
| 985 | // Helper as we often need to take dot products as part of doing other things. |
| 986 | SIMD::Float Dot(unsigned numComponents, GenericValue const & x, GenericValue const & y) const; |
Ben Clayton | fc77af1 | 2019-04-09 10:48:00 -0400 | [diff] [blame] | 987 | |
Chris Forbes | 50e6493 | 2019-04-08 17:49:27 -0700 | [diff] [blame] | 988 | SIMD::UInt FloatToHalfBits(SIMD::UInt floatBits, bool storeInUpperBits) const; |
Ben Clayton | fc77af1 | 2019-04-09 10:48:00 -0400 | [diff] [blame] | 989 | |
| 990 | // Splits x into a floating-point significand in the range [0.5, 1.0) |
| 991 | // and an integral exponent of two, such that: |
| 992 | // x = significand * 2^exponent |
| 993 | // Returns the pair <significand, exponent> |
| 994 | std::pair<SIMD::Float, SIMD::Int> Frexp(RValue<SIMD::Float> val) const; |
Ben Clayton | 96fbe08 | 2019-04-16 19:28:11 -0400 | [diff] [blame] | 995 | |
Chris Forbes | 45f9a93 | 2019-05-08 13:30:38 -0700 | [diff] [blame] | 996 | static ImageSampler *getImageSampler(uint32_t instruction, vk::SampledImageDescriptor const *imageDescriptor, const vk::Sampler *sampler); |
Nicolas Capens | 1e7120e | 2019-04-30 17:33:26 -0400 | [diff] [blame] | 997 | static ImageSampler *emitSamplerFunction(ImageInstruction instruction, const Sampler &samplerState); |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 998 | |
| 999 | // TODO(b/129523279): Eliminate conversion and use vk::Sampler members directly. |
Nicolas Capens | a47a516 | 2019-04-24 02:41:27 -0400 | [diff] [blame] | 1000 | static sw::TextureType convertTextureType(VkImageViewType imageViewType); |
Nicolas Capens | 9e73510 | 2019-04-18 15:03:06 -0400 | [diff] [blame] | 1001 | static sw::FilterType convertFilterMode(const vk::Sampler *sampler); |
| 1002 | static sw::MipmapType convertMipmapMode(const vk::Sampler *sampler); |
Nicolas Capens | 6a12e09 | 2019-04-29 17:26:51 -0400 | [diff] [blame] | 1003 | static sw::AddressingMode convertAddressingMode(int coordinateIndex, VkSamplerAddressMode addressMode, VkImageViewType imageViewType); |
Ben Clayton | 60f15ec | 2019-05-09 17:50:01 +0100 | [diff] [blame] | 1004 | static VkShaderStageFlagBits executionModelToStage(spv::ExecutionModel model); |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 1005 | }; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1006 | |
| 1007 | class SpirvRoutine |
| 1008 | { |
| 1009 | public: |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 1010 | SpirvRoutine(vk::PipelineLayout const *pipelineLayout); |
| 1011 | |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 1012 | using Variable = Array<SIMD::Float>; |
Ben Clayton | 24ea515 | 2019-02-26 11:02:42 +0000 | [diff] [blame] | 1013 | |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 1014 | vk::PipelineLayout const * const pipelineLayout; |
| 1015 | |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 1016 | std::unordered_map<SpirvShader::Object::ID, Variable> variables; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1017 | |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 1018 | std::unordered_map<SpirvShader::Object::ID, Intermediate> intermediates; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1019 | |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1020 | std::unordered_map<SpirvShader::Object::ID, SIMD::Pointer> pointers; |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 1021 | |
Ben Clayton | 69c3749 | 2019-05-13 17:31:16 +0100 | [diff] [blame] | 1022 | std::unordered_map<SpirvShader::Object::ID, Variable> phis; |
| 1023 | |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 1024 | Variable inputs = Variable{MAX_INTERFACE_COMPONENTS}; |
| 1025 | Variable outputs = Variable{MAX_INTERFACE_COMPONENTS}; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1026 | |
Ben Clayton | ecd3848 | 2019-04-19 17:11:08 -0400 | [diff] [blame] | 1027 | Pointer<Byte> workgroupMemory; |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 1028 | Pointer<Pointer<Byte>> descriptorSets; |
| 1029 | Pointer<Int> descriptorDynamicOffsets; |
Chris Forbes | a30de54 | 2019-03-18 18:51:55 -0700 | [diff] [blame] | 1030 | Pointer<Byte> pushConstants; |
Chris Forbes | 548e366 | 2019-04-25 10:00:06 -0700 | [diff] [blame] | 1031 | Pointer<Byte> constants; |
Chris Forbes | 97e9589 | 2019-04-02 13:37:37 +1300 | [diff] [blame] | 1032 | Int killMask = Int{0}; |
Chris Forbes | 2446604 | 2019-04-22 10:54:23 -0700 | [diff] [blame] | 1033 | SIMD::Int windowSpacePosition[2]; |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 1034 | |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 1035 | void createVariable(SpirvShader::Object::ID id, uint32_t size) |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1036 | { |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 1037 | bool added = variables.emplace(id, Variable(size)).second; |
| 1038 | ASSERT_MSG(added, "Variable %d created twice", id.value()); |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1041 | void createPointer(SpirvShader::Object::ID id, SIMD::Pointer ptr) |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 1042 | { |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1043 | bool added = pointers.emplace(id, ptr).second; |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 1044 | ASSERT_MSG(added, "Pointer %d created twice", id.value()); |
| 1045 | } |
| 1046 | |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 1047 | Intermediate& createIntermediate(SpirvShader::Object::ID id, uint32_t size) |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1048 | { |
Chris Forbes | 928dfee | 2019-02-26 21:22:32 -0800 | [diff] [blame] | 1049 | auto it = intermediates.emplace(std::piecewise_construct, |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1050 | std::forward_as_tuple(id), |
| 1051 | std::forward_as_tuple(size)); |
Ben Clayton | 0e3d328 | 2019-04-04 15:53:04 +0100 | [diff] [blame] | 1052 | ASSERT_MSG(it.second, "Intermediate %d created twice", id.value()); |
Chris Forbes | 928dfee | 2019-02-26 21:22:32 -0800 | [diff] [blame] | 1053 | return it.first->second; |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 1056 | Variable& getVariable(SpirvShader::Object::ID id) |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1057 | { |
Ben Clayton | 4774761 | 2019-04-04 16:27:35 +0100 | [diff] [blame] | 1058 | auto it = variables.find(id); |
| 1059 | ASSERT_MSG(it != variables.end(), "Unknown variables %d", id.value()); |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1060 | return it->second; |
| 1061 | } |
| 1062 | |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 1063 | Intermediate const& getIntermediate(SpirvShader::Object::ID id) const |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1064 | { |
| 1065 | auto it = intermediates.find(id); |
Ben Clayton | af26cfe | 2019-03-21 17:32:44 +0000 | [diff] [blame] | 1066 | ASSERT_MSG(it != intermediates.end(), "Unknown intermediate %d", id.value()); |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1067 | return it->second; |
| 1068 | } |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 1069 | |
Ben Clayton | 5f7e911 | 2019-04-16 11:03:40 -0400 | [diff] [blame] | 1070 | SIMD::Pointer const& getPointer(SpirvShader::Object::ID id) const |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 1071 | { |
Ben Clayton | 484e08e | 2019-04-05 12:11:39 +0100 | [diff] [blame] | 1072 | auto it = pointers.find(id); |
| 1073 | ASSERT_MSG(it != pointers.end(), "Unknown pointer %d", id.value()); |
Ben Clayton | 831db96 | 2019-02-27 14:57:18 +0000 | [diff] [blame] | 1074 | return it->second; |
| 1075 | } |
Ben Clayton | ab51bbf | 2019-02-20 14:36:27 +0000 | [diff] [blame] | 1076 | }; |
| 1077 | |
Chris Forbes | d30b5ac | 2019-02-26 21:53:56 -0800 | [diff] [blame] | 1078 | class GenericValue |
| 1079 | { |
| 1080 | // Generic wrapper over either per-lane intermediate value, or a constant. |
| 1081 | // Constants are transparently widened to per-lane values in operator[]. |
| 1082 | // This is appropriate in most cases -- if we're not going to do something |
| 1083 | // significantly different based on whether the value is uniform across lanes. |
| 1084 | |
| 1085 | SpirvShader::Object const &obj; |
| 1086 | Intermediate const *intermediate; |
| 1087 | |
| 1088 | public: |
Ben Clayton | af973b6 | 2019-03-13 18:19:20 +0000 | [diff] [blame] | 1089 | GenericValue(SpirvShader const *shader, SpirvRoutine const *routine, SpirvShader::Object::ID objId) : |
Chris Forbes | d30b5ac | 2019-02-26 21:53:56 -0800 | [diff] [blame] | 1090 | obj(shader->getObject(objId)), |
Ben Clayton | 16ab9e9 | 2019-04-08 10:57:35 -0400 | [diff] [blame] | 1091 | intermediate(obj.kind == SpirvShader::Object::Kind::Intermediate ? &routine->getIntermediate(objId) : nullptr), |
| 1092 | type(obj.type) {} |
Chris Forbes | d30b5ac | 2019-02-26 21:53:56 -0800 | [diff] [blame] | 1093 | |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 1094 | RValue<SIMD::Float> Float(uint32_t i) const |
Chris Forbes | d30b5ac | 2019-02-26 21:53:56 -0800 | [diff] [blame] | 1095 | { |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 1096 | if (intermediate != nullptr) |
| 1097 | { |
| 1098 | return intermediate->Float(i); |
| 1099 | } |
Chris Forbes | d30b5ac | 2019-02-26 21:53:56 -0800 | [diff] [blame] | 1100 | auto constantValue = reinterpret_cast<float *>(obj.constantValue.get()); |
Ben Clayton | 24ea515 | 2019-02-26 11:02:42 +0000 | [diff] [blame] | 1101 | return RValue<SIMD::Float>(constantValue[i]); |
Chris Forbes | d30b5ac | 2019-02-26 21:53:56 -0800 | [diff] [blame] | 1102 | } |
Ben Clayton | 9b62c5e | 2019-03-08 09:32:34 +0000 | [diff] [blame] | 1103 | |
| 1104 | RValue<SIMD::Int> Int(uint32_t i) const |
| 1105 | { |
| 1106 | return As<SIMD::Int>(Float(i)); |
| 1107 | } |
| 1108 | |
| 1109 | RValue<SIMD::UInt> UInt(uint32_t i) const |
| 1110 | { |
| 1111 | return As<SIMD::UInt>(Float(i)); |
| 1112 | } |
Ben Clayton | 16ab9e9 | 2019-04-08 10:57:35 -0400 | [diff] [blame] | 1113 | |
| 1114 | SpirvShader::Type::ID const type; |
Chris Forbes | d30b5ac | 2019-02-26 21:53:56 -0800 | [diff] [blame] | 1115 | }; |
| 1116 | |
Chris Forbes | af4ed53 | 2018-12-06 18:33:27 -0800 | [diff] [blame] | 1117 | } |
| 1118 | |
Chris Forbes | c25b807 | 2018-12-10 15:10:39 -0800 | [diff] [blame] | 1119 | #endif // sw_SpirvShader_hpp |