Tev.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 RawTexColor;
  93. TevColor TexColor;
  94. TevColor RasColor;
  95. TevColor StageKonst;
  96. // Fixed constants, corresponding to KonstSel
  97. static constexpr s16 V0 = 0;
  98. static constexpr s16 V1_8 = 32;
  99. static constexpr s16 V1_4 = 64;
  100. static constexpr s16 V3_8 = 96;
  101. static constexpr s16 V1_2 = 128;
  102. static constexpr s16 V5_8 = 159;
  103. static constexpr s16 V3_4 = 191;
  104. static constexpr s16 V7_8 = 223;
  105. static constexpr s16 V1 = 255;
  106. u8 AlphaBump = 0;
  107. u8 IndirectTex[4][4]{};
  108. TextureCoordinateType TexCoord{};
  109. const Common::EnumMap<TevColorRef, TevColorArg::Zero> m_ColorInputLUT{
  110. TevColorRef::Color(Reg[TevOutput::Prev]), // prev.rgb
  111. TevColorRef::Alpha(Reg[TevOutput::Prev]), // prev.aaa
  112. TevColorRef::Color(Reg[TevOutput::Color0]), // c0.rgb
  113. TevColorRef::Alpha(Reg[TevOutput::Color0]), // c0.aaa
  114. TevColorRef::Color(Reg[TevOutput::Color1]), // c1.rgb
  115. TevColorRef::Alpha(Reg[TevOutput::Color1]), // c1.aaa
  116. TevColorRef::Color(Reg[TevOutput::Color2]), // c2.rgb
  117. TevColorRef::Alpha(Reg[TevOutput::Color2]), // c2.aaa
  118. TevColorRef::Color(TexColor), // tex.rgb
  119. TevColorRef::Alpha(TexColor), // tex.aaa
  120. TevColorRef::Color(RasColor), // ras.rgb
  121. TevColorRef::Alpha(RasColor), // ras.aaa
  122. TevColorRef::All(V1), // one
  123. TevColorRef::All(V1_2), // half
  124. TevColorRef::Color(StageKonst), // konst
  125. TevColorRef::All(V0), // zero
  126. };
  127. const Common::EnumMap<TevAlphaRef, TevAlphaArg::Zero> m_AlphaInputLUT{
  128. TevAlphaRef(Reg[TevOutput::Prev]), // prev
  129. TevAlphaRef(Reg[TevOutput::Color0]), // c0
  130. TevAlphaRef(Reg[TevOutput::Color1]), // c1
  131. TevAlphaRef(Reg[TevOutput::Color2]), // c2
  132. TevAlphaRef(TexColor), // tex
  133. TevAlphaRef(RasColor), // ras
  134. TevAlphaRef(StageKonst), // konst
  135. TevAlphaRef(V0), // zero
  136. };
  137. const Common::EnumMap<TevKonstRef, KonstSel::K3_A> m_KonstLUT{
  138. TevKonstRef::Value(V1), // 1
  139. TevKonstRef::Value(V7_8), // 7/8
  140. TevKonstRef::Value(V3_4), // 3/4
  141. TevKonstRef::Value(V5_8), // 5/8
  142. TevKonstRef::Value(V1_2), // 1/2
  143. TevKonstRef::Value(V3_8), // 3/8
  144. TevKonstRef::Value(V1_4), // 1/4
  145. TevKonstRef::Value(V1_8), // 1/8
  146. // These are "invalid" values, not meant to be used. On hardware,
  147. // they all output zero.
  148. TevKonstRef::Value(V0), TevKonstRef::Value(V0), TevKonstRef::Value(V0),
  149. TevKonstRef::Value(V0),
  150. // These values are valid for RGB only; they're invalid for alpha
  151. TevKonstRef::Konst(V0, KonstantColors[0]), // Konst 0 RGB
  152. TevKonstRef::Konst(V0, KonstantColors[1]), // Konst 1 RGB
  153. TevKonstRef::Konst(V0, KonstantColors[2]), // Konst 2 RGB
  154. TevKonstRef::Konst(V0, KonstantColors[3]), // Konst 3 RGB
  155. TevKonstRef::Value(KonstantColors[0].r), // Konst 0 Red
  156. TevKonstRef::Value(KonstantColors[1].r), // Konst 1 Red
  157. TevKonstRef::Value(KonstantColors[2].r), // Konst 2 Red
  158. TevKonstRef::Value(KonstantColors[3].r), // Konst 3 Red
  159. TevKonstRef::Value(KonstantColors[0].g), // Konst 0 Green
  160. TevKonstRef::Value(KonstantColors[1].g), // Konst 1 Green
  161. TevKonstRef::Value(KonstantColors[2].g), // Konst 2 Green
  162. TevKonstRef::Value(KonstantColors[3].g), // Konst 3 Green
  163. TevKonstRef::Value(KonstantColors[0].b), // Konst 0 Blue
  164. TevKonstRef::Value(KonstantColors[1].b), // Konst 1 Blue
  165. TevKonstRef::Value(KonstantColors[2].b), // Konst 2 Blue
  166. TevKonstRef::Value(KonstantColors[3].b), // Konst 3 Blue
  167. TevKonstRef::Value(KonstantColors[0].a), // Konst 0 Alpha
  168. TevKonstRef::Value(KonstantColors[1].a), // Konst 1 Alpha
  169. TevKonstRef::Value(KonstantColors[2].a), // Konst 2 Alpha
  170. TevKonstRef::Value(KonstantColors[3].a), // Konst 3 Alpha
  171. };
  172. static constexpr Common::EnumMap<s16, TevBias::Compare> s_BiasLUT{0, 128, -128, 0};
  173. static constexpr Common::EnumMap<u8, TevScale::Divide2> s_ScaleLShiftLUT{0, 1, 2, 0};
  174. static constexpr Common::EnumMap<u8, TevScale::Divide2> s_ScaleRShiftLUT{0, 0, 0, 1};
  175. enum BufferBase
  176. {
  177. DIRECT = 0,
  178. DIRECT_TFETCH = 16,
  179. INDIRECT = 32
  180. };
  181. void SetRasColor(RasColorChan colorChan, u32 swaptable);
  182. void DrawColorRegular(const TevStageCombiner::ColorCombiner& cc, const InputRegType inputs[4]);
  183. void DrawColorCompare(const TevStageCombiner::ColorCombiner& cc, const InputRegType inputs[4]);
  184. void DrawAlphaRegular(const TevStageCombiner::AlphaCombiner& ac, const InputRegType inputs[4]);
  185. void DrawAlphaCompare(const TevStageCombiner::AlphaCombiner& ac, const InputRegType inputs[4]);
  186. void Indirect(unsigned int stageNum, s32 s, s32 t);
  187. public:
  188. s32 Position[3]{};
  189. u8 Color[2][4]{}; // must be RGBA for correct swap table ordering
  190. TextureCoordinateType Uv[8]{};
  191. s32 IndirectLod[4]{};
  192. bool IndirectLinear[4]{};
  193. s32 TextureLod[16]{};
  194. bool TextureLinear[16]{};
  195. enum
  196. {
  197. ALP_C,
  198. BLU_C,
  199. GRN_C,
  200. RED_C
  201. };
  202. void SetKonstColors();
  203. void Draw();
  204. };