maxwell_dma.cpp 16 KB

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