Tev.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright 2009 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include "Common/EnumMap.h"
  6. #include "VideoCommon/BPMemory.h"
  7. class Tev
  8. {
  9. struct TevColor
  10. {
  11. constexpr TevColor() = default;
  12. constexpr explicit TevColor(s16 a_, s16 b_, s16 g_, s16 r_) : a(a_), b(b_), g(g_), r(r_) {}
  13. s16 a = 0;
  14. s16 b = 0;
  15. s16 g = 0;
  16. s16 r = 0;
  17. constexpr static TevColor All(s16 value) { return TevColor(value, value, value, value); }
  18. constexpr s16& operator[](int index)
  19. {
  20. switch (index)
  21. {
  22. case ALP_C:
  23. return a;
  24. case BLU_C:
  25. return b;
  26. case GRN_C:
  27. return g;
  28. case RED_C:
  29. return r;
  30. default:
  31. // invalid
  32. return a;
  33. }
  34. }
  35. };
  36. struct TevColorRef
  37. {
  38. constexpr explicit TevColorRef(const s16& r_, const s16& g_, const s16& b_)
  39. : r(r_), g(g_), b(b_)
  40. {
  41. }
  42. const s16& r;
  43. const s16& g;
  44. const s16& b;
  45. constexpr static TevColorRef Color(const TevColor& color)
  46. {
  47. return TevColorRef(color.r, color.g, color.b);
  48. }
  49. constexpr static TevColorRef All(const s16& value) { return TevColorRef(value, value, value); }
  50. constexpr static TevColorRef Alpha(const TevColor& color) { return All(color.a); }
  51. };
  52. struct TevAlphaRef
  53. {
  54. constexpr explicit TevAlphaRef(const TevColor& color) : a(color.a) {}
  55. constexpr explicit TevAlphaRef(const s16& a_) : a(a_) {}
  56. const s16& a;
  57. };
  58. struct TevKonstRef
  59. {
  60. constexpr explicit TevKonstRef(const s16& a_, const s16& r_, const s16& g_, const s16& b_)
  61. : a(a_), r(r_), g(g_), b(b_)
  62. {
  63. }
  64. const s16& a;
  65. const s16& r;
  66. const s16& g;
  67. const s16& b;
  68. constexpr static TevKonstRef Value(const s16& value)
  69. {
  70. return TevKonstRef(value, value, value, value);
  71. }
  72. constexpr static TevKonstRef Konst(const s16& alpha, const TevColor& color)
  73. {
  74. return TevKonstRef(alpha, color.r, color.g, color.b);
  75. }
  76. };
  77. struct InputRegType
  78. {
  79. unsigned a : 8;
  80. unsigned b : 8;
  81. unsigned c : 8;
  82. signed d : 11;
  83. };
  84. struct TextureCoordinateType
  85. {
  86. signed s : 24;
  87. signed t : 24;
  88. };
  89. // color order: ABGR
  90. Common::EnumMap<TevColor, TevOutput::Color2> Reg;
  91. std::array<TevColor, 4> KonstantColors;
  92. TevColor TexColor;
  93. TevColor RasColor;
  94. TevColor StageKonst;
  95. // Fixed constants, corresponding to KonstSel
  96. static constexpr s16 V0 = 0;
  97. static constexpr s16 V1_8 = 32;
  98. static constexpr s16 V1_4 = 64;
  99. static constexpr s16 V3_8 = 96;
  100. static constexpr s16 V1_2 = 128;
  101. static constexpr s16 V5_8 = 159;
  102. static constexpr s16 V3_4 = 191;
  103. static constexpr s16 V7_8 = 223;
  104. static constexpr s16 V1 = 255;
  105. u8 AlphaBump = 0;
  106. u8 IndirectTex[4][4]{};
  107. TextureCoordinateType TexCoord{};
  108. const Common::EnumMap<TevColorRef, TevColorArg::Zero> m_ColorInputLUT{
  109. TevColorRef::Color(Reg[TevOutput::Prev]), // prev.rgb
  110. TevColorRef::Alpha(Reg[TevOutput::Prev]), // prev.aaa
  111. TevColorRef::Color(Reg[TevOutput::Color0]), // c0.rgb
  112. TevColorRef::Alpha(Reg[TevOutput::Color0]), // c0.aaa
  113. TevColorRef::Color(Reg[TevOutput::Color1]), // c1.rgb
  114. TevColorRef::Alpha(Reg[TevOutput::Color1]), // c1.aaa
  115. TevColorRef::Color(Reg[TevOutput::Color2]), // c2.rgb
  116. TevColorRef::Alpha(Reg[TevOutput::Color2]), // c2.aaa
  117. TevColorRef::Color(TexColor), // tex.rgb
  118. TevColorRef::Alpha(TexColor), // tex.aaa
  119. TevColorRef::Color(RasColor), // ras.rgb
  120. TevColorRef::Alpha(RasColor), // ras.aaa
  121. TevColorRef::All(V1), // one
  122. TevColorRef::All(V1_2), // half
  123. TevColorRef::Color(StageKonst), // konst
  124. TevColorRef::All(V0), // zero
  125. };
  126. const Common::EnumMap<TevAlphaRef, TevAlphaArg::Zero> m_AlphaInputLUT{
  127. TevAlphaRef(Reg[TevOutput::Prev]), // prev
  128. TevAlphaRef(Reg[TevOutput::Color0]), // c0
  129. TevAlphaRef(Reg[TevOutput::Color1]), // c1
  130. TevAlphaRef(Reg[TevOutput::Color2]), // c2
  131. TevAlphaRef(TexColor), // tex
  132. TevAlphaRef(RasColor), // ras
  133. TevAlphaRef(StageKonst), // konst
  134. TevAlphaRef(V0), // zero
  135. };
  136. const Common::EnumMap<TevKonstRef, KonstSel::K3_A> m_KonstLUT{
  137. TevKonstRef::Value(V1), // 1
  138. TevKonstRef::Value(V7_8), // 7/8
  139. TevKonstRef::Value(V3_4), // 3/4
  140. TevKonstRef::Value(V5_8), // 5/8
  141. TevKonstRef::Value(V1_2), // 1/2
  142. TevKonstRef::Value(V3_8), // 3/8
  143. TevKonstRef::Value(V1_4), // 1/4
  144. TevKonstRef::Value(V1_8), // 1/8
  145. // These are "invalid" values, not meant to be used. On hardware,
  146. // they all output zero.
  147. TevKonstRef::Value(V0), TevKonstRef::Value(V0), TevKonstRef::Value(V0),
  148. TevKonstRef::Value(V0),
  149. // These values are valid for RGB only; they're invalid for alpha
  150. TevKonstRef::Konst(V0, KonstantColors[0]), // Konst 0 RGB
  151. TevKonstRef::Konst(V0, KonstantColors[1]), // Konst 1 RGB
  152. TevKonstRef::Konst(V0, KonstantColors[2]), // Konst 2 RGB
  153. TevKonstRef::Konst(V0, KonstantColors[3]), // Konst 3 RGB
  154. TevKonstRef::Value(KonstantColors[0].r), // Konst 0 Red
  155. TevKonstRef::Value(KonstantColors[1].r), // Konst 1 Red
  156. TevKonstRef::Value(KonstantColors[2].r), // Konst 2 Red
  157. TevKonstRef::Value(KonstantColors[3].r), // Konst 3 Red
  158. TevKonstRef::Value(KonstantColors[0].g), // Konst 0 Green
  159. TevKonstRef::Value(KonstantColors[1].g), // Konst 1 Green
  160. TevKonstRef::Value(KonstantColors[2].g), // Konst 2 Green
  161. TevKonstRef::Value(KonstantColors[3].g), // Konst 3 Green
  162. TevKonstRef::Value(KonstantColors[0].b), // Konst 0 Blue
  163. TevKonstRef::Value(KonstantColors[1].b), // Konst 1 Blue
  164. TevKonstRef::Value(KonstantColors[2].b), // Konst 2 Blue
  165. TevKonstRef::Value(KonstantColors[3].b), // Konst 3 Blue
  166. TevKonstRef::Value(KonstantColors[0].a), // Konst 0 Alpha
  167. TevKonstRef::Value(KonstantColors[1].a), // Konst 1 Alpha
  168. TevKonstRef::Value(KonstantColors[2].a), // Konst 2 Alpha
  169. TevKonstRef::Value(KonstantColors[3].a), // Konst 3 Alpha
  170. };
  171. static constexpr Common::EnumMap<s16, TevBias::Compare> s_BiasLUT{0, 128, -128, 0};
  172. static constexpr Common::EnumMap<u8, TevScale::Divide2> s_ScaleLShiftLUT{0, 1, 2, 0};
  173. static constexpr Common::EnumMap<u8, TevScale::Divide2> s_ScaleRShiftLUT{0, 0, 0, 1};
  174. enum BufferBase
  175. {
  176. DIRECT = 0,
  177. DIRECT_TFETCH = 16,
  178. INDIRECT = 32
  179. };
  180. void SetRasColor(RasColorChan colorChan, u32 swaptable);
  181. void DrawColorRegular(const TevStageCombiner::ColorCombiner& cc, const InputRegType inputs[4]);
  182. void DrawColorCompare(const TevStageCombiner::ColorCombiner& cc, const InputRegType inputs[4]);
  183. void DrawAlphaRegular(const TevStageCombiner::AlphaCombiner& ac, const InputRegType inputs[4]);
  184. void DrawAlphaCompare(const TevStageCombiner::AlphaCombiner& ac, const InputRegType inputs[4]);
  185. void Indirect(unsigned int stageNum, s32 s, s32 t);
  186. public:
  187. s32 Position[3]{};
  188. u8 Color[2][4]{}; // must be RGBA for correct swap table ordering
  189. TextureCoordinateType Uv[8]{};
  190. s32 IndirectLod[4]{};
  191. bool IndirectLinear[4]{};
  192. s32 TextureLod[16]{};
  193. bool TextureLinear[16]{};
  194. enum
  195. {
  196. ALP_C,
  197. BLU_C,
  198. GRN_C,
  199. RED_C
  200. };
  201. void SetKonstColors();
  202. void Draw();
  203. };