gpu.h 9.6 KB

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