texture_gradient.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 TextureType : u64 {
  12. _1D,
  13. ARRAY_1D,
  14. _2D,
  15. ARRAY_2D,
  16. _3D,
  17. ARRAY_3D,
  18. CUBE,
  19. ARRAY_CUBE,
  20. };
  21. Shader::TextureType GetType(TextureType type, bool dc) {
  22. switch (type) {
  23. case TextureType::_1D:
  24. return dc ? Shader::TextureType::Shadow1D : Shader::TextureType::Color1D;
  25. case TextureType::ARRAY_1D:
  26. return dc ? Shader::TextureType::ShadowArray1D : Shader::TextureType::ColorArray1D;
  27. case TextureType::_2D:
  28. return dc ? Shader::TextureType::Shadow2D : Shader::TextureType::Color2D;
  29. case TextureType::ARRAY_2D:
  30. return dc ? Shader::TextureType::ShadowArray2D : Shader::TextureType::ColorArray2D;
  31. case TextureType::_3D:
  32. return dc ? Shader::TextureType::Shadow3D : Shader::TextureType::Color3D;
  33. case TextureType::ARRAY_3D:
  34. throw NotImplementedException("3D array texture type");
  35. case TextureType::CUBE:
  36. return dc ? Shader::TextureType::ShadowCube : Shader::TextureType::ColorCube;
  37. case TextureType::ARRAY_CUBE:
  38. return dc ? Shader::TextureType::ShadowArrayCube : Shader::TextureType::ColorArrayCube;
  39. }
  40. throw NotImplementedException("Invalid texture type {}", type);
  41. }
  42. IR::Value MakeOffset(TranslatorVisitor& v, IR::Reg reg, bool has_lod_clamp) {
  43. const IR::U32 value{v.X(reg)};
  44. const u32 base{has_lod_clamp ? 12U : 16U};
  45. return v.ir.CompositeConstruct(
  46. v.ir.BitFieldExtract(value, v.ir.Imm32(base), v.ir.Imm32(4), true),
  47. v.ir.BitFieldExtract(value, v.ir.Imm32(base + 4), v.ir.Imm32(4), true));
  48. }
  49. void Impl(TranslatorVisitor& v, u64 insn, bool is_bindless) {
  50. union {
  51. u64 raw;
  52. BitField<49, 1, u64> nodep;
  53. BitField<35, 1, u64> aoffi;
  54. BitField<50, 1, u64> lc;
  55. BitField<51, 3, IR::Pred> sparse_pred;
  56. BitField<0, 8, IR::Reg> dest_reg;
  57. BitField<8, 8, IR::Reg> coord_reg;
  58. BitField<20, 8, IR::Reg> derivate_reg;
  59. BitField<28, 3, TextureType> type;
  60. BitField<31, 4, u64> mask;
  61. BitField<36, 13, u64> cbuf_offset;
  62. } const txd{insn};
  63. const bool has_lod_clamp = txd.lc != 0;
  64. if (has_lod_clamp) {
  65. throw NotImplementedException("TXD.LC - CLAMP is not implemented");
  66. }
  67. IR::Value coords;
  68. u32 num_derivates{};
  69. IR::Reg base_reg{txd.coord_reg};
  70. IR::Reg last_reg;
  71. IR::Value handle;
  72. if (is_bindless) {
  73. handle = v.X(base_reg++);
  74. } else {
  75. handle = v.ir.Imm32(static_cast<u32>(txd.cbuf_offset.Value() * 4));
  76. }
  77. const auto read_array{[&]() -> IR::F32 {
  78. const IR::U32 base{v.ir.Imm32(0)};
  79. const IR::U32 count{v.ir.Imm32(has_lod_clamp ? 12 : 16)};
  80. const IR::U32 array_index{v.ir.BitFieldExtract(v.X(last_reg), base, count)};
  81. return v.ir.ConvertUToF(32, 16, array_index);
  82. }};
  83. switch (txd.type) {
  84. case TextureType::_1D: {
  85. coords = v.F(base_reg);
  86. num_derivates = 1;
  87. last_reg = base_reg + 1;
  88. break;
  89. }
  90. case TextureType::ARRAY_1D: {
  91. last_reg = base_reg + 1;
  92. coords = v.ir.CompositeConstruct(v.F(base_reg), read_array());
  93. num_derivates = 1;
  94. break;
  95. }
  96. case TextureType::_2D: {
  97. last_reg = base_reg + 2;
  98. coords = v.ir.CompositeConstruct(v.F(base_reg), v.F(base_reg + 1));
  99. num_derivates = 2;
  100. break;
  101. }
  102. case TextureType::ARRAY_2D: {
  103. last_reg = base_reg + 2;
  104. coords = v.ir.CompositeConstruct(v.F(base_reg), v.F(base_reg + 1), read_array());
  105. num_derivates = 2;
  106. break;
  107. }
  108. default:
  109. throw NotImplementedException("Invalid texture type");
  110. }
  111. const IR::Reg derivate_reg{txd.derivate_reg};
  112. IR::Value derivates;
  113. switch (num_derivates) {
  114. case 1: {
  115. derivates = v.ir.CompositeConstruct(v.F(derivate_reg), v.F(derivate_reg + 1));
  116. break;
  117. }
  118. case 2: {
  119. derivates = v.ir.CompositeConstruct(v.F(derivate_reg), v.F(derivate_reg + 1),
  120. v.F(derivate_reg + 2), v.F(derivate_reg + 3));
  121. break;
  122. }
  123. default:
  124. throw NotImplementedException("Invalid texture type");
  125. }
  126. IR::Value offset;
  127. if (txd.aoffi != 0) {
  128. offset = MakeOffset(v, last_reg, has_lod_clamp);
  129. }
  130. IR::F32 lod_clamp;
  131. if (has_lod_clamp) {
  132. // Lod Clamp is a Fixed Point 4.8, we need to transform it to float.
  133. // to convert a fixed point, float(value) / float(1 << fixed_point)
  134. // in this case the fixed_point is 8.
  135. const IR::F32 conv4_8fixp_f{v.ir.Imm32(static_cast<f32>(1U << 8))};
  136. const IR::F32 fixp_lc{v.ir.ConvertUToF(
  137. 32, 16, v.ir.BitFieldExtract(v.X(last_reg), v.ir.Imm32(20), v.ir.Imm32(12)))};
  138. lod_clamp = v.ir.FPMul(fixp_lc, conv4_8fixp_f);
  139. }
  140. IR::TextureInstInfo info{};
  141. info.type.Assign(GetType(txd.type, false));
  142. info.num_derivates.Assign(num_derivates);
  143. info.has_lod_clamp.Assign(has_lod_clamp ? 1 : 0);
  144. const IR::Value sample{v.ir.ImageGradient(handle, coords, derivates, offset, lod_clamp, info)};
  145. IR::Reg dest_reg{txd.dest_reg};
  146. for (size_t element = 0; element < 4; ++element) {
  147. if (((txd.mask >> element) & 1) == 0) {
  148. continue;
  149. }
  150. v.F(dest_reg, IR::F32{v.ir.CompositeExtract(sample, element)});
  151. ++dest_reg;
  152. }
  153. if (txd.sparse_pred != IR::Pred::PT) {
  154. v.ir.SetPred(txd.sparse_pred, v.ir.LogicalNot(v.ir.GetSparseFromOp(sample)));
  155. }
  156. }
  157. } // Anonymous namespace
  158. void TranslatorVisitor::TXD(u64 insn) {
  159. Impl(*this, insn, false);
  160. }
  161. void TranslatorVisitor::TXD_b(u64 insn) {
  162. Impl(*this, insn, true);
  163. }
  164. } // namespace Shader::Maxwell