emit_context.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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 <string_view>
  7. #include <fmt/format.h>
  8. #include "common/common_types.h"
  9. #include "common/div_ceil.h"
  10. #include "shader_recompiler/backend/spirv/emit_context.h"
  11. namespace Shader::Backend::SPIRV {
  12. namespace {
  13. Id ImageType(EmitContext& ctx, const TextureDescriptor& desc) {
  14. const spv::ImageFormat format{spv::ImageFormat::Unknown};
  15. const Id type{ctx.F32[1]};
  16. switch (desc.type) {
  17. case TextureType::Color1D:
  18. return ctx.TypeImage(type, spv::Dim::Dim1D, false, false, false, 1, format);
  19. case TextureType::ColorArray1D:
  20. return ctx.TypeImage(type, spv::Dim::Dim1D, false, true, false, 1, format);
  21. case TextureType::Color2D:
  22. return ctx.TypeImage(type, spv::Dim::Dim2D, false, false, false, 1, format);
  23. case TextureType::ColorArray2D:
  24. return ctx.TypeImage(type, spv::Dim::Dim2D, false, true, false, 1, format);
  25. case TextureType::Color3D:
  26. return ctx.TypeImage(type, spv::Dim::Dim3D, false, false, false, 1, format);
  27. case TextureType::ColorCube:
  28. return ctx.TypeImage(type, spv::Dim::Cube, false, false, false, 1, format);
  29. case TextureType::ColorArrayCube:
  30. return ctx.TypeImage(type, spv::Dim::Cube, false, true, false, 1, format);
  31. case TextureType::Shadow1D:
  32. return ctx.TypeImage(type, spv::Dim::Dim1D, true, false, false, 1, format);
  33. case TextureType::ShadowArray1D:
  34. return ctx.TypeImage(type, spv::Dim::Dim1D, true, true, false, 1, format);
  35. case TextureType::Shadow2D:
  36. return ctx.TypeImage(type, spv::Dim::Dim2D, true, false, false, 1, format);
  37. case TextureType::ShadowArray2D:
  38. return ctx.TypeImage(type, spv::Dim::Dim2D, true, true, false, 1, format);
  39. case TextureType::Shadow3D:
  40. return ctx.TypeImage(type, spv::Dim::Dim3D, true, false, false, 1, format);
  41. case TextureType::ShadowCube:
  42. return ctx.TypeImage(type, spv::Dim::Cube, true, false, false, 1, format);
  43. case TextureType::ShadowArrayCube:
  44. return ctx.TypeImage(type, spv::Dim::Cube, true, true, false, 1, format);
  45. }
  46. throw InvalidArgument("Invalid texture type {}", desc.type);
  47. }
  48. Id DefineVariable(EmitContext& ctx, Id type, std::optional<spv::BuiltIn> builtin,
  49. spv::StorageClass storage_class) {
  50. const Id pointer_type{ctx.TypePointer(storage_class, type)};
  51. const Id id{ctx.AddGlobalVariable(pointer_type, storage_class)};
  52. if (builtin) {
  53. ctx.Decorate(id, spv::Decoration::BuiltIn, *builtin);
  54. }
  55. ctx.interfaces.push_back(id);
  56. return id;
  57. }
  58. Id DefineInput(EmitContext& ctx, Id type, std::optional<spv::BuiltIn> builtin = std::nullopt) {
  59. return DefineVariable(ctx, type, builtin, spv::StorageClass::Input);
  60. }
  61. Id DefineOutput(EmitContext& ctx, Id type, std::optional<spv::BuiltIn> builtin = std::nullopt) {
  62. return DefineVariable(ctx, type, builtin, spv::StorageClass::Output);
  63. }
  64. Id GetAttributeType(EmitContext& ctx, AttributeType type) {
  65. switch (type) {
  66. case AttributeType::Float:
  67. return ctx.F32[4];
  68. case AttributeType::SignedInt:
  69. return ctx.TypeVector(ctx.TypeInt(32, true), 4);
  70. case AttributeType::UnsignedInt:
  71. return ctx.U32[4];
  72. case AttributeType::Disabled:
  73. break;
  74. }
  75. throw InvalidArgument("Invalid attribute type {}", type);
  76. }
  77. struct AttrInfo {
  78. Id pointer;
  79. Id id;
  80. bool needs_cast;
  81. };
  82. std::optional<AttrInfo> AttrTypes(EmitContext& ctx, u32 index) {
  83. const AttributeType type{ctx.profile.generic_input_types.at(index)};
  84. switch (type) {
  85. case AttributeType::Float:
  86. return AttrInfo{ctx.input_f32, ctx.F32[1], false};
  87. case AttributeType::UnsignedInt:
  88. return AttrInfo{ctx.input_u32, ctx.U32[1], true};
  89. case AttributeType::SignedInt:
  90. return AttrInfo{ctx.input_s32, ctx.TypeInt(32, true), true};
  91. case AttributeType::Disabled:
  92. return std::nullopt;
  93. }
  94. throw InvalidArgument("Invalid attribute type {}", type);
  95. }
  96. } // Anonymous namespace
  97. void VectorTypes::Define(Sirit::Module& sirit_ctx, Id base_type, std::string_view name) {
  98. defs[0] = sirit_ctx.Name(base_type, name);
  99. std::array<char, 6> def_name;
  100. for (int i = 1; i < 4; ++i) {
  101. const std::string_view def_name_view(
  102. def_name.data(),
  103. fmt::format_to_n(def_name.data(), def_name.size(), "{}x{}", name, i + 1).size);
  104. defs[i] = sirit_ctx.Name(sirit_ctx.TypeVector(base_type, i + 1), def_name_view);
  105. }
  106. }
  107. EmitContext::EmitContext(const Profile& profile_, IR::Program& program, u32& binding)
  108. : Sirit::Module(profile_.supported_spirv), profile{profile_}, stage{program.stage} {
  109. AddCapability(spv::Capability::Shader);
  110. DefineCommonTypes(program.info);
  111. DefineCommonConstants();
  112. DefineInterfaces(program.info);
  113. DefineLocalMemory(program);
  114. DefineSharedMemory(program);
  115. DefineConstantBuffers(program.info, binding);
  116. DefineStorageBuffers(program.info, binding);
  117. DefineTextures(program.info, binding);
  118. DefineAttributeMemAccess(program.info);
  119. DefineLabels(program);
  120. }
  121. EmitContext::~EmitContext() = default;
  122. Id EmitContext::Def(const IR::Value& value) {
  123. if (!value.IsImmediate()) {
  124. return value.InstRecursive()->Definition<Id>();
  125. }
  126. switch (value.Type()) {
  127. case IR::Type::Void:
  128. // Void instructions are used for optional arguments (e.g. texture offsets)
  129. // They are not meant to be used in the SPIR-V module
  130. return Id{};
  131. case IR::Type::U1:
  132. return value.U1() ? true_value : false_value;
  133. case IR::Type::U32:
  134. return Constant(U32[1], value.U32());
  135. case IR::Type::U64:
  136. return Constant(U64, value.U64());
  137. case IR::Type::F32:
  138. return Constant(F32[1], value.F32());
  139. case IR::Type::F64:
  140. return Constant(F64[1], value.F64());
  141. case IR::Type::Label:
  142. return value.Label()->Definition<Id>();
  143. default:
  144. throw NotImplementedException("Immediate type {}", value.Type());
  145. }
  146. }
  147. void EmitContext::DefineCommonTypes(const Info& info) {
  148. void_id = TypeVoid();
  149. U1 = Name(TypeBool(), "u1");
  150. F32.Define(*this, TypeFloat(32), "f32");
  151. U32.Define(*this, TypeInt(32, false), "u32");
  152. private_u32 = Name(TypePointer(spv::StorageClass::Private, U32[1]), "private_u32");
  153. input_f32 = Name(TypePointer(spv::StorageClass::Input, F32[1]), "input_f32");
  154. input_u32 = Name(TypePointer(spv::StorageClass::Input, U32[1]), "input_u32");
  155. input_s32 = Name(TypePointer(spv::StorageClass::Input, TypeInt(32, true)), "input_s32");
  156. output_f32 = Name(TypePointer(spv::StorageClass::Output, F32[1]), "output_f32");
  157. if (info.uses_int8) {
  158. AddCapability(spv::Capability::Int8);
  159. U8 = Name(TypeInt(8, false), "u8");
  160. S8 = Name(TypeInt(8, true), "s8");
  161. }
  162. if (info.uses_int16) {
  163. AddCapability(spv::Capability::Int16);
  164. U16 = Name(TypeInt(16, false), "u16");
  165. S16 = Name(TypeInt(16, true), "s16");
  166. }
  167. if (info.uses_int64) {
  168. AddCapability(spv::Capability::Int64);
  169. U64 = Name(TypeInt(64, false), "u64");
  170. }
  171. if (info.uses_fp16) {
  172. AddCapability(spv::Capability::Float16);
  173. F16.Define(*this, TypeFloat(16), "f16");
  174. }
  175. if (info.uses_fp64) {
  176. AddCapability(spv::Capability::Float64);
  177. F64.Define(*this, TypeFloat(64), "f64");
  178. }
  179. }
  180. void EmitContext::DefineCommonConstants() {
  181. true_value = ConstantTrue(U1);
  182. false_value = ConstantFalse(U1);
  183. u32_zero_value = Constant(U32[1], 0U);
  184. f32_zero_value = Constant(F32[1], 0.0f);
  185. }
  186. void EmitContext::DefineInterfaces(const Info& info) {
  187. DefineInputs(info);
  188. DefineOutputs(info);
  189. }
  190. void EmitContext::DefineLocalMemory(const IR::Program& program) {
  191. if (program.local_memory_size == 0) {
  192. return;
  193. }
  194. const u32 num_elements{Common::DivCeil(program.local_memory_size, 4U)};
  195. const Id type{TypeArray(U32[1], Constant(U32[1], num_elements))};
  196. const Id pointer{TypePointer(spv::StorageClass::Private, type)};
  197. local_memory = AddGlobalVariable(pointer, spv::StorageClass::Private);
  198. if (profile.supported_spirv >= 0x00010400) {
  199. interfaces.push_back(local_memory);
  200. }
  201. }
  202. void EmitContext::DefineSharedMemory(const IR::Program& program) {
  203. if (program.shared_memory_size == 0) {
  204. return;
  205. }
  206. const auto make{[&](Id element_type, u32 element_size) {
  207. const u32 num_elements{Common::DivCeil(program.shared_memory_size, element_size)};
  208. const Id array_type{TypeArray(element_type, Constant(U32[1], num_elements))};
  209. Decorate(array_type, spv::Decoration::ArrayStride, element_size);
  210. const Id struct_type{TypeStruct(array_type)};
  211. MemberDecorate(struct_type, 0U, spv::Decoration::Offset, 0U);
  212. Decorate(struct_type, spv::Decoration::Block);
  213. const Id pointer{TypePointer(spv::StorageClass::Workgroup, struct_type)};
  214. const Id element_pointer{TypePointer(spv::StorageClass::Workgroup, element_type)};
  215. const Id variable{AddGlobalVariable(pointer, spv::StorageClass::Workgroup)};
  216. Decorate(variable, spv::Decoration::Aliased);
  217. interfaces.push_back(variable);
  218. return std::make_pair(variable, element_pointer);
  219. }};
  220. if (profile.support_explicit_workgroup_layout) {
  221. AddExtension("SPV_KHR_workgroup_memory_explicit_layout");
  222. AddCapability(spv::Capability::WorkgroupMemoryExplicitLayoutKHR);
  223. if (program.info.uses_int8) {
  224. AddCapability(spv::Capability::WorkgroupMemoryExplicitLayout8BitAccessKHR);
  225. std::tie(shared_memory_u8, shared_u8) = make(U8, 1);
  226. }
  227. if (program.info.uses_int16) {
  228. AddCapability(spv::Capability::WorkgroupMemoryExplicitLayout16BitAccessKHR);
  229. std::tie(shared_memory_u16, shared_u16) = make(U16, 2);
  230. }
  231. std::tie(shared_memory_u32, shared_u32) = make(U32[1], 4);
  232. std::tie(shared_memory_u32x2, shared_u32x2) = make(U32[2], 8);
  233. std::tie(shared_memory_u32x4, shared_u32x4) = make(U32[4], 16);
  234. return;
  235. }
  236. const u32 num_elements{Common::DivCeil(program.shared_memory_size, 4U)};
  237. const Id type{TypeArray(U32[1], Constant(U32[1], num_elements))};
  238. const Id pointer_type{TypePointer(spv::StorageClass::Workgroup, type)};
  239. shared_u32 = TypePointer(spv::StorageClass::Workgroup, U32[1]);
  240. shared_memory_u32 = AddGlobalVariable(pointer_type, spv::StorageClass::Workgroup);
  241. interfaces.push_back(shared_memory_u32);
  242. const Id func_type{TypeFunction(void_id, U32[1], U32[1])};
  243. const auto make_function{[&](u32 mask, u32 size) {
  244. const Id loop_header{OpLabel()};
  245. const Id continue_block{OpLabel()};
  246. const Id merge_block{OpLabel()};
  247. const Id func{OpFunction(void_id, spv::FunctionControlMask::MaskNone, func_type)};
  248. const Id offset{OpFunctionParameter(U32[1])};
  249. const Id insert_value{OpFunctionParameter(U32[1])};
  250. AddLabel();
  251. OpBranch(loop_header);
  252. AddLabel(loop_header);
  253. const Id word_offset{OpShiftRightArithmetic(U32[1], offset, Constant(U32[1], 2U))};
  254. const Id shift_offset{OpShiftLeftLogical(U32[1], offset, Constant(U32[1], 3U))};
  255. const Id bit_offset{OpBitwiseAnd(U32[1], shift_offset, Constant(U32[1], mask))};
  256. const Id count{Constant(U32[1], size)};
  257. OpLoopMerge(merge_block, continue_block, spv::LoopControlMask::MaskNone);
  258. OpBranch(continue_block);
  259. AddLabel(continue_block);
  260. const Id word_pointer{OpAccessChain(shared_u32, shared_memory_u32, word_offset)};
  261. const Id old_value{OpLoad(U32[1], word_pointer)};
  262. const Id new_value{OpBitFieldInsert(U32[1], old_value, insert_value, bit_offset, count)};
  263. const Id atomic_res{OpAtomicCompareExchange(U32[1], word_pointer, Constant(U32[1], 1U),
  264. u32_zero_value, u32_zero_value, new_value,
  265. old_value)};
  266. const Id success{OpIEqual(U1, atomic_res, old_value)};
  267. OpBranchConditional(success, merge_block, loop_header);
  268. AddLabel(merge_block);
  269. OpReturn();
  270. OpFunctionEnd();
  271. return func;
  272. }};
  273. if (program.info.uses_int8) {
  274. shared_store_u8_func = make_function(24, 8);
  275. }
  276. if (program.info.uses_int16) {
  277. shared_store_u16_func = make_function(16, 16);
  278. }
  279. }
  280. void EmitContext::DefineAttributeMemAccess(const Info& info) {
  281. const auto make_load{[&] {
  282. const Id end_block{OpLabel()};
  283. const Id default_label{OpLabel()};
  284. const Id func_type_load{TypeFunction(F32[1], U32[1])};
  285. const Id func{OpFunction(F32[1], spv::FunctionControlMask::MaskNone, func_type_load)};
  286. const Id offset{OpFunctionParameter(U32[1])};
  287. AddLabel();
  288. const Id base_index{OpShiftRightArithmetic(U32[1], offset, Constant(U32[1], 2U))};
  289. const Id masked_index{OpBitwiseAnd(U32[1], base_index, Constant(U32[1], 3U))};
  290. const Id compare_index{OpShiftRightArithmetic(U32[1], base_index, Constant(U32[1], 2U))};
  291. std::vector<Sirit::Literal> literals;
  292. std::vector<Id> labels;
  293. if (info.loads_position) {
  294. literals.push_back(static_cast<u32>(IR::Attribute::PositionX) >> 2);
  295. labels.push_back(OpLabel());
  296. }
  297. const u32 base_attribute_value = static_cast<u32>(IR::Attribute::Generic0X) >> 2;
  298. for (u32 i = 0; i < info.input_generics.size(); i++) {
  299. if (!info.input_generics[i].used) {
  300. continue;
  301. }
  302. literals.push_back(base_attribute_value + i);
  303. labels.push_back(OpLabel());
  304. }
  305. OpSelectionMerge(end_block, spv::SelectionControlMask::MaskNone);
  306. OpSwitch(compare_index, default_label, literals, labels);
  307. AddLabel(default_label);
  308. OpReturnValue(Constant(F32[1], 0.0f));
  309. size_t label_index{0};
  310. if (info.loads_position) {
  311. AddLabel(labels[label_index]);
  312. const Id result{OpLoad(F32[1], OpAccessChain(input_f32, input_position, masked_index))};
  313. OpReturnValue(result);
  314. ++label_index;
  315. }
  316. for (size_t i = 0; i < info.input_generics.size(); i++) {
  317. if (!info.input_generics[i].used) {
  318. continue;
  319. }
  320. AddLabel(labels[label_index]);
  321. const auto type{AttrTypes(*this, static_cast<u32>(i))};
  322. if (!type) {
  323. OpReturnValue(Constant(F32[1], 0.0f));
  324. ++label_index;
  325. continue;
  326. }
  327. const Id generic_id{input_generics.at(i)};
  328. const Id pointer{OpAccessChain(type->pointer, generic_id, masked_index)};
  329. const Id value{OpLoad(type->id, pointer)};
  330. const Id result{type->needs_cast ? OpBitcast(F32[1], value) : value};
  331. OpReturnValue(result);
  332. ++label_index;
  333. }
  334. AddLabel(end_block);
  335. OpUnreachable();
  336. OpFunctionEnd();
  337. return func;
  338. }};
  339. const auto make_store{[&] {
  340. const Id end_block{OpLabel()};
  341. const Id default_label{OpLabel()};
  342. const Id func_type_store{TypeFunction(void_id, U32[1], F32[1])};
  343. const Id func{OpFunction(void_id, spv::FunctionControlMask::MaskNone, func_type_store)};
  344. const Id offset{OpFunctionParameter(U32[1])};
  345. const Id store_value{OpFunctionParameter(F32[1])};
  346. AddLabel();
  347. const Id base_index{OpShiftRightArithmetic(U32[1], offset, Constant(U32[1], 2U))};
  348. const Id masked_index{OpBitwiseAnd(U32[1], base_index, Constant(U32[1], 3U))};
  349. const Id compare_index{OpShiftRightArithmetic(U32[1], base_index, Constant(U32[1], 2U))};
  350. std::vector<Sirit::Literal> literals;
  351. std::vector<Id> labels;
  352. if (info.stores_position) {
  353. literals.push_back(static_cast<u32>(IR::Attribute::PositionX) >> 2);
  354. labels.push_back(OpLabel());
  355. }
  356. const u32 base_attribute_value = static_cast<u32>(IR::Attribute::Generic0X) >> 2;
  357. for (size_t i = 0; i < info.stores_generics.size(); i++) {
  358. if (!info.stores_generics[i]) {
  359. continue;
  360. }
  361. literals.push_back(base_attribute_value + static_cast<u32>(i));
  362. labels.push_back(OpLabel());
  363. }
  364. if (info.stores_clip_distance) {
  365. literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance0) >> 2);
  366. labels.push_back(OpLabel());
  367. literals.push_back(static_cast<u32>(IR::Attribute::ClipDistance4) >> 2);
  368. labels.push_back(OpLabel());
  369. }
  370. OpSelectionMerge(end_block, spv::SelectionControlMask::MaskNone);
  371. OpSwitch(compare_index, default_label, literals, labels);
  372. AddLabel(default_label);
  373. OpReturn();
  374. size_t label_index{0};
  375. if (info.stores_position) {
  376. AddLabel(labels[label_index]);
  377. const Id pointer{OpAccessChain(output_f32, output_position, masked_index)};
  378. OpStore(pointer, store_value);
  379. OpReturn();
  380. ++label_index;
  381. }
  382. for (size_t i = 0; i < info.stores_generics.size(); i++) {
  383. if (!info.stores_generics[i]) {
  384. continue;
  385. }
  386. AddLabel(labels[label_index]);
  387. const Id generic_id{output_generics.at(i)};
  388. const Id pointer{OpAccessChain(output_f32, generic_id, masked_index)};
  389. OpStore(pointer, store_value);
  390. OpReturn();
  391. ++label_index;
  392. }
  393. if (info.stores_clip_distance) {
  394. AddLabel(labels[label_index]);
  395. const Id pointer{OpAccessChain(output_f32, clip_distances, masked_index)};
  396. OpStore(pointer, store_value);
  397. OpReturn();
  398. ++label_index;
  399. AddLabel(labels[label_index]);
  400. const Id fixed_index{OpIAdd(U32[1], masked_index, Constant(U32[1], 4))};
  401. const Id pointer2{OpAccessChain(output_f32, clip_distances, fixed_index)};
  402. OpStore(pointer2, store_value);
  403. OpReturn();
  404. ++label_index;
  405. }
  406. AddLabel(end_block);
  407. OpUnreachable();
  408. OpFunctionEnd();
  409. return func;
  410. }};
  411. if (info.loads_indexed_attributes) {
  412. indexed_load_func = make_load();
  413. }
  414. if (info.stores_indexed_attributes) {
  415. indexed_store_func = make_store();
  416. }
  417. }
  418. void EmitContext::DefineConstantBuffers(const Info& info, u32& binding) {
  419. if (info.constant_buffer_descriptors.empty()) {
  420. return;
  421. }
  422. if (True(info.used_constant_buffer_types & IR::Type::U8)) {
  423. DefineConstantBuffers(info, &UniformDefinitions::U8, binding, U8, 'u', sizeof(u8));
  424. DefineConstantBuffers(info, &UniformDefinitions::S8, binding, S8, 's', sizeof(s8));
  425. }
  426. if (True(info.used_constant_buffer_types & IR::Type::U16)) {
  427. DefineConstantBuffers(info, &UniformDefinitions::U16, binding, U16, 'u', sizeof(u16));
  428. DefineConstantBuffers(info, &UniformDefinitions::S16, binding, S16, 's', sizeof(s16));
  429. }
  430. if (True(info.used_constant_buffer_types & IR::Type::U32)) {
  431. DefineConstantBuffers(info, &UniformDefinitions::U32, binding, U32[1], 'u', sizeof(u32));
  432. }
  433. if (True(info.used_constant_buffer_types & IR::Type::F32)) {
  434. DefineConstantBuffers(info, &UniformDefinitions::F32, binding, F32[1], 'f', sizeof(f32));
  435. }
  436. if (True(info.used_constant_buffer_types & IR::Type::U32x2)) {
  437. DefineConstantBuffers(info, &UniformDefinitions::U32x2, binding, U32[2], 'u', sizeof(u64));
  438. }
  439. for (const ConstantBufferDescriptor& desc : info.constant_buffer_descriptors) {
  440. binding += desc.count;
  441. }
  442. }
  443. void EmitContext::DefineStorageBuffers(const Info& info, u32& binding) {
  444. if (info.storage_buffers_descriptors.empty()) {
  445. return;
  446. }
  447. AddExtension("SPV_KHR_storage_buffer_storage_class");
  448. const Id array_type{TypeRuntimeArray(U32[1])};
  449. Decorate(array_type, spv::Decoration::ArrayStride, 4U);
  450. const Id struct_type{TypeStruct(array_type)};
  451. Name(struct_type, "ssbo_block");
  452. Decorate(struct_type, spv::Decoration::Block);
  453. MemberName(struct_type, 0, "data");
  454. MemberDecorate(struct_type, 0, spv::Decoration::Offset, 0U);
  455. const Id storage_type{TypePointer(spv::StorageClass::StorageBuffer, struct_type)};
  456. storage_u32 = TypePointer(spv::StorageClass::StorageBuffer, U32[1]);
  457. u32 index{};
  458. for (const StorageBufferDescriptor& desc : info.storage_buffers_descriptors) {
  459. const Id id{AddGlobalVariable(storage_type, spv::StorageClass::StorageBuffer)};
  460. Decorate(id, spv::Decoration::Binding, binding);
  461. Decorate(id, spv::Decoration::DescriptorSet, 0U);
  462. Name(id, fmt::format("ssbo{}", index));
  463. if (profile.supported_spirv >= 0x00010400) {
  464. interfaces.push_back(id);
  465. }
  466. std::fill_n(ssbos.data() + index, desc.count, id);
  467. index += desc.count;
  468. binding += desc.count;
  469. }
  470. }
  471. void EmitContext::DefineTextures(const Info& info, u32& binding) {
  472. textures.reserve(info.texture_descriptors.size());
  473. for (const TextureDescriptor& desc : info.texture_descriptors) {
  474. if (desc.count != 1) {
  475. throw NotImplementedException("Array of textures");
  476. }
  477. const Id image_type{ImageType(*this, desc)};
  478. const Id sampled_type{TypeSampledImage(image_type)};
  479. const Id pointer_type{TypePointer(spv::StorageClass::UniformConstant, sampled_type)};
  480. const Id id{AddGlobalVariable(pointer_type, spv::StorageClass::UniformConstant)};
  481. Decorate(id, spv::Decoration::Binding, binding);
  482. Decorate(id, spv::Decoration::DescriptorSet, 0U);
  483. Name(id, fmt::format("tex{}_{:02x}", desc.cbuf_index, desc.cbuf_offset));
  484. for (u32 index = 0; index < desc.count; ++index) {
  485. // TODO: Pass count info
  486. textures.push_back(TextureDefinition{
  487. .id{id},
  488. .sampled_type{sampled_type},
  489. .image_type{image_type},
  490. });
  491. }
  492. if (profile.supported_spirv >= 0x00010400) {
  493. interfaces.push_back(id);
  494. }
  495. binding += desc.count;
  496. }
  497. }
  498. void EmitContext::DefineLabels(IR::Program& program) {
  499. for (IR::Block* const block : program.blocks) {
  500. block->SetDefinition(OpLabel());
  501. }
  502. }
  503. void EmitContext::DefineInputs(const Info& info) {
  504. if (info.uses_workgroup_id) {
  505. workgroup_id = DefineInput(*this, U32[3], spv::BuiltIn::WorkgroupId);
  506. }
  507. if (info.uses_local_invocation_id) {
  508. local_invocation_id = DefineInput(*this, U32[3], spv::BuiltIn::LocalInvocationId);
  509. }
  510. if (info.uses_subgroup_mask) {
  511. subgroup_mask_eq = DefineInput(*this, U32[4], spv::BuiltIn::SubgroupEqMaskKHR);
  512. subgroup_mask_lt = DefineInput(*this, U32[4], spv::BuiltIn::SubgroupLtMaskKHR);
  513. subgroup_mask_le = DefineInput(*this, U32[4], spv::BuiltIn::SubgroupLeMaskKHR);
  514. subgroup_mask_gt = DefineInput(*this, U32[4], spv::BuiltIn::SubgroupGtMaskKHR);
  515. subgroup_mask_ge = DefineInput(*this, U32[4], spv::BuiltIn::SubgroupGeMaskKHR);
  516. }
  517. if (info.uses_subgroup_invocation_id ||
  518. (profile.warp_size_potentially_larger_than_guest &&
  519. (info.uses_subgroup_vote || info.uses_subgroup_mask))) {
  520. subgroup_local_invocation_id =
  521. DefineInput(*this, U32[1], spv::BuiltIn::SubgroupLocalInvocationId);
  522. }
  523. if (info.uses_fswzadd) {
  524. const Id f32_one{Constant(F32[1], 1.0f)};
  525. const Id f32_minus_one{Constant(F32[1], -1.0f)};
  526. const Id f32_zero{Constant(F32[1], 0.0f)};
  527. fswzadd_lut_a = ConstantComposite(F32[4], f32_minus_one, f32_one, f32_minus_one, f32_zero);
  528. fswzadd_lut_b =
  529. ConstantComposite(F32[4], f32_minus_one, f32_minus_one, f32_one, f32_minus_one);
  530. }
  531. if (info.loads_position) {
  532. const bool is_fragment{stage != Stage::Fragment};
  533. const spv::BuiltIn built_in{is_fragment ? spv::BuiltIn::Position : spv::BuiltIn::FragCoord};
  534. input_position = DefineInput(*this, F32[4], built_in);
  535. }
  536. if (info.loads_instance_id) {
  537. if (profile.support_vertex_instance_id) {
  538. instance_id = DefineInput(*this, U32[1], spv::BuiltIn::InstanceId);
  539. } else {
  540. instance_index = DefineInput(*this, U32[1], spv::BuiltIn::InstanceIndex);
  541. base_instance = DefineInput(*this, U32[1], spv::BuiltIn::BaseInstance);
  542. }
  543. }
  544. if (info.loads_vertex_id) {
  545. if (profile.support_vertex_instance_id) {
  546. vertex_id = DefineInput(*this, U32[1], spv::BuiltIn::VertexId);
  547. } else {
  548. vertex_index = DefineInput(*this, U32[1], spv::BuiltIn::VertexIndex);
  549. base_vertex = DefineInput(*this, U32[1], spv::BuiltIn::BaseVertex);
  550. }
  551. }
  552. if (info.loads_front_face) {
  553. front_face = DefineInput(*this, U1, spv::BuiltIn::FrontFacing);
  554. }
  555. if (info.loads_point_coord) {
  556. point_coord = DefineInput(*this, F32[2], spv::BuiltIn::PointCoord);
  557. }
  558. for (size_t index = 0; index < info.input_generics.size(); ++index) {
  559. const InputVarying generic{info.input_generics[index]};
  560. if (!generic.used) {
  561. continue;
  562. }
  563. const AttributeType input_type{profile.generic_input_types[index]};
  564. if (input_type == AttributeType::Disabled) {
  565. continue;
  566. }
  567. const Id type{GetAttributeType(*this, input_type)};
  568. const Id id{DefineInput(*this, type)};
  569. Decorate(id, spv::Decoration::Location, static_cast<u32>(index));
  570. Name(id, fmt::format("in_attr{}", index));
  571. input_generics[index] = id;
  572. if (stage != Stage::Fragment) {
  573. continue;
  574. }
  575. switch (generic.interpolation) {
  576. case Interpolation::Smooth:
  577. // Default
  578. // Decorate(id, spv::Decoration::Smooth);
  579. break;
  580. case Interpolation::NoPerspective:
  581. Decorate(id, spv::Decoration::NoPerspective);
  582. break;
  583. case Interpolation::Flat:
  584. Decorate(id, spv::Decoration::Flat);
  585. break;
  586. }
  587. }
  588. }
  589. void EmitContext::DefineConstantBuffers(const Info& info, Id UniformDefinitions::*member_type,
  590. u32 binding, Id type, char type_char, u32 element_size) {
  591. const Id array_type{TypeArray(type, Constant(U32[1], 65536U / element_size))};
  592. Decorate(array_type, spv::Decoration::ArrayStride, element_size);
  593. const Id struct_type{TypeStruct(array_type)};
  594. Name(struct_type, fmt::format("cbuf_block_{}{}", type_char, element_size * CHAR_BIT));
  595. Decorate(struct_type, spv::Decoration::Block);
  596. MemberName(struct_type, 0, "data");
  597. MemberDecorate(struct_type, 0, spv::Decoration::Offset, 0U);
  598. const Id struct_pointer_type{TypePointer(spv::StorageClass::Uniform, struct_type)};
  599. const Id uniform_type{TypePointer(spv::StorageClass::Uniform, type)};
  600. uniform_types.*member_type = uniform_type;
  601. for (const ConstantBufferDescriptor& desc : info.constant_buffer_descriptors) {
  602. const Id id{AddGlobalVariable(struct_pointer_type, spv::StorageClass::Uniform)};
  603. Decorate(id, spv::Decoration::Binding, binding);
  604. Decorate(id, spv::Decoration::DescriptorSet, 0U);
  605. Name(id, fmt::format("c{}", desc.index));
  606. for (size_t i = 0; i < desc.count; ++i) {
  607. cbufs[desc.index + i].*member_type = id;
  608. }
  609. if (profile.supported_spirv >= 0x00010400) {
  610. interfaces.push_back(id);
  611. }
  612. binding += desc.count;
  613. }
  614. }
  615. void EmitContext::DefineOutputs(const Info& info) {
  616. if (info.stores_position || stage == Stage::VertexB) {
  617. output_position = DefineOutput(*this, F32[4], spv::BuiltIn::Position);
  618. }
  619. if (info.stores_point_size || profile.fixed_state_point_size) {
  620. if (stage == Stage::Fragment) {
  621. throw NotImplementedException("Storing PointSize in Fragment stage");
  622. }
  623. output_point_size = DefineOutput(*this, F32[1], spv::BuiltIn::PointSize);
  624. }
  625. if (info.stores_clip_distance) {
  626. if (stage == Stage::Fragment) {
  627. throw NotImplementedException("Storing PointSize in Fragment stage");
  628. }
  629. const Id type{TypeArray(F32[1], Constant(U32[1], 8U))};
  630. clip_distances = DefineOutput(*this, type, spv::BuiltIn::ClipDistance);
  631. }
  632. if (info.stores_viewport_index &&
  633. (profile.support_viewport_index_layer_non_geometry || stage == Shader::Stage::Geometry)) {
  634. if (stage == Stage::Fragment) {
  635. throw NotImplementedException("Storing ViewportIndex in Fragment stage");
  636. }
  637. viewport_index = DefineOutput(*this, U32[1], spv::BuiltIn::ViewportIndex);
  638. }
  639. for (size_t i = 0; i < info.stores_generics.size(); ++i) {
  640. if (info.stores_generics[i]) {
  641. output_generics[i] = DefineOutput(*this, F32[4]);
  642. Decorate(output_generics[i], spv::Decoration::Location, static_cast<u32>(i));
  643. Name(output_generics[i], fmt::format("out_attr{}", i));
  644. }
  645. }
  646. if (stage == Stage::Fragment) {
  647. for (u32 index = 0; index < 8; ++index) {
  648. if (!info.stores_frag_color[index]) {
  649. continue;
  650. }
  651. frag_color[index] = DefineOutput(*this, F32[4]);
  652. Decorate(frag_color[index], spv::Decoration::Location, index);
  653. Name(frag_color[index], fmt::format("frag_color{}", index));
  654. }
  655. if (info.stores_frag_depth) {
  656. frag_depth = DefineOutput(*this, F32[1]);
  657. Decorate(frag_depth, spv::Decoration::BuiltIn, spv::BuiltIn::FragDepth);
  658. Name(frag_depth, "frag_depth");
  659. }
  660. }
  661. }
  662. } // namespace Shader::Backend::SPIRV