| 1 | // Copyright 2009 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/Software/SWEfbInterface.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <array> |
| 8 | #include <cstring> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "Common/CommonTypes.h" |
| 12 | #include "Common/Logging/Log.h" |
| 13 | |
| 14 | #include "Core/Config/GraphicsSettings.h" |
| 15 | |
| 16 | #include "VideoBackends/Software/CopyRegion.h" |
| 17 | #include "VideoCommon/BPMemory.h" |
| 18 | #include "VideoCommon/LookUpTables.h" |
| 19 | #include "VideoCommon/PerfQueryBase.h" |
| 20 | #include "VideoCommon/VideoCommon.h" |
| 21 | |
| 22 | namespace EfbInterface |
| 23 | { |
| 24 | static std::array<u8, EFB_WIDTH * EFB_HEIGHT * 6> efb; |
| 25 | |
| 26 | static std::array<u32, PQ_NUM_MEMBERS> perf_values; |
| 27 | |
| 28 | static inline u32 GetColorOffset(u16 x, u16 y) |
| 29 | { |
| 30 | return (x + y * EFB_WIDTH) * 3; |
| 31 | } |
| 32 | |
| 33 | static inline u32 GetDepthOffset(u16 x, u16 y) |
| 34 | { |
| 35 | constexpr u32 depth_buffer_start = EFB_WIDTH * EFB_HEIGHT * 3; |
| 36 | |
| 37 | return (x + y * EFB_WIDTH) * 3 + depth_buffer_start; |
| 38 | } |
| 39 | |
| 40 | static void SetPixelAlphaOnly(u32 offset, u8 a) |
| 41 | { |
| 42 | switch (bpmem.zcontrol.pixel_format) |
| 43 | { |
| 44 | case PixelFormat::RGB8_Z24: |
| 45 | case PixelFormat::Z24: |
| 46 | case PixelFormat::RGB565_Z16: |
| 47 | // do nothing |
| 48 | break; |
| 49 | case PixelFormat::RGBA6_Z24: |
| 50 | { |
| 51 | u32 a32 = a; |
| 52 | u32* dst = (u32*)&efb[offset]; |
| 53 | u32 val = *dst & 0xffffffc0; |
| 54 | val |= (a32 >> 2) & 0x0000003f; |
| 55 | *dst = val; |
| 56 | } |
| 57 | break; |
| 58 | default: |
| 59 | ERROR_LOG_FMT(VIDEO, "Unsupported pixel format: {}", bpmem.zcontrol.pixel_format); |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static void SetPixelColorOnly(u32 offset, u8* rgb) |
| 65 | { |
| 66 | switch (bpmem.zcontrol.pixel_format) |
| 67 | { |
| 68 | case PixelFormat::RGB8_Z24: |
| 69 | case PixelFormat::Z24: |
| 70 | { |
| 71 | u32 src = *(u32*)rgb; |
| 72 | u32* dst = (u32*)&efb[offset]; |
| 73 | u32 val = *dst & 0xff000000; |
| 74 | val |= src >> 8; |
| 75 | *dst = val; |
| 76 | } |
| 77 | break; |
| 78 | case PixelFormat::RGBA6_Z24: |
| 79 | { |
| 80 | u32 src = *(u32*)rgb; |
| 81 | u32* dst = (u32*)&efb[offset]; |
| 82 | u32 val = *dst & 0xff00003f; |
| 83 | val |= (src >> 4) & 0x00000fc0; // blue |
| 84 | val |= (src >> 6) & 0x0003f000; // green |
| 85 | val |= (src >> 8) & 0x00fc0000; // red |
| 86 | *dst = val; |
| 87 | } |
| 88 | break; |
| 89 | case PixelFormat::RGB565_Z16: |
| 90 | { |
| 91 | // TODO: RGB565_Z16 is not supported correctly yet |
| 92 | u32 src = *(u32*)rgb; |
| 93 | u32* dst = (u32*)&efb[offset]; |
| 94 | u32 val = *dst & 0xff000000; |
| 95 | val |= src >> 8; |
| 96 | *dst = val; |
| 97 | } |
| 98 | break; |
| 99 | default: |
| 100 | ERROR_LOG_FMT(VIDEO, "Unsupported pixel format: {}", bpmem.zcontrol.pixel_format); |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static void SetPixelAlphaColor(u32 offset, u8* color) |
| 106 | { |
| 107 | switch (bpmem.zcontrol.pixel_format) |
| 108 | { |
| 109 | case PixelFormat::RGB8_Z24: |
| 110 | case PixelFormat::Z24: |
| 111 | { |
| 112 | u32 src = *(u32*)color; |
| 113 | u32* dst = (u32*)&efb[offset]; |
| 114 | u32 val = *dst & 0xff000000; |
| 115 | val |= src >> 8; |
| 116 | *dst = val; |
| 117 | } |
| 118 | break; |
| 119 | case PixelFormat::RGBA6_Z24: |
| 120 | { |
| 121 | u32 src = *(u32*)color; |
| 122 | u32* dst = (u32*)&efb[offset]; |
| 123 | u32 val = *dst & 0xff000000; |
| 124 | val |= (src >> 2) & 0x0000003f; // alpha |
| 125 | val |= (src >> 4) & 0x00000fc0; // blue |
| 126 | val |= (src >> 6) & 0x0003f000; // green |
| 127 | val |= (src >> 8) & 0x00fc0000; // red |
| 128 | *dst = val; |
| 129 | } |
| 130 | break; |
| 131 | case PixelFormat::RGB565_Z16: |
| 132 | { |
| 133 | // TODO: RGB565_Z16 is not supported correctly yet |
| 134 | u32 src = *(u32*)color; |
| 135 | u32* dst = (u32*)&efb[offset]; |
| 136 | u32 val = *dst & 0xff000000; |
| 137 | val |= src >> 8; |
| 138 | *dst = val; |
| 139 | } |
| 140 | break; |
| 141 | default: |
| 142 | ERROR_LOG_FMT(VIDEO, "Unsupported pixel format: {}", bpmem.zcontrol.pixel_format); |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static u32 GetPixelColor(u32 offset) |
| 148 | { |
| 149 | u32 src; |
| 150 | std::memcpy(&src, &efb[offset], sizeof(u32)); |
| 151 | |
| 152 | switch (bpmem.zcontrol.pixel_format) |
| 153 | { |
| 154 | case PixelFormat::RGB8_Z24: |
| 155 | case PixelFormat::Z24: |
| 156 | return 0xff | ((src & 0x00ffffff) << 8); |
| 157 | |
| 158 | case PixelFormat::RGBA6_Z24: |
| 159 | return Convert6To8(src & 0x3f) | // Alpha |
| 160 | Convert6To8((src >> 6) & 0x3f) << 8 | // Blue |
| 161 | Convert6To8((src >> 12) & 0x3f) << 16 | // Green |
| 162 | Convert6To8((src >> 18) & 0x3f) << 24; // Red |
| 163 | |
| 164 | case PixelFormat::RGB565_Z16: |
| 165 | // TODO: RGB565_Z16 is not supported correctly yet |
| 166 | return 0xff | ((src & 0x00ffffff) << 8); |
| 167 | |
| 168 | default: |
| 169 | ERROR_LOG_FMT(VIDEO, "Unsupported pixel format: {}", bpmem.zcontrol.pixel_format); |
| 170 | return 0; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | static void SetPixelDepth(u32 offset, u32 depth) |
| 175 | { |
| 176 | switch (bpmem.zcontrol.pixel_format) |
| 177 | { |
| 178 | case PixelFormat::RGB8_Z24: |
| 179 | case PixelFormat::RGBA6_Z24: |
| 180 | case PixelFormat::Z24: |
| 181 | { |
| 182 | u32* dst = (u32*)&efb[offset]; |
| 183 | u32 val = *dst & 0xff000000; |
| 184 | val |= depth & 0x00ffffff; |
| 185 | *dst = val; |
| 186 | } |
| 187 | break; |
| 188 | case PixelFormat::RGB565_Z16: |
| 189 | { |
| 190 | // TODO: RGB565_Z16 is not supported correctly yet |
| 191 | u32* dst = (u32*)&efb[offset]; |
| 192 | u32 val = *dst & 0xff000000; |
| 193 | val |= depth & 0x00ffffff; |
| 194 | *dst = val; |
| 195 | } |
| 196 | break; |
| 197 | default: |
| 198 | ERROR_LOG_FMT(VIDEO, "Unsupported pixel format: {}", bpmem.zcontrol.pixel_format); |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | static u32 GetPixelDepth(u32 offset) |
| 204 | { |
| 205 | u32 depth = 0; |
| 206 | |
| 207 | switch (bpmem.zcontrol.pixel_format) |
| 208 | { |
| 209 | case PixelFormat::RGB8_Z24: |
| 210 | case PixelFormat::RGBA6_Z24: |
| 211 | case PixelFormat::Z24: |
| 212 | { |
| 213 | depth = (*(u32*)&efb[offset]) & 0x00ffffff; |
| 214 | } |
| 215 | break; |
| 216 | case PixelFormat::RGB565_Z16: |
| 217 | { |
| 218 | // TODO: RGB565_Z16 is not supported correctly yet |
| 219 | depth = (*(u32*)&efb[offset]) & 0x00ffffff; |
| 220 | } |
| 221 | break; |
| 222 | default: |
| 223 | ERROR_LOG_FMT(VIDEO, "Unsupported pixel format: {}", bpmem.zcontrol.pixel_format); |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | return depth; |
| 228 | } |
| 229 | |
| 230 | static u32 GetSourceFactor(u8* srcClr, u8* dstClr, SrcBlendFactor mode) |
| 231 | { |
| 232 | switch (mode) |
| 233 | { |
| 234 | case SrcBlendFactor::Zero: |
| 235 | return 0; |
| 236 | case SrcBlendFactor::One: |
| 237 | return 0xffffffff; |
| 238 | case SrcBlendFactor::DstClr: |
| 239 | return *(u32*)dstClr; |
| 240 | case SrcBlendFactor::InvDstClr: |
| 241 | return 0xffffffff - *(u32*)dstClr; |
| 242 | case SrcBlendFactor::SrcAlpha: |
| 243 | { |
| 244 | u8 alpha = srcClr[ALP_C]; |
| 245 | u32 factor = alpha << 24 | alpha << 16 | alpha << 8 | alpha; |
| 246 | return factor; |
| 247 | } |
| 248 | case SrcBlendFactor::InvSrcAlpha: |
| 249 | { |
| 250 | u8 alpha = 0xff - srcClr[ALP_C]; |
| 251 | u32 factor = alpha << 24 | alpha << 16 | alpha << 8 | alpha; |
| 252 | return factor; |
| 253 | } |
| 254 | case SrcBlendFactor::DstAlpha: |
| 255 | { |
| 256 | u8 alpha = dstClr[ALP_C]; |
| 257 | u32 factor = alpha << 24 | alpha << 16 | alpha << 8 | alpha; |
| 258 | return factor; |
| 259 | } |
| 260 | case SrcBlendFactor::InvDstAlpha: |
| 261 | { |
| 262 | u8 alpha = 0xff - dstClr[ALP_C]; |
| 263 | u32 factor = alpha << 24 | alpha << 16 | alpha << 8 | alpha; |
| 264 | return factor; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static u32 GetDestinationFactor(u8* srcClr, u8* dstClr, DstBlendFactor mode) |
| 272 | { |
| 273 | switch (mode) |
| 274 | { |
| 275 | case DstBlendFactor::Zero: |
| 276 | return 0; |
| 277 | case DstBlendFactor::One: |
| 278 | return 0xffffffff; |
| 279 | case DstBlendFactor::SrcClr: |
| 280 | return *(u32*)srcClr; |
| 281 | case DstBlendFactor::InvSrcClr: |
| 282 | return 0xffffffff - *(u32*)srcClr; |
| 283 | case DstBlendFactor::SrcAlpha: |
| 284 | { |
| 285 | u8 alpha = srcClr[ALP_C]; |
| 286 | u32 factor = alpha << 24 | alpha << 16 | alpha << 8 | alpha; |
| 287 | return factor; |
| 288 | } |
| 289 | case DstBlendFactor::InvSrcAlpha: |
| 290 | { |
| 291 | u8 alpha = 0xff - srcClr[ALP_C]; |
| 292 | u32 factor = alpha << 24 | alpha << 16 | alpha << 8 | alpha; |
| 293 | return factor; |
| 294 | } |
| 295 | case DstBlendFactor::DstAlpha: |
| 296 | { |
| 297 | u8 alpha = dstClr[ALP_C] & 0xff; |
| 298 | u32 factor = alpha << 24 | alpha << 16 | alpha << 8 | alpha; |
| 299 | return factor; |
| 300 | } |
| 301 | case DstBlendFactor::InvDstAlpha: |
| 302 | { |
| 303 | u8 alpha = 0xff - dstClr[ALP_C]; |
| 304 | u32 factor = alpha << 24 | alpha << 16 | alpha << 8 | alpha; |
| 305 | return factor; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static void BlendColor(u8* srcClr, u8* dstClr) |
| 313 | { |
| 314 | u32 srcFactor = GetSourceFactor(srcClr, dstClr, bpmem.blendmode.src_factor); |
| 315 | u32 dstFactor = GetDestinationFactor(srcClr, dstClr, bpmem.blendmode.dst_factor); |
| 316 | |
| 317 | for (int i = 0; i < 4; i++) |
| 318 | { |
| 319 | // add MSB of factors to make their range 0 -> 256 |
| 320 | u32 sf = (srcFactor & 0xff); |
| 321 | sf += sf >> 7; |
| 322 | |
| 323 | u32 df = (dstFactor & 0xff); |
| 324 | df += df >> 7; |
| 325 | |
| 326 | u32 color = (srcClr[i] * sf + dstClr[i] * df) >> 8; |
| 327 | dstClr[i] = (color > 255) ? 255 : color; |
| 328 | |
| 329 | dstFactor >>= 8; |
| 330 | srcFactor >>= 8; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | static void LogicBlend(u32 srcClr, u32* dstClr, LogicOp op) |
| 335 | { |
| 336 | switch (op) |
| 337 | { |
| 338 | case LogicOp::Clear: |
| 339 | *dstClr = 0; |
| 340 | break; |
| 341 | case LogicOp::And: |
| 342 | *dstClr = srcClr & *dstClr; |
| 343 | break; |
| 344 | case LogicOp::AndReverse: |
| 345 | *dstClr = srcClr & (~*dstClr); |
| 346 | break; |
| 347 | case LogicOp::Copy: |
| 348 | *dstClr = srcClr; |
| 349 | break; |
| 350 | case LogicOp::AndInverted: |
| 351 | *dstClr = (~srcClr) & *dstClr; |
| 352 | break; |
| 353 | case LogicOp::NoOp: |
| 354 | // Do nothing |
| 355 | break; |
| 356 | case LogicOp::Xor: |
| 357 | *dstClr = srcClr ^ *dstClr; |
| 358 | break; |
| 359 | case LogicOp::Or: |
| 360 | *dstClr = srcClr | *dstClr; |
| 361 | break; |
| 362 | case LogicOp::Nor: |
| 363 | *dstClr = ~(srcClr | *dstClr); |
| 364 | break; |
| 365 | case LogicOp::Equiv: |
| 366 | *dstClr = ~(srcClr ^ *dstClr); |
| 367 | break; |
| 368 | case LogicOp::Invert: |
| 369 | *dstClr = ~*dstClr; |
| 370 | break; |
| 371 | case LogicOp::OrReverse: |
| 372 | *dstClr = srcClr | (~*dstClr); |
| 373 | break; |
| 374 | case LogicOp::CopyInverted: |
| 375 | *dstClr = ~srcClr; |
| 376 | break; |
| 377 | case LogicOp::OrInverted: |
| 378 | *dstClr = (~srcClr) | *dstClr; |
| 379 | break; |
| 380 | case LogicOp::Nand: |
| 381 | *dstClr = ~(srcClr & *dstClr); |
| 382 | break; |
| 383 | case LogicOp::Set: |
| 384 | *dstClr = 0xffffffff; |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | static void SubtractBlend(u8* srcClr, u8* dstClr) |
| 390 | { |
| 391 | for (int i = 0; i < 4; i++) |
| 392 | { |
| 393 | int c = (int)dstClr[i] - (int)srcClr[i]; |
| 394 | dstClr[i] = (c < 0) ? 0 : c; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | static void Dither(u16 x, u16 y, u8* color) |
| 399 | { |
| 400 | // No dithering for RGB8 mode |
| 401 | if (!bpmem.blendmode.dither || bpmem.zcontrol.pixel_format != PixelFormat::RGBA6_Z24) |
| 402 | return; |
| 403 | |
| 404 | // Flipper uses a standard 2x2 Bayer Matrix for 6 bit dithering |
| 405 | static const u8 dither[2][2] = {{0, 2}, {3, 1}}; |
| 406 | |
| 407 | // Only the color channels are dithered? |
| 408 | for (int i = BLU_C; i <= RED_C; i++) |
| 409 | color[i] = ((color[i] - (color[i] >> 6)) + dither[y & 1][x & 1]) & 0xfc; |
| 410 | } |
| 411 | |
| 412 | void BlendTev(u16 x, u16 y, u8* color) |
| 413 | { |
| 414 | const u32 offset = GetColorOffset(x, y); |
| 415 | u32 dstClr = GetPixelColor(offset); |
| 416 | |
| 417 | u8* dstClrPtr = (u8*)&dstClr; |
| 418 | |
| 419 | if (bpmem.blendmode.blend_enable) |
| 420 | { |
| 421 | if (bpmem.blendmode.subtract) |
| 422 | SubtractBlend(color, dstClrPtr); |
| 423 | else |
| 424 | BlendColor(color, dstClrPtr); |
| 425 | } |
| 426 | else if (bpmem.blendmode.logic_op_enable) |
| 427 | { |
| 428 | LogicBlend(*((u32*)color), &dstClr, bpmem.blendmode.logic_mode); |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | dstClrPtr = color; |
| 433 | } |
| 434 | |
| 435 | if (bpmem.dstalpha.enable) |
| 436 | dstClrPtr[ALP_C] = bpmem.dstalpha.alpha; |
| 437 | |
| 438 | if (bpmem.blendmode.color_update) |
| 439 | { |
| 440 | Dither(x, y, dstClrPtr); |
| 441 | if (bpmem.blendmode.alpha_update) |
| 442 | SetPixelAlphaColor(offset, dstClrPtr); |
| 443 | else |
| 444 | SetPixelColorOnly(offset, dstClrPtr); |
| 445 | } |
| 446 | else if (bpmem.blendmode.alpha_update) |
| 447 | { |
| 448 | SetPixelAlphaOnly(offset, dstClrPtr[ALP_C]); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | void SetColor(u16 x, u16 y, u8* color) |
| 453 | { |
| 454 | u32 offset = GetColorOffset(x, y); |
| 455 | if (bpmem.blendmode.color_update) |
| 456 | { |
| 457 | if (bpmem.blendmode.alpha_update) |
| 458 | SetPixelAlphaColor(offset, color); |
| 459 | else |
| 460 | SetPixelColorOnly(offset, color); |
| 461 | } |
| 462 | else if (bpmem.blendmode.alpha_update) |
| 463 | { |
| 464 | SetPixelAlphaOnly(offset, color[ALP_C]); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | void SetDepth(u16 x, u16 y, u32 depth) |
| 469 | { |
| 470 | if (bpmem.zmode.update_enable) |
| 471 | SetPixelDepth(GetDepthOffset(x, y), depth); |
| 472 | } |
| 473 | |
| 474 | static u32 GetColor(u16 x, u16 y) |
| 475 | { |
| 476 | u32 offset = GetColorOffset(x, y); |
| 477 | return GetPixelColor(offset); |
| 478 | } |
| 479 | |
| 480 | static u32 VerticalFilter(const std::array<u32, 3>& colors, |
| 481 | const std::array<u8, 7>& filterCoefficients) |
| 482 | { |
| 483 | u8 in_colors[3][4]; |
| 484 | std::memcpy(&in_colors, colors.data(), sizeof(in_colors)); |
| 485 | |
| 486 | // Alpha channel is not used |
| 487 | u8 out_color[4]; |
| 488 | out_color[ALP_C] = 0; |
| 489 | |
| 490 | // All Coefficients should sum to 64, otherwise the total brightness will change, which many games |
| 491 | // do on purpose to implement a brightness filter across the whole copy. |
| 492 | for (int i = BLU_C; i <= RED_C; i++) |
| 493 | { |
| 494 | // TODO: implement support for multisampling. |
| 495 | // In non-multisampling mode: |
| 496 | // * Coefficients 2, 3 and 4 sample from the current pixel. |
| 497 | // * Coefficients 0 and 1 sample from the pixel above this one |
| 498 | // * Coefficients 5 and 6 sample from the pixel below this one |
| 499 | // |
| 500 | // We normally don't implement enhancements in the software renderer. |
| 501 | // However, disabling the copy filter is useful for debugging. |
| 502 | const bool disable_copy_filter = Config::Get(Config::GFX_ENHANCE_DISABLE_COPY_FILTER); |
| 503 | int sum; |
| 504 | if (disable_copy_filter) |
| 505 | { |
| 506 | sum = |
| 507 | in_colors[1][i] * (filterCoefficients[0] + filterCoefficients[1] + filterCoefficients[2] + |
| 508 | filterCoefficients[3] + filterCoefficients[4] + filterCoefficients[5] + |
| 509 | filterCoefficients[6]); |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | sum = in_colors[0][i] * (filterCoefficients[0] + filterCoefficients[1]) + |
| 514 | in_colors[1][i] * |
| 515 | (filterCoefficients[2] + filterCoefficients[3] + filterCoefficients[4]) + |
| 516 | in_colors[2][i] * (filterCoefficients[5] + filterCoefficients[6]); |
| 517 | } |
| 518 | |
| 519 | // TODO: this clamping behavior appears to be correct, but isn't confirmed on hardware. |
| 520 | out_color[i] = std::min(255, sum >> 6); // clamp larger values to 255 |
| 521 | } |
| 522 | |
| 523 | u32 out_color32; |
| 524 | std::memcpy(&out_color32, out_color, sizeof(out_color32)); |
| 525 | return out_color32; |
| 526 | } |
| 527 | |
| 528 | static u32 GammaCorrection(u32 color, const float gamma_rcp) |
| 529 | { |
| 530 | u8 in_colors[4]; |
| 531 | std::memcpy(&in_colors, &color, sizeof(in_colors)); |
| 532 | |
| 533 | u8 out_color[4]; |
| 534 | for (int i = BLU_C; i <= RED_C; i++) |
| 535 | { |
| 536 | out_color[i] = static_cast<u8>( |
| 537 | std::clamp(std::pow(in_colors[i] / 255.0f, gamma_rcp) * 255.0f, 0.0f, 255.0f)); |
| 538 | } |
| 539 | |
| 540 | u32 out_color32; |
| 541 | std::memcpy(&out_color32, out_color, sizeof(out_color32)); |
| 542 | return out_color32; |
| 543 | } |
| 544 | |
| 545 | // For internal used only, return a non-normalized value, which saves work later. |
| 546 | static yuv444 ConvertColorToYUV(u32 color) |
| 547 | { |
| 548 | const u8 red = static_cast<u8>(color >> 24); |
| 549 | const u8 green = static_cast<u8>(color >> 16); |
| 550 | const u8 blue = static_cast<u8>(color >> 8); |
| 551 | |
| 552 | // GameCube/Wii uses the BT.601 standard algorithm for converting to YCbCr; see |
| 553 | // http://www.equasys.de/colorconversion.html#YCbCr-RGBColorFormatConversion |
| 554 | // These numbers were determined by hardware testing |
| 555 | const u16 y = +66 * red + 129 * green + +25 * blue; |
| 556 | const s16 u = -38 * red + -74 * green + 112 * blue; |
| 557 | const s16 v = 112 * red + -94 * green + -18 * blue; |
| 558 | const u8 y_round = static_cast<u8>((y >> 8) + ((y >> 7) & 1)); |
| 559 | const s8 u_round = static_cast<s8>((u >> 8) + ((u >> 7) & 1)); |
| 560 | const s8 v_round = static_cast<s8>((v >> 8) + ((v >> 7) & 1)); |
| 561 | return {y_round, u_round, v_round}; |
| 562 | } |
| 563 | |
| 564 | static u32 GetDepth(u16 x, u16 y) |
| 565 | { |
| 566 | u32 offset = GetDepthOffset(x, y); |
| 567 | return GetPixelDepth(offset); |
| 568 | } |
| 569 | |
| 570 | u8* GetPixelPointer(u16 x, u16 y, bool depth) |
| 571 | { |
| 572 | if (depth) |
| 573 | return &efb[GetDepthOffset(x, y)]; |
| 574 | return &efb[GetColorOffset(x, y)]; |
| 575 | } |
| 576 | |
| 577 | void EncodeXFB(u8* xfb_in_ram, u32 memory_stride, const MathUtil::Rectangle<int>& source_rect, |
| 578 | float y_scale, float gamma) |
| 579 | { |
| 580 | if (!xfb_in_ram) |
| 581 | { |
| 582 | WARN_LOG_FMT(VIDEO, "Tried to copy to invalid XFB address"); |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | const int left = source_rect.left; |
| 587 | const int right = source_rect.right; |
| 588 | const bool clamp_top = bpmem.triggerEFBCopy.clamp_top; |
| 589 | const bool clamp_bottom = bpmem.triggerEFBCopy.clamp_bottom; |
| 590 | const float gamma_rcp = 1.0f / gamma; |
| 591 | const auto filter_coefficients = bpmem.copyfilter.GetCoefficients(); |
| 592 | |
| 593 | // this assumes copies will always start on an even (YU) pixel and the |
| 594 | // copy always has an even width, which might not be true. |
| 595 | if (left & 1 || right & 1) |
| 596 | { |
| 597 | WARN_LOG_FMT(VIDEO, "Trying to copy XFB to from unaligned EFB source"); |
| 598 | // this will show up as wrongly encoded |
| 599 | } |
| 600 | |
| 601 | // Scanline buffer, leave room for borders |
| 602 | yuv444 scanline[EFB_WIDTH + 2]; |
| 603 | |
| 604 | static std::vector<yuv422_packed> source; |
| 605 | source.resize(EFB_WIDTH * EFB_HEIGHT); |
| 606 | yuv422_packed* src_ptr = &source[0]; |
| 607 | |
| 608 | for (int y = source_rect.top; y < source_rect.bottom; y++) |
| 609 | { |
| 610 | // Clamping behavior |
| 611 | // NOTE: when the clamp bits aren't set, the hardware will happily read beyond the EFB, |
| 612 | // which returns random garbage from the empty bus (confirmed by hardware tests). |
| 613 | // |
| 614 | // In our implementation, the garbage just so happens to be the top or bottom row. |
| 615 | // Statistically, that could happen. |
| 616 | const u16 y_prev = static_cast<u16>(std::max(clamp_top ? source_rect.top : 0, y - 1)); |
| 617 | const u16 y_next = static_cast<u16>( |
| 618 | std::min<int>((clamp_bottom ? source_rect.bottom : EFB_HEIGHT) - 1, y + 1)); |
| 619 | |
| 620 | // Get a scanline of YUV pixels in 4:4:4 format |
| 621 | for (int i = 1, x = left; x < right; i++, x++) |
| 622 | { |
| 623 | // Get RGB colors |
| 624 | std::array<u32, 3> colors = {{GetColor(x, y_prev), GetColor(x, y), GetColor(x, y_next)}}; |
| 625 | |
| 626 | // Vertical Filter (Multisampling resolve, deflicker, brightness) |
| 627 | u32 filtered = VerticalFilter(colors, filter_coefficients); |
| 628 | |
| 629 | // Gamma correction happens here. |
| 630 | filtered = GammaCorrection(filtered, gamma_rcp); |
| 631 | |
| 632 | scanline[i] = ConvertColorToYUV(filtered); |
| 633 | } |
| 634 | |
| 635 | // Flipper clamps the border colors |
| 636 | scanline[0] = scanline[1]; |
| 637 | scanline[right + 1] = scanline[right]; |
| 638 | |
| 639 | // And Downsample them to 4:2:2 |
| 640 | for (int i = 1, x = left; x < right; i += 2, x += 2) |
| 641 | { |
| 642 | // YU pixel |
| 643 | src_ptr[x].Y = scanline[i].Y + 16; |
| 644 | // we mix our color differences in 10 bit space so it will round more accurately |
| 645 | // U[i] = 1/4 * U[i-1] + 1/2 * U[i] + 1/4 * U[i+1] |
| 646 | src_ptr[x].UV = 128 + ((scanline[i - 1].U + (scanline[i].U << 1) + scanline[i + 1].U) >> 2); |
| 647 | |
| 648 | // YV pixel |
| 649 | src_ptr[x + 1].Y = scanline[i + 1].Y + 16; |
| 650 | // V[i] = 1/4 * V[i-1] + 1/2 * V[i] + 1/4 * V[i+1] |
| 651 | src_ptr[x + 1].UV = |
| 652 | 128 + ((scanline[i - 1].V + (scanline[i].V << 1) + scanline[i + 1].V) >> 2); |
| 653 | } |
| 654 | src_ptr += memory_stride; |
| 655 | } |
| 656 | |
| 657 | const int src_width = source_rect.GetWidth(); |
| 658 | const int src_height = source_rect.GetHeight(); |
| 659 | const int dst_width = src_width; |
| 660 | const int dst_height = src_height * y_scale; |
| 661 | |
| 662 | SW::CopyRegion(source.data(), src_width, src_height, reinterpret_cast<yuv422_packed*>(xfb_in_ram), |
| 663 | dst_width, dst_height); |
| 664 | } |
| 665 | |
| 666 | bool ZCompare(u16 x, u16 y, u32 z) |
| 667 | { |
| 668 | u32 offset = GetDepthOffset(x, y); |
| 669 | u32 depth = GetPixelDepth(offset); |
| 670 | |
| 671 | bool pass; |
| 672 | |
| 673 | switch (bpmem.zmode.func) |
| 674 | { |
| 675 | case CompareMode::Never: |
| 676 | pass = false; |
| 677 | break; |
| 678 | case CompareMode::Less: |
| 679 | pass = z < depth; |
| 680 | break; |
| 681 | case CompareMode::Equal: |
| 682 | pass = z == depth; |
| 683 | break; |
| 684 | case CompareMode::LEqual: |
| 685 | pass = z <= depth; |
| 686 | break; |
| 687 | case CompareMode::Greater: |
| 688 | pass = z > depth; |
| 689 | break; |
| 690 | case CompareMode::NEqual: |
| 691 | pass = z != depth; |
| 692 | break; |
| 693 | case CompareMode::GEqual: |
| 694 | pass = z >= depth; |
| 695 | break; |
| 696 | case CompareMode::Always: |
| 697 | pass = true; |
| 698 | break; |
| 699 | default: |
| 700 | pass = false; |
| 701 | ERROR_LOG_FMT(VIDEO, "Bad Z compare mode {}", bpmem.zmode.func); |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | if (pass && bpmem.zmode.update_enable) |
| 706 | { |
| 707 | SetPixelDepth(offset, z); |
| 708 | } |
| 709 | |
| 710 | return pass; |
| 711 | } |
| 712 | |
| 713 | u32 GetPerfQueryResult(PerfQueryType type) |
| 714 | { |
| 715 | return perf_values[type]; |
| 716 | } |
| 717 | |
| 718 | void ResetPerfQuery() |
| 719 | { |
| 720 | perf_values = {}; |
| 721 | } |
| 722 | |
| 723 | void IncPerfCounterQuadCount(PerfQueryType type) |
| 724 | { |
| 725 | // NOTE: hardware doesn't process individual pixels but quads instead. |
| 726 | // Current software renderer architecture works on pixels though, so |
| 727 | // we have this "quad" hack here to only increment the registers on |
| 728 | // every fourth rendered pixel |
| 729 | static u32 quad[PQ_NUM_MEMBERS]; |
| 730 | if (++quad[type] != 3) |
| 731 | return; |
| 732 | quad[type] = 0; |
| 733 | ++perf_values[type]; |
| 734 | } |
| 735 | } // namespace EfbInterface |
| 736 | |
| 737 | namespace SW |
| 738 | { |
| 739 | |
| 740 | void SWEFBInterface::ReinterpretPixelData(EFBReinterpretType convtype) |
| 741 | { |
| 742 | } |
| 743 | |
| 744 | void SWEFBInterface::PokeColor(u16 x, u16 y, u32 color) |
| 745 | { |
| 746 | } |
| 747 | |
| 748 | void SWEFBInterface::PokeDepth(u16 x, u16 y, u32 depth) |
| 749 | { |
| 750 | } |
| 751 | |
| 752 | u32 SWEFBInterface::PeekColorInternal(u16 x, u16 y) |
| 753 | { |
| 754 | const u32 color = EfbInterface::GetColor(x, y); |
| 755 | |
| 756 | // rgba to argb |
| 757 | u32 value = (color >> 8) | (color & 0xff) << 24; |
| 758 | |
| 759 | return value; |
| 760 | } |
| 761 | |
| 762 | u32 SWEFBInterface::PeekDepthInternal(u16 x, u16 y) |
| 763 | { |
| 764 | return EfbInterface::GetDepth(x, y); |
| 765 | } |
| 766 | |
| 767 | } // namespace SW |