gpu.h 7.5 KB

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