fermi_2d.cpp 3.6 KB

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