Debian Codegraph @

Source/Core/VideoCommon/VideoCommon.h

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