dma_pusher.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/cityhash.h"
  4. #include "common/microprofile.h"
  5. #include "common/settings.h"
  6. #include "core/core.h"
  7. #include "video_core/dma_pusher.h"
  8. #include "video_core/engines/maxwell_3d.h"
  9. #include "video_core/gpu.h"
  10. #include "video_core/memory_manager.h"
  11. namespace Tegra {
  12. DmaPusher::DmaPusher(Core::System& system_, GPU& gpu_, MemoryManager& memory_manager_,
  13. Control::ChannelState& channel_state_)
  14. : gpu{gpu_}, system{system_}, memory_manager{memory_manager_}, puller{gpu_, memory_manager_,
  15. *this, channel_state_} {}
  16. DmaPusher::~DmaPusher() = default;
  17. MICROPROFILE_DEFINE(DispatchCalls, "GPU", "Execute command buffer", MP_RGB(128, 128, 192));
  18. void DmaPusher::DispatchCalls() {
  19. MICROPROFILE_SCOPE(DispatchCalls);
  20. dma_pushbuffer_subindex = 0;
  21. dma_state.is_last_call = true;
  22. while (system.IsPoweredOn()) {
  23. if (!Step()) {
  24. break;
  25. }
  26. }
  27. gpu.FlushCommands();
  28. gpu.OnCommandListEnd();
  29. }
  30. bool DmaPusher::Step() {
  31. if (!ib_enable || dma_pushbuffer.empty()) {
  32. // pushbuffer empty and IB empty or nonexistent - nothing to do
  33. return false;
  34. }
  35. CommandList& command_list{dma_pushbuffer.front()};
  36. ASSERT_OR_EXECUTE(
  37. command_list.command_lists.size() || command_list.prefetch_command_list.size(), {
  38. // Somehow the command_list is empty, in order to avoid a crash
  39. // We ignore it and assume its size is 0.
  40. dma_pushbuffer.pop();
  41. dma_pushbuffer_subindex = 0;
  42. return true;
  43. });
  44. if (command_list.prefetch_command_list.size()) {
  45. // Prefetched command list from nvdrv, used for things like synchronization
  46. ProcessCommands(command_list.prefetch_command_list);
  47. dma_pushbuffer.pop();
  48. } else {
  49. const CommandListHeader command_list_header{
  50. command_list.command_lists[dma_pushbuffer_subindex++]};
  51. dma_state.dma_get = command_list_header.addr;
  52. if (dma_pushbuffer_subindex >= command_list.command_lists.size()) {
  53. // We've gone through the current list, remove it from the queue
  54. dma_pushbuffer.pop();
  55. dma_pushbuffer_subindex = 0;
  56. }
  57. if (command_list_header.size == 0) {
  58. return true;
  59. }
  60. // Push buffer non-empty, read a word
  61. command_headers.resize_destructive(command_list_header.size);
  62. if (Settings::IsGPULevelExtreme()) {
  63. memory_manager.ReadBlock(dma_state.dma_get, command_headers.data(),
  64. command_list_header.size * sizeof(u32));
  65. } else {
  66. memory_manager.ReadBlockUnsafe(dma_state.dma_get, command_headers.data(),
  67. command_list_header.size * sizeof(u32));
  68. }
  69. ProcessCommands(command_headers);
  70. }
  71. return true;
  72. }
  73. void DmaPusher::ProcessCommands(std::span<const CommandHeader> commands) {
  74. for (std::size_t index = 0; index < commands.size();) {
  75. const CommandHeader& command_header = commands[index];
  76. if (dma_state.method_count) {
  77. // Data word of methods command
  78. if (dma_state.non_incrementing) {
  79. const u32 max_write = static_cast<u32>(
  80. std::min<std::size_t>(index + dma_state.method_count, commands.size()) - index);
  81. CallMultiMethod(&command_header.argument, max_write);
  82. dma_state.method_count -= max_write;
  83. dma_state.is_last_call = true;
  84. index += max_write;
  85. continue;
  86. } else {
  87. dma_state.is_last_call = dma_state.method_count <= 1;
  88. CallMethod(command_header.argument);
  89. }
  90. if (!dma_state.non_incrementing) {
  91. dma_state.method++;
  92. }
  93. if (dma_increment_once) {
  94. dma_state.non_incrementing = true;
  95. }
  96. dma_state.method_count--;
  97. } else {
  98. // No command active - this is the first word of a new one
  99. switch (command_header.mode) {
  100. case SubmissionMode::Increasing:
  101. SetState(command_header);
  102. dma_state.non_incrementing = false;
  103. dma_increment_once = false;
  104. break;
  105. case SubmissionMode::NonIncreasing:
  106. SetState(command_header);
  107. dma_state.non_incrementing = true;
  108. dma_increment_once = false;
  109. break;
  110. case SubmissionMode::Inline:
  111. dma_state.method = command_header.method;
  112. dma_state.subchannel = command_header.subchannel;
  113. CallMethod(command_header.arg_count);
  114. dma_state.non_incrementing = true;
  115. dma_increment_once = false;
  116. break;
  117. case SubmissionMode::IncreaseOnce:
  118. SetState(command_header);
  119. dma_state.non_incrementing = false;
  120. dma_increment_once = true;
  121. break;
  122. default:
  123. break;
  124. }
  125. }
  126. index++;
  127. }
  128. }
  129. void DmaPusher::SetState(const CommandHeader& command_header) {
  130. dma_state.method = command_header.method;
  131. dma_state.subchannel = command_header.subchannel;
  132. dma_state.method_count = command_header.method_count;
  133. }
  134. void DmaPusher::CallMethod(u32 argument) const {
  135. if (dma_state.method < non_puller_methods) {
  136. puller.CallPullerMethod(Engines::Puller::MethodCall{
  137. dma_state.method,
  138. argument,
  139. dma_state.subchannel,
  140. dma_state.method_count,
  141. });
  142. } else {
  143. subchannels[dma_state.subchannel]->CallMethod(dma_state.method, argument,
  144. dma_state.is_last_call);
  145. }
  146. }
  147. void DmaPusher::CallMultiMethod(const u32* base_start, u32 num_methods) const {
  148. if (dma_state.method < non_puller_methods) {
  149. puller.CallMultiMethod(dma_state.method, dma_state.subchannel, base_start, num_methods,
  150. dma_state.method_count);
  151. } else {
  152. auto subchannel = subchannels[dma_state.subchannel];
  153. subchannel->current_dma_segment = dma_state.dma_get;
  154. subchannel->CallMultiMethod(dma_state.method, base_start, num_methods,
  155. dma_state.method_count);
  156. }
  157. }
  158. void DmaPusher::BindRasterizer(VideoCore::RasterizerInterface* rasterizer) {
  159. puller.BindRasterizer(rasterizer);
  160. }
  161. } // namespace Tegra