gpu.h 9.0 KB

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