gpu.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. BGRA8_UNORM = 0xCF,
  38. BGRA8_SRGB = 0xD0,
  39. RGB10_A2_UNORM = 0xD1,
  40. RGBA8_UNORM = 0xD5,
  41. RGBA8_SRGB = 0xD6,
  42. RGBA8_SNORM = 0xD7,
  43. RGBA8_UINT = 0xD9,
  44. RG16_UNORM = 0xDA,
  45. RG16_SNORM = 0xDB,
  46. RG16_SINT = 0xDC,
  47. RG16_UINT = 0xDD,
  48. RG16_FLOAT = 0xDE,
  49. R11G11B10_FLOAT = 0xE0,
  50. R32_UINT = 0xE4,
  51. R32_FLOAT = 0xE5,
  52. B5G6R5_UNORM = 0xE8,
  53. BGR5A1_UNORM = 0xE9,
  54. RG8_UNORM = 0xEA,
  55. RG8_SNORM = 0xEB,
  56. R16_UNORM = 0xEE,
  57. R16_SNORM = 0xEF,
  58. R16_SINT = 0xF0,
  59. R16_UINT = 0xF1,
  60. R16_FLOAT = 0xF2,
  61. R8_UNORM = 0xF3,
  62. R8_UINT = 0xF6,
  63. };
  64. enum class DepthFormat : u32 {
  65. Z32_FLOAT = 0xA,
  66. Z16_UNORM = 0x13,
  67. S8_Z24_UNORM = 0x14,
  68. Z24_X8_UNORM = 0x15,
  69. Z24_S8_UNORM = 0x16,
  70. Z24_C8_UNORM = 0x18,
  71. Z32_S8_X24_FLOAT = 0x19,
  72. };
  73. /// Returns the number of bytes per pixel of each rendertarget format.
  74. u32 RenderTargetBytesPerPixel(RenderTargetFormat format);
  75. /// Returns the number of bytes per pixel of each depth format.
  76. u32 DepthFormatBytesPerPixel(DepthFormat format);
  77. struct CommandListHeader;
  78. class DebugContext;
  79. /**
  80. * Struct describing framebuffer configuration
  81. */
  82. struct FramebufferConfig {
  83. enum class PixelFormat : u32 {
  84. ABGR8 = 1,
  85. BGRA8 = 5,
  86. };
  87. /**
  88. * Returns the number of bytes per pixel.
  89. */
  90. static u32 BytesPerPixel(PixelFormat format);
  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, VideoCore::RendererBase& renderer, bool is_async);
  119. virtual ~GPU();
  120. struct MethodCall {
  121. u32 method{};
  122. u32 argument{};
  123. u32 subchannel{};
  124. u32 method_count{};
  125. bool IsLastCall() const {
  126. return method_count <= 1;
  127. }
  128. MethodCall(u32 method, u32 argument, u32 subchannel = 0, u32 method_count = 0)
  129. : method(method), argument(argument), subchannel(subchannel),
  130. method_count(method_count) {}
  131. };
  132. /// Calls a GPU method.
  133. void CallMethod(const MethodCall& method_call);
  134. void FlushCommands();
  135. /// Returns a reference to the Maxwell3D GPU engine.
  136. Engines::Maxwell3D& Maxwell3D();
  137. /// Returns a const reference to the Maxwell3D GPU engine.
  138. const Engines::Maxwell3D& Maxwell3D() const;
  139. /// Returns a reference to the KeplerCompute GPU engine.
  140. Engines::KeplerCompute& KeplerCompute();
  141. /// Returns a reference to the KeplerCompute GPU engine.
  142. const Engines::KeplerCompute& KeplerCompute() const;
  143. /// Returns a reference to the GPU memory manager.
  144. Tegra::MemoryManager& MemoryManager();
  145. /// Returns a const reference to the GPU memory manager.
  146. const Tegra::MemoryManager& MemoryManager() const;
  147. /// Returns a reference to the GPU DMA pusher.
  148. Tegra::DmaPusher& DmaPusher();
  149. void IncrementSyncPoint(u32 syncpoint_id);
  150. u32 GetSyncpointValue(u32 syncpoint_id) const;
  151. void RegisterSyncptInterrupt(u32 syncpoint_id, u32 value);
  152. bool CancelSyncptInterrupt(u32 syncpoint_id, u32 value);
  153. std::unique_lock<std::mutex> LockSync() {
  154. return std::unique_lock{sync_mutex};
  155. }
  156. bool IsAsync() const {
  157. return is_async;
  158. }
  159. /// Returns a const reference to the GPU DMA pusher.
  160. const Tegra::DmaPusher& DmaPusher() const;
  161. struct Regs {
  162. static constexpr size_t NUM_REGS = 0x100;
  163. union {
  164. struct {
  165. INSERT_PADDING_WORDS(0x4);
  166. struct {
  167. u32 address_high;
  168. u32 address_low;
  169. GPUVAddr SemaphoreAddress() const {
  170. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  171. address_low);
  172. }
  173. } semaphore_address;
  174. u32 semaphore_sequence;
  175. u32 semaphore_trigger;
  176. INSERT_PADDING_WORDS(0xC);
  177. // The puser and the puller share the reference counter, the pusher only has read
  178. // access
  179. u32 reference_count;
  180. INSERT_PADDING_WORDS(0x5);
  181. u32 semaphore_acquire;
  182. u32 semaphore_release;
  183. u32 fence_value;
  184. union {
  185. BitField<4, 4, u32> operation;
  186. BitField<8, 8, u32> id;
  187. } fence_action;
  188. INSERT_PADDING_WORDS(0xE2);
  189. // Puller state
  190. u32 acquire_mode;
  191. u32 acquire_source;
  192. u32 acquire_active;
  193. u32 acquire_timeout;
  194. u32 acquire_value;
  195. };
  196. std::array<u32, NUM_REGS> reg_array;
  197. };
  198. } regs{};
  199. /// Performs any additional setup necessary in order to begin GPU emulation.
  200. /// This can be used to launch any necessary threads and register any necessary
  201. /// core timing events.
  202. virtual void Start() = 0;
  203. /// Push GPU command entries to be processed
  204. virtual void PushGPUEntries(Tegra::CommandList&& entries) = 0;
  205. /// Swap buffers (render frame)
  206. virtual void SwapBuffers(
  207. std::optional<std::reference_wrapper<const Tegra::FramebufferConfig>> framebuffer) = 0;
  208. /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory
  209. virtual void FlushRegion(CacheAddr addr, u64 size) = 0;
  210. /// Notify rasterizer that any caches of the specified region should be invalidated
  211. virtual void InvalidateRegion(CacheAddr addr, u64 size) = 0;
  212. /// Notify rasterizer that any caches of the specified region should be flushed and invalidated
  213. virtual void FlushAndInvalidateRegion(CacheAddr addr, u64 size) = 0;
  214. protected:
  215. virtual void TriggerCpuInterrupt(u32 syncpoint_id, u32 value) const = 0;
  216. private:
  217. void ProcessBindMethod(const MethodCall& method_call);
  218. void ProcessSemaphoreTriggerMethod();
  219. void ProcessSemaphoreRelease();
  220. void ProcessSemaphoreAcquire();
  221. /// Calls a GPU puller method.
  222. void CallPullerMethod(const MethodCall& method_call);
  223. /// Calls a GPU engine method.
  224. void CallEngineMethod(const MethodCall& method_call);
  225. /// Determines where the method should be executed.
  226. bool ExecuteMethodOnEngine(const MethodCall& method_call);
  227. protected:
  228. std::unique_ptr<Tegra::DmaPusher> dma_pusher;
  229. VideoCore::RendererBase& renderer;
  230. Core::System& system;
  231. private:
  232. std::unique_ptr<Tegra::MemoryManager> memory_manager;
  233. /// Mapping of command subchannels to their bound engine ids
  234. std::array<EngineID, 8> bound_engines = {};
  235. /// 3D engine
  236. std::unique_ptr<Engines::Maxwell3D> maxwell_3d;
  237. /// 2D engine
  238. std::unique_ptr<Engines::Fermi2D> fermi_2d;
  239. /// Compute engine
  240. std::unique_ptr<Engines::KeplerCompute> kepler_compute;
  241. /// DMA engine
  242. std::unique_ptr<Engines::MaxwellDMA> maxwell_dma;
  243. /// Inline memory engine
  244. std::unique_ptr<Engines::KeplerMemory> kepler_memory;
  245. std::array<std::atomic<u32>, Service::Nvidia::MaxSyncPoints> syncpoints{};
  246. std::array<std::list<u32>, Service::Nvidia::MaxSyncPoints> syncpt_interrupts;
  247. std::mutex sync_mutex;
  248. const bool is_async;
  249. };
  250. #define ASSERT_REG_POSITION(field_name, position) \
  251. static_assert(offsetof(GPU::Regs, field_name) == position * 4, \
  252. "Field " #field_name " has invalid position")
  253. ASSERT_REG_POSITION(semaphore_address, 0x4);
  254. ASSERT_REG_POSITION(semaphore_sequence, 0x6);
  255. ASSERT_REG_POSITION(semaphore_trigger, 0x7);
  256. ASSERT_REG_POSITION(reference_count, 0x14);
  257. ASSERT_REG_POSITION(semaphore_acquire, 0x1A);
  258. ASSERT_REG_POSITION(semaphore_release, 0x1B);
  259. ASSERT_REG_POSITION(fence_value, 0x1C);
  260. ASSERT_REG_POSITION(fence_action, 0x1D);
  261. ASSERT_REG_POSITION(acquire_mode, 0x100);
  262. ASSERT_REG_POSITION(acquire_source, 0x101);
  263. ASSERT_REG_POSITION(acquire_active, 0x102);
  264. ASSERT_REG_POSITION(acquire_timeout, 0x103);
  265. ASSERT_REG_POSITION(acquire_value, 0x104);
  266. #undef ASSERT_REG_POSITION
  267. } // namespace Tegra