Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 1 | /* |
Hans-Kristian Arntzen | f9818f0 | 2020-01-16 15:24:37 +0100 | [diff] [blame] | 2 | * Copyright 2018-2020 Arm Limited |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "spirv_cross_parsed_ir.hpp" |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 18 | #include <algorithm> |
Hans-Kristian Arntzen | 6e1c3cc | 2019-01-11 12:56:00 +0100 | [diff] [blame] | 19 | #include <assert.h> |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 20 | |
| 21 | using namespace std; |
| 22 | using namespace spv; |
| 23 | |
Hans-Kristian Arntzen | 9b92e68 | 2019-03-29 10:29:44 +0100 | [diff] [blame] | 24 | namespace SPIRV_CROSS_NAMESPACE |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 25 | { |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 26 | ParsedIR::ParsedIR() |
| 27 | { |
| 28 | // If we move ParsedIR, we need to make sure the pointer stays fixed since the child Variant objects consume a pointer to this group, |
| 29 | // so need an extra pointer here. |
| 30 | pool_group.reset(new ObjectPoolGroup); |
| 31 | |
| 32 | pool_group->pools[TypeType].reset(new ObjectPool<SPIRType>); |
| 33 | pool_group->pools[TypeVariable].reset(new ObjectPool<SPIRVariable>); |
| 34 | pool_group->pools[TypeConstant].reset(new ObjectPool<SPIRConstant>); |
| 35 | pool_group->pools[TypeFunction].reset(new ObjectPool<SPIRFunction>); |
| 36 | pool_group->pools[TypeFunctionPrototype].reset(new ObjectPool<SPIRFunctionPrototype>); |
| 37 | pool_group->pools[TypeBlock].reset(new ObjectPool<SPIRBlock>); |
| 38 | pool_group->pools[TypeExtension].reset(new ObjectPool<SPIRExtension>); |
| 39 | pool_group->pools[TypeExpression].reset(new ObjectPool<SPIRExpression>); |
| 40 | pool_group->pools[TypeConstantOp].reset(new ObjectPool<SPIRConstantOp>); |
| 41 | pool_group->pools[TypeCombinedImageSampler].reset(new ObjectPool<SPIRCombinedImageSampler>); |
| 42 | pool_group->pools[TypeAccessChain].reset(new ObjectPool<SPIRAccessChain>); |
| 43 | pool_group->pools[TypeUndef].reset(new ObjectPool<SPIRUndef>); |
Hans-Kristian Arntzen | 65af09d | 2019-05-28 13:41:46 +0200 | [diff] [blame] | 44 | pool_group->pools[TypeString].reset(new ObjectPool<SPIRString>); |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Hans-Kristian Arntzen | dede9be | 2019-04-09 10:25:41 +0200 | [diff] [blame] | 47 | // Should have been default-implemented, but need this on MSVC 2013. |
| 48 | ParsedIR::ParsedIR(ParsedIR &&other) SPIRV_CROSS_NOEXCEPT |
| 49 | { |
| 50 | *this = move(other); |
| 51 | } |
| 52 | |
| 53 | ParsedIR &ParsedIR::operator=(ParsedIR &&other) SPIRV_CROSS_NOEXCEPT |
| 54 | { |
| 55 | if (this != &other) |
| 56 | { |
Hans-Kristian Arntzen | 02bb986 | 2019-04-09 10:54:28 +0200 | [diff] [blame] | 57 | pool_group = move(other.pool_group); |
Hans-Kristian Arntzen | dede9be | 2019-04-09 10:25:41 +0200 | [diff] [blame] | 58 | spirv = move(other.spirv); |
| 59 | meta = move(other.meta); |
| 60 | for (int i = 0; i < TypeCount; i++) |
Hans-Kristian Arntzen | 9f31a94 | 2019-04-09 15:16:37 +0200 | [diff] [blame] | 61 | ids_for_type[i] = move(other.ids_for_type[i]); |
Hans-Kristian Arntzen | dede9be | 2019-04-09 10:25:41 +0200 | [diff] [blame] | 62 | ids_for_constant_or_type = move(other.ids_for_constant_or_type); |
| 63 | ids_for_constant_or_variable = move(other.ids_for_constant_or_variable); |
| 64 | declared_capabilities = move(other.declared_capabilities); |
| 65 | declared_extensions = move(other.declared_extensions); |
| 66 | block_meta = move(other.block_meta); |
| 67 | continue_block_to_loop_header = move(other.continue_block_to_loop_header); |
| 68 | entry_points = move(other.entry_points); |
Hans-Kristian Arntzen | dede9be | 2019-04-09 10:25:41 +0200 | [diff] [blame] | 69 | ids = move(other.ids); |
Hans-Kristian Arntzen | 2cc374a | 2019-04-24 14:12:50 +0200 | [diff] [blame] | 70 | addressing_model = other.addressing_model; |
| 71 | memory_model = other.memory_model; |
Hans-Kristian Arntzen | 9f31a94 | 2019-04-09 15:16:37 +0200 | [diff] [blame] | 72 | |
| 73 | default_entry_point = other.default_entry_point; |
| 74 | source = other.source; |
Hans-Kristian Arntzen | dd7ebaf | 2019-07-18 17:05:28 +0200 | [diff] [blame] | 75 | loop_iteration_depth_hard = other.loop_iteration_depth_hard; |
| 76 | loop_iteration_depth_soft = other.loop_iteration_depth_soft; |
Hans-Kristian Arntzen | a074415 | 2020-08-21 16:14:13 +0200 | [diff] [blame] | 77 | |
| 78 | meta_needing_name_fixup = std::move(other.meta_needing_name_fixup); |
Hans-Kristian Arntzen | dede9be | 2019-04-09 10:25:41 +0200 | [diff] [blame] | 79 | } |
| 80 | return *this; |
| 81 | } |
| 82 | |
Hans-Kristian Arntzen | 9f31a94 | 2019-04-09 15:16:37 +0200 | [diff] [blame] | 83 | ParsedIR::ParsedIR(const ParsedIR &other) |
| 84 | : ParsedIR() |
| 85 | { |
| 86 | *this = other; |
| 87 | } |
| 88 | |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 89 | ParsedIR &ParsedIR::operator=(const ParsedIR &other) |
| 90 | { |
| 91 | if (this != &other) |
| 92 | { |
| 93 | spirv = other.spirv; |
| 94 | meta = other.meta; |
| 95 | for (int i = 0; i < TypeCount; i++) |
| 96 | ids_for_type[i] = other.ids_for_type[i]; |
| 97 | ids_for_constant_or_type = other.ids_for_constant_or_type; |
| 98 | ids_for_constant_or_variable = other.ids_for_constant_or_variable; |
| 99 | declared_capabilities = other.declared_capabilities; |
| 100 | declared_extensions = other.declared_extensions; |
| 101 | block_meta = other.block_meta; |
| 102 | continue_block_to_loop_header = other.continue_block_to_loop_header; |
| 103 | entry_points = other.entry_points; |
| 104 | default_entry_point = other.default_entry_point; |
| 105 | source = other.source; |
Hans-Kristian Arntzen | dd7ebaf | 2019-07-18 17:05:28 +0200 | [diff] [blame] | 106 | loop_iteration_depth_hard = other.loop_iteration_depth_hard; |
| 107 | loop_iteration_depth_soft = other.loop_iteration_depth_soft; |
Hans-Kristian Arntzen | 2cc374a | 2019-04-24 14:12:50 +0200 | [diff] [blame] | 108 | addressing_model = other.addressing_model; |
| 109 | memory_model = other.memory_model; |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 110 | |
Hans-Kristian Arntzen | a074415 | 2020-08-21 16:14:13 +0200 | [diff] [blame] | 111 | meta_needing_name_fixup = other.meta_needing_name_fixup; |
| 112 | |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 113 | // Very deliberate copying of IDs. There is no default copy constructor, nor a simple default constructor. |
| 114 | // Construct object first so we have the correct allocator set-up, then we can copy object into our new pool group. |
| 115 | ids.clear(); |
| 116 | ids.reserve(other.ids.size()); |
| 117 | for (size_t i = 0; i < other.ids.size(); i++) |
| 118 | { |
| 119 | ids.emplace_back(pool_group.get()); |
| 120 | ids.back() = other.ids[i]; |
| 121 | } |
| 122 | } |
| 123 | return *this; |
| 124 | } |
| 125 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 126 | void ParsedIR::set_id_bounds(uint32_t bounds) |
| 127 | { |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 128 | ids.reserve(bounds); |
| 129 | while (ids.size() < bounds) |
| 130 | ids.emplace_back(pool_group.get()); |
| 131 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 132 | block_meta.resize(bounds); |
| 133 | } |
| 134 | |
Hans-Kristian Arntzen | fc4a07c | 2020-01-06 13:04:57 +0100 | [diff] [blame] | 135 | // Roll our own versions of these functions to avoid potential locale shenanigans. |
| 136 | static bool is_alpha(char c) |
| 137 | { |
| 138 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); |
| 139 | } |
| 140 | |
Hans-Kristian Arntzen | a074415 | 2020-08-21 16:14:13 +0200 | [diff] [blame] | 141 | static bool is_numeric(char c) |
Hans-Kristian Arntzen | fc4a07c | 2020-01-06 13:04:57 +0100 | [diff] [blame] | 142 | { |
Hans-Kristian Arntzen | a074415 | 2020-08-21 16:14:13 +0200 | [diff] [blame] | 143 | return c >= '0' && c <= '9'; |
Hans-Kristian Arntzen | fc4a07c | 2020-01-06 13:04:57 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Hans-Kristian Arntzen | a074415 | 2020-08-21 16:14:13 +0200 | [diff] [blame] | 146 | static bool is_alphanumeric(char c) |
| 147 | { |
| 148 | return is_alpha(c) || is_numeric(c); |
| 149 | } |
| 150 | |
| 151 | static bool is_valid_identifier(const string &name) |
| 152 | { |
| 153 | if (name.empty()) |
| 154 | return true; |
| 155 | |
| 156 | if (is_numeric(name[0])) |
| 157 | return false; |
| 158 | |
| 159 | for (auto c : name) |
| 160 | if (!is_alphanumeric(c) && c != '_') |
| 161 | return false; |
| 162 | |
| 163 | bool saw_underscore = false; |
| 164 | // Two underscores in a row is not a valid identifier either. |
| 165 | // Technically reserved, but it's easier to treat it as invalid. |
| 166 | for (auto c : name) |
| 167 | { |
| 168 | bool is_underscore = c == '_'; |
| 169 | if (is_underscore && saw_underscore) |
| 170 | return false; |
| 171 | saw_underscore = is_underscore; |
| 172 | } |
| 173 | |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | static bool is_reserved_prefix(const string &name) |
| 178 | { |
| 179 | // Generic reserved identifiers used by the implementation. |
| 180 | return name.compare(0, 3, "gl_", 3) == 0 || |
| 181 | // Ignore this case for now, might rewrite internal code to always use spv prefix. |
| 182 | //name.compare(0, 11, "SPIRV_Cross", 11) == 0 || |
| 183 | name.compare(0, 3, "spv", 3) == 0; |
| 184 | } |
| 185 | |
| 186 | static bool is_reserved_identifier(const string &name, bool member, bool allow_reserved_prefixes) |
| 187 | { |
| 188 | if (!allow_reserved_prefixes && is_reserved_prefix(name)) |
| 189 | return true; |
| 190 | |
| 191 | if (member) |
| 192 | { |
| 193 | // Reserved member identifiers come in one form: |
| 194 | // _m[0-9]+$. |
| 195 | if (name.size() < 3) |
| 196 | return false; |
| 197 | |
| 198 | if (name.compare(0, 2, "_m", 2) != 0) |
| 199 | return false; |
| 200 | |
| 201 | size_t index = 2; |
| 202 | while (index < name.size() && is_numeric(name[index])) |
| 203 | index++; |
| 204 | |
| 205 | return index == name.size(); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | // Reserved non-member identifiers come in two forms: |
| 210 | // _[0-9]+$, used for temporaries which map directly to a SPIR-V ID. |
| 211 | // _[0-9]+_, used for auxillary temporaries which derived from a SPIR-V ID. |
| 212 | if (name.size() < 2) |
| 213 | return false; |
| 214 | |
| 215 | if (name[0] != '_' || !is_numeric(name[1])) |
| 216 | return false; |
| 217 | |
| 218 | size_t index = 2; |
| 219 | while (index < name.size() && is_numeric(name[index])) |
| 220 | index++; |
| 221 | |
| 222 | return index == name.size() || (index < name.size() && name[index] == '_'); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | bool ParsedIR::is_globally_reserved_identifier(std::string &str, bool allow_reserved_prefixes) |
| 227 | { |
| 228 | return is_reserved_identifier(str, false, allow_reserved_prefixes); |
| 229 | } |
| 230 | |
| 231 | static string make_unreserved_identifier(const string &name) |
| 232 | { |
| 233 | if (is_reserved_prefix(name)) |
| 234 | return "_RESERVED_IDENTIFIER_FIXUP_" + name; |
| 235 | else |
| 236 | return "_RESERVED_IDENTIFIER_FIXUP" + name; |
| 237 | } |
| 238 | |
| 239 | void ParsedIR::sanitize_underscores(std::string &str) |
| 240 | { |
| 241 | // Compact adjacent underscores to make it valid. |
| 242 | auto dst = str.begin(); |
| 243 | auto src = dst; |
| 244 | bool saw_underscore = false; |
| 245 | while (src != str.end()) |
| 246 | { |
| 247 | bool is_underscore = *src == '_'; |
| 248 | if (saw_underscore && is_underscore) |
| 249 | { |
| 250 | src++; |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | if (dst != src) |
| 255 | *dst = *src; |
| 256 | dst++; |
| 257 | src++; |
| 258 | saw_underscore = is_underscore; |
| 259 | } |
| 260 | } |
| 261 | str.erase(dst, str.end()); |
| 262 | } |
| 263 | |
| 264 | static string ensure_valid_identifier(const string &name) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 265 | { |
| 266 | // Functions in glslangValidator are mangled with name(<mangled> stuff. |
| 267 | // Normally, we would never see '(' in any legal identifiers, so just strip them out. |
| 268 | auto str = name.substr(0, name.find('(')); |
| 269 | |
Hans-Kristian Arntzen | a074415 | 2020-08-21 16:14:13 +0200 | [diff] [blame] | 270 | if (str.empty()) |
| 271 | return str; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 272 | |
Hans-Kristian Arntzen | a074415 | 2020-08-21 16:14:13 +0200 | [diff] [blame] | 273 | if (is_numeric(str[0])) |
| 274 | str[0] = '_'; |
| 275 | |
| 276 | for (auto &c : str) |
| 277 | if (!is_alphanumeric(c) && c != '_') |
| 278 | c = '_'; |
| 279 | |
| 280 | ParsedIR::sanitize_underscores(str); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 281 | return str; |
| 282 | } |
| 283 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 284 | const string &ParsedIR::get_name(ID id) const |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 285 | { |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 286 | auto *m = find_meta(id); |
| 287 | if (m) |
| 288 | return m->decoration.alias; |
| 289 | else |
| 290 | return empty_string; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 293 | const string &ParsedIR::get_member_name(TypeID id, uint32_t index) const |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 294 | { |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 295 | auto *m = find_meta(id); |
| 296 | if (m) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 297 | { |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 298 | if (index >= m->members.size()) |
| 299 | return empty_string; |
| 300 | return m->members[index].alias; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 301 | } |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 302 | else |
| 303 | return empty_string; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Hans-Kristian Arntzen | a074415 | 2020-08-21 16:14:13 +0200 | [diff] [blame] | 306 | void ParsedIR::sanitize_identifier(std::string &name, bool member, bool allow_reserved_prefixes) |
| 307 | { |
| 308 | if (!is_valid_identifier(name)) |
| 309 | name = ensure_valid_identifier(name); |
| 310 | if (is_reserved_identifier(name, member, allow_reserved_prefixes)) |
| 311 | name = make_unreserved_identifier(name); |
| 312 | } |
| 313 | |
| 314 | void ParsedIR::fixup_reserved_names() |
| 315 | { |
| 316 | for (uint32_t id : meta_needing_name_fixup) |
| 317 | { |
| 318 | auto &m = meta[id]; |
| 319 | sanitize_identifier(m.decoration.alias, false, false); |
| 320 | for (auto &memb : m.members) |
| 321 | sanitize_identifier(memb.alias, true, false); |
| 322 | } |
| 323 | meta_needing_name_fixup.clear(); |
| 324 | } |
| 325 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 326 | void ParsedIR::set_name(ID id, const string &name) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 327 | { |
Hans-Kristian Arntzen | a074415 | 2020-08-21 16:14:13 +0200 | [diff] [blame] | 328 | auto &m = meta[id]; |
| 329 | m.decoration.alias = name; |
| 330 | if (!is_valid_identifier(name) || is_reserved_identifier(name, false, false)) |
| 331 | meta_needing_name_fixup.insert(id); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 334 | void ParsedIR::set_member_name(TypeID id, uint32_t index, const string &name) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 335 | { |
Hans-Kristian Arntzen | a074415 | 2020-08-21 16:14:13 +0200 | [diff] [blame] | 336 | auto &m = meta[id]; |
| 337 | m.members.resize(max(meta[id].members.size(), size_t(index) + 1)); |
| 338 | m.members[index].alias = name; |
| 339 | if (!is_valid_identifier(name) || is_reserved_identifier(name, true, false)) |
| 340 | meta_needing_name_fixup.insert(id); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 343 | void ParsedIR::set_decoration_string(ID id, Decoration decoration, const string &argument) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 344 | { |
| 345 | auto &dec = meta[id].decoration; |
| 346 | dec.decoration_flags.set(decoration); |
| 347 | |
| 348 | switch (decoration) |
| 349 | { |
| 350 | case DecorationHlslSemanticGOOGLE: |
| 351 | dec.hlsl_semantic = argument; |
| 352 | break; |
| 353 | |
| 354 | default: |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 359 | void ParsedIR::set_decoration(ID id, Decoration decoration, uint32_t argument) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 360 | { |
| 361 | auto &dec = meta[id].decoration; |
| 362 | dec.decoration_flags.set(decoration); |
| 363 | |
| 364 | switch (decoration) |
| 365 | { |
| 366 | case DecorationBuiltIn: |
| 367 | dec.builtin = true; |
| 368 | dec.builtin_type = static_cast<BuiltIn>(argument); |
| 369 | break; |
| 370 | |
| 371 | case DecorationLocation: |
| 372 | dec.location = argument; |
| 373 | break; |
| 374 | |
| 375 | case DecorationComponent: |
| 376 | dec.component = argument; |
| 377 | break; |
| 378 | |
| 379 | case DecorationOffset: |
| 380 | dec.offset = argument; |
| 381 | break; |
| 382 | |
Hans-Kristian Arntzen | 655312c | 2020-01-27 12:56:48 +0100 | [diff] [blame] | 383 | case DecorationXfbBuffer: |
| 384 | dec.xfb_buffer = argument; |
| 385 | break; |
| 386 | |
| 387 | case DecorationXfbStride: |
| 388 | dec.xfb_stride = argument; |
| 389 | break; |
| 390 | |
Hans-Kristian Arntzen | e0c9aad | 2020-09-30 13:01:35 +0200 | [diff] [blame] | 391 | case DecorationStream: |
| 392 | dec.stream = argument; |
| 393 | break; |
| 394 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 395 | case DecorationArrayStride: |
| 396 | dec.array_stride = argument; |
| 397 | break; |
| 398 | |
| 399 | case DecorationMatrixStride: |
| 400 | dec.matrix_stride = argument; |
| 401 | break; |
| 402 | |
| 403 | case DecorationBinding: |
| 404 | dec.binding = argument; |
| 405 | break; |
| 406 | |
| 407 | case DecorationDescriptorSet: |
| 408 | dec.set = argument; |
| 409 | break; |
| 410 | |
| 411 | case DecorationInputAttachmentIndex: |
| 412 | dec.input_attachment = argument; |
| 413 | break; |
| 414 | |
| 415 | case DecorationSpecId: |
| 416 | dec.spec_id = argument; |
| 417 | break; |
| 418 | |
| 419 | case DecorationIndex: |
| 420 | dec.index = argument; |
| 421 | break; |
| 422 | |
| 423 | case DecorationHlslCounterBufferGOOGLE: |
| 424 | meta[id].hlsl_magic_counter_buffer = argument; |
Hans-Kristian Arntzen | 9aa623a | 2018-11-22 10:23:58 +0100 | [diff] [blame] | 425 | meta[argument].hlsl_is_magic_counter_buffer = true; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 426 | break; |
| 427 | |
Hans-Kristian Arntzen | 6e5df7a | 2019-01-07 10:51:44 +0100 | [diff] [blame] | 428 | case DecorationFPRoundingMode: |
| 429 | dec.fp_rounding_mode = static_cast<FPRoundingMode>(argument); |
| 430 | break; |
| 431 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 432 | default: |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 437 | void ParsedIR::set_member_decoration(TypeID id, uint32_t index, Decoration decoration, uint32_t argument) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 438 | { |
| 439 | meta[id].members.resize(max(meta[id].members.size(), size_t(index) + 1)); |
| 440 | auto &dec = meta[id].members[index]; |
| 441 | dec.decoration_flags.set(decoration); |
| 442 | |
| 443 | switch (decoration) |
| 444 | { |
| 445 | case DecorationBuiltIn: |
| 446 | dec.builtin = true; |
| 447 | dec.builtin_type = static_cast<BuiltIn>(argument); |
| 448 | break; |
| 449 | |
| 450 | case DecorationLocation: |
| 451 | dec.location = argument; |
| 452 | break; |
| 453 | |
| 454 | case DecorationComponent: |
| 455 | dec.component = argument; |
| 456 | break; |
| 457 | |
| 458 | case DecorationBinding: |
| 459 | dec.binding = argument; |
| 460 | break; |
| 461 | |
| 462 | case DecorationOffset: |
| 463 | dec.offset = argument; |
| 464 | break; |
| 465 | |
Hans-Kristian Arntzen | 655312c | 2020-01-27 12:56:48 +0100 | [diff] [blame] | 466 | case DecorationXfbBuffer: |
| 467 | dec.xfb_buffer = argument; |
| 468 | break; |
| 469 | |
| 470 | case DecorationXfbStride: |
| 471 | dec.xfb_stride = argument; |
| 472 | break; |
| 473 | |
Hans-Kristian Arntzen | e0c9aad | 2020-09-30 13:01:35 +0200 | [diff] [blame] | 474 | case DecorationStream: |
| 475 | dec.stream = argument; |
| 476 | break; |
| 477 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 478 | case DecorationSpecId: |
| 479 | dec.spec_id = argument; |
| 480 | break; |
| 481 | |
| 482 | case DecorationMatrixStride: |
| 483 | dec.matrix_stride = argument; |
| 484 | break; |
| 485 | |
| 486 | case DecorationIndex: |
| 487 | dec.index = argument; |
| 488 | break; |
| 489 | |
| 490 | default: |
| 491 | break; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // Recursively marks any constants referenced by the specified constant instruction as being used |
| 496 | // as an array length. The id must be a constant instruction (SPIRConstant or SPIRConstantOp). |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 497 | void ParsedIR::mark_used_as_array_length(ID id) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 498 | { |
| 499 | switch (ids[id].get_type()) |
| 500 | { |
| 501 | case TypeConstant: |
| 502 | get<SPIRConstant>(id).is_used_as_array_length = true; |
| 503 | break; |
| 504 | |
| 505 | case TypeConstantOp: |
| 506 | { |
| 507 | auto &cop = get<SPIRConstantOp>(id); |
lifpan | 89e7d21 | 2019-09-05 06:56:05 +0800 | [diff] [blame] | 508 | if (cop.opcode == OpCompositeExtract) |
| 509 | mark_used_as_array_length(cop.arguments[0]); |
| 510 | else if (cop.opcode == OpCompositeInsert) |
| 511 | { |
| 512 | mark_used_as_array_length(cop.arguments[0]); |
| 513 | mark_used_as_array_length(cop.arguments[1]); |
| 514 | } |
| 515 | else |
| 516 | for (uint32_t arg_id : cop.arguments) |
| 517 | mark_used_as_array_length(arg_id); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 518 | break; |
| 519 | } |
| 520 | |
| 521 | case TypeUndef: |
| 522 | break; |
| 523 | |
| 524 | default: |
| 525 | assert(0); |
| 526 | } |
| 527 | } |
| 528 | |
Hans-Kristian Arntzen | c5826b4 | 2020-11-23 16:26:33 +0100 | [diff] [blame^] | 529 | Bitset ParsedIR::get_buffer_block_type_flags(const SPIRType &type) const |
| 530 | { |
| 531 | if (type.member_types.empty()) |
| 532 | return {}; |
| 533 | |
| 534 | Bitset all_members_flags = get_member_decoration_bitset(type.self, 0); |
| 535 | for (uint32_t i = 1; i < uint32_t(type.member_types.size()); i++) |
| 536 | all_members_flags.merge_and(get_member_decoration_bitset(type.self, i)); |
| 537 | return all_members_flags; |
| 538 | } |
| 539 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 540 | Bitset ParsedIR::get_buffer_block_flags(const SPIRVariable &var) const |
| 541 | { |
| 542 | auto &type = get<SPIRType>(var.basetype); |
| 543 | assert(type.basetype == SPIRType::Struct); |
| 544 | |
| 545 | // Some flags like non-writable, non-readable are actually found |
| 546 | // as member decorations. If all members have a decoration set, propagate |
| 547 | // the decoration up as a regular variable decoration. |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 548 | Bitset base_flags; |
| 549 | auto *m = find_meta(var.self); |
| 550 | if (m) |
| 551 | base_flags = m->decoration.decoration_flags; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 552 | |
| 553 | if (type.member_types.empty()) |
| 554 | return base_flags; |
| 555 | |
Hans-Kristian Arntzen | c5826b4 | 2020-11-23 16:26:33 +0100 | [diff] [blame^] | 556 | auto all_members_flags = get_buffer_block_type_flags(type); |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 557 | base_flags.merge_or(all_members_flags); |
| 558 | return base_flags; |
| 559 | } |
| 560 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 561 | const Bitset &ParsedIR::get_member_decoration_bitset(TypeID id, uint32_t index) const |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 562 | { |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 563 | auto *m = find_meta(id); |
| 564 | if (m) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 565 | { |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 566 | if (index >= m->members.size()) |
| 567 | return cleared_bitset; |
| 568 | return m->members[index].decoration_flags; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 569 | } |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 570 | else |
| 571 | return cleared_bitset; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 572 | } |
| 573 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 574 | bool ParsedIR::has_decoration(ID id, Decoration decoration) const |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 575 | { |
| 576 | return get_decoration_bitset(id).get(decoration); |
| 577 | } |
| 578 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 579 | uint32_t ParsedIR::get_decoration(ID id, Decoration decoration) const |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 580 | { |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 581 | auto *m = find_meta(id); |
| 582 | if (!m) |
| 583 | return 0; |
| 584 | |
| 585 | auto &dec = m->decoration; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 586 | if (!dec.decoration_flags.get(decoration)) |
| 587 | return 0; |
| 588 | |
| 589 | switch (decoration) |
| 590 | { |
| 591 | case DecorationBuiltIn: |
| 592 | return dec.builtin_type; |
| 593 | case DecorationLocation: |
| 594 | return dec.location; |
| 595 | case DecorationComponent: |
| 596 | return dec.component; |
| 597 | case DecorationOffset: |
| 598 | return dec.offset; |
Hans-Kristian Arntzen | 655312c | 2020-01-27 12:56:48 +0100 | [diff] [blame] | 599 | case DecorationXfbBuffer: |
| 600 | return dec.xfb_buffer; |
| 601 | case DecorationXfbStride: |
| 602 | return dec.xfb_stride; |
Hans-Kristian Arntzen | e0c9aad | 2020-09-30 13:01:35 +0200 | [diff] [blame] | 603 | case DecorationStream: |
| 604 | return dec.stream; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 605 | case DecorationBinding: |
| 606 | return dec.binding; |
| 607 | case DecorationDescriptorSet: |
| 608 | return dec.set; |
| 609 | case DecorationInputAttachmentIndex: |
| 610 | return dec.input_attachment; |
| 611 | case DecorationSpecId: |
| 612 | return dec.spec_id; |
| 613 | case DecorationArrayStride: |
| 614 | return dec.array_stride; |
| 615 | case DecorationMatrixStride: |
| 616 | return dec.matrix_stride; |
| 617 | case DecorationIndex: |
| 618 | return dec.index; |
Hans-Kristian Arntzen | 6e5df7a | 2019-01-07 10:51:44 +0100 | [diff] [blame] | 619 | case DecorationFPRoundingMode: |
| 620 | return dec.fp_rounding_mode; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 621 | default: |
| 622 | return 1; |
| 623 | } |
| 624 | } |
| 625 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 626 | const string &ParsedIR::get_decoration_string(ID id, Decoration decoration) const |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 627 | { |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 628 | auto *m = find_meta(id); |
| 629 | if (!m) |
| 630 | return empty_string; |
| 631 | |
| 632 | auto &dec = m->decoration; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 633 | |
| 634 | if (!dec.decoration_flags.get(decoration)) |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 635 | return empty_string; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 636 | |
| 637 | switch (decoration) |
| 638 | { |
| 639 | case DecorationHlslSemanticGOOGLE: |
| 640 | return dec.hlsl_semantic; |
| 641 | |
| 642 | default: |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 643 | return empty_string; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 647 | void ParsedIR::unset_decoration(ID id, Decoration decoration) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 648 | { |
| 649 | auto &dec = meta[id].decoration; |
| 650 | dec.decoration_flags.clear(decoration); |
| 651 | switch (decoration) |
| 652 | { |
| 653 | case DecorationBuiltIn: |
| 654 | dec.builtin = false; |
| 655 | break; |
| 656 | |
| 657 | case DecorationLocation: |
| 658 | dec.location = 0; |
| 659 | break; |
| 660 | |
| 661 | case DecorationComponent: |
| 662 | dec.component = 0; |
| 663 | break; |
| 664 | |
| 665 | case DecorationOffset: |
| 666 | dec.offset = 0; |
| 667 | break; |
| 668 | |
Hans-Kristian Arntzen | 655312c | 2020-01-27 12:56:48 +0100 | [diff] [blame] | 669 | case DecorationXfbBuffer: |
| 670 | dec.xfb_buffer = 0; |
| 671 | break; |
| 672 | |
| 673 | case DecorationXfbStride: |
| 674 | dec.xfb_stride = 0; |
| 675 | break; |
| 676 | |
Hans-Kristian Arntzen | e0c9aad | 2020-09-30 13:01:35 +0200 | [diff] [blame] | 677 | case DecorationStream: |
| 678 | dec.stream = 0; |
| 679 | break; |
| 680 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 681 | case DecorationBinding: |
| 682 | dec.binding = 0; |
| 683 | break; |
| 684 | |
| 685 | case DecorationDescriptorSet: |
| 686 | dec.set = 0; |
| 687 | break; |
| 688 | |
| 689 | case DecorationInputAttachmentIndex: |
| 690 | dec.input_attachment = 0; |
| 691 | break; |
| 692 | |
| 693 | case DecorationSpecId: |
| 694 | dec.spec_id = 0; |
| 695 | break; |
| 696 | |
| 697 | case DecorationHlslSemanticGOOGLE: |
| 698 | dec.hlsl_semantic.clear(); |
| 699 | break; |
| 700 | |
Hans-Kristian Arntzen | 6e5df7a | 2019-01-07 10:51:44 +0100 | [diff] [blame] | 701 | case DecorationFPRoundingMode: |
| 702 | dec.fp_rounding_mode = FPRoundingModeMax; |
| 703 | break; |
| 704 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 705 | case DecorationHlslCounterBufferGOOGLE: |
| 706 | { |
| 707 | auto &counter = meta[id].hlsl_magic_counter_buffer; |
| 708 | if (counter) |
| 709 | { |
| 710 | meta[counter].hlsl_is_magic_counter_buffer = false; |
| 711 | counter = 0; |
| 712 | } |
| 713 | break; |
| 714 | } |
| 715 | |
| 716 | default: |
| 717 | break; |
| 718 | } |
| 719 | } |
| 720 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 721 | bool ParsedIR::has_member_decoration(TypeID id, uint32_t index, Decoration decoration) const |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 722 | { |
| 723 | return get_member_decoration_bitset(id, index).get(decoration); |
| 724 | } |
| 725 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 726 | uint32_t ParsedIR::get_member_decoration(TypeID id, uint32_t index, Decoration decoration) const |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 727 | { |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 728 | auto *m = find_meta(id); |
| 729 | if (!m) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 730 | return 0; |
| 731 | |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 732 | if (index >= m->members.size()) |
| 733 | return 0; |
| 734 | |
| 735 | auto &dec = m->members[index]; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 736 | if (!dec.decoration_flags.get(decoration)) |
| 737 | return 0; |
| 738 | |
| 739 | switch (decoration) |
| 740 | { |
| 741 | case DecorationBuiltIn: |
| 742 | return dec.builtin_type; |
| 743 | case DecorationLocation: |
| 744 | return dec.location; |
| 745 | case DecorationComponent: |
| 746 | return dec.component; |
| 747 | case DecorationBinding: |
| 748 | return dec.binding; |
| 749 | case DecorationOffset: |
| 750 | return dec.offset; |
Hans-Kristian Arntzen | 655312c | 2020-01-27 12:56:48 +0100 | [diff] [blame] | 751 | case DecorationXfbBuffer: |
| 752 | return dec.xfb_buffer; |
| 753 | case DecorationXfbStride: |
| 754 | return dec.xfb_stride; |
Hans-Kristian Arntzen | e0c9aad | 2020-09-30 13:01:35 +0200 | [diff] [blame] | 755 | case DecorationStream: |
| 756 | return dec.stream; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 757 | case DecorationSpecId: |
| 758 | return dec.spec_id; |
| 759 | case DecorationIndex: |
| 760 | return dec.index; |
| 761 | default: |
| 762 | return 1; |
| 763 | } |
| 764 | } |
| 765 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 766 | const Bitset &ParsedIR::get_decoration_bitset(ID id) const |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 767 | { |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 768 | auto *m = find_meta(id); |
| 769 | if (m) |
| 770 | { |
| 771 | auto &dec = m->decoration; |
| 772 | return dec.decoration_flags; |
| 773 | } |
| 774 | else |
| 775 | return cleared_bitset; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 776 | } |
| 777 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 778 | void ParsedIR::set_member_decoration_string(TypeID id, uint32_t index, Decoration decoration, const string &argument) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 779 | { |
| 780 | meta[id].members.resize(max(meta[id].members.size(), size_t(index) + 1)); |
| 781 | auto &dec = meta[id].members[index]; |
| 782 | dec.decoration_flags.set(decoration); |
| 783 | |
| 784 | switch (decoration) |
| 785 | { |
| 786 | case DecorationHlslSemanticGOOGLE: |
| 787 | dec.hlsl_semantic = argument; |
| 788 | break; |
| 789 | |
| 790 | default: |
| 791 | break; |
| 792 | } |
| 793 | } |
| 794 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 795 | const string &ParsedIR::get_member_decoration_string(TypeID id, uint32_t index, Decoration decoration) const |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 796 | { |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 797 | auto *m = find_meta(id); |
| 798 | if (m) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 799 | { |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 800 | if (!has_member_decoration(id, index, decoration)) |
| 801 | return empty_string; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 802 | |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 803 | auto &dec = m->members[index]; |
| 804 | |
| 805 | switch (decoration) |
| 806 | { |
| 807 | case DecorationHlslSemanticGOOGLE: |
| 808 | return dec.hlsl_semantic; |
| 809 | |
| 810 | default: |
| 811 | return empty_string; |
| 812 | } |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 813 | } |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 814 | else |
| 815 | return empty_string; |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 816 | } |
| 817 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 818 | void ParsedIR::unset_member_decoration(TypeID id, uint32_t index, Decoration decoration) |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 819 | { |
| 820 | auto &m = meta[id]; |
| 821 | if (index >= m.members.size()) |
| 822 | return; |
| 823 | |
| 824 | auto &dec = m.members[index]; |
| 825 | |
| 826 | dec.decoration_flags.clear(decoration); |
| 827 | switch (decoration) |
| 828 | { |
| 829 | case DecorationBuiltIn: |
| 830 | dec.builtin = false; |
| 831 | break; |
| 832 | |
| 833 | case DecorationLocation: |
| 834 | dec.location = 0; |
| 835 | break; |
| 836 | |
| 837 | case DecorationComponent: |
| 838 | dec.component = 0; |
| 839 | break; |
| 840 | |
| 841 | case DecorationOffset: |
| 842 | dec.offset = 0; |
| 843 | break; |
| 844 | |
Hans-Kristian Arntzen | 655312c | 2020-01-27 12:56:48 +0100 | [diff] [blame] | 845 | case DecorationXfbBuffer: |
| 846 | dec.xfb_buffer = 0; |
| 847 | break; |
| 848 | |
| 849 | case DecorationXfbStride: |
| 850 | dec.xfb_stride = 0; |
| 851 | break; |
| 852 | |
Hans-Kristian Arntzen | e0c9aad | 2020-09-30 13:01:35 +0200 | [diff] [blame] | 853 | case DecorationStream: |
| 854 | dec.stream = 0; |
| 855 | break; |
| 856 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 857 | case DecorationSpecId: |
| 858 | dec.spec_id = 0; |
| 859 | break; |
| 860 | |
| 861 | case DecorationHlslSemanticGOOGLE: |
| 862 | dec.hlsl_semantic.clear(); |
| 863 | break; |
| 864 | |
| 865 | default: |
| 866 | break; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | uint32_t ParsedIR::increase_bound_by(uint32_t incr_amount) |
| 871 | { |
| 872 | auto curr_bound = ids.size(); |
| 873 | auto new_bound = curr_bound + incr_amount; |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 874 | |
| 875 | ids.reserve(ids.size() + incr_amount); |
| 876 | for (uint32_t i = 0; i < incr_amount; i++) |
| 877 | ids.emplace_back(pool_group.get()); |
| 878 | |
Hans-Kristian Arntzen | 5bcf02f | 2018-10-05 11:30:57 +0200 | [diff] [blame] | 879 | block_meta.resize(new_bound); |
| 880 | return uint32_t(curr_bound); |
| 881 | } |
| 882 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 883 | void ParsedIR::remove_typed_id(Types type, ID id) |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 884 | { |
| 885 | auto &type_ids = ids_for_type[type]; |
| 886 | type_ids.erase(remove(begin(type_ids), end(type_ids), id), end(type_ids)); |
| 887 | } |
| 888 | |
| 889 | void ParsedIR::reset_all_of_type(Types type) |
| 890 | { |
| 891 | for (auto &id : ids_for_type[type]) |
| 892 | if (ids[id].get_type() == type) |
| 893 | ids[id].reset(); |
| 894 | |
| 895 | ids_for_type[type].clear(); |
| 896 | } |
| 897 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 898 | void ParsedIR::add_typed_id(Types type, ID id) |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 899 | { |
Hans-Kristian Arntzen | dd7ebaf | 2019-07-18 17:05:28 +0200 | [diff] [blame] | 900 | if (loop_iteration_depth_hard != 0) |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 901 | SPIRV_CROSS_THROW("Cannot add typed ID while looping over it."); |
| 902 | |
Hans-Kristian Arntzen | dd7ebaf | 2019-07-18 17:05:28 +0200 | [diff] [blame] | 903 | if (loop_iteration_depth_soft != 0) |
| 904 | { |
| 905 | if (!ids[id].empty()) |
| 906 | SPIRV_CROSS_THROW("Cannot override IDs when loop is soft locked."); |
| 907 | return; |
| 908 | } |
| 909 | |
Hans-Kristian Arntzen | 2cc374a | 2019-04-24 14:12:50 +0200 | [diff] [blame] | 910 | if (ids[id].empty() || ids[id].get_type() != type) |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 911 | { |
Hans-Kristian Arntzen | 2cc374a | 2019-04-24 14:12:50 +0200 | [diff] [blame] | 912 | switch (type) |
| 913 | { |
| 914 | case TypeConstant: |
| 915 | ids_for_constant_or_variable.push_back(id); |
| 916 | ids_for_constant_or_type.push_back(id); |
| 917 | break; |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 918 | |
Hans-Kristian Arntzen | 2cc374a | 2019-04-24 14:12:50 +0200 | [diff] [blame] | 919 | case TypeVariable: |
| 920 | ids_for_constant_or_variable.push_back(id); |
| 921 | break; |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 922 | |
Hans-Kristian Arntzen | 2cc374a | 2019-04-24 14:12:50 +0200 | [diff] [blame] | 923 | case TypeType: |
| 924 | case TypeConstantOp: |
| 925 | ids_for_constant_or_type.push_back(id); |
| 926 | break; |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 927 | |
Hans-Kristian Arntzen | 2cc374a | 2019-04-24 14:12:50 +0200 | [diff] [blame] | 928 | default: |
| 929 | break; |
| 930 | } |
Hans-Kristian Arntzen | d92de00 | 2019-01-10 09:49:33 +0100 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | if (ids[id].empty()) |
| 934 | { |
| 935 | ids_for_type[type].push_back(id); |
| 936 | } |
| 937 | else if (ids[id].get_type() != type) |
| 938 | { |
| 939 | remove_typed_id(ids[id].get_type(), id); |
| 940 | ids_for_type[type].push_back(id); |
| 941 | } |
| 942 | } |
| 943 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 944 | const Meta *ParsedIR::find_meta(ID id) const |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 945 | { |
| 946 | auto itr = meta.find(id); |
| 947 | if (itr != end(meta)) |
| 948 | return &itr->second; |
| 949 | else |
| 950 | return nullptr; |
| 951 | } |
| 952 | |
Hans-Kristian Arntzen | 333980a | 2019-09-05 12:43:40 +0200 | [diff] [blame] | 953 | Meta *ParsedIR::find_meta(ID id) |
Hans-Kristian Arntzen | b629878 | 2019-01-10 14:04:01 +0100 | [diff] [blame] | 954 | { |
| 955 | auto itr = meta.find(id); |
| 956 | if (itr != end(meta)) |
| 957 | return &itr->second; |
| 958 | else |
| 959 | return nullptr; |
| 960 | } |
| 961 | |
Hans-Kristian Arntzen | dd7ebaf | 2019-07-18 17:05:28 +0200 | [diff] [blame] | 962 | ParsedIR::LoopLock ParsedIR::create_loop_hard_lock() const |
Hans-Kristian Arntzen | a86308b | 2019-07-18 13:34:47 +0200 | [diff] [blame] | 963 | { |
Hans-Kristian Arntzen | dd7ebaf | 2019-07-18 17:05:28 +0200 | [diff] [blame] | 964 | return ParsedIR::LoopLock(&loop_iteration_depth_hard); |
| 965 | } |
| 966 | |
| 967 | ParsedIR::LoopLock ParsedIR::create_loop_soft_lock() const |
| 968 | { |
| 969 | return ParsedIR::LoopLock(&loop_iteration_depth_soft); |
Hans-Kristian Arntzen | a86308b | 2019-07-18 13:34:47 +0200 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | ParsedIR::LoopLock::~LoopLock() |
| 973 | { |
| 974 | if (lock) |
| 975 | (*lock)--; |
| 976 | } |
| 977 | |
| 978 | ParsedIR::LoopLock::LoopLock(uint32_t *lock_) |
Hans-Kristian Arntzen | 3fa2b14 | 2019-07-23 12:23:41 +0200 | [diff] [blame] | 979 | : lock(lock_) |
Hans-Kristian Arntzen | a86308b | 2019-07-18 13:34:47 +0200 | [diff] [blame] | 980 | { |
| 981 | if (lock) |
| 982 | (*lock)++; |
| 983 | } |
| 984 | |
| 985 | ParsedIR::LoopLock::LoopLock(LoopLock &&other) SPIRV_CROSS_NOEXCEPT |
| 986 | { |
| 987 | *this = move(other); |
| 988 | } |
| 989 | |
| 990 | ParsedIR::LoopLock &ParsedIR::LoopLock::operator=(LoopLock &&other) SPIRV_CROSS_NOEXCEPT |
| 991 | { |
| 992 | if (lock) |
| 993 | (*lock)--; |
| 994 | lock = other.lock; |
| 995 | other.lock = nullptr; |
| 996 | return *this; |
| 997 | } |
| 998 | |
Hans-Kristian Arntzen | b8905bb | 2020-03-26 11:21:23 +0100 | [diff] [blame] | 999 | void ParsedIR::make_constant_null(uint32_t id, uint32_t type, bool add_to_typed_id_set) |
| 1000 | { |
| 1001 | auto &constant_type = get<SPIRType>(type); |
| 1002 | |
| 1003 | if (constant_type.pointer) |
| 1004 | { |
| 1005 | if (add_to_typed_id_set) |
| 1006 | add_typed_id(TypeConstant, id); |
| 1007 | auto &constant = variant_set<SPIRConstant>(ids[id], type); |
| 1008 | constant.self = id; |
| 1009 | constant.make_null(constant_type); |
| 1010 | } |
| 1011 | else if (!constant_type.array.empty()) |
| 1012 | { |
| 1013 | assert(constant_type.parent_type); |
| 1014 | uint32_t parent_id = increase_bound_by(1); |
| 1015 | make_constant_null(parent_id, constant_type.parent_type, add_to_typed_id_set); |
| 1016 | |
| 1017 | if (!constant_type.array_size_literal.back()) |
| 1018 | SPIRV_CROSS_THROW("Array size of OpConstantNull must be a literal."); |
| 1019 | |
| 1020 | SmallVector<uint32_t> elements(constant_type.array.back()); |
| 1021 | for (uint32_t i = 0; i < constant_type.array.back(); i++) |
| 1022 | elements[i] = parent_id; |
| 1023 | |
| 1024 | if (add_to_typed_id_set) |
| 1025 | add_typed_id(TypeConstant, id); |
| 1026 | variant_set<SPIRConstant>(ids[id], type, elements.data(), uint32_t(elements.size()), false).self = id; |
| 1027 | } |
| 1028 | else if (!constant_type.member_types.empty()) |
| 1029 | { |
| 1030 | uint32_t member_ids = increase_bound_by(uint32_t(constant_type.member_types.size())); |
| 1031 | SmallVector<uint32_t> elements(constant_type.member_types.size()); |
| 1032 | for (uint32_t i = 0; i < constant_type.member_types.size(); i++) |
| 1033 | { |
| 1034 | make_constant_null(member_ids + i, constant_type.member_types[i], add_to_typed_id_set); |
| 1035 | elements[i] = member_ids + i; |
| 1036 | } |
| 1037 | |
| 1038 | if (add_to_typed_id_set) |
| 1039 | add_typed_id(TypeConstant, id); |
| 1040 | variant_set<SPIRConstant>(ids[id], type, elements.data(), uint32_t(elements.size()), false).self = id; |
| 1041 | } |
| 1042 | else |
| 1043 | { |
| 1044 | if (add_to_typed_id_set) |
| 1045 | add_typed_id(TypeConstant, id); |
| 1046 | auto &constant = variant_set<SPIRConstant>(ids[id], type); |
| 1047 | constant.self = id; |
| 1048 | constant.make_null(constant_type); |
| 1049 | } |
| 1050 | } |
| 1051 | |
Hans-Kristian Arntzen | a489ba7 | 2019-04-02 11:19:03 +0200 | [diff] [blame] | 1052 | } // namespace SPIRV_CROSS_NAMESPACE |