gpu.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <memory>
  5. #include "common/bit_field.h"
  6. #include "common/common_types.h"
  7. #include "core/hle/service/nvdrv/nvdata.h"
  8. #include "video_core/cdma_pusher.h"
  9. #include "video_core/framebuffer_config.h"
  10. namespace Core {
  11. class System;
  12. } // namespace Core
  13. namespace VideoCore {
  14. class RendererBase;
  15. class ShaderNotify;
  16. } // namespace VideoCore
  17. namespace Tegra {
  18. class DmaPusher;
  19. struct CommandList;
  20. enum class RenderTargetFormat : u32 {
  21. NONE = 0x0,
  22. R32B32G32A32_FLOAT = 0xC0,
  23. R32G32B32A32_SINT = 0xC1,
  24. R32G32B32A32_UINT = 0xC2,
  25. R16G16B16A16_UNORM = 0xC6,
  26. R16G16B16A16_SNORM = 0xC7,
  27. R16G16B16A16_SINT = 0xC8,
  28. R16G16B16A16_UINT = 0xC9,
  29. R16G16B16A16_FLOAT = 0xCA,
  30. R32G32_FLOAT = 0xCB,
  31. R32G32_SINT = 0xCC,
  32. R32G32_UINT = 0xCD,
  33. R16G16B16X16_FLOAT = 0xCE,
  34. B8G8R8A8_UNORM = 0xCF,
  35. B8G8R8A8_SRGB = 0xD0,
  36. A2B10G10R10_UNORM = 0xD1,
  37. A2B10G10R10_UINT = 0xD2,
  38. A8B8G8R8_UNORM = 0xD5,
  39. A8B8G8R8_SRGB = 0xD6,
  40. A8B8G8R8_SNORM = 0xD7,
  41. A8B8G8R8_SINT = 0xD8,
  42. A8B8G8R8_UINT = 0xD9,
  43. R16G16_UNORM = 0xDA,
  44. R16G16_SNORM = 0xDB,
  45. R16G16_SINT = 0xDC,
  46. R16G16_UINT = 0xDD,
  47. R16G16_FLOAT = 0xDE,
  48. B10G11R11_FLOAT = 0xE0,
  49. R32_SINT = 0xE3,
  50. R32_UINT = 0xE4,
  51. R32_FLOAT = 0xE5,
  52. R5G6B5_UNORM = 0xE8,
  53. A1R5G5B5_UNORM = 0xE9,
  54. R8G8_UNORM = 0xEA,
  55. R8G8_SNORM = 0xEB,
  56. R8G8_SINT = 0xEC,
  57. R8G8_UINT = 0xED,
  58. R16_UNORM = 0xEE,
  59. R16_SNORM = 0xEF,
  60. R16_SINT = 0xF0,
  61. R16_UINT = 0xF1,
  62. R16_FLOAT = 0xF2,
  63. R8_UNORM = 0xF3,
  64. R8_SNORM = 0xF4,
  65. R8_SINT = 0xF5,
  66. R8_UINT = 0xF6,
  67. };
  68. enum class DepthFormat : u32 {
  69. D32_FLOAT = 0xA,
  70. D16_UNORM = 0x13,
  71. S8_UINT_Z24_UNORM = 0x14,
  72. D24X8_UNORM = 0x15,
  73. D24S8_UNORM = 0x16,
  74. S8_UINT = 0x17,
  75. D24C8_UNORM = 0x18,
  76. D32_FLOAT_S8X24_UINT = 0x19,
  77. };
  78. namespace Engines {
  79. class Maxwell3D;
  80. class KeplerCompute;
  81. } // namespace Engines
  82. enum class EngineID {
  83. FERMI_TWOD_A = 0x902D, // 2D Engine
  84. MAXWELL_B = 0xB197, // 3D Engine
  85. KEPLER_COMPUTE_B = 0xB1C0,
  86. KEPLER_INLINE_TO_MEMORY_B = 0xA140,
  87. MAXWELL_DMA_COPY_A = 0xB0B5,
  88. };
  89. class MemoryManager;
  90. class GPU final {
  91. public:
  92. struct MethodCall {
  93. u32 method{};
  94. u32 argument{};
  95. u32 subchannel{};
  96. u32 method_count{};
  97. explicit MethodCall(u32 method_, u32 argument_, u32 subchannel_ = 0, u32 method_count_ = 0)
  98. : method(method_), argument(argument_), subchannel(subchannel_),
  99. method_count(method_count_) {}
  100. [[nodiscard]] bool IsLastCall() const {
  101. return method_count <= 1;
  102. }
  103. };
  104. enum class FenceOperation : u32 {
  105. Acquire = 0,
  106. Increment = 1,
  107. };
  108. union FenceAction {
  109. u32 raw;
  110. BitField<0, 1, FenceOperation> op;
  111. BitField<8, 24, u32> syncpoint_id;
  112. };
  113. explicit GPU(Core::System& system, bool is_async, bool use_nvdec);
  114. ~GPU();
  115. /// Binds a renderer to the GPU.
  116. void BindRenderer(std::unique_ptr<VideoCore::RendererBase> renderer);
  117. /// Calls a GPU method.
  118. void CallMethod(const MethodCall& method_call);
  119. /// Calls a GPU multivalue method.
  120. void CallMultiMethod(u32 method, u32 subchannel, const u32* base_start, u32 amount,
  121. u32 methods_pending);
  122. /// Flush all current written commands into the host GPU for execution.
  123. void FlushCommands();
  124. /// Synchronizes CPU writes with Host GPU memory.
  125. void SyncGuestHost();
  126. /// Signal the ending of command list.
  127. void OnCommandListEnd();
  128. /// Request a host GPU memory flush from the CPU.
  129. [[nodiscard]] u64 RequestFlush(VAddr addr, std::size_t size);
  130. /// Obtains current flush request fence id.
  131. [[nodiscard]] u64 CurrentFlushRequestFence() const;
  132. /// Tick pending requests within the GPU.
  133. void TickWork();
  134. /// Returns a reference to the Maxwell3D GPU engine.
  135. [[nodiscard]] Engines::Maxwell3D& Maxwell3D();
  136. /// Returns a const reference to the Maxwell3D GPU engine.
  137. [[nodiscard]] const Engines::Maxwell3D& Maxwell3D() const;
  138. /// Returns a reference to the KeplerCompute GPU engine.
  139. [[nodiscard]] Engines::KeplerCompute& KeplerCompute();
  140. /// Returns a reference to the KeplerCompute GPU engine.
  141. [[nodiscard]] const Engines::KeplerCompute& KeplerCompute() const;
  142. /// Returns a reference to the GPU memory manager.
  143. [[nodiscard]] Tegra::MemoryManager& MemoryManager();
  144. /// Returns a const reference to the GPU memory manager.
  145. [[nodiscard]] const Tegra::MemoryManager& MemoryManager() const;
  146. /// Returns a reference to the GPU DMA pusher.
  147. [[nodiscard]] Tegra::DmaPusher& DmaPusher();
  148. /// Returns a const reference to the GPU DMA pusher.
  149. [[nodiscard]] const Tegra::DmaPusher& DmaPusher() const;
  150. /// Returns a reference to the underlying renderer.
  151. [[nodiscard]] VideoCore::RendererBase& Renderer();
  152. /// Returns a const reference to the underlying renderer.
  153. [[nodiscard]] const VideoCore::RendererBase& Renderer() const;
  154. /// Returns a reference to the shader notifier.
  155. [[nodiscard]] VideoCore::ShaderNotify& ShaderNotify();
  156. /// Returns a const reference to the shader notifier.
  157. [[nodiscard]] const VideoCore::ShaderNotify& ShaderNotify() const;
  158. /// Allows the CPU/NvFlinger to wait on the GPU before presenting a frame.
  159. void WaitFence(u32 syncpoint_id, u32 value);
  160. void IncrementSyncPoint(u32 syncpoint_id);
  161. [[nodiscard]] u32 GetSyncpointValue(u32 syncpoint_id) const;
  162. void RegisterSyncptInterrupt(u32 syncpoint_id, u32 value);
  163. [[nodiscard]] bool CancelSyncptInterrupt(u32 syncpoint_id, u32 value);
  164. [[nodiscard]] u64 GetTicks() const;
  165. [[nodiscard]] bool IsAsync() const;
  166. [[nodiscard]] bool UseNvdec() const;
  167. void RendererFrameEndNotify();
  168. /// Performs any additional setup necessary in order to begin GPU emulation.
  169. /// This can be used to launch any necessary threads and register any necessary
  170. /// core timing events.
  171. void Start();
  172. /// Performs any additional necessary steps to shutdown GPU emulation.
  173. void NotifyShutdown();
  174. /// Obtain the CPU Context
  175. void ObtainContext();
  176. /// Release the CPU Context
  177. void ReleaseContext();
  178. /// Push GPU command entries to be processed
  179. void PushGPUEntries(Tegra::CommandList&& entries);
  180. /// Push GPU command buffer entries to be processed
  181. void PushCommandBuffer(u32 id, Tegra::ChCommandHeaderList& entries);
  182. /// Frees the CDMAPusher instance to free up resources
  183. void ClearCdmaInstance(u32 id);
  184. /// Swap buffers (render frame)
  185. void SwapBuffers(const Tegra::FramebufferConfig* framebuffer);
  186. /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory
  187. void FlushRegion(VAddr addr, u64 size);
  188. /// Notify rasterizer that any caches of the specified region should be invalidated
  189. void InvalidateRegion(VAddr addr, u64 size);
  190. /// Notify rasterizer that any caches of the specified region should be flushed and invalidated
  191. void FlushAndInvalidateRegion(VAddr addr, u64 size);
  192. private:
  193. struct Impl;
  194. std::unique_ptr<Impl> impl;
  195. };
  196. } // namespace Tegra