gpu.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. void IncrementSyncPoint(u32 syncpoint_id);
  148. u32 GetSyncpointValue(u32 syncpoint_id) const;
  149. void RegisterSyncptInterrupt(u32 syncpoint_id, u32 value);
  150. bool CancelSyncptInterrupt(u32 syncpoint_id, u32 value);
  151. std::unique_lock<std::mutex> LockSync() {
  152. return std::unique_lock{sync_mutex};
  153. }
  154. bool IsAsync() const {
  155. return is_async;
  156. }
  157. /// Returns a const reference to the GPU DMA pusher.
  158. const Tegra::DmaPusher& DmaPusher() const;
  159. struct Regs {
  160. static constexpr size_t NUM_REGS = 0x100;
  161. union {
  162. struct {
  163. INSERT_PADDING_WORDS(0x4);
  164. struct {
  165. u32 address_high;
  166. u32 address_low;
  167. GPUVAddr SemaphoreAddress() const {
  168. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  169. address_low);
  170. }
  171. } semaphore_address;
  172. u32 semaphore_sequence;
  173. u32 semaphore_trigger;
  174. INSERT_PADDING_WORDS(0xC);
  175. // The puser and the puller share the reference counter, the pusher only has read
  176. // access
  177. u32 reference_count;
  178. INSERT_PADDING_WORDS(0x5);
  179. u32 semaphore_acquire;
  180. u32 semaphore_release;
  181. u32 fence_value;
  182. union {
  183. BitField<4, 4, u32> operation;
  184. BitField<8, 8, u32> id;
  185. } fence_action;
  186. INSERT_PADDING_WORDS(0xE2);
  187. // Puller state
  188. u32 acquire_mode;
  189. u32 acquire_source;
  190. u32 acquire_active;
  191. u32 acquire_timeout;
  192. u32 acquire_value;
  193. };
  194. std::array<u32, NUM_REGS> reg_array;
  195. };
  196. } regs{};
  197. /// Performs any additional setup necessary in order to begin GPU emulation.
  198. /// This can be used to launch any necessary threads and register any necessary
  199. /// core timing events.
  200. virtual void Start() = 0;
  201. /// Push GPU command entries to be processed
  202. virtual void PushGPUEntries(Tegra::CommandList&& entries) = 0;
  203. /// Swap buffers (render frame)
  204. virtual void SwapBuffers(const Tegra::FramebufferConfig* framebuffer) = 0;
  205. /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory
  206. virtual void FlushRegion(CacheAddr addr, u64 size) = 0;
  207. /// Notify rasterizer that any caches of the specified region should be invalidated
  208. virtual void InvalidateRegion(CacheAddr addr, u64 size) = 0;
  209. /// Notify rasterizer that any caches of the specified region should be flushed and invalidated
  210. virtual void FlushAndInvalidateRegion(CacheAddr addr, u64 size) = 0;
  211. protected:
  212. virtual void TriggerCpuInterrupt(u32 syncpoint_id, u32 value) const = 0;
  213. private:
  214. void ProcessBindMethod(const MethodCall& method_call);
  215. void ProcessSemaphoreTriggerMethod();
  216. void ProcessSemaphoreRelease();
  217. void ProcessSemaphoreAcquire();
  218. /// Calls a GPU puller method.
  219. void CallPullerMethod(const MethodCall& method_call);
  220. /// Calls a GPU engine method.
  221. void CallEngineMethod(const MethodCall& method_call);
  222. /// Determines where the method should be executed.
  223. bool ExecuteMethodOnEngine(const MethodCall& method_call);
  224. protected:
  225. std::unique_ptr<Tegra::DmaPusher> dma_pusher;
  226. Core::System& system;
  227. VideoCore::RendererBase& renderer;
  228. private:
  229. std::unique_ptr<Tegra::MemoryManager> memory_manager;
  230. /// Mapping of command subchannels to their bound engine ids
  231. std::array<EngineID, 8> bound_engines = {};
  232. /// 3D engine
  233. std::unique_ptr<Engines::Maxwell3D> maxwell_3d;
  234. /// 2D engine
  235. std::unique_ptr<Engines::Fermi2D> fermi_2d;
  236. /// Compute engine
  237. std::unique_ptr<Engines::KeplerCompute> kepler_compute;
  238. /// DMA engine
  239. std::unique_ptr<Engines::MaxwellDMA> maxwell_dma;
  240. /// Inline memory engine
  241. std::unique_ptr<Engines::KeplerMemory> kepler_memory;
  242. std::array<std::atomic<u32>, Service::Nvidia::MaxSyncPoints> syncpoints{};
  243. std::array<std::list<u32>, Service::Nvidia::MaxSyncPoints> syncpt_interrupts;
  244. std::mutex sync_mutex;
  245. const bool is_async;
  246. };
  247. #define ASSERT_REG_POSITION(field_name, position) \
  248. static_assert(offsetof(GPU::Regs, field_name) == position * 4, \
  249. "Field " #field_name " has invalid position")
  250. ASSERT_REG_POSITION(semaphore_address, 0x4);
  251. ASSERT_REG_POSITION(semaphore_sequence, 0x6);
  252. ASSERT_REG_POSITION(semaphore_trigger, 0x7);
  253. ASSERT_REG_POSITION(reference_count, 0x14);
  254. ASSERT_REG_POSITION(semaphore_acquire, 0x1A);
  255. ASSERT_REG_POSITION(semaphore_release, 0x1B);
  256. ASSERT_REG_POSITION(fence_value, 0x1C);
  257. ASSERT_REG_POSITION(fence_action, 0x1D);
  258. ASSERT_REG_POSITION(acquire_mode, 0x100);
  259. ASSERT_REG_POSITION(acquire_source, 0x101);
  260. ASSERT_REG_POSITION(acquire_active, 0x102);
  261. ASSERT_REG_POSITION(acquire_timeout, 0x103);
  262. ASSERT_REG_POSITION(acquire_value, 0x104);
  263. #undef ASSERT_REG_POSITION
  264. } // namespace Tegra