blitter.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-3.0-or-later
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. #include "common/scratch_buffer.h"
  7. #include "video_core/engines/sw_blitter/blitter.h"
  8. #include "video_core/engines/sw_blitter/converter.h"
  9. #include "video_core/guest_memory.h"
  10. #include "video_core/memory_manager.h"
  11. #include "video_core/surface.h"
  12. #include "video_core/textures/decoders.h"
  13. namespace Tegra {
  14. class MemoryManager;
  15. }
  16. using VideoCore::Surface::BytesPerBlock;
  17. using VideoCore::Surface::PixelFormatFromRenderTargetFormat;
  18. namespace Tegra::Engines::Blitter {
  19. using namespace Texture;
  20. namespace {
  21. constexpr size_t ir_components = 4;
  22. void NearestNeighbor(std::span<const u8> input, std::span<u8> output, u32 src_width, u32 src_height,
  23. u32 dst_width, u32 dst_height, size_t bpp) {
  24. const size_t dx_du = std::llround((static_cast<f64>(src_width) / dst_width) * (1ULL << 32));
  25. const size_t dy_dv = std::llround((static_cast<f64>(src_height) / dst_height) * (1ULL << 32));
  26. size_t src_y = 0;
  27. for (u32 y = 0; y < dst_height; y++) {
  28. size_t src_x = 0;
  29. for (u32 x = 0; x < dst_width; x++) {
  30. const size_t read_from = ((src_y * src_width + src_x) >> 32) * bpp;
  31. const size_t write_to = (y * dst_width + x) * bpp;
  32. std::memcpy(&output[write_to], &input[read_from], bpp);
  33. src_x += dx_du;
  34. }
  35. src_y += dy_dv;
  36. }
  37. }
  38. void NearestNeighborFast(std::span<const f32> input, std::span<f32> output, u32 src_width,
  39. u32 src_height, u32 dst_width, u32 dst_height) {
  40. const size_t dx_du = std::llround((static_cast<f64>(src_width) / dst_width) * (1ULL << 32));
  41. const size_t dy_dv = std::llround((static_cast<f64>(src_height) / dst_height) * (1ULL << 32));
  42. size_t src_y = 0;
  43. for (u32 y = 0; y < dst_height; y++) {
  44. size_t src_x = 0;
  45. for (u32 x = 0; x < dst_width; x++) {
  46. const size_t read_from = ((src_y * src_width + src_x) >> 32) * ir_components;
  47. const size_t write_to = (y * dst_width + x) * ir_components;
  48. std::memcpy(&output[write_to], &input[read_from], sizeof(f32) * ir_components);
  49. src_x += dx_du;
  50. }
  51. src_y += dy_dv;
  52. }
  53. }
  54. void Bilinear(std::span<const f32> input, std::span<f32> output, size_t src_width,
  55. size_t src_height, size_t dst_width, size_t dst_height) {
  56. const auto bilinear_sample = [](std::span<const f32> x0_y0, std::span<const f32> x1_y0,
  57. std::span<const f32> x0_y1, std::span<const f32> x1_y1,
  58. f32 weight_x, f32 weight_y) {
  59. std::array<f32, ir_components> result{};
  60. for (size_t i = 0; i < ir_components; i++) {
  61. const f32 a = std::lerp(x0_y0[i], x1_y0[i], weight_x);
  62. const f32 b = std::lerp(x0_y1[i], x1_y1[i], weight_x);
  63. result[i] = std::lerp(a, b, weight_y);
  64. }
  65. return result;
  66. };
  67. const f32 dx_du =
  68. dst_width > 1 ? static_cast<f32>(src_width - 1) / static_cast<f32>(dst_width - 1) : 0.f;
  69. const f32 dy_dv =
  70. dst_height > 1 ? static_cast<f32>(src_height - 1) / static_cast<f32>(dst_height - 1) : 0.f;
  71. for (u32 y = 0; y < dst_height; y++) {
  72. for (u32 x = 0; x < dst_width; x++) {
  73. const f32 x_low = std::floor(static_cast<f32>(x) * dx_du);
  74. const f32 y_low = std::floor(static_cast<f32>(y) * dy_dv);
  75. const f32 x_high = std::ceil(static_cast<f32>(x) * dx_du);
  76. const f32 y_high = std::ceil(static_cast<f32>(y) * dy_dv);
  77. const f32 weight_x = (static_cast<f32>(x) * dx_du) - x_low;
  78. const f32 weight_y = (static_cast<f32>(y) * dy_dv) - y_low;
  79. const auto read_src = [&](f32 in_x, f32 in_y) {
  80. const size_t read_from =
  81. ((static_cast<size_t>(in_x) * src_width + static_cast<size_t>(in_y)) >> 32) *
  82. ir_components;
  83. return std::span<const f32>(&input[read_from], ir_components);
  84. };
  85. auto x0_y0 = read_src(x_low, y_low);
  86. auto x1_y0 = read_src(x_high, y_low);
  87. auto x0_y1 = read_src(x_low, y_high);
  88. auto x1_y1 = read_src(x_high, y_high);
  89. const auto result = bilinear_sample(x0_y0, x1_y0, x0_y1, x1_y1, weight_x, weight_y);
  90. const size_t write_to = (y * dst_width + x) * ir_components;
  91. std::memcpy(&output[write_to], &result, sizeof(f32) * ir_components);
  92. }
  93. }
  94. }
  95. } // namespace
  96. struct SoftwareBlitEngine::BlitEngineImpl {
  97. Common::ScratchBuffer<u8> tmp_buffer;
  98. Common::ScratchBuffer<u8> src_buffer;
  99. Common::ScratchBuffer<u8> dst_buffer;
  100. Common::ScratchBuffer<f32> intermediate_src;
  101. Common::ScratchBuffer<f32> intermediate_dst;
  102. ConverterFactory converter_factory;
  103. };
  104. SoftwareBlitEngine::SoftwareBlitEngine(MemoryManager& memory_manager_)
  105. : memory_manager{memory_manager_} {
  106. impl = std::make_unique<BlitEngineImpl>();
  107. }
  108. SoftwareBlitEngine::~SoftwareBlitEngine() = default;
  109. bool SoftwareBlitEngine::Blit(Fermi2D::Surface& src, Fermi2D::Surface& dst,
  110. Fermi2D::Config& config) {
  111. const auto get_surface_size = [](Fermi2D::Surface& surface, u32 bytes_per_pixel) {
  112. if (surface.linear == Fermi2D::MemoryLayout::BlockLinear) {
  113. return CalculateSize(true, bytes_per_pixel, surface.width, surface.height,
  114. surface.depth, surface.block_height, surface.block_depth);
  115. }
  116. return static_cast<size_t>(surface.pitch * surface.height);
  117. };
  118. const auto process_pitch_linear = [](bool unpack, std::span<const u8> input,
  119. std::span<u8> output, u32 extent_x, u32 extent_y,
  120. u32 pitch, u32 x0, u32 y0, size_t bpp) {
  121. const size_t base_offset = x0 * bpp;
  122. const size_t copy_size = extent_x * bpp;
  123. for (u32 y = y0; y < extent_y; y++) {
  124. const size_t first_offset = y * pitch + base_offset;
  125. const size_t second_offset = y * extent_x * bpp;
  126. u8* write_to = unpack ? &output[first_offset] : &output[second_offset];
  127. const u8* read_from = unpack ? &input[second_offset] : &input[first_offset];
  128. std::memcpy(write_to, read_from, copy_size);
  129. }
  130. };
  131. const u32 src_extent_x = config.src_x1 - config.src_x0;
  132. const u32 src_extent_y = config.src_y1 - config.src_y0;
  133. const u32 dst_extent_x = config.dst_x1 - config.dst_x0;
  134. const u32 dst_extent_y = config.dst_y1 - config.dst_y0;
  135. const auto src_bytes_per_pixel = BytesPerBlock(PixelFormatFromRenderTargetFormat(src.format));
  136. const auto dst_bytes_per_pixel = BytesPerBlock(PixelFormatFromRenderTargetFormat(dst.format));
  137. const size_t src_size = get_surface_size(src, src_bytes_per_pixel);
  138. Tegra::Memory::GpuGuestMemory<u8, Tegra::Memory::GuestMemoryFlags::SafeRead> tmp_buffer(
  139. memory_manager, src.Address(), src_size, &impl->tmp_buffer);
  140. const size_t src_copy_size = src_extent_x * src_extent_y * src_bytes_per_pixel;
  141. const size_t dst_copy_size = dst_extent_x * dst_extent_y * dst_bytes_per_pixel;
  142. impl->src_buffer.resize_destructive(src_copy_size);
  143. const bool no_passthrough =
  144. src.format != dst.format || src_extent_x != dst_extent_x || src_extent_y != dst_extent_y;
  145. const auto conversion_phase_same_format = [&]() {
  146. NearestNeighbor(impl->src_buffer, impl->dst_buffer, src_extent_x, src_extent_y,
  147. dst_extent_x, dst_extent_y, dst_bytes_per_pixel);
  148. };
  149. const auto conversion_phase_ir = [&]() {
  150. auto* input_converter = impl->converter_factory.GetFormatConverter(src.format);
  151. impl->intermediate_src.resize_destructive((src_copy_size / src_bytes_per_pixel) *
  152. ir_components);
  153. impl->intermediate_dst.resize_destructive((dst_copy_size / dst_bytes_per_pixel) *
  154. ir_components);
  155. input_converter->ConvertTo(impl->src_buffer, impl->intermediate_src);
  156. if (config.filter != Fermi2D::Filter::Bilinear) {
  157. NearestNeighborFast(impl->intermediate_src, impl->intermediate_dst, src_extent_x,
  158. src_extent_y, dst_extent_x, dst_extent_y);
  159. } else {
  160. Bilinear(impl->intermediate_src, impl->intermediate_dst, src_extent_x, src_extent_y,
  161. dst_extent_x, dst_extent_y);
  162. }
  163. auto* output_converter = impl->converter_factory.GetFormatConverter(dst.format);
  164. output_converter->ConvertFrom(impl->intermediate_dst, impl->dst_buffer);
  165. };
  166. // Do actual Blit
  167. impl->dst_buffer.resize_destructive(dst_copy_size);
  168. if (src.linear == Fermi2D::MemoryLayout::BlockLinear) {
  169. UnswizzleSubrect(impl->src_buffer, tmp_buffer, src_bytes_per_pixel, src.width, src.height,
  170. src.depth, config.src_x0, config.src_y0, src_extent_x, src_extent_y,
  171. src.block_height, src.block_depth, src_extent_x * src_bytes_per_pixel);
  172. } else {
  173. process_pitch_linear(false, tmp_buffer, impl->src_buffer, src_extent_x, src_extent_y,
  174. src.pitch, config.src_x0, config.src_y0, src_bytes_per_pixel);
  175. }
  176. // Conversion Phase
  177. if (no_passthrough) {
  178. if (src.format != dst.format || config.filter == Fermi2D::Filter::Bilinear) {
  179. conversion_phase_ir();
  180. } else {
  181. conversion_phase_same_format();
  182. }
  183. } else {
  184. impl->dst_buffer.swap(impl->src_buffer);
  185. }
  186. const size_t dst_size = get_surface_size(dst, dst_bytes_per_pixel);
  187. Tegra::Memory::GpuGuestMemoryScoped<u8, Tegra::Memory::GuestMemoryFlags::SafeReadWrite>
  188. tmp_buffer2(memory_manager, dst.Address(), dst_size, &impl->tmp_buffer);
  189. if (dst.linear == Fermi2D::MemoryLayout::BlockLinear) {
  190. SwizzleSubrect(tmp_buffer2, impl->dst_buffer, dst_bytes_per_pixel, dst.width, dst.height,
  191. dst.depth, config.dst_x0, config.dst_y0, dst_extent_x, dst_extent_y,
  192. dst.block_height, dst.block_depth, dst_extent_x * dst_bytes_per_pixel);
  193. } else {
  194. process_pitch_linear(true, impl->dst_buffer, tmp_buffer2, dst_extent_x, dst_extent_y,
  195. dst.pitch, config.dst_x0, config.dst_y0,
  196. static_cast<size_t>(dst_bytes_per_pixel));
  197. }
  198. return true;
  199. }
  200. } // namespace Tegra::Engines::Blitter