spirv_common.hpp 47 KB

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