maxwell_dma.cpp 5.0 KB

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