emit_spirv_image.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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, const IR::Value& index) {
  137. if (index.IsImmediate()) {
  138. const TextureDefinition def{ctx.textures.at(index.U32())};
  139. return ctx.OpLoad(def.sampled_type, def.id);
  140. }
  141. throw NotImplementedException("Indirect texture sample");
  142. }
  143. Id TextureImage(EmitContext& ctx, const IR::Value& index, IR::TextureInstInfo info) {
  144. if (!index.IsImmediate()) {
  145. throw NotImplementedException("Indirect texture sample");
  146. }
  147. if (info.type == TextureType::Buffer) {
  148. const Id sampler_id{ctx.texture_buffers.at(index.U32())};
  149. const Id id{ctx.OpLoad(ctx.sampled_texture_buffer_type, sampler_id)};
  150. return ctx.OpImage(ctx.image_buffer_type, id);
  151. } else {
  152. const TextureDefinition def{ctx.textures.at(index.U32())};
  153. return ctx.OpImage(def.image_type, ctx.OpLoad(def.sampled_type, def.id));
  154. }
  155. }
  156. Id Image(EmitContext& ctx, const IR::Value& index, IR::TextureInstInfo info) {
  157. if (!index.IsImmediate()) {
  158. throw NotImplementedException("Indirect image indexing");
  159. }
  160. if (info.type == TextureType::Buffer) {
  161. const ImageBufferDefinition def{ctx.image_buffers.at(index.U32())};
  162. return ctx.OpLoad(def.image_type, def.id);
  163. } else {
  164. const ImageDefinition def{ctx.images.at(index.U32())};
  165. return ctx.OpLoad(def.image_type, def.id);
  166. }
  167. }
  168. Id Decorate(EmitContext& ctx, IR::Inst* inst, Id sample) {
  169. const auto info{inst->Flags<IR::TextureInstInfo>()};
  170. if (info.relaxed_precision != 0) {
  171. ctx.Decorate(sample, spv::Decoration::RelaxedPrecision);
  172. }
  173. return sample;
  174. }
  175. template <typename MethodPtrType, typename... Args>
  176. Id Emit(MethodPtrType sparse_ptr, MethodPtrType non_sparse_ptr, EmitContext& ctx, IR::Inst* inst,
  177. Id result_type, Args&&... args) {
  178. IR::Inst* const sparse{inst->GetAssociatedPseudoOperation(IR::Opcode::GetSparseFromOp)};
  179. if (!sparse) {
  180. return Decorate(ctx, inst, (ctx.*non_sparse_ptr)(result_type, std::forward<Args>(args)...));
  181. }
  182. const Id struct_type{ctx.TypeStruct(ctx.U32[1], result_type)};
  183. const Id sample{(ctx.*sparse_ptr)(struct_type, std::forward<Args>(args)...)};
  184. const Id resident_code{ctx.OpCompositeExtract(ctx.U32[1], sample, 0U)};
  185. sparse->SetDefinition(ctx.OpImageSparseTexelsResident(ctx.U1, resident_code));
  186. sparse->Invalidate();
  187. Decorate(ctx, inst, sample);
  188. return ctx.OpCompositeExtract(result_type, sample, 1U);
  189. }
  190. } // Anonymous namespace
  191. Id EmitBindlessImageSampleImplicitLod(EmitContext&) {
  192. throw LogicError("Unreachable instruction");
  193. }
  194. Id EmitBindlessImageSampleExplicitLod(EmitContext&) {
  195. throw LogicError("Unreachable instruction");
  196. }
  197. Id EmitBindlessImageSampleDrefImplicitLod(EmitContext&) {
  198. throw LogicError("Unreachable instruction");
  199. }
  200. Id EmitBindlessImageSampleDrefExplicitLod(EmitContext&) {
  201. throw LogicError("Unreachable instruction");
  202. }
  203. Id EmitBindlessImageGather(EmitContext&) {
  204. throw LogicError("Unreachable instruction");
  205. }
  206. Id EmitBindlessImageGatherDref(EmitContext&) {
  207. throw LogicError("Unreachable instruction");
  208. }
  209. Id EmitBindlessImageFetch(EmitContext&) {
  210. throw LogicError("Unreachable instruction");
  211. }
  212. Id EmitBindlessImageQueryDimensions(EmitContext&) {
  213. throw LogicError("Unreachable instruction");
  214. }
  215. Id EmitBindlessImageQueryLod(EmitContext&) {
  216. throw LogicError("Unreachable instruction");
  217. }
  218. Id EmitBindlessImageGradient(EmitContext&) {
  219. throw LogicError("Unreachable instruction");
  220. }
  221. Id EmitBindlessImageRead(EmitContext&) {
  222. throw LogicError("Unreachable instruction");
  223. }
  224. Id EmitBindlessImageWrite(EmitContext&) {
  225. throw LogicError("Unreachable instruction");
  226. }
  227. Id EmitBoundImageSampleImplicitLod(EmitContext&) {
  228. throw LogicError("Unreachable instruction");
  229. }
  230. Id EmitBoundImageSampleExplicitLod(EmitContext&) {
  231. throw LogicError("Unreachable instruction");
  232. }
  233. Id EmitBoundImageSampleDrefImplicitLod(EmitContext&) {
  234. throw LogicError("Unreachable instruction");
  235. }
  236. Id EmitBoundImageSampleDrefExplicitLod(EmitContext&) {
  237. throw LogicError("Unreachable instruction");
  238. }
  239. Id EmitBoundImageGather(EmitContext&) {
  240. throw LogicError("Unreachable instruction");
  241. }
  242. Id EmitBoundImageGatherDref(EmitContext&) {
  243. throw LogicError("Unreachable instruction");
  244. }
  245. Id EmitBoundImageFetch(EmitContext&) {
  246. throw LogicError("Unreachable instruction");
  247. }
  248. Id EmitBoundImageQueryDimensions(EmitContext&) {
  249. throw LogicError("Unreachable instruction");
  250. }
  251. Id EmitBoundImageQueryLod(EmitContext&) {
  252. throw LogicError("Unreachable instruction");
  253. }
  254. Id EmitBoundImageGradient(EmitContext&) {
  255. throw LogicError("Unreachable instruction");
  256. }
  257. Id EmitBoundImageRead(EmitContext&) {
  258. throw LogicError("Unreachable instruction");
  259. }
  260. Id EmitBoundImageWrite(EmitContext&) {
  261. throw LogicError("Unreachable instruction");
  262. }
  263. Id EmitImageSampleImplicitLod(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords,
  264. Id bias_lc, const IR::Value& offset) {
  265. const auto info{inst->Flags<IR::TextureInstInfo>()};
  266. if (ctx.stage == Stage::Fragment) {
  267. const ImageOperands operands(ctx, info.has_bias != 0, false, info.has_lod_clamp != 0,
  268. bias_lc, offset);
  269. return Emit(&EmitContext::OpImageSparseSampleImplicitLod,
  270. &EmitContext::OpImageSampleImplicitLod, ctx, inst, ctx.F32[4],
  271. Texture(ctx, index), coords, operands.Mask(), operands.Span());
  272. } else {
  273. // We can't use implicit lods on non-fragment stages on SPIR-V. Maxwell hardware behaves as
  274. // if the lod was explicitly zero. This may change on Turing with implicit compute
  275. // derivatives
  276. const Id lod{ctx.Const(0.0f)};
  277. const ImageOperands operands(ctx, false, true, info.has_lod_clamp != 0, lod, offset);
  278. return Emit(&EmitContext::OpImageSparseSampleExplicitLod,
  279. &EmitContext::OpImageSampleExplicitLod, ctx, inst, ctx.F32[4],
  280. Texture(ctx, index), coords, operands.Mask(), operands.Span());
  281. }
  282. }
  283. Id EmitImageSampleExplicitLod(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords,
  284. Id lod_lc, const IR::Value& offset) {
  285. const auto info{inst->Flags<IR::TextureInstInfo>()};
  286. const ImageOperands operands(ctx, false, true, info.has_lod_clamp != 0, lod_lc, offset);
  287. return Emit(&EmitContext::OpImageSparseSampleExplicitLod,
  288. &EmitContext::OpImageSampleExplicitLod, ctx, inst, ctx.F32[4], Texture(ctx, index),
  289. coords, operands.Mask(), operands.Span());
  290. }
  291. Id EmitImageSampleDrefImplicitLod(EmitContext& ctx, IR::Inst* inst, const IR::Value& index,
  292. Id coords, Id dref, Id bias_lc, const IR::Value& offset) {
  293. const auto info{inst->Flags<IR::TextureInstInfo>()};
  294. const ImageOperands operands(ctx, info.has_bias != 0, false, info.has_lod_clamp != 0, bias_lc,
  295. offset);
  296. return Emit(&EmitContext::OpImageSparseSampleDrefImplicitLod,
  297. &EmitContext::OpImageSampleDrefImplicitLod, ctx, inst, ctx.F32[1],
  298. Texture(ctx, index), coords, dref, operands.Mask(), operands.Span());
  299. }
  300. Id EmitImageSampleDrefExplicitLod(EmitContext& ctx, IR::Inst* inst, const IR::Value& index,
  301. Id coords, Id dref, Id lod_lc, const IR::Value& offset) {
  302. const auto info{inst->Flags<IR::TextureInstInfo>()};
  303. const ImageOperands operands(ctx, false, true, info.has_lod_clamp != 0, lod_lc, offset);
  304. return Emit(&EmitContext::OpImageSparseSampleDrefExplicitLod,
  305. &EmitContext::OpImageSampleDrefExplicitLod, ctx, inst, ctx.F32[1],
  306. Texture(ctx, index), coords, dref, operands.Mask(), operands.Span());
  307. }
  308. Id EmitImageGather(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords,
  309. const IR::Value& offset, const IR::Value& offset2) {
  310. const auto info{inst->Flags<IR::TextureInstInfo>()};
  311. const ImageOperands operands(ctx, offset, offset2);
  312. return Emit(&EmitContext::OpImageSparseGather, &EmitContext::OpImageGather, ctx, inst,
  313. ctx.F32[4], Texture(ctx, index), coords, ctx.Const(info.gather_component),
  314. operands.Mask(), operands.Span());
  315. }
  316. Id EmitImageGatherDref(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords,
  317. const IR::Value& offset, const IR::Value& offset2, Id dref) {
  318. const ImageOperands operands(ctx, offset, offset2);
  319. return Emit(&EmitContext::OpImageSparseDrefGather, &EmitContext::OpImageDrefGather, ctx, inst,
  320. ctx.F32[4], Texture(ctx, index), coords, dref, operands.Mask(), operands.Span());
  321. }
  322. Id EmitImageFetch(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id offset,
  323. Id lod, Id ms) {
  324. const auto info{inst->Flags<IR::TextureInstInfo>()};
  325. if (info.type == TextureType::Buffer) {
  326. lod = Id{};
  327. }
  328. const ImageOperands operands(offset, lod, ms);
  329. return Emit(&EmitContext::OpImageSparseFetch, &EmitContext::OpImageFetch, ctx, inst, ctx.F32[4],
  330. TextureImage(ctx, index, info), coords, operands.Mask(), operands.Span());
  331. }
  332. Id EmitImageQueryDimensions(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id lod) {
  333. const auto info{inst->Flags<IR::TextureInstInfo>()};
  334. const Id image{TextureImage(ctx, index, info)};
  335. const Id zero{ctx.u32_zero_value};
  336. const auto mips{[&] { return ctx.OpImageQueryLevels(ctx.U32[1], image); }};
  337. switch (info.type) {
  338. case TextureType::Color1D:
  339. return ctx.OpCompositeConstruct(ctx.U32[4], ctx.OpImageQuerySizeLod(ctx.U32[1], image, lod),
  340. zero, zero, mips());
  341. case TextureType::ColorArray1D:
  342. case TextureType::Color2D:
  343. case TextureType::ColorCube:
  344. return ctx.OpCompositeConstruct(ctx.U32[4], ctx.OpImageQuerySizeLod(ctx.U32[2], image, lod),
  345. zero, mips());
  346. case TextureType::ColorArray2D:
  347. case TextureType::Color3D:
  348. case TextureType::ColorArrayCube:
  349. return ctx.OpCompositeConstruct(ctx.U32[4], ctx.OpImageQuerySizeLod(ctx.U32[3], image, lod),
  350. mips());
  351. case TextureType::Buffer:
  352. return ctx.OpCompositeConstruct(ctx.U32[4], ctx.OpImageQuerySize(ctx.U32[1], image), zero,
  353. zero, mips());
  354. }
  355. throw LogicError("Unspecified image type {}", info.type.Value());
  356. }
  357. Id EmitImageQueryLod(EmitContext& ctx, IR::Inst*, const IR::Value& index, Id coords) {
  358. const Id zero{ctx.f32_zero_value};
  359. const Id sampler{Texture(ctx, index)};
  360. return ctx.OpCompositeConstruct(ctx.F32[4], ctx.OpImageQueryLod(ctx.F32[2], sampler, coords),
  361. zero, zero);
  362. }
  363. Id EmitImageGradient(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords,
  364. Id derivates, Id offset, Id lod_clamp) {
  365. const auto info{inst->Flags<IR::TextureInstInfo>()};
  366. const ImageOperands operands(ctx, info.has_lod_clamp != 0, derivates, info.num_derivates,
  367. offset, lod_clamp);
  368. return Emit(&EmitContext::OpImageSparseSampleExplicitLod,
  369. &EmitContext::OpImageSampleExplicitLod, ctx, inst, ctx.F32[4], Texture(ctx, index),
  370. coords, operands.Mask(), operands.Span());
  371. }
  372. Id EmitImageRead(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords) {
  373. const auto info{inst->Flags<IR::TextureInstInfo>()};
  374. if (info.image_format == ImageFormat::Typeless && !ctx.profile.support_typeless_image_loads) {
  375. // LOG_WARNING(..., "Typeless image read not supported by host");
  376. return ctx.ConstantNull(ctx.U32[4]);
  377. }
  378. return Emit(&EmitContext::OpImageSparseRead, &EmitContext::OpImageRead, ctx, inst, ctx.U32[4],
  379. Image(ctx, index, info), coords, std::nullopt, std::span<const Id>{});
  380. }
  381. void EmitImageWrite(EmitContext& ctx, IR::Inst* inst, const IR::Value& index, Id coords, Id color) {
  382. const auto info{inst->Flags<IR::TextureInstInfo>()};
  383. ctx.OpImageWrite(Image(ctx, index, info), coords, color);
  384. }
  385. } // namespace Shader::Backend::SPIRV