gpu.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // TODO: Implement the commented ones
  21. enum class RenderTargetFormat : u32 {
  22. NONE = 0x0,
  23. R32G32B32A32_FLOAT = 0xC0,
  24. R32G32B32A32_SINT = 0xC1,
  25. R32G32B32A32_UINT = 0xC2,
  26. R32G32B32X32_FLOAT = 0xC3,
  27. R32G32B32X32_SINT = 0xC4,
  28. R32G32B32X32_UINT = 0xC5,
  29. R16G16B16A16_UNORM = 0xC6,
  30. R16G16B16A16_SNORM = 0xC7,
  31. R16G16B16A16_SINT = 0xC8,
  32. R16G16B16A16_UINT = 0xC9,
  33. R16G16B16A16_FLOAT = 0xCA,
  34. R32G32_FLOAT = 0xCB,
  35. R32G32_SINT = 0xCC,
  36. R32G32_UINT = 0xCD,
  37. R16G16B16X16_FLOAT = 0xCE,
  38. A8R8G8B8_UNORM = 0xCF,
  39. A8R8G8B8_SRGB = 0xD0,
  40. A2B10G10R10_UNORM = 0xD1,
  41. A2B10G10R10_UINT = 0xD2,
  42. A8B8G8R8_UNORM = 0xD5,
  43. A8B8G8R8_SRGB = 0xD6,
  44. A8B8G8R8_SNORM = 0xD7,
  45. A8B8G8R8_SINT = 0xD8,
  46. A8B8G8R8_UINT = 0xD9,
  47. R16G16_UNORM = 0xDA,
  48. R16G16_SNORM = 0xDB,
  49. R16G16_SINT = 0xDC,
  50. R16G16_UINT = 0xDD,
  51. R16G16_FLOAT = 0xDE,
  52. A2R10G10B10_UNORM = 0xDF,
  53. B10G11R11_FLOAT = 0xE0,
  54. R32_SINT = 0xE3,
  55. R32_UINT = 0xE4,
  56. R32_FLOAT = 0xE5,
  57. X8R8G8B8_UNORM = 0xE6,
  58. X8R8G8B8_SRGB = 0xE7,
  59. R5G6B5_UNORM = 0xE8,
  60. A1R5G5B5_UNORM = 0xE9,
  61. R8G8_UNORM = 0xEA,
  62. R8G8_SNORM = 0xEB,
  63. R8G8_SINT = 0xEC,
  64. R8G8_UINT = 0xED,
  65. R16_UNORM = 0xEE,
  66. R16_SNORM = 0xEF,
  67. R16_SINT = 0xF0,
  68. R16_UINT = 0xF1,
  69. R16_FLOAT = 0xF2,
  70. R8_UNORM = 0xF3,
  71. R8_SNORM = 0xF4,
  72. R8_SINT = 0xF5,
  73. R8_UINT = 0xF6,
  74. // A8_UNORM = 0xF7,
  75. X1R5G5B5_UNORM = 0xF8,
  76. X8B8G8R8_UNORM = 0xF9,
  77. X8B8G8R8_SRGB = 0xFA,
  78. /*
  79. Z1R5G5B5_UNORM = 0xFB,
  80. O1R5G5B5_UNORM = 0xFC,
  81. Z8R8G8B8_UNORM = 0xFD,
  82. O8R8G8B8_UNORM = 0xFE,
  83. R32_UNORM = 0xFF,
  84. A16_UNORM = 0x40,
  85. A16_FLOAT = 0x41,
  86. A32_FLOAT = 0x42,
  87. A8R8_UNORM = 0x43,
  88. R16A16_UNORM = 0x44,
  89. R16A16_FLOAT = 0x45,
  90. R32A32_FLOAT = 0x46,
  91. B8G8R8A8_UNORM = 0x47,
  92. */
  93. };
  94. enum class DepthFormat : u32 {
  95. Z32_FLOAT = 0xA,
  96. Z16_UNORM = 0x13,
  97. Z24_UNORM_S8_UINT = 0x14,
  98. X8Z24_UNORM = 0x15,
  99. S8Z24_UNORM = 0x16,
  100. S8_UINT = 0x17,
  101. V8Z24_UNORM = 0x18,
  102. Z32_FLOAT_X24S8_UINT = 0x19,
  103. /*
  104. X8Z24_UNORM_X16V8S8_UINT = 0x1D,
  105. Z32_FLOAT_X16V8X8_UINT = 0x1E,
  106. Z32_FLOAT_X16V8S8_UINT = 0x1F,
  107. */
  108. };
  109. namespace Engines {
  110. class Maxwell3D;
  111. class KeplerCompute;
  112. } // namespace Engines
  113. namespace Control {
  114. struct ChannelState;
  115. }
  116. namespace Host1x {
  117. class Host1x;
  118. } // namespace Host1x
  119. class MemoryManager;
  120. class GPU final {
  121. public:
  122. explicit GPU(Core::System& system, bool is_async, bool use_nvdec);
  123. ~GPU();
  124. /// Binds a renderer to the GPU.
  125. void BindRenderer(std::unique_ptr<VideoCore::RendererBase> renderer);
  126. /// Flush all current written commands into the host GPU for execution.
  127. void FlushCommands();
  128. /// Synchronizes CPU writes with Host GPU memory.
  129. void InvalidateGPUCache();
  130. /// Signal the ending of command list.
  131. void OnCommandListEnd();
  132. std::shared_ptr<Control::ChannelState> AllocateChannel();
  133. void InitChannel(Control::ChannelState& to_init);
  134. void BindChannel(s32 channel_id);
  135. void ReleaseChannel(Control::ChannelState& to_release);
  136. void InitAddressSpace(Tegra::MemoryManager& memory_manager);
  137. /// Request a host GPU memory flush from the CPU.
  138. [[nodiscard]] u64 RequestFlush(VAddr addr, std::size_t size);
  139. /// Obtains current flush request fence id.
  140. [[nodiscard]] u64 CurrentSyncRequestFence() const;
  141. void WaitForSyncOperation(u64 fence);
  142. /// Tick pending requests within the GPU.
  143. void TickWork();
  144. /// Gets a mutable reference to the Host1x interface
  145. [[nodiscard]] Host1x::Host1x& Host1x();
  146. /// Gets an immutable reference to the Host1x interface.
  147. [[nodiscard]] const Host1x::Host1x& Host1x() const;
  148. /// Returns a reference to the Maxwell3D GPU engine.
  149. [[nodiscard]] Engines::Maxwell3D& Maxwell3D();
  150. /// Returns a const reference to the Maxwell3D GPU engine.
  151. [[nodiscard]] const Engines::Maxwell3D& Maxwell3D() const;
  152. /// Returns a reference to the KeplerCompute GPU engine.
  153. [[nodiscard]] Engines::KeplerCompute& KeplerCompute();
  154. /// Returns a reference to the KeplerCompute GPU engine.
  155. [[nodiscard]] const Engines::KeplerCompute& KeplerCompute() const;
  156. /// Returns a reference to the GPU DMA pusher.
  157. [[nodiscard]] Tegra::DmaPusher& DmaPusher();
  158. /// Returns a const reference to the GPU DMA pusher.
  159. [[nodiscard]] const Tegra::DmaPusher& DmaPusher() const;
  160. /// Returns a reference to the underlying renderer.
  161. [[nodiscard]] VideoCore::RendererBase& Renderer();
  162. /// Returns a const reference to the underlying renderer.
  163. [[nodiscard]] const VideoCore::RendererBase& Renderer() const;
  164. /// Returns a reference to the shader notifier.
  165. [[nodiscard]] VideoCore::ShaderNotify& ShaderNotify();
  166. /// Returns a const reference to the shader notifier.
  167. [[nodiscard]] const VideoCore::ShaderNotify& ShaderNotify() const;
  168. [[nodiscard]] u64 GetTicks() const;
  169. [[nodiscard]] bool IsAsync() const;
  170. [[nodiscard]] bool UseNvdec() const;
  171. void RendererFrameEndNotify();
  172. void RequestSwapBuffers(const Tegra::FramebufferConfig* framebuffer,
  173. std::array<Service::Nvidia::NvFence, 4>& fences, size_t num_fences);
  174. /// Performs any additional setup necessary in order to begin GPU emulation.
  175. /// This can be used to launch any necessary threads and register any necessary
  176. /// core timing events.
  177. void Start();
  178. /// Performs any additional necessary steps to shutdown GPU emulation.
  179. void NotifyShutdown();
  180. /// Obtain the CPU Context
  181. void ObtainContext();
  182. /// Release the CPU Context
  183. void ReleaseContext();
  184. /// Push GPU command entries to be processed
  185. void PushGPUEntries(s32 channel, Tegra::CommandList&& entries);
  186. /// Push GPU command buffer entries to be processed
  187. void PushCommandBuffer(u32 id, Tegra::ChCommandHeaderList& entries);
  188. /// Frees the CDMAPusher instance to free up resources
  189. void ClearCdmaInstance(u32 id);
  190. /// Swap buffers (render frame)
  191. void SwapBuffers(const Tegra::FramebufferConfig* framebuffer);
  192. /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory
  193. void FlushRegion(VAddr addr, u64 size);
  194. /// Notify rasterizer that any caches of the specified region should be invalidated
  195. void InvalidateRegion(VAddr addr, u64 size);
  196. /// Notify rasterizer that any caches of the specified region should be flushed and invalidated
  197. void FlushAndInvalidateRegion(VAddr addr, u64 size);
  198. private:
  199. struct Impl;
  200. mutable std::unique_ptr<Impl> impl;
  201. };
  202. } // namespace Tegra