Debian Codegraph @

Source/Core/VideoCommon/EFBInterface.cpp

raw symbols SCIP
1 // Copyright 2025 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/EFBInterface.h"
5
6 #include <algorithm>
7 #include <memory>
8
9 #include "Common/MsgHandler.h"
10
11 #include "Core/ConfigManager.h"
12 #include "Core/System.h"
13
14 #include "VideoCommon/AsyncRequests.h"
15 #include "VideoCommon/FramebufferManager.h"
16 #include "VideoCommon/PixelEngine.h"
17 #include "VideoCommon/Statistics.h"
18 #include "VideoCommon/VideoBackendBase.h"
19 #include "VideoCommon/VideoCommon.h"
20 #include "VideoCommon/VideoConfig.h"
21
22 std::unique_ptr<EFBInterfaceBase> g_efb_interface;
23
24 EFBInterfaceBase::~EFBInterfaceBase() = default;
25
26 bool EFBInterfaceBase::ShouldSkipAccess(u16 x, u16 y) const
27 {
28 return !g_ActiveConfig.bEFBAccessEnable || x >= EFB_WIDTH || y >= EFB_HEIGHT;
29 }
30
31 void HardwareEFBInterface::ReinterpretPixelData(EFBReinterpretType convtype)
32 {
33 g_framebuffer_manager->ReinterpretPixelData(convtype);
34 }
35
36 u32 HardwareEFBInterface::PeekColorInternal(u16 x, u16 y)
37 {
38 u32 color = AsyncRequests::GetInstance()->PushBlockingEvent([&] {
39 INCSTAT(g_stats.this_frame.num_efb_peeks);
40 return g_framebuffer_manager->PeekEFBColor(x, y);
41 });
42
43 // a little-endian value is expected to be returned
44 color = ((color & 0xFF00FF00) | ((color >> 16) & 0xFF) | ((color << 16) & 0xFF0000));
45
46 if (bpmem.zcontrol.pixel_format == PixelFormat::RGBA6_Z24)
47 {
48 color = RGBA8ToRGBA6ToRGBA8(color);
49 }
50 else if (bpmem.zcontrol.pixel_format == PixelFormat::RGB565_Z16)
51 {
52 color = RGBA8ToRGB565ToRGBA8(color);
53 }
54 if (bpmem.zcontrol.pixel_format != PixelFormat::RGBA6_Z24)
55 {
56 color |= 0xFF000000;
57 }
58
59 return color;
60 }
61
62 u32 EFBInterfaceBase::PeekColor(u16 x, u16 y)
63 {
64 if (ShouldSkipAccess(x, y))
65 return 0;
66
67 u32 color = PeekColorInternal(x, y);
68
69 // check what to do with the alpha channel (GX_PokeAlphaRead)
70 PixelEngine::AlphaReadMode alpha_read_mode =
71 Core::System::GetInstance().GetPixelEngine().GetAlphaReadMode();
72
73 if (alpha_read_mode == PixelEngine::AlphaReadMode::ReadNone)
74 {
75 return color;
76 }
77 else if (alpha_read_mode == PixelEngine::AlphaReadMode::ReadFF)
78 {
79 return color | 0xFF000000;
80 }
81 else
82 {
83 if (alpha_read_mode != PixelEngine::AlphaReadMode::Read00)
84 {
85 PanicAlertFmt("Invalid PE alpha read mode: {}", static_cast<u16>(alpha_read_mode));
86 }
87 return color & 0x00FFFFFF;
88 }
89 }
90
91 u32 HardwareEFBInterface::PeekDepthInternal(u16 x, u16 y)
92 {
93 float depth = AsyncRequests::GetInstance()->PushBlockingEvent([&] {
94 INCSTAT(g_stats.this_frame.num_efb_peeks);
95 return g_framebuffer_manager->PeekEFBDepth(x, y);
96 });
97
98 // Depth buffer is inverted for improved precision near far plane
99 if (!g_backend_info.bSupportsReversedDepthRange)
100 depth = 1.0f - depth;
101
102 // Convert to 24bit depth
103 u32 z24depth = std::clamp<u32>(static_cast<u32>(depth * 16777216.0f), 0, 0xFFFFFF);
104
105 if (bpmem.zcontrol.pixel_format == PixelFormat::RGB565_Z16)
106 {
107 // When in RGB565_Z16 mode, EFB Z peeks return a 16bit value, which is presumably a
108 // resolved sample from the MSAA buffer.
109 // Dolphin doesn't currently emulate the 3 sample MSAA mode (and potentially never will)
110 // it just transparently upgrades the framebuffer to 24bit depth and color and whatever
111 // level of MSAA and higher Internal Resolution the user has configured.
112
113 // This is mostly transparent, unless the game does an EFB read.
114 // But we can simply convert the 24bit depth on the fly to the 16bit depth the game expects.
115
116 return CompressZ16(z24depth, bpmem.zcontrol.zformat);
117 }
118
119 return z24depth;
120 }
121
122 u32 EFBInterfaceBase::PeekDepth(u16 x, u16 y)
123 {
124 if (ShouldSkipAccess(x, y))
125 return 0;
126
127 return PeekDepthInternal(x, y);
128 }
129
130 void HardwareEFBInterface::PokeColor(u16 x, u16 y, u32 poke_data)
131 {
132 if (ShouldSkipAccess(x, y))
133 return;
134
135 // Convert to expected format (BGRA->RGBA)
136 // TODO: Check alpha, depending on mode?
137 const u32 color =
138 ((poke_data & 0xFF00FF00) | ((poke_data >> 16) & 0xFF) | ((poke_data << 16) & 0xFF0000));
139
140 AsyncRequests::GetInstance()->PushEvent([x, y, color] {
141 INCSTAT(g_stats.this_frame.num_efb_pokes);
142 g_framebuffer_manager->PokeEFBColor(x, y, color);
143 });
144 }
145
146 void HardwareEFBInterface::PokeDepth(u16 x, u16 y, u32 poke_data)
147 {
148 if (ShouldSkipAccess(x, y))
149 return;
150
151 // Convert to floating-point depth.
152 float depth = float(poke_data & 0xFFFFFF) / 16777216.0f;
153 if (!g_backend_info.bSupportsReversedDepthRange)
154 depth = 1.0f - depth;
155
156 AsyncRequests::GetInstance()->PushEvent([x, y, depth] {
157 INCSTAT(g_stats.this_frame.num_efb_pokes);
158 g_framebuffer_manager->PokeEFBDepth(x, y, depth);
159 });
160 }