maxwell_dma.cpp 8.8 KB

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