gpu.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/assert.h"
  5. #include "core/core.h"
  6. #include "core/core_timing.h"
  7. #include "core/memory.h"
  8. #include "video_core/engines/fermi_2d.h"
  9. #include "video_core/engines/kepler_compute.h"
  10. #include "video_core/engines/kepler_memory.h"
  11. #include "video_core/engines/maxwell_3d.h"
  12. #include "video_core/engines/maxwell_dma.h"
  13. #include "video_core/gpu.h"
  14. #include "video_core/memory_manager.h"
  15. #include "video_core/renderer_base.h"
  16. namespace Tegra {
  17. u32 FramebufferConfig::BytesPerPixel(PixelFormat format) {
  18. switch (format) {
  19. case PixelFormat::ABGR8:
  20. case PixelFormat::BGRA8:
  21. return 4;
  22. default:
  23. return 4;
  24. }
  25. UNREACHABLE();
  26. }
  27. GPU::GPU(Core::System& system, VideoCore::RendererBase& renderer, bool is_async)
  28. : system{system}, renderer{renderer}, is_async{is_async} {
  29. auto& rasterizer{renderer.Rasterizer()};
  30. memory_manager = std::make_unique<Tegra::MemoryManager>(system, rasterizer);
  31. dma_pusher = std::make_unique<Tegra::DmaPusher>(*this);
  32. maxwell_3d = std::make_unique<Engines::Maxwell3D>(system, rasterizer, *memory_manager);
  33. fermi_2d = std::make_unique<Engines::Fermi2D>(rasterizer, *memory_manager);
  34. kepler_compute = std::make_unique<Engines::KeplerCompute>(system, rasterizer, *memory_manager);
  35. maxwell_dma = std::make_unique<Engines::MaxwellDMA>(system, rasterizer, *memory_manager);
  36. kepler_memory = std::make_unique<Engines::KeplerMemory>(system, *memory_manager);
  37. }
  38. GPU::~GPU() = default;
  39. Engines::Maxwell3D& GPU::Maxwell3D() {
  40. return *maxwell_3d;
  41. }
  42. const Engines::Maxwell3D& GPU::Maxwell3D() const {
  43. return *maxwell_3d;
  44. }
  45. Engines::KeplerCompute& GPU::KeplerCompute() {
  46. return *kepler_compute;
  47. }
  48. const Engines::KeplerCompute& GPU::KeplerCompute() const {
  49. return *kepler_compute;
  50. }
  51. MemoryManager& GPU::MemoryManager() {
  52. return *memory_manager;
  53. }
  54. const MemoryManager& GPU::MemoryManager() const {
  55. return *memory_manager;
  56. }
  57. DmaPusher& GPU::DmaPusher() {
  58. return *dma_pusher;
  59. }
  60. const DmaPusher& GPU::DmaPusher() const {
  61. return *dma_pusher;
  62. }
  63. void GPU::IncrementSyncPoint(const u32 syncpoint_id) {
  64. syncpoints[syncpoint_id]++;
  65. std::lock_guard lock{sync_mutex};
  66. if (!syncpt_interrupts[syncpoint_id].empty()) {
  67. u32 value = syncpoints[syncpoint_id].load();
  68. auto it = syncpt_interrupts[syncpoint_id].begin();
  69. while (it != syncpt_interrupts[syncpoint_id].end()) {
  70. if (value >= *it) {
  71. TriggerCpuInterrupt(syncpoint_id, *it);
  72. it = syncpt_interrupts[syncpoint_id].erase(it);
  73. continue;
  74. }
  75. it++;
  76. }
  77. }
  78. }
  79. u32 GPU::GetSyncpointValue(const u32 syncpoint_id) const {
  80. return syncpoints[syncpoint_id].load();
  81. }
  82. void GPU::RegisterSyncptInterrupt(const u32 syncpoint_id, const u32 value) {
  83. auto& interrupt = syncpt_interrupts[syncpoint_id];
  84. bool contains = std::any_of(interrupt.begin(), interrupt.end(),
  85. [value](u32 in_value) { return in_value == value; });
  86. if (contains) {
  87. return;
  88. }
  89. syncpt_interrupts[syncpoint_id].emplace_back(value);
  90. }
  91. bool GPU::CancelSyncptInterrupt(const u32 syncpoint_id, const u32 value) {
  92. std::lock_guard lock{sync_mutex};
  93. auto& interrupt = syncpt_interrupts[syncpoint_id];
  94. const auto iter =
  95. std::find_if(interrupt.begin(), interrupt.end(),
  96. [value](u32 interrupt_value) { return value == interrupt_value; });
  97. if (iter == interrupt.end()) {
  98. return false;
  99. }
  100. interrupt.erase(iter);
  101. return true;
  102. }
  103. u32 RenderTargetBytesPerPixel(RenderTargetFormat format) {
  104. ASSERT(format != RenderTargetFormat::NONE);
  105. switch (format) {
  106. case RenderTargetFormat::RGBA32_FLOAT:
  107. case RenderTargetFormat::RGBA32_UINT:
  108. return 16;
  109. case RenderTargetFormat::RGBA16_UINT:
  110. case RenderTargetFormat::RGBA16_UNORM:
  111. case RenderTargetFormat::RGBA16_FLOAT:
  112. case RenderTargetFormat::RG32_FLOAT:
  113. case RenderTargetFormat::RG32_UINT:
  114. return 8;
  115. case RenderTargetFormat::RGBA8_UNORM:
  116. case RenderTargetFormat::RGBA8_SNORM:
  117. case RenderTargetFormat::RGBA8_SRGB:
  118. case RenderTargetFormat::RGBA8_UINT:
  119. case RenderTargetFormat::RGB10_A2_UNORM:
  120. case RenderTargetFormat::BGRA8_UNORM:
  121. case RenderTargetFormat::BGRA8_SRGB:
  122. case RenderTargetFormat::RG16_UNORM:
  123. case RenderTargetFormat::RG16_SNORM:
  124. case RenderTargetFormat::RG16_UINT:
  125. case RenderTargetFormat::RG16_SINT:
  126. case RenderTargetFormat::RG16_FLOAT:
  127. case RenderTargetFormat::R32_FLOAT:
  128. case RenderTargetFormat::R11G11B10_FLOAT:
  129. case RenderTargetFormat::R32_UINT:
  130. return 4;
  131. case RenderTargetFormat::R16_UNORM:
  132. case RenderTargetFormat::R16_SNORM:
  133. case RenderTargetFormat::R16_UINT:
  134. case RenderTargetFormat::R16_SINT:
  135. case RenderTargetFormat::R16_FLOAT:
  136. case RenderTargetFormat::RG8_UNORM:
  137. case RenderTargetFormat::RG8_SNORM:
  138. return 2;
  139. case RenderTargetFormat::R8_UNORM:
  140. case RenderTargetFormat::R8_UINT:
  141. return 1;
  142. default:
  143. UNIMPLEMENTED_MSG("Unimplemented render target format {}", static_cast<u32>(format));
  144. return 1;
  145. }
  146. }
  147. u32 DepthFormatBytesPerPixel(DepthFormat format) {
  148. switch (format) {
  149. case DepthFormat::Z32_S8_X24_FLOAT:
  150. return 8;
  151. case DepthFormat::Z32_FLOAT:
  152. case DepthFormat::S8_Z24_UNORM:
  153. case DepthFormat::Z24_X8_UNORM:
  154. case DepthFormat::Z24_S8_UNORM:
  155. case DepthFormat::Z24_C8_UNORM:
  156. return 4;
  157. case DepthFormat::Z16_UNORM:
  158. return 2;
  159. default:
  160. UNIMPLEMENTED_MSG("Unimplemented Depth format {}", static_cast<u32>(format));
  161. return 1;
  162. }
  163. }
  164. // Note that, traditionally, methods are treated as 4-byte addressable locations, and hence
  165. // their numbers are written down multiplied by 4 in Docs. Here we are not multiply by 4.
  166. // So the values you see in docs might be multiplied by 4.
  167. enum class BufferMethods {
  168. BindObject = 0x0,
  169. Nop = 0x2,
  170. SemaphoreAddressHigh = 0x4,
  171. SemaphoreAddressLow = 0x5,
  172. SemaphoreSequence = 0x6,
  173. SemaphoreTrigger = 0x7,
  174. NotifyIntr = 0x8,
  175. WrcacheFlush = 0x9,
  176. Unk28 = 0xA,
  177. UnkCacheFlush = 0xB,
  178. RefCnt = 0x14,
  179. SemaphoreAcquire = 0x1A,
  180. SemaphoreRelease = 0x1B,
  181. FenceValue = 0x1C,
  182. FenceAction = 0x1D,
  183. Unk78 = 0x1E,
  184. Unk7c = 0x1F,
  185. Yield = 0x20,
  186. NonPullerMethods = 0x40,
  187. };
  188. enum class GpuSemaphoreOperation {
  189. AcquireEqual = 0x1,
  190. WriteLong = 0x2,
  191. AcquireGequal = 0x4,
  192. AcquireMask = 0x8,
  193. };
  194. void GPU::CallMethod(const MethodCall& method_call) {
  195. LOG_TRACE(HW_GPU, "Processing method {:08X} on subchannel {}", method_call.method,
  196. method_call.subchannel);
  197. ASSERT(method_call.subchannel < bound_engines.size());
  198. if (ExecuteMethodOnEngine(method_call)) {
  199. CallEngineMethod(method_call);
  200. } else {
  201. CallPullerMethod(method_call);
  202. }
  203. }
  204. bool GPU::ExecuteMethodOnEngine(const MethodCall& method_call) {
  205. const auto method = static_cast<BufferMethods>(method_call.method);
  206. return method >= BufferMethods::NonPullerMethods;
  207. }
  208. void GPU::CallPullerMethod(const MethodCall& method_call) {
  209. regs.reg_array[method_call.method] = method_call.argument;
  210. const auto method = static_cast<BufferMethods>(method_call.method);
  211. switch (method) {
  212. case BufferMethods::BindObject: {
  213. ProcessBindMethod(method_call);
  214. break;
  215. }
  216. case BufferMethods::Nop:
  217. case BufferMethods::SemaphoreAddressHigh:
  218. case BufferMethods::SemaphoreAddressLow:
  219. case BufferMethods::SemaphoreSequence:
  220. case BufferMethods::RefCnt:
  221. case BufferMethods::UnkCacheFlush:
  222. case BufferMethods::WrcacheFlush:
  223. case BufferMethods::FenceValue:
  224. case BufferMethods::FenceAction:
  225. break;
  226. case BufferMethods::SemaphoreTrigger: {
  227. ProcessSemaphoreTriggerMethod();
  228. break;
  229. }
  230. case BufferMethods::NotifyIntr: {
  231. // TODO(Kmather73): Research and implement this method.
  232. LOG_ERROR(HW_GPU, "Special puller engine method NotifyIntr not implemented");
  233. break;
  234. }
  235. case BufferMethods::Unk28: {
  236. // TODO(Kmather73): Research and implement this method.
  237. LOG_ERROR(HW_GPU, "Special puller engine method Unk28 not implemented");
  238. break;
  239. }
  240. case BufferMethods::SemaphoreAcquire: {
  241. ProcessSemaphoreAcquire();
  242. break;
  243. }
  244. case BufferMethods::SemaphoreRelease: {
  245. ProcessSemaphoreRelease();
  246. break;
  247. }
  248. case BufferMethods::Yield: {
  249. // TODO(Kmather73): Research and implement this method.
  250. LOG_ERROR(HW_GPU, "Special puller engine method Yield not implemented");
  251. break;
  252. }
  253. default:
  254. LOG_ERROR(HW_GPU, "Special puller engine method {:X} not implemented",
  255. static_cast<u32>(method));
  256. break;
  257. }
  258. }
  259. void GPU::CallEngineMethod(const MethodCall& method_call) {
  260. const EngineID engine = bound_engines[method_call.subchannel];
  261. switch (engine) {
  262. case EngineID::FERMI_TWOD_A:
  263. fermi_2d->CallMethod(method_call);
  264. break;
  265. case EngineID::MAXWELL_B:
  266. maxwell_3d->CallMethod(method_call);
  267. break;
  268. case EngineID::KEPLER_COMPUTE_B:
  269. kepler_compute->CallMethod(method_call);
  270. break;
  271. case EngineID::MAXWELL_DMA_COPY_A:
  272. maxwell_dma->CallMethod(method_call);
  273. break;
  274. case EngineID::KEPLER_INLINE_TO_MEMORY_B:
  275. kepler_memory->CallMethod(method_call);
  276. break;
  277. default:
  278. UNIMPLEMENTED_MSG("Unimplemented engine");
  279. }
  280. }
  281. void GPU::ProcessBindMethod(const MethodCall& method_call) {
  282. // Bind the current subchannel to the desired engine id.
  283. LOG_DEBUG(HW_GPU, "Binding subchannel {} to engine {}", method_call.subchannel,
  284. method_call.argument);
  285. bound_engines[method_call.subchannel] = static_cast<EngineID>(method_call.argument);
  286. }
  287. void GPU::ProcessSemaphoreTriggerMethod() {
  288. const auto semaphoreOperationMask = 0xF;
  289. const auto op =
  290. static_cast<GpuSemaphoreOperation>(regs.semaphore_trigger & semaphoreOperationMask);
  291. if (op == GpuSemaphoreOperation::WriteLong) {
  292. struct Block {
  293. u32 sequence;
  294. u32 zeros = 0;
  295. u64 timestamp;
  296. };
  297. Block block{};
  298. block.sequence = regs.semaphore_sequence;
  299. // TODO(Kmather73): Generate a real GPU timestamp and write it here instead of
  300. // CoreTiming
  301. block.timestamp = Core::System::GetInstance().CoreTiming().GetTicks();
  302. memory_manager->WriteBlock(regs.semaphore_address.SemaphoreAddress(), &block,
  303. sizeof(block));
  304. } else {
  305. const u32 word{memory_manager->Read<u32>(regs.semaphore_address.SemaphoreAddress())};
  306. if ((op == GpuSemaphoreOperation::AcquireEqual && word == regs.semaphore_sequence) ||
  307. (op == GpuSemaphoreOperation::AcquireGequal &&
  308. static_cast<s32>(word - regs.semaphore_sequence) > 0) ||
  309. (op == GpuSemaphoreOperation::AcquireMask && (word & regs.semaphore_sequence))) {
  310. // Nothing to do in this case
  311. } else {
  312. regs.acquire_source = true;
  313. regs.acquire_value = regs.semaphore_sequence;
  314. if (op == GpuSemaphoreOperation::AcquireEqual) {
  315. regs.acquire_active = true;
  316. regs.acquire_mode = false;
  317. } else if (op == GpuSemaphoreOperation::AcquireGequal) {
  318. regs.acquire_active = true;
  319. regs.acquire_mode = true;
  320. } else if (op == GpuSemaphoreOperation::AcquireMask) {
  321. // TODO(kemathe) The acquire mask operation waits for a value that, ANDed with
  322. // semaphore_sequence, gives a non-0 result
  323. LOG_ERROR(HW_GPU, "Invalid semaphore operation AcquireMask not implemented");
  324. } else {
  325. LOG_ERROR(HW_GPU, "Invalid semaphore operation");
  326. }
  327. }
  328. }
  329. }
  330. void GPU::ProcessSemaphoreRelease() {
  331. memory_manager->Write<u32>(regs.semaphore_address.SemaphoreAddress(), regs.semaphore_release);
  332. }
  333. void GPU::ProcessSemaphoreAcquire() {
  334. const u32 word = memory_manager->Read<u32>(regs.semaphore_address.SemaphoreAddress());
  335. const auto value = regs.semaphore_acquire;
  336. if (word != value) {
  337. regs.acquire_active = true;
  338. regs.acquire_value = value;
  339. // TODO(kemathe73) figure out how to do the acquire_timeout
  340. regs.acquire_mode = false;
  341. regs.acquire_source = false;
  342. }
  343. }
  344. } // namespace Tegra