fermi_2d.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/fermi_2d.h"
  7. #include "video_core/engines/maxwell_3d.h"
  8. #include "video_core/rasterizer_interface.h"
  9. #include "video_core/textures/decoders.h"
  10. namespace Tegra::Engines {
  11. Fermi2D::Fermi2D(VideoCore::RasterizerInterface& rasterizer, MemoryManager& memory_manager)
  12. : memory_manager(memory_manager), rasterizer{rasterizer} {}
  13. void Fermi2D::CallMethod(const GPU::MethodCall& method_call) {
  14. ASSERT_MSG(method_call.method < Regs::NUM_REGS,
  15. "Invalid Fermi2D register, increase the size of the Regs structure");
  16. regs.reg_array[method_call.method] = method_call.argument;
  17. switch (method_call.method) {
  18. case FERMI2D_REG_INDEX(trigger): {
  19. HandleSurfaceCopy();
  20. break;
  21. }
  22. }
  23. }
  24. void Fermi2D::HandleSurfaceCopy() {
  25. LOG_WARNING(HW_GPU, "Requested a surface copy with operation {}",
  26. static_cast<u32>(regs.operation));
  27. const GPUVAddr source = regs.src.Address();
  28. const GPUVAddr dest = regs.dst.Address();
  29. // TODO(Subv): Only same-format and same-size copies are allowed for now.
  30. ASSERT(regs.src.format == regs.dst.format);
  31. ASSERT(regs.src.width * regs.src.height == regs.dst.width * regs.dst.height);
  32. // TODO(Subv): Only raw copies are implemented.
  33. ASSERT(regs.operation == Regs::Operation::SrcCopy);
  34. const VAddr source_cpu = *memory_manager.GpuToCpuAddress(source);
  35. const VAddr dest_cpu = *memory_manager.GpuToCpuAddress(dest);
  36. u32 src_bytes_per_pixel = RenderTargetBytesPerPixel(regs.src.format);
  37. u32 dst_bytes_per_pixel = RenderTargetBytesPerPixel(regs.dst.format);
  38. if (!rasterizer.AccelerateSurfaceCopy(regs.src, regs.dst)) {
  39. // All copies here update the main memory, so mark all rasterizer states as invalid.
  40. Core::System::GetInstance().GPU().Maxwell3D().dirty_flags.OnMemoryWrite();
  41. rasterizer.FlushRegion(source_cpu, src_bytes_per_pixel * regs.src.width * regs.src.height);
  42. // We have to invalidate the destination region to evict any outdated surfaces from the
  43. // cache. We do this before actually writing the new data because the destination address
  44. // might contain a dirty surface that will have to be written back to memory.
  45. rasterizer.InvalidateRegion(dest_cpu,
  46. dst_bytes_per_pixel * regs.dst.width * regs.dst.height);
  47. if (regs.src.linear == regs.dst.linear) {
  48. // If the input layout and the output layout are the same, just perform a raw copy.
  49. ASSERT(regs.src.BlockHeight() == regs.dst.BlockHeight());
  50. Memory::CopyBlock(dest_cpu, source_cpu,
  51. src_bytes_per_pixel * regs.dst.width * regs.dst.height);
  52. return;
  53. }
  54. u8* src_buffer = Memory::GetPointer(source_cpu);
  55. u8* dst_buffer = Memory::GetPointer(dest_cpu);
  56. if (!regs.src.linear && regs.dst.linear) {
  57. // If the input is tiled and the output is linear, deswizzle the input and copy it over.
  58. Texture::CopySwizzledData(regs.src.width, regs.src.height, regs.src.depth,
  59. src_bytes_per_pixel, dst_bytes_per_pixel, src_buffer,
  60. dst_buffer, true, regs.src.BlockHeight(),
  61. regs.src.BlockDepth(), 0);
  62. } else {
  63. // If the input is linear and the output is tiled, swizzle the input and copy it over.
  64. Texture::CopySwizzledData(regs.src.width, regs.src.height, regs.src.depth,
  65. src_bytes_per_pixel, dst_bytes_per_pixel, dst_buffer,
  66. src_buffer, false, regs.dst.BlockHeight(),
  67. regs.dst.BlockDepth(), 0);
  68. }
  69. }
  70. }
  71. } // namespace Tegra::Engines