PixelEngine.cpp 7.6 KB

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