fermi_2d.cpp 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 auto source_cpu = memory_manager.GpuToCpuAddress(source);
  35. const auto dest_cpu = memory_manager.GpuToCpuAddress(dest);
  36. ASSERT_MSG(source_cpu, "Invalid source GPU address");
  37. ASSERT_MSG(dest_cpu, "Invalid destination GPU address");
  38. u32 src_bytes_per_pixel = RenderTargetBytesPerPixel(regs.src.format);
  39. u32 dst_bytes_per_pixel = RenderTargetBytesPerPixel(regs.dst.format);
  40. if (!rasterizer.AccelerateSurfaceCopy(regs.src, regs.dst)) {
  41. // All copies here update the main memory, so mark all rasterizer states as invalid.
  42. Core::System::GetInstance().GPU().Maxwell3D().dirty_flags.OnMemoryWrite();
  43. rasterizer.FlushRegion(*source_cpu, src_bytes_per_pixel * regs.src.width * regs.src.height);
  44. // We have to invalidate the destination region to evict any outdated surfaces from the
  45. // cache. We do this before actually writing the new data because the destination address
  46. // might contain a dirty surface that will have to be written back to memory.
  47. rasterizer.InvalidateRegion(*dest_cpu,
  48. dst_bytes_per_pixel * regs.dst.width * regs.dst.height);
  49. if (regs.src.linear == regs.dst.linear) {
  50. // If the input layout and the output layout are the same, just perform a raw copy.
  51. ASSERT(regs.src.BlockHeight() == regs.dst.BlockHeight());
  52. Memory::CopyBlock(*dest_cpu, *source_cpu,
  53. src_bytes_per_pixel * regs.dst.width * regs.dst.height);
  54. return;
  55. }
  56. u8* src_buffer = Memory::GetPointer(*source_cpu);
  57. u8* dst_buffer = Memory::GetPointer(*dest_cpu);
  58. if (!regs.src.linear && regs.dst.linear) {
  59. // If the input is tiled and the output is linear, deswizzle the input and copy it over.
  60. Texture::CopySwizzledData(regs.src.width, regs.src.height, regs.src.depth,
  61. src_bytes_per_pixel, dst_bytes_per_pixel, src_buffer,
  62. dst_buffer, true, regs.src.BlockHeight(),
  63. regs.src.BlockDepth(), 0);
  64. } else {
  65. // If the input is linear and the output is tiled, swizzle the input and copy it over.
  66. Texture::CopySwizzledData(regs.src.width, regs.src.height, regs.src.depth,
  67. src_bytes_per_pixel, dst_bytes_per_pixel, dst_buffer,
  68. src_buffer, false, regs.dst.BlockHeight(),
  69. regs.dst.BlockDepth(), 0);
  70. }
  71. }
  72. }
  73. } // namespace Tegra::Engines