emit_context.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <climits>
  7. #include <string_view>
  8. #include <queue>
  9. #include <fmt/format.h>
  10. #include "common/common_types.h"
  11. #include "common/div_ceil.h"
  12. #include "shader_recompiler/backend/spirv/emit_context.h"
  13. namespace Shader::Backend::SPIRV {
  14. namespace {
  15. enum class Operation {
  16. Increment,
  17. Decrement,
  18. FPAdd,
  19. FPMin,
  20. FPMax,
  21. };
  22. struct AttrInfo {
  23. Id pointer;
  24. Id id;
  25. bool needs_cast;
  26. };
  27. Id ImageType(EmitContext& ctx, const TextureDescriptor& desc) {
  28. const spv::ImageFormat format{spv::ImageFormat::Unknown};
  29. const Id type{ctx.F32[1]};
  30. const bool depth{desc.is_depth};
  31. switch (desc.type) {
  32. case TextureType::Color1D:
  33. return ctx.TypeImage(type, spv::Dim::Dim1D, depth, false, false, 1, format);
  34. case TextureType::ColorArray1D:
  35. return ctx.TypeImage(type, spv::Dim::Dim1D, depth, true, false, 1, format);
  36. case TextureType::Color2D:
  37. return ctx.TypeImage(type, spv::Dim::Dim2D, depth, false, false, 1, format);
  38. case TextureType::ColorArray2D:
  39. return ctx.TypeImage(type, spv::Dim::Dim2D, depth, true, false, 1, format);
  40. case TextureType::Color3D:
  41. return ctx.TypeImage(type, spv::Dim::Dim3D, depth, false, false, 1, format);
  42. case TextureType::ColorCube:
  43. return ctx.TypeImage(type, spv::Dim::Cube, depth, false, false, 1, format);
  44. case TextureType::ColorArrayCube:
  45. return ctx.TypeImage(type, spv::Dim::Cube, depth, true, false, 1, format);
  46. case TextureType::Buffer:
  47. break;
  48. }
  49. throw InvalidArgument("Invalid texture type {}", desc.type);
  50. }
  51. spv::ImageFormat GetImageFormat(ImageFormat format) {
  52. switch (format) {
  53. case ImageFormat::Typeless:
  54. return spv::ImageFormat::Unknown;
  55. case ImageFormat::R8_UINT:
  56. return spv::ImageFormat::R8ui;
  57. case ImageFormat::R8_SINT:
  58. return spv::ImageFormat::R8i;
  59. case ImageFormat::R16_UINT:
  60. return spv::ImageFormat::R16ui;
  61. case ImageFormat::R16_SINT:
  62. return spv::ImageFormat::R16i;
  63. case ImageFormat::R32_UINT:
  64. return spv::ImageFormat::R32ui;
  65. case ImageFormat::R32G32_UINT:
  66. return spv::ImageFormat::Rg32ui;
  67. case ImageFormat::R32G32B32A32_UINT:
  68. return spv::ImageFormat::Rgba32ui;
  69. }
  70. throw InvalidArgument("Invalid image format {}", format);
  71. }
  72. Id ImageType(EmitContext& ctx, const ImageDescriptor& desc) {
  73. const spv::ImageFormat format{GetImageFormat(desc.format)};
  74. const Id type{ctx.U32[1]};
  75. switch (desc.type) {
  76. case TextureType::Color1D:
  77. return ctx.TypeImage(type, spv::Dim::Dim1D, false, false, false, 2, format);
  78. case TextureType::ColorArray1D:
  79. return ctx.TypeImage(type, spv::Dim::Dim1D, false, true, false, 2, format);
  80. case TextureType::Color2D:
  81. return ctx.TypeImage(type, spv::Dim::Dim2D, false, false, false, 2, format);
  82. case TextureType::ColorArray2D:
  83. return ctx.TypeImage(type, spv::Dim::Dim2D, false, true, false, 2, format);
  84. case TextureType::Color3D:
  85. return ctx.TypeImage(type, spv::Dim::Dim3D, false, false, false, 2, format);
  86. case TextureType::Buffer:
  87. throw NotImplementedException("Image buffer");
  88. default:
  89. break;
  90. }
  91. throw InvalidArgument("Invalid texture type {}", desc.type);
  92. }
  93. Id DefineVariable(EmitContext& ctx, Id type, std::optional<spv::BuiltIn> builtin,
  94. spv::StorageClass storage_class) {
  95. const Id pointer_type{ctx.TypePointer(storage_class, type)};
  96. const Id id{ctx.AddGlobalVariable(pointer_type, storage_class)};
  97. if (builtin) {
  98. ctx.Decorate(id, spv::Decoration::BuiltIn, *builtin);
  99. }
  100. ctx.interfaces.push_back(id);
  101. return id;
  102. }
  103. u32 NumVertices(InputTopology input_topology) {
  104. switch (input_topology) {
  105. case InputTopology::Points:
  106. return 1;
  107. case InputTopology::Lines:
  108. return 2;
  109. case InputTopology::LinesAdjacency:
  110. return 4;
  111. case InputTopology::Triangles:
  112. return 3;
  113. case InputTopology::TrianglesAdjacency:
  114. return 6;
  115. }
  116. throw InvalidArgument("Invalid input topology {}", input_topology);
  117. }
  118. Id DefineInput(EmitContext& ctx, Id type, bool per_invocation,
  119. std::optional<spv::BuiltIn> builtin = std::nullopt) {
  120. switch (ctx.stage) {
  121. case Stage::TessellationControl:
  122. case Stage::TessellationEval:
  123. if (per_invocation) {
  124. type = ctx.TypeArray(type, ctx.Const(32u));
  125. }
  126. break;
  127. case Stage::Geometry:
  128. if (per_invocation) {
  129. const u32 num_vertices{NumVertices(ctx.runtime_info.input_topology)};
  130. type = ctx.TypeArray(type, ctx.Const(num_vertices));
  131. }
  132. break;
  133. default:
  134. break;
  135. }
  136. return DefineVariable(ctx, type, builtin, spv::StorageClass::Input);
  137. }
  138. Id DefineOutput(EmitContext& ctx, Id type, std::optional<u32> invocations,
  139. std::optional<spv::BuiltIn> builtin = std::nullopt) {
  140. if (invocations && ctx.stage == Stage::TessellationControl) {
  141. type = ctx.TypeArray(type, ctx.Const(*invocations));
  142. }
  143. return DefineVariable(ctx, type, builtin, spv::StorageClass::Output);
  144. }
  145. void DefineGenericOutput(EmitContext& ctx, size_t index, std::optional<u32> invocations) {
  146. static constexpr std::string_view swizzle{"xyzw"};
  147. const size_t base_attr_index{static_cast<size_t>(IR::Attribute::Generic0X) + index * 4};
  148. u32 element{0};
  149. while (element < 4) {
  150. const u32 remainder{4 - element};
  151. const TransformFeedbackVarying* xfb_varying{};
  152. if (!ctx.runtime_info.xfb_varyings.empty()) {
  153. xfb_varying = &ctx.runtime_info.xfb_varyings[base_attr_index + element];
  154. xfb_varying = xfb_varying && xfb_varying->components > 0 ? xfb_varying : nullptr;
  155. }
  156. const u32 num_components{xfb_varying ? xfb_varying->components : remainder};
  157. const Id id{DefineOutput(ctx, ctx.F32[num_components], invocations)};
  158. ctx.Decorate(id, spv::Decoration::Location, static_cast<u32>(index));
  159. if (element > 0) {
  160. ctx.Decorate(id, spv::Decoration::Component, element);
  161. }
  162. if (xfb_varying) {
  163. ctx.Decorate(id, spv::Decoration::XfbBuffer, xfb_varying->buffer);
  164. ctx.Decorate(id, spv::Decoration::XfbStride, xfb_varying->stride);
  165. ctx.Decorate(id, spv::Decoration::Offset, xfb_varying->offset);
  166. }
  167. if (num_components < 4 || element > 0) {
  168. const std::string_view subswizzle{swizzle.substr(element, num_components)};
  169. ctx.Name(id, fmt::format("out_attr{}_{}", index, subswizzle));
  170. } else {
  171. ctx.Name(id, fmt::format("out_attr{}", index));
  172. }
  173. const GenericElementInfo info{
  174. .id = id,
  175. .first_element = element,
  176. .num_components = num_components,
  177. };
  178. std::fill_n(ctx.output_generics[index].begin() + element, num_components, info);
  179. element += num_components;
  180. }
  181. }
  182. Id GetAttributeType(EmitContext& ctx, AttributeType type) {
  183. switch (type) {
  184. case AttributeType::Float:
  185. return ctx.F32[4];
  186. case AttributeType::SignedInt:
  187. return ctx.TypeVector(ctx.TypeInt(32, true), 4);
  188. case AttributeType::UnsignedInt:
  189. return ctx.U32[4];
  190. case AttributeType::Disabled:
  191. break;
  192. }
  193. throw InvalidArgument("Invalid attribute type {}", type);
  194. }
  195. std::optional<AttrInfo> AttrTypes(EmitContext& ctx, u32 index) {
  196. const AttributeType type{ctx.runtime_info.generic_input_types.at(index)};
  197. switch (type) {
  198. case AttributeType::Float:
  199. return AttrInfo{ctx.input_f32, ctx.F32[1], false};
  200. case AttributeType::UnsignedInt:
  201. return AttrInfo{ctx.input_u32, ctx.U32[1], true};
  202. case AttributeType::SignedInt:
  203. return AttrInfo{ctx.input_s32, ctx.TypeInt(32, true), true};
  204. case AttributeType::Disabled:
  205. return std::nullopt;
  206. }
  207. throw InvalidArgument("Invalid attribute type {}", type);
  208. }
  209. std::string_view StageName(Stage stage) {
  210. switch (stage) {
  211. case Stage::VertexA:
  212. return "vs_a";
  213. case Stage::VertexB:
  214. return "vs";
  215. case Stage::TessellationControl:
  216. return "tcs";
  217. case Stage::TessellationEval:
  218. return "tes";
  219. case Stage::Geometry:
  220. return "gs";
  221. case Stage::Fragment:
  222. return "fs";
  223. case Stage::Compute:
  224. return "cs";
  225. }
  226. throw InvalidArgument("Invalid stage {}", stage);
  227. }
  228. template <typename... Args>
  229. void Name(EmitContext& ctx, Id object, std::string_view format_str, Args&&... args) {
  230. ctx.Name(object, fmt::format(fmt::runtime(format_str), StageName(ctx.stage),
  231. std::forward<Args>(args)...)
  232. .c_str());
  233. }
  234. void DefineConstBuffers(EmitContext& ctx, const Info& info, Id UniformDefinitions::*member_type,
  235. u32 binding, Id type, char type_char, u32 element_size) {
  236. const Id array_type{ctx.TypeArray(type, ctx.Const(65536U / element_size))};
  237. ctx.Decorate(array_type, spv::Decoration::ArrayStride, element_size);
  238. const Id struct_type{ctx.TypeStruct(array_type)};
  239. Name(ctx, struct_type, "{}_cbuf_block_{}{}", ctx.stage, type_char, element_size * CHAR_BIT);
  240. ctx.Decorate(struct_type, spv::Decoration::Block);
  241. ctx.MemberName(struct_type, 0, "data");
  242. ctx.MemberDecorate(struct_type, 0, spv::Decoration::Offset, 0U);
  243. const Id struct_pointer_type{ctx.TypePointer(spv::StorageClass::Uniform, struct_type)};
  244. const Id uniform_type{ctx.TypePointer(spv::StorageClass::Uniform, type)};
  245. ctx.uniform_types.*member_type = uniform_type;
  246. for (const ConstantBufferDescriptor& desc : info.constant_buffer_descriptors) {
  247. const Id id{ctx.AddGlobalVariable(struct_pointer_type, spv::StorageClass::Uniform)};
  248. ctx.Decorate(id, spv::Decoration::Binding, binding);
  249. ctx.Decorate(id, spv::Decoration::DescriptorSet, 0U);
  250. ctx.Name(id, fmt::format("c{}", desc.index));
  251. for (size_t i = 0; i < desc.count; ++i) {
  252. ctx.cbufs[desc.index + i].*member_type = id;
  253. }
  254. if (ctx.profile.supported_spirv >= 0x00010400) {
  255. ctx.interfaces.push_back(id);
  256. }
  257. binding += desc.count;
  258. }
  259. }
  260. void DefineSsbos(EmitContext& ctx, StorageTypeDefinition& type_def,
  261. Id StorageDefinitions::*member_type, const Info& info, u32 binding, Id type,
  262. u32 stride) {
  263. const Id array_type{ctx.TypeRuntimeArray(type)};
  264. ctx.Decorate(array_type, spv::Decoration::ArrayStride, stride);
  265. const Id struct_type{ctx.TypeStruct(array_type)};
  266. ctx.Decorate(struct_type, spv::Decoration::Block);
  267. ctx.MemberDecorate(struct_type, 0, spv::Decoration::Offset, 0U);
  268. const Id struct_pointer{ctx.TypePointer(spv::StorageClass::StorageBuffer, struct_type)};
  269. type_def.array = struct_pointer;
  270. type_def.element = ctx.TypePointer(spv::StorageClass::StorageBuffer, type);
  271. u32 index{};
  272. for (const StorageBufferDescriptor& desc : info.storage_buffers_descriptors) {
  273. const Id id{ctx.AddGlobalVariable(struct_pointer, spv::StorageClass::StorageBuffer)};
  274. ctx.Decorate(id, spv::Decoration::Binding, binding);
  275. ctx.Decorate(id, spv::Decoration::DescriptorSet, 0U);
  276. ctx.Name(id, fmt::format("ssbo{}", index));
  277. if (ctx.profile.supported_spirv >= 0x00010400) {
  278. ctx.interfaces.push_back(id);
  279. }
  280. for (size_t i = 0; i < desc.count; ++i) {
  281. ctx.ssbos[index + i].*member_type = id;
  282. }
  283. index += desc.count;
  284. binding += desc.count;
  285. }
  286. }
  287. Id CasFunction(EmitContext& ctx, Operation operation, Id value_type) {
  288. const Id func_type{ctx.TypeFunction(value_type, value_type, value_type)};
  289. const Id func{ctx.OpFunction(value_type, spv::FunctionControlMask::MaskNone, func_type)};
  290. const Id op_a{ctx.OpFunctionParameter(value_type)};
  291. const Id op_b{ctx.OpFunctionParameter(value_type)};
  292. ctx.AddLabel();
  293. Id result{};
  294. switch (operation) {
  295. case Operation::Increment: {
  296. const Id pred{ctx.OpUGreaterThanEqual(ctx.U1, op_a, op_b)};
  297. const Id incr{ctx.OpIAdd(value_type, op_a, ctx.Constant(value_type, 1))};
  298. result = ctx.OpSelect(value_type, pred, ctx.u32_zero_value, incr);
  299. break;
  300. }
  301. case Operation::Decrement: {
  302. const Id lhs{ctx.OpIEqual(ctx.U1, op_a, ctx.Constant(value_type, 0u))};
  303. const Id rhs{ctx.OpUGreaterThan(ctx.U1, op_a, op_b)};
  304. const Id pred{ctx.OpLogicalOr(ctx.U1, lhs, rhs)};
  305. const Id decr{ctx.OpISub(value_type, op_a, ctx.Constant(value_type, 1))};
  306. result = ctx.OpSelect(value_type, pred, op_b, decr);
  307. break;
  308. }
  309. case Operation::FPAdd:
  310. result = ctx.OpFAdd(value_type, op_a, op_b);
  311. break;
  312. case Operation::FPMin:
  313. result = ctx.OpFMin(value_type, op_a, op_b);
  314. break;
  315. case Operation::FPMax:
  316. result = ctx.OpFMax(value_type, op_a, op_b);
  317. break;
  318. default:
  319. break;
  320. }
  321. ctx.OpReturnValue(result);
  322. ctx.OpFunctionEnd();
  323. return func;
  324. }
  325. Id CasLoop(EmitContext& ctx, Operation operation, Id array_pointer, Id element_pointer,
  326. Id value_type, Id memory_type, spv::Scope scope) {
  327. const bool is_shared{scope == spv::Scope::Workgroup};
  328. const bool is_struct{!is_shared || ctx.profile.support_explicit_workgroup_layout};
  329. const Id cas_func{CasFunction(ctx, operation, value_type)};
  330. const Id zero{ctx.u32_zero_value};
  331. const Id scope_id{ctx.Const(static_cast<u32>(scope))};
  332. const Id loop_header{ctx.OpLabel()};
  333. const Id continue_block{ctx.OpLabel()};
  334. const Id merge_block{ctx.OpLabel()};
  335. const Id func_type{is_shared
  336. ? ctx.TypeFunction(value_type, ctx.U32[1], value_type)
  337. : ctx.TypeFunction(value_type, ctx.U32[1], value_type, array_pointer)};
  338. const Id func{ctx.OpFunction(value_type, spv::FunctionControlMask::MaskNone, func_type)};
  339. const Id index{ctx.OpFunctionParameter(ctx.U32[1])};
  340. const Id op_b{ctx.OpFunctionParameter(value_type)};
  341. const Id base{is_shared ? ctx.shared_memory_u32 : ctx.OpFunctionParameter(array_pointer)};
  342. ctx.AddLabel();
  343. ctx.OpBranch(loop_header);
  344. ctx.AddLabel(loop_header);
  345. ctx.OpLoopMerge(merge_block, continue_block, spv::LoopControlMask::MaskNone);
  346. ctx.OpBranch(continue_block);
  347. ctx.AddLabel(continue_block);
  348. const Id word_pointer{is_struct ? ctx.OpAccessChain(element_pointer, base, zero, index)
  349. : ctx.OpAccessChain(element_pointer, base, index)};
  350. if (value_type.value == ctx.F32[2].value) {
  351. const Id u32_value{ctx.OpLoad(ctx.U32[1], word_pointer)};
  352. const Id value{ctx.OpUnpackHalf2x16(ctx.F32[2], u32_value)};
  353. const Id new_value{ctx.OpFunctionCall(value_type, cas_func, value, op_b)};
  354. const Id u32_new_value{ctx.OpPackHalf2x16(ctx.U32[1], new_value)};
  355. const Id atomic_res{ctx.OpAtomicCompareExchange(ctx.U32[1], word_pointer, scope_id, zero,
  356. zero, u32_new_value, u32_value)};
  357. const Id success{ctx.OpIEqual(ctx.U1, atomic_res, u32_value)};
  358. ctx.OpBranchConditional(success, merge_block, loop_header);
  359. ctx.AddLabel(merge_block);
  360. ctx.OpReturnValue(ctx.OpUnpackHalf2x16(ctx.F32[2], atomic_res));
  361. } else {
  362. const Id value{ctx.OpLoad(memory_type, word_pointer)};
  363. const bool matching_type{value_type.value == memory_type.value};
  364. const Id bitcast_value{matching_type ? value : ctx.OpBitcast(value_type, value)};
  365. const Id cal_res{ctx.OpFunctionCall(value_type, cas_func, bitcast_value, op_b)};
  366. const Id new_value{matching_type ? cal_res : ctx.OpBitcast(memory_type, cal_res)};
  367. const Id atomic_res{ctx.OpAtomicCompareExchange(ctx.U32[1], word_pointer, scope_id, zero,
  368. zero, new_value, value)};
  369. const Id success{ctx.OpIEqual(ctx.U1, atomic_res, value)};
  370. ctx.OpBranchConditional(success, merge_block, loop_header);
  371. ctx.AddLabel(merge_block);
  372. ctx.OpReturnValue(ctx.OpBitcast(value_type, atomic_res));
  373. }
  374. ctx.OpFunctionEnd();
  375. return func;
  376. }
  377. template <typename Desc>
  378. std::string NameOf(Stage stage, const Desc& desc, std::string_view prefix) {
  379. if (desc.count > 1) {
  380. return fmt::format("{}_{}{}_{:02x}x{}", StageName(stage), prefix, desc.cbuf_index,
  381. desc.cbuf_offset, desc.count);
  382. } else {
  383. return fmt::format("{}_{}{}_{:02x}", StageName(stage), prefix, desc.cbuf_index,
  384. desc.cbuf_offset);
  385. }
  386. }
  387. Id DescType(EmitContext& ctx, Id sampled_type, Id pointer_type, u32 count) {
  388. if (count > 1) {
  389. const Id array_type{ctx.TypeArray(sampled_type, ctx.Const(count))};
  390. return ctx.TypePointer(spv::StorageClass::UniformConstant, array_type);
  391. } else {
  392. return pointer_type;
  393. }
  394. }
  395. constexpr size_t NUM_FIXEDFNCTEXTURE = 10;
  396. } // Anonymous namespace
  397. void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) {
  398. defs[0] = sirit_ctx.Name(base_type, name);
  399. std::array<char, 6> def_name;
  400. for (int i = 1; i < 4; ++i) {
  401. const std::string_view def_name_view(
  402. def_name.data(),
  403. fmt::format_to_n(def_name.data(), def_name.size(), "{}x{}", name, i + 1).size);
  404. defs[static_cast<size_t>(i)] =
  405. sirit_ctx.Name(sirit_ctx.TypeVector(base_type, i + 1), def_name_view);
  406. }
  407. }
  408. EmitContext::EmitContext(const Profile& profile_, const RuntimeInfo& runtime_info_,
  409. IR::Program& program, Bindings& bindings)
  410. : Sirit::Module(profile_.supported_spirv), profile{profile_},
  411. runtime_info{runtime_info_}, stage{program.stage} {
  412. const bool is_unified{profile.unified_descriptor_binding};
  413. u32& uniform_binding{is_unified ? bindings.unified : bindings.uniform_buffer};
  414. u32& storage_binding{is_unified ? bindings.unified : bindings.storage_buffer};
  415. u32& texture_binding{is_unified ? bindings.unified : bindings.texture};
  416. u32& image_binding{is_unified ? bindings.unified : bindings.image};
  417. AddCapability(spv::Capability::Shader);
  418. DefineCommonTypes(program.info);
  419. DefineCommonConstants();
  420. DefineInterfaces(program);
  421. DefineLocalMemory(program);
  422. DefineSharedMemory(program);
  423. DefineSharedMemoryFunctions(program);
  424. DefineConstantBuffers(program.info, uniform_binding);
  425. DefineStorageBuffers(program.info, storage_binding);
  426. DefineTextureBuffers(program.info, texture_binding);
  427. DefineImageBuffers(program.info, image_binding);
  428. DefineTextures(program.info, texture_binding);
  429. DefineImages(program.info, image_binding);
  430. DefineAttributeMemAccess(program.info);
  431. DefineGlobalMemoryFunctions(program.info);
  432. }
  433. EmitContext::~EmitContext() = default;
  434. Id EmitContext::Def(const IR::Value& value) {
  435. if (!value.IsImmediate()) {
  436. return value.InstRecursive()->Definition<Id>();
  437. }
  438. switch (value.Type()) {
  439. case IR::Type::Void:
  440. // Void instructions are used for optional arguments (e.g. texture offsets)
  441. // They are not meant to be used in the SPIR-V module
  442. return Id{};
  443. case IR::Type::U1:
  444. return value.U1() ? true_value : false_value;
  445. case IR::Type::U32:
  446. return Const(value.U32());
  447. case IR::Type::U64:
  448. return Constant(U64, value.U64());
  449. case IR::Type::F32:
  450. return Const(value.F32());
  451. case IR::Type::F64:
  452. return Constant(F64[1], value.F64());
  453. default:
  454. throw NotImplementedException("Immediate type {}", value.Type());
  455. }
  456. }
  457. Id EmitContext::BitOffset8(const IR::Value& offset) {
  458. if (offset.IsImmediate()) {
  459. return Const((offset.U32() % 4) * 8);
  460. }
  461. return OpBitwiseAnd(U32[1], OpShiftLeftLogical(U32[1], Def(offset), Const(3u)), Const(24u));
  462. }
  463. Id EmitContext::BitOffset16(const IR::Value& offset) {
  464. if (offset.IsImmediate()) {
  465. return Const(((offset.U32() / 2) % 2) * 16);
  466. }
  467. return OpBitwiseAnd(U32[1], OpShiftLeftLogical(U32[1], Def(offset), Const(3u)), Const(16u));
  468. }
  469. void EmitContext::DefineCommonTypes(const Info& info) {
  470. void_id = TypeVoid();
  471. U1 = Name(TypeBool(), "u1");
  472. F32.Define(*this, TypeFloat(32), "f32");
  473. U32.Define(*this, TypeInt(32, false), "u32");
  474. S32.Define(*this, TypeInt(32, true), "s32");
  475. private_u32 = Name(TypePointer(spv::StorageClass::Private, U32[1]), "private_u32");
  476. input_f32 = Name(TypePointer(spv::StorageClass::Input, F32[1]), "input_f32");
  477. input_u32 = Name(TypePointer(spv::StorageClass::Input, U32[1]), "input_u32");
  478. input_s32 = Name(TypePointer(spv::StorageClass::Input, TypeInt(32, true)), "input_s32");
  479. output_f32 = Name(TypePointer(spv::StorageClass::Output, F32[1]), "output_f32");
  480. output_u32 = Name(TypePointer(spv::StorageClass::Output, U32[1]), "output_u32");
  481. if (info.uses_int8 && profile.support_int8) {
  482. AddCapability(spv::Capability::Int8);
  483. U8 = Name(TypeInt(8, false), "u8");
  484. S8 = Name(TypeInt(8, true), "s8");
  485. }
  486. if (info.uses_int16 && profile.support_int16) {
  487. AddCapability(spv::Capability::Int16);
  488. U16 = Name(TypeInt(16, false), "u16");
  489. S16 = Name(TypeInt(16, true), "s16");
  490. }
  491. if (info.uses_int64) {
  492. AddCapability(spv::Capability::Int64);
  493. U64 = Name(TypeInt(64, false), "u64");
  494. }
  495. if (info.uses_fp16) {
  496. AddCapability(spv::Capability::Float16);
  497. F16.Define(*this, TypeFloat(16), "f16");
  498. }
  499. if (info.uses_fp64) {
  500. AddCapability(spv::Capability::Float64);
  501. F64.Define(*this, TypeFloat(64), "f64");
  502. }
  503. }
  504. void EmitContext::DefineCommonConstants() {
  505. true_value = ConstantTrue(U1);
  506. false_value = ConstantFalse(U1);
  507. u32_zero_value = Const(0U);
  508. f32_zero_value = Const(0.0f);
  509. }
  510. void EmitContext::DefineInterfaces(const IR::Program& program) {
  511. DefineInputs(program);
  512. DefineOutputs(program);
  513. }
  514. void EmitContext::DefineLocalMemory(const IR::Program& program) {
  515. if (program.local_memory_size == 0) {
  516. return;
  517. }
  518. const u32 num_elements{Common::DivCeil(program.local_memory_size, 4U)};
  519. const Id type{TypeArray(U32[1], Const(num_elements))};
  520. const Id pointer{TypePointer(spv::StorageClass::Private, type)};
  521. local_memory = AddGlobalVariable(pointer, spv::StorageClass::Private);
  522. if (profile.supported_spirv >= 0x00010400) {
  523. interfaces.push_back(local_memory);
  524. }
  525. }
  526. void EmitContext::DefineSharedMemory(const IR::Program& program) {
  527. if (program.shared_memory_size == 0) {
  528. return;
  529. }
  530. const auto make{[&](Id element_type, u32 element_size) {
  531. const u32 num_elements{Common::DivCeil(program.shared_memory_size, element_size)};
  532. const Id array_type{TypeArray(element_type, Const(num_elements))};
  533. Decorate(array_type, spv::Decoration::ArrayStride, element_size);
  534. const Id struct_type{TypeStruct(array_type)};
  535. MemberDecorate(struct_type, 0U, spv::Decoration::Offset, 0U);
  536. Decorate(struct_type, spv::Decoration::Block);
  537. const Id pointer{TypePointer(spv::StorageClass::Workgroup, struct_type)};
  538. const Id element_pointer{TypePointer(spv::StorageClass::Workgroup, element_type)};
  539. const Id variable{AddGlobalVariable(pointer, spv::StorageClass::Workgroup)};
  540. Decorate(variable, spv::Decoration::Aliased);
  541. interfaces.push_back(variable);
  542. return std::make_tuple(variable, element_pointer, pointer);
  543. }};
  544. if (profile.support_explicit_workgroup_layout) {
  545. AddExtension("SPV_KHR_workgroup_memory_explicit_layout");
  546. AddCapability(spv::Capability::WorkgroupMemoryExplicitLayoutKHR);
  547. if (program.info.uses_int8) {
  548. AddCapability(spv::Capability::WorkgroupMemoryExplicitLayout8BitAccessKHR);
  549. std::tie(shared_memory_u8, shared_u8, std::ignore) = make(U8, 1);
  550. }
  551. if (program.info.uses_int16) {
  552. AddCapability(spv::Capability::WorkgroupMemoryExplicitLayout16BitAccessKHR);
  553. std::tie(shared_memory_u16, shared_u16, std::ignore) = make(U16, 2);
  554. }
  555. if (program.info.uses_int64) {
  556. std::tie(shared_memory_u64, shared_u64, std::ignore) = make(U64, 8);
  557. }
  558. std::tie(shared_memory_u32, shared_u32, shared_memory_u32_type) = make(U32[1], 4);
  559. std::tie(shared_memory_u32x2, shared_u32x2, std::ignore) = make(U32[2], 8);
  560. std::tie(shared_memory_u32x4, shared_u32x4, std::ignore) = make(U32[4], 16);
  561. return;
  562. }
  563. const u32 num_elements{Common::DivCeil(program.shared_memory_size, 4U)};
  564. const Id type{TypeArray(U32[1], Const(num_elements))};
  565. shared_memory_u32_type = TypePointer(spv::StorageClass::Workgroup, type);
  566. shared_u32 = TypePointer(spv::StorageClass::Workgroup, U32[1]);
  567. shared_memory_u32 = AddGlobalVariable(shared_memory_u32_type, spv::StorageClass::Workgroup);
  568. interfaces.push_back(shared_memory_u32);
  569. const Id func_type{TypeFunction(void_id, U32[1], U32[1])};
  570. const auto make_function{[&](u32 mask, u32 size) {
  571. const Id loop_header{OpLabel()};
  572. const Id continue_block{OpLabel()};
  573. const Id merge_block{OpLabel()};
  574. const Id func{OpFunction(void_id, spv::FunctionControlMask::MaskNone, func_type)};
  575. const Id offset{OpFunctionParameter(U32[1])};
  576. const Id insert_value{OpFunctionParameter(U32[1])};
  577. AddLabel();
  578. OpBranch(loop_header);
  579. AddLabel(loop_header);
  580. const Id word_offset{OpShiftRightArithmetic(U32[1], offset, Const(2U))};
  581. const Id shift_offset{OpShiftLeftLogical(U32[1], offset, Const(3U))};
  582. const Id bit_offset{OpBitwiseAnd(U32[1], shift_offset, Const(mask))};
  583. const Id count{Const(size)};
  584. OpLoopMerge(merge_block, continue_block, spv::LoopControlMask::MaskNone);
  585. OpBranch(continue_block);
  586. AddLabel(continue_block);
  587. const Id word_pointer{OpAccessChain(shared_u32, shared_memory_u32, word_offset)};
  588. const Id old_value{OpLoad(U32[1], word_pointer)};
  589. const Id new_value{OpBitFieldInsert(U32[1], old_value, insert_value, bit_offset, count)};
  590. const Id atomic_res{OpAtomicCompareExchange(U32[1], word_pointer, Const(1U), u32_zero_value,
  591. u32_zero_value, new_value, old_value)};
  592. const Id success{OpIEqual(U1, atomic_res, old_value)};
  593. OpBranchConditional(success, merge_block, loop_header);
  594. AddLabel(merge_block);
  595. OpReturn();
  596. OpFunctionEnd();
  597. return func;
  598. }};
  599. if (program.info.uses_int8) {
  600. shared_store_u8_func = make_function(24, 8);
  601. }
  602. if (program.info.uses_int16) {
  603. shared_store_u16_func = make_function(16, 16);
  604. }
  605. }
  606. void EmitContext::DefineSharedMemoryFunctions(const IR::Program& program) {
  607. if (program.info.uses_shared_increment) {
  608. increment_cas_shared = CasLoop(*this, Operation::Increment, shared_memory_u32_type,
  609. shared_u32, U32[1], U32[1], spv::Scope::Workgroup);
  610. }
  611. if (program.info.uses_shared_decrement) {
  612. decrement_cas_shared = CasLoop(*this, Operation::Decrement, shared_memory_u32_type,
  613. shared_u32, U32[1], U32[1], spv::Scope::Workgroup);
  614. }
  615. }
  616. void EmitContext::DefineAttributeMemAccess(const Info& info) {
  617. const auto make_load{[&] {
  618. const bool is_array{stage == Stage::Geometry};
  619. const Id end_block{OpLabel()};
  620. const Id default_label{OpLabel()};
  621. const Id func_type_load{is_array ? TypeFunction(F32[1], U32[1], U32[1])
  622. : TypeFunction(F32[1], U32[1])};
  623. const Id func{OpFunction(F32[1], spv::FunctionControlMask::MaskNone, func_type_load)};
  624. const Id offset{OpFunctionParameter(U32[1])};
  625. const Id vertex{is_array ? OpFunctionParameter(U32[1]) : Id{}};
  626. AddLabel();
  627. const Id base_index{OpShiftRightArithmetic(U32[1], offset, Const(2U))};
  628. const Id masked_index{OpBitwiseAnd(U32[1], base_index, Const(3U))};
  629. const Id compare_index{OpShiftRightArithmetic(U32[1], base_index, Const(2U))};
  630. std::vector<Sirit::Literal> literals;
  631. std::vector<Id> labels;
  632. if (info.loads.AnyComponent(IR::Attribute::PositionX)) {
  633. literals.push_back(static_cast<u32>(IR::Attribute::PositionX) >> 2);
  634. labels.push_back(OpLabel());
  635. }
  636. const u32 base_attribute_value = static_cast<u32>(IR::Attribute::Generic0X) >> 2;
  637. for (u32 index = 0; index < static_cast<u32>(IR::NUM_GENERICS); ++index) {
  638. if (!info.loads.Generic(index)) {
  639. continue;
  640. }
  641. literals.push_back(base_attribute_value + index);
  642. labels.push_back(OpLabel());
  643. }
  644. OpSelectionMerge(end_block, spv::SelectionControlMask::MaskNone);
  645. OpSwitch(compare_index, default_label, literals, labels);
  646. AddLabel(default_label);
  647. OpReturnValue(Const(0.0f));
  648. size_t label_index{0};
  649. if (info.loads.AnyComponent(IR::Attribute::PositionX)) {
  650. AddLabel(labels[label_index]);
  651. const Id pointer{is_array
  652. ? OpAccessChain(input_f32, input_position, vertex, masked_index)
  653. : OpAccessChain(input_f32, input_position, masked_index)};
  654. const Id result{OpLoad(F32[1], pointer)};
  655. OpReturnValue(result);
  656. ++label_index;
  657. }
  658. for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
  659. if (!info.loads.Generic(index)) {
  660. continue;
  661. }
  662. AddLabel(labels[label_index]);
  663. const auto type{AttrTypes(*this, static_cast<u32>(index))};
  664. if (!type) {
  665. OpReturnValue(Const(0.0f));
  666. ++label_index;
  667. continue;
  668. }
  669. const Id generic_id{input_generics.at(index)};
  670. const Id pointer{is_array
  671. ? OpAccessChain(type->pointer, generic_id, vertex, masked_index)
  672. : OpAccessChain(type->pointer, generic_id, masked_index)};
  673. const Id value{OpLoad(type->id, pointer)};
  674. const Id result{type->needs_cast ? OpBitcast(F32[1], value) : value};
  675. OpReturnValue(result);
  676. ++label_index;
  677. }
  678. AddLabel(end_block);
  679. OpUnreachable();
  680. OpFunctionEnd();
  681. return func;
  682. }};
  683. const auto make_store{[&] {
  684. const Id end_block{OpLabel()};
  685. const Id default_label{OpLabel()};
  686. const Id func_type_store{TypeFunction(void_id, U32[1], F32[1])};
  687. const Id func{OpFunction(void_id, spv::FunctionControlMask::MaskNone, func_type_store)};
  688. const Id offset{OpFunctionParameter(U32[1])};
  689. const Id store_value{OpFunctionParameter(F32[1])};
  690. AddLabel();
  691. const Id base_index{OpShiftRightArithmetic(U32[1], offset, Const(2U))};
  692. const Id masked_index{OpBitwiseAnd(U32[1], base_index, Const(3U))};
  693. const Id compare_index{OpShiftRightArithmetic(U32[1], base_index, Const(2U))};
  694. std::vector<Sirit::Literal> literals;
  695. std::vector<Id> labels;
  696. if (info.stores.AnyComponent(IR::Attribute::PositionX)) {
  697. literals.push_back(static_cast<u32>(IR::Attribute::PositionX) >> 2);
  698. labels.push_back(OpLabel());
  699. }
  700. const u32 base_attribute_value = static_cast<u32>(IR::Attribute::Generic0X) >> 2;
  701. for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
  702. if (!info.stores.Generic(index)) {
  703. continue;
  704. }
  705. literals.push_back(base_attribute_value + static_cast<u32>(index));
  706. labels.push_back(OpLabel());
  707. }
  708. if (info.stores.ClipDistances()) {
  709. literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance0) >> 2);
  710. labels.push_back(OpLabel());
  711. literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance4) >> 2);
  712. labels.push_back(OpLabel());
  713. }
  714. OpSelectionMerge(end_block, spv::SelectionControlMask::MaskNone);
  715. OpSwitch(compare_index, default_label, literals, labels);
  716. AddLabel(default_label);
  717. OpReturn();
  718. size_t label_index{0};
  719. if (info.stores.AnyComponent(IR::Attribute::PositionX)) {
  720. AddLabel(labels[label_index]);
  721. const Id pointer{OpAccessChain(output_f32, output_position, masked_index)};
  722. OpStore(pointer, store_value);
  723. OpReturn();
  724. ++label_index;
  725. }
  726. for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
  727. if (!info.stores.Generic(index)) {
  728. continue;
  729. }
  730. if (output_generics[index][0].num_components != 4) {
  731. throw NotImplementedException("Physical stores and transform feedbacks");
  732. }
  733. AddLabel(labels[label_index]);
  734. const Id generic_id{output_generics[index][0].id};
  735. const Id pointer{OpAccessChain(output_f32, generic_id, masked_index)};
  736. OpStore(pointer, store_value);
  737. OpReturn();
  738. ++label_index;
  739. }
  740. if (info.stores.ClipDistances()) {
  741. AddLabel(labels[label_index]);
  742. const Id pointer{OpAccessChain(output_f32, clip_distances, masked_index)};
  743. OpStore(pointer, store_value);
  744. OpReturn();
  745. ++label_index;
  746. AddLabel(labels[label_index]);
  747. const Id fixed_index{OpIAdd(U32[1], masked_index, Const(4U))};
  748. const Id pointer2{OpAccessChain(output_f32, clip_distances, fixed_index)};
  749. OpStore(pointer2, store_value);
  750. OpReturn();
  751. ++label_index;
  752. }
  753. AddLabel(end_block);
  754. OpUnreachable();
  755. OpFunctionEnd();
  756. return func;
  757. }};
  758. if (info.loads_indexed_attributes) {
  759. indexed_load_func = make_load();
  760. }
  761. if (info.stores_indexed_attributes) {
  762. indexed_store_func = make_store();
  763. }
  764. }
  765. void EmitContext::DefineGlobalMemoryFunctions(const Info& info) {
  766. if (!info.uses_global_memory || !profile.support_int64) {
  767. return;
  768. }
  769. using DefPtr = Id StorageDefinitions::*;
  770. const Id zero{u32_zero_value};
  771. const auto define_body{[&](DefPtr ssbo_member, Id addr, Id element_pointer, u32 shift,
  772. auto&& callback) {
  773. AddLabel();
  774. const size_t num_buffers{info.storage_buffers_descriptors.size()};
  775. for (size_t index = 0; index < num_buffers; ++index) {
  776. if (!info.nvn_buffer_used[index]) {
  777. continue;
  778. }
  779. const auto& ssbo{info.storage_buffers_descriptors[index]};
  780. const Id ssbo_addr_cbuf_offset{Const(ssbo.cbuf_offset / 8)};
  781. const Id ssbo_size_cbuf_offset{Const(ssbo.cbuf_offset / 4 + 2)};
  782. const Id ssbo_addr_pointer{OpAccessChain(
  783. uniform_types.U32x2, cbufs[ssbo.cbuf_index].U32x2, zero, ssbo_addr_cbuf_offset)};
  784. const Id ssbo_size_pointer{OpAccessChain(uniform_types.U32, cbufs[ssbo.cbuf_index].U32,
  785. zero, ssbo_size_cbuf_offset)};
  786. const Id ssbo_addr{OpBitcast(U64, OpLoad(U32[2], ssbo_addr_pointer))};
  787. const Id ssbo_size{OpUConvert(U64, OpLoad(U32[1], ssbo_size_pointer))};
  788. const Id ssbo_end{OpIAdd(U64, ssbo_addr, ssbo_size)};
  789. const Id cond{OpLogicalAnd(U1, OpUGreaterThanEqual(U1, addr, ssbo_addr),
  790. OpULessThan(U1, addr, ssbo_end))};
  791. const Id then_label{OpLabel()};
  792. const Id else_label{OpLabel()};
  793. OpSelectionMerge(else_label, spv::SelectionControlMask::MaskNone);
  794. OpBranchConditional(cond, then_label, else_label);
  795. AddLabel(then_label);
  796. const Id ssbo_id{ssbos[index].*ssbo_member};
  797. const Id ssbo_offset{OpUConvert(U32[1], OpISub(U64, addr, ssbo_addr))};
  798. const Id ssbo_index{OpShiftRightLogical(U32[1], ssbo_offset, Const(shift))};
  799. const Id ssbo_pointer{OpAccessChain(element_pointer, ssbo_id, zero, ssbo_index)};
  800. callback(ssbo_pointer);
  801. AddLabel(else_label);
  802. }
  803. }};
  804. const auto define_load{[&](DefPtr ssbo_member, Id element_pointer, Id type, u32 shift) {
  805. const Id function_type{TypeFunction(type, U64)};
  806. const Id func_id{OpFunction(type, spv::FunctionControlMask::MaskNone, function_type)};
  807. const Id addr{OpFunctionParameter(U64)};
  808. define_body(ssbo_member, addr, element_pointer, shift,
  809. [&](Id ssbo_pointer) { OpReturnValue(OpLoad(type, ssbo_pointer)); });
  810. OpReturnValue(ConstantNull(type));
  811. OpFunctionEnd();
  812. return func_id;
  813. }};
  814. const auto define_write{[&](DefPtr ssbo_member, Id element_pointer, Id type, u32 shift) {
  815. const Id function_type{TypeFunction(void_id, U64, type)};
  816. const Id func_id{OpFunction(void_id, spv::FunctionControlMask::MaskNone, function_type)};
  817. const Id addr{OpFunctionParameter(U64)};
  818. const Id data{OpFunctionParameter(type)};
  819. define_body(ssbo_member, addr, element_pointer, shift, [&](Id ssbo_pointer) {
  820. OpStore(ssbo_pointer, data);
  821. OpReturn();
  822. });
  823. OpReturn();
  824. OpFunctionEnd();
  825. return func_id;
  826. }};
  827. const auto define{
  828. [&](DefPtr ssbo_member, const StorageTypeDefinition& type_def, Id type, size_t size) {
  829. const Id element_type{type_def.element};
  830. const u32 shift{static_cast<u32>(std::countr_zero(size))};
  831. const Id load_func{define_load(ssbo_member, element_type, type, shift)};
  832. const Id write_func{define_write(ssbo_member, element_type, type, shift)};
  833. return std::make_pair(load_func, write_func);
  834. }};
  835. std::tie(load_global_func_u32, write_global_func_u32) =
  836. define(&StorageDefinitions::U32, storage_types.U32, U32[1], sizeof(u32));
  837. std::tie(load_global_func_u32x2, write_global_func_u32x2) =
  838. define(&StorageDefinitions::U32x2, storage_types.U32x2, U32[2], sizeof(u32[2]));
  839. std::tie(load_global_func_u32x4, write_global_func_u32x4) =
  840. define(&StorageDefinitions::U32x4, storage_types.U32x4, U32[4], sizeof(u32[4]));
  841. }
  842. void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) {
  843. if (info.constant_buffer_descriptors.empty()) {
  844. return;
  845. }
  846. if (!profile.support_descriptor_aliasing) {
  847. DefineConstBuffers(*this, info, &UniformDefinitions::U32x4, binding, U32[4], 'u',
  848. sizeof(u32[4]));
  849. for (const ConstantBufferDescriptor& desc : info.constant_buffer_descriptors) {
  850. binding += desc.count;
  851. }
  852. return;
  853. }
  854. IR::Type types{info.used_constant_buffer_types};
  855. if (True(types & IR::Type::U8)) {
  856. if (profile.support_int8) {
  857. DefineConstBuffers(*this, info, &UniformDefinitions::U8, binding, U8, 'u', sizeof(u8));
  858. DefineConstBuffers(*this, info, &UniformDefinitions::S8, binding, S8, 's', sizeof(s8));
  859. } else {
  860. types |= IR::Type::U32;
  861. }
  862. }
  863. if (True(types & IR::Type::U16)) {
  864. if (profile.support_int16) {
  865. DefineConstBuffers(*this, info, &UniformDefinitions::U16, binding, U16, 'u',
  866. sizeof(u16));
  867. DefineConstBuffers(*this, info, &UniformDefinitions::S16, binding, S16, 's',
  868. sizeof(s16));
  869. } else {
  870. types |= IR::Type::U32;
  871. }
  872. }
  873. if (True(types & IR::Type::U32)) {
  874. DefineConstBuffers(*this, info, &UniformDefinitions::U32, binding, U32[1], 'u',
  875. sizeof(u32));
  876. }
  877. if (True(types & IR::Type::F32)) {
  878. DefineConstBuffers(*this, info, &UniformDefinitions::F32, binding, F32[1], 'f',
  879. sizeof(f32));
  880. }
  881. if (True(types & IR::Type::U32x2)) {
  882. DefineConstBuffers(*this, info, &UniformDefinitions::U32x2, binding, U32[2], 'u',
  883. sizeof(u32[2]));
  884. }
  885. binding += static_cast<u32>(info.constant_buffer_descriptors.size());
  886. }
  887. void EmitContext::DefineStorageBuffers(const Info& info, u32& binding) {
  888. if (info.storage_buffers_descriptors.empty()) {
  889. return;
  890. }
  891. AddExtension("SPV_KHR_storage_buffer_storage_class");
  892. const IR::Type used_types{profile.support_descriptor_aliasing ? info.used_storage_buffer_types
  893. : IR::Type::U32};
  894. if (profile.support_int8 && True(used_types & IR::Type::U8)) {
  895. DefineSsbos(*this, storage_types.U8, &StorageDefinitions::U8, info, binding, U8,
  896. sizeof(u8));
  897. DefineSsbos(*this, storage_types.S8, &StorageDefinitions::S8, info, binding, S8,
  898. sizeof(u8));
  899. }
  900. if (profile.support_int16 && True(used_types & IR::Type::U16)) {
  901. DefineSsbos(*this, storage_types.U16, &StorageDefinitions::U16, info, binding, U16,
  902. sizeof(u16));
  903. DefineSsbos(*this, storage_types.S16, &StorageDefinitions::S16, info, binding, S16,
  904. sizeof(u16));
  905. }
  906. if (True(used_types & IR::Type::U32)) {
  907. DefineSsbos(*this, storage_types.U32, &StorageDefinitions::U32, info, binding, U32[1],
  908. sizeof(u32));
  909. }
  910. if (True(used_types & IR::Type::F32)) {
  911. DefineSsbos(*this, storage_types.F32, &StorageDefinitions::F32, info, binding, F32[1],
  912. sizeof(f32));
  913. }
  914. if (True(used_types & IR::Type::U64)) {
  915. DefineSsbos(*this, storage_types.U64, &StorageDefinitions::U64, info, binding, U64,
  916. sizeof(u64));
  917. }
  918. if (True(used_types & IR::Type::U32x2)) {
  919. DefineSsbos(*this, storage_types.U32x2, &StorageDefinitions::U32x2, info, binding, U32[2],
  920. sizeof(u32[2]));
  921. }
  922. if (True(used_types & IR::Type::U32x4)) {
  923. DefineSsbos(*this, storage_types.U32x4, &StorageDefinitions::U32x4, info, binding, U32[4],
  924. sizeof(u32[4]));
  925. }
  926. for (const StorageBufferDescriptor& desc : info.storage_buffers_descriptors) {
  927. binding += desc.count;
  928. }
  929. const bool needs_function{
  930. info.uses_global_increment || info.uses_global_decrement || info.uses_atomic_f32_add ||
  931. info.uses_atomic_f16x2_add || info.uses_atomic_f16x2_min || info.uses_atomic_f16x2_max ||
  932. info.uses_atomic_f32x2_add || info.uses_atomic_f32x2_min || info.uses_atomic_f32x2_max};
  933. if (needs_function) {
  934. AddCapability(spv::Capability::VariablePointersStorageBuffer);
  935. }
  936. if (info.uses_global_increment) {
  937. increment_cas_ssbo = CasLoop(*this, Operation::Increment, storage_types.U32.array,
  938. storage_types.U32.element, U32[1], U32[1], spv::Scope::Device);
  939. }
  940. if (info.uses_global_decrement) {
  941. decrement_cas_ssbo = CasLoop(*this, Operation::Decrement, storage_types.U32.array,
  942. storage_types.U32.element, U32[1], U32[1], spv::Scope::Device);
  943. }
  944. if (info.uses_atomic_f32_add) {
  945. f32_add_cas = CasLoop(*this, Operation::FPAdd, storage_types.U32.array,
  946. storage_types.U32.element, F32[1], U32[1], spv::Scope::Device);
  947. }
  948. if (info.uses_atomic_f16x2_add) {
  949. f16x2_add_cas = CasLoop(*this, Operation::FPAdd, storage_types.U32.array,
  950. storage_types.U32.element, F16[2], F16[2], spv::Scope::Device);
  951. }
  952. if (info.uses_atomic_f16x2_min) {
  953. f16x2_min_cas = CasLoop(*this, Operation::FPMin, storage_types.U32.array,
  954. storage_types.U32.element, F16[2], F16[2], spv::Scope::Device);
  955. }
  956. if (info.uses_atomic_f16x2_max) {
  957. f16x2_max_cas = CasLoop(*this, Operation::FPMax, storage_types.U32.array,
  958. storage_types.U32.element, F16[2], F16[2], spv::Scope::Device);
  959. }
  960. if (info.uses_atomic_f32x2_add) {
  961. f32x2_add_cas = CasLoop(*this, Operation::FPAdd, storage_types.U32.array,
  962. storage_types.U32.element, F32[2], F32[2], spv::Scope::Device);
  963. }
  964. if (info.uses_atomic_f32x2_min) {
  965. f32x2_min_cas = CasLoop(*this, Operation::FPMin, storage_types.U32.array,
  966. storage_types.U32.element, F32[2], F32[2], spv::Scope::Device);
  967. }
  968. if (info.uses_atomic_f32x2_max) {
  969. f32x2_max_cas = CasLoop(*this, Operation::FPMax, storage_types.U32.array,
  970. storage_types.U32.element, F32[2], F32[2], spv::Scope::Device);
  971. }
  972. }
  973. void EmitContext::DefineTextureBuffers(const Info& info, u32& binding) {
  974. if (info.texture_buffer_descriptors.empty()) {
  975. return;
  976. }
  977. const spv::ImageFormat format{spv::ImageFormat::Unknown};
  978. image_buffer_type = TypeImage(F32[1], spv::Dim::Buffer, 0U, false, false, 1, format);
  979. sampled_texture_buffer_type = TypeSampledImage(image_buffer_type);
  980. const Id type{TypePointer(spv::StorageClass::UniformConstant, sampled_texture_buffer_type)};
  981. texture_buffers.reserve(info.texture_buffer_descriptors.size());
  982. for (const TextureBufferDescriptor& desc : info.texture_buffer_descriptors) {
  983. if (desc.count != 1) {
  984. throw NotImplementedException("Array of texture buffers");
  985. }
  986. const Id id{AddGlobalVariable(type, spv::StorageClass::UniformConstant)};
  987. Decorate(id, spv::Decoration::Binding, binding);
  988. Decorate(id, spv::Decoration::DescriptorSet, 0U);
  989. Name(id, NameOf(stage, desc, "texbuf"));
  990. texture_buffers.push_back({
  991. .id = id,
  992. .count = desc.count,
  993. });
  994. if (profile.supported_spirv >= 0x00010400) {
  995. interfaces.push_back(id);
  996. }
  997. ++binding;
  998. }
  999. }
  1000. void EmitContext::DefineImageBuffers(const Info& info, u32& binding) {
  1001. image_buffers.reserve(info.image_buffer_descriptors.size());
  1002. for (const ImageBufferDescriptor& desc : info.image_buffer_descriptors) {
  1003. if (desc.count != 1) {
  1004. throw NotImplementedException("Array of image buffers");
  1005. }
  1006. const spv::ImageFormat format{GetImageFormat(desc.format)};
  1007. const Id image_type{TypeImage(U32[1], spv::Dim::Buffer, false, false, false, 2, format)};
  1008. const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, image_type)};
  1009. const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)};
  1010. Decorate(id, spv::Decoration::Binding, binding);
  1011. Decorate(id, spv::Decoration::DescriptorSet, 0U);
  1012. Name(id, NameOf(stage, desc, "imgbuf"));
  1013. image_buffers.push_back({
  1014. .id = id,
  1015. .image_type = image_type,
  1016. .count = desc.count,
  1017. });
  1018. if (profile.supported_spirv >= 0x00010400) {
  1019. interfaces.push_back(id);
  1020. }
  1021. ++binding;
  1022. }
  1023. }
  1024. void EmitContext::DefineTextures(const Info& info, u32& binding) {
  1025. textures.reserve(info.texture_descriptors.size());
  1026. for (const TextureDescriptor& desc : info.texture_descriptors) {
  1027. const Id image_type{ImageType(*this, desc)};
  1028. const Id sampled_type{TypeSampledImage(image_type)};
  1029. const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, sampled_type)};
  1030. const Id desc_type{DescType(*this, sampled_type, pointer_type, desc.count)};
  1031. const Id id{AddGlobalVariable(desc_type, spv::StorageClass::UniformConstant)};
  1032. Decorate(id, spv::Decoration::Binding, binding);
  1033. Decorate(id, spv::Decoration::DescriptorSet, 0U);
  1034. Name(id, NameOf(stage, desc, "tex"));
  1035. textures.push_back({
  1036. .id = id,
  1037. .sampled_type = sampled_type,
  1038. .pointer_type = pointer_type,
  1039. .image_type = image_type,
  1040. .count = desc.count,
  1041. });
  1042. if (profile.supported_spirv >= 0x00010400) {
  1043. interfaces.push_back(id);
  1044. }
  1045. ++binding;
  1046. }
  1047. if (info.uses_atomic_image_u32) {
  1048. image_u32 = TypePointer(spv::StorageClass::Image, U32[1]);
  1049. }
  1050. }
  1051. void EmitContext::DefineImages(const Info& info, u32& binding) {
  1052. images.reserve(info.image_descriptors.size());
  1053. for (const ImageDescriptor& desc : info.image_descriptors) {
  1054. if (desc.count != 1) {
  1055. throw NotImplementedException("Array of images");
  1056. }
  1057. const Id image_type{ImageType(*this, desc)};
  1058. const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, image_type)};
  1059. const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)};
  1060. Decorate(id, spv::Decoration::Binding, binding);
  1061. Decorate(id, spv::Decoration::DescriptorSet, 0U);
  1062. Name(id, NameOf(stage, desc, "img"));
  1063. images.push_back({
  1064. .id = id,
  1065. .image_type = image_type,
  1066. .count = desc.count,
  1067. });
  1068. if (profile.supported_spirv >= 0x00010400) {
  1069. interfaces.push_back(id);
  1070. }
  1071. ++binding;
  1072. }
  1073. }
  1074. void EmitContext::DefineInputs(const IR::Program& program) {
  1075. const Info& info{program.info};
  1076. const VaryingState loads{info.loads.mask | info.passthrough.mask};
  1077. if (info.uses_workgroup_id) {
  1078. workgroup_id = DefineInput(*this, U32[3], false, spv::BuiltIn::WorkgroupId);
  1079. }
  1080. if (info.uses_local_invocation_id) {
  1081. local_invocation_id = DefineInput(*this, U32[3], false, spv::BuiltIn::LocalInvocationId);
  1082. }
  1083. if (info.uses_invocation_id) {
  1084. invocation_id = DefineInput(*this, U32[1], false, spv::BuiltIn::InvocationId);
  1085. }
  1086. if (info.uses_sample_id) {
  1087. sample_id = DefineInput(*this, U32[1], false, spv::BuiltIn::SampleId);
  1088. }
  1089. if (info.uses_is_helper_invocation) {
  1090. is_helper_invocation = DefineInput(*this, U1, false, spv::BuiltIn::HelperInvocation);
  1091. }
  1092. if (info.uses_subgroup_mask) {
  1093. subgroup_mask_eq = DefineInput(*this, U32[4], false, spv::BuiltIn::SubgroupEqMaskKHR);
  1094. subgroup_mask_lt = DefineInput(*this, U32[4], false, spv::BuiltIn::SubgroupLtMaskKHR);
  1095. subgroup_mask_le = DefineInput(*this, U32[4], false, spv::BuiltIn::SubgroupLeMaskKHR);
  1096. subgroup_mask_gt = DefineInput(*this, U32[4], false, spv::BuiltIn::SubgroupGtMaskKHR);
  1097. subgroup_mask_ge = DefineInput(*this, U32[4], false, spv::BuiltIn::SubgroupGeMaskKHR);
  1098. }
  1099. if (info.uses_subgroup_invocation_id || info.uses_subgroup_shuffles ||
  1100. (profile.warp_size_potentially_larger_than_guest &&
  1101. (info.uses_subgroup_vote || info.uses_subgroup_mask))) {
  1102. subgroup_local_invocation_id =
  1103. DefineInput(*this, U32[1], false, spv::BuiltIn::SubgroupLocalInvocationId);
  1104. }
  1105. if (info.uses_fswzadd) {
  1106. const Id f32_one{Const(1.0f)};
  1107. const Id f32_minus_one{Const(-1.0f)};
  1108. const Id f32_zero{Const(0.0f)};
  1109. fswzadd_lut_a = ConstantComposite(F32[4], f32_minus_one, f32_one, f32_minus_one, f32_zero);
  1110. fswzadd_lut_b =
  1111. ConstantComposite(F32[4], f32_minus_one, f32_minus_one, f32_one, f32_minus_one);
  1112. }
  1113. if (loads[IR::Attribute::PrimitiveId]) {
  1114. primitive_id = DefineInput(*this, U32[1], false, spv::BuiltIn::PrimitiveId);
  1115. }
  1116. if (loads.AnyComponent(IR::Attribute::PositionX)) {
  1117. const bool is_fragment{stage != Stage::Fragment};
  1118. const spv::BuiltIn built_in{is_fragment ? spv::BuiltIn::Position : spv::BuiltIn::FragCoord};
  1119. input_position = DefineInput(*this, F32[4], true, built_in);
  1120. if (profile.support_geometry_shader_passthrough) {
  1121. if (info.passthrough.AnyComponent(IR::Attribute::PositionX)) {
  1122. Decorate(input_position, spv::Decoration::PassthroughNV);
  1123. }
  1124. }
  1125. }
  1126. if (loads[IR::Attribute::InstanceId]) {
  1127. if (profile.support_vertex_instance_id) {
  1128. instance_id = DefineInput(*this, U32[1], true, spv::BuiltIn::InstanceId);
  1129. } else {
  1130. instance_index = DefineInput(*this, U32[1], true, spv::BuiltIn::InstanceIndex);
  1131. base_instance = DefineInput(*this, U32[1], true, spv::BuiltIn::BaseInstance);
  1132. }
  1133. }
  1134. if (loads[IR::Attribute::VertexId]) {
  1135. if (profile.support_vertex_instance_id) {
  1136. vertex_id = DefineInput(*this, U32[1], true, spv::BuiltIn::VertexId);
  1137. } else {
  1138. vertex_index = DefineInput(*this, U32[1], true, spv::BuiltIn::VertexIndex);
  1139. base_vertex = DefineInput(*this, U32[1], true, spv::BuiltIn::BaseVertex);
  1140. }
  1141. }
  1142. if (loads[IR::Attribute::FrontFace]) {
  1143. front_face = DefineInput(*this, U1, true, spv::BuiltIn::FrontFacing);
  1144. }
  1145. if (loads[IR::Attribute::PointSpriteS] || loads[IR::Attribute::PointSpriteT]) {
  1146. point_coord = DefineInput(*this, F32[2], true, spv::BuiltIn::PointCoord);
  1147. }
  1148. if (loads[IR::Attribute::TessellationEvaluationPointU] ||
  1149. loads[IR::Attribute::TessellationEvaluationPointV]) {
  1150. tess_coord = DefineInput(*this, F32[3], false, spv::BuiltIn::TessCoord);
  1151. }
  1152. std::queue<u32> ununsed_location;
  1153. for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
  1154. const AttributeType input_type{runtime_info.generic_input_types[index]};
  1155. if (!runtime_info.previous_stage_stores.Generic(index) || !loads.Generic(index) ||
  1156. input_type == AttributeType::Disabled) {
  1157. ununsed_location.push(static_cast<u32>(index));
  1158. continue;
  1159. }
  1160. const Id type{GetAttributeType(*this, input_type)};
  1161. const Id id{DefineInput(*this, type, true)};
  1162. Decorate(id, spv::Decoration::Location, static_cast<u32>(index));
  1163. Name(id, fmt::format("in_attr{}", index));
  1164. input_generics[index] = id;
  1165. if (info.passthrough.Generic(index) && profile.support_geometry_shader_passthrough) {
  1166. Decorate(id, spv::Decoration::PassthroughNV);
  1167. }
  1168. if (stage != Stage::Fragment) {
  1169. continue;
  1170. }
  1171. switch (info.interpolation[index]) {
  1172. case Interpolation::Smooth:
  1173. // Default
  1174. // Decorate(id, spv::Decoration::Smooth);
  1175. break;
  1176. case Interpolation::NoPerspective:
  1177. Decorate(id, spv::Decoration::NoPerspective);
  1178. break;
  1179. case Interpolation::Flat:
  1180. Decorate(id, spv::Decoration::Flat);
  1181. break;
  1182. }
  1183. }
  1184. if (loads.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
  1185. if (ununsed_location.empty()) {
  1186. throw RuntimeError("Unable to get an unused location");
  1187. }
  1188. u32 location = ununsed_location.front();
  1189. ununsed_location.pop();
  1190. const Id id{DefineInput(*this, F32[4], true)};
  1191. Decorate(id, spv::Decoration::Location, location);
  1192. input_front_color = id;
  1193. }
  1194. for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) {
  1195. if (loads.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) {
  1196. if (ununsed_location.empty()) {
  1197. throw RuntimeError("Unable to get an unused location");
  1198. }
  1199. u32 location = ununsed_location.front();
  1200. ununsed_location.pop();
  1201. const Id id{DefineInput(*this, F32[4], true)};
  1202. Decorate(id, spv::Decoration::Location, location);
  1203. input_fixed_fnc_textures[index] = id;
  1204. }
  1205. }
  1206. if (stage == Stage::TessellationEval) {
  1207. for (size_t index = 0; index < info.uses_patches.size(); ++index) {
  1208. if (!info.uses_patches[index]) {
  1209. continue;
  1210. }
  1211. const Id id{DefineInput(*this, F32[4], false)};
  1212. Decorate(id, spv::Decoration::Patch);
  1213. Decorate(id, spv::Decoration::Location, static_cast<u32>(index));
  1214. patches[index] = id;
  1215. }
  1216. }
  1217. }
  1218. void EmitContext::DefineOutputs(const IR::Program& program) {
  1219. const Info& info{program.info};
  1220. const std::optional<u32> invocations{program.invocations};
  1221. if (info.stores.AnyComponent(IR::Attribute::PositionX) || stage == Stage::VertexB) {
  1222. output_position = DefineOutput(*this, F32[4], invocations, spv::BuiltIn::Position);
  1223. }
  1224. if (info.stores[IR::Attribute::PointSize] || runtime_info.fixed_state_point_size) {
  1225. if (stage == Stage::Fragment) {
  1226. throw NotImplementedException("Storing PointSize in fragment stage");
  1227. }
  1228. output_point_size = DefineOutput(*this, F32[1], invocations, spv::BuiltIn::PointSize);
  1229. }
  1230. if (info.stores.ClipDistances()) {
  1231. if (stage == Stage::Fragment) {
  1232. throw NotImplementedException("Storing ClipDistance in fragment stage");
  1233. }
  1234. const Id type{TypeArray(F32[1], Const(8U))};
  1235. clip_distances = DefineOutput(*this, type, invocations, spv::BuiltIn::ClipDistance);
  1236. }
  1237. if (info.stores[IR::Attribute::Layer] &&
  1238. (profile.support_viewport_index_layer_non_geometry || stage == Stage::Geometry)) {
  1239. if (stage == Stage::Fragment) {
  1240. throw NotImplementedException("Storing Layer in fragment stage");
  1241. }
  1242. layer = DefineOutput(*this, U32[1], invocations, spv::BuiltIn::Layer);
  1243. }
  1244. if (info.stores[IR::Attribute::ViewportIndex] &&
  1245. (profile.support_viewport_index_layer_non_geometry || stage == Stage::Geometry)) {
  1246. if (stage == Stage::Fragment) {
  1247. throw NotImplementedException("Storing ViewportIndex in fragment stage");
  1248. }
  1249. viewport_index = DefineOutput(*this, U32[1], invocations, spv::BuiltIn::ViewportIndex);
  1250. }
  1251. if (info.stores[IR::Attribute::ViewportMask] && profile.support_viewport_mask) {
  1252. viewport_mask = DefineOutput(*this, TypeArray(U32[1], Const(1u)), std::nullopt,
  1253. spv::BuiltIn::ViewportMaskNV);
  1254. }
  1255. std::queue<u32> ununsed_location;
  1256. for (size_t index = 0; index < IR::NUM_GENERICS; ++index) {
  1257. if (info.stores.Generic(index)) {
  1258. DefineGenericOutput(*this, index, invocations);
  1259. } else {
  1260. ununsed_location.push(static_cast<u32>(index));
  1261. }
  1262. }
  1263. if (info.stores.AnyComponent(IR::Attribute::ColorFrontDiffuseR)) {
  1264. if (ununsed_location.empty()) {
  1265. throw RuntimeError("Unable to get an unused location");
  1266. }
  1267. u32 location = ununsed_location.front();
  1268. ununsed_location.pop();
  1269. const Id id{DefineOutput(*this, F32[4], invocations)};
  1270. Decorate(id, spv::Decoration::Location, location);
  1271. output_front_color = id;
  1272. }
  1273. for (size_t index = 0; index < NUM_FIXEDFNCTEXTURE; ++index) {
  1274. if (info.stores.AnyComponent(IR::Attribute::FixedFncTexture0S + index * 4)) {
  1275. if (ununsed_location.empty()) {
  1276. throw RuntimeError("Unable to get an unused location");
  1277. }
  1278. u32 location = ununsed_location.front();
  1279. ununsed_location.pop();
  1280. const Id id{DefineOutput(*this, F32[4], invocations)};
  1281. Decorate(id, spv::Decoration::Location, location);
  1282. output_fixed_fnc_textures[index] = id;
  1283. }
  1284. }
  1285. switch (stage) {
  1286. case Stage::TessellationControl:
  1287. if (info.stores_tess_level_outer) {
  1288. const Id type{TypeArray(F32[1], Const(4U))};
  1289. output_tess_level_outer =
  1290. DefineOutput(*this, type, std::nullopt, spv::BuiltIn::TessLevelOuter);
  1291. Decorate(output_tess_level_outer, spv::Decoration::Patch);
  1292. }
  1293. if (info.stores_tess_level_inner) {
  1294. const Id type{TypeArray(F32[1], Const(2U))};
  1295. output_tess_level_inner =
  1296. DefineOutput(*this, type, std::nullopt, spv::BuiltIn::TessLevelInner);
  1297. Decorate(output_tess_level_inner, spv::Decoration::Patch);
  1298. }
  1299. for (size_t index = 0; index < info.uses_patches.size(); ++index) {
  1300. if (!info.uses_patches[index]) {
  1301. continue;
  1302. }
  1303. const Id id{DefineOutput(*this, F32[4], std::nullopt)};
  1304. Decorate(id, spv::Decoration::Patch);
  1305. Decorate(id, spv::Decoration::Location, static_cast<u32>(index));
  1306. patches[index] = id;
  1307. }
  1308. break;
  1309. case Stage::Fragment:
  1310. for (u32 index = 0; index < 8; ++index) {
  1311. if (!info.stores_frag_color[index] && !profile.need_declared_frag_colors) {
  1312. continue;
  1313. }
  1314. frag_color[index] = DefineOutput(*this, F32[4], std::nullopt);
  1315. Decorate(frag_color[index], spv::Decoration::Location, index);
  1316. Name(frag_color[index], fmt::format("frag_color{}", index));
  1317. }
  1318. if (info.stores_frag_depth) {
  1319. frag_depth = DefineOutput(*this, F32[1], std::nullopt);
  1320. Decorate(frag_depth, spv::Decoration::BuiltIn, spv::BuiltIn::FragDepth);
  1321. }
  1322. if (info.stores_sample_mask) {
  1323. sample_mask = DefineOutput(*this, U32[1], std::nullopt);
  1324. Decorate(sample_mask, spv::Decoration::BuiltIn, spv::BuiltIn::SampleMask);
  1325. }
  1326. break;
  1327. default:
  1328. break;
  1329. }
  1330. }
  1331. } // namespace Shader::Backend::SPIRV