Debian Codegraph @

Source/Core/VideoCommon/PixelEngine.cpp

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/PixelEngine.h"
5
6 #include <mutex>
7
8 #include "Common/BitField.h"
9 #include "Common/ChunkFile.h"
10 #include "Common/CommonTypes.h"
11 #include "Common/Logging/Log.h"
12
13 #include "Core/ConfigManager.h"
14 #include "Core/Core.h"
15 #include "Core/CoreTiming.h"
16 #include "Core/HW/MMIO.h"
17 #include "Core/HW/ProcessorInterface.h"
18 #include "Core/System.h"
19
20 #include "VideoCommon/BoundingBox.h"
21 #include "VideoCommon/Fifo.h"
22 #include "VideoCommon/PerfQueryBase.h"
23 #include "VideoCommon/VideoBackendBase.h"
24
25 namespace PixelEngine
26 {
27 enum
28 {
29 INT_CAUSE_PE_TOKEN = 0x200, // GP Token
30 INT_CAUSE_PE_FINISH = 0x400, // GP Finished
31 };
32
33 PixelEngineManager::PixelEngineManager(Core::System& system) : m_system{system}
34 {
35 }
36
37 void PixelEngineManager::DoState(PointerWrap& p)
38 {
39 p.Do(m_z_conf);
40 p.Do(m_alpha_conf);
41 p.Do(m_dst_alpha_conf);
42 p.Do(m_alpha_mode_conf);
43 p.Do(m_alpha_read);
44 p.Do(m_control);
45
46 p.Do(m_token);
47 p.Do(m_token_pending);
48 p.Do(m_token_interrupt_pending);
49 p.Do(m_finish_interrupt_pending);
50 p.Do(m_event_raised);
51
52 p.Do(m_signal_token_interrupt);
53 p.Do(m_signal_finish_interrupt);
54 }
55
56 void PixelEngineManager::Init()
57 {
58 m_control.hex = 0;
59 m_z_conf.hex = 0;
60 m_alpha_conf.hex = 0;
61 m_dst_alpha_conf.hex = 0;
62 m_alpha_mode_conf.hex = 0;
63 m_alpha_read.hex = 0;
64
65 m_token = 0;
66 m_token_pending = 0;
67 m_token_interrupt_pending = false;
68 m_finish_interrupt_pending = false;
69 m_event_raised = false;
70
71 m_signal_token_interrupt = false;
72 m_signal_finish_interrupt = false;
73
74 m_event_type_set_token_finish =
75 m_system.GetCoreTiming().RegisterEvent("SetTokenFinish", SetTokenFinish_OnMainThread_Static);
76 }
77
78 void PixelEngineManager::RegisterMMIO(MMIO::Mapping* mmio, u32 base)
79 {
80 // Directly mapped registers.
81 struct
82 {
83 u32 addr;
84 u16* ptr;
85 } directly_mapped_vars[] = {
86 {PE_ZCONF, &m_z_conf.hex},
87 {PE_ALPHACONF, &m_alpha_conf.hex},
88 {PE_DSTALPHACONF, &m_dst_alpha_conf.hex},
89 {PE_ALPHAMODE, &m_alpha_mode_conf.hex},
90 {PE_ALPHAREAD, &m_alpha_read.hex},
91 };
92 for (auto& mapped_var : directly_mapped_vars)
93 {
94 mmio->Register(base | mapped_var.addr, MMIO::DirectRead<u16>(mapped_var.ptr),
95 MMIO::DirectWrite<u16>(mapped_var.ptr));
96 }
97
98 // Performance queries registers: read only, need to call the video backend
99 // to get the results.
100 struct
101 {
102 u32 addr;
103 PerfQueryType pqtype;
104 } pq_regs[] = {
105 {PE_PERF_ZCOMP_INPUT_ZCOMPLOC_L, PQ_ZCOMP_INPUT_ZCOMPLOC},
106 {PE_PERF_ZCOMP_OUTPUT_ZCOMPLOC_L, PQ_ZCOMP_OUTPUT_ZCOMPLOC},
107 {PE_PERF_ZCOMP_INPUT_L, PQ_ZCOMP_INPUT},
108 {PE_PERF_ZCOMP_OUTPUT_L, PQ_ZCOMP_OUTPUT},
109 {PE_PERF_BLEND_INPUT_L, PQ_BLEND_INPUT},
110 {PE_PERF_EFB_COPY_CLOCKS_L, PQ_EFB_COPY_CLOCKS},
111 };
112 for (auto& pq_reg : pq_regs)
113 {
114 mmio->Register(base | pq_reg.addr, MMIO::ComplexRead<u16>([pq_reg](Core::System&, u32) {
115 return g_video_backend->Video_GetQueryResult(pq_reg.pqtype) & 0xFFFF;
116 }),
117 MMIO::InvalidWrite<u16>());
118 mmio->Register(base | (pq_reg.addr + 2), MMIO::ComplexRead<u16>([pq_reg](Core::System&, u32) {
119 return g_video_backend->Video_GetQueryResult(pq_reg.pqtype) >> 16;
120 }),
121 MMIO::InvalidWrite<u16>());
122 }
123
124 // Control register
125 mmio->Register(base | PE_CTRL_REGISTER, MMIO::DirectRead<u16>(&m_control.hex),
126 MMIO::ComplexWrite<u16>([](Core::System& system, u32, u16 val) {
127 auto& pe = system.GetPixelEngine();
128
129 UPECtrlReg tmpCtrl{.hex = val};
130
131 if (tmpCtrl.pe_token)
132 pe.m_signal_token_interrupt = false;
133
134 if (tmpCtrl.pe_finish)
135 pe.m_signal_finish_interrupt = false;
136
137 pe.m_control.pe_token_enable = tmpCtrl.pe_token_enable.Value();
138 pe.m_control.pe_finish_enable = tmpCtrl.pe_finish_enable.Value();
139 pe.m_control.pe_token = false; // this flag is write only
140 pe.m_control.pe_finish = false; // this flag is write only
141
142 DEBUG_LOG_FMT(PIXELENGINE, "(w16) CTRL_REGISTER: {:#06x}", val);
143 pe.UpdateInterrupts();
144 }));
145
146 // Token register, readonly.
147 mmio->Register(base | PE_TOKEN_REG, MMIO::DirectRead<u16>(&m_token), MMIO::InvalidWrite<u16>());
148
149 // BBOX registers, readonly and need to update a flag.
150 for (int i = 0; i < 4; ++i)
151 {
152 mmio->Register(base | (PE_BBOX_LEFT + 2 * i),
153 MMIO::ComplexRead<u16>([i](Core::System& system, u32) {
154 g_bounding_box->Disable(system.GetPixelShaderManager());
155 return g_video_backend->Video_GetBoundingBox(i);
156 }),
157 MMIO::InvalidWrite<u16>());
158 }
159 }
160
161 void PixelEngineManager::UpdateInterrupts()
162 {
163 auto& processor_interface = m_system.GetProcessorInterface();
164
165 // check if there is a token-interrupt
166 processor_interface.SetInterrupt(INT_CAUSE_PE_TOKEN,
167 m_signal_token_interrupt && m_control.pe_token_enable);
168
169 // check if there is a finish-interrupt
170 processor_interface.SetInterrupt(INT_CAUSE_PE_FINISH,
171 m_signal_finish_interrupt && m_control.pe_finish_enable);
172 }
173
174 void PixelEngineManager::SetTokenFinish_OnMainThread_Static(Core::System& system, u64 userdata,
175 s64 cycles_late)
176 {
177 system.GetPixelEngine().SetTokenFinish_OnMainThread(userdata, cycles_late);
178 }
179
180 void PixelEngineManager::SetTokenFinish_OnMainThread(u64 userdata, s64 cycles_late)
181 {
182 std::unique_lock lk(m_token_finish_mutex);
183 m_event_raised = false;
184
185 m_token = m_token_pending;
186
187 if (m_token_interrupt_pending)
188 {
189 m_token_interrupt_pending = false;
190 m_signal_token_interrupt = true;
191 UpdateInterrupts();
192 }
193
194 if (m_finish_interrupt_pending)
195 {
196 m_finish_interrupt_pending = false;
197 m_signal_finish_interrupt = true;
198 UpdateInterrupts();
199 lk.unlock();
200 Core::FrameUpdateOnCPUThread();
201 }
202 }
203
204 // Raise the event handler above on the CPU thread.
205 // m_token_finish_mutex must be locked.
206 // THIS IS EXECUTED FROM VIDEO THREAD
207 void PixelEngineManager::RaiseEvent(int cycles_into_future)
208 {
209 if (m_event_raised)
210 return;
211
212 m_event_raised = true;
213
214 CoreTiming::FromThread from = CoreTiming::FromThread::NON_CPU;
215 s64 cycles = 0; // we don't care about timings for dual core mode.
216 if (!m_system.IsDualCoreMode() || m_system.GetFifo().UseDeterministicGPUThread())
217 {
218 from = CoreTiming::FromThread::CPU;
219
220 // Hack: Dolphin's single-core gpu timings are way too fast. Enforce a minimum delay to give
221 // games time to setup any interrupt state
222 cycles = std::max(500, cycles_into_future);
223 }
224 m_system.GetCoreTiming().ScheduleEvent(cycles, m_event_type_set_token_finish, 0, from);
225 }
226
227 // SetToken
228 // THIS IS EXECUTED FROM VIDEO THREAD
229 void PixelEngineManager::SetToken(const u16 token, const bool interrupt, int cycles_into_future)
230 {
231 DEBUG_LOG_FMT(PIXELENGINE, "VIDEO Backend raises INT_CAUSE_PE_TOKEN (btw, token: {:04x})", token);
232
233 std::lock_guard lk(m_token_finish_mutex);
234
235 m_token_pending = token;
236 m_token_interrupt_pending |= interrupt;
237
238 RaiseEvent(cycles_into_future);
239 }
240
241 // SetFinish
242 // THIS IS EXECUTED FROM VIDEO THREAD (BPStructs.cpp) when a new frame has been drawn
243 void PixelEngineManager::SetFinish(int cycles_into_future)
244 {
245 DEBUG_LOG_FMT(PIXELENGINE, "VIDEO Set Finish");
246
247 std::lock_guard lk(m_token_finish_mutex);
248
249 m_finish_interrupt_pending |= true;
250
251 RaiseEvent(cycles_into_future);
252 }
253
254 } // namespace PixelEngine