maxwell_dma.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "core/core.h"
  7. #include "core/settings.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. void MaxwellDMA::CallMethod(u32 method, u32 method_argument, bool is_last_call) {
  18. ASSERT_MSG(method < NUM_REGS, "Invalid MaxwellDMA register");
  19. regs.reg_array[method] = method_argument;
  20. if (method == offsetof(Regs, launch_dma) / sizeof(u32)) {
  21. Launch();
  22. }
  23. }
  24. void MaxwellDMA::CallMultiMethod(u32 method, const u32* base_start, u32 amount,
  25. u32 methods_pending) {
  26. for (size_t i = 0; i < amount; ++i) {
  27. CallMethod(method, base_start[i], methods_pending - static_cast<u32>(i) <= 1);
  28. }
  29. }
  30. void MaxwellDMA::Launch() {
  31. LOG_TRACE(Render_OpenGL, "DMA copy 0x{:x} -> 0x{:x}", static_cast<GPUVAddr>(regs.offset_in),
  32. static_cast<GPUVAddr>(regs.offset_out));
  33. // TODO(Subv): Perform more research and implement all features of this engine.
  34. const LaunchDMA& launch = regs.launch_dma;
  35. ASSERT(launch.remap_enable == 0);
  36. ASSERT(launch.semaphore_type == LaunchDMA::SemaphoreType::NONE);
  37. ASSERT(launch.interrupt_type == LaunchDMA::InterruptType::NONE);
  38. ASSERT(launch.data_transfer_type == LaunchDMA::DataTransferType::NON_PIPELINED);
  39. ASSERT(regs.dst_params.origin.x == 0);
  40. ASSERT(regs.dst_params.origin.y == 0);
  41. const bool is_src_pitch = launch.src_memory_layout == LaunchDMA::MemoryLayout::PITCH;
  42. const bool is_dst_pitch = launch.dst_memory_layout == LaunchDMA::MemoryLayout::PITCH;
  43. if (!is_src_pitch && !is_dst_pitch) {
  44. // If both the source and the destination are in block layout, assert.
  45. UNREACHABLE_MSG("Tiled->Tiled DMA transfers are not yet implemented");
  46. return;
  47. }
  48. // All copies here update the main memory, so mark all rasterizer states as invalid.
  49. system.GPU().Maxwell3D().OnMemoryWrite();
  50. if (is_src_pitch && is_dst_pitch) {
  51. CopyPitchToPitch();
  52. } else {
  53. ASSERT(launch.multi_line_enable == 1);
  54. if (!is_src_pitch && is_dst_pitch) {
  55. CopyBlockLinearToPitch();
  56. } else {
  57. CopyPitchToBlockLinear();
  58. }
  59. }
  60. }
  61. void MaxwellDMA::CopyPitchToPitch() {
  62. // When `multi_line_enable` bit is disabled the copy is performed as if we were copying a 1D
  63. // buffer of length `line_length_in`.
  64. // Otherwise we copy a 2D image of dimensions (line_length_in, line_count).
  65. if (!regs.launch_dma.multi_line_enable) {
  66. memory_manager.CopyBlock(regs.offset_out, regs.offset_in, regs.line_length_in);
  67. return;
  68. }
  69. // Perform a line-by-line copy.
  70. // We're going to take a subrect of size (line_length_in, line_count) from the source rectangle.
  71. // There is no need to manually flush/invalidate the regions because CopyBlock does that for us.
  72. for (u32 line = 0; line < regs.line_count; ++line) {
  73. const GPUVAddr source_line = regs.offset_in + static_cast<size_t>(line) * regs.pitch_in;
  74. const GPUVAddr dest_line = regs.offset_out + static_cast<size_t>(line) * regs.pitch_out;
  75. memory_manager.CopyBlock(dest_line, source_line, regs.line_length_in);
  76. }
  77. }
  78. void MaxwellDMA::CopyBlockLinearToPitch() {
  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. const auto& dst_params = regs.dst_params;
  112. const u32 bytes_per_pixel = regs.pitch_in / regs.line_length_in;
  113. const u32 width = dst_params.width;
  114. const u32 height = dst_params.height;
  115. const u32 depth = dst_params.depth;
  116. const u32 block_height = dst_params.block_size.height;
  117. const u32 block_depth = dst_params.block_size.depth;
  118. const size_t dst_size =
  119. CalculateSize(true, bytes_per_pixel, width, height, depth, block_height, block_depth);
  120. const size_t dst_layer_size =
  121. CalculateSize(true, bytes_per_pixel, width, height, 1, block_height, block_depth);
  122. const size_t src_size = static_cast<size_t>(regs.pitch_in) * regs.line_count;
  123. if (read_buffer.size() < src_size) {
  124. read_buffer.resize(src_size);
  125. }
  126. if (write_buffer.size() < dst_size) {
  127. write_buffer.resize(dst_size);
  128. }
  129. if (Settings::IsGPULevelExtreme()) {
  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. } else {
  133. memory_manager.ReadBlockUnsafe(regs.offset_in, read_buffer.data(), src_size);
  134. memory_manager.ReadBlockUnsafe(regs.offset_out, write_buffer.data(), dst_size);
  135. }
  136. // If the input is linear and the output is tiled, swizzle the input and copy it over.
  137. if (regs.dst_params.block_size.depth > 0) {
  138. ASSERT(dst_params.layer == 0);
  139. SwizzleSliceToVoxel(regs.line_length_in, regs.line_count, regs.pitch_in, width, height,
  140. bytes_per_pixel, block_height, block_depth, dst_params.origin.x,
  141. dst_params.origin.y, write_buffer.data(), read_buffer.data());
  142. } else {
  143. SwizzleSubrect(regs.line_length_in, regs.line_count, regs.pitch_in, width, bytes_per_pixel,
  144. write_buffer.data() + dst_layer_size * dst_params.layer, read_buffer.data(),
  145. block_height, dst_params.origin.x, dst_params.origin.y);
  146. }
  147. memory_manager.WriteBlock(regs.offset_out, write_buffer.data(), dst_size);
  148. }
  149. void MaxwellDMA::FastCopyBlockLinearToPitch() {
  150. const u32 bytes_per_pixel = regs.pitch_out / regs.line_length_in;
  151. const size_t src_size = GOB_SIZE;
  152. const size_t dst_size = static_cast<size_t>(regs.pitch_out) * regs.line_count;
  153. u32 pos_x = regs.src_params.origin.x;
  154. u32 pos_y = regs.src_params.origin.y;
  155. const u64 offset = GetGOBOffset(regs.src_params.width, regs.src_params.height, pos_x, pos_y,
  156. regs.src_params.block_size.height, bytes_per_pixel);
  157. const u32 x_in_gob = 64 / bytes_per_pixel;
  158. pos_x = pos_x % x_in_gob;
  159. pos_y = pos_y % 8;
  160. if (read_buffer.size() < src_size) {
  161. read_buffer.resize(src_size);
  162. }
  163. if (write_buffer.size() < dst_size) {
  164. write_buffer.resize(dst_size);
  165. }
  166. if (Settings::IsGPULevelExtreme()) {
  167. memory_manager.ReadBlock(regs.offset_in + offset, read_buffer.data(), src_size);
  168. memory_manager.ReadBlock(regs.offset_out, write_buffer.data(), dst_size);
  169. } else {
  170. memory_manager.ReadBlockUnsafe(regs.offset_in + offset, read_buffer.data(), src_size);
  171. memory_manager.ReadBlockUnsafe(regs.offset_out, write_buffer.data(), dst_size);
  172. }
  173. UnswizzleSubrect(regs.line_length_in, regs.line_count, regs.pitch_out, regs.src_params.width,
  174. bytes_per_pixel, regs.src_params.block_size.height, pos_x, pos_y,
  175. write_buffer.data(), read_buffer.data());
  176. memory_manager.WriteBlock(regs.offset_out, write_buffer.data(), dst_size);
  177. }
  178. } // namespace Tegra::Engines