fermi_2d.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/assert.h"
  4. #include "common/logging/log.h"
  5. #include "common/microprofile.h"
  6. #include "video_core/engines/fermi_2d.h"
  7. #include "video_core/engines/sw_blitter/blitter.h"
  8. #include "video_core/rasterizer_interface.h"
  9. #include "video_core/surface.h"
  10. #include "video_core/textures/decoders.h"
  11. MICROPROFILE_DECLARE(GPU_BlitEngine);
  12. MICROPROFILE_DEFINE(GPU_BlitEngine, "GPU", "Blit Engine", MP_RGB(224, 224, 128));
  13. using VideoCore::Surface::BytesPerBlock;
  14. using VideoCore::Surface::PixelFormatFromRenderTargetFormat;
  15. namespace Tegra::Engines {
  16. using namespace Texture;
  17. Fermi2D::Fermi2D(MemoryManager& memory_manager_) {
  18. sw_blitter = std::make_unique<Blitter::SoftwareBlitEngine>(memory_manager_);
  19. // Nvidia's OpenGL driver seems to assume these values
  20. regs.src.depth = 1;
  21. regs.dst.depth = 1;
  22. execution_mask.reset();
  23. execution_mask[FERMI2D_REG_INDEX(pixels_from_memory.src_y0) + 1] = true;
  24. }
  25. Fermi2D::~Fermi2D() = default;
  26. void Fermi2D::BindRasterizer(VideoCore::RasterizerInterface* rasterizer_) {
  27. rasterizer = rasterizer_;
  28. }
  29. void Fermi2D::CallMethod(u32 method, u32 method_argument, bool is_last_call) {
  30. ASSERT_MSG(method < Regs::NUM_REGS,
  31. "Invalid Fermi2D register, increase the size of the Regs structure");
  32. regs.reg_array[method] = method_argument;
  33. if (method == FERMI2D_REG_INDEX(pixels_from_memory.src_y0) + 1) {
  34. Blit();
  35. }
  36. }
  37. void Fermi2D::CallMultiMethod(u32 method, const u32* base_start, u32 amount, u32 methods_pending) {
  38. for (u32 i = 0; i < amount; ++i) {
  39. CallMethod(method, base_start[i], methods_pending - i <= 1);
  40. }
  41. }
  42. void Fermi2D::ConsumeSinkImpl() {
  43. for (auto [method, value] : method_sink) {
  44. regs.reg_array[method] = value;
  45. }
  46. method_sink.clear();
  47. }
  48. void Fermi2D::Blit() {
  49. MICROPROFILE_SCOPE(GPU_BlitEngine);
  50. LOG_DEBUG(HW_GPU, "called. source address=0x{:x}, destination address=0x{:x}",
  51. regs.src.Address(), regs.dst.Address());
  52. UNIMPLEMENTED_IF_MSG(regs.operation != Operation::SrcCopy, "Operation is not copy");
  53. UNIMPLEMENTED_IF_MSG(regs.src.layer != 0, "Source layer is not zero");
  54. UNIMPLEMENTED_IF_MSG(regs.dst.layer != 0, "Destination layer is not zero");
  55. UNIMPLEMENTED_IF_MSG(regs.src.depth != 1, "Source depth is not one");
  56. UNIMPLEMENTED_IF_MSG(regs.clip_enable != 0, "Clipped blit enabled");
  57. const auto& args = regs.pixels_from_memory;
  58. constexpr s64 null_derivate = 1ULL << 32;
  59. Surface src = regs.src;
  60. const auto bytes_per_pixel = BytesPerBlock(PixelFormatFromRenderTargetFormat(src.format));
  61. const bool delegate_to_gpu = src.width > 512 && src.height > 512 && bytes_per_pixel <= 8 &&
  62. src.format != regs.dst.format;
  63. Config config{
  64. .operation = regs.operation,
  65. .filter = args.sample_mode.filter,
  66. .must_accelerate =
  67. args.du_dx != null_derivate || args.dv_dy != null_derivate || delegate_to_gpu,
  68. .dst_x0 = args.dst_x0,
  69. .dst_y0 = args.dst_y0,
  70. .dst_x1 = args.dst_x0 + args.dst_width,
  71. .dst_y1 = args.dst_y0 + args.dst_height,
  72. .src_x0 = static_cast<s32>(args.src_x0 >> 32),
  73. .src_y0 = static_cast<s32>(args.src_y0 >> 32),
  74. .src_x1 = static_cast<s32>((args.du_dx * args.dst_width + args.src_x0) >> 32),
  75. .src_y1 = static_cast<s32>((args.dv_dy * args.dst_height + args.src_y0) >> 32),
  76. };
  77. const auto need_align_to_pitch =
  78. src.linear == Tegra::Engines::Fermi2D::MemoryLayout::Pitch &&
  79. static_cast<s32>(src.width) == config.src_x1 &&
  80. config.src_x1 > static_cast<s32>(src.pitch / bytes_per_pixel) && config.src_x0 > 0;
  81. if (need_align_to_pitch) {
  82. auto address = src.Address() + config.src_x0 * bytes_per_pixel;
  83. src.addr_upper = static_cast<u32>(address >> 32);
  84. src.addr_lower = static_cast<u32>(address);
  85. src.width -= config.src_x0;
  86. config.src_x1 -= config.src_x0;
  87. config.src_x0 = 0;
  88. }
  89. if (!rasterizer->AccelerateSurfaceCopy(src, regs.dst, config)) {
  90. sw_blitter->Blit(src, regs.dst, config);
  91. }
  92. }
  93. } // namespace Tegra::Engines