gpu.h 7.2 KB

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