maxwell_dma.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/algorithm.h"
  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 (u32 i = 0; i < amount; ++i) {
  34. CallMethod(method, base_start[i], methods_pending - 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.interrupt_type == LaunchDMA::InterruptType::NONE);
  44. ASSERT(launch.data_transfer_type == LaunchDMA::DataTransferType::NON_PIPELINED);
  45. if (launch.multi_line_enable) {
  46. const bool is_src_pitch = launch.src_memory_layout == LaunchDMA::MemoryLayout::PITCH;
  47. const bool is_dst_pitch = launch.dst_memory_layout == LaunchDMA::MemoryLayout::PITCH;
  48. if (!is_src_pitch && !is_dst_pitch) {
  49. // If both the source and the destination are in block layout, assert.
  50. CopyBlockLinearToBlockLinear();
  51. ReleaseSemaphore();
  52. return;
  53. }
  54. if (is_src_pitch && is_dst_pitch) {
  55. for (u32 line = 0; line < regs.line_count; ++line) {
  56. const GPUVAddr source_line =
  57. regs.offset_in + static_cast<size_t>(line) * regs.pitch_in;
  58. const GPUVAddr dest_line =
  59. regs.offset_out + static_cast<size_t>(line) * regs.pitch_out;
  60. memory_manager.CopyBlock(dest_line, source_line, regs.line_length_in);
  61. }
  62. } else {
  63. if (!is_src_pitch && is_dst_pitch) {
  64. CopyBlockLinearToPitch();
  65. } else {
  66. CopyPitchToBlockLinear();
  67. }
  68. }
  69. } else {
  70. // TODO: allow multisized components.
  71. auto& accelerate = rasterizer->AccessAccelerateDMA();
  72. const bool is_const_a_dst = regs.remap_const.dst_x == RemapConst::Swizzle::CONST_A;
  73. if (regs.launch_dma.remap_enable != 0 && is_const_a_dst) {
  74. ASSERT(regs.remap_const.component_size_minus_one == 3);
  75. accelerate.BufferClear(regs.offset_out, regs.line_length_in, regs.remap_consta_value);
  76. std::vector<u32> tmp_buffer(regs.line_length_in, regs.remap_consta_value);
  77. memory_manager.WriteBlockUnsafe(regs.offset_out,
  78. reinterpret_cast<u8*>(tmp_buffer.data()),
  79. regs.line_length_in * sizeof(u32));
  80. } else {
  81. const auto convert_linear_2_blocklinear_addr = [](u64 address) {
  82. return (address & ~0x1f0ULL) | ((address & 0x40) >> 2) | ((address & 0x10) << 1) |
  83. ((address & 0x180) >> 1) | ((address & 0x20) << 3);
  84. };
  85. const auto src_kind = memory_manager.GetPageKind(regs.offset_in);
  86. const auto dst_kind = memory_manager.GetPageKind(regs.offset_out);
  87. const bool is_src_pitch = IsPitchKind(src_kind);
  88. const bool is_dst_pitch = IsPitchKind(dst_kind);
  89. if (!is_src_pitch && is_dst_pitch) {
  90. UNIMPLEMENTED_IF(regs.line_length_in % 16 != 0);
  91. UNIMPLEMENTED_IF(regs.offset_in % 16 != 0);
  92. UNIMPLEMENTED_IF(regs.offset_out % 16 != 0);
  93. std::vector<u8> tmp_buffer(16);
  94. for (u32 offset = 0; offset < regs.line_length_in; offset += 16) {
  95. memory_manager.ReadBlockUnsafe(
  96. convert_linear_2_blocklinear_addr(regs.offset_in + offset),
  97. tmp_buffer.data(), tmp_buffer.size());
  98. memory_manager.WriteBlock(regs.offset_out + offset, tmp_buffer.data(),
  99. tmp_buffer.size());
  100. }
  101. } else if (is_src_pitch && !is_dst_pitch) {
  102. UNIMPLEMENTED_IF(regs.line_length_in % 16 != 0);
  103. UNIMPLEMENTED_IF(regs.offset_in % 16 != 0);
  104. UNIMPLEMENTED_IF(regs.offset_out % 16 != 0);
  105. std::vector<u8> tmp_buffer(16);
  106. for (u32 offset = 0; offset < regs.line_length_in; offset += 16) {
  107. memory_manager.ReadBlockUnsafe(regs.offset_in + offset, tmp_buffer.data(),
  108. tmp_buffer.size());
  109. memory_manager.WriteBlock(
  110. convert_linear_2_blocklinear_addr(regs.offset_out + offset),
  111. tmp_buffer.data(), tmp_buffer.size());
  112. }
  113. } else {
  114. if (!accelerate.BufferCopy(regs.offset_in, regs.offset_out, regs.line_length_in)) {
  115. std::vector<u8> tmp_buffer(regs.line_length_in);
  116. memory_manager.ReadBlockUnsafe(regs.offset_in, tmp_buffer.data(),
  117. regs.line_length_in);
  118. memory_manager.WriteBlock(regs.offset_out, tmp_buffer.data(),
  119. regs.line_length_in);
  120. }
  121. }
  122. }
  123. }
  124. ReleaseSemaphore();
  125. }
  126. void MaxwellDMA::CopyBlockLinearToPitch() {
  127. UNIMPLEMENTED_IF(regs.src_params.block_size.width != 0);
  128. UNIMPLEMENTED_IF(regs.src_params.layer != 0);
  129. const bool is_remapping = regs.launch_dma.remap_enable != 0;
  130. // Optimized path for micro copies.
  131. const size_t dst_size = static_cast<size_t>(regs.pitch_out) * regs.line_count;
  132. if (!is_remapping && dst_size < GOB_SIZE && regs.pitch_out <= GOB_SIZE_X &&
  133. regs.src_params.height > GOB_SIZE_Y) {
  134. FastCopyBlockLinearToPitch();
  135. return;
  136. }
  137. // Deswizzle the input and copy it over.
  138. const Parameters& src_params = regs.src_params;
  139. const u32 num_remap_components = regs.remap_const.num_dst_components_minus_one + 1;
  140. const u32 remap_components_size = regs.remap_const.component_size_minus_one + 1;
  141. const u32 base_bpp = !is_remapping ? 1U : num_remap_components * remap_components_size;
  142. u32 width = src_params.width;
  143. u32 x_elements = regs.line_length_in;
  144. u32 x_offset = src_params.origin.x;
  145. u32 bpp_shift = 0U;
  146. if (!is_remapping) {
  147. bpp_shift = Common::FoldRight(
  148. 4U, [](u32 x, u32 y) { return std::min(x, static_cast<u32>(std::countr_zero(y))); },
  149. width, x_elements, x_offset, static_cast<u32>(regs.offset_in));
  150. width >>= bpp_shift;
  151. x_elements >>= bpp_shift;
  152. x_offset >>= bpp_shift;
  153. }
  154. const u32 bytes_per_pixel = base_bpp << bpp_shift;
  155. const u32 height = src_params.height;
  156. const u32 depth = src_params.depth;
  157. const u32 block_height = src_params.block_size.height;
  158. const u32 block_depth = src_params.block_size.depth;
  159. const size_t src_size =
  160. CalculateSize(true, bytes_per_pixel, width, height, depth, block_height, block_depth);
  161. read_buffer.resize_destructive(src_size);
  162. write_buffer.resize_destructive(dst_size);
  163. memory_manager.ReadBlock(regs.offset_in, read_buffer.data(), src_size);
  164. memory_manager.ReadBlock(regs.offset_out, write_buffer.data(), dst_size);
  165. UnswizzleSubrect(write_buffer, read_buffer, bytes_per_pixel, width, height, depth, x_offset,
  166. src_params.origin.y, x_elements, regs.line_count, block_height, block_depth,
  167. regs.pitch_out);
  168. memory_manager.WriteBlock(regs.offset_out, write_buffer.data(), dst_size);
  169. }
  170. void MaxwellDMA::CopyPitchToBlockLinear() {
  171. UNIMPLEMENTED_IF_MSG(regs.dst_params.block_size.width != 0, "Block width is not one");
  172. UNIMPLEMENTED_IF(regs.dst_params.layer != 0);
  173. const bool is_remapping = regs.launch_dma.remap_enable != 0;
  174. const u32 num_remap_components = regs.remap_const.num_dst_components_minus_one + 1;
  175. const u32 remap_components_size = regs.remap_const.component_size_minus_one + 1;
  176. const auto& dst_params = regs.dst_params;
  177. const u32 base_bpp = !is_remapping ? 1U : num_remap_components * remap_components_size;
  178. u32 width = dst_params.width;
  179. u32 x_elements = regs.line_length_in;
  180. u32 x_offset = dst_params.origin.x;
  181. u32 bpp_shift = 0U;
  182. if (!is_remapping) {
  183. bpp_shift = Common::FoldRight(
  184. 4U, [](u32 x, u32 y) { return std::min(x, static_cast<u32>(std::countr_zero(y))); },
  185. width, x_elements, x_offset, static_cast<u32>(regs.offset_out));
  186. width >>= bpp_shift;
  187. x_elements >>= bpp_shift;
  188. x_offset >>= bpp_shift;
  189. }
  190. const u32 bytes_per_pixel = base_bpp << bpp_shift;
  191. const u32 height = dst_params.height;
  192. const u32 depth = dst_params.depth;
  193. const u32 block_height = dst_params.block_size.height;
  194. const u32 block_depth = dst_params.block_size.depth;
  195. const size_t dst_size =
  196. CalculateSize(true, bytes_per_pixel, width, height, depth, block_height, block_depth);
  197. const size_t src_size = static_cast<size_t>(regs.pitch_in) * regs.line_count;
  198. read_buffer.resize_destructive(src_size);
  199. write_buffer.resize_destructive(dst_size);
  200. memory_manager.ReadBlock(regs.offset_in, read_buffer.data(), src_size);
  201. if (Settings::IsGPULevelExtreme()) {
  202. memory_manager.ReadBlock(regs.offset_out, write_buffer.data(), dst_size);
  203. } else {
  204. memory_manager.ReadBlockUnsafe(regs.offset_out, write_buffer.data(), dst_size);
  205. }
  206. // If the input is linear and the output is tiled, swizzle the input and copy it over.
  207. SwizzleSubrect(write_buffer, read_buffer, bytes_per_pixel, width, height, depth, x_offset,
  208. dst_params.origin.y, x_elements, regs.line_count, block_height, block_depth,
  209. regs.pitch_in);
  210. memory_manager.WriteBlock(regs.offset_out, write_buffer.data(), dst_size);
  211. }
  212. void MaxwellDMA::FastCopyBlockLinearToPitch() {
  213. const u32 bytes_per_pixel = 1U;
  214. const size_t src_size = GOB_SIZE;
  215. const size_t dst_size = static_cast<size_t>(regs.pitch_out) * regs.line_count;
  216. u32 pos_x = regs.src_params.origin.x;
  217. u32 pos_y = regs.src_params.origin.y;
  218. const u64 offset = GetGOBOffset(regs.src_params.width, regs.src_params.height, pos_x, pos_y,
  219. regs.src_params.block_size.height, bytes_per_pixel);
  220. const u32 x_in_gob = 64 / bytes_per_pixel;
  221. pos_x = pos_x % x_in_gob;
  222. pos_y = pos_y % 8;
  223. read_buffer.resize_destructive(src_size);
  224. write_buffer.resize_destructive(dst_size);
  225. if (Settings::IsGPULevelExtreme()) {
  226. memory_manager.ReadBlock(regs.offset_in + offset, read_buffer.data(), src_size);
  227. memory_manager.ReadBlock(regs.offset_out, write_buffer.data(), dst_size);
  228. } else {
  229. memory_manager.ReadBlockUnsafe(regs.offset_in + offset, read_buffer.data(), src_size);
  230. memory_manager.ReadBlockUnsafe(regs.offset_out, write_buffer.data(), dst_size);
  231. }
  232. UnswizzleSubrect(write_buffer, read_buffer, bytes_per_pixel, regs.src_params.width,
  233. regs.src_params.height, 1, pos_x, pos_y, regs.line_length_in, regs.line_count,
  234. regs.src_params.block_size.height, regs.src_params.block_size.depth,
  235. regs.pitch_out);
  236. memory_manager.WriteBlock(regs.offset_out, write_buffer.data(), dst_size);
  237. }
  238. void MaxwellDMA::CopyBlockLinearToBlockLinear() {
  239. UNIMPLEMENTED_IF(regs.src_params.block_size.width != 0);
  240. const bool is_remapping = regs.launch_dma.remap_enable != 0;
  241. // Deswizzle the input and copy it over.
  242. const Parameters& src = regs.src_params;
  243. const Parameters& dst = regs.dst_params;
  244. const u32 num_remap_components = regs.remap_const.num_dst_components_minus_one + 1;
  245. const u32 remap_components_size = regs.remap_const.component_size_minus_one + 1;
  246. const u32 base_bpp = !is_remapping ? 1U : num_remap_components * remap_components_size;
  247. u32 src_width = src.width;
  248. u32 dst_width = dst.width;
  249. u32 x_elements = regs.line_length_in;
  250. u32 src_x_offset = src.origin.x;
  251. u32 dst_x_offset = dst.origin.x;
  252. u32 bpp_shift = 0U;
  253. if (!is_remapping) {
  254. bpp_shift = Common::FoldRight(
  255. 4U, [](u32 x, u32 y) { return std::min(x, static_cast<u32>(std::countr_zero(y))); },
  256. src_width, dst_width, x_elements, src_x_offset, dst_x_offset,
  257. static_cast<u32>(regs.offset_in), static_cast<u32>(regs.offset_out));
  258. src_width >>= bpp_shift;
  259. dst_width >>= bpp_shift;
  260. x_elements >>= bpp_shift;
  261. src_x_offset >>= bpp_shift;
  262. dst_x_offset >>= bpp_shift;
  263. }
  264. const u32 bytes_per_pixel = base_bpp << bpp_shift;
  265. const size_t src_size = CalculateSize(true, bytes_per_pixel, src_width, src.height, src.depth,
  266. src.block_size.height, src.block_size.depth);
  267. const size_t dst_size = CalculateSize(true, bytes_per_pixel, dst_width, dst.height, dst.depth,
  268. dst.block_size.height, dst.block_size.depth);
  269. const u32 pitch = x_elements * bytes_per_pixel;
  270. const size_t mid_buffer_size = pitch * regs.line_count;
  271. read_buffer.resize_destructive(src_size);
  272. write_buffer.resize_destructive(dst_size);
  273. intermediate_buffer.resize_destructive(mid_buffer_size);
  274. memory_manager.ReadBlock(regs.offset_in, read_buffer.data(), src_size);
  275. memory_manager.ReadBlock(regs.offset_out, write_buffer.data(), dst_size);
  276. UnswizzleSubrect(intermediate_buffer, read_buffer, bytes_per_pixel, src_width, src.height,
  277. src.depth, src_x_offset, src.origin.y, x_elements, regs.line_count,
  278. src.block_size.height, src.block_size.depth, pitch);
  279. SwizzleSubrect(write_buffer, intermediate_buffer, bytes_per_pixel, dst_width, dst.height,
  280. dst.depth, dst_x_offset, dst.origin.y, x_elements, regs.line_count,
  281. dst.block_size.height, dst.block_size.depth, pitch);
  282. memory_manager.WriteBlock(regs.offset_out, write_buffer.data(), dst_size);
  283. }
  284. void MaxwellDMA::ReleaseSemaphore() {
  285. const auto type = regs.launch_dma.semaphore_type;
  286. const GPUVAddr address = regs.semaphore.address;
  287. const u32 payload = regs.semaphore.payload;
  288. switch (type) {
  289. case LaunchDMA::SemaphoreType::NONE:
  290. break;
  291. case LaunchDMA::SemaphoreType::RELEASE_ONE_WORD_SEMAPHORE: {
  292. std::function<void()> operation(
  293. [this, address, payload] { memory_manager.Write<u32>(address, payload); });
  294. rasterizer->SignalFence(std::move(operation));
  295. break;
  296. }
  297. case LaunchDMA::SemaphoreType::RELEASE_FOUR_WORD_SEMAPHORE: {
  298. std::function<void()> operation([this, address, payload] {
  299. memory_manager.Write<u64>(address + sizeof(u64), system.GPU().GetTicks());
  300. memory_manager.Write<u64>(address, payload);
  301. });
  302. rasterizer->SignalFence(std::move(operation));
  303. break;
  304. }
  305. default:
  306. ASSERT_MSG(false, "Unknown semaphore type: {}", static_cast<u32>(type.Value()));
  307. break;
  308. }
  309. }
  310. } // namespace Tegra::Engines