VideoCommon.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <algorithm>
  5. #include <bit>
  6. #include "Common/BitUtils.h"
  7. #include "Common/CommonTypes.h"
  8. #include "VideoCommon/BPMemory.h"
  9. // These are accurate (disregarding AA modes).
  10. constexpr u32 EFB_WIDTH = 640u;
  11. constexpr u32 EFB_HEIGHT = 528u;
  12. // Max XFB width is 720. You can only copy out 640 wide areas of efb to XFB
  13. // so you need multiple copies to do the full width.
  14. // The VI can do horizontal scaling (TODO: emulate).
  15. constexpr u32 MAX_XFB_WIDTH = 720u;
  16. // Although EFB height is 528, 576-line XFB's can be created either with
  17. // vertical scaling by the EFB copy operation or copying to multiple XFB's
  18. // that are next to each other in memory (TODO: handle that situation).
  19. constexpr u32 MAX_XFB_HEIGHT = 576u;
  20. #define PRIM_LOG(t, ...) DEBUG_LOG_FMT(VIDEO, t __VA_OPT__(, ) __VA_ARGS__)
  21. // warning: mapping buffer should be disabled to use this
  22. // #define LOG_VTX() DEBUG_LOG_FMT(VIDEO, "vtx: {} {} {}, ",
  23. // ((float*)g_vertex_manager_write_ptr)[-3],
  24. // ((float*)g_vertex_manager_write_ptr)[-2],
  25. // ((float*)g_vertex_manager_write_ptr)[-1]);
  26. #define LOG_VTX()
  27. enum class APIType
  28. {
  29. OpenGL,
  30. D3D,
  31. Vulkan,
  32. Metal,
  33. Nothing
  34. };
  35. inline u32 RGBA8ToRGBA6ToRGBA8(u32 src)
  36. {
  37. u32 color = src;
  38. color &= 0xFCFCFCFC;
  39. color |= (color >> 6) & 0x03030303;
  40. return color;
  41. }
  42. inline u32 RGBA8ToRGB565ToRGBA8(u32 src)
  43. {
  44. u32 color = (src & 0xF8FCF8);
  45. color |= (color >> 5) & 0x070007;
  46. color |= (color >> 6) & 0x000300;
  47. color |= 0xFF000000;
  48. return color;
  49. }
  50. inline u32 Z24ToZ16ToZ24(u32 src)
  51. {
  52. return (src & 0xFFFF00) | (src >> 16);
  53. }
  54. inline u32 CompressZ16(u32 z24depth, DepthFormat format)
  55. {
  56. // Flipper offers a number of choices for 16bit Z formats that adjust
  57. // where the bulk of the precision lies.
  58. if (format == DepthFormat::ZLINEAR)
  59. {
  60. // This is just a linear depth buffer with 16 bits of precision
  61. return z24depth >> 8;
  62. }
  63. // ZNEAR/ZMID/ZFAR are custom floating point formats with 2/3/4 bits of exponent
  64. // The exponent is simply the number of leading ones that have been removed
  65. // The first zero bit is skipped and not stored. The mantissa contains the next 14/13/12 bits
  66. // If exponent is at the MAX (3, 7, or 12) then the next bit might still be a one, and can't
  67. // be skipped, so the mantissa simply contains the next 14/13/12 bits
  68. u32 leading_ones = static_cast<u32>(std::countl_one(z24depth << 8));
  69. bool next_bit_is_one = false; // AKA: Did we clamp leading_ones?
  70. u32 exp_bits;
  71. switch (format)
  72. {
  73. case DepthFormat::ZNEAR:
  74. exp_bits = 2;
  75. if (leading_ones >= 3u)
  76. {
  77. leading_ones = 3u;
  78. next_bit_is_one = true;
  79. }
  80. break;
  81. case DepthFormat::ZMID:
  82. exp_bits = 3;
  83. if (leading_ones >= 7u)
  84. {
  85. leading_ones = 7u;
  86. next_bit_is_one = true;
  87. }
  88. break;
  89. case DepthFormat::ZFAR:
  90. exp_bits = 4;
  91. if (leading_ones >= 12u)
  92. {
  93. // The hardware implementation only uses values 0 to 12 in the exponent
  94. leading_ones = 12u;
  95. next_bit_is_one = true;
  96. }
  97. break;
  98. default:
  99. return z24depth >> 8;
  100. }
  101. u32 mantissa_bits = 16 - exp_bits;
  102. // Calculate which bits we need to extract from z24depth for our mantissa
  103. u32 top = std::max<u32>(24 - leading_ones, mantissa_bits);
  104. if (!next_bit_is_one)
  105. {
  106. top -= 1; // We know the next bit is zero, so we don't need to include it.
  107. }
  108. u32 bottom = top - mantissa_bits;
  109. u32 exponent = leading_ones << mantissa_bits; // Upper bits contain exponent
  110. u32 mantissa = Common::ExtractBits(z24depth, bottom, top - 1);
  111. return exponent | mantissa;
  112. }