gpu.h 6.9 KB

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