gpu.h 9.7 KB

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