maxwell_dma.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 "common/logging/log.h"
  6. #include "common/microprofile.h"
  7. #include "common/settings.h"
  8. #include "core/core.h"
  9. #include "video_core/engines/maxwell_3d.h"
  10. #include "video_core/engines/maxwell_dma.h"
  11. #include "video_core/memory_manager.h"
  12. #include "video_core/renderer_base.h"
  13. #include "video_core/textures/decoders.h"
  14. MICROPROFILE_DECLARE(GPU_DMAEngine);
  15. MICROPROFILE_DEFINE(GPU_DMAEngine, "GPU", "DMA Engine", MP_RGB(224, 224, 128));
  16. namespace Tegra::Engines {
  17. using namespace Texture;
  18. MaxwellDMA::MaxwellDMA(Core::System& system_, MemoryManager& memory_manager_)
  19. : system{system_}, memory_manager{memory_manager_} {}
  20. MaxwellDMA::~MaxwellDMA() = default;
  21. void MaxwellDMA::BindRasterizer(VideoCore::RasterizerInterface* rasterizer_) {
  22. rasterizer = rasterizer_;
  23. }
  24. void MaxwellDMA::CallMethod(u32 method, u32 method_argument, bool is_last_call) {
  25. ASSERT_MSG(method < NUM_REGS, "Invalid MaxwellDMA register");
  26. regs.reg_array[method] = method_argument;
  27. if (method == offsetof(Regs, launch_dma) / sizeof(u32)) {
  28. Launch();
  29. }
  30. }
  31. void MaxwellDMA::CallMultiMethod(u32 method, const u32* base_start, u32 amount,
  32. u32 methods_pending) {
  33. for (size_t i = 0; i < amount; ++i) {
  34. CallMethod(method, base_start[i], methods_pending - static_cast<u32>(i) <= 1);
  35. }
  36. }
  37. void MaxwellDMA::Launch() {
  38. MICROPROFILE_SCOPE(GPU_DMAEngine);
  39. LOG_TRACE(Render_OpenGL, "DMA copy 0x{:x} -> 0x{:x}", static_cast<GPUVAddr>(regs.offset_in),
  40. static_cast<GPUVAddr>(regs.offset_out));
  41. // TODO(Subv): Perform more research and implement all features of this engine.
  42. const LaunchDMA& launch = regs.launch_dma;
  43. ASSERT(launch.semaphore_type == LaunchDMA::SemaphoreType::NONE);
  44. ASSERT(launch.interrupt_type == LaunchDMA::InterruptType::NONE);
  45. ASSERT(launch.data_transfer_type == LaunchDMA::DataTransferType::NON_PIPELINED);
  46. ASSERT(regs.dst_params.origin.x == 0);
  47. ASSERT(regs.dst_params.origin.y == 0);
  48. const bool is_src_pitch = launch.src_memory_layout == LaunchDMA::MemoryLayout::PITCH;
  49. const bool is_dst_pitch = launch.dst_memory_layout == LaunchDMA::MemoryLayout::PITCH;
  50. if (!is_src_pitch && !is_dst_pitch) {
  51. // If both the source and the destination are in block layout, assert.
  52. UNREACHABLE_MSG("Tiled->Tiled DMA transfers are not yet implemented");
  53. return;
  54. }
  55. if (is_src_pitch && is_dst_pitch) {
  56. CopyPitchToPitch();
  57. } else {
  58. ASSERT(launch.multi_line_enable == 1);
  59. if (!is_src_pitch && is_dst_pitch) {
  60. CopyBlockLinearToPitch();
  61. } else {
  62. CopyPitchToBlockLinear();
  63. }
  64. }
  65. }
  66. void MaxwellDMA::CopyPitchToPitch() {
  67. // When `multi_line_enable` bit is disabled the copy is performed as if we were copying a 1D
  68. // buffer of length `line_length_in`.
  69. // Otherwise we copy a 2D image of dimensions (line_length_in, line_count).
  70. auto& accelerate = rasterizer->AccessAccelerateDMA();
  71. if (!regs.launch_dma.multi_line_enable) {
  72. const bool is_buffer_clear = regs.launch_dma.remap_enable != 0 &&
  73. regs.remap_const.dst_x == RemapConst::Swizzle::CONST_A;
  74. // TODO: allow multisized components.
  75. if (is_buffer_clear) {
  76. ASSERT(regs.remap_const.component_size_minus_one == 3);
  77. accelerate.BufferClear(regs.offset_out, regs.line_length_in, regs.remap_consta_value);
  78. std::vector<u32> tmp_buffer(regs.line_length_in, regs.remap_consta_value);
  79. memory_manager.WriteBlockUnsafe(regs.offset_out,
  80. reinterpret_cast<u8*>(tmp_buffer.data()),
  81. regs.line_length_in * sizeof(u32));
  82. return;
  83. }
  84. UNIMPLEMENTED_IF(regs.launch_dma.remap_enable != 0);
  85. if (!accelerate.BufferCopy(regs.offset_in, regs.offset_out, regs.line_length_in)) {
  86. std::vector<u8> tmp_buffer(regs.line_length_in);
  87. memory_manager.ReadBlockUnsafe(regs.offset_in, tmp_buffer.data(), regs.line_length_in);
  88. memory_manager.WriteBlock(regs.offset_out, tmp_buffer.data(), regs.line_length_in);
  89. }
  90. return;
  91. }
  92. UNIMPLEMENTED_IF(regs.launch_dma.remap_enable != 0);
  93. // Perform a line-by-line copy.
  94. // We're going to take a subrect of size (line_length_in, line_count) from the source rectangle.
  95. // There is no need to manually flush/invalidate the regions because CopyBlock does that for us.
  96. for (u32 line = 0; line < regs.line_count; ++line) {
  97. const GPUVAddr source_line = regs.offset_in + static_cast<size_t>(line) * regs.pitch_in;
  98. const GPUVAddr dest_line = regs.offset_out + static_cast<size_t>(line) * regs.pitch_out;
  99. memory_manager.CopyBlock(dest_line, source_line, regs.line_length_in);
  100. }
  101. }
  102. void MaxwellDMA::CopyBlockLinearToPitch() {
  103. UNIMPLEMENTED_IF(regs.src_params.block_size.width != 0);
  104. UNIMPLEMENTED_IF(regs.src_params.block_size.depth != 0);
  105. UNIMPLEMENTED_IF(regs.src_params.layer != 0);
  106. // Optimized path for micro copies.
  107. const size_t dst_size = static_cast<size_t>(regs.pitch_out) * regs.line_count;
  108. if (dst_size < GOB_SIZE && regs.pitch_out <= GOB_SIZE_X &&
  109. regs.src_params.height > GOB_SIZE_Y) {
  110. FastCopyBlockLinearToPitch();
  111. return;
  112. }
  113. // Deswizzle the input and copy it over.
  114. UNIMPLEMENTED_IF(regs.launch_dma.remap_enable != 0);
  115. const u32 bytes_per_pixel = regs.pitch_out / regs.line_length_in;
  116. const Parameters& src_params = regs.src_params;
  117. const u32 width = src_params.width;
  118. const u32 height = src_params.height;
  119. const u32 depth = src_params.depth;
  120. const u32 block_height = src_params.block_size.height;
  121. const u32 block_depth = src_params.block_size.depth;
  122. const size_t src_size =
  123. CalculateSize(true, bytes_per_pixel, width, height, depth, block_height, block_depth);
  124. if (read_buffer.size() < src_size) {
  125. read_buffer.resize(src_size);
  126. }
  127. if (write_buffer.size() < dst_size) {
  128. write_buffer.resize(dst_size);
  129. }
  130. memory_manager.ReadBlock(regs.offset_in, read_buffer.data(), src_size);
  131. memory_manager.ReadBlock(regs.offset_out, write_buffer.data(), dst_size);
  132. UnswizzleSubrect(regs.line_length_in, regs.line_count, regs.pitch_out, width, bytes_per_pixel,
  133. block_height, src_params.origin.x, src_params.origin.y, write_buffer.data(),
  134. read_buffer.data());
  135. memory_manager.WriteBlock(regs.offset_out, write_buffer.data(), dst_size);
  136. }
  137. void MaxwellDMA::CopyPitchToBlockLinear() {
  138. UNIMPLEMENTED_IF_MSG(regs.dst_params.block_size.width != 0, "Block width is not one");
  139. UNIMPLEMENTED_IF(regs.launch_dma.remap_enable != 0);
  140. const auto& dst_params = regs.dst_params;
  141. const u32 bytes_per_pixel = regs.pitch_in / regs.line_length_in;
  142. const u32 width = dst_params.width;
  143. const u32 height = dst_params.height;
  144. const u32 depth = dst_params.depth;
  145. const u32 block_height = dst_params.block_size.height;
  146. const u32 block_depth = dst_params.block_size.depth;
  147. const size_t dst_size =
  148. CalculateSize(true, bytes_per_pixel, width, height, depth, block_height, block_depth);
  149. const size_t dst_layer_size =
  150. CalculateSize(true, bytes_per_pixel, width, height, 1, block_height, block_depth);
  151. const size_t src_size = static_cast<size_t>(regs.pitch_in) * regs.line_count;
  152. if (read_buffer.size() < src_size) {
  153. read_buffer.resize(src_size);
  154. }
  155. if (write_buffer.size() < dst_size) {
  156. write_buffer.resize(dst_size);
  157. }
  158. if (Settings::IsGPULevelExtreme()) {
  159. memory_manager.ReadBlock(regs.offset_in, read_buffer.data(), src_size);
  160. memory_manager.ReadBlock(regs.offset_out, write_buffer.data(), dst_size);
  161. } else {
  162. memory_manager.ReadBlockUnsafe(regs.offset_in, read_buffer.data(), src_size);
  163. memory_manager.ReadBlockUnsafe(regs.offset_out, write_buffer.data(), dst_size);
  164. }
  165. // If the input is linear and the output is tiled, swizzle the input and copy it over.
  166. if (regs.dst_params.block_size.depth > 0) {
  167. ASSERT(dst_params.layer == 0);
  168. SwizzleSliceToVoxel(regs.line_length_in, regs.line_count, regs.pitch_in, width, height,
  169. bytes_per_pixel, block_height, block_depth, dst_params.origin.x,
  170. dst_params.origin.y, write_buffer.data(), read_buffer.data());
  171. } else {
  172. SwizzleSubrect(regs.line_length_in, regs.line_count, regs.pitch_in, width, bytes_per_pixel,
  173. write_buffer.data() + dst_layer_size * dst_params.layer, read_buffer.data(),
  174. block_height, dst_params.origin.x, dst_params.origin.y);
  175. }
  176. memory_manager.WriteBlock(regs.offset_out, write_buffer.data(), dst_size);
  177. }
  178. void MaxwellDMA::FastCopyBlockLinearToPitch() {
  179. const u32 bytes_per_pixel = regs.pitch_out / regs.line_length_in;
  180. const size_t src_size = GOB_SIZE;
  181. const size_t dst_size = static_cast<size_t>(regs.pitch_out) * regs.line_count;
  182. u32 pos_x = regs.src_params.origin.x;
  183. u32 pos_y = regs.src_params.origin.y;
  184. const u64 offset = GetGOBOffset(regs.src_params.width, regs.src_params.height, pos_x, pos_y,
  185. regs.src_params.block_size.height, bytes_per_pixel);
  186. const u32 x_in_gob = 64 / bytes_per_pixel;
  187. pos_x = pos_x % x_in_gob;
  188. pos_y = pos_y % 8;
  189. if (read_buffer.size() < src_size) {
  190. read_buffer.resize(src_size);
  191. }
  192. if (write_buffer.size() < dst_size) {
  193. write_buffer.resize(dst_size);
  194. }
  195. if (Settings::IsGPULevelExtreme()) {
  196. memory_manager.ReadBlock(regs.offset_in + offset, read_buffer.data(), src_size);
  197. memory_manager.ReadBlock(regs.offset_out, write_buffer.data(), dst_size);
  198. } else {
  199. memory_manager.ReadBlockUnsafe(regs.offset_in + offset, read_buffer.data(), src_size);
  200. memory_manager.ReadBlockUnsafe(regs.offset_out, write_buffer.data(), dst_size);
  201. }
  202. UnswizzleSubrect(regs.line_length_in, regs.line_count, regs.pitch_out, regs.src_params.width,
  203. bytes_per_pixel, regs.src_params.block_size.height, pos_x, pos_y,
  204. write_buffer.data(), read_buffer.data());
  205. memory_manager.WriteBlock(regs.offset_out, write_buffer.data(), dst_size);
  206. }
  207. } // namespace Tegra::Engines