blitter.cpp 10 KB

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