Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1 | /* |
Hans-Kristian Arntzen | 4704482 | 2021-01-14 16:07:49 +0100 | [diff] [blame] | 2 | * Copyright 2015-2021 Arm Limited |
Jon Leech | f2a6554 | 2021-05-08 01:47:48 -0700 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 OR MIT |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
Hans-Kristian Arntzen | cf1e9e0 | 2020-11-25 15:22:08 +0100 | [diff] [blame] | 18 | /* |
| 19 | * At your option, you may choose to accept this material under either: |
| 20 | * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or |
| 21 | * 2. The MIT License, found at <http://opensource.org/licenses/MIT>. |
Hans-Kristian Arntzen | cf1e9e0 | 2020-11-25 15:22:08 +0100 | [diff] [blame] | 22 | */ |
| 23 | |
Hans-Kristian Arntzen | dad4a34 | 2016-11-11 18:04:14 +0100 | [diff] [blame] | 24 | #ifndef SPIRV_CROSS_COMMON_HPP |
| 25 | #define SPIRV_CROSS_COMMON_HPP |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 26 | |
Hans-Kristian Arntzen | bf5c075 | 2017-03-25 16:28:44 +0100 | [diff] [blame] | 27 | #include "spirv.hpp" |
Hans-Kristian Arntzen | 03ddea8 | 2019-04-09 13:07:30 +0200 | [diff] [blame] | 28 | #include "spirv_cross_containers.hpp" |
Hans-Kristian Arntzen | 3fa00f9 | 2019-04-09 15:10:02 +0200 | [diff] [blame] | 29 | #include "spirv_cross_error_handling.hpp" |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 30 | #include <functional> |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 31 | |
Hans-Kristian Arntzen | 9b92e68 | 2019-03-29 10:29:44 +0100 | [diff] [blame] | 32 | // A bit crude, but allows projects which embed SPIRV-Cross statically to |
| 33 | // effectively hide all the symbols from other projects. |
| 34 | // There is a case where we have: |
| 35 | // - Project A links against SPIRV-Cross statically. |
| 36 | // - Project A links against Project B statically. |
| 37 | // - Project B links against SPIRV-Cross statically (might be a different version). |
| 38 | // This leads to a conflict with extremely bizarre results. |
| 39 | // By overriding the namespace in one of the project builds, we can work around this. |
| 40 | // If SPIRV-Cross is embedded in dynamic libraries, |
| 41 | // prefer using -fvisibility=hidden on GCC/Clang instead. |
| 42 | #ifdef SPIRV_CROSS_NAMESPACE_OVERRIDE |
| 43 | #define SPIRV_CROSS_NAMESPACE SPIRV_CROSS_NAMESPACE_OVERRIDE |
| 44 | #else |
| 45 | #define SPIRV_CROSS_NAMESPACE spirv_cross |
| 46 | #endif |
| 47 | |
| 48 | namespace SPIRV_CROSS_NAMESPACE |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 49 | { |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 50 | namespace inner |
| 51 | { |
| 52 | template <typename T> |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 53 | void join_helper(StringStream<> &stream, T &&t) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 54 | { |
| 55 | stream << std::forward<T>(t); |
| 56 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 57 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 58 | template <typename T, typename... Ts> |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 59 | void join_helper(StringStream<> &stream, T &&t, Ts &&... ts) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 60 | { |
| 61 | stream << std::forward<T>(t); |
| 62 | join_helper(stream, std::forward<Ts>(ts)...); |
| 63 | } |
Hans-Kristian Arntzen | 382101b | 2018-04-03 14:08:15 +0200 | [diff] [blame] | 64 | } // namespace inner |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 65 | |
Hans-Kristian Arntzen | e8e5884 | 2018-03-12 13:09:25 +0100 | [diff] [blame] | 66 | class Bitset |
| 67 | { |
| 68 | public: |
| 69 | Bitset() = default; |
| 70 | explicit inline Bitset(uint64_t lower_) |
Hans-Kristian Arntzen | 719cf9d | 2018-03-13 14:05:33 +0100 | [diff] [blame] | 71 | : lower(lower_) |
Hans-Kristian Arntzen | e8e5884 | 2018-03-12 13:09:25 +0100 | [diff] [blame] | 72 | { |
| 73 | } |
| 74 | |
| 75 | inline bool get(uint32_t bit) const |
| 76 | { |
| 77 | if (bit < 64) |
| 78 | return (lower & (1ull << bit)) != 0; |
| 79 | else |
| 80 | return higher.count(bit) != 0; |
| 81 | } |
| 82 | |
| 83 | inline void set(uint32_t bit) |
| 84 | { |
| 85 | if (bit < 64) |
| 86 | lower |= 1ull << bit; |
| 87 | else |
| 88 | higher.insert(bit); |
| 89 | } |
| 90 | |
| 91 | inline void clear(uint32_t bit) |
| 92 | { |
| 93 | if (bit < 64) |
| 94 | lower &= ~(1ull << bit); |
| 95 | else |
| 96 | higher.erase(bit); |
| 97 | } |
| 98 | |
| 99 | inline uint64_t get_lower() const |
| 100 | { |
| 101 | return lower; |
| 102 | } |
| 103 | |
| 104 | inline void reset() |
| 105 | { |
| 106 | lower = 0; |
| 107 | higher.clear(); |
| 108 | } |
| 109 | |
| 110 | inline void merge_and(const Bitset &other) |
| 111 | { |
| 112 | lower &= other.lower; |
| 113 | std::unordered_set<uint32_t> tmp_set; |
| 114 | for (auto &v : higher) |
| 115 | if (other.higher.count(v) != 0) |
| 116 | tmp_set.insert(v); |
| 117 | higher = std::move(tmp_set); |
| 118 | } |
| 119 | |
| 120 | inline void merge_or(const Bitset &other) |
| 121 | { |
| 122 | lower |= other.lower; |
| 123 | for (auto &v : other.higher) |
| 124 | higher.insert(v); |
| 125 | } |
| 126 | |
| 127 | inline bool operator==(const Bitset &other) const |
| 128 | { |
| 129 | if (lower != other.lower) |
| 130 | return false; |
| 131 | |
| 132 | if (higher.size() != other.higher.size()) |
| 133 | return false; |
| 134 | |
| 135 | for (auto &v : higher) |
| 136 | if (other.higher.count(v) == 0) |
| 137 | return false; |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | inline bool operator!=(const Bitset &other) const |
| 143 | { |
| 144 | return !(*this == other); |
| 145 | } |
| 146 | |
| 147 | template <typename Op> |
| 148 | void for_each_bit(const Op &op) const |
| 149 | { |
| 150 | // TODO: Add ctz-based iteration. |
| 151 | for (uint32_t i = 0; i < 64; i++) |
| 152 | { |
| 153 | if (lower & (1ull << i)) |
| 154 | op(i); |
| 155 | } |
| 156 | |
| 157 | if (higher.empty()) |
| 158 | return; |
| 159 | |
| 160 | // Need to enforce an order here for reproducible results, |
| 161 | // but hitting this path should happen extremely rarely, so having this slow path is fine. |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 162 | SmallVector<uint32_t> bits; |
Hans-Kristian Arntzen | e8e5884 | 2018-03-12 13:09:25 +0100 | [diff] [blame] | 163 | bits.reserve(higher.size()); |
| 164 | for (auto &v : higher) |
| 165 | bits.push_back(v); |
| 166 | std::sort(std::begin(bits), std::end(bits)); |
| 167 | |
| 168 | for (auto &v : bits) |
| 169 | op(v); |
| 170 | } |
| 171 | |
| 172 | inline bool empty() const |
| 173 | { |
| 174 | return lower == 0 && higher.empty(); |
| 175 | } |
| 176 | |
| 177 | private: |
| 178 | // The most common bits to set are all lower than 64, |
| 179 | // so optimize for this case. Bits spilling outside 64 go into a slower data structure. |
| 180 | // In almost all cases, higher data structure will not be used. |
| 181 | uint64_t lower = 0; |
| 182 | std::unordered_set<uint32_t> higher; |
| 183 | }; |
| 184 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 185 | // Helper template to avoid lots of nasty string temporary munging. |
| 186 | template <typename... Ts> |
| 187 | std::string join(Ts &&... ts) |
| 188 | { |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 189 | StringStream<> stream; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 190 | inner::join_helper(stream, std::forward<Ts>(ts)...); |
| 191 | return stream.str(); |
| 192 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 193 | |
Hans-Kristian Arntzen | c365cc1 | 2019-06-21 11:16:51 +0200 | [diff] [blame] | 194 | inline std::string merge(const SmallVector<std::string> &list, const char *between = ", ") |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 195 | { |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 196 | StringStream<> stream; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 197 | for (auto &elem : list) |
| 198 | { |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 199 | stream << elem; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 200 | if (&elem != &list.back()) |
Hans-Kristian Arntzen | c365cc1 | 2019-06-21 11:16:51 +0200 | [diff] [blame] | 201 | stream << between; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 202 | } |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 203 | return stream.str(); |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 204 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 205 | |
Hans-Kristian Arntzen | 825ff4a | 2019-02-28 11:26:26 +0100 | [diff] [blame] | 206 | // Make sure we don't accidentally call this with float or doubles with SFINAE. |
| 207 | // Have to use the radix-aware overload. |
| 208 | template <typename T, typename std::enable_if<!std::is_floating_point<T>::value, int>::type = 0> |
| 209 | inline std::string convert_to_string(const T &t) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 210 | { |
Hans-Kristian Arntzen | 825ff4a | 2019-02-28 11:26:26 +0100 | [diff] [blame] | 211 | return std::to_string(t); |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 212 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 213 | |
Hans-Kristian Arntzen | f72bb3c | 2021-09-30 16:17:04 +0200 | [diff] [blame] | 214 | static inline std::string convert_to_string(int32_t value) |
| 215 | { |
| 216 | // INT_MIN is ... special on some backends. If we use a decimal literal, and negate it, we |
| 217 | // could accidentally promote the literal to long first, then negate. |
| 218 | // To workaround it, emit int(0x80000000) instead. |
| 219 | if (value == std::numeric_limits<int32_t>::min()) |
| 220 | return "int(0x80000000)"; |
| 221 | else |
| 222 | return std::to_string(value); |
| 223 | } |
| 224 | |
| 225 | static inline std::string convert_to_string(int64_t value, const std::string &int64_type, bool long_long_literal_suffix) |
| 226 | { |
| 227 | // INT64_MIN is ... special on some backends. |
| 228 | // If we use a decimal literal, and negate it, we might overflow the representable numbers. |
| 229 | // To workaround it, emit int(0x80000000) instead. |
| 230 | if (value == std::numeric_limits<int64_t>::min()) |
| 231 | return join(int64_type, "(0x8000000000000000u", (long_long_literal_suffix ? "ll" : "l"), ")"); |
| 232 | else |
| 233 | return std::to_string(value) + (long_long_literal_suffix ? "ll" : "l"); |
| 234 | } |
| 235 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 236 | // Allow implementations to set a convenient standard precision |
Bill Hollings | 6ddd80e | 2016-04-08 15:12:40 -0400 | [diff] [blame] | 237 | #ifndef SPIRV_CROSS_FLT_FMT |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 238 | #define SPIRV_CROSS_FLT_FMT "%.32g" |
Bill Hollings | 103aabf | 2016-04-06 17:42:27 -0400 | [diff] [blame] | 239 | #endif |
| 240 | |
Hanno | 4560ee2 | 2020-04-09 17:30:20 +0200 | [diff] [blame] | 241 | // Disable sprintf and strcat warnings. |
| 242 | // We cannot rely on snprintf and family existing because, ..., MSVC. |
| 243 | #if defined(__clang__) || defined(__GNUC__) |
| 244 | #pragma GCC diagnostic push |
| 245 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 246 | #elif defined(_MSC_VER) |
Hans-Kristian Arntzen | ce3fe29 | 2017-01-12 10:57:44 +0100 | [diff] [blame] | 247 | #pragma warning(push) |
Hans-Kristian Arntzen | 27f4f75 | 2017-01-13 16:32:54 +0100 | [diff] [blame] | 248 | #pragma warning(disable : 4996) |
Hans-Kristian Arntzen | ce3fe29 | 2017-01-12 10:57:44 +0100 | [diff] [blame] | 249 | #endif |
| 250 | |
Hans-Kristian Arntzen | 825ff4a | 2019-02-28 11:26:26 +0100 | [diff] [blame] | 251 | static inline void fixup_radix_point(char *str, char radix_point) |
| 252 | { |
| 253 | // Setting locales is a very risky business in multi-threaded program, |
| 254 | // so just fixup locales instead. We only need to care about the radix point. |
| 255 | if (radix_point != '.') |
| 256 | { |
| 257 | while (*str != '\0') |
| 258 | { |
| 259 | if (*str == radix_point) |
| 260 | *str = '.'; |
| 261 | str++; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | inline std::string convert_to_string(float t, char locale_radix_point) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 267 | { |
| 268 | // std::to_string for floating point values is broken. |
| 269 | // Fallback to something more sane. |
| 270 | char buf[64]; |
| 271 | sprintf(buf, SPIRV_CROSS_FLT_FMT, t); |
Hans-Kristian Arntzen | 825ff4a | 2019-02-28 11:26:26 +0100 | [diff] [blame] | 272 | fixup_radix_point(buf, locale_radix_point); |
| 273 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 274 | // Ensure that the literal is float. |
| 275 | if (!strchr(buf, '.') && !strchr(buf, 'e')) |
| 276 | strcat(buf, ".0"); |
| 277 | return buf; |
| 278 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 279 | |
Hans-Kristian Arntzen | 825ff4a | 2019-02-28 11:26:26 +0100 | [diff] [blame] | 280 | inline std::string convert_to_string(double t, char locale_radix_point) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 281 | { |
| 282 | // std::to_string for floating point values is broken. |
| 283 | // Fallback to something more sane. |
| 284 | char buf[64]; |
| 285 | sprintf(buf, SPIRV_CROSS_FLT_FMT, t); |
Hans-Kristian Arntzen | 825ff4a | 2019-02-28 11:26:26 +0100 | [diff] [blame] | 286 | fixup_radix_point(buf, locale_radix_point); |
| 287 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 288 | // Ensure that the literal is float. |
| 289 | if (!strchr(buf, '.') && !strchr(buf, 'e')) |
| 290 | strcat(buf, ".0"); |
| 291 | return buf; |
| 292 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 293 | |
Hans-Kristian Arntzen | 3afbfdb | 2020-06-29 12:20:35 +0200 | [diff] [blame] | 294 | template <typename T> |
| 295 | struct ValueSaver |
| 296 | { |
| 297 | explicit ValueSaver(T ¤t_) |
Hans-Kristian Arntzen | d573a95 | 2020-07-01 11:42:58 +0200 | [diff] [blame] | 298 | : current(current_) |
| 299 | , saved(current_) |
Hans-Kristian Arntzen | 3afbfdb | 2020-06-29 12:20:35 +0200 | [diff] [blame] | 300 | { |
| 301 | } |
| 302 | |
| 303 | void release() |
| 304 | { |
| 305 | current = saved; |
| 306 | } |
| 307 | |
| 308 | ~ValueSaver() |
| 309 | { |
| 310 | release(); |
| 311 | } |
| 312 | |
| 313 | T ¤t; |
| 314 | T saved; |
| 315 | }; |
| 316 | |
Hanno | 4560ee2 | 2020-04-09 17:30:20 +0200 | [diff] [blame] | 317 | #if defined(__clang__) || defined(__GNUC__) |
| 318 | #pragma GCC diagnostic pop |
| 319 | #elif defined(_MSC_VER) |
Hans-Kristian Arntzen | ce3fe29 | 2017-01-12 10:57:44 +0100 | [diff] [blame] | 320 | #pragma warning(pop) |
| 321 | #endif |
| 322 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 323 | struct Instruction |
| 324 | { |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 325 | uint16_t op = 0; |
| 326 | uint16_t count = 0; |
Hans-Kristian Arntzen | 4ca06c7 | 2021-03-08 14:09:32 +0100 | [diff] [blame] | 327 | // If offset is 0 (not a valid offset into the instruction stream), |
| 328 | // we have an instruction stream which is embedded in the object. |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 329 | uint32_t offset = 0; |
| 330 | uint32_t length = 0; |
Hans-Kristian Arntzen | 4ca06c7 | 2021-03-08 14:09:32 +0100 | [diff] [blame] | 331 | |
| 332 | inline bool is_embedded() const |
| 333 | { |
| 334 | return offset == 0; |
| 335 | } |
| 336 | }; |
| 337 | |
| 338 | struct EmbeddedInstruction : Instruction |
| 339 | { |
| 340 | SmallVector<uint32_t> ops; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 341 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 342 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 343 | enum Types |
| 344 | { |
| 345 | TypeNone, |
| 346 | TypeType, |
| 347 | TypeVariable, |
| 348 | TypeConstant, |
| 349 | TypeFunction, |
| 350 | TypeFunctionPrototype, |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 351 | TypeBlock, |
| 352 | TypeExtension, |
| 353 | TypeExpression, |
Hans-Kristian Arntzen | 7e8afa8 | 2016-10-03 15:54:02 +0200 | [diff] [blame] | 354 | TypeConstantOp, |
Hans-Kristian Arntzen | 100e9d3 | 2017-04-25 10:44:55 +0200 | [diff] [blame] | 355 | TypeCombinedImageSampler, |
Hans-Kristian Arntzen | 3cbdbec | 2017-08-10 15:36:30 +0200 | [diff] [blame] | 356 | TypeAccessChain, |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 357 | TypeUndef, |
Hans-Kristian Arntzen | 65af09d | 2019-05-28 13:41:46 +0200 | [diff] [blame] | 358 | TypeString, |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 359 | TypeCount |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 360 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 361 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 362 | template <Types type> |
| 363 | class TypedID; |
| 364 | |
| 365 | template <> |
| 366 | class TypedID<TypeNone> |
| 367 | { |
| 368 | public: |
| 369 | TypedID() = default; |
| 370 | TypedID(uint32_t id_) |
| 371 | : id(id_) |
| 372 | { |
| 373 | } |
| 374 | |
| 375 | template <Types U> |
| 376 | TypedID(const TypedID<U> &other) |
| 377 | { |
| 378 | *this = other; |
| 379 | } |
| 380 | |
| 381 | template <Types U> |
| 382 | TypedID &operator=(const TypedID<U> &other) |
| 383 | { |
| 384 | id = uint32_t(other); |
| 385 | return *this; |
| 386 | } |
| 387 | |
| 388 | // Implicit conversion to u32 is desired here. |
| 389 | // As long as we block implicit conversion between TypedID<A> and TypedID<B> we're good. |
| 390 | operator uint32_t() const |
| 391 | { |
| 392 | return id; |
| 393 | } |
| 394 | |
| 395 | template <Types U> |
| 396 | operator TypedID<U>() const |
| 397 | { |
| 398 | return TypedID<U>(*this); |
| 399 | } |
| 400 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 401 | private: |
| 402 | uint32_t id = 0; |
| 403 | }; |
| 404 | |
| 405 | template <Types type> |
| 406 | class TypedID |
| 407 | { |
| 408 | public: |
| 409 | TypedID() = default; |
| 410 | TypedID(uint32_t id_) |
| 411 | : id(id_) |
| 412 | { |
| 413 | } |
| 414 | |
| 415 | explicit TypedID(const TypedID<TypeNone> &other) |
| 416 | : id(uint32_t(other)) |
| 417 | { |
| 418 | } |
| 419 | |
| 420 | operator uint32_t() const |
| 421 | { |
| 422 | return id; |
| 423 | } |
| 424 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 425 | private: |
| 426 | uint32_t id = 0; |
| 427 | }; |
| 428 | |
| 429 | using VariableID = TypedID<TypeVariable>; |
| 430 | using TypeID = TypedID<TypeType>; |
| 431 | using ConstantID = TypedID<TypeConstant>; |
| 432 | using FunctionID = TypedID<TypeFunction>; |
| 433 | using BlockID = TypedID<TypeBlock>; |
| 434 | using ID = TypedID<TypeNone>; |
| 435 | |
| 436 | // Helper for Variant interface. |
| 437 | struct IVariant |
| 438 | { |
| 439 | virtual ~IVariant() = default; |
| 440 | virtual IVariant *clone(ObjectPoolBase *pool) = 0; |
| 441 | ID self = 0; |
Corentin Wallez | 6a85c69 | 2021-06-21 13:18:21 +0200 | [diff] [blame] | 442 | |
| 443 | protected: |
| 444 | IVariant() = default; |
| 445 | IVariant(const IVariant&) = default; |
| 446 | IVariant &operator=(const IVariant&) = default; |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 447 | }; |
| 448 | |
| 449 | #define SPIRV_CROSS_DECLARE_CLONE(T) \ |
| 450 | IVariant *clone(ObjectPoolBase *pool) override \ |
| 451 | { \ |
| 452 | return static_cast<ObjectPool<T> *>(pool)->allocate(*this); \ |
| 453 | } |
| 454 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 455 | struct SPIRUndef : IVariant |
| 456 | { |
| 457 | enum |
| 458 | { |
| 459 | type = TypeUndef |
| 460 | }; |
Hans-Kristian Arntzen | 9bbdccd | 2019-02-12 11:11:29 +0100 | [diff] [blame] | 461 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 462 | explicit SPIRUndef(TypeID basetype_) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 463 | : basetype(basetype_) |
| 464 | { |
| 465 | } |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 466 | TypeID basetype; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 467 | |
| 468 | SPIRV_CROSS_DECLARE_CLONE(SPIRUndef) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 469 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 470 | |
Hans-Kristian Arntzen | 65af09d | 2019-05-28 13:41:46 +0200 | [diff] [blame] | 471 | struct SPIRString : IVariant |
| 472 | { |
| 473 | enum |
| 474 | { |
| 475 | type = TypeString |
| 476 | }; |
| 477 | |
| 478 | explicit SPIRString(std::string str_) |
| 479 | : str(std::move(str_)) |
| 480 | { |
| 481 | } |
| 482 | |
| 483 | std::string str; |
| 484 | |
| 485 | SPIRV_CROSS_DECLARE_CLONE(SPIRString) |
| 486 | }; |
| 487 | |
Hans-Kristian Arntzen | 100e9d3 | 2017-04-25 10:44:55 +0200 | [diff] [blame] | 488 | // This type is only used by backends which need to access the combined image and sampler IDs separately after |
| 489 | // the OpSampledImage opcode. |
| 490 | struct SPIRCombinedImageSampler : IVariant |
| 491 | { |
| 492 | enum |
| 493 | { |
| 494 | type = TypeCombinedImageSampler |
| 495 | }; |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 496 | SPIRCombinedImageSampler(TypeID type_, VariableID image_, VariableID sampler_) |
Hans-Kristian Arntzen | 100e9d3 | 2017-04-25 10:44:55 +0200 | [diff] [blame] | 497 | : combined_type(type_) |
| 498 | , image(image_) |
| 499 | , sampler(sampler_) |
| 500 | { |
| 501 | } |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 502 | TypeID combined_type; |
| 503 | VariableID image; |
| 504 | VariableID sampler; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 505 | |
| 506 | SPIRV_CROSS_DECLARE_CLONE(SPIRCombinedImageSampler) |
Hans-Kristian Arntzen | 100e9d3 | 2017-04-25 10:44:55 +0200 | [diff] [blame] | 507 | }; |
| 508 | |
Hans-Kristian Arntzen | 7e8afa8 | 2016-10-03 15:54:02 +0200 | [diff] [blame] | 509 | struct SPIRConstantOp : IVariant |
| 510 | { |
| 511 | enum |
| 512 | { |
| 513 | type = TypeConstantOp |
| 514 | }; |
| 515 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 516 | SPIRConstantOp(TypeID result_type, spv::Op op, const uint32_t *args, uint32_t length) |
Hans-Kristian Arntzen | 7e8afa8 | 2016-10-03 15:54:02 +0200 | [diff] [blame] | 517 | : opcode(op) |
Hans-Kristian Arntzen | 7e8afa8 | 2016-10-03 15:54:02 +0200 | [diff] [blame] | 518 | , basetype(result_type) |
| 519 | { |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 520 | arguments.reserve(length); |
| 521 | for (uint32_t i = 0; i < length; i++) |
| 522 | arguments.push_back(args[i]); |
Hans-Kristian Arntzen | 7e8afa8 | 2016-10-03 15:54:02 +0200 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | spv::Op opcode; |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 526 | SmallVector<uint32_t> arguments; |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 527 | TypeID basetype; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 528 | |
| 529 | SPIRV_CROSS_DECLARE_CLONE(SPIRConstantOp) |
Hans-Kristian Arntzen | 7e8afa8 | 2016-10-03 15:54:02 +0200 | [diff] [blame] | 530 | }; |
| 531 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 532 | struct SPIRType : IVariant |
| 533 | { |
| 534 | enum |
| 535 | { |
| 536 | type = TypeType |
| 537 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 538 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 539 | enum BaseType |
| 540 | { |
| 541 | Unknown, |
| 542 | Void, |
Hans-Kristian Arntzen | 5f62927 | 2016-06-05 20:13:45 +0200 | [diff] [blame] | 543 | Boolean, |
Chip Davis | 117ccf4 | 2018-11-01 17:20:07 -0500 | [diff] [blame] | 544 | SByte, |
| 545 | UByte, |
| 546 | Short, |
| 547 | UShort, |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 548 | Int, |
| 549 | UInt, |
Hans-Kristian Arntzen | fc2230f | 2016-07-27 11:27:00 +0200 | [diff] [blame] | 550 | Int64, |
| 551 | UInt64, |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 552 | AtomicCounter, |
Hans-Kristian Arntzen | 91f85d3 | 2018-03-06 15:32:26 +0100 | [diff] [blame] | 553 | Half, |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 554 | Float, |
Hans-Kristian Arntzen | fa0255c | 2016-07-27 10:59:00 +0200 | [diff] [blame] | 555 | Double, |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 556 | Struct, |
| 557 | Image, |
| 558 | SampledImage, |
Chip Davis | e75add4 | 2019-02-05 18:13:26 -0600 | [diff] [blame] | 559 | Sampler, |
Hans-Kristian Arntzen | 6b0e558 | 2020-04-21 14:25:18 +0200 | [diff] [blame] | 560 | AccelerationStructure, |
| 561 | RayQuery, |
Hans-Kristian Arntzen | 88ce958 | 2019-03-27 10:21:30 +0100 | [diff] [blame] | 562 | |
| 563 | // Keep internal types at the end. |
Patrick Mours | da39a7b | 2019-02-26 15:43:03 +0100 | [diff] [blame] | 564 | ControlPointArray, |
Chip Davis | aca9b68 | 2020-11-02 20:56:46 -0600 | [diff] [blame] | 565 | Interpolant, |
Hans-Kristian Arntzen | 88ce958 | 2019-03-27 10:21:30 +0100 | [diff] [blame] | 566 | Char |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 567 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 568 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 569 | // Scalar/vector/matrix support. |
| 570 | BaseType basetype = Unknown; |
| 571 | uint32_t width = 0; |
| 572 | uint32_t vecsize = 1; |
| 573 | uint32_t columns = 1; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 574 | |
Hans-Kristian Arntzen | 5d4bb68 | 2016-10-03 17:17:11 +0200 | [diff] [blame] | 575 | // Arrays, support array of arrays by having a vector of array sizes. |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 576 | SmallVector<uint32_t> array; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 577 | |
Hans-Kristian Arntzen | 5d4bb68 | 2016-10-03 17:17:11 +0200 | [diff] [blame] | 578 | // Array elements can be either specialization constants or specialization ops. |
| 579 | // This array determines how to interpret the array size. |
| 580 | // If an element is true, the element is a literal, |
| 581 | // otherwise, it's an expression, which must be resolved on demand. |
| 582 | // The actual size is not really known until runtime. |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 583 | SmallVector<bool> array_size_literal; |
Hans-Kristian Arntzen | 5d4bb68 | 2016-10-03 17:17:11 +0200 | [diff] [blame] | 584 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 585 | // Pointers |
Hans-Kristian Arntzen | d0b9372 | 2018-11-26 12:23:28 +0100 | [diff] [blame] | 586 | // Keep track of how many pointer layers we have. |
| 587 | uint32_t pointer_depth = 0; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 588 | bool pointer = false; |
Hans-Kristian Arntzen | 58dad82 | 2020-05-25 11:05:42 +0200 | [diff] [blame] | 589 | bool forward_pointer = false; |
Hans-Kristian Arntzen | d0b9372 | 2018-11-26 12:23:28 +0100 | [diff] [blame] | 590 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 591 | spv::StorageClass storage = spv::StorageClassGeneric; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 592 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 593 | SmallVector<TypeID> member_types; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 594 | |
Hans-Kristian Arntzen | 0ebb88c | 2020-04-30 11:48:53 +0200 | [diff] [blame] | 595 | // If member order has been rewritten to handle certain scenarios with Offset, |
| 596 | // allow codegen to rewrite the index. |
| 597 | SmallVector<uint32_t> member_type_index_redirection; |
| 598 | |
Hans-Kristian Arntzen | afc2c0c | 2017-10-24 10:25:38 +0200 | [diff] [blame] | 599 | struct ImageType |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 600 | { |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 601 | TypeID type; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 602 | spv::Dim dim; |
| 603 | bool depth; |
| 604 | bool arrayed; |
| 605 | bool ms; |
| 606 | uint32_t sampled; |
| 607 | spv::ImageFormat format; |
Bill Hollings | b41e148 | 2017-05-29 20:45:05 -0400 | [diff] [blame] | 608 | spv::AccessQualifier access; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 609 | } image; |
Hans-Kristian Arntzen | f05373b | 2016-05-23 10:57:22 +0200 | [diff] [blame] | 610 | |
| 611 | // Structs can be declared multiple times if they are used as part of interface blocks. |
| 612 | // We want to detect this so that we only emit the struct definition once. |
| 613 | // Since we cannot rely on OpName to be equal, we need to figure out aliases. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 614 | TypeID type_alias = 0; |
Hans-Kristian Arntzen | 6aa2007 | 2016-05-23 12:25:09 +0200 | [diff] [blame] | 615 | |
Hans-Kristian Arntzen | d3cad99 | 2017-01-21 11:30:33 +0100 | [diff] [blame] | 616 | // Denotes the type which this type is based on. |
| 617 | // Allows the backend to traverse how a complex type is built up during access chains. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 618 | TypeID parent_type = 0; |
Hans-Kristian Arntzen | d3cad99 | 2017-01-21 11:30:33 +0100 | [diff] [blame] | 619 | |
Hans-Kristian Arntzen | 6aa2007 | 2016-05-23 12:25:09 +0200 | [diff] [blame] | 620 | // Used in backends to avoid emitting members with conflicting names. |
| 621 | std::unordered_set<std::string> member_name_cache; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 622 | |
| 623 | SPIRV_CROSS_DECLARE_CLONE(SPIRType) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 624 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 625 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 626 | struct SPIRExtension : IVariant |
| 627 | { |
| 628 | enum |
| 629 | { |
| 630 | type = TypeExtension |
| 631 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 632 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 633 | enum Extension |
| 634 | { |
Hans-Kristian Arntzen | 299e19f | 2017-03-21 16:33:54 +0100 | [diff] [blame] | 635 | Unsupported, |
Lou Kramer | 6671f52 | 2017-11-21 14:04:57 +0100 | [diff] [blame] | 636 | GLSL, |
Lifeng Pan | 5ca8779 | 2019-07-04 16:03:06 +0800 | [diff] [blame] | 637 | SPV_debug_info, |
Lou Kramer | 6671f52 | 2017-11-21 14:04:57 +0100 | [diff] [blame] | 638 | SPV_AMD_shader_ballot, |
| 639 | SPV_AMD_shader_explicit_vertex_parameter, |
| 640 | SPV_AMD_shader_trinary_minmax, |
| 641 | SPV_AMD_gcn_shader |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 642 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 643 | |
Hans-Kristian Arntzen | 9bbdccd | 2019-02-12 11:11:29 +0100 | [diff] [blame] | 644 | explicit SPIRExtension(Extension ext_) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 645 | : ext(ext_) |
| 646 | { |
| 647 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 648 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 649 | Extension ext; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 650 | SPIRV_CROSS_DECLARE_CLONE(SPIRExtension) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 651 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 652 | |
Hans-Kristian Arntzen | 042475e | 2016-07-28 11:16:02 +0200 | [diff] [blame] | 653 | // SPIREntryPoint is not a variant since its IDs are used to decorate OpFunction, |
| 654 | // so in order to avoid conflicts, we can't stick them in the ids array. |
| 655 | struct SPIREntryPoint |
| 656 | { |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 657 | SPIREntryPoint(FunctionID self_, spv::ExecutionModel execution_model, const std::string &entry_name) |
Hans-Kristian Arntzen | 042475e | 2016-07-28 11:16:02 +0200 | [diff] [blame] | 658 | : self(self_) |
Bill Hollings | 1c18078 | 2017-11-05 21:34:42 -0500 | [diff] [blame] | 659 | , name(entry_name) |
| 660 | , orig_name(entry_name) |
Hans-Kristian Arntzen | 042475e | 2016-07-28 11:16:02 +0200 | [diff] [blame] | 661 | , model(execution_model) |
| 662 | { |
| 663 | } |
| 664 | SPIREntryPoint() = default; |
| 665 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 666 | FunctionID self = 0; |
Hans-Kristian Arntzen | 042475e | 2016-07-28 11:16:02 +0200 | [diff] [blame] | 667 | std::string name; |
Bill Hollings | 1c18078 | 2017-11-05 21:34:42 -0500 | [diff] [blame] | 668 | std::string orig_name; |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 669 | SmallVector<VariableID> interface_variables; |
Hans-Kristian Arntzen | 042475e | 2016-07-28 11:16:02 +0200 | [diff] [blame] | 670 | |
Hans-Kristian Arntzen | e8e5884 | 2018-03-12 13:09:25 +0100 | [diff] [blame] | 671 | Bitset flags; |
Hans-Kristian Arntzen | 36c999a | 2020-07-11 13:35:44 +0200 | [diff] [blame] | 672 | struct WorkgroupSize |
Hans-Kristian Arntzen | 042475e | 2016-07-28 11:16:02 +0200 | [diff] [blame] | 673 | { |
| 674 | uint32_t x = 0, y = 0, z = 0; |
Hans-Kristian Arntzen | 7c83fc2 | 2022-01-05 15:53:51 +0100 | [diff] [blame] | 675 | uint32_t id_x = 0, id_y = 0, id_z = 0; |
Hans-Kristian Arntzen | 86eb874 | 2017-09-28 11:33:30 +0200 | [diff] [blame] | 676 | uint32_t constant = 0; // Workgroup size can be expressed as a constant/spec-constant instead. |
Hans-Kristian Arntzen | 042475e | 2016-07-28 11:16:02 +0200 | [diff] [blame] | 677 | } workgroup_size; |
| 678 | uint32_t invocations = 0; |
| 679 | uint32_t output_vertices = 0; |
Hans-Kristian Arntzen | 9bbdccd | 2019-02-12 11:11:29 +0100 | [diff] [blame] | 680 | spv::ExecutionModel model = spv::ExecutionModelMax; |
Hans-Kristian Arntzen | 55fe605 | 2020-01-15 16:18:29 +0100 | [diff] [blame] | 681 | bool geometry_passthrough = false; |
Hans-Kristian Arntzen | 042475e | 2016-07-28 11:16:02 +0200 | [diff] [blame] | 682 | }; |
| 683 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 684 | struct SPIRExpression : IVariant |
| 685 | { |
| 686 | enum |
| 687 | { |
| 688 | type = TypeExpression |
| 689 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 690 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 691 | // Only created by the backend target to avoid creating tons of temporaries. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 692 | SPIRExpression(std::string expr, TypeID expression_type_, bool immutable_) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 693 | : expression(move(expr)) |
| 694 | , expression_type(expression_type_) |
| 695 | , immutable(immutable_) |
| 696 | { |
| 697 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 698 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 699 | // If non-zero, prepend expression with to_expression(base_expression). |
| 700 | // Used in amortizing multiple calls to to_expression() |
| 701 | // where in certain cases that would quickly force a temporary when not needed. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 702 | ID base_expression = 0; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 703 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 704 | std::string expression; |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 705 | TypeID expression_type = 0; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 706 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 707 | // If this expression is a forwarded load, |
| 708 | // allow us to reference the original variable. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 709 | ID loaded_from = 0; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 710 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 711 | // If this expression will never change, we can avoid lots of temporaries |
| 712 | // in high level source. |
Hans-Kristian Arntzen | 36a0b63 | 2016-07-12 14:33:04 +0200 | [diff] [blame] | 713 | // An expression being immutable can be speculative, |
| 714 | // it is assumed that this is true almost always. |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 715 | bool immutable = false; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 716 | |
Hans-Kristian Arntzen | fadaec2 | 2017-01-13 16:31:13 +0100 | [diff] [blame] | 717 | // Before use, this expression must be transposed. |
| 718 | // This is needed for targets which don't support row_major layouts. |
| 719 | bool need_transpose = false; |
| 720 | |
Chip Davis | 3bfb2f9 | 2018-12-03 02:06:33 -0600 | [diff] [blame] | 721 | // Whether or not this is an access chain expression. |
| 722 | bool access_chain = false; |
| 723 | |
Hans-Kristian Arntzen | 36a0b63 | 2016-07-12 14:33:04 +0200 | [diff] [blame] | 724 | // A list of expressions which this expression depends on. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 725 | SmallVector<ID> expression_dependencies; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 726 | |
Hans-Kristian Arntzen | acae607 | 2019-01-04 13:19:50 +0100 | [diff] [blame] | 727 | // By reading this expression, we implicitly read these expressions as well. |
| 728 | // Used by access chain Store and Load since we read multiple expressions in this case. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 729 | SmallVector<ID> implied_read_expressions; |
Hans-Kristian Arntzen | acae607 | 2019-01-04 13:19:50 +0100 | [diff] [blame] | 730 | |
Hans-Kristian Arntzen | 3afbfdb | 2020-06-29 12:20:35 +0200 | [diff] [blame] | 731 | // The expression was emitted at a certain scope. Lets us track when an expression read means multiple reads. |
| 732 | uint32_t emitted_loop_level = 0; |
| 733 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 734 | SPIRV_CROSS_DECLARE_CLONE(SPIRExpression) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 735 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 736 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 737 | struct SPIRFunctionPrototype : IVariant |
| 738 | { |
| 739 | enum |
| 740 | { |
| 741 | type = TypeFunctionPrototype |
| 742 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 743 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 744 | explicit SPIRFunctionPrototype(TypeID return_type_) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 745 | : return_type(return_type_) |
| 746 | { |
| 747 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 748 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 749 | TypeID return_type; |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 750 | SmallVector<uint32_t> parameter_types; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 751 | |
| 752 | SPIRV_CROSS_DECLARE_CLONE(SPIRFunctionPrototype) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 753 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 754 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 755 | struct SPIRBlock : IVariant |
| 756 | { |
| 757 | enum |
| 758 | { |
| 759 | type = TypeBlock |
| 760 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 761 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 762 | enum Terminator |
| 763 | { |
| 764 | Unknown, |
| 765 | Direct, // Emit next block directly without a particular condition. |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 766 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 767 | Select, // Block ends with an if/else block. |
| 768 | MultiSelect, // Block ends with switch statement. |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 769 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 770 | Return, // Block ends with return. |
| 771 | Unreachable, // Noop |
Hans-Kristian Arntzen | 2097c30 | 2021-01-08 11:37:29 +0100 | [diff] [blame] | 772 | Kill, // Discard |
| 773 | IgnoreIntersection, // Ray Tracing |
| 774 | TerminateRay // Ray Tracing |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 775 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 776 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 777 | enum Merge |
| 778 | { |
| 779 | MergeNone, |
| 780 | MergeLoop, |
| 781 | MergeSelection |
| 782 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 783 | |
Hans-Kristian Arntzen | 33c61d2 | 2018-06-25 10:33:13 +0200 | [diff] [blame] | 784 | enum Hints |
| 785 | { |
| 786 | HintNone, |
| 787 | HintUnroll, |
| 788 | HintDontUnroll, |
| 789 | HintFlatten, |
| 790 | HintDontFlatten |
| 791 | }; |
| 792 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 793 | enum Method |
| 794 | { |
| 795 | MergeToSelectForLoop, |
Hans-Kristian Arntzen | 28cccc3 | 2018-03-08 17:51:55 +0100 | [diff] [blame] | 796 | MergeToDirectForLoop, |
| 797 | MergeToSelectContinueForLoop |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 798 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 799 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 800 | enum ContinueBlockType |
| 801 | { |
| 802 | ContinueNone, |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 803 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 804 | // Continue block is branchless and has at least one instruction. |
| 805 | ForLoop, |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 806 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 807 | // Noop continue block. |
| 808 | WhileLoop, |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 809 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 810 | // Continue block is conditional. |
| 811 | DoWhileLoop, |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 812 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 813 | // Highly unlikely that anything will use this, |
| 814 | // since it is really awkward/impossible to express in GLSL. |
| 815 | ComplexLoop |
| 816 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 817 | |
Corentin Wallez | a3a590a | 2020-04-30 10:00:28 +0200 | [diff] [blame] | 818 | enum : uint32_t |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 819 | { |
| 820 | NoDominator = 0xffffffffu |
| 821 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 822 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 823 | Terminator terminator = Unknown; |
| 824 | Merge merge = MergeNone; |
Hans-Kristian Arntzen | 33c61d2 | 2018-06-25 10:33:13 +0200 | [diff] [blame] | 825 | Hints hint = HintNone; |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 826 | BlockID next_block = 0; |
| 827 | BlockID merge_block = 0; |
| 828 | BlockID continue_block = 0; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 829 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 830 | ID return_value = 0; // If 0, return nothing (void). |
| 831 | ID condition = 0; |
| 832 | BlockID true_block = 0; |
| 833 | BlockID false_block = 0; |
| 834 | BlockID default_block = 0; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 835 | |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 836 | SmallVector<Instruction> ops; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 837 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 838 | struct Phi |
| 839 | { |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 840 | ID local_variable; // flush local variable ... |
| 841 | BlockID parent; // If we're in from_block and want to branch into this block ... |
| 842 | VariableID function_variable; // to this function-global "phi" variable first. |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 843 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 844 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 845 | // Before entering this block flush out local variables to magical "phi" variables. |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 846 | SmallVector<Phi> phi_variables; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 847 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 848 | // Declare these temporaries before beginning the block. |
| 849 | // Used for handling complex continue blocks which have side effects. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 850 | SmallVector<std::pair<TypeID, ID>> declare_temporary; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 851 | |
Hans-Kristian Arntzen | c1947aa | 2018-03-24 04:16:18 +0100 | [diff] [blame] | 852 | // Declare these temporaries, but only conditionally if this block turns out to be |
| 853 | // a complex loop header. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 854 | SmallVector<std::pair<TypeID, ID>> potential_declare_temporary; |
Hans-Kristian Arntzen | c1947aa | 2018-03-24 04:16:18 +0100 | [diff] [blame] | 855 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 856 | struct Case |
| 857 | { |
Sebastián Aedo | 3eb5532 | 2021-10-28 19:57:41 -0300 | [diff] [blame] | 858 | uint64_t value; |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 859 | BlockID block; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 860 | }; |
Sebastián Aedo | 75e3752 | 2021-11-12 10:17:38 -0300 | [diff] [blame] | 861 | SmallVector<Case> cases_32bit; |
Sebastián Aedo | f099d71 | 2021-11-02 17:17:13 -0300 | [diff] [blame] | 862 | SmallVector<Case> cases_64bit; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 863 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 864 | // If we have tried to optimize code for this block but failed, |
| 865 | // keep track of this. |
| 866 | bool disable_block_optimization = false; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 867 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 868 | // If the continue block is complex, fallback to "dumb" for loops. |
| 869 | bool complex_continue = false; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 870 | |
Hans-Kristian Arntzen | 3b5968b | 2018-09-18 10:50:48 +0200 | [diff] [blame] | 871 | // Do we need a ladder variable to defer breaking out of a loop construct after a switch block? |
| 872 | bool need_ladder_break = false; |
| 873 | |
Hans-Kristian Arntzen | c365cc1 | 2019-06-21 11:16:51 +0200 | [diff] [blame] | 874 | // If marked, we have explicitly handled Phi from this block, so skip any flushes related to that on a branch. |
| 875 | // Used to handle an edge case with switch and case-label fallthrough where fall-through writes to Phi. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 876 | BlockID ignore_phi_from_block = 0; |
Hans-Kristian Arntzen | c365cc1 | 2019-06-21 11:16:51 +0200 | [diff] [blame] | 877 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 878 | // The dominating block which this block might be within. |
| 879 | // Used in continue; blocks to determine if we really need to write continue. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 880 | BlockID loop_dominator = 0; |
Hans-Kristian Arntzen | dad4a34 | 2016-11-11 18:04:14 +0100 | [diff] [blame] | 881 | |
| 882 | // All access to these variables are dominated by this block, |
| 883 | // so before branching anywhere we need to make sure that we declare these variables. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 884 | SmallVector<VariableID> dominated_variables; |
Hans-Kristian Arntzen | 4f07a32 | 2016-12-15 17:14:47 +0100 | [diff] [blame] | 885 | |
| 886 | // These are variables which should be declared in a for loop header, if we |
| 887 | // fail to use a classic for-loop, |
| 888 | // we remove these variables, and fall back to regular variables outside the loop. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 889 | SmallVector<VariableID> loop_variables; |
Hans-Kristian Arntzen | 938c7de | 2018-03-12 17:34:54 +0100 | [diff] [blame] | 890 | |
| 891 | // Some expressions are control-flow dependent, i.e. any instruction which relies on derivatives or |
| 892 | // sub-group-like operations. |
| 893 | // Make sure that we only use these expressions in the original block. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 894 | SmallVector<ID> invalidate_expressions; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 895 | |
| 896 | SPIRV_CROSS_DECLARE_CLONE(SPIRBlock) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 897 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 898 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 899 | struct SPIRFunction : IVariant |
| 900 | { |
| 901 | enum |
| 902 | { |
| 903 | type = TypeFunction |
| 904 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 905 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 906 | SPIRFunction(TypeID return_type_, TypeID function_type_) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 907 | : return_type(return_type_) |
| 908 | , function_type(function_type_) |
| 909 | { |
| 910 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 911 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 912 | struct Parameter |
| 913 | { |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 914 | TypeID type; |
| 915 | ID id; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 916 | uint32_t read_count; |
| 917 | uint32_t write_count; |
Hans-Kristian Arntzen | 9cb8616 | 2017-02-05 10:50:14 +0100 | [diff] [blame] | 918 | |
| 919 | // Set to true if this parameter aliases a global variable, |
| 920 | // used mostly in Metal where global variables |
| 921 | // have to be passed down to functions as regular arguments. |
| 922 | // However, for this kind of variable, we should not care about |
| 923 | // read and write counts as access to the function arguments |
| 924 | // is not local to the function in question. |
| 925 | bool alias_global_variable; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 926 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 927 | |
Hans-Kristian Arntzen | ed98a8e | 2016-09-11 11:39:20 +0200 | [diff] [blame] | 928 | // When calling a function, and we're remapping separate image samplers, |
| 929 | // resolve these arguments into combined image samplers and pass them |
| 930 | // as additional arguments in this order. |
| 931 | // It gets more complicated as functions can pull in their own globals |
| 932 | // and combine them with parameters, |
| 933 | // so we need to distinguish if something is local parameter index |
| 934 | // or a global ID. |
| 935 | struct CombinedImageSamplerParameter |
| 936 | { |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 937 | VariableID id; |
| 938 | VariableID image_id; |
| 939 | VariableID sampler_id; |
Hans-Kristian Arntzen | 378fbe8 | 2016-09-11 13:47:06 +0200 | [diff] [blame] | 940 | bool global_image; |
Hans-Kristian Arntzen | ed98a8e | 2016-09-11 11:39:20 +0200 | [diff] [blame] | 941 | bool global_sampler; |
Hans-Kristian Arntzen | f4d7268 | 2017-05-06 13:21:35 +0200 | [diff] [blame] | 942 | bool depth; |
Hans-Kristian Arntzen | ed98a8e | 2016-09-11 11:39:20 +0200 | [diff] [blame] | 943 | }; |
| 944 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 945 | TypeID return_type; |
| 946 | TypeID function_type; |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 947 | SmallVector<Parameter> arguments; |
Hans-Kristian Arntzen | 948930b | 2016-09-11 12:36:12 +0200 | [diff] [blame] | 948 | |
| 949 | // Can be used by backends to add magic arguments. |
| 950 | // Currently used by combined image/sampler implementation. |
| 951 | |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 952 | SmallVector<Parameter> shadow_arguments; |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 953 | SmallVector<VariableID> local_variables; |
| 954 | BlockID entry_block = 0; |
| 955 | SmallVector<BlockID> blocks; |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 956 | SmallVector<CombinedImageSamplerParameter> combined_parameters; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 957 | |
Hans-Kristian Arntzen | 65af09d | 2019-05-28 13:41:46 +0200 | [diff] [blame] | 958 | struct EntryLine |
| 959 | { |
| 960 | uint32_t file_id = 0; |
| 961 | uint32_t line_literal = 0; |
| 962 | }; |
| 963 | EntryLine entry_line; |
| 964 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 965 | void add_local_variable(VariableID id) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 966 | { |
| 967 | local_variables.push_back(id); |
| 968 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 969 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 970 | void add_parameter(TypeID parameter_type, ID id, bool alias_global_variable = false) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 971 | { |
| 972 | // Arguments are read-only until proven otherwise. |
Hans-Kristian Arntzen | 9cb8616 | 2017-02-05 10:50:14 +0100 | [diff] [blame] | 973 | arguments.push_back({ parameter_type, id, 0u, 0u, alias_global_variable }); |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 974 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 975 | |
Hans-Kristian Arntzen | 340957a | 2018-09-17 14:04:55 +0200 | [diff] [blame] | 976 | // Hooks to be run when the function returns. |
Hans-Kristian Arntzen | 8e90382 | 2018-03-13 14:03:35 +0100 | [diff] [blame] | 977 | // Mostly used for lowering internal data structures onto flattened structures. |
Hans-Kristian Arntzen | 340957a | 2018-09-17 14:04:55 +0200 | [diff] [blame] | 978 | // Need to defer this, because they might rely on things which change during compilation. |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 979 | // Intentionally not a small vector, this one is rare, and std::function can be large. |
| 980 | Vector<std::function<void()>> fixup_hooks_out; |
Bill Hollings | 9b4defe | 2018-06-12 11:41:35 -0400 | [diff] [blame] | 981 | |
Hans-Kristian Arntzen | 340957a | 2018-09-17 14:04:55 +0200 | [diff] [blame] | 982 | // Hooks to be run when the function begins. |
Bill Hollings | 9b4defe | 2018-06-12 11:41:35 -0400 | [diff] [blame] | 983 | // Mostly used for populating internal data structures from flattened structures. |
Hans-Kristian Arntzen | 340957a | 2018-09-17 14:04:55 +0200 | [diff] [blame] | 984 | // Need to defer this, because they might rely on things which change during compilation. |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 985 | // Intentionally not a small vector, this one is rare, and std::function can be large. |
| 986 | Vector<std::function<void()>> fixup_hooks_in; |
Hans-Kristian Arntzen | 8e90382 | 2018-03-13 14:03:35 +0100 | [diff] [blame] | 987 | |
Hans-Kristian Arntzen | 30343f3 | 2020-02-24 13:22:52 +0100 | [diff] [blame] | 988 | // On function entry, make sure to copy a constant array into thread addr space to work around |
| 989 | // the case where we are passing a constant array by value to a function on backends which do not |
| 990 | // consider arrays value types. |
| 991 | SmallVector<ID> constant_arrays_needed_on_stack; |
| 992 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 993 | bool active = false; |
| 994 | bool flush_undeclared = true; |
Hans-Kristian Arntzen | ed98a8e | 2016-09-11 11:39:20 +0200 | [diff] [blame] | 995 | bool do_combined_parameters = true; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 996 | |
| 997 | SPIRV_CROSS_DECLARE_CLONE(SPIRFunction) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 998 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 999 | |
Hans-Kristian Arntzen | 3cbdbec | 2017-08-10 15:36:30 +0200 | [diff] [blame] | 1000 | struct SPIRAccessChain : IVariant |
| 1001 | { |
| 1002 | enum |
| 1003 | { |
| 1004 | type = TypeAccessChain |
| 1005 | }; |
| 1006 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1007 | SPIRAccessChain(TypeID basetype_, spv::StorageClass storage_, std::string base_, std::string dynamic_index_, |
Hans-Kristian Arntzen | 713bd7c | 2017-08-28 09:01:03 +0200 | [diff] [blame] | 1008 | int32_t static_index_) |
| 1009 | : basetype(basetype_) |
| 1010 | , storage(storage_) |
Hans-Kristian Arntzen | 9bbdccd | 2019-02-12 11:11:29 +0100 | [diff] [blame] | 1011 | , base(std::move(base_)) |
Hans-Kristian Arntzen | 713bd7c | 2017-08-28 09:01:03 +0200 | [diff] [blame] | 1012 | , dynamic_index(std::move(dynamic_index_)) |
| 1013 | , static_index(static_index_) |
Hans-Kristian Arntzen | 3cbdbec | 2017-08-10 15:36:30 +0200 | [diff] [blame] | 1014 | { |
| 1015 | } |
| 1016 | |
| 1017 | // The access chain represents an offset into a buffer. |
| 1018 | // Some backends need more complicated handling of access chains to be able to use buffers, like HLSL |
| 1019 | // which has no usable buffer type ala GLSL SSBOs. |
| 1020 | // StructuredBuffer is too limited, so our only option is to deal with ByteAddressBuffer which works with raw addresses. |
| 1021 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1022 | TypeID basetype; |
Hans-Kristian Arntzen | 3cbdbec | 2017-08-10 15:36:30 +0200 | [diff] [blame] | 1023 | spv::StorageClass storage; |
| 1024 | std::string base; |
| 1025 | std::string dynamic_index; |
| 1026 | int32_t static_index; |
| 1027 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1028 | VariableID loaded_from = 0; |
Hans-Kristian Arntzen | 551424c | 2017-10-26 16:35:18 +0200 | [diff] [blame] | 1029 | uint32_t matrix_stride = 0; |
Hans-Kristian Arntzen | ca9398c | 2020-01-08 13:05:56 +0100 | [diff] [blame] | 1030 | uint32_t array_stride = 0; |
Hans-Kristian Arntzen | 551424c | 2017-10-26 16:35:18 +0200 | [diff] [blame] | 1031 | bool row_major_matrix = false; |
Hans-Kristian Arntzen | 7d7f4b3 | 2017-08-10 17:12:48 +0200 | [diff] [blame] | 1032 | bool immutable = false; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1033 | |
Hans-Kristian Arntzen | acae607 | 2019-01-04 13:19:50 +0100 | [diff] [blame] | 1034 | // By reading this expression, we implicitly read these expressions as well. |
| 1035 | // Used by access chain Store and Load since we read multiple expressions in this case. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1036 | SmallVector<ID> implied_read_expressions; |
Hans-Kristian Arntzen | acae607 | 2019-01-04 13:19:50 +0100 | [diff] [blame] | 1037 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1038 | SPIRV_CROSS_DECLARE_CLONE(SPIRAccessChain) |
Hans-Kristian Arntzen | 3cbdbec | 2017-08-10 15:36:30 +0200 | [diff] [blame] | 1039 | }; |
| 1040 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1041 | struct SPIRVariable : IVariant |
| 1042 | { |
| 1043 | enum |
| 1044 | { |
| 1045 | type = TypeVariable |
| 1046 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1047 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1048 | SPIRVariable() = default; |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1049 | SPIRVariable(TypeID basetype_, spv::StorageClass storage_, ID initializer_ = 0, VariableID basevariable_ = 0) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1050 | : basetype(basetype_) |
| 1051 | , storage(storage_) |
| 1052 | , initializer(initializer_) |
Vadim Shcherbakov | 6c41f9e | 2017-12-06 09:51:23 -0800 | [diff] [blame] | 1053 | , basevariable(basevariable_) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1054 | { |
| 1055 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1056 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1057 | TypeID basetype = 0; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1058 | spv::StorageClass storage = spv::StorageClassGeneric; |
| 1059 | uint32_t decoration = 0; |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1060 | ID initializer = 0; |
| 1061 | VariableID basevariable = 0; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1062 | |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1063 | SmallVector<uint32_t> dereference_chain; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1064 | bool compat_builtin = false; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1065 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1066 | // If a variable is shadowed, we only statically assign to it |
| 1067 | // and never actually emit a statement for it. |
| 1068 | // When we read the variable as an expression, just forward |
| 1069 | // shadowed_id as the expression. |
| 1070 | bool statically_assigned = false; |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1071 | ID static_expression = 0; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1072 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1073 | // Temporaries which can remain forwarded as long as this variable is not modified. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1074 | SmallVector<ID> dependees; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1075 | bool forwardable = true; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1076 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1077 | bool deferred_declaration = false; |
| 1078 | bool phi_variable = false; |
Hans-Kristian Arntzen | d4926a0 | 2019-01-07 14:19:27 +0100 | [diff] [blame] | 1079 | |
| 1080 | // Used to deal with Phi variable flushes. See flush_phi(). |
| 1081 | bool allocate_temporary_copy = false; |
| 1082 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1083 | bool remapped_variable = false; |
Hans-Kristian Arntzen | 078eec5 | 2016-07-06 11:04:06 +0200 | [diff] [blame] | 1084 | uint32_t remapped_components = 0; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1085 | |
Hans-Kristian Arntzen | 4f07a32 | 2016-12-15 17:14:47 +0100 | [diff] [blame] | 1086 | // The block which dominates all access to this variable. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1087 | BlockID dominator = 0; |
Hans-Kristian Arntzen | 4f07a32 | 2016-12-15 17:14:47 +0100 | [diff] [blame] | 1088 | // If true, this variable is a loop variable, when accessing the variable |
| 1089 | // outside a loop, |
| 1090 | // we should statically forward it. |
| 1091 | bool loop_variable = false; |
| 1092 | // Set to true while we're inside the for loop. |
| 1093 | bool loop_variable_enable = false; |
| 1094 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1095 | SPIRFunction::Parameter *parameter = nullptr; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1096 | |
| 1097 | SPIRV_CROSS_DECLARE_CLONE(SPIRVariable) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1098 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1099 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1100 | struct SPIRConstant : IVariant |
| 1101 | { |
| 1102 | enum |
| 1103 | { |
| 1104 | type = TypeConstant |
| 1105 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1106 | |
Hans-Kristian Arntzen | d573a95 | 2020-07-01 11:42:58 +0200 | [diff] [blame] | 1107 | union Constant |
| 1108 | { |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1109 | uint32_t u32; |
| 1110 | int32_t i32; |
| 1111 | float f32; |
Hans-Kristian Arntzen | fa0255c | 2016-07-27 10:59:00 +0200 | [diff] [blame] | 1112 | |
| 1113 | uint64_t u64; |
| 1114 | int64_t i64; |
| 1115 | double f64; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1116 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1117 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1118 | struct ConstantVector |
| 1119 | { |
| 1120 | Constant r[4]; |
Hans-Kristian Arntzen | 153fed0 | 2017-09-28 13:28:44 +0200 | [diff] [blame] | 1121 | // If != 0, this element is a specialization constant, and we should keep track of it as such. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1122 | ID id[4]; |
Hans-Kristian Arntzen | 5e1d6fb | 2017-09-27 15:16:33 +0200 | [diff] [blame] | 1123 | uint32_t vecsize = 1; |
twinaphex | e3f4041 | 2018-02-03 23:33:08 +0100 | [diff] [blame] | 1124 | |
| 1125 | ConstantVector() |
| 1126 | { |
Hans-Kristian Arntzen | 903b798 | 2018-05-07 23:41:53 +0200 | [diff] [blame] | 1127 | memset(r, 0, sizeof(r)); |
twinaphex | e3f4041 | 2018-02-03 23:33:08 +0100 | [diff] [blame] | 1128 | } |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1129 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1130 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1131 | struct ConstantMatrix |
| 1132 | { |
| 1133 | ConstantVector c[4]; |
Hans-Kristian Arntzen | 153fed0 | 2017-09-28 13:28:44 +0200 | [diff] [blame] | 1134 | // If != 0, this column is a specialization constant, and we should keep track of it as such. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1135 | ID id[4]; |
Hans-Kristian Arntzen | 5e1d6fb | 2017-09-27 15:16:33 +0200 | [diff] [blame] | 1136 | uint32_t columns = 1; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1137 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1138 | |
Hans-Kristian Arntzen | 91f85d3 | 2018-03-06 15:32:26 +0100 | [diff] [blame] | 1139 | static inline float f16_to_f32(uint16_t u16_value) |
| 1140 | { |
| 1141 | // Based on the GLM implementation. |
| 1142 | int s = (u16_value >> 15) & 0x1; |
| 1143 | int e = (u16_value >> 10) & 0x1f; |
| 1144 | int m = (u16_value >> 0) & 0x3ff; |
| 1145 | |
Hans-Kristian Arntzen | d573a95 | 2020-07-01 11:42:58 +0200 | [diff] [blame] | 1146 | union |
| 1147 | { |
Hans-Kristian Arntzen | 91f85d3 | 2018-03-06 15:32:26 +0100 | [diff] [blame] | 1148 | float f32; |
| 1149 | uint32_t u32; |
| 1150 | } u; |
| 1151 | |
| 1152 | if (e == 0) |
| 1153 | { |
| 1154 | if (m == 0) |
| 1155 | { |
| 1156 | u.u32 = uint32_t(s) << 31; |
| 1157 | return u.f32; |
| 1158 | } |
| 1159 | else |
| 1160 | { |
| 1161 | while ((m & 0x400) == 0) |
| 1162 | { |
| 1163 | m <<= 1; |
| 1164 | e--; |
| 1165 | } |
| 1166 | |
| 1167 | e++; |
| 1168 | m &= ~0x400; |
| 1169 | } |
| 1170 | } |
| 1171 | else if (e == 31) |
| 1172 | { |
| 1173 | if (m == 0) |
| 1174 | { |
| 1175 | u.u32 = (uint32_t(s) << 31) | 0x7f800000u; |
| 1176 | return u.f32; |
| 1177 | } |
| 1178 | else |
| 1179 | { |
| 1180 | u.u32 = (uint32_t(s) << 31) | 0x7f800000u | (m << 13); |
| 1181 | return u.f32; |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | e += 127 - 15; |
| 1186 | m <<= 13; |
| 1187 | u.u32 = (uint32_t(s) << 31) | (e << 23) | m; |
| 1188 | return u.f32; |
| 1189 | } |
| 1190 | |
Hans-Kristian Arntzen | ceefae5 | 2017-09-27 16:10:29 +0200 | [diff] [blame] | 1191 | inline uint32_t specialization_constant_id(uint32_t col, uint32_t row) const |
| 1192 | { |
| 1193 | return m.c[col].id[row]; |
| 1194 | } |
| 1195 | |
| 1196 | inline uint32_t specialization_constant_id(uint32_t col) const |
| 1197 | { |
| 1198 | return m.id[col]; |
| 1199 | } |
| 1200 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1201 | inline uint32_t scalar(uint32_t col = 0, uint32_t row = 0) const |
| 1202 | { |
| 1203 | return m.c[col].r[row].u32; |
| 1204 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1205 | |
Chip Davis | 117ccf4 | 2018-11-01 17:20:07 -0500 | [diff] [blame] | 1206 | inline int16_t scalar_i16(uint32_t col = 0, uint32_t row = 0) const |
| 1207 | { |
| 1208 | return int16_t(m.c[col].r[row].u32 & 0xffffu); |
| 1209 | } |
| 1210 | |
Hans-Kristian Arntzen | 91f85d3 | 2018-03-06 15:32:26 +0100 | [diff] [blame] | 1211 | inline uint16_t scalar_u16(uint32_t col = 0, uint32_t row = 0) const |
| 1212 | { |
| 1213 | return uint16_t(m.c[col].r[row].u32 & 0xffffu); |
| 1214 | } |
| 1215 | |
Hans-Kristian Arntzen | 2ed171e | 2019-01-30 14:49:55 +0100 | [diff] [blame] | 1216 | inline int8_t scalar_i8(uint32_t col = 0, uint32_t row = 0) const |
| 1217 | { |
| 1218 | return int8_t(m.c[col].r[row].u32 & 0xffu); |
| 1219 | } |
| 1220 | |
| 1221 | inline uint8_t scalar_u8(uint32_t col = 0, uint32_t row = 0) const |
| 1222 | { |
| 1223 | return uint8_t(m.c[col].r[row].u32 & 0xffu); |
| 1224 | } |
| 1225 | |
Hans-Kristian Arntzen | 91f85d3 | 2018-03-06 15:32:26 +0100 | [diff] [blame] | 1226 | inline float scalar_f16(uint32_t col = 0, uint32_t row = 0) const |
| 1227 | { |
| 1228 | return f16_to_f32(scalar_u16(col, row)); |
| 1229 | } |
| 1230 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1231 | inline float scalar_f32(uint32_t col = 0, uint32_t row = 0) const |
| 1232 | { |
| 1233 | return m.c[col].r[row].f32; |
| 1234 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1235 | |
Hans-Kristian Arntzen | fa0255c | 2016-07-27 10:59:00 +0200 | [diff] [blame] | 1236 | inline int32_t scalar_i32(uint32_t col = 0, uint32_t row = 0) const |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1237 | { |
| 1238 | return m.c[col].r[row].i32; |
| 1239 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1240 | |
Hans-Kristian Arntzen | fa0255c | 2016-07-27 10:59:00 +0200 | [diff] [blame] | 1241 | inline double scalar_f64(uint32_t col = 0, uint32_t row = 0) const |
| 1242 | { |
| 1243 | return m.c[col].r[row].f64; |
| 1244 | } |
| 1245 | |
| 1246 | inline int64_t scalar_i64(uint32_t col = 0, uint32_t row = 0) const |
| 1247 | { |
| 1248 | return m.c[col].r[row].i64; |
| 1249 | } |
| 1250 | |
| 1251 | inline uint64_t scalar_u64(uint32_t col = 0, uint32_t row = 0) const |
| 1252 | { |
| 1253 | return m.c[col].r[row].u64; |
| 1254 | } |
| 1255 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1256 | inline const ConstantVector &vector() const |
| 1257 | { |
| 1258 | return m.c[0]; |
| 1259 | } |
Hans-Kristian Arntzen | ceefae5 | 2017-09-27 16:10:29 +0200 | [diff] [blame] | 1260 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1261 | inline uint32_t vector_size() const |
| 1262 | { |
| 1263 | return m.c[0].vecsize; |
| 1264 | } |
Hans-Kristian Arntzen | ceefae5 | 2017-09-27 16:10:29 +0200 | [diff] [blame] | 1265 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1266 | inline uint32_t columns() const |
| 1267 | { |
| 1268 | return m.columns; |
| 1269 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1270 | |
Hans-Kristian Arntzen | 48ccde3 | 2017-08-03 14:32:07 +0200 | [diff] [blame] | 1271 | inline void make_null(const SPIRType &constant_type_) |
| 1272 | { |
Hans-Kristian Arntzen | 903b798 | 2018-05-07 23:41:53 +0200 | [diff] [blame] | 1273 | m = {}; |
Hans-Kristian Arntzen | 48ccde3 | 2017-08-03 14:32:07 +0200 | [diff] [blame] | 1274 | m.columns = constant_type_.columns; |
| 1275 | for (auto &c : m.c) |
| 1276 | c.vecsize = constant_type_.vecsize; |
| 1277 | } |
| 1278 | |
Hans-Kristian Arntzen | 649ce3c | 2019-01-07 10:01:00 +0100 | [diff] [blame] | 1279 | inline bool constant_is_null() const |
| 1280 | { |
| 1281 | if (specialization) |
| 1282 | return false; |
| 1283 | if (!subconstants.empty()) |
| 1284 | return false; |
| 1285 | |
| 1286 | for (uint32_t col = 0; col < columns(); col++) |
| 1287 | for (uint32_t row = 0; row < vector_size(); row++) |
| 1288 | if (scalar_u64(col, row) != 0) |
| 1289 | return false; |
| 1290 | |
| 1291 | return true; |
| 1292 | } |
| 1293 | |
Hans-Kristian Arntzen | 84f8c99 | 2017-09-29 10:13:45 +0200 | [diff] [blame] | 1294 | explicit SPIRConstant(uint32_t constant_type_) |
Hans-Kristian Arntzen | 2abdc13 | 2017-08-02 10:33:03 +0200 | [diff] [blame] | 1295 | : constant_type(constant_type_) |
| 1296 | { |
Hans-Kristian Arntzen | 2abdc13 | 2017-08-02 10:33:03 +0200 | [diff] [blame] | 1297 | } |
| 1298 | |
Hans-Kristian Arntzen | 3951b94 | 2018-05-15 11:16:06 +0200 | [diff] [blame] | 1299 | SPIRConstant() = default; |
| 1300 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1301 | SPIRConstant(TypeID constant_type_, const uint32_t *elements, uint32_t num_elements, bool specialized) |
Hans-Kristian Arntzen | 153fed0 | 2017-09-28 13:28:44 +0200 | [diff] [blame] | 1302 | : constant_type(constant_type_) |
| 1303 | , specialization(specialized) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1304 | { |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1305 | subconstants.reserve(num_elements); |
| 1306 | for (uint32_t i = 0; i < num_elements; i++) |
| 1307 | subconstants.push_back(elements[i]); |
Hans-Kristian Arntzen | 5e1d6fb | 2017-09-27 15:16:33 +0200 | [diff] [blame] | 1308 | specialization = specialized; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1309 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1310 | |
Hans-Kristian Arntzen | 5e1d6fb | 2017-09-27 15:16:33 +0200 | [diff] [blame] | 1311 | // Construct scalar (32-bit). |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1312 | SPIRConstant(TypeID constant_type_, uint32_t v0, bool specialized) |
Hans-Kristian Arntzen | 153fed0 | 2017-09-28 13:28:44 +0200 | [diff] [blame] | 1313 | : constant_type(constant_type_) |
| 1314 | , specialization(specialized) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1315 | { |
| 1316 | m.c[0].r[0].u32 = v0; |
| 1317 | m.c[0].vecsize = 1; |
| 1318 | m.columns = 1; |
| 1319 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1320 | |
Hans-Kristian Arntzen | 5e1d6fb | 2017-09-27 15:16:33 +0200 | [diff] [blame] | 1321 | // Construct scalar (64-bit). |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1322 | SPIRConstant(TypeID constant_type_, uint64_t v0, bool specialized) |
Hans-Kristian Arntzen | 153fed0 | 2017-09-28 13:28:44 +0200 | [diff] [blame] | 1323 | : constant_type(constant_type_) |
| 1324 | , specialization(specialized) |
Hans-Kristian Arntzen | fa0255c | 2016-07-27 10:59:00 +0200 | [diff] [blame] | 1325 | { |
| 1326 | m.c[0].r[0].u64 = v0; |
| 1327 | m.c[0].vecsize = 1; |
| 1328 | m.columns = 1; |
| 1329 | } |
| 1330 | |
Hans-Kristian Arntzen | ceefae5 | 2017-09-27 16:10:29 +0200 | [diff] [blame] | 1331 | // Construct vectors and matrices. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1332 | SPIRConstant(TypeID constant_type_, const SPIRConstant *const *vector_elements, uint32_t num_elements, |
Hans-Kristian Arntzen | 153fed0 | 2017-09-28 13:28:44 +0200 | [diff] [blame] | 1333 | bool specialized) |
| 1334 | : constant_type(constant_type_) |
| 1335 | , specialization(specialized) |
Hans-Kristian Arntzen | fa0255c | 2016-07-27 10:59:00 +0200 | [diff] [blame] | 1336 | { |
Hans-Kristian Arntzen | ceefae5 | 2017-09-27 16:10:29 +0200 | [diff] [blame] | 1337 | bool matrix = vector_elements[0]->m.c[0].vecsize > 1; |
Hans-Kristian Arntzen | 5e1d6fb | 2017-09-27 15:16:33 +0200 | [diff] [blame] | 1338 | |
Hans-Kristian Arntzen | ceefae5 | 2017-09-27 16:10:29 +0200 | [diff] [blame] | 1339 | if (matrix) |
Hans-Kristian Arntzen | 5e1d6fb | 2017-09-27 15:16:33 +0200 | [diff] [blame] | 1340 | { |
Hans-Kristian Arntzen | ceefae5 | 2017-09-27 16:10:29 +0200 | [diff] [blame] | 1341 | m.columns = num_elements; |
Hans-Kristian Arntzen | fa0255c | 2016-07-27 10:59:00 +0200 | [diff] [blame] | 1342 | |
Hans-Kristian Arntzen | ceefae5 | 2017-09-27 16:10:29 +0200 | [diff] [blame] | 1343 | for (uint32_t i = 0; i < num_elements; i++) |
| 1344 | { |
| 1345 | m.c[i] = vector_elements[i]->m.c[0]; |
| 1346 | if (vector_elements[i]->specialization) |
| 1347 | m.id[i] = vector_elements[i]->self; |
| 1348 | } |
| 1349 | } |
| 1350 | else |
| 1351 | { |
| 1352 | m.c[0].vecsize = num_elements; |
| 1353 | m.columns = 1; |
| 1354 | |
| 1355 | for (uint32_t i = 0; i < num_elements; i++) |
| 1356 | { |
| 1357 | m.c[0].r[i] = vector_elements[i]->m.c[0].r[0]; |
| 1358 | if (vector_elements[i]->specialization) |
| 1359 | m.c[0].id[i] = vector_elements[i]->self; |
| 1360 | } |
| 1361 | } |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1362 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1363 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1364 | TypeID constant_type = 0; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1365 | ConstantMatrix m; |
Hans-Kristian Arntzen | 3951b94 | 2018-05-15 11:16:06 +0200 | [diff] [blame] | 1366 | |
| 1367 | // If this constant is a specialization constant (i.e. created with OpSpecConstant*). |
| 1368 | bool specialization = false; |
| 1369 | // If this constant is used as an array length which creates specialization restrictions on some backends. |
| 1370 | bool is_used_as_array_length = false; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1371 | |
Hans-Kristian Arntzen | d29f48e | 2018-07-05 13:25:57 +0200 | [diff] [blame] | 1372 | // If true, this is a LUT, and should always be declared in the outer scope. |
| 1373 | bool is_used_as_lut = false; |
| 1374 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1375 | // For composites which are constant arrays, etc. |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1376 | SmallVector<ConstantID> subconstants; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1377 | |
Hans-Kristian Arntzen | fd6ff36 | 2018-11-01 10:53:00 +0100 | [diff] [blame] | 1378 | // Non-Vulkan GLSL, HLSL and sometimes MSL emits defines for each specialization constant, |
Grigory Dzhavadyan | a5d82d1 | 2018-10-29 23:31:32 -0700 | [diff] [blame] | 1379 | // and uses them to initialize the constant. This allows the user |
| 1380 | // to still be able to specialize the value by supplying corresponding |
| 1381 | // preprocessor directives before compiling the shader. |
Hans-Kristian Arntzen | fd6ff36 | 2018-11-01 10:53:00 +0100 | [diff] [blame] | 1382 | std::string specialization_constant_macro_name; |
Grigory Dzhavadyan | a5d82d1 | 2018-10-29 23:31:32 -0700 | [diff] [blame] | 1383 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1384 | SPIRV_CROSS_DECLARE_CLONE(SPIRConstant) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1385 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1386 | |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1387 | // Variants have a very specific allocation scheme. |
| 1388 | struct ObjectPoolGroup |
| 1389 | { |
| 1390 | std::unique_ptr<ObjectPoolBase> pools[TypeCount]; |
| 1391 | }; |
| 1392 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1393 | class Variant |
| 1394 | { |
| 1395 | public: |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1396 | explicit Variant(ObjectPoolGroup *group_) |
| 1397 | : group(group_) |
| 1398 | { |
| 1399 | } |
| 1400 | |
| 1401 | ~Variant() |
| 1402 | { |
| 1403 | if (holder) |
Hans-Kristian Arntzen | 96d95fb | 2021-09-30 14:12:08 +0200 | [diff] [blame] | 1404 | group->pools[type]->deallocate_opaque(holder); |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1405 | } |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1406 | |
| 1407 | // Marking custom move constructor as noexcept is important. |
| 1408 | Variant(Variant &&other) SPIRV_CROSS_NOEXCEPT |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1409 | { |
| 1410 | *this = std::move(other); |
| 1411 | } |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1412 | |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1413 | // We cannot copy from other variant without our own pool group. |
| 1414 | // Have to explicitly copy. |
| 1415 | Variant(const Variant &variant) = delete; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1416 | |
| 1417 | // Marking custom move constructor as noexcept is important. |
| 1418 | Variant &operator=(Variant &&other) SPIRV_CROSS_NOEXCEPT |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1419 | { |
| 1420 | if (this != &other) |
| 1421 | { |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1422 | if (holder) |
Hans-Kristian Arntzen | 96d95fb | 2021-09-30 14:12:08 +0200 | [diff] [blame] | 1423 | group->pools[type]->deallocate_opaque(holder); |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1424 | holder = other.holder; |
| 1425 | group = other.group; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1426 | type = other.type; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1427 | allow_type_rewrite = other.allow_type_rewrite; |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1428 | |
| 1429 | other.holder = nullptr; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1430 | other.type = TypeNone; |
| 1431 | } |
| 1432 | return *this; |
| 1433 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1434 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1435 | // This copy/clone should only be called in the Compiler constructor. |
| 1436 | // If this is called inside ::compile(), we invalidate any references we took higher in the stack. |
| 1437 | // This should never happen. |
| 1438 | Variant &operator=(const Variant &other) |
| 1439 | { |
Hans-Kristian Arntzen | 03ddea8 | 2019-04-09 13:07:30 +0200 | [diff] [blame] | 1440 | //#define SPIRV_CROSS_COPY_CONSTRUCTOR_SANITIZE |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1441 | #ifdef SPIRV_CROSS_COPY_CONSTRUCTOR_SANITIZE |
| 1442 | abort(); |
| 1443 | #endif |
| 1444 | if (this != &other) |
| 1445 | { |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1446 | if (holder) |
Hans-Kristian Arntzen | 96d95fb | 2021-09-30 14:12:08 +0200 | [diff] [blame] | 1447 | group->pools[type]->deallocate_opaque(holder); |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1448 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1449 | if (other.holder) |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1450 | holder = other.holder->clone(group->pools[other.type].get()); |
Hans-Kristian Arntzen | 0262f60 | 2019-04-09 15:25:11 +0200 | [diff] [blame] | 1451 | else |
| 1452 | holder = nullptr; |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1453 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1454 | type = other.type; |
| 1455 | allow_type_rewrite = other.allow_type_rewrite; |
| 1456 | } |
| 1457 | return *this; |
| 1458 | } |
| 1459 | |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1460 | void set(IVariant *val, Types new_type) |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1461 | { |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1462 | if (holder) |
Hans-Kristian Arntzen | 96d95fb | 2021-09-30 14:12:08 +0200 | [diff] [blame] | 1463 | group->pools[type]->deallocate_opaque(holder); |
Hans-Kristian Arntzen | 0262f60 | 2019-04-09 15:25:11 +0200 | [diff] [blame] | 1464 | holder = nullptr; |
| 1465 | |
Lukas Hermanns | 7ad0a84 | 2019-09-23 18:05:04 -0400 | [diff] [blame] | 1466 | if (!allow_type_rewrite && type != TypeNone && type != new_type) |
Hans-Kristian Arntzen | 0262f60 | 2019-04-09 15:25:11 +0200 | [diff] [blame] | 1467 | { |
| 1468 | if (val) |
Hans-Kristian Arntzen | 96d95fb | 2021-09-30 14:12:08 +0200 | [diff] [blame] | 1469 | group->pools[new_type]->deallocate_opaque(val); |
Panagiotis Christopoulos Charitos | 946f779 | 2016-12-12 22:33:22 +0100 | [diff] [blame] | 1470 | SPIRV_CROSS_THROW("Overwriting a variant with new type."); |
Lukas Hermanns | 7ad0a84 | 2019-09-23 18:05:04 -0400 | [diff] [blame] | 1471 | } |
Hans-Kristian Arntzen | 0262f60 | 2019-04-09 15:25:11 +0200 | [diff] [blame] | 1472 | |
| 1473 | holder = val; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1474 | type = new_type; |
Hans-Kristian Arntzen | 7eba247 | 2018-05-11 10:14:20 +0200 | [diff] [blame] | 1475 | allow_type_rewrite = false; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1476 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1477 | |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1478 | template <typename T, typename... Ts> |
| 1479 | T *allocate_and_set(Types new_type, Ts &&... ts) |
| 1480 | { |
| 1481 | T *val = static_cast<ObjectPool<T> &>(*group->pools[new_type]).allocate(std::forward<Ts>(ts)...); |
| 1482 | set(val, new_type); |
| 1483 | return val; |
| 1484 | } |
| 1485 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1486 | template <typename T> |
| 1487 | T &get() |
| 1488 | { |
| 1489 | if (!holder) |
Panagiotis Christopoulos Charitos | 946f779 | 2016-12-12 22:33:22 +0100 | [diff] [blame] | 1490 | SPIRV_CROSS_THROW("nullptr"); |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 1491 | if (static_cast<Types>(T::type) != type) |
Panagiotis Christopoulos Charitos | 946f779 | 2016-12-12 22:33:22 +0100 | [diff] [blame] | 1492 | SPIRV_CROSS_THROW("Bad cast"); |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1493 | return *static_cast<T *>(holder); |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1494 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1495 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1496 | template <typename T> |
| 1497 | const T &get() const |
| 1498 | { |
| 1499 | if (!holder) |
Panagiotis Christopoulos Charitos | 946f779 | 2016-12-12 22:33:22 +0100 | [diff] [blame] | 1500 | SPIRV_CROSS_THROW("nullptr"); |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 1501 | if (static_cast<Types>(T::type) != type) |
Panagiotis Christopoulos Charitos | 946f779 | 2016-12-12 22:33:22 +0100 | [diff] [blame] | 1502 | SPIRV_CROSS_THROW("Bad cast"); |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1503 | return *static_cast<const T *>(holder); |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1504 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1505 | |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 1506 | Types get_type() const |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1507 | { |
| 1508 | return type; |
| 1509 | } |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1510 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1511 | ID get_id() const |
Bill Hollings | 1c18078 | 2017-11-05 21:34:42 -0500 | [diff] [blame] | 1512 | { |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1513 | return holder ? holder->self : ID(0); |
Bill Hollings | 1c18078 | 2017-11-05 21:34:42 -0500 | [diff] [blame] | 1514 | } |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1515 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1516 | bool empty() const |
| 1517 | { |
| 1518 | return !holder; |
| 1519 | } |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1520 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1521 | void reset() |
| 1522 | { |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1523 | if (holder) |
Hans-Kristian Arntzen | 96d95fb | 2021-09-30 14:12:08 +0200 | [diff] [blame] | 1524 | group->pools[type]->deallocate_opaque(holder); |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1525 | holder = nullptr; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1526 | type = TypeNone; |
| 1527 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1528 | |
Hans-Kristian Arntzen | 7eba247 | 2018-05-11 10:14:20 +0200 | [diff] [blame] | 1529 | void set_allow_type_rewrite() |
| 1530 | { |
| 1531 | allow_type_rewrite = true; |
| 1532 | } |
| 1533 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1534 | private: |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1535 | ObjectPoolGroup *group = nullptr; |
| 1536 | IVariant *holder = nullptr; |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 1537 | Types type = TypeNone; |
Hans-Kristian Arntzen | 7eba247 | 2018-05-11 10:14:20 +0200 | [diff] [blame] | 1538 | bool allow_type_rewrite = false; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1539 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1540 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1541 | template <typename T> |
| 1542 | T &variant_get(Variant &var) |
| 1543 | { |
| 1544 | return var.get<T>(); |
| 1545 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1546 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1547 | template <typename T> |
| 1548 | const T &variant_get(const Variant &var) |
| 1549 | { |
| 1550 | return var.get<T>(); |
| 1551 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1552 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1553 | template <typename T, typename... P> |
| 1554 | T &variant_set(Variant &var, P &&... args) |
| 1555 | { |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1556 | auto *ptr = var.allocate_and_set<T>(static_cast<Types>(T::type), std::forward<P>(args)...); |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1557 | return *ptr; |
| 1558 | } |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1559 | |
Hans-Kristian Arntzen | 816c116 | 2018-11-22 11:55:57 +0100 | [diff] [blame] | 1560 | struct AccessChainMeta |
| 1561 | { |
Hans-Kristian Arntzen | a86308b | 2019-07-18 13:34:47 +0200 | [diff] [blame] | 1562 | uint32_t storage_physical_type = 0; |
Hans-Kristian Arntzen | 816c116 | 2018-11-22 11:55:57 +0100 | [diff] [blame] | 1563 | bool need_transpose = false; |
| 1564 | bool storage_is_packed = false; |
| 1565 | bool storage_is_invariant = false; |
Hans-Kristian Arntzen | 2d43103 | 2020-07-03 13:12:05 +0200 | [diff] [blame] | 1566 | bool flattened_struct = false; |
Hans-Kristian Arntzen | 816c116 | 2018-11-22 11:55:57 +0100 | [diff] [blame] | 1567 | }; |
| 1568 | |
Hans-Kristian Arntzen | a86308b | 2019-07-18 13:34:47 +0200 | [diff] [blame] | 1569 | enum ExtendedDecorations |
| 1570 | { |
| 1571 | // Marks if a buffer block is re-packed, i.e. member declaration might be subject to PhysicalTypeID remapping and padding. |
| 1572 | SPIRVCrossDecorationBufferBlockRepacked = 0, |
| 1573 | |
| 1574 | // A type in a buffer block might be declared with a different physical type than the logical type. |
| 1575 | // If this is not set, PhysicalTypeID == the SPIR-V type as declared. |
| 1576 | SPIRVCrossDecorationPhysicalTypeID, |
| 1577 | |
| 1578 | // Marks if the physical type is to be declared with tight packing rules, i.e. packed_floatN on MSL and friends. |
| 1579 | // If this is set, PhysicalTypeID might also be set. It can be set to same as logical type if all we're doing |
| 1580 | // is converting float3 to packed_float3 for example. |
Hans-Kristian Arntzen | e90d816 | 2019-07-19 14:18:14 +0200 | [diff] [blame] | 1581 | // If this is marked on a struct, it means the struct itself must use only Packed types for all its members. |
Hans-Kristian Arntzen | a86308b | 2019-07-18 13:34:47 +0200 | [diff] [blame] | 1582 | SPIRVCrossDecorationPhysicalTypePacked, |
| 1583 | |
| 1584 | // The padding in bytes before declaring this struct member. |
Hans-Kristian Arntzen | be2fccd | 2019-07-22 10:23:39 +0200 | [diff] [blame] | 1585 | // If used on a struct type, marks the target size of a struct. |
| 1586 | SPIRVCrossDecorationPaddingTarget, |
Hans-Kristian Arntzen | a86308b | 2019-07-18 13:34:47 +0200 | [diff] [blame] | 1587 | |
| 1588 | SPIRVCrossDecorationInterfaceMemberIndex, |
| 1589 | SPIRVCrossDecorationInterfaceOrigID, |
| 1590 | SPIRVCrossDecorationResourceIndexPrimary, |
| 1591 | // Used for decorations like resource indices for samplers when part of combined image samplers. |
| 1592 | // A variable might need to hold two resource indices in this case. |
| 1593 | SPIRVCrossDecorationResourceIndexSecondary, |
Chip Davis | 39dce88 | 2019-08-02 15:11:19 -0500 | [diff] [blame] | 1594 | // Used for resource indices for multiplanar images when part of combined image samplers. |
| 1595 | SPIRVCrossDecorationResourceIndexTertiary, |
| 1596 | SPIRVCrossDecorationResourceIndexQuaternary, |
Hans-Kristian Arntzen | a86308b | 2019-07-18 13:34:47 +0200 | [diff] [blame] | 1597 | |
| 1598 | // Marks a buffer block for using explicit offsets (GLSL/HLSL). |
| 1599 | SPIRVCrossDecorationExplicitOffset, |
| 1600 | |
Chip Davis | 688c5fc | 2020-02-20 21:38:28 -0600 | [diff] [blame] | 1601 | // Apply to a variable in the Input storage class; marks it as holding the base group passed to vkCmdDispatchBase(), |
| 1602 | // or the base vertex and instance indices passed to vkCmdDrawIndexed(). |
| 1603 | // In MSL, this is used to adjust the WorkgroupId and GlobalInvocationId variables in compute shaders, |
| 1604 | // and to hold the BaseVertex and BaseInstance variables in vertex shaders. |
Chip Davis | fb5ee4c | 2019-07-22 13:08:04 -0500 | [diff] [blame] | 1605 | SPIRVCrossDecorationBuiltInDispatchBase, |
| 1606 | |
Chip Davis | 39dce88 | 2019-08-02 15:11:19 -0500 | [diff] [blame] | 1607 | // Apply to a variable that is a function parameter; marks it as being a "dynamic" |
| 1608 | // combined image-sampler. In MSL, this is used when a function parameter might hold |
| 1609 | // either a regular combined image-sampler or one that has an attached sampler |
| 1610 | // Y'CbCr conversion. |
| 1611 | SPIRVCrossDecorationDynamicImageSampler, |
| 1612 | |
Chip Davis | 688c5fc | 2020-02-20 21:38:28 -0600 | [diff] [blame] | 1613 | // Apply to a variable in the Input storage class; marks it as holding the size of the stage |
| 1614 | // input grid. |
| 1615 | // In MSL, this is used to hold the vertex and instance counts in a tessellation pipeline |
| 1616 | // vertex shader. |
| 1617 | SPIRVCrossDecorationBuiltInStageInputSize, |
| 1618 | |
| 1619 | // Apply to any access chain of a tessellation I/O variable; stores the type of the sub-object |
| 1620 | // that was chained to, as recorded in the input variable itself. This is used in case the pointer |
| 1621 | // is itself used as the base of an access chain, to calculate the original type of the sub-object |
| 1622 | // chained to, in case a swizzle needs to be applied. This should not happen normally with valid |
| 1623 | // SPIR-V, but the MSL backend can change the type of input variables, necessitating the |
| 1624 | // addition of swizzles to keep the generated code compiling. |
| 1625 | SPIRVCrossDecorationTessIOOriginalInputTypeID, |
| 1626 | |
Chip Davis | aca9b68 | 2020-11-02 20:56:46 -0600 | [diff] [blame] | 1627 | // Apply to any access chain of an interface variable used with pull-model interpolation, where the variable is a |
| 1628 | // vector but the resulting pointer is a scalar; stores the component index that is to be accessed by the chain. |
| 1629 | // This is used when emitting calls to interpolation functions on the chain in MSL: in this case, the component |
| 1630 | // must be applied to the result, since pull-model interpolants in MSL cannot be swizzled directly, but the |
| 1631 | // results of interpolation can. |
| 1632 | SPIRVCrossDecorationInterpolantComponentExpr, |
| 1633 | |
Hans-Kristian Arntzen | a86308b | 2019-07-18 13:34:47 +0200 | [diff] [blame] | 1634 | SPIRVCrossDecorationCount |
| 1635 | }; |
| 1636 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1637 | struct Meta |
| 1638 | { |
| 1639 | struct Decoration |
| 1640 | { |
| 1641 | std::string alias; |
Bill Hollings | ac00c60 | 2016-10-24 09:24:24 -0400 | [diff] [blame] | 1642 | std::string qualified_alias; |
Hans-Kristian Arntzen | 215d3ca | 2018-03-20 20:04:12 +0100 | [diff] [blame] | 1643 | std::string hlsl_semantic; |
Hans-Kristian Arntzen | e8e5884 | 2018-03-12 13:09:25 +0100 | [diff] [blame] | 1644 | Bitset decoration_flags; |
Hans-Kristian Arntzen | 5345756 | 2019-01-08 11:03:59 +0100 | [diff] [blame] | 1645 | spv::BuiltIn builtin_type = spv::BuiltInMax; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1646 | uint32_t location = 0; |
Chip Davis | 4b99fdd | 2018-09-05 17:31:10 -0500 | [diff] [blame] | 1647 | uint32_t component = 0; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1648 | uint32_t set = 0; |
| 1649 | uint32_t binding = 0; |
| 1650 | uint32_t offset = 0; |
Hans-Kristian Arntzen | 655312c | 2020-01-27 12:56:48 +0100 | [diff] [blame] | 1651 | uint32_t xfb_buffer = 0; |
| 1652 | uint32_t xfb_stride = 0; |
Hans-Kristian Arntzen | e0c9aad | 2020-09-30 13:01:35 +0200 | [diff] [blame] | 1653 | uint32_t stream = 0; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1654 | uint32_t array_stride = 0; |
Hans-Kristian Arntzen | d3cad99 | 2017-01-21 11:30:33 +0100 | [diff] [blame] | 1655 | uint32_t matrix_stride = 0; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1656 | uint32_t input_attachment = 0; |
Hans-Kristian Arntzen | 6bd545b | 2016-09-17 15:16:07 +0200 | [diff] [blame] | 1657 | uint32_t spec_id = 0; |
Hans-Kristian Arntzen | a6e211e | 2018-04-03 15:56:22 +0200 | [diff] [blame] | 1658 | uint32_t index = 0; |
Hans-Kristian Arntzen | 6e5df7a | 2019-01-07 10:51:44 +0100 | [diff] [blame] | 1659 | spv::FPRoundingMode fp_rounding_mode = spv::FPRoundingModeMax; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1660 | bool builtin = false; |
Hans-Kristian Arntzen | de7e5cc | 2019-01-17 11:22:24 +0100 | [diff] [blame] | 1661 | |
Hans-Kristian Arntzen | a86308b | 2019-07-18 13:34:47 +0200 | [diff] [blame] | 1662 | struct Extended |
Hans-Kristian Arntzen | de7e5cc | 2019-01-17 11:22:24 +0100 | [diff] [blame] | 1663 | { |
Hans-Kristian Arntzen | a86308b | 2019-07-18 13:34:47 +0200 | [diff] [blame] | 1664 | Extended() |
| 1665 | { |
| 1666 | // MSVC 2013 workaround to init like this. |
| 1667 | for (auto &v : values) |
| 1668 | v = 0; |
| 1669 | } |
| 1670 | |
| 1671 | Bitset flags; |
| 1672 | uint32_t values[SPIRVCrossDecorationCount]; |
Hans-Kristian Arntzen | de7e5cc | 2019-01-17 11:22:24 +0100 | [diff] [blame] | 1673 | } extended; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1674 | }; |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1675 | |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1676 | Decoration decoration; |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1677 | |
| 1678 | // Intentionally not a SmallVector. Decoration is large and somewhat rare. |
| 1679 | Vector<Decoration> members; |
Hans-Kristian Arntzen | 93fe19c | 2017-04-25 20:10:24 +0200 | [diff] [blame] | 1680 | |
Hans-Kristian Arntzen | 173dc9e | 2017-04-26 09:25:28 +0200 | [diff] [blame] | 1681 | std::unordered_map<uint32_t, uint32_t> decoration_word_offset; |
Hans-Kristian Arntzen | ec45c9e | 2017-04-19 17:33:14 +0200 | [diff] [blame] | 1682 | |
Hans-Kristian Arntzen | 9aa623a | 2018-11-22 10:23:58 +0100 | [diff] [blame] | 1683 | // For SPV_GOOGLE_hlsl_functionality1. |
Hans-Kristian Arntzen | 215d3ca | 2018-03-20 20:04:12 +0100 | [diff] [blame] | 1684 | bool hlsl_is_magic_counter_buffer = false; |
| 1685 | // ID for the sibling counter buffer. |
| 1686 | uint32_t hlsl_magic_counter_buffer = 0; |
Hans-Kristian Arntzen | 4b8ed53 | 2016-05-05 09:33:18 +0200 | [diff] [blame] | 1687 | }; |
Panagiotis Christopoulos Charitos | 66e76d9 | 2016-09-20 10:17:41 +0200 | [diff] [blame] | 1688 | |
| 1689 | // A user callback that remaps the type of any variable. |
Hans-Kristian Arntzen | 4d4e6d7 | 2016-09-20 10:55:09 +0200 | [diff] [blame] | 1690 | // var_name is the declared name of the variable. |
| 1691 | // name_of_type is the textual name of the type which will be used in the code unless written to by the callback. |
Panagiotis Christopoulos Charitos | 66e76d9 | 2016-09-20 10:17:41 +0200 | [diff] [blame] | 1692 | using VariableTypeRemapCallback = |
| 1693 | std::function<void(const SPIRType &type, const std::string &var_name, std::string &name_of_type)>; |
Endre Oma | 6ad8b30 | 2017-01-11 15:57:05 +0100 | [diff] [blame] | 1694 | |
Hans-Kristian Arntzen | a04bdcc | 2018-02-23 14:13:46 +0100 | [diff] [blame] | 1695 | class Hasher |
| 1696 | { |
| 1697 | public: |
| 1698 | inline void u32(uint32_t value) |
| 1699 | { |
| 1700 | h = (h * 0x100000001b3ull) ^ value; |
| 1701 | } |
| 1702 | |
| 1703 | inline uint64_t get() const |
| 1704 | { |
| 1705 | return h; |
| 1706 | } |
| 1707 | |
| 1708 | private: |
| 1709 | uint64_t h = 0xcbf29ce484222325ull; |
| 1710 | }; |
Hans-Kristian Arntzen | 47d94ff | 2018-03-07 10:21:25 +0100 | [diff] [blame] | 1711 | |
| 1712 | static inline bool type_is_floating_point(const SPIRType &type) |
| 1713 | { |
| 1714 | return type.basetype == SPIRType::Half || type.basetype == SPIRType::Float || type.basetype == SPIRType::Double; |
| 1715 | } |
Chip Davis | 9e6469b | 2018-09-04 16:08:22 -0500 | [diff] [blame] | 1716 | |
| 1717 | static inline bool type_is_integral(const SPIRType &type) |
| 1718 | { |
Chip Davis | 117ccf4 | 2018-11-01 17:20:07 -0500 | [diff] [blame] | 1719 | return type.basetype == SPIRType::SByte || type.basetype == SPIRType::UByte || type.basetype == SPIRType::Short || |
| 1720 | type.basetype == SPIRType::UShort || type.basetype == SPIRType::Int || type.basetype == SPIRType::UInt || |
| 1721 | type.basetype == SPIRType::Int64 || type.basetype == SPIRType::UInt64; |
Chip Davis | 9e6469b | 2018-09-04 16:08:22 -0500 | [diff] [blame] | 1722 | } |
Hans-Kristian Arntzen | 2ed171e | 2019-01-30 14:49:55 +0100 | [diff] [blame] | 1723 | |
| 1724 | static inline SPIRType::BaseType to_signed_basetype(uint32_t width) |
| 1725 | { |
| 1726 | switch (width) |
| 1727 | { |
| 1728 | case 8: |
| 1729 | return SPIRType::SByte; |
| 1730 | case 16: |
| 1731 | return SPIRType::Short; |
| 1732 | case 32: |
| 1733 | return SPIRType::Int; |
| 1734 | case 64: |
| 1735 | return SPIRType::Int64; |
| 1736 | default: |
| 1737 | SPIRV_CROSS_THROW("Invalid bit width."); |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | static inline SPIRType::BaseType to_unsigned_basetype(uint32_t width) |
| 1742 | { |
| 1743 | switch (width) |
| 1744 | { |
| 1745 | case 8: |
| 1746 | return SPIRType::UByte; |
| 1747 | case 16: |
| 1748 | return SPIRType::UShort; |
| 1749 | case 32: |
| 1750 | return SPIRType::UInt; |
| 1751 | case 64: |
| 1752 | return SPIRType::UInt64; |
| 1753 | default: |
| 1754 | SPIRV_CROSS_THROW("Invalid bit width."); |
| 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | // Returns true if an arithmetic operation does not change behavior depending on signedness. |
| 1759 | static inline bool opcode_is_sign_invariant(spv::Op opcode) |
| 1760 | { |
| 1761 | switch (opcode) |
| 1762 | { |
| 1763 | case spv::OpIEqual: |
| 1764 | case spv::OpINotEqual: |
| 1765 | case spv::OpISub: |
| 1766 | case spv::OpIAdd: |
| 1767 | case spv::OpIMul: |
| 1768 | case spv::OpShiftLeftLogical: |
| 1769 | case spv::OpBitwiseOr: |
| 1770 | case spv::OpBitwiseXor: |
| 1771 | case spv::OpBitwiseAnd: |
| 1772 | return true; |
| 1773 | |
| 1774 | default: |
| 1775 | return false; |
| 1776 | } |
| 1777 | } |
Hans-Kristian Arntzen | cc153f8 | 2020-01-09 11:18:14 +0100 | [diff] [blame] | 1778 | |
| 1779 | struct SetBindingPair |
| 1780 | { |
| 1781 | uint32_t desc_set; |
| 1782 | uint32_t binding; |
| 1783 | |
| 1784 | inline bool operator==(const SetBindingPair &other) const |
| 1785 | { |
| 1786 | return desc_set == other.desc_set && binding == other.binding; |
| 1787 | } |
| 1788 | |
| 1789 | inline bool operator<(const SetBindingPair &other) const |
| 1790 | { |
| 1791 | return desc_set < other.desc_set || (desc_set == other.desc_set && binding < other.binding); |
| 1792 | } |
| 1793 | }; |
| 1794 | |
Hans-Kristian Arntzen | 2a2d57d | 2021-03-25 18:08:49 +0100 | [diff] [blame] | 1795 | struct LocationComponentPair |
| 1796 | { |
| 1797 | uint32_t location; |
| 1798 | uint32_t component; |
| 1799 | |
| 1800 | inline bool operator==(const LocationComponentPair &other) const |
| 1801 | { |
| 1802 | return location == other.location && component == other.component; |
| 1803 | } |
| 1804 | |
| 1805 | inline bool operator<(const LocationComponentPair &other) const |
| 1806 | { |
| 1807 | return location < other.location || (location == other.location && component < other.component); |
| 1808 | } |
| 1809 | }; |
| 1810 | |
Hans-Kristian Arntzen | cc153f8 | 2020-01-09 11:18:14 +0100 | [diff] [blame] | 1811 | struct StageSetBinding |
| 1812 | { |
| 1813 | spv::ExecutionModel model; |
| 1814 | uint32_t desc_set; |
| 1815 | uint32_t binding; |
| 1816 | |
| 1817 | inline bool operator==(const StageSetBinding &other) const |
| 1818 | { |
| 1819 | return model == other.model && desc_set == other.desc_set && binding == other.binding; |
| 1820 | } |
| 1821 | }; |
| 1822 | |
| 1823 | struct InternalHasher |
| 1824 | { |
| 1825 | inline size_t operator()(const SetBindingPair &value) const |
| 1826 | { |
| 1827 | // Quality of hash doesn't really matter here. |
| 1828 | auto hash_set = std::hash<uint32_t>()(value.desc_set); |
| 1829 | auto hash_binding = std::hash<uint32_t>()(value.binding); |
| 1830 | return (hash_set * 0x10001b31) ^ hash_binding; |
| 1831 | } |
| 1832 | |
Hans-Kristian Arntzen | 2a2d57d | 2021-03-25 18:08:49 +0100 | [diff] [blame] | 1833 | inline size_t operator()(const LocationComponentPair &value) const |
| 1834 | { |
| 1835 | // Quality of hash doesn't really matter here. |
| 1836 | auto hash_set = std::hash<uint32_t>()(value.location); |
| 1837 | auto hash_binding = std::hash<uint32_t>()(value.component); |
| 1838 | return (hash_set * 0x10001b31) ^ hash_binding; |
| 1839 | } |
| 1840 | |
Hans-Kristian Arntzen | cc153f8 | 2020-01-09 11:18:14 +0100 | [diff] [blame] | 1841 | inline size_t operator()(const StageSetBinding &value) const |
| 1842 | { |
| 1843 | // Quality of hash doesn't really matter here. |
| 1844 | auto hash_model = std::hash<uint32_t>()(value.model); |
| 1845 | auto hash_set = std::hash<uint32_t>()(value.desc_set); |
| 1846 | auto tmp_hash = (hash_model * 0x10001b31) ^ hash_set; |
| 1847 | return (tmp_hash * 0x10001b31) ^ value.binding; |
| 1848 | } |
| 1849 | }; |
| 1850 | |
| 1851 | // Special constant used in a {MSL,HLSL}ResourceBinding desc_set |
| 1852 | // element to indicate the bindings for the push constants. |
| 1853 | static const uint32_t ResourceBindingPushConstantDescriptorSet = ~(0u); |
| 1854 | |
| 1855 | // Special constant used in a {MSL,HLSL}ResourceBinding binding |
| 1856 | // element to indicate the bindings for the push constants. |
| 1857 | static const uint32_t ResourceBindingPushConstantBinding = 0; |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1858 | } // namespace SPIRV_CROSS_NAMESPACE |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1859 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 1860 | namespace std |
| 1861 | { |
| 1862 | template <SPIRV_CROSS_NAMESPACE::Types type> |
| 1863 | struct hash<SPIRV_CROSS_NAMESPACE::TypedID<type>> |
| 1864 | { |
| 1865 | size_t operator()(const SPIRV_CROSS_NAMESPACE::TypedID<type> &value) const |
| 1866 | { |
| 1867 | return std::hash<uint32_t>()(value); |
| 1868 | } |
| 1869 | }; |
| 1870 | } // namespace std |
| 1871 | |
Hans-Kristian Arntzen | 75471fb | 2016-03-02 18:09:16 +0100 | [diff] [blame] | 1872 | #endif |