maxwell_dma.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. ASSERT(regs.src_params.block_size.depth == 0);
  80. // Optimized path for micro copies.
  81. const size_t dst_size = static_cast<size_t>(regs.pitch_out) * regs.line_count;
  82. if (dst_size < GOB_SIZE && regs.pitch_out <= GOB_SIZE_X) {
  83. FastCopyBlockLinearToPitch();
  84. return;
  85. }
  86. // Deswizzle the input and copy it over.
  87. const u32 bytes_per_pixel = regs.pitch_out / regs.line_length_in;
  88. const Parameters& src_params = regs.src_params;
  89. const u32 width = src_params.width;
  90. const u32 height = src_params.height;
  91. const u32 depth = src_params.depth;
  92. const u32 block_height = src_params.block_size.height;
  93. const u32 block_depth = src_params.block_size.depth;
  94. const size_t src_size =
  95. CalculateSize(true, bytes_per_pixel, width, height, depth, block_height, block_depth);
  96. const size_t src_layer_size =
  97. CalculateSize(true, bytes_per_pixel, width, height, 1, block_height, block_depth);
  98. if (read_buffer.size() < src_size) {
  99. read_buffer.resize(src_size);
  100. }
  101. if (write_buffer.size() < dst_size) {
  102. write_buffer.resize(dst_size);
  103. }
  104. if (Settings::IsGPULevelExtreme()) {
  105. memory_manager.ReadBlock(regs.offset_in, read_buffer.data(), src_size);
  106. memory_manager.ReadBlock(regs.offset_out, write_buffer.data(), dst_size);
  107. } else {
  108. memory_manager.ReadBlockUnsafe(regs.offset_in, read_buffer.data(), src_size);
  109. memory_manager.ReadBlockUnsafe(regs.offset_out, write_buffer.data(), dst_size);
  110. }
  111. UnswizzleSubrect(regs.line_length_in, regs.line_count, regs.pitch_out, width, bytes_per_pixel,
  112. read_buffer.data() + src_layer_size * src_params.layer, write_buffer.data(),
  113. block_height, src_params.origin.x, src_params.origin.y);
  114. memory_manager.WriteBlock(regs.offset_out, write_buffer.data(), dst_size);
  115. }
  116. void MaxwellDMA::CopyPitchToBlockLinear() {
  117. const auto& dst_params = regs.dst_params;
  118. const u32 bytes_per_pixel = regs.pitch_in / regs.line_length_in;
  119. const u32 width = dst_params.width;
  120. const u32 height = dst_params.height;
  121. const u32 depth = dst_params.depth;
  122. const u32 block_height = dst_params.block_size.height;
  123. const u32 block_depth = dst_params.block_size.depth;
  124. const size_t dst_size =
  125. CalculateSize(true, bytes_per_pixel, width, height, depth, block_height, block_depth);
  126. const size_t dst_layer_size =
  127. CalculateSize(true, bytes_per_pixel, width, height, 1, block_height, block_depth);
  128. const size_t src_size = static_cast<size_t>(regs.pitch_in) * regs.line_count;
  129. if (read_buffer.size() < src_size) {
  130. read_buffer.resize(src_size);
  131. }
  132. if (write_buffer.size() < dst_size) {
  133. write_buffer.resize(dst_size);
  134. }
  135. if (Settings::IsGPULevelExtreme()) {
  136. memory_manager.ReadBlock(regs.offset_in, read_buffer.data(), src_size);
  137. memory_manager.ReadBlock(regs.offset_out, write_buffer.data(), dst_size);
  138. } else {
  139. memory_manager.ReadBlockUnsafe(regs.offset_in, read_buffer.data(), src_size);
  140. memory_manager.ReadBlockUnsafe(regs.offset_out, write_buffer.data(), dst_size);
  141. }
  142. // If the input is linear and the output is tiled, swizzle the input and copy it over.
  143. if (regs.dst_params.block_size.depth > 0) {
  144. ASSERT(dst_params.layer == 0);
  145. SwizzleSliceToVoxel(regs.line_length_in, regs.line_count, regs.pitch_in, width, height,
  146. bytes_per_pixel, block_height, block_depth, dst_params.origin.x,
  147. dst_params.origin.y, write_buffer.data(), read_buffer.data());
  148. } else {
  149. SwizzleSubrect(regs.line_length_in, regs.line_count, regs.pitch_in, width, bytes_per_pixel,
  150. write_buffer.data() + dst_layer_size * dst_params.layer, read_buffer.data(),
  151. block_height, dst_params.origin.x, dst_params.origin.y);
  152. }
  153. memory_manager.WriteBlock(regs.offset_out, write_buffer.data(), dst_size);
  154. }
  155. void MaxwellDMA::FastCopyBlockLinearToPitch() {
  156. const u32 bytes_per_pixel = regs.pitch_out / regs.line_length_in;
  157. const size_t src_size = GOB_SIZE;
  158. const size_t dst_size = static_cast<size_t>(regs.pitch_out) * regs.line_count;
  159. u32 pos_x = regs.src_params.origin.x;
  160. u32 pos_y = regs.src_params.origin.y;
  161. const u64 offset = GetGOBOffset(regs.src_params.width, regs.src_params.height, pos_x, pos_y,
  162. regs.src_params.block_size.height, bytes_per_pixel);
  163. const u32 x_in_gob = 64 / bytes_per_pixel;
  164. pos_x = pos_x % x_in_gob;
  165. pos_y = pos_y % 8;
  166. if (read_buffer.size() < src_size) {
  167. read_buffer.resize(src_size);
  168. }
  169. if (write_buffer.size() < dst_size) {
  170. write_buffer.resize(dst_size);
  171. }
  172. if (Settings::IsGPULevelExtreme()) {
  173. memory_manager.ReadBlock(regs.offset_in + offset, read_buffer.data(), src_size);
  174. memory_manager.ReadBlock(regs.offset_out, write_buffer.data(), dst_size);
  175. } else {
  176. memory_manager.ReadBlockUnsafe(regs.offset_in + offset, read_buffer.data(), src_size);
  177. memory_manager.ReadBlockUnsafe(regs.offset_out, write_buffer.data(), dst_size);
  178. }
  179. UnswizzleSubrect(regs.line_length_in, regs.line_count, regs.pitch_out, regs.src_params.width,
  180. bytes_per_pixel, read_buffer.data(), write_buffer.data(),
  181. regs.src_params.block_size.height, pos_x, pos_y);
  182. memory_manager.WriteBlock(regs.offset_out, write_buffer.data(), dst_size);
  183. }
  184. } // namespace Tegra::Engines