maxwell_dma.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. MaxwellDMA::MaxwellDMA(Core::System& system, MemoryManager& memory_manager)
  15. : system{system}, memory_manager{memory_manager} {}
  16. void MaxwellDMA::CallMethod(const GPU::MethodCall& method_call) {
  17. ASSERT_MSG(method_call.method < Regs::NUM_REGS,
  18. "Invalid MaxwellDMA register, increase the size of the Regs structure");
  19. regs.reg_array[method_call.method] = method_call.argument;
  20. #define MAXWELLDMA_REG_INDEX(field_name) \
  21. (offsetof(Tegra::Engines::MaxwellDMA::Regs, field_name) / sizeof(u32))
  22. switch (method_call.method) {
  23. case MAXWELLDMA_REG_INDEX(exec): {
  24. HandleCopy();
  25. break;
  26. }
  27. }
  28. #undef MAXWELLDMA_REG_INDEX
  29. }
  30. void MaxwellDMA::HandleCopy() {
  31. LOG_TRACE(HW_GPU, "Requested a DMA copy");
  32. const GPUVAddr source = regs.src_address.Address();
  33. const GPUVAddr dest = regs.dst_address.Address();
  34. // TODO(Subv): Perform more research and implement all features of this engine.
  35. ASSERT(regs.exec.enable_swizzle == 0);
  36. ASSERT(regs.exec.query_mode == Regs::QueryMode::None);
  37. ASSERT(regs.exec.query_intr == Regs::QueryIntr::None);
  38. ASSERT(regs.exec.copy_mode == Regs::CopyMode::Unk2);
  39. ASSERT(regs.dst_params.pos_x == 0);
  40. ASSERT(regs.dst_params.pos_y == 0);
  41. if (!regs.exec.is_dst_linear && !regs.exec.is_src_linear) {
  42. // If both the source and the destination are in block layout, assert.
  43. UNREACHABLE_MSG("Tiled->Tiled DMA transfers are not yet implemented");
  44. return;
  45. }
  46. // All copies here update the main memory, so mark all rasterizer states as invalid.
  47. system.GPU().Maxwell3D().dirty.OnMemoryWrite();
  48. if (regs.exec.is_dst_linear && regs.exec.is_src_linear) {
  49. // When the enable_2d bit is disabled, the copy is performed as if we were copying a 1D
  50. // buffer of length `x_count`, otherwise we copy a 2D image of dimensions (x_count,
  51. // y_count).
  52. if (!regs.exec.enable_2d) {
  53. memory_manager.CopyBlock(dest, source, regs.x_count);
  54. return;
  55. }
  56. // If both the source and the destination are in linear layout, perform a line-by-line
  57. // copy. We're going to take a subrect of size (x_count, y_count) from the source
  58. // rectangle. There is no need to manually flush/invalidate the regions because
  59. // CopyBlock does that for us.
  60. for (u32 line = 0; line < regs.y_count; ++line) {
  61. const GPUVAddr source_line = source + line * regs.src_pitch;
  62. const GPUVAddr dest_line = dest + line * regs.dst_pitch;
  63. memory_manager.CopyBlock(dest_line, source_line, regs.x_count);
  64. }
  65. return;
  66. }
  67. ASSERT(regs.exec.enable_2d == 1);
  68. if (regs.exec.is_dst_linear && !regs.exec.is_src_linear) {
  69. ASSERT(regs.src_params.BlockDepth() == 0);
  70. // If the input is tiled and the output is linear, deswizzle the input and copy it over.
  71. const u32 bytes_per_pixel = regs.dst_pitch / regs.x_count;
  72. const std::size_t src_size = Texture::CalculateSize(
  73. true, bytes_per_pixel, regs.src_params.size_x, regs.src_params.size_y,
  74. regs.src_params.size_z, regs.src_params.BlockHeight(), regs.src_params.BlockDepth());
  75. const std::size_t src_layer_size = Texture::CalculateSize(
  76. true, bytes_per_pixel, regs.src_params.size_x, regs.src_params.size_y, 1,
  77. regs.src_params.BlockHeight(), regs.src_params.BlockDepth());
  78. const std::size_t dst_size = regs.dst_pitch * regs.y_count;
  79. if (read_buffer.size() < src_size) {
  80. read_buffer.resize(src_size);
  81. }
  82. if (write_buffer.size() < dst_size) {
  83. write_buffer.resize(dst_size);
  84. }
  85. memory_manager.ReadBlock(source, read_buffer.data(), src_size);
  86. memory_manager.ReadBlock(dest, write_buffer.data(), dst_size);
  87. Texture::UnswizzleSubrect(
  88. regs.x_count, regs.y_count, regs.dst_pitch, regs.src_params.size_x, bytes_per_pixel,
  89. read_buffer.data() + src_layer_size * regs.src_params.pos_z, write_buffer.data(),
  90. regs.src_params.BlockHeight(), regs.src_params.pos_x, regs.src_params.pos_y);
  91. memory_manager.WriteBlock(dest, write_buffer.data(), dst_size);
  92. } else {
  93. ASSERT(regs.dst_params.BlockDepth() == 0);
  94. const u32 bytes_per_pixel = regs.src_pitch / regs.x_count;
  95. const std::size_t dst_size = Texture::CalculateSize(
  96. true, bytes_per_pixel, regs.dst_params.size_x, regs.dst_params.size_y,
  97. regs.dst_params.size_z, regs.dst_params.BlockHeight(), regs.dst_params.BlockDepth());
  98. const std::size_t dst_layer_size = Texture::CalculateSize(
  99. true, bytes_per_pixel, regs.dst_params.size_x, regs.dst_params.size_y, 1,
  100. regs.dst_params.BlockHeight(), regs.dst_params.BlockDepth());
  101. const std::size_t src_size = regs.src_pitch * regs.y_count;
  102. if (read_buffer.size() < src_size) {
  103. read_buffer.resize(src_size);
  104. }
  105. if (write_buffer.size() < dst_size) {
  106. write_buffer.resize(dst_size);
  107. }
  108. if (Settings::values.use_accurate_gpu_emulation) {
  109. memory_manager.ReadBlock(source, read_buffer.data(), src_size);
  110. memory_manager.ReadBlock(dest, write_buffer.data(), dst_size);
  111. } else {
  112. memory_manager.ReadBlockUnsafe(source, read_buffer.data(), src_size);
  113. memory_manager.ReadBlockUnsafe(dest, write_buffer.data(), dst_size);
  114. }
  115. // If the input is linear and the output is tiled, swizzle the input and copy it over.
  116. Texture::SwizzleSubrect(
  117. regs.x_count, regs.y_count, regs.src_pitch, regs.dst_params.size_x, bytes_per_pixel,
  118. write_buffer.data() + dst_layer_size * regs.dst_params.pos_z, read_buffer.data(),
  119. regs.dst_params.BlockHeight(), regs.dst_params.pos_x, regs.dst_params.pos_y);
  120. memory_manager.WriteBlock(dest, write_buffer.data(), dst_size);
  121. }
  122. }
  123. } // namespace Tegra::Engines