Debian Codegraph @

Source/Core/VideoCommon/Fifo.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 <atomic>
7 #include <cstddef>
8 #include <optional>
9
10 #include "Common/BlockingLoop.h"
11 #include "Common/CommonTypes.h"
12 #include "Common/Config/Config.h"
13 #include "Common/Event.h"
14 #include "Common/Flag.h"
15
16 class PointerWrap;
17
18 namespace Core
19 {
20 class System;
21 }
22 namespace CoreTiming
23 {
24 struct EventType;
25 }
26
27 namespace Fifo
28 {
29 // Used for diagnostics.
30 enum class SyncGPUReason
31 {
32 Other,
33 Wraparound,
34 EFBPoke,
35 PerfQuery,
36 BBox,
37 Swap,
38 AuxSpace,
39 };
40
41 class FifoManager final
42 {
43 public:
44 explicit FifoManager(Core::System& system);
45 FifoManager(const FifoManager& other) = delete;
46 FifoManager(FifoManager&& other) = delete;
47 FifoManager& operator=(const FifoManager& other) = delete;
48 FifoManager& operator=(FifoManager&& other) = delete;
49 ~FifoManager();
50
51 void Init();
52 void Shutdown();
53 void Prepare(); // Must be called from the CPU thread.
54 void DoState(PointerWrap& f);
55 void PauseAndLock();
56 void RestoreState(bool was_running);
57 void UpdateWantDeterminism(bool want);
58 bool UseDeterministicGPUThread() const { return m_use_deterministic_gpu_thread; }
59 bool UseSyncGPU() const { return m_config_sync_gpu; }
60
61 // In deterministic GPU thread mode this waits for the GPU to be done with pending work.
62 void SyncGPU(SyncGPUReason reason, bool may_move_read_ptr = true);
63
64 // In single core mode, this runs the GPU for a single slice.
65 // In dual core mode, this synchronizes with the GPU thread.
66 void SyncGPUForRegisterAccess();
67
68 void PushFifoAuxBuffer(const void* ptr, size_t size);
69 void* PopFifoAuxBuffer(size_t size);
70
71 void FlushGpu();
72 void RunGpu();
73 void GpuMaySleep();
74 void RunGpuLoop();
75 void ExitGpuLoop();
76 void EmulatorState(bool running);
77 void ResetVideoBuffer();
78
79 private:
80 void RefreshConfig();
81 void ReadDataFromFifo(u32 read_ptr);
82 void ReadDataFromFifoOnCPU(u32 read_ptr);
83 int RunGpuOnCpu(int ticks);
84 int WaitForGpuThread(int ticks);
85 static void SyncGPUCallback(Core::System& system, u64 ticks, s64 cyclesLate);
86
87 static constexpr u32 FIFO_SIZE = 2 * 1024 * 1024;
88
89 Common::BlockingLoop m_gpu_mainloop;
90
91 Common::Flag m_emu_running_state;
92
93 // Most of this array is unlikely to be faulted in...
94 u8 m_fifo_aux_data[FIFO_SIZE]{};
95 u8* m_fifo_aux_write_ptr = nullptr;
96 u8* m_fifo_aux_read_ptr = nullptr;
97
98 // This could be in SConfig, but it depends on multiple settings
99 // and can change at runtime.
100 bool m_use_deterministic_gpu_thread = false;
101
102 CoreTiming::EventType* m_event_sync_gpu = nullptr;
103
104 // STATE_TO_SAVE
105 u8* m_video_buffer = nullptr;
106 u8* m_video_buffer_read_ptr = nullptr;
107 std::atomic<u8*> m_video_buffer_write_ptr = nullptr;
108 std::atomic<u8*> m_video_buffer_seen_ptr = nullptr;
109 u8* m_video_buffer_pp_read_ptr = nullptr;
110 // The read_ptr is always owned by the GPU thread. In normal mode, so is the
111 // write_ptr, despite it being atomic. In deterministic GPU thread mode,
112 // things get a bit more complicated:
113 // - The seen_ptr is written by the GPU thread, and points to what it's already
114 // processed as much of as possible - in the case of a partial command which
115 // caused it to stop, not the same as the read ptr. It's written by the GPU,
116 // under the lock, and updating the cond.
117 // - The write_ptr is written by the CPU thread after it copies data from the
118 // FIFO. Maybe someday it will be under the lock. For now, because RunGpuLoop
119 // polls, it's just atomic.
120 // - The pp_read_ptr is the CPU preprocessing version of the read_ptr.
121
122 std::atomic<int> m_sync_ticks = 0;
123 bool m_syncing_suspended = false;
124 Common::Event m_sync_wakeup_event;
125
126 std::optional<Config::ConfigChangedCallbackID> m_config_callback_id = std::nullopt;
127 bool m_config_sync_gpu = false;
128 int m_config_sync_gpu_max_distance = 0;
129 int m_config_sync_gpu_min_distance = 0;
130 float m_config_sync_gpu_overclock = 0.0f;
131
132 Core::System& m_system;
133 };
134
135 bool AtBreakpoint(Core::System& system);
136 } // namespace Fifo