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