gpu.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <atomic>
  7. #include <condition_variable>
  8. #include <list>
  9. #include <memory>
  10. #include <mutex>
  11. #include "common/common_types.h"
  12. #include "core/hle/service/nvdrv/nvdata.h"
  13. #include "core/hle/service/nvflinger/buffer_queue.h"
  14. #include "video_core/dma_pusher.h"
  15. using CacheAddr = std::uintptr_t;
  16. inline CacheAddr ToCacheAddr(const void* host_ptr) {
  17. return reinterpret_cast<CacheAddr>(host_ptr);
  18. }
  19. inline u8* FromCacheAddr(CacheAddr cache_addr) {
  20. return reinterpret_cast<u8*>(cache_addr);
  21. }
  22. namespace Core {
  23. namespace Frontend {
  24. class EmuWindow;
  25. }
  26. class System;
  27. } // namespace Core
  28. namespace VideoCore {
  29. class RendererBase;
  30. } // namespace VideoCore
  31. namespace Tegra {
  32. enum class RenderTargetFormat : u32 {
  33. NONE = 0x0,
  34. RGBA32_FLOAT = 0xC0,
  35. RGBA32_UINT = 0xC2,
  36. RGBA16_UNORM = 0xC6,
  37. RGBA16_SNORM = 0xC7,
  38. RGBA16_UINT = 0xC9,
  39. RGBA16_FLOAT = 0xCA,
  40. RG32_FLOAT = 0xCB,
  41. RG32_UINT = 0xCD,
  42. RGBX16_FLOAT = 0xCE,
  43. BGRA8_UNORM = 0xCF,
  44. BGRA8_SRGB = 0xD0,
  45. RGB10_A2_UNORM = 0xD1,
  46. RGBA8_UNORM = 0xD5,
  47. RGBA8_SRGB = 0xD6,
  48. RGBA8_SNORM = 0xD7,
  49. RGBA8_UINT = 0xD9,
  50. RG16_UNORM = 0xDA,
  51. RG16_SNORM = 0xDB,
  52. RG16_SINT = 0xDC,
  53. RG16_UINT = 0xDD,
  54. RG16_FLOAT = 0xDE,
  55. R11G11B10_FLOAT = 0xE0,
  56. R32_SINT = 0xE3,
  57. R32_UINT = 0xE4,
  58. R32_FLOAT = 0xE5,
  59. B5G6R5_UNORM = 0xE8,
  60. BGR5A1_UNORM = 0xE9,
  61. RG8_UNORM = 0xEA,
  62. RG8_SNORM = 0xEB,
  63. R16_UNORM = 0xEE,
  64. R16_SNORM = 0xEF,
  65. R16_SINT = 0xF0,
  66. R16_UINT = 0xF1,
  67. R16_FLOAT = 0xF2,
  68. R8_UNORM = 0xF3,
  69. R8_UINT = 0xF6,
  70. };
  71. enum class DepthFormat : u32 {
  72. Z32_FLOAT = 0xA,
  73. Z16_UNORM = 0x13,
  74. S8_Z24_UNORM = 0x14,
  75. Z24_X8_UNORM = 0x15,
  76. Z24_S8_UNORM = 0x16,
  77. Z24_C8_UNORM = 0x18,
  78. Z32_S8_X24_FLOAT = 0x19,
  79. };
  80. struct CommandListHeader;
  81. class DebugContext;
  82. /**
  83. * Struct describing framebuffer configuration
  84. */
  85. struct FramebufferConfig {
  86. enum class PixelFormat : u32 {
  87. ABGR8 = 1,
  88. RGB565 = 4,
  89. BGRA8 = 5,
  90. };
  91. VAddr address;
  92. u32 offset;
  93. u32 width;
  94. u32 height;
  95. u32 stride;
  96. PixelFormat pixel_format;
  97. using TransformFlags = Service::NVFlinger::BufferQueue::BufferTransformFlags;
  98. TransformFlags transform_flags;
  99. Common::Rectangle<int> crop_rect;
  100. };
  101. namespace Engines {
  102. class Fermi2D;
  103. class Maxwell3D;
  104. class MaxwellDMA;
  105. class KeplerCompute;
  106. class KeplerMemory;
  107. } // namespace Engines
  108. enum class EngineID {
  109. FERMI_TWOD_A = 0x902D, // 2D Engine
  110. MAXWELL_B = 0xB197, // 3D Engine
  111. KEPLER_COMPUTE_B = 0xB1C0,
  112. KEPLER_INLINE_TO_MEMORY_B = 0xA140,
  113. MAXWELL_DMA_COPY_A = 0xB0B5,
  114. };
  115. class MemoryManager;
  116. class GPU {
  117. public:
  118. explicit GPU(Core::System& system, std::unique_ptr<VideoCore::RendererBase>&& renderer,
  119. bool is_async);
  120. virtual ~GPU();
  121. struct MethodCall {
  122. u32 method{};
  123. u32 argument{};
  124. u32 subchannel{};
  125. u32 method_count{};
  126. bool IsLastCall() const {
  127. return method_count <= 1;
  128. }
  129. MethodCall(u32 method, u32 argument, u32 subchannel = 0, u32 method_count = 0)
  130. : method(method), argument(argument), subchannel(subchannel),
  131. method_count(method_count) {}
  132. };
  133. /// Calls a GPU method.
  134. void CallMethod(const MethodCall& method_call);
  135. void FlushCommands();
  136. /// Returns a reference to the Maxwell3D GPU engine.
  137. Engines::Maxwell3D& Maxwell3D();
  138. /// Returns a const reference to the Maxwell3D GPU engine.
  139. const Engines::Maxwell3D& Maxwell3D() const;
  140. /// Returns a reference to the KeplerCompute GPU engine.
  141. Engines::KeplerCompute& KeplerCompute();
  142. /// Returns a reference to the KeplerCompute GPU engine.
  143. const Engines::KeplerCompute& KeplerCompute() const;
  144. /// Returns a reference to the GPU memory manager.
  145. Tegra::MemoryManager& MemoryManager();
  146. /// Returns a const reference to the GPU memory manager.
  147. const Tegra::MemoryManager& MemoryManager() const;
  148. /// Returns a reference to the GPU DMA pusher.
  149. Tegra::DmaPusher& DmaPusher();
  150. VideoCore::RendererBase& Renderer() {
  151. return *renderer;
  152. }
  153. const VideoCore::RendererBase& Renderer() const {
  154. return *renderer;
  155. }
  156. // Waits for the GPU to finish working
  157. virtual void WaitIdle() const = 0;
  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. u32 GetSyncpointValue(u32 syncpoint_id) const;
  162. void RegisterSyncptInterrupt(u32 syncpoint_id, u32 value);
  163. bool CancelSyncptInterrupt(u32 syncpoint_id, u32 value);
  164. u64 GetTicks() const;
  165. std::unique_lock<std::mutex> LockSync() {
  166. return std::unique_lock{sync_mutex};
  167. }
  168. bool IsAsync() const {
  169. return is_async;
  170. }
  171. /// Returns a const reference to the GPU DMA pusher.
  172. const Tegra::DmaPusher& DmaPusher() const;
  173. struct Regs {
  174. static constexpr size_t NUM_REGS = 0x100;
  175. union {
  176. struct {
  177. INSERT_UNION_PADDING_WORDS(0x4);
  178. struct {
  179. u32 address_high;
  180. u32 address_low;
  181. GPUVAddr SemaphoreAddress() const {
  182. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  183. address_low);
  184. }
  185. } semaphore_address;
  186. u32 semaphore_sequence;
  187. u32 semaphore_trigger;
  188. INSERT_UNION_PADDING_WORDS(0xC);
  189. // The puser and the puller share the reference counter, the pusher only has read
  190. // access
  191. u32 reference_count;
  192. INSERT_UNION_PADDING_WORDS(0x5);
  193. u32 semaphore_acquire;
  194. u32 semaphore_release;
  195. u32 fence_value;
  196. union {
  197. BitField<4, 4, u32> operation;
  198. BitField<8, 8, u32> id;
  199. } fence_action;
  200. INSERT_UNION_PADDING_WORDS(0xE2);
  201. // Puller state
  202. u32 acquire_mode;
  203. u32 acquire_source;
  204. u32 acquire_active;
  205. u32 acquire_timeout;
  206. u32 acquire_value;
  207. };
  208. std::array<u32, NUM_REGS> reg_array;
  209. };
  210. } regs{};
  211. /// Performs any additional setup necessary in order to begin GPU emulation.
  212. /// This can be used to launch any necessary threads and register any necessary
  213. /// core timing events.
  214. virtual void Start() = 0;
  215. /// Push GPU command entries to be processed
  216. virtual void PushGPUEntries(Tegra::CommandList&& entries) = 0;
  217. /// Swap buffers (render frame)
  218. virtual void SwapBuffers(const Tegra::FramebufferConfig* framebuffer) = 0;
  219. /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory
  220. virtual void FlushRegion(VAddr addr, u64 size) = 0;
  221. /// Notify rasterizer that any caches of the specified region should be invalidated
  222. virtual void InvalidateRegion(VAddr addr, u64 size) = 0;
  223. /// Notify rasterizer that any caches of the specified region should be flushed and invalidated
  224. virtual void FlushAndInvalidateRegion(VAddr addr, u64 size) = 0;
  225. protected:
  226. virtual void TriggerCpuInterrupt(u32 syncpoint_id, u32 value) const = 0;
  227. private:
  228. void ProcessBindMethod(const MethodCall& method_call);
  229. void ProcessSemaphoreTriggerMethod();
  230. void ProcessSemaphoreRelease();
  231. void ProcessSemaphoreAcquire();
  232. /// Calls a GPU puller method.
  233. void CallPullerMethod(const MethodCall& method_call);
  234. /// Calls a GPU engine method.
  235. void CallEngineMethod(const MethodCall& method_call);
  236. /// Determines where the method should be executed.
  237. bool ExecuteMethodOnEngine(const MethodCall& method_call);
  238. protected:
  239. std::unique_ptr<Tegra::DmaPusher> dma_pusher;
  240. Core::System& system;
  241. std::unique_ptr<VideoCore::RendererBase> renderer;
  242. private:
  243. std::unique_ptr<Tegra::MemoryManager> memory_manager;
  244. /// Mapping of command subchannels to their bound engine ids
  245. std::array<EngineID, 8> bound_engines = {};
  246. /// 3D engine
  247. std::unique_ptr<Engines::Maxwell3D> maxwell_3d;
  248. /// 2D engine
  249. std::unique_ptr<Engines::Fermi2D> fermi_2d;
  250. /// Compute engine
  251. std::unique_ptr<Engines::KeplerCompute> kepler_compute;
  252. /// DMA engine
  253. std::unique_ptr<Engines::MaxwellDMA> maxwell_dma;
  254. /// Inline memory engine
  255. std::unique_ptr<Engines::KeplerMemory> kepler_memory;
  256. std::array<std::atomic<u32>, Service::Nvidia::MaxSyncPoints> syncpoints{};
  257. std::array<std::list<u32>, Service::Nvidia::MaxSyncPoints> syncpt_interrupts;
  258. std::mutex sync_mutex;
  259. std::condition_variable sync_cv;
  260. const bool is_async;
  261. };
  262. #define ASSERT_REG_POSITION(field_name, position) \
  263. static_assert(offsetof(GPU::Regs, field_name) == position * 4, \
  264. "Field " #field_name " has invalid position")
  265. ASSERT_REG_POSITION(semaphore_address, 0x4);
  266. ASSERT_REG_POSITION(semaphore_sequence, 0x6);
  267. ASSERT_REG_POSITION(semaphore_trigger, 0x7);
  268. ASSERT_REG_POSITION(reference_count, 0x14);
  269. ASSERT_REG_POSITION(semaphore_acquire, 0x1A);
  270. ASSERT_REG_POSITION(semaphore_release, 0x1B);
  271. ASSERT_REG_POSITION(fence_value, 0x1C);
  272. ASSERT_REG_POSITION(fence_action, 0x1D);
  273. ASSERT_REG_POSITION(acquire_mode, 0x100);
  274. ASSERT_REG_POSITION(acquire_source, 0x101);
  275. ASSERT_REG_POSITION(acquire_active, 0x102);
  276. ASSERT_REG_POSITION(acquire_timeout, 0x103);
  277. ASSERT_REG_POSITION(acquire_value, 0x104);
  278. #undef ASSERT_REG_POSITION
  279. } // namespace Tegra