Tev.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. // Copyright 2009 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "VideoBackends/Software/Tev.h"
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <cstring>
  7. #include "Common/ChunkFile.h"
  8. #include "Common/CommonTypes.h"
  9. #include "Core/System.h"
  10. #include "VideoBackends/Software/EfbInterface.h"
  11. #include "VideoBackends/Software/SWBoundingBox.h"
  12. #include "VideoBackends/Software/TextureSampler.h"
  13. #include "VideoCommon/PerfQueryBase.h"
  14. #include "VideoCommon/PixelShaderManager.h"
  15. #include "VideoCommon/Statistics.h"
  16. #include "VideoCommon/VideoCommon.h"
  17. #include "VideoCommon/VideoConfig.h"
  18. #include "VideoCommon/XFMemory.h"
  19. static inline s16 Clamp255(s16 in)
  20. {
  21. return std::clamp<s16>(in, 0, 255);
  22. }
  23. static inline s16 Clamp1024(s16 in)
  24. {
  25. return std::clamp<s16>(in, -1024, 1023);
  26. }
  27. void Tev::SetRasColor(RasColorChan colorChan, u32 swaptable)
  28. {
  29. switch (colorChan)
  30. {
  31. case RasColorChan::Color0:
  32. {
  33. const u8* color = Color[0];
  34. const auto& swap = bpmem.tevksel.GetSwapTable(swaptable);
  35. RasColor.r = color[u32(swap[ColorChannel::Red])];
  36. RasColor.g = color[u32(swap[ColorChannel::Green])];
  37. RasColor.b = color[u32(swap[ColorChannel::Blue])];
  38. RasColor.a = color[u32(swap[ColorChannel::Alpha])];
  39. }
  40. break;
  41. case RasColorChan::Color1:
  42. {
  43. const u8* color = Color[1];
  44. const auto& swap = bpmem.tevksel.GetSwapTable(swaptable);
  45. RasColor.r = color[u32(swap[ColorChannel::Red])];
  46. RasColor.g = color[u32(swap[ColorChannel::Green])];
  47. RasColor.b = color[u32(swap[ColorChannel::Blue])];
  48. RasColor.a = color[u32(swap[ColorChannel::Alpha])];
  49. }
  50. break;
  51. case RasColorChan::AlphaBump:
  52. {
  53. RasColor = TevColor::All(AlphaBump);
  54. }
  55. break;
  56. case RasColorChan::NormalizedAlphaBump:
  57. {
  58. const u8 normalized = AlphaBump | AlphaBump >> 5;
  59. RasColor = TevColor::All(normalized);
  60. }
  61. break;
  62. default:
  63. {
  64. if (colorChan != RasColorChan::Zero)
  65. PanicAlertFmt("Invalid ras color channel: {}", colorChan);
  66. RasColor = TevColor::All(0);
  67. }
  68. break;
  69. }
  70. }
  71. void Tev::DrawColorRegular(const TevStageCombiner::ColorCombiner& cc, const InputRegType inputs[4])
  72. {
  73. for (int i = BLU_C; i <= RED_C; i++)
  74. {
  75. const InputRegType& InputReg = inputs[i];
  76. const u16 c = InputReg.c + (InputReg.c >> 7);
  77. s32 temp = InputReg.a * (256 - c) + (InputReg.b * c);
  78. temp <<= s_ScaleLShiftLUT[cc.scale];
  79. temp += (cc.scale == TevScale::Divide2) ? 0 : (cc.op == TevOp::Sub) ? 127 : 128;
  80. temp >>= 8;
  81. temp = cc.op == TevOp::Sub ? -temp : temp;
  82. s32 result = ((InputReg.d + s_BiasLUT[cc.bias]) << s_ScaleLShiftLUT[cc.scale]) + temp;
  83. result = result >> s_ScaleRShiftLUT[cc.scale];
  84. Reg[cc.dest][i] = result;
  85. }
  86. }
  87. void Tev::DrawColorCompare(const TevStageCombiner::ColorCombiner& cc, const InputRegType inputs[4])
  88. {
  89. for (int i = BLU_C; i <= RED_C; i++)
  90. {
  91. u32 a, b;
  92. switch (cc.compare_mode)
  93. {
  94. case TevCompareMode::R8:
  95. a = inputs[RED_C].a;
  96. b = inputs[RED_C].b;
  97. break;
  98. case TevCompareMode::GR16:
  99. a = (inputs[GRN_C].a << 8) | inputs[RED_C].a;
  100. b = (inputs[GRN_C].b << 8) | inputs[RED_C].b;
  101. break;
  102. case TevCompareMode::BGR24:
  103. a = (inputs[BLU_C].a << 16) | (inputs[GRN_C].a << 8) | inputs[RED_C].a;
  104. b = (inputs[BLU_C].b << 16) | (inputs[GRN_C].b << 8) | inputs[RED_C].b;
  105. break;
  106. case TevCompareMode::RGB8:
  107. a = inputs[i].a;
  108. b = inputs[i].b;
  109. break;
  110. default:
  111. PanicAlertFmt("Invalid compare mode {}", cc.compare_mode);
  112. continue;
  113. }
  114. if (cc.comparison == TevComparison::GT)
  115. Reg[cc.dest][i] = inputs[i].d + ((a > b) ? inputs[i].c : 0);
  116. else
  117. Reg[cc.dest][i] = inputs[i].d + ((a == b) ? inputs[i].c : 0);
  118. }
  119. }
  120. void Tev::DrawAlphaRegular(const TevStageCombiner::AlphaCombiner& ac, const InputRegType inputs[4])
  121. {
  122. const InputRegType& InputReg = inputs[ALP_C];
  123. const u16 c = InputReg.c + (InputReg.c >> 7);
  124. s32 temp = InputReg.a * (256 - c) + (InputReg.b * c);
  125. temp <<= s_ScaleLShiftLUT[ac.scale];
  126. temp += (ac.scale == TevScale::Divide2) ? 0 : (ac.op == TevOp::Sub) ? 127 : 128;
  127. temp = ac.op == TevOp::Sub ? (-temp >> 8) : (temp >> 8);
  128. s32 result = ((InputReg.d + s_BiasLUT[ac.bias]) << s_ScaleLShiftLUT[ac.scale]) + temp;
  129. result = result >> s_ScaleRShiftLUT[ac.scale];
  130. Reg[ac.dest].a = result;
  131. }
  132. void Tev::DrawAlphaCompare(const TevStageCombiner::AlphaCombiner& ac, const InputRegType inputs[4])
  133. {
  134. u32 a, b;
  135. switch (ac.compare_mode)
  136. {
  137. case TevCompareMode::R8:
  138. a = inputs[RED_C].a;
  139. b = inputs[RED_C].b;
  140. break;
  141. case TevCompareMode::GR16:
  142. a = (inputs[GRN_C].a << 8) | inputs[RED_C].a;
  143. b = (inputs[GRN_C].b << 8) | inputs[RED_C].b;
  144. break;
  145. case TevCompareMode::BGR24:
  146. a = (inputs[BLU_C].a << 16) | (inputs[GRN_C].a << 8) | inputs[RED_C].a;
  147. b = (inputs[BLU_C].b << 16) | (inputs[GRN_C].b << 8) | inputs[RED_C].b;
  148. break;
  149. case TevCompareMode::A8:
  150. a = inputs[ALP_C].a;
  151. b = inputs[ALP_C].b;
  152. break;
  153. default:
  154. PanicAlertFmt("Invalid compare mode {}", ac.compare_mode);
  155. return;
  156. }
  157. if (ac.comparison == TevComparison::GT)
  158. Reg[ac.dest].a = inputs[ALP_C].d + ((a > b) ? inputs[ALP_C].c : 0);
  159. else
  160. Reg[ac.dest].a = inputs[ALP_C].d + ((a == b) ? inputs[ALP_C].c : 0);
  161. }
  162. static bool AlphaCompare(int alpha, int ref, CompareMode comp)
  163. {
  164. switch (comp)
  165. {
  166. case CompareMode::Always:
  167. return true;
  168. case CompareMode::Never:
  169. return false;
  170. case CompareMode::LEqual:
  171. return alpha <= ref;
  172. case CompareMode::Less:
  173. return alpha < ref;
  174. case CompareMode::GEqual:
  175. return alpha >= ref;
  176. case CompareMode::Greater:
  177. return alpha > ref;
  178. case CompareMode::Equal:
  179. return alpha == ref;
  180. case CompareMode::NEqual:
  181. return alpha != ref;
  182. default:
  183. PanicAlertFmt("Invalid compare mode {}", comp);
  184. return true;
  185. }
  186. }
  187. static bool TevAlphaTest(int alpha)
  188. {
  189. const bool comp0 = AlphaCompare(alpha, bpmem.alpha_test.ref0, bpmem.alpha_test.comp0);
  190. const bool comp1 = AlphaCompare(alpha, bpmem.alpha_test.ref1, bpmem.alpha_test.comp1);
  191. switch (bpmem.alpha_test.logic)
  192. {
  193. case AlphaTestOp::And:
  194. return comp0 && comp1;
  195. case AlphaTestOp::Or:
  196. return comp0 || comp1;
  197. case AlphaTestOp::Xor:
  198. return comp0 ^ comp1;
  199. case AlphaTestOp::Xnor:
  200. return !(comp0 ^ comp1);
  201. default:
  202. PanicAlertFmt("Invalid AlphaTestOp {}", bpmem.alpha_test.logic);
  203. return true;
  204. }
  205. }
  206. static inline s32 WrapIndirectCoord(s32 coord, IndTexWrap wrapMode)
  207. {
  208. switch (wrapMode)
  209. {
  210. case IndTexWrap::ITW_OFF:
  211. return coord;
  212. case IndTexWrap::ITW_256:
  213. return (coord & ((256 << 7) - 1));
  214. case IndTexWrap::ITW_128:
  215. return (coord & ((128 << 7) - 1));
  216. case IndTexWrap::ITW_64:
  217. return (coord & ((64 << 7) - 1));
  218. case IndTexWrap::ITW_32:
  219. return (coord & ((32 << 7) - 1));
  220. case IndTexWrap::ITW_16:
  221. return (coord & ((16 << 7) - 1));
  222. case IndTexWrap::ITW_0:
  223. return 0;
  224. default:
  225. PanicAlertFmt("Invalid indirect wrap mode {}", wrapMode);
  226. return 0;
  227. }
  228. }
  229. void Tev::Indirect(unsigned int stageNum, s32 s, s32 t)
  230. {
  231. const TevStageIndirect& indirect = bpmem.tevind[stageNum];
  232. const u8* indmap = IndirectTex[indirect.bt];
  233. s32 indcoord[3];
  234. // alpha bump select
  235. switch (indirect.bs)
  236. {
  237. case IndTexBumpAlpha::Off:
  238. AlphaBump = 0;
  239. break;
  240. case IndTexBumpAlpha::S:
  241. AlphaBump = indmap[TextureSampler::ALP_SMP];
  242. break;
  243. case IndTexBumpAlpha::T:
  244. AlphaBump = indmap[TextureSampler::BLU_SMP];
  245. break;
  246. case IndTexBumpAlpha::U:
  247. AlphaBump = indmap[TextureSampler::GRN_SMP];
  248. break;
  249. default:
  250. PanicAlertFmt("Invalid alpha bump {}", indirect.bs);
  251. return;
  252. }
  253. // bias select
  254. const s16 biasValue = indirect.fmt == IndTexFormat::ITF_8 ? -128 : 1;
  255. s16 bias[3];
  256. bias[0] = indirect.bias_s ? biasValue : 0;
  257. bias[1] = indirect.bias_t ? biasValue : 0;
  258. bias[2] = indirect.bias_u ? biasValue : 0;
  259. // format
  260. switch (indirect.fmt)
  261. {
  262. case IndTexFormat::ITF_8:
  263. indcoord[0] = indmap[TextureSampler::ALP_SMP] + bias[0];
  264. indcoord[1] = indmap[TextureSampler::BLU_SMP] + bias[1];
  265. indcoord[2] = indmap[TextureSampler::GRN_SMP] + bias[2];
  266. AlphaBump = AlphaBump & 0xf8;
  267. break;
  268. case IndTexFormat::ITF_5:
  269. indcoord[0] = (indmap[TextureSampler::ALP_SMP] >> 3) + bias[0];
  270. indcoord[1] = (indmap[TextureSampler::BLU_SMP] >> 3) + bias[1];
  271. indcoord[2] = (indmap[TextureSampler::GRN_SMP] >> 3) + bias[2];
  272. AlphaBump = AlphaBump << 5;
  273. break;
  274. case IndTexFormat::ITF_4:
  275. indcoord[0] = (indmap[TextureSampler::ALP_SMP] >> 4) + bias[0];
  276. indcoord[1] = (indmap[TextureSampler::BLU_SMP] >> 4) + bias[1];
  277. indcoord[2] = (indmap[TextureSampler::GRN_SMP] >> 4) + bias[2];
  278. AlphaBump = AlphaBump << 4;
  279. break;
  280. case IndTexFormat::ITF_3:
  281. indcoord[0] = (indmap[TextureSampler::ALP_SMP] >> 5) + bias[0];
  282. indcoord[1] = (indmap[TextureSampler::BLU_SMP] >> 5) + bias[1];
  283. indcoord[2] = (indmap[TextureSampler::GRN_SMP] >> 5) + bias[2];
  284. AlphaBump = AlphaBump << 3;
  285. break;
  286. default:
  287. PanicAlertFmt("Invalid indirect format {}", indirect.fmt);
  288. return;
  289. }
  290. s32 indtevtrans[2] = {0, 0};
  291. // matrix multiply - results might overflow, but we don't care since we only use the lower 24 bits
  292. // of the result.
  293. if (indirect.matrix_index != IndMtxIndex::Off)
  294. {
  295. const IND_MTX& indmtx = bpmem.indmtx[static_cast<u32>(indirect.matrix_index.Value()) - 1];
  296. const int shift = 17 - indmtx.GetScale();
  297. switch (indirect.matrix_id)
  298. {
  299. case IndMtxId::Indirect:
  300. // matrix values are S0.10, output format is S17.7, so divide by 8
  301. indtevtrans[0] = (indmtx.col0.ma * indcoord[0] + indmtx.col1.mc * indcoord[1] +
  302. indmtx.col2.me * indcoord[2]) >>
  303. 3;
  304. indtevtrans[1] = (indmtx.col0.mb * indcoord[0] + indmtx.col1.md * indcoord[1] +
  305. indmtx.col2.mf * indcoord[2]) >>
  306. 3;
  307. break;
  308. case IndMtxId::S:
  309. // s is S17.7, matrix elements are divided by 256, output is S17.7, so divide by 256. - TODO:
  310. // Maybe, since s is actually stored as S24, we should divide by 256*64?
  311. indtevtrans[0] = s * indcoord[0] / 256;
  312. indtevtrans[1] = t * indcoord[0] / 256;
  313. break;
  314. case IndMtxId::T:
  315. indtevtrans[0] = s * indcoord[1] / 256;
  316. indtevtrans[1] = t * indcoord[1] / 256;
  317. break;
  318. default:
  319. PanicAlertFmt("Invalid indirect matrix ID {}", indirect.matrix_id);
  320. return;
  321. }
  322. indtevtrans[0] = shift >= 0 ? indtevtrans[0] >> shift : indtevtrans[0] << -shift;
  323. indtevtrans[1] = shift >= 0 ? indtevtrans[1] >> shift : indtevtrans[1] << -shift;
  324. }
  325. else
  326. {
  327. // If matrix_index is Off (0), matrix_id should be Indirect (0)
  328. ASSERT(indirect.matrix_id == IndMtxId::Indirect);
  329. }
  330. if (indirect.fb_addprev)
  331. {
  332. TexCoord.s += (int)(WrapIndirectCoord(s, indirect.sw) + indtevtrans[0]);
  333. TexCoord.t += (int)(WrapIndirectCoord(t, indirect.tw) + indtevtrans[1]);
  334. }
  335. else
  336. {
  337. TexCoord.s = (int)(WrapIndirectCoord(s, indirect.sw) + indtevtrans[0]);
  338. TexCoord.t = (int)(WrapIndirectCoord(t, indirect.tw) + indtevtrans[1]);
  339. }
  340. }
  341. void Tev::Draw()
  342. {
  343. ASSERT(Position[0] >= 0 && Position[0] < s32(EFB_WIDTH));
  344. ASSERT(Position[1] >= 0 && Position[1] < s32(EFB_HEIGHT));
  345. INCSTAT(g_stats.this_frame.tev_pixels_in);
  346. auto& system = Core::System::GetInstance();
  347. auto& pixel_shader_manager = system.GetPixelShaderManager();
  348. // initial color values
  349. for (int i = 0; i < 4; i++)
  350. {
  351. Reg[static_cast<TevOutput>(i)].r = pixel_shader_manager.constants.colors[i][0];
  352. Reg[static_cast<TevOutput>(i)].g = pixel_shader_manager.constants.colors[i][1];
  353. Reg[static_cast<TevOutput>(i)].b = pixel_shader_manager.constants.colors[i][2];
  354. Reg[static_cast<TevOutput>(i)].a = pixel_shader_manager.constants.colors[i][3];
  355. }
  356. for (unsigned int stageNum = 0; stageNum < bpmem.genMode.numindstages; stageNum++)
  357. {
  358. const int stageNum2 = stageNum >> 1;
  359. const int stageOdd = stageNum & 1;
  360. u32 texcoordSel = bpmem.tevindref.getTexCoord(stageNum);
  361. const u32 texmap = bpmem.tevindref.getTexMap(stageNum);
  362. // Quirk: when the tex coord is not less than the number of tex gens (i.e. the tex coord does
  363. // not exist), then tex coord 0 is used (though sometimes glitchy effects happen on console).
  364. // This affects the Mario portrait in Luigi's Mansion, where the developers forgot to set
  365. // the number of tex gens to 2 (bug 11462).
  366. if (texcoordSel >= bpmem.genMode.numtexgens)
  367. texcoordSel = 0;
  368. const TEXSCALE& texscale = bpmem.texscale[stageNum2];
  369. const s32 scaleS = stageOdd ? texscale.ss1 : texscale.ss0;
  370. const s32 scaleT = stageOdd ? texscale.ts1 : texscale.ts0;
  371. TextureSampler::Sample(Uv[texcoordSel].s >> scaleS, Uv[texcoordSel].t >> scaleT,
  372. IndirectLod[stageNum], IndirectLinear[stageNum], texmap,
  373. IndirectTex[stageNum]);
  374. }
  375. for (unsigned int stageNum = 0; stageNum <= bpmem.genMode.numtevstages; stageNum++)
  376. {
  377. const int stageNum2 = stageNum >> 1;
  378. const int stageOdd = stageNum & 1;
  379. const TwoTevStageOrders& order = bpmem.tevorders[stageNum2];
  380. // stage combiners
  381. const TevStageCombiner::ColorCombiner& cc = bpmem.combiners[stageNum].colorC;
  382. const TevStageCombiner::AlphaCombiner& ac = bpmem.combiners[stageNum].alphaC;
  383. u32 texcoordSel = order.getTexCoord(stageOdd);
  384. const u32 texmap = order.getTexMap(stageOdd);
  385. // Quirk: when the tex coord is not less than the number of tex gens (i.e. the tex coord does
  386. // not exist), then tex coord 0 is used (though sometimes glitchy effects happen on console).
  387. if (texcoordSel >= bpmem.genMode.numtexgens)
  388. texcoordSel = 0;
  389. Indirect(stageNum, Uv[texcoordSel].s, Uv[texcoordSel].t);
  390. // sample texture
  391. if (order.getEnable(stageOdd))
  392. {
  393. // RGBA
  394. u8 texel[4];
  395. if (bpmem.genMode.numtexgens > 0)
  396. {
  397. TextureSampler::Sample(TexCoord.s, TexCoord.t, TextureLod[stageNum],
  398. TextureLinear[stageNum], texmap, texel);
  399. }
  400. else
  401. {
  402. // It seems like the result is always black when no tex coords are enabled, but further
  403. // hardware testing is needed.
  404. std::memset(texel, 0, 4);
  405. }
  406. const auto& swap = bpmem.tevksel.GetSwapTable(ac.tswap);
  407. TexColor.r = texel[u32(swap[ColorChannel::Red])];
  408. TexColor.g = texel[u32(swap[ColorChannel::Green])];
  409. TexColor.b = texel[u32(swap[ColorChannel::Blue])];
  410. TexColor.a = texel[u32(swap[ColorChannel::Alpha])];
  411. }
  412. // set konst for this stage
  413. const auto kc = bpmem.tevksel.GetKonstColor(stageNum);
  414. const auto ka = bpmem.tevksel.GetKonstAlpha(stageNum);
  415. StageKonst.r = m_KonstLUT[kc].r;
  416. StageKonst.g = m_KonstLUT[kc].g;
  417. StageKonst.b = m_KonstLUT[kc].b;
  418. StageKonst.a = m_KonstLUT[ka].a;
  419. // set color
  420. SetRasColor(order.getColorChan(stageOdd), ac.rswap);
  421. // combine inputs
  422. InputRegType inputs[4];
  423. inputs[BLU_C].a = m_ColorInputLUT[cc.a].b;
  424. inputs[BLU_C].b = m_ColorInputLUT[cc.b].b;
  425. inputs[BLU_C].c = m_ColorInputLUT[cc.c].b;
  426. inputs[BLU_C].d = m_ColorInputLUT[cc.d].b;
  427. inputs[GRN_C].a = m_ColorInputLUT[cc.a].g;
  428. inputs[GRN_C].b = m_ColorInputLUT[cc.b].g;
  429. inputs[GRN_C].c = m_ColorInputLUT[cc.c].g;
  430. inputs[GRN_C].d = m_ColorInputLUT[cc.d].g;
  431. inputs[RED_C].a = m_ColorInputLUT[cc.a].r;
  432. inputs[RED_C].b = m_ColorInputLUT[cc.b].r;
  433. inputs[RED_C].c = m_ColorInputLUT[cc.c].r;
  434. inputs[RED_C].d = m_ColorInputLUT[cc.d].r;
  435. inputs[ALP_C].a = m_AlphaInputLUT[ac.a].a;
  436. inputs[ALP_C].b = m_AlphaInputLUT[ac.b].a;
  437. inputs[ALP_C].c = m_AlphaInputLUT[ac.c].a;
  438. inputs[ALP_C].d = m_AlphaInputLUT[ac.d].a;
  439. if (cc.bias != TevBias::Compare)
  440. DrawColorRegular(cc, inputs);
  441. else
  442. DrawColorCompare(cc, inputs);
  443. if (cc.clamp)
  444. {
  445. Reg[cc.dest].r = Clamp255(Reg[cc.dest].r);
  446. Reg[cc.dest].g = Clamp255(Reg[cc.dest].g);
  447. Reg[cc.dest].b = Clamp255(Reg[cc.dest].b);
  448. }
  449. else
  450. {
  451. Reg[cc.dest].r = Clamp1024(Reg[cc.dest].r);
  452. Reg[cc.dest].g = Clamp1024(Reg[cc.dest].g);
  453. Reg[cc.dest].b = Clamp1024(Reg[cc.dest].b);
  454. }
  455. if (ac.bias != TevBias::Compare)
  456. DrawAlphaRegular(ac, inputs);
  457. else
  458. DrawAlphaCompare(ac, inputs);
  459. if (ac.clamp)
  460. Reg[ac.dest].a = Clamp255(Reg[ac.dest].a);
  461. else
  462. Reg[ac.dest].a = Clamp1024(Reg[ac.dest].a);
  463. }
  464. // convert to 8 bits per component
  465. // the results of the last tev stage are put onto the screen,
  466. // regardless of the used destination register - TODO: Verify!
  467. const auto& color_index = bpmem.combiners[bpmem.genMode.numtevstages].colorC.dest;
  468. const auto& alpha_index = bpmem.combiners[bpmem.genMode.numtevstages].alphaC.dest;
  469. u8 output[4] = {(u8)Reg[alpha_index].a, (u8)Reg[color_index].b, (u8)Reg[color_index].g,
  470. (u8)Reg[color_index].r};
  471. if (!TevAlphaTest(output[ALP_C]))
  472. return;
  473. // z texture
  474. if (bpmem.ztex2.op != ZTexOp::Disabled)
  475. {
  476. u32 ztex = bpmem.ztex1.bias;
  477. switch (bpmem.ztex2.type)
  478. {
  479. case ZTexFormat::U8:
  480. ztex += TexColor[ALP_C];
  481. break;
  482. case ZTexFormat::U16:
  483. ztex += TexColor[ALP_C] << 8 | TexColor[RED_C];
  484. break;
  485. case ZTexFormat::U24:
  486. ztex += TexColor[RED_C] << 16 | TexColor[GRN_C] << 8 | TexColor[BLU_C];
  487. break;
  488. default:
  489. PanicAlertFmt("Invalid ztex format {}", bpmem.ztex2.type);
  490. }
  491. if (bpmem.ztex2.op == ZTexOp::Add)
  492. ztex += Position[2];
  493. Position[2] = ztex & 0x00ffffff;
  494. }
  495. // fog
  496. if (bpmem.fog.c_proj_fsel.fsel != FogType::Off)
  497. {
  498. float ze;
  499. if (bpmem.fog.c_proj_fsel.proj == FogProjection::Perspective)
  500. {
  501. // perspective
  502. // ze = A/(B - (Zs >> B_SHF))
  503. const s32 denom = bpmem.fog.b_magnitude - (Position[2] >> bpmem.fog.b_shift);
  504. // in addition downscale magnitude and zs to 0.24 bits
  505. ze = (bpmem.fog.GetA() * 16777215.0f) / static_cast<float>(denom);
  506. }
  507. else
  508. {
  509. // orthographic
  510. // ze = a*Zs
  511. // in addition downscale zs to 0.24 bits
  512. ze = bpmem.fog.GetA() * (static_cast<float>(Position[2]) / 16777215.0f);
  513. }
  514. if (bpmem.fogRange.Base.Enabled)
  515. {
  516. // TODO: This is untested and should definitely be checked against real hw.
  517. // - No idea if offset is really normalized against the viewport width or against the
  518. // projection matrix or yet something else
  519. // - scaling of the "k" coefficient isn't clear either.
  520. // First, calculate the offset from the viewport center (normalized to 0..1)
  521. const float offset =
  522. (Position[0] - (static_cast<s32>(bpmem.fogRange.Base.Center.Value()) - 342)) /
  523. static_cast<float>(xfmem.viewport.wd);
  524. // Based on that, choose the index such that points which are far away from the z-axis use the
  525. // 10th "k" value and such that central points use the first value.
  526. float floatindex = 9.f - std::abs(offset) * 9.f;
  527. floatindex = std::clamp(floatindex, 0.f, 9.f); // TODO: This shouldn't be necessary!
  528. // Get the two closest integer indices, look up the corresponding samples
  529. const int indexlower = (int)floatindex;
  530. const int indexupper = indexlower + 1;
  531. // Look up coefficient... Seems like multiplying by 4 makes Fortune Street work properly (fog
  532. // is too strong without the factor)
  533. const float klower = bpmem.fogRange.K[indexlower / 2].GetValue(indexlower % 2) * 4.f;
  534. const float kupper = bpmem.fogRange.K[indexupper / 2].GetValue(indexupper % 2) * 4.f;
  535. // linearly interpolate the samples and multiple ze by the resulting adjustment factor
  536. const float factor = indexupper - floatindex;
  537. const float k = klower * factor + kupper * (1.f - factor);
  538. const float x_adjust = sqrt(offset * offset + k * k) / k;
  539. ze *= x_adjust; // NOTE: This is basically dividing by a cosine (hidden behind
  540. // GXInitFogAdjTable): 1/cos = c/b = sqrt(a^2+b^2)/b
  541. }
  542. ze -= bpmem.fog.GetC();
  543. // clamp 0 to 1
  544. float fog = std::clamp(ze, 0.f, 1.f);
  545. switch (bpmem.fog.c_proj_fsel.fsel)
  546. {
  547. case FogType::Exp:
  548. fog = 1.0f - pow(2.0f, -8.0f * fog);
  549. break;
  550. case FogType::ExpSq:
  551. fog = 1.0f - pow(2.0f, -8.0f * fog * fog);
  552. break;
  553. case FogType::BackwardsExp:
  554. fog = 1.0f - fog;
  555. fog = pow(2.0f, -8.0f * fog);
  556. break;
  557. case FogType::BackwardsExpSq:
  558. fog = 1.0f - fog;
  559. fog = pow(2.0f, -8.0f * fog * fog);
  560. break;
  561. default:
  562. break;
  563. }
  564. // lerp from output to fog color
  565. const u32 fogInt = (u32)(fog * 256);
  566. const u32 invFog = 256 - fogInt;
  567. output[RED_C] = (output[RED_C] * invFog + fogInt * bpmem.fog.color.r) >> 8;
  568. output[GRN_C] = (output[GRN_C] * invFog + fogInt * bpmem.fog.color.g) >> 8;
  569. output[BLU_C] = (output[BLU_C] * invFog + fogInt * bpmem.fog.color.b) >> 8;
  570. }
  571. if (bpmem.GetEmulatedZ() == EmulatedZ::Late)
  572. {
  573. // TODO: Check against hw if these values get incremented even if depth testing is disabled
  574. EfbInterface::IncPerfCounterQuadCount(PQ_ZCOMP_INPUT);
  575. if (!EfbInterface::ZCompare(Position[0], Position[1], Position[2]))
  576. return;
  577. EfbInterface::IncPerfCounterQuadCount(PQ_ZCOMP_OUTPUT);
  578. }
  579. // The GC/Wii GPU rasterizes in 2x2 pixel groups, so bounding box values will be rounded to the
  580. // extents of these groups, rather than the exact pixel.
  581. BBoxManager::Update(static_cast<u16>(Position[0] & ~1), static_cast<u16>(Position[0] | 1),
  582. static_cast<u16>(Position[1] & ~1), static_cast<u16>(Position[1] | 1));
  583. INCSTAT(g_stats.this_frame.tev_pixels_out);
  584. EfbInterface::IncPerfCounterQuadCount(PQ_BLEND_INPUT);
  585. EfbInterface::BlendTev(Position[0], Position[1], output);
  586. }
  587. void Tev::SetKonstColors()
  588. {
  589. auto& system = Core::System::GetInstance();
  590. auto& pixel_shader_manager = system.GetPixelShaderManager();
  591. for (int i = 0; i < 4; i++)
  592. {
  593. KonstantColors[i].r = pixel_shader_manager.constants.kcolors[i][0];
  594. KonstantColors[i].g = pixel_shader_manager.constants.kcolors[i][1];
  595. KonstantColors[i].b = pixel_shader_manager.constants.kcolors[i][2];
  596. KonstantColors[i].a = pixel_shader_manager.constants.kcolors[i][3];
  597. }
  598. }