gpu.h 8.6 KB

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