maxwell_dma.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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/polyfill_ranges.h"
  8. #include "common/settings.h"
  9. #include "core/core.h"
  10. #include "core/memory.h"
  11. #include "video_core/engines/maxwell_3d.h"
  12. #include "video_core/engines/maxwell_dma.h"
  13. #include "video_core/memory_manager.h"
  14. #include "video_core/renderer_base.h"
  15. #include "video_core/textures/decoders.h"
  16. MICROPROFILE_DECLARE(GPU_DMAEngine);
  17. MICROPROFILE_DECLARE(GPU_DMAEngineBL);
  18. MICROPROFILE_DECLARE(GPU_DMAEngineLB);
  19. MICROPROFILE_DECLARE(GPU_DMAEngineBB);
  20. MICROPROFILE_DEFINE(GPU_DMAEngine, "GPU", "DMA Engine", MP_RGB(224, 224, 128));
  21. MICROPROFILE_DEFINE(GPU_DMAEngineBL, "GPU", "DMA Engine Block - Linear", MP_RGB(224, 224, 128));
  22. MICROPROFILE_DEFINE(GPU_DMAEngineLB, "GPU", "DMA Engine Linear - Block", MP_RGB(224, 224, 128));
  23. MICROPROFILE_DEFINE(GPU_DMAEngineBB, "GPU", "DMA Engine Block - Block", MP_RGB(224, 224, 128));
  24. namespace Tegra::Engines {
  25. using namespace Texture;
  26. MaxwellDMA::MaxwellDMA(Core::System& system_, MemoryManager& memory_manager_)
  27. : system{system_}, memory_manager{memory_manager_} {
  28. execution_mask.reset();
  29. execution_mask[offsetof(Regs, launch_dma) / sizeof(u32)] = true;
  30. }
  31. MaxwellDMA::~MaxwellDMA() = default;
  32. void MaxwellDMA::BindRasterizer(VideoCore::RasterizerInterface* rasterizer_) {
  33. rasterizer = rasterizer_;
  34. }
  35. void MaxwellDMA::ConsumeSinkImpl() {
  36. for (auto [method, value] : method_sink) {
  37. regs.reg_array[method] = value;
  38. }
  39. method_sink.clear();
  40. }
  41. void MaxwellDMA::CallMethod(u32 method, u32 method_argument, bool is_last_call) {
  42. ASSERT_MSG(method < NUM_REGS, "Invalid MaxwellDMA register");
  43. regs.reg_array[method] = method_argument;
  44. if (method == offsetof(Regs, launch_dma) / sizeof(u32)) {
  45. Launch();
  46. }
  47. }
  48. void MaxwellDMA::CallMultiMethod(u32 method, const u32* base_start, u32 amount,
  49. u32 methods_pending) {
  50. for (u32 i = 0; i < amount; ++i) {
  51. CallMethod(method, base_start[i], methods_pending - i <= 1);
  52. }
  53. }
  54. void MaxwellDMA::Launch() {
  55. MICROPROFILE_SCOPE(GPU_DMAEngine);
  56. LOG_TRACE(Render_OpenGL, "DMA copy 0x{:x} -> 0x{:x}", static_cast<GPUVAddr>(regs.offset_in),
  57. static_cast<GPUVAddr>(regs.offset_out));
  58. // TODO(Subv): Perform more research and implement all features of this engine.
  59. const LaunchDMA& launch = regs.launch_dma;
  60. ASSERT(launch.interrupt_type == LaunchDMA::InterruptType::NONE);
  61. ASSERT(launch.data_transfer_type == LaunchDMA::DataTransferType::NON_PIPELINED);
  62. if (launch.multi_line_enable) {
  63. const bool is_src_pitch = launch.src_memory_layout == LaunchDMA::MemoryLayout::PITCH;
  64. const bool is_dst_pitch = launch.dst_memory_layout == LaunchDMA::MemoryLayout::PITCH;
  65. memory_manager.FlushCaching();
  66. if (!is_src_pitch && !is_dst_pitch) {
  67. // If both the source and the destination are in block layout, assert.
  68. MICROPROFILE_SCOPE(GPU_DMAEngineBB);
  69. CopyBlockLinearToBlockLinear();
  70. ReleaseSemaphore();
  71. return;
  72. }
  73. if (is_src_pitch && is_dst_pitch) {
  74. for (u32 line = 0; line < regs.line_count; ++line) {
  75. const GPUVAddr source_line =
  76. regs.offset_in + static_cast<size_t>(line) * regs.pitch_in;
  77. const GPUVAddr dest_line =
  78. regs.offset_out + static_cast<size_t>(line) * regs.pitch_out;
  79. memory_manager.CopyBlock(dest_line, source_line, regs.line_length_in);
  80. }
  81. } else {
  82. if (!is_src_pitch && is_dst_pitch) {
  83. MICROPROFILE_SCOPE(GPU_DMAEngineBL);
  84. CopyBlockLinearToPitch();
  85. } else {
  86. MICROPROFILE_SCOPE(GPU_DMAEngineLB);
  87. CopyPitchToBlockLinear();
  88. }
  89. }
  90. } else {
  91. // TODO: allow multisized components.
  92. auto& accelerate = rasterizer->AccessAccelerateDMA();
  93. const bool is_const_a_dst = regs.remap_const.dst_x == RemapConst::Swizzle::CONST_A;
  94. if (regs.launch_dma.remap_enable != 0 && is_const_a_dst) {
  95. ASSERT(regs.remap_const.component_size_minus_one == 3);
  96. accelerate.BufferClear(regs.offset_out, regs.line_length_in, regs.remap_consta_value);
  97. read_buffer.resize_destructive(regs.line_length_in * sizeof(u32));
  98. std::span<u32> span(reinterpret_cast<u32*>(read_buffer.data()), regs.line_length_in);
  99. std::ranges::fill(span, regs.remap_consta_value);
  100. memory_manager.WriteBlockUnsafe(regs.offset_out,
  101. reinterpret_cast<u8*>(read_buffer.data()),
  102. regs.line_length_in * sizeof(u32));
  103. } else {
  104. memory_manager.FlushCaching();
  105. const auto convert_linear_2_blocklinear_addr = [](u64 address) {
  106. return (address & ~0x1f0ULL) | ((address & 0x40) >> 2) | ((address & 0x10) << 1) |
  107. ((address & 0x180) >> 1) | ((address & 0x20) << 3);
  108. };
  109. const auto src_kind = memory_manager.GetPageKind(regs.offset_in);
  110. const auto dst_kind = memory_manager.GetPageKind(regs.offset_out);
  111. const bool is_src_pitch = IsPitchKind(src_kind);
  112. const bool is_dst_pitch = IsPitchKind(dst_kind);
  113. if (!is_src_pitch && is_dst_pitch) {
  114. UNIMPLEMENTED_IF(regs.line_length_in % 16 != 0);
  115. UNIMPLEMENTED_IF(regs.offset_in % 16 != 0);
  116. UNIMPLEMENTED_IF(regs.offset_out % 16 != 0);
  117. read_buffer.resize_destructive(16);
  118. for (u32 offset = 0; offset < regs.line_length_in; offset += 16) {
  119. Core::Memory::GpuGuestMemoryScoped<
  120. u8, Core::Memory::GuestMemoryFlags::SafeReadCachedWrite>
  121. tmp_write_buffer(memory_manager,
  122. convert_linear_2_blocklinear_addr(regs.offset_in + offset),
  123. 16, &read_buffer);
  124. tmp_write_buffer.SetAddressAndSize(regs.offset_out + offset, 16);
  125. }
  126. } else if (is_src_pitch && !is_dst_pitch) {
  127. UNIMPLEMENTED_IF(regs.line_length_in % 16 != 0);
  128. UNIMPLEMENTED_IF(regs.offset_in % 16 != 0);
  129. UNIMPLEMENTED_IF(regs.offset_out % 16 != 0);
  130. read_buffer.resize_destructive(16);
  131. for (u32 offset = 0; offset < regs.line_length_in; offset += 16) {
  132. Core::Memory::GpuGuestMemoryScoped<
  133. u8, Core::Memory::GuestMemoryFlags::SafeReadCachedWrite>
  134. tmp_write_buffer(memory_manager, regs.offset_in + offset, 16, &read_buffer);
  135. tmp_write_buffer.SetAddressAndSize(
  136. convert_linear_2_blocklinear_addr(regs.offset_out + offset), 16);
  137. }
  138. } else {
  139. if (!accelerate.BufferCopy(regs.offset_in, regs.offset_out, regs.line_length_in)) {
  140. Core::Memory::GpuGuestMemoryScoped<
  141. u8, Core::Memory::GuestMemoryFlags::SafeReadCachedWrite>
  142. tmp_write_buffer(memory_manager, regs.offset_in, regs.line_length_in,
  143. &read_buffer);
  144. tmp_write_buffer.SetAddressAndSize(regs.offset_out, regs.line_length_in);
  145. }
  146. }
  147. }
  148. }
  149. ReleaseSemaphore();
  150. }
  151. void MaxwellDMA::CopyBlockLinearToPitch() {
  152. UNIMPLEMENTED_IF(regs.launch_dma.remap_enable != 0);
  153. u32 bytes_per_pixel = 1;
  154. DMA::ImageOperand src_operand;
  155. src_operand.bytes_per_pixel = bytes_per_pixel;
  156. src_operand.params = regs.src_params;
  157. src_operand.address = regs.offset_in;
  158. DMA::BufferOperand dst_operand;
  159. dst_operand.pitch = static_cast<u32>(std::abs(regs.pitch_out));
  160. dst_operand.width = regs.line_length_in;
  161. dst_operand.height = regs.line_count;
  162. dst_operand.address = regs.offset_out;
  163. DMA::ImageCopy copy_info{};
  164. copy_info.length_x = regs.line_length_in;
  165. copy_info.length_y = regs.line_count;
  166. auto& accelerate = rasterizer->AccessAccelerateDMA();
  167. if (accelerate.ImageToBuffer(copy_info, src_operand, dst_operand)) {
  168. return;
  169. }
  170. UNIMPLEMENTED_IF(regs.src_params.block_size.width != 0);
  171. UNIMPLEMENTED_IF(regs.src_params.block_size.depth != 0);
  172. UNIMPLEMENTED_IF(regs.src_params.block_size.depth == 0 && regs.src_params.depth != 1);
  173. // Deswizzle the input and copy it over.
  174. const DMA::Parameters& src_params = regs.src_params;
  175. const bool is_remapping = regs.launch_dma.remap_enable != 0;
  176. const u32 num_remap_components = regs.remap_const.num_dst_components_minus_one + 1;
  177. const u32 remap_components_size = regs.remap_const.component_size_minus_one + 1;
  178. const u32 base_bpp = !is_remapping ? 1U : num_remap_components * remap_components_size;
  179. u32 width = src_params.width;
  180. u32 x_elements = regs.line_length_in;
  181. u32 x_offset = src_params.origin.x;
  182. u32 bpp_shift = 0U;
  183. if (!is_remapping) {
  184. bpp_shift = Common::FoldRight(
  185. 4U, [](u32 x, u32 y) { return std::min(x, static_cast<u32>(std::countr_zero(y))); },
  186. width, x_elements, x_offset, static_cast<u32>(regs.offset_in));
  187. width >>= bpp_shift;
  188. x_elements >>= bpp_shift;
  189. x_offset >>= bpp_shift;
  190. }
  191. bytes_per_pixel = base_bpp << bpp_shift;
  192. const u32 height = src_params.height;
  193. const u32 depth = src_params.depth;
  194. const u32 block_height = src_params.block_size.height;
  195. const u32 block_depth = src_params.block_size.depth;
  196. const size_t src_size =
  197. CalculateSize(true, bytes_per_pixel, width, height, depth, block_height, block_depth);
  198. const size_t dst_size = dst_operand.pitch * regs.line_count;
  199. Core::Memory::GpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead> tmp_read_buffer(
  200. memory_manager, src_operand.address, src_size, &read_buffer);
  201. Core::Memory::GpuGuestMemoryScoped<u8, Core::Memory::GuestMemoryFlags::SafeReadCachedWrite>
  202. tmp_write_buffer(memory_manager, dst_operand.address, dst_size, &write_buffer);
  203. UnswizzleSubrect(tmp_write_buffer, tmp_read_buffer, bytes_per_pixel, width, height, depth,
  204. x_offset, src_params.origin.y, x_elements, regs.line_count, block_height,
  205. block_depth, dst_operand.pitch);
  206. }
  207. void MaxwellDMA::CopyPitchToBlockLinear() {
  208. UNIMPLEMENTED_IF_MSG(regs.dst_params.block_size.width != 0, "Block width is not one");
  209. UNIMPLEMENTED_IF(regs.dst_params.layer != 0);
  210. const bool is_remapping = regs.launch_dma.remap_enable != 0;
  211. const u32 num_remap_components = regs.remap_const.num_dst_components_minus_one + 1;
  212. const u32 remap_components_size = regs.remap_const.component_size_minus_one + 1;
  213. u32 bytes_per_pixel = 1;
  214. DMA::ImageOperand dst_operand;
  215. dst_operand.bytes_per_pixel = bytes_per_pixel;
  216. dst_operand.params = regs.dst_params;
  217. dst_operand.address = regs.offset_out;
  218. DMA::BufferOperand src_operand;
  219. src_operand.pitch = regs.pitch_in;
  220. src_operand.width = regs.line_length_in;
  221. src_operand.height = regs.line_count;
  222. src_operand.address = regs.offset_in;
  223. DMA::ImageCopy copy_info{};
  224. copy_info.length_x = regs.line_length_in;
  225. copy_info.length_y = regs.line_count;
  226. auto& accelerate = rasterizer->AccessAccelerateDMA();
  227. if (accelerate.BufferToImage(copy_info, src_operand, dst_operand)) {
  228. return;
  229. }
  230. const auto& dst_params = regs.dst_params;
  231. const u32 base_bpp = !is_remapping ? 1U : num_remap_components * remap_components_size;
  232. u32 width = dst_params.width;
  233. u32 x_elements = regs.line_length_in;
  234. u32 x_offset = dst_params.origin.x;
  235. u32 bpp_shift = 0U;
  236. if (!is_remapping) {
  237. bpp_shift = Common::FoldRight(
  238. 4U, [](u32 x, u32 y) { return std::min(x, static_cast<u32>(std::countr_zero(y))); },
  239. width, x_elements, x_offset, static_cast<u32>(regs.offset_out));
  240. width >>= bpp_shift;
  241. x_elements >>= bpp_shift;
  242. x_offset >>= bpp_shift;
  243. }
  244. bytes_per_pixel = base_bpp << bpp_shift;
  245. const u32 height = dst_params.height;
  246. const u32 depth = dst_params.depth;
  247. const u32 block_height = dst_params.block_size.height;
  248. const u32 block_depth = dst_params.block_size.depth;
  249. const size_t dst_size =
  250. CalculateSize(true, bytes_per_pixel, width, height, depth, block_height, block_depth);
  251. const size_t src_size = static_cast<size_t>(regs.pitch_in) * regs.line_count;
  252. GPUVAddr src_addr = regs.offset_in;
  253. GPUVAddr dst_addr = regs.offset_out;
  254. Core::Memory::GpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead> tmp_read_buffer(
  255. memory_manager, src_addr, src_size, &read_buffer);
  256. Core::Memory::GpuGuestMemoryScoped<u8, Core::Memory::GuestMemoryFlags::SafeReadCachedWrite>
  257. tmp_write_buffer(memory_manager, dst_addr, dst_size, &write_buffer);
  258. // If the input is linear and the output is tiled, swizzle the input and copy it over.
  259. SwizzleSubrect(tmp_write_buffer, tmp_read_buffer, bytes_per_pixel, width, height, depth,
  260. x_offset, dst_params.origin.y, x_elements, regs.line_count, block_height,
  261. block_depth, regs.pitch_in);
  262. }
  263. void MaxwellDMA::CopyBlockLinearToBlockLinear() {
  264. UNIMPLEMENTED_IF(regs.src_params.block_size.width != 0);
  265. const bool is_remapping = regs.launch_dma.remap_enable != 0;
  266. // Deswizzle the input and copy it over.
  267. const DMA::Parameters& src = regs.src_params;
  268. const DMA::Parameters& dst = regs.dst_params;
  269. const u32 num_remap_components = regs.remap_const.num_dst_components_minus_one + 1;
  270. const u32 remap_components_size = regs.remap_const.component_size_minus_one + 1;
  271. const u32 base_bpp = !is_remapping ? 1U : num_remap_components * remap_components_size;
  272. u32 src_width = src.width;
  273. u32 dst_width = dst.width;
  274. u32 x_elements = regs.line_length_in;
  275. u32 src_x_offset = src.origin.x;
  276. u32 dst_x_offset = dst.origin.x;
  277. u32 bpp_shift = 0U;
  278. if (!is_remapping) {
  279. bpp_shift = Common::FoldRight(
  280. 4U, [](u32 x, u32 y) { return std::min(x, static_cast<u32>(std::countr_zero(y))); },
  281. src_width, dst_width, x_elements, src_x_offset, dst_x_offset,
  282. static_cast<u32>(regs.offset_in), static_cast<u32>(regs.offset_out));
  283. src_width >>= bpp_shift;
  284. dst_width >>= bpp_shift;
  285. x_elements >>= bpp_shift;
  286. src_x_offset >>= bpp_shift;
  287. dst_x_offset >>= bpp_shift;
  288. }
  289. const u32 bytes_per_pixel = base_bpp << bpp_shift;
  290. const size_t src_size = CalculateSize(true, bytes_per_pixel, src_width, src.height, src.depth,
  291. src.block_size.height, src.block_size.depth);
  292. const size_t dst_size = CalculateSize(true, bytes_per_pixel, dst_width, dst.height, dst.depth,
  293. dst.block_size.height, dst.block_size.depth);
  294. const u32 pitch = x_elements * bytes_per_pixel;
  295. const size_t mid_buffer_size = pitch * regs.line_count;
  296. intermediate_buffer.resize_destructive(mid_buffer_size);
  297. Core::Memory::GpuGuestMemory<u8, Core::Memory::GuestMemoryFlags::SafeRead> tmp_read_buffer(
  298. memory_manager, regs.offset_in, src_size, &read_buffer);
  299. Core::Memory::GpuGuestMemoryScoped<u8, Core::Memory::GuestMemoryFlags::SafeReadCachedWrite>
  300. tmp_write_buffer(memory_manager, regs.offset_out, dst_size, &write_buffer);
  301. UnswizzleSubrect(intermediate_buffer, tmp_read_buffer, bytes_per_pixel, src_width, src.height,
  302. src.depth, src_x_offset, src.origin.y, x_elements, regs.line_count,
  303. src.block_size.height, src.block_size.depth, pitch);
  304. SwizzleSubrect(tmp_write_buffer, intermediate_buffer, bytes_per_pixel, dst_width, dst.height,
  305. dst.depth, dst_x_offset, dst.origin.y, x_elements, regs.line_count,
  306. dst.block_size.height, dst.block_size.depth, pitch);
  307. }
  308. void MaxwellDMA::ReleaseSemaphore() {
  309. const auto type = regs.launch_dma.semaphore_type;
  310. const GPUVAddr address = regs.semaphore.address;
  311. const u32 payload = regs.semaphore.payload;
  312. switch (type) {
  313. case LaunchDMA::SemaphoreType::NONE:
  314. break;
  315. case LaunchDMA::SemaphoreType::RELEASE_ONE_WORD_SEMAPHORE: {
  316. std::function<void()> operation(
  317. [this, address, payload] { memory_manager.Write<u32>(address, payload); });
  318. rasterizer->SignalFence(std::move(operation));
  319. break;
  320. }
  321. case LaunchDMA::SemaphoreType::RELEASE_FOUR_WORD_SEMAPHORE: {
  322. std::function<void()> operation([this, address, payload] {
  323. memory_manager.Write<u64>(address + sizeof(u64), system.GPU().GetTicks());
  324. memory_manager.Write<u64>(address, payload);
  325. });
  326. rasterizer->SignalFence(std::move(operation));
  327. break;
  328. }
  329. default:
  330. ASSERT_MSG(false, "Unknown semaphore type: {}", static_cast<u32>(type.Value()));
  331. break;
  332. }
  333. }
  334. } // namespace Tegra::Engines