gpu.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 GPU memory manager.
  132. Tegra::MemoryManager& MemoryManager();
  133. /// Returns a const reference to the GPU memory manager.
  134. const Tegra::MemoryManager& MemoryManager() const;
  135. /// Returns a reference to the GPU DMA pusher.
  136. Tegra::DmaPusher& DmaPusher();
  137. /// Returns a const reference to the GPU DMA pusher.
  138. const Tegra::DmaPusher& DmaPusher() const;
  139. struct Regs {
  140. static constexpr size_t NUM_REGS = 0x100;
  141. union {
  142. struct {
  143. INSERT_PADDING_WORDS(0x4);
  144. struct {
  145. u32 address_high;
  146. u32 address_low;
  147. GPUVAddr SemaphoreAddress() const {
  148. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  149. address_low);
  150. }
  151. } semaphore_address;
  152. u32 semaphore_sequence;
  153. u32 semaphore_trigger;
  154. INSERT_PADDING_WORDS(0xC);
  155. // The puser and the puller share the reference counter, the pusher only has read
  156. // access
  157. u32 reference_count;
  158. INSERT_PADDING_WORDS(0x5);
  159. u32 semaphore_acquire;
  160. u32 semaphore_release;
  161. INSERT_PADDING_WORDS(0xE4);
  162. // Puller state
  163. u32 acquire_mode;
  164. u32 acquire_source;
  165. u32 acquire_active;
  166. u32 acquire_timeout;
  167. u32 acquire_value;
  168. };
  169. std::array<u32, NUM_REGS> reg_array;
  170. };
  171. } regs{};
  172. /// Performs any additional setup necessary in order to begin GPU emulation.
  173. /// This can be used to launch any necessary threads and register any necessary
  174. /// core timing events.
  175. virtual void Start() = 0;
  176. /// Push GPU command entries to be processed
  177. virtual void PushGPUEntries(Tegra::CommandList&& entries) = 0;
  178. /// Swap buffers (render frame)
  179. virtual void SwapBuffers(
  180. std::optional<std::reference_wrapper<const Tegra::FramebufferConfig>> framebuffer) = 0;
  181. /// Notify rasterizer that any caches of the specified region should be flushed to Switch memory
  182. virtual void FlushRegion(CacheAddr addr, u64 size) = 0;
  183. /// Notify rasterizer that any caches of the specified region should be invalidated
  184. virtual void InvalidateRegion(CacheAddr addr, u64 size) = 0;
  185. /// Notify rasterizer that any caches of the specified region should be flushed and invalidated
  186. virtual void FlushAndInvalidateRegion(CacheAddr addr, u64 size) = 0;
  187. private:
  188. void ProcessBindMethod(const MethodCall& method_call);
  189. void ProcessSemaphoreTriggerMethod();
  190. void ProcessSemaphoreRelease();
  191. void ProcessSemaphoreAcquire();
  192. /// Calls a GPU puller method.
  193. void CallPullerMethod(const MethodCall& method_call);
  194. /// Calls a GPU engine method.
  195. void CallEngineMethod(const MethodCall& method_call);
  196. /// Determines where the method should be executed.
  197. bool ExecuteMethodOnEngine(const MethodCall& method_call);
  198. protected:
  199. std::unique_ptr<Tegra::DmaPusher> dma_pusher;
  200. VideoCore::RendererBase& renderer;
  201. private:
  202. std::unique_ptr<Tegra::MemoryManager> memory_manager;
  203. /// Mapping of command subchannels to their bound engine ids
  204. std::array<EngineID, 8> bound_engines = {};
  205. /// 3D engine
  206. std::unique_ptr<Engines::Maxwell3D> maxwell_3d;
  207. /// 2D engine
  208. std::unique_ptr<Engines::Fermi2D> fermi_2d;
  209. /// Compute engine
  210. std::unique_ptr<Engines::KeplerCompute> kepler_compute;
  211. /// DMA engine
  212. std::unique_ptr<Engines::MaxwellDMA> maxwell_dma;
  213. /// Inline memory engine
  214. std::unique_ptr<Engines::KeplerMemory> kepler_memory;
  215. };
  216. #define ASSERT_REG_POSITION(field_name, position) \
  217. static_assert(offsetof(GPU::Regs, field_name) == position * 4, \
  218. "Field " #field_name " has invalid position")
  219. ASSERT_REG_POSITION(semaphore_address, 0x4);
  220. ASSERT_REG_POSITION(semaphore_sequence, 0x6);
  221. ASSERT_REG_POSITION(semaphore_trigger, 0x7);
  222. ASSERT_REG_POSITION(reference_count, 0x14);
  223. ASSERT_REG_POSITION(semaphore_acquire, 0x1A);
  224. ASSERT_REG_POSITION(semaphore_release, 0x1B);
  225. ASSERT_REG_POSITION(acquire_mode, 0x100);
  226. ASSERT_REG_POSITION(acquire_source, 0x101);
  227. ASSERT_REG_POSITION(acquire_active, 0x102);
  228. ASSERT_REG_POSITION(acquire_timeout, 0x103);
  229. ASSERT_REG_POSITION(acquire_value, 0x104);
  230. #undef ASSERT_REG_POSITION
  231. } // namespace Tegra