emit_spirv_image.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <boost/container/static_vector.hpp>
  5. #include "shader_recompiler/backend/spirv/emit_spirv.h"
  6. #include "shader_recompiler/frontend/ir/modifiers.h"
  7. namespace Shader::Backend::SPIRV {
  8. namespace {
  9. class ImageOperands {
  10. public:
  11. explicit ImageOperands(EmitContext& ctx, bool has_bias, bool has_lod, bool has_lod_clamp,
  12. Id lod, const IR::Value& offset) {
  13. if (has_bias) {
  14. const Id bias{has_lod_clamp ? ctx.OpCompositeExtract(ctx.F32[1], lod, 0) : lod};
  15. Add(spv::ImageOperandsMask::Bias, bias);
  16. }
  17. if (has_lod) {
  18. const Id lod_value{has_lod_clamp ? ctx.OpCompositeExtract(ctx.F32[1], lod, 0) : lod};
  19. Add(spv::ImageOperandsMask::Lod, lod_value);
  20. }
  21. AddOffset(ctx, offset);
  22. if (has_lod_clamp) {
  23. const Id lod_clamp{has_bias ? ctx.OpCompositeExtract(ctx.F32[1], lod, 1) : lod};
  24. Add(spv::ImageOperandsMask::MinLod, lod_clamp);
  25. }
  26. }
  27. explicit ImageOperands(EmitContext& ctx, const IR::Value& offset, const IR::Value& offset2) {
  28. if (offset2.IsEmpty()) {
  29. if (offset.IsEmpty()) {
  30. return;
  31. }
  32. Add(spv::ImageOperandsMask::Offset, ctx.Def(offset));
  33. return;
  34. }
  35. const std::array values{offset.InstRecursive(), offset2.InstRecursive()};
  36. if (!values[0]->AreAllArgsImmediates() || !values[1]->AreAllArgsImmediates()) {
  37. // LOG_WARNING("Not all arguments in PTP are immediate, STUBBING");
  38. return;
  39. }
  40. const IR::Opcode opcode{values[0]->GetOpcode()};
  41. if (opcode != values[1]->GetOpcode() || opcode != IR::Opcode::CompositeConstructU32x4) {
  42. throw LogicError("Invalid PTP arguments");
  43. }
  44. auto read{[&](unsigned int a, unsigned int b) { return values[a]->Arg(b).U32(); }};
  45. const Id offsets{ctx.ConstantComposite(
  46. ctx.TypeArray(ctx.U32[2], ctx.Const(4U)), ctx.Const(read(0, 0), read(0, 1)),
  47. ctx.Const(read(0, 2), read(0, 3)), ctx.Const(read(1, 0), read(1, 1)),
  48. ctx.Const(read(1, 2), read(1, 3)))};
  49. Add(spv::ImageOperandsMask::ConstOffsets, offsets);
  50. }
  51. explicit ImageOperands(Id offset, Id lod, Id ms) {
  52. if (Sirit::ValidId(lod)) {
  53. Add(spv::ImageOperandsMask::Lod, lod);
  54. }
  55. if (Sirit::ValidId(offset)) {
  56. Add(spv::ImageOperandsMask::Offset, offset);
  57. }
  58. if (Sirit::ValidId(ms)) {
  59. Add(spv::ImageOperandsMask::Sample, ms);
  60. }
  61. }
  62. explicit ImageOperands(EmitContext& ctx, bool has_lod_clamp, Id derivates, u32 num_derivates,
  63. Id offset, Id lod_clamp) {
  64. if (!Sirit::ValidId(derivates)) {
  65. throw LogicError("Derivates must be present");
  66. }
  67. boost::container::static_vector<Id, 3> deriv_x_accum;
  68. boost::container::static_vector<Id, 3> deriv_y_accum;
  69. for (u32 i = 0; i < num_derivates; ++i) {
  70. deriv_x_accum.push_back(ctx.OpCompositeExtract(ctx.F32[1], derivates, i * 2));
  71. deriv_y_accum.push_back(ctx.OpCompositeExtract(ctx.F32[1], derivates, i * 2 + 1));
  72. }
  73. const Id derivates_X{ctx.OpCompositeConstruct(
  74. ctx.F32[num_derivates], std::span{deriv_x_accum.data(), deriv_x_accum.size()})};
  75. const Id derivates_Y{ctx.OpCompositeConstruct(
  76. ctx.F32[num_derivates], std::span{deriv_y_accum.data(), deriv_y_accum.size()})};
  77. Add(spv::ImageOperandsMask::Grad, derivates_X, derivates_Y);
  78. if (Sirit::ValidId(offset)) {
  79. Add(spv::ImageOperandsMask::Offset, offset);
  80. }
  81. if (has_lod_clamp) {
  82. Add(spv::ImageOperandsMask::MinLod, lod_clamp);
  83. }
  84. }
  85. std::span<const Id> Span() const noexcept {
  86. return std::span{operands.data(), operands.size()};
  87. }
  88. spv::ImageOperandsMask Mask() const noexcept {
  89. return mask;
  90. }
  91. private:
  92. void AddOffset(EmitContext& ctx, const IR::Value& offset) {
  93. if (offset.IsEmpty()) {
  94. return;
  95. }
  96. if (offset.IsImmediate()) {
  97. Add(spv::ImageOperandsMask::ConstOffset, ctx.Const(offset.U32()));
  98. return;
  99. }
  100. IR::Inst* const inst{offset.InstRecursive()};
  101. if (inst->AreAllArgsImmediates()) {
  102. switch (inst->GetOpcode()) {
  103. case IR::Opcode::CompositeConstructU32x2:
  104. Add(spv::ImageOperandsMask::ConstOffset,
  105. ctx.Const(inst->Arg(0).U32(), inst->Arg(1).U32()));
  106. return;
  107. case IR::Opcode::CompositeConstructU32x3:
  108. Add(spv::ImageOperandsMask::ConstOffset,
  109. ctx.Const(inst->Arg(0).U32(), inst->Arg(1).U32(), inst->Arg(2).U32()));
  110. return;
  111. case IR::Opcode::CompositeConstructU32x4:
  112. Add(spv::ImageOperandsMask::ConstOffset,
  113. ctx.Const(inst->Arg(0).U32(), inst->Arg(1).U32(), inst->Arg(2).U32(),
  114. inst->Arg(3).U32()));
  115. return;
  116. default:
  117. break;
  118. }
  119. }
  120. Add(spv::ImageOperandsMask::Offset, ctx.Def(offset));
  121. }
  122. void Add(spv::ImageOperandsMask new_mask, Id value) {
  123. mask = static_cast<spv::ImageOperandsMask>(static_cast<unsigned>(mask) |
  124. static_cast<unsigned>(new_mask));
  125. operands.push_back(value);
  126. }
  127. void Add(spv::ImageOperandsMask new_mask, Id value_1, Id value_2) {
  128. mask = static_cast<spv::ImageOperandsMask>(static_cast<unsigned>(mask) |
  129. static_cast<unsigned>(new_mask));
  130. operands.push_back(value_1);
  131. operands.push_back(value_2);
  132. }
  133. boost::container::static_vector<Id, 4> operands;
  134. spv::ImageOperandsMask mask{};
  135. };
  136. Id Texture(EmitContext& ctx, IR::TextureInstInfo info, [[maybe_unused]] const IR::Value& index) {
  137. const TextureDefinition& def{ctx.textures.at(info.descriptor_index)};
  138. if (def.count > 1) {
  139. const Id pointer{ctx.OpAccessChain(def.pointer_type, def.id, ctx.Def(index))};
  140. return ctx.OpLoad(def.sampled_type, pointer);
  141. } else {
  142. return ctx.OpLoad(def.sampled_type, def.id);
  143. }
  144. }
  145. Id TextureImage(EmitContext& ctx, IR::TextureInstInfo info,
  146. [[maybe_unused]] const IR::Value& index) {
  147. if (info.type == TextureType::Buffer) {
  148. const TextureBufferDefinition& def{ctx.texture_buffers.at(info.descriptor_index)};
  149. if (def.count > 1) {
  150. throw NotImplementedException("Indirect texture sample");
  151. }
  152. const Id sampler_id{def.id};
  153. const Id id{ctx.OpLoad(ctx.sampled_texture_buffer_type, sampler_id)};
  154. return ctx.OpImage(ctx.image_buffer_type, id);
  155. } else {
  156. const TextureDefinition& def{ctx.textures.at(info.descriptor_index)};
  157. if (def.count > 1) {
  158. throw NotImplementedException("Indirect texture sample");
  159. }
  160. return ctx.OpImage(def.image_type, ctx.OpLoad(def.sampled_type, def.id));
  161. }
  162. }
  163. Id Image(EmitContext& ctx, const IR::Value& index, IR::TextureInstInfo info) {
  164. if (!index.IsImmediate()) {
  165. throw NotImplementedException("Indirect image indexing");
  166. }
  167. if (info.type == TextureType::Buffer) {
  168. const ImageBufferDefinition def{ctx.image_buffers.at(index.U32())};
  169. return ctx.OpLoad(def.image_type, def.id);
  170. } else {
  171. const ImageDefinition def{ctx.images.at(index.U32())};
  172. return ctx.OpLoad(def.image_type, def.id);
  173. }
  174. }
  175. Id Decorate(EmitContext& ctx, IR::Inst* inst, Id sample) {
  176. const auto info{inst->Flags<IR::TextureInstInfo>()};
  177. if (info.relaxed_precision != 0) {
  178. ctx.Decorate(sample, spv::Decoration::RelaxedPrecision);
  179. }
  180. return sample;
  181. }
  182. template <typename MethodPtrType, typename... Args>
  183. Id Emit(MethodPtrType sparse_ptr, MethodPtrType non_sparse_ptr, EmitContext& ctx, IR::Inst* inst,
  184. Id result_type, Args&&... args) {
  185. IR::Inst* const sparse{inst->GetAssociatedPseudoOperation(IR::Opcode::GetSparseFromOp)};
  186. if (!sparse) {
  187. return Decorate(ctx, inst, (ctx.*non_sparse_ptr)(result_type, std::forward<Args>(args)...));
  188. }
  189. const Id struct_type{ctx.TypeStruct(ctx.U32[1], result_type)};
  190. const Id sample{(ctx.*sparse_ptr)(struct_type, std::forward<Args>(args)...)};
  191. const Id resident_code{ctx.OpCompositeExtract(ctx.U32[1], sample, 0U)};
  192. sparse->SetDefinition(ctx.OpImageSparseTexelsResident(ctx.U1, resident_code));
  193. sparse->Invalidate();
  194. Decorate(ctx, inst, sample);
  195. return ctx.OpCompositeExtract(result_type, sample, 1U);
  196. }
  197. } // Anonymous namespace
  198. Id EmitBindlessImageSampleImplicitLod(EmitContext&) {
  199. throw LogicError("Unreachable instruction");
  200. }
  201. Id EmitBindlessImageSampleExplicitLod(EmitContext&) {
  202. throw LogicError("Unreachable instruction");
  203. }
  204. Id EmitBindlessImageSampleDrefImplicitLod(EmitContext&) {
  205. throw LogicError("Unreachable instruction");
  206. }
  207. Id EmitBindlessImageSampleDrefExplicitLod(EmitContext&) {
  208. throw LogicError("Unreachable instruction");
  209. }
  210. Id EmitBindlessImageGather(EmitContext&) {
  211. throw LogicError("Unreachable instruction");
  212. }
  213. Id EmitBindlessImageGatherDref(EmitContext&) {
  214. throw LogicError("Unreachable instruction");
  215. }
  216. Id EmitBindlessImageFetch(EmitContext&) {
  217. throw LogicError("Unreachable instruction");
  218. }
  219. Id EmitBindlessImageQueryDimensions(EmitContext&) {
  220. throw LogicError("Unreachable instruction");
  221. }
  222. Id EmitBindlessImageQueryLod(EmitContext&) {
  223. throw LogicError("Unreachable instruction");
  224. }
  225. Id EmitBindlessImageGradient(EmitContext&) {
  226. throw LogicError("Unreachable instruction");
  227. }
  228. Id EmitBindlessImageRead(EmitContext&) {
  229. throw LogicError("Unreachable instruction");
  230. }
  231. Id EmitBindlessImageWrite(EmitContext&) {
  232. throw LogicError("Unreachable instruction");
  233. }
  234. Id EmitBoundImageSampleImplicitLod(EmitContext&) {
  235. throw LogicError("Unreachable instruction");
  236. }
  237. Id EmitBoundImageSampleExplicitLod(EmitContext&) {
  238. throw LogicError("Unreachable instruction");
  239. }
  240. Id EmitBoundImageSampleDrefImplicitLod(EmitContext&) {
  241. throw LogicError("Unreachable instruction");
  242. }
  243. Id EmitBoundImageSampleDrefExplicitLod(EmitContext&) {
  244. throw LogicError("Unreachable instruction");
  245. }
  246. Id EmitBoundImageGather(EmitContext&) {
  247. throw LogicError("Unreachable instruction");
  248. }
  249. Id EmitBoundImageGatherDref(EmitContext&) {
  250. throw LogicError("Unreachable instruction");
  251. }
  252. Id EmitBoundImageFetch(EmitContext&) {
  253. throw LogicError("Unreachable instruction");
  254. }
  255. Id EmitBoundImageQueryDimensions(EmitContext&) {
  256. throw LogicError("Unreachable instruction");
  257. }
  258. Id EmitBoundImageQueryLod(EmitContext&) {
  259. throw LogicError("Unreachable instruction");
  260. }
  261. Id EmitBoundImageGradient(EmitContext&) {
  262. throw LogicError("Unreachable instruction");
  263. }
  264. Id EmitBoundImageRead(EmitContext&) {
  265. throw LogicError("Unreachable instruction");
  266. }
  267. Id EmitBoundImageWrite(EmitContext&) {
  268. throw LogicError("Unreachable instruction");
  269. }
  270. Id EmitImageSampleImplicitLod(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords,
  271. Id bias_lc, const IR::Value& offset) {
  272. const auto info{inst->Flags<IR::TextureInstInfo>()};
  273. if (ctx.stage == Stage::Fragment) {
  274. const ImageOperands operands(ctx, info.has_bias != 0, false, info.has_lod_clamp != 0,
  275. bias_lc, offset);
  276. return Emit(&EmitContext::OpImageSparseSampleImplicitLod,
  277. &EmitContext::OpImageSampleImplicitLod, ctx, inst, ctx.F32[4],
  278. Texture(ctx, info, index), coords, operands.Mask(), operands.Span());
  279. } else {
  280. // We can't use implicit lods on non-fragment stages on SPIR-V. Maxwell hardware behaves as
  281. // if the lod was explicitly zero. This may change on Turing with implicit compute
  282. // derivatives
  283. const Id lod{ctx.Const(0.0f)};
  284. const ImageOperands operands(ctx, false, true, info.has_lod_clamp != 0, lod, offset);
  285. return Emit(&EmitContext::OpImageSparseSampleExplicitLod,
  286. &EmitContext::OpImageSampleExplicitLod, ctx, inst, ctx.F32[4],
  287. Texture(ctx, info, index), coords, operands.Mask(), operands.Span());
  288. }
  289. }
  290. Id EmitImageSampleExplicitLod(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords,
  291. Id lod_lc, const IR::Value& offset) {
  292. const auto info{inst->Flags<IR::TextureInstInfo>()};
  293. const ImageOperands operands(ctx, false, true, info.has_lod_clamp != 0, lod_lc, offset);
  294. return Emit(&EmitContext::OpImageSparseSampleExplicitLod,
  295. &EmitContext::OpImageSampleExplicitLod, ctx, inst, ctx.F32[4],
  296. Texture(ctx, info, index), coords, operands.Mask(), operands.Span());
  297. }
  298. Id EmitImageSampleDrefImplicitLod(EmitContext& ctx, IR::Inst* inst, const IR::Value& index,
  299. Id coords, Id dref, Id bias_lc, const IR::Value& offset) {
  300. const auto info{inst->Flags<IR::TextureInstInfo>()};
  301. const ImageOperands operands(ctx, info.has_bias != 0, false, info.has_lod_clamp != 0, bias_lc,
  302. offset);
  303. return Emit(&EmitContext::OpImageSparseSampleDrefImplicitLod,
  304. &EmitContext::OpImageSampleDrefImplicitLod, ctx, inst, ctx.F32[1],
  305. Texture(ctx, info, index), coords, dref, operands.Mask(), operands.Span());
  306. }
  307. Id EmitImageSampleDrefExplicitLod(EmitContext& ctx, IR::Inst* inst, const IR::Value& index,
  308. Id coords, Id dref, Id lod_lc, const IR::Value& offset) {
  309. const auto info{inst->Flags<IR::TextureInstInfo>()};
  310. const ImageOperands operands(ctx, false, true, info.has_lod_clamp != 0, lod_lc, offset);
  311. return Emit(&EmitContext::OpImageSparseSampleDrefExplicitLod,
  312. &EmitContext::OpImageSampleDrefExplicitLod, ctx, inst, ctx.F32[1],
  313. Texture(ctx, info, index), coords, dref, operands.Mask(), operands.Span());
  314. }
  315. Id EmitImageGather(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords,
  316. const IR::Value& offset, const IR::Value& offset2) {
  317. const auto info{inst->Flags<IR::TextureInstInfo>()};
  318. const ImageOperands operands(ctx, offset, offset2);
  319. return Emit(&EmitContext::OpImageSparseGather, &EmitContext::OpImageGather, ctx, inst,
  320. ctx.F32[4], Texture(ctx, info, index), coords, ctx.Const(info.gather_component),
  321. operands.Mask(), operands.Span());
  322. }
  323. Id EmitImageGatherDref(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords,
  324. const IR::Value& offset, const IR::Value& offset2, Id dref) {
  325. const auto info{inst->Flags<IR::TextureInstInfo>()};
  326. const ImageOperands operands(ctx, offset, offset2);
  327. return Emit(&EmitContext::OpImageSparseDrefGather, &EmitContext::OpImageDrefGather, ctx, inst,
  328. ctx.F32[4], Texture(ctx, info, index), coords, dref, operands.Mask(),
  329. operands.Span());
  330. }
  331. Id EmitImageFetch(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id offset,
  332. Id lod, Id ms) {
  333. const auto info{inst->Flags<IR::TextureInstInfo>()};
  334. if (info.type == TextureType::Buffer) {
  335. lod = Id{};
  336. }
  337. const ImageOperands operands(offset, lod, ms);
  338. return Emit(&EmitContext::OpImageSparseFetch, &EmitContext::OpImageFetch, ctx, inst, ctx.F32[4],
  339. TextureImage(ctx, info, index), coords, operands.Mask(), operands.Span());
  340. }
  341. Id EmitImageQueryDimensions(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id lod) {
  342. const auto info{inst->Flags<IR::TextureInstInfo>()};
  343. const Id image{TextureImage(ctx, info, index)};
  344. const Id zero{ctx.u32_zero_value};
  345. const auto mips{[&] { return ctx.OpImageQueryLevels(ctx.U32[1], image); }};
  346. switch (info.type) {
  347. case TextureType::Color1D:
  348. return ctx.OpCompositeConstruct(ctx.U32[4], ctx.OpImageQuerySizeLod(ctx.U32[1], image, lod),
  349. zero, zero, mips());
  350. case TextureType::ColorArray1D:
  351. case TextureType::Color2D:
  352. case TextureType::ColorCube:
  353. return ctx.OpCompositeConstruct(ctx.U32[4], ctx.OpImageQuerySizeLod(ctx.U32[2], image, lod),
  354. zero, mips());
  355. case TextureType::ColorArray2D:
  356. case TextureType::Color3D:
  357. case TextureType::ColorArrayCube:
  358. return ctx.OpCompositeConstruct(ctx.U32[4], ctx.OpImageQuerySizeLod(ctx.U32[3], image, lod),
  359. mips());
  360. case TextureType::Buffer:
  361. return ctx.OpCompositeConstruct(ctx.U32[4], ctx.OpImageQuerySize(ctx.U32[1], image), zero,
  362. zero, mips());
  363. }
  364. throw LogicError("Unspecified image type {}", info.type.Value());
  365. }
  366. Id EmitImageQueryLod(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords) {
  367. const auto info{inst->Flags<IR::TextureInstInfo>()};
  368. const Id zero{ctx.f32_zero_value};
  369. const Id sampler{Texture(ctx, info, index)};
  370. return ctx.OpCompositeConstruct(ctx.F32[4], ctx.OpImageQueryLod(ctx.F32[2], sampler, coords),
  371. zero, zero);
  372. }
  373. Id EmitImageGradient(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords,
  374. Id derivates, Id offset, Id lod_clamp) {
  375. const auto info{inst->Flags<IR::TextureInstInfo>()};
  376. const ImageOperands operands(ctx, info.has_lod_clamp != 0, derivates, info.num_derivates,
  377. offset, lod_clamp);
  378. return Emit(&EmitContext::OpImageSparseSampleExplicitLod,
  379. &EmitContext::OpImageSampleExplicitLod, ctx, inst, ctx.F32[4],
  380. Texture(ctx, info, index), coords, operands.Mask(), operands.Span());
  381. }
  382. Id EmitImageRead(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords) {
  383. const auto info{inst->Flags<IR::TextureInstInfo>()};
  384. if (info.image_format == ImageFormat::Typeless && !ctx.profile.support_typeless_image_loads) {
  385. // LOG_WARNING(..., "Typeless image read not supported by host");
  386. return ctx.ConstantNull(ctx.U32[4]);
  387. }
  388. return Emit(&EmitContext::OpImageSparseRead, &EmitContext::OpImageRead, ctx, inst, ctx.U32[4],
  389. Image(ctx, index, info), coords, std::nullopt, std::span<const Id>{});
  390. }
  391. void EmitImageWrite(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id color) {
  392. const auto info{inst->Flags<IR::TextureInstInfo>()};
  393. ctx.OpImageWrite(Image(ctx, index, info), coords, color);
  394. }
  395. } // namespace Shader::Backend::SPIRV