maxwell_dma.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "core/core.h"
  6. #include "core/memory.h"
  7. #include "video_core/engines/maxwell_3d.h"
  8. #include "video_core/engines/maxwell_dma.h"
  9. #include "video_core/rasterizer_interface.h"
  10. #include "video_core/textures/decoders.h"
  11. namespace Tegra::Engines {
  12. MaxwellDMA::MaxwellDMA(Core::System& system, VideoCore::RasterizerInterface& rasterizer,
  13. MemoryManager& memory_manager)
  14. : memory_manager(memory_manager), system{system}, rasterizer{rasterizer} {}
  15. void MaxwellDMA::CallMethod(const GPU::MethodCall& method_call) {
  16. ASSERT_MSG(method_call.method < Regs::NUM_REGS,
  17. "Invalid MaxwellDMA register, increase the size of the Regs structure");
  18. regs.reg_array[method_call.method] = method_call.argument;
  19. #define MAXWELLDMA_REG_INDEX(field_name) \
  20. (offsetof(Tegra::Engines::MaxwellDMA::Regs, field_name) / sizeof(u32))
  21. switch (method_call.method) {
  22. case MAXWELLDMA_REG_INDEX(exec): {
  23. HandleCopy();
  24. break;
  25. }
  26. }
  27. #undef MAXWELLDMA_REG_INDEX
  28. }
  29. void MaxwellDMA::HandleCopy() {
  30. LOG_WARNING(HW_GPU, "Requested a DMA copy");
  31. const GPUVAddr source = regs.src_address.Address();
  32. const GPUVAddr dest = regs.dst_address.Address();
  33. const auto source_cpu = memory_manager.GpuToCpuAddress(source);
  34. const auto dest_cpu = memory_manager.GpuToCpuAddress(dest);
  35. ASSERT_MSG(source_cpu, "Invalid source GPU address");
  36. ASSERT_MSG(dest_cpu, "Invalid destination GPU address");
  37. // TODO(Subv): Perform more research and implement all features of this engine.
  38. ASSERT(regs.exec.enable_swizzle == 0);
  39. ASSERT(regs.exec.query_mode == Regs::QueryMode::None);
  40. ASSERT(regs.exec.query_intr == Regs::QueryIntr::None);
  41. ASSERT(regs.exec.copy_mode == Regs::CopyMode::Unk2);
  42. ASSERT(regs.dst_params.pos_x == 0);
  43. ASSERT(regs.dst_params.pos_y == 0);
  44. if (!regs.exec.is_dst_linear && !regs.exec.is_src_linear) {
  45. // If both the source and the destination are in block layout, assert.
  46. UNREACHABLE_MSG("Tiled->Tiled DMA transfers are not yet implemented");
  47. return;
  48. }
  49. // All copies here update the main memory, so mark all rasterizer states as invalid.
  50. system.GPU().Maxwell3D().dirty_flags.OnMemoryWrite();
  51. if (regs.exec.is_dst_linear && regs.exec.is_src_linear) {
  52. // When the enable_2d bit is disabled, the copy is performed as if we were copying a 1D
  53. // buffer of length `x_count`, otherwise we copy a 2D image of dimensions (x_count,
  54. // y_count).
  55. if (!regs.exec.enable_2d) {
  56. Memory::CopyBlock(*dest_cpu, *source_cpu, regs.x_count);
  57. return;
  58. }
  59. // If both the source and the destination are in linear layout, perform a line-by-line
  60. // copy. We're going to take a subrect of size (x_count, y_count) from the source
  61. // rectangle. There is no need to manually flush/invalidate the regions because
  62. // CopyBlock does that for us.
  63. for (u32 line = 0; line < regs.y_count; ++line) {
  64. const VAddr source_line = *source_cpu + line * regs.src_pitch;
  65. const VAddr dest_line = *dest_cpu + line * regs.dst_pitch;
  66. Memory::CopyBlock(dest_line, source_line, regs.x_count);
  67. }
  68. return;
  69. }
  70. ASSERT(regs.exec.enable_2d == 1);
  71. const std::size_t copy_size = regs.x_count * regs.y_count;
  72. const auto FlushAndInvalidate = [&](u32 src_size, u64 dst_size) {
  73. // TODO(Subv): For now, manually flush the regions until we implement GPU-accelerated
  74. // copying.
  75. rasterizer.FlushRegion(*source_cpu, src_size);
  76. // We have to invalidate the destination region to evict any outdated surfaces from the
  77. // cache. We do this before actually writing the new data because the destination address
  78. // might contain a dirty surface that will have to be written back to memory.
  79. rasterizer.InvalidateRegion(*dest_cpu, dst_size);
  80. };
  81. if (regs.exec.is_dst_linear && !regs.exec.is_src_linear) {
  82. ASSERT(regs.src_params.size_z == 1);
  83. // If the input is tiled and the output is linear, deswizzle the input and copy it over.
  84. const u32 src_bytes_per_pixel = regs.src_pitch / regs.src_params.size_x;
  85. FlushAndInvalidate(regs.src_pitch * regs.src_params.size_y,
  86. copy_size * src_bytes_per_pixel);
  87. Texture::UnswizzleSubrect(regs.x_count, regs.y_count, regs.dst_pitch,
  88. regs.src_params.size_x, src_bytes_per_pixel, *source_cpu,
  89. *dest_cpu, regs.src_params.BlockHeight(), regs.src_params.pos_x,
  90. regs.src_params.pos_y);
  91. } else {
  92. ASSERT(regs.dst_params.size_z == 1);
  93. ASSERT(regs.src_pitch == regs.x_count);
  94. const u32 src_bpp = regs.src_pitch / regs.x_count;
  95. FlushAndInvalidate(regs.src_pitch * regs.y_count,
  96. regs.dst_params.size_x * regs.dst_params.size_y * src_bpp);
  97. // If the input is linear and the output is tiled, swizzle the input and copy it over.
  98. Texture::SwizzleSubrect(regs.x_count, regs.y_count, regs.src_pitch, regs.dst_params.size_x,
  99. src_bpp, *dest_cpu, *source_cpu, regs.dst_params.BlockHeight());
  100. }
  101. }
  102. } // namespace Tegra::Engines