maxwell_dma.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 "video_core/engines/maxwell_3d.h"
  11. #include "video_core/engines/maxwell_dma.h"
  12. #include "video_core/guest_memory.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,
  97. regs.remap_const.remap_consta_value);
  98. read_buffer.resize_destructive(regs.line_length_in * sizeof(u32));
  99. std::span<u32> span(reinterpret_cast<u32*>(read_buffer.data()), regs.line_length_in);
  100. std::ranges::fill(span, regs.remap_const.remap_consta_value);
  101. memory_manager.WriteBlockUnsafe(regs.offset_out,
  102. reinterpret_cast<u8*>(read_buffer.data()),
  103. regs.line_length_in * sizeof(u32));
  104. } else {
  105. memory_manager.FlushCaching();
  106. const auto convert_linear_2_blocklinear_addr = [](u64 address) {
  107. return (address & ~0x1f0ULL) | ((address & 0x40) >> 2) | ((address & 0x10) << 1) |
  108. ((address & 0x180) >> 1) | ((address & 0x20) << 3);
  109. };
  110. const auto src_kind = memory_manager.GetPageKind(regs.offset_in);
  111. const auto dst_kind = memory_manager.GetPageKind(regs.offset_out);
  112. const bool is_src_pitch = IsPitchKind(src_kind);
  113. const bool is_dst_pitch = IsPitchKind(dst_kind);
  114. if (!is_src_pitch && is_dst_pitch) {
  115. UNIMPLEMENTED_IF(regs.line_length_in % 16 != 0);
  116. UNIMPLEMENTED_IF(regs.offset_in % 16 != 0);
  117. UNIMPLEMENTED_IF(regs.offset_out % 16 != 0);
  118. read_buffer.resize_destructive(16);
  119. for (u32 offset = 0; offset < regs.line_length_in; offset += 16) {
  120. Tegra::Memory::GpuGuestMemoryScoped<
  121. u8, Tegra::Memory::GuestMemoryFlags::SafeReadCachedWrite>
  122. tmp_write_buffer(memory_manager,
  123. convert_linear_2_blocklinear_addr(regs.offset_in + offset),
  124. 16, &read_buffer);
  125. tmp_write_buffer.SetAddressAndSize(regs.offset_out + offset, 16);
  126. }
  127. } else if (is_src_pitch && !is_dst_pitch) {
  128. UNIMPLEMENTED_IF(regs.line_length_in % 16 != 0);
  129. UNIMPLEMENTED_IF(regs.offset_in % 16 != 0);
  130. UNIMPLEMENTED_IF(regs.offset_out % 16 != 0);
  131. read_buffer.resize_destructive(16);
  132. for (u32 offset = 0; offset < regs.line_length_in; offset += 16) {
  133. Tegra::Memory::GpuGuestMemoryScoped<
  134. u8, Tegra::Memory::GuestMemoryFlags::SafeReadCachedWrite>
  135. tmp_write_buffer(memory_manager, regs.offset_in + offset, 16, &read_buffer);
  136. tmp_write_buffer.SetAddressAndSize(
  137. convert_linear_2_blocklinear_addr(regs.offset_out + offset), 16);
  138. }
  139. } else {
  140. if (!accelerate.BufferCopy(regs.offset_in, regs.offset_out, regs.line_length_in)) {
  141. Tegra::Memory::GpuGuestMemoryScoped<
  142. u8, Tegra::Memory::GuestMemoryFlags::SafeReadCachedWrite>
  143. tmp_write_buffer(memory_manager, regs.offset_in, regs.line_length_in,
  144. &read_buffer);
  145. tmp_write_buffer.SetAddressAndSize(regs.offset_out, regs.line_length_in);
  146. }
  147. }
  148. }
  149. }
  150. ReleaseSemaphore();
  151. }
  152. void MaxwellDMA::CopyBlockLinearToPitch() {
  153. UNIMPLEMENTED_IF(regs.launch_dma.remap_enable != 0);
  154. u32 bytes_per_pixel = 1;
  155. DMA::ImageOperand src_operand;
  156. src_operand.bytes_per_pixel = bytes_per_pixel;
  157. src_operand.params = regs.src_params;
  158. src_operand.address = regs.offset_in;
  159. DMA::BufferOperand dst_operand;
  160. dst_operand.pitch = static_cast<u32>(std::abs(regs.pitch_out));
  161. dst_operand.width = regs.line_length_in;
  162. dst_operand.height = regs.line_count;
  163. dst_operand.address = regs.offset_out;
  164. DMA::ImageCopy copy_info{};
  165. copy_info.length_x = regs.line_length_in;
  166. copy_info.length_y = regs.line_count;
  167. auto& accelerate = rasterizer->AccessAccelerateDMA();
  168. if (accelerate.ImageToBuffer(copy_info, src_operand, dst_operand)) {
  169. return;
  170. }
  171. UNIMPLEMENTED_IF(regs.src_params.block_size.width != 0);
  172. UNIMPLEMENTED_IF(regs.src_params.block_size.depth != 0);
  173. UNIMPLEMENTED_IF(regs.src_params.block_size.depth == 0 && regs.src_params.depth != 1);
  174. // Deswizzle the input and copy it over.
  175. const DMA::Parameters& src_params = regs.src_params;
  176. const bool is_remapping = regs.launch_dma.remap_enable != 0;
  177. const u32 num_remap_components = regs.remap_const.num_dst_components_minus_one + 1;
  178. const u32 remap_components_size = regs.remap_const.component_size_minus_one + 1;
  179. const u32 base_bpp = !is_remapping ? 1U : num_remap_components * remap_components_size;
  180. u32 width = src_params.width;
  181. u32 x_elements = regs.line_length_in;
  182. u32 x_offset = src_params.origin.x;
  183. u32 bpp_shift = 0U;
  184. if (!is_remapping) {
  185. bpp_shift = Common::FoldRight(
  186. 4U, [](u32 x, u32 y) { return std::min(x, static_cast<u32>(std::countr_zero(y))); },
  187. width, x_elements, x_offset, static_cast<u32>(regs.offset_in));
  188. width >>= bpp_shift;
  189. x_elements >>= bpp_shift;
  190. x_offset >>= bpp_shift;
  191. }
  192. bytes_per_pixel = base_bpp << bpp_shift;
  193. const u32 height = src_params.height;
  194. const u32 depth = src_params.depth;
  195. const u32 block_height = src_params.block_size.height;
  196. const u32 block_depth = src_params.block_size.depth;
  197. const size_t src_size =
  198. CalculateSize(true, bytes_per_pixel, width, height, depth, block_height, block_depth);
  199. const size_t dst_size = dst_operand.pitch * regs.line_count;
  200. Tegra::Memory::GpuGuestMemory<u8, Tegra::Memory::GuestMemoryFlags::SafeRead> tmp_read_buffer(
  201. memory_manager, src_operand.address, src_size, &read_buffer);
  202. Tegra::Memory::GpuGuestMemoryScoped<u8, Tegra::Memory::GuestMemoryFlags::UnsafeReadCachedWrite>
  203. tmp_write_buffer(memory_manager, dst_operand.address, dst_size, &write_buffer);
  204. UnswizzleSubrect(tmp_write_buffer, tmp_read_buffer, bytes_per_pixel, width, height, depth,
  205. x_offset, src_params.origin.y, x_elements, regs.line_count, block_height,
  206. block_depth, dst_operand.pitch);
  207. }
  208. void MaxwellDMA::CopyPitchToBlockLinear() {
  209. UNIMPLEMENTED_IF_MSG(regs.dst_params.block_size.width != 0, "Block width is not one");
  210. UNIMPLEMENTED_IF(regs.dst_params.layer != 0);
  211. const bool is_remapping = regs.launch_dma.remap_enable != 0;
  212. const u32 num_remap_components = regs.remap_const.num_dst_components_minus_one + 1;
  213. const u32 remap_components_size = regs.remap_const.component_size_minus_one + 1;
  214. u32 bytes_per_pixel = 1;
  215. DMA::ImageOperand dst_operand;
  216. dst_operand.bytes_per_pixel = bytes_per_pixel;
  217. dst_operand.params = regs.dst_params;
  218. dst_operand.address = regs.offset_out;
  219. DMA::BufferOperand src_operand;
  220. src_operand.pitch = regs.pitch_in;
  221. src_operand.width = regs.line_length_in;
  222. src_operand.height = regs.line_count;
  223. src_operand.address = regs.offset_in;
  224. DMA::ImageCopy copy_info{};
  225. copy_info.length_x = regs.line_length_in;
  226. copy_info.length_y = regs.line_count;
  227. auto& accelerate = rasterizer->AccessAccelerateDMA();
  228. if (accelerate.BufferToImage(copy_info, src_operand, dst_operand)) {
  229. return;
  230. }
  231. const auto& dst_params = regs.dst_params;
  232. const u32 base_bpp = !is_remapping ? 1U : num_remap_components * remap_components_size;
  233. u32 width = dst_params.width;
  234. u32 x_elements = regs.line_length_in;
  235. u32 x_offset = dst_params.origin.x;
  236. u32 bpp_shift = 0U;
  237. if (!is_remapping) {
  238. bpp_shift = Common::FoldRight(
  239. 4U, [](u32 x, u32 y) { return std::min(x, static_cast<u32>(std::countr_zero(y))); },
  240. width, x_elements, x_offset, static_cast<u32>(regs.offset_out));
  241. width >>= bpp_shift;
  242. x_elements >>= bpp_shift;
  243. x_offset >>= bpp_shift;
  244. }
  245. bytes_per_pixel = base_bpp << bpp_shift;
  246. const u32 height = dst_params.height;
  247. const u32 depth = dst_params.depth;
  248. const u32 block_height = dst_params.block_size.height;
  249. const u32 block_depth = dst_params.block_size.depth;
  250. const size_t dst_size =
  251. CalculateSize(true, bytes_per_pixel, width, height, depth, block_height, block_depth);
  252. const size_t src_size = static_cast<size_t>(regs.pitch_in) * regs.line_count;
  253. GPUVAddr src_addr = regs.offset_in;
  254. GPUVAddr dst_addr = regs.offset_out;
  255. Tegra::Memory::GpuGuestMemory<u8, Tegra::Memory::GuestMemoryFlags::SafeRead> tmp_read_buffer(
  256. memory_manager, src_addr, src_size, &read_buffer);
  257. Tegra::Memory::GpuGuestMemoryScoped<u8, Tegra::Memory::GuestMemoryFlags::UnsafeReadCachedWrite>
  258. tmp_write_buffer(memory_manager, dst_addr, dst_size, &write_buffer);
  259. // If the input is linear and the output is tiled, swizzle the input and copy it over.
  260. SwizzleSubrect(tmp_write_buffer, tmp_read_buffer, bytes_per_pixel, width, height, depth,
  261. x_offset, dst_params.origin.y, x_elements, regs.line_count, block_height,
  262. block_depth, regs.pitch_in);
  263. }
  264. void MaxwellDMA::CopyBlockLinearToBlockLinear() {
  265. UNIMPLEMENTED_IF(regs.src_params.block_size.width != 0);
  266. const bool is_remapping = regs.launch_dma.remap_enable != 0;
  267. // Deswizzle the input and copy it over.
  268. const DMA::Parameters& src = regs.src_params;
  269. const DMA::Parameters& dst = regs.dst_params;
  270. const u32 num_remap_components = regs.remap_const.num_dst_components_minus_one + 1;
  271. const u32 remap_components_size = regs.remap_const.component_size_minus_one + 1;
  272. const u32 base_bpp = !is_remapping ? 1U : num_remap_components * remap_components_size;
  273. u32 src_width = src.width;
  274. u32 dst_width = dst.width;
  275. u32 x_elements = regs.line_length_in;
  276. u32 src_x_offset = src.origin.x;
  277. u32 dst_x_offset = dst.origin.x;
  278. u32 bpp_shift = 0U;
  279. if (!is_remapping) {
  280. bpp_shift = Common::FoldRight(
  281. 4U, [](u32 x, u32 y) { return std::min(x, static_cast<u32>(std::countr_zero(y))); },
  282. src_width, dst_width, x_elements, src_x_offset, dst_x_offset,
  283. static_cast<u32>(regs.offset_in), static_cast<u32>(regs.offset_out));
  284. src_width >>= bpp_shift;
  285. dst_width >>= bpp_shift;
  286. x_elements >>= bpp_shift;
  287. src_x_offset >>= bpp_shift;
  288. dst_x_offset >>= bpp_shift;
  289. }
  290. const u32 bytes_per_pixel = base_bpp << bpp_shift;
  291. const size_t src_size = CalculateSize(true, bytes_per_pixel, src_width, src.height, src.depth,
  292. src.block_size.height, src.block_size.depth);
  293. const size_t dst_size = CalculateSize(true, bytes_per_pixel, dst_width, dst.height, dst.depth,
  294. dst.block_size.height, dst.block_size.depth);
  295. const u32 pitch = x_elements * bytes_per_pixel;
  296. const size_t mid_buffer_size = pitch * regs.line_count;
  297. intermediate_buffer.resize_destructive(mid_buffer_size);
  298. Tegra::Memory::GpuGuestMemory<u8, Tegra::Memory::GuestMemoryFlags::SafeRead> tmp_read_buffer(
  299. memory_manager, regs.offset_in, src_size, &read_buffer);
  300. Tegra::Memory::GpuGuestMemoryScoped<u8, Tegra::Memory::GuestMemoryFlags::SafeReadCachedWrite>
  301. tmp_write_buffer(memory_manager, regs.offset_out, dst_size, &write_buffer);
  302. UnswizzleSubrect(intermediate_buffer, tmp_read_buffer, bytes_per_pixel, src_width, src.height,
  303. src.depth, src_x_offset, src.origin.y, x_elements, regs.line_count,
  304. src.block_size.height, src.block_size.depth, pitch);
  305. SwizzleSubrect(tmp_write_buffer, intermediate_buffer, bytes_per_pixel, dst_width, dst.height,
  306. dst.depth, dst_x_offset, dst.origin.y, x_elements, regs.line_count,
  307. dst.block_size.height, dst.block_size.depth, pitch);
  308. }
  309. void MaxwellDMA::ReleaseSemaphore() {
  310. const auto type = regs.launch_dma.semaphore_type;
  311. const GPUVAddr address = regs.semaphore.address;
  312. const u32 payload = regs.semaphore.payload;
  313. VideoCommon::QueryPropertiesFlags flags{VideoCommon::QueryPropertiesFlags::IsAFence};
  314. switch (type) {
  315. case LaunchDMA::SemaphoreType::NONE:
  316. break;
  317. case LaunchDMA::SemaphoreType::RELEASE_ONE_WORD_SEMAPHORE: {
  318. rasterizer->Query(address, VideoCommon::QueryType::Payload, flags, payload, 0);
  319. break;
  320. }
  321. case LaunchDMA::SemaphoreType::RELEASE_FOUR_WORD_SEMAPHORE: {
  322. rasterizer->Query(address, VideoCommon::QueryType::Payload,
  323. flags | VideoCommon::QueryPropertiesFlags::HasTimeout, payload, 0);
  324. break;
  325. }
  326. default:
  327. ASSERT_MSG(false, "Unknown semaphore type: {}", static_cast<u32>(type.Value()));
  328. break;
  329. }
  330. }
  331. } // namespace Tegra::Engines