maxwell_dma.cpp 5.2 KB

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