blitter.cpp 10 KB

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