texture_fetch.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <optional>
  5. #include "common/bit_field.h"
  6. #include "common/common_types.h"
  7. #include "shader_recompiler/frontend/ir/modifiers.h"
  8. #include "shader_recompiler/frontend/maxwell/translate/impl/impl.h"
  9. namespace Shader::Maxwell {
  10. namespace {
  11. enum class Blod : u64 {
  12. None,
  13. LZ,
  14. LB,
  15. LL,
  16. INVALIDBLOD4,
  17. INVALIDBLOD5,
  18. LBA,
  19. LLA,
  20. };
  21. enum class TextureType : u64 {
  22. _1D,
  23. ARRAY_1D,
  24. _2D,
  25. ARRAY_2D,
  26. _3D,
  27. ARRAY_3D,
  28. CUBE,
  29. ARRAY_CUBE,
  30. };
  31. Shader::TextureType GetType(TextureType type, bool dc) {
  32. switch (type) {
  33. case TextureType::_1D:
  34. return dc ? Shader::TextureType::Shadow1D : Shader::TextureType::Color1D;
  35. case TextureType::ARRAY_1D:
  36. return dc ? Shader::TextureType::ShadowArray1D : Shader::TextureType::ColorArray1D;
  37. case TextureType::_2D:
  38. return dc ? Shader::TextureType::Shadow2D : Shader::TextureType::Color2D;
  39. case TextureType::ARRAY_2D:
  40. return dc ? Shader::TextureType::ShadowArray2D : Shader::TextureType::ColorArray2D;
  41. case TextureType::_3D:
  42. return dc ? Shader::TextureType::Shadow3D : Shader::TextureType::Color3D;
  43. case TextureType::ARRAY_3D:
  44. throw NotImplementedException("3D array texture type");
  45. case TextureType::CUBE:
  46. return dc ? Shader::TextureType::ShadowCube : Shader::TextureType::ColorCube;
  47. case TextureType::ARRAY_CUBE:
  48. return dc ? Shader::TextureType::ShadowArrayCube : Shader::TextureType::ColorArrayCube;
  49. }
  50. throw NotImplementedException("Invalid texture type {}", type);
  51. }
  52. IR::Value MakeCoords(TranslatorVisitor& v, IR::Reg reg, TextureType type) {
  53. const auto read_array{[&]() -> IR::F32 { return v.ir.ConvertUToF(32, v.X(reg)); }};
  54. switch (type) {
  55. case TextureType::_1D:
  56. return v.F(reg);
  57. case TextureType::ARRAY_1D:
  58. return v.ir.CompositeConstruct(read_array(), v.F(reg + 1));
  59. case TextureType::_2D:
  60. return v.ir.CompositeConstruct(v.F(reg), v.F(reg + 1));
  61. case TextureType::ARRAY_2D:
  62. return v.ir.CompositeConstruct(read_array(), v.F(reg + 1), v.F(reg + 2));
  63. case TextureType::_3D:
  64. return v.ir.CompositeConstruct(v.F(reg), v.F(reg + 1), v.F(reg + 2));
  65. case TextureType::ARRAY_3D:
  66. throw NotImplementedException("3D array texture type");
  67. case TextureType::CUBE:
  68. return v.ir.CompositeConstruct(v.F(reg), v.F(reg + 1), v.F(reg + 2));
  69. case TextureType::ARRAY_CUBE:
  70. return v.ir.CompositeConstruct(read_array(), v.F(reg + 1), v.F(reg + 2), v.F(reg + 3));
  71. }
  72. throw NotImplementedException("Invalid texture type {}", type);
  73. }
  74. IR::F32 MakeLod(TranslatorVisitor& v, IR::Reg& reg, Blod blod) {
  75. switch (blod) {
  76. case Blod::None:
  77. return v.ir.Imm32(0.0f);
  78. case Blod::LZ:
  79. return v.ir.Imm32(0.0f);
  80. case Blod::LB:
  81. case Blod::LL:
  82. case Blod::LBA:
  83. case Blod::LLA:
  84. return v.F(reg++);
  85. case Blod::INVALIDBLOD4:
  86. case Blod::INVALIDBLOD5:
  87. break;
  88. }
  89. throw NotImplementedException("Invalid blod {}", blod);
  90. }
  91. IR::Value MakeOffset(TranslatorVisitor& v, IR::Reg& reg, TextureType type) {
  92. const IR::U32 value{v.X(reg++)};
  93. switch (type) {
  94. case TextureType::_1D:
  95. case TextureType::ARRAY_1D:
  96. return v.ir.BitFieldExtract(value, v.ir.Imm32(0), v.ir.Imm32(4));
  97. case TextureType::_2D:
  98. case TextureType::ARRAY_2D:
  99. return v.ir.CompositeConstruct(v.ir.BitFieldExtract(value, v.ir.Imm32(0), v.ir.Imm32(4)),
  100. v.ir.BitFieldExtract(value, v.ir.Imm32(4), v.ir.Imm32(4)));
  101. case TextureType::_3D:
  102. case TextureType::ARRAY_3D:
  103. return v.ir.CompositeConstruct(v.ir.BitFieldExtract(value, v.ir.Imm32(0), v.ir.Imm32(4)),
  104. v.ir.BitFieldExtract(value, v.ir.Imm32(4), v.ir.Imm32(4)),
  105. v.ir.BitFieldExtract(value, v.ir.Imm32(8), v.ir.Imm32(4)));
  106. case TextureType::CUBE:
  107. case TextureType::ARRAY_CUBE:
  108. throw NotImplementedException("Illegal offset on CUBE sample");
  109. }
  110. throw NotImplementedException("Invalid texture type {}", type);
  111. }
  112. bool HasExplicitLod(Blod blod) {
  113. switch (blod) {
  114. case Blod::LL:
  115. case Blod::LLA:
  116. case Blod::LZ:
  117. return true;
  118. default:
  119. return false;
  120. }
  121. }
  122. void Impl(TranslatorVisitor& v, u64 insn, bool aoffi, Blod blod, bool lc,
  123. std::optional<u32> cbuf_offset) {
  124. union {
  125. u64 raw;
  126. BitField<35, 1, u64> ndv;
  127. BitField<49, 1, u64> nodep;
  128. BitField<50, 1, u64> dc;
  129. BitField<51, 3, IR::Pred> sparse_pred;
  130. BitField<0, 8, IR::Reg> dest_reg;
  131. BitField<8, 8, IR::Reg> coord_reg;
  132. BitField<20, 8, IR::Reg> meta_reg;
  133. BitField<28, 3, TextureType> type;
  134. BitField<31, 4, u64> mask;
  135. } const tex{insn};
  136. if (lc) {
  137. throw NotImplementedException("LC");
  138. }
  139. const IR::Value coords{MakeCoords(v, tex.coord_reg, tex.type)};
  140. IR::Reg meta_reg{tex.meta_reg};
  141. IR::Value handle;
  142. IR::Value offset;
  143. IR::F32 dref;
  144. IR::F32 lod_clamp;
  145. if (cbuf_offset) {
  146. handle = v.ir.Imm32(*cbuf_offset);
  147. } else {
  148. handle = v.X(meta_reg++);
  149. }
  150. const IR::F32 lod{MakeLod(v, meta_reg, blod)};
  151. if (aoffi) {
  152. offset = MakeOffset(v, meta_reg, tex.type);
  153. }
  154. if (tex.dc != 0) {
  155. dref = v.F(meta_reg++);
  156. }
  157. IR::TextureInstInfo info{};
  158. info.type.Assign(GetType(tex.type, tex.dc != 0));
  159. info.has_bias.Assign(blod == Blod::LB || blod == Blod::LBA ? 1 : 0);
  160. info.has_lod_clamp.Assign(lc ? 1 : 0);
  161. const IR::Value sample{[&]() -> IR::Value {
  162. if (tex.dc == 0) {
  163. if (HasExplicitLod(blod)) {
  164. return v.ir.ImageSampleExplicitLod(handle, coords, lod, offset, lod_clamp, info);
  165. } else {
  166. return v.ir.ImageSampleImplicitLod(handle, coords, lod, offset, lod_clamp, info);
  167. }
  168. }
  169. if (HasExplicitLod(blod)) {
  170. return v.ir.ImageSampleDrefExplicitLod(handle, coords, dref, lod, offset, lod_clamp,
  171. info);
  172. } else {
  173. return v.ir.ImageSampleDrefImplicitLod(handle, coords, dref, lod, offset, lod_clamp,
  174. info);
  175. }
  176. }()};
  177. for (int element = 0; element < 4; ++element) {
  178. if (((tex.mask >> element) & 1) == 0) {
  179. continue;
  180. }
  181. IR::F32 value;
  182. if (tex.dc != 0) {
  183. value = element < 3 ? IR::F32{sample} : v.ir.Imm32(1.0f);
  184. } else {
  185. value = IR::F32{v.ir.CompositeExtract(sample, element)};
  186. }
  187. v.F(tex.dest_reg + element, value);
  188. }
  189. if (tex.sparse_pred != IR::Pred::PT) {
  190. v.ir.SetPred(tex.sparse_pred, v.ir.LogicalNot(v.ir.GetSparseFromOp(sample)));
  191. }
  192. }
  193. } // Anonymous namespace
  194. void TranslatorVisitor::TEX(u64 insn) {
  195. union {
  196. u64 raw;
  197. BitField<54, 1, u64> aoffi;
  198. BitField<55, 3, Blod> blod;
  199. BitField<58, 1, u64> lc;
  200. BitField<36, 13, u64> cbuf_offset;
  201. } const tex{insn};
  202. Impl(*this, insn, tex.aoffi != 0, tex.blod, tex.lc != 0, static_cast<u32>(tex.cbuf_offset));
  203. }
  204. void TranslatorVisitor::TEX_b(u64 insn) {
  205. union {
  206. u64 raw;
  207. BitField<36, 1, u64> aoffi;
  208. BitField<37, 3, Blod> blod;
  209. BitField<40, 1, u64> lc;
  210. } const tex{insn};
  211. Impl(*this, insn, tex.aoffi != 0, tex.blod, tex.lc != 0, std::nullopt);
  212. }
  213. } // namespace Shader::Maxwell