vic.cpp 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <array>
  4. #include <tuple>
  5. #include <stdint.h>
  6. #if defined(ARCHITECTURE_x86_64)
  7. #if defined(_MSC_VER)
  8. #include <intrin.h>
  9. #else
  10. #include <immintrin.h>
  11. #endif
  12. #elif defined(ARCHITECTURE_arm64)
  13. #pragma GCC diagnostic push
  14. #pragma GCC diagnostic ignored "-Wimplicit-int-conversion"
  15. #include <sse2neon.h>
  16. #pragma GCC diagnostic pop
  17. #endif
  18. extern "C" {
  19. #if defined(__GNUC__) || defined(__clang__)
  20. #pragma GCC diagnostic push
  21. #pragma GCC diagnostic ignored "-Wconversion"
  22. #endif
  23. #include <libswscale/swscale.h>
  24. #if defined(__GNUC__) || defined(__clang__)
  25. #pragma GCC diagnostic pop
  26. #endif
  27. }
  28. #include "common/alignment.h"
  29. #include "common/assert.h"
  30. #include "common/bit_field.h"
  31. #include "common/logging/log.h"
  32. #include "common/polyfill_thread.h"
  33. #include "common/settings.h"
  34. #include "video_core/engines/maxwell_3d.h"
  35. #include "video_core/guest_memory.h"
  36. #include "video_core/host1x/host1x.h"
  37. #include "video_core/host1x/nvdec.h"
  38. #include "video_core/host1x/vic.h"
  39. #include "video_core/memory_manager.h"
  40. #include "video_core/textures/decoders.h"
  41. #if defined(ARCHITECTURE_x86_64)
  42. #include "common/x64/cpu_detect.h"
  43. #endif
  44. namespace Tegra::Host1x {
  45. namespace {
  46. static bool HasSSE41() {
  47. #if defined(ARCHITECTURE_x86_64)
  48. const auto& cpu_caps{Common::GetCPUCaps()};
  49. return cpu_caps.sse4_1;
  50. #else
  51. return false;
  52. #endif
  53. }
  54. void SwizzleSurface(std::span<u8> output, u32 out_stride, std::span<const u8> input, u32 in_stride,
  55. u32 height) {
  56. /*
  57. * Taken from https://github.com/averne/FFmpeg/blob/nvtegra/libavutil/hwcontext_nvtegra.c#L949
  58. * Can only handle block height == 1.
  59. */
  60. const uint32_t x_mask = 0xFFFFFFD2u;
  61. const uint32_t y_mask = 0x2Cu;
  62. uint32_t offs_x{};
  63. uint32_t offs_y{};
  64. uint32_t offs_line{};
  65. for (u32 y = 0; y < height; y += 2) {
  66. auto dst_line = output.data() + offs_y * 16;
  67. const auto src_line = input.data() + y * (in_stride / 16) * 16;
  68. offs_line = offs_x;
  69. for (u32 x = 0; x < in_stride; x += 16) {
  70. std::memcpy(&dst_line[offs_line * 16], &src_line[x], 16);
  71. std::memcpy(&dst_line[offs_line * 16 + 16], &src_line[x + in_stride], 16);
  72. offs_line = (offs_line - x_mask) & x_mask;
  73. }
  74. offs_y = (offs_y - y_mask) & y_mask;
  75. /* Wrap into next tile row */
  76. if (!offs_y) {
  77. offs_x += out_stride;
  78. }
  79. }
  80. }
  81. } // namespace
  82. Vic::Vic(Host1x& host1x_, s32 id_, u32 syncpt, FrameQueue& frame_queue_)
  83. : CDmaPusher{host1x_, id_}, id{id_}, syncpoint{syncpt}, frame_queue{frame_queue_},
  84. has_sse41{HasSSE41()} {
  85. LOG_INFO(HW_GPU, "Created vic {}", id);
  86. }
  87. Vic::~Vic() {
  88. LOG_INFO(HW_GPU, "Destroying vic {}", id);
  89. frame_queue.Close(id);
  90. }
  91. void Vic::ProcessMethod(u32 method, u32 arg) {
  92. LOG_TRACE(HW_GPU, "Vic {} method 0x{:X}", id, static_cast<u32>(method));
  93. regs.reg_array[method] = arg;
  94. switch (static_cast<Method>(method * sizeof(u32))) {
  95. case Method::Execute: {
  96. Execute();
  97. } break;
  98. default:
  99. break;
  100. }
  101. }
  102. void Vic::Execute() {
  103. ConfigStruct config{};
  104. memory_manager.ReadBlock(regs.config_struct_offset.Address(), &config, sizeof(ConfigStruct));
  105. auto output_width{config.output_surface_config.out_surface_width + 1};
  106. auto output_height{config.output_surface_config.out_surface_height + 1};
  107. output_surface.resize_destructive(output_width * output_height);
  108. if (Settings::values.nvdec_emulation.GetValue() == Settings::NvdecEmulation::Off) [[unlikely]] {
  109. // Fill the frame with black, as otherwise they can have random data and be very glitchy.
  110. std::fill(output_surface.begin(), output_surface.end(), Pixel{});
  111. } else {
  112. for (size_t i = 0; i < config.slot_structs.size(); i++) {
  113. auto& slot_config{config.slot_structs[i]};
  114. if (!slot_config.config.slot_enable) {
  115. continue;
  116. }
  117. auto luma_offset{regs.surfaces[i][SurfaceIndex::Current].luma.Address()};
  118. if (nvdec_id == -1) {
  119. nvdec_id = frame_queue.VicFindNvdecFdFromOffset(luma_offset);
  120. }
  121. auto frame = frame_queue.GetFrame(nvdec_id, luma_offset);
  122. if (!frame.get()) {
  123. LOG_ERROR(HW_GPU, "Vic {} failed to get frame with offset 0x{:X}", id, luma_offset);
  124. continue;
  125. }
  126. switch (frame->GetPixelFormat()) {
  127. case AV_PIX_FMT_YUV420P:
  128. ReadY8__V8U8_N420<true>(slot_config, regs.surfaces[i], std::move(frame));
  129. break;
  130. case AV_PIX_FMT_NV12:
  131. ReadY8__V8U8_N420<false>(slot_config, regs.surfaces[i], std::move(frame));
  132. break;
  133. default:
  134. UNIMPLEMENTED_MSG(
  135. "Unimplemented slot pixel format {}",
  136. static_cast<u32>(slot_config.surface_config.slot_pixel_format.Value()));
  137. break;
  138. }
  139. Blend(config, slot_config);
  140. }
  141. }
  142. switch (config.output_surface_config.out_pixel_format) {
  143. case VideoPixelFormat::A8B8G8R8:
  144. case VideoPixelFormat::X8B8G8R8:
  145. WriteABGR<VideoPixelFormat::A8B8G8R8>(config.output_surface_config);
  146. break;
  147. case VideoPixelFormat::A8R8G8B8:
  148. WriteABGR<VideoPixelFormat::A8R8G8B8>(config.output_surface_config);
  149. break;
  150. case VideoPixelFormat::Y8__V8U8_N420:
  151. WriteY8__V8U8_N420(config.output_surface_config);
  152. break;
  153. default:
  154. UNIMPLEMENTED_MSG("Unknown video pixel format {}",
  155. config.output_surface_config.out_pixel_format.Value());
  156. break;
  157. }
  158. }
  159. template <bool Planar, bool Interlaced>
  160. void Vic::ReadProgressiveY8__V8U8_N420(const SlotStruct& slot,
  161. std::span<const PlaneOffsets> offsets,
  162. std::shared_ptr<const FFmpeg::Frame> frame) {
  163. const auto out_luma_width{slot.surface_config.slot_surface_width + 1};
  164. auto out_luma_height{slot.surface_config.slot_surface_height + 1};
  165. const auto out_luma_stride{out_luma_width};
  166. if constexpr (Interlaced) {
  167. out_luma_height *= 2;
  168. }
  169. slot_surface.resize_destructive(out_luma_width * out_luma_height);
  170. const auto in_luma_width{std::min(frame->GetWidth(), static_cast<s32>(out_luma_width))};
  171. const auto in_luma_height{std::min(frame->GetHeight(), static_cast<s32>(out_luma_height))};
  172. const auto in_luma_stride{frame->GetStride(0)};
  173. const auto in_chroma_stride{frame->GetStride(1)};
  174. const auto* luma_buffer{frame->GetPlane(0)};
  175. const auto* chroma_u_buffer{frame->GetPlane(1)};
  176. const auto* chroma_v_buffer{frame->GetPlane(2)};
  177. LOG_TRACE(HW_GPU,
  178. "Reading frame"
  179. "\ninput luma {}x{} stride {} chroma {}x{} stride {}\n"
  180. "output luma {}x{} stride {} chroma {}x{} stride {}",
  181. in_luma_width, in_luma_height, in_luma_stride, in_luma_width / 2, in_luma_height / 2,
  182. in_chroma_stride, out_luma_width, out_luma_height, out_luma_stride, out_luma_width,
  183. out_luma_height, out_luma_stride);
  184. [[maybe_unused]] auto DecodeLinear = [&]() {
  185. const auto alpha{static_cast<u16>(slot.config.planar_alpha.Value())};
  186. for (s32 y = 0; y < in_luma_height; y++) {
  187. const auto src_luma{y * in_luma_stride};
  188. const auto src_chroma{(y / 2) * in_chroma_stride};
  189. const auto dst{y * out_luma_stride};
  190. for (s32 x = 0; x < in_luma_width; x++) {
  191. slot_surface[dst + x].r = static_cast<u16>(luma_buffer[src_luma + x] << 2);
  192. // Chroma samples are duplicated horizontally and vertically.
  193. if constexpr (Planar) {
  194. slot_surface[dst + x].g =
  195. static_cast<u16>(chroma_u_buffer[src_chroma + x / 2] << 2);
  196. slot_surface[dst + x].b =
  197. static_cast<u16>(chroma_v_buffer[src_chroma + x / 2] << 2);
  198. } else {
  199. slot_surface[dst + x].g =
  200. static_cast<u16>(chroma_u_buffer[src_chroma + (x & ~1) + 0] << 2);
  201. slot_surface[dst + x].b =
  202. static_cast<u16>(chroma_u_buffer[src_chroma + (x & ~1) + 1] << 2);
  203. }
  204. slot_surface[dst + x].a = alpha;
  205. }
  206. }
  207. };
  208. #if defined(ARCHITECTURE_x86_64)
  209. if (!has_sse41) {
  210. DecodeLinear();
  211. return;
  212. }
  213. #endif
  214. #if defined(ARCHITECTURE_x86_64) || defined(ARCHITECTURE_arm64)
  215. const auto alpha_linear{static_cast<u16>(slot.config.planar_alpha.Value())};
  216. const auto alpha =
  217. _mm_slli_epi64(_mm_set1_epi64x(static_cast<s64>(slot.config.planar_alpha.Value())), 48);
  218. const auto shuffle_mask = _mm_set_epi8(13, 15, 14, 12, 9, 11, 10, 8, 5, 7, 6, 4, 1, 3, 2, 0);
  219. const auto sse_aligned_width = Common::AlignDown(in_luma_width, 16);
  220. for (s32 y = 0; y < in_luma_height; y++) {
  221. const auto src_luma{y * in_luma_stride};
  222. const auto src_chroma{(y / 2) * in_chroma_stride};
  223. const auto dst{y * out_luma_stride};
  224. s32 x = 0;
  225. for (; x < sse_aligned_width; x += 16) {
  226. // clang-format off
  227. // Prefetch next iteration's memory
  228. _mm_prefetch((const char*)&luma_buffer[src_luma + x + 16], _MM_HINT_T0);
  229. // Load 8 bytes * 2 of 8-bit luma samples
  230. // luma0 = 00 00 00 00 00 00 00 00 LL LL LL LL LL LL LL LL
  231. auto luma0 = _mm_loadl_epi64((__m128i*)&luma_buffer[src_luma + x + 0]);
  232. auto luma1 = _mm_loadl_epi64((__m128i*)&luma_buffer[src_luma + x + 8]);
  233. __m128i chroma;
  234. if constexpr (Planar) {
  235. _mm_prefetch((const char*)&chroma_u_buffer[src_chroma + x / 2 + 8], _MM_HINT_T0);
  236. _mm_prefetch((const char*)&chroma_v_buffer[src_chroma + x / 2 + 8], _MM_HINT_T0);
  237. // If Chroma is planar, we have separate U and V planes, load 8 bytes of each
  238. // chroma_u0 = 00 00 00 00 00 00 00 00 UU UU UU UU UU UU UU UU
  239. // chroma_v0 = 00 00 00 00 00 00 00 00 VV VV VV VV VV VV VV VV
  240. auto chroma_u0 = _mm_loadl_epi64((__m128i*)&chroma_u_buffer[src_chroma + x / 2]);
  241. auto chroma_v0 = _mm_loadl_epi64((__m128i*)&chroma_v_buffer[src_chroma + x / 2]);
  242. // Interleave the 8 bytes of U and V into a single 16 byte reg
  243. // chroma = VV UU VV UU VV UU VV UU VV UU VV UU VV UU VV UU
  244. chroma = _mm_unpacklo_epi8(chroma_u0, chroma_v0);
  245. } else {
  246. _mm_prefetch((const char*)&chroma_u_buffer[src_chroma + x / 2 + 8], _MM_HINT_T0);
  247. // Chroma is already interleaved in semiplanar format, just load 16 bytes
  248. // chroma = VV UU VV UU VV UU VV UU VV UU VV UU VV UU VV UU
  249. chroma = _mm_load_si128((__m128i*)&chroma_u_buffer[src_chroma + x]);
  250. }
  251. // Convert the low 8 bytes of 8-bit luma into 16-bit luma
  252. // luma0 = [00] [00] [00] [00] [00] [00] [00] [00] [LL] [LL] [LL] [LL] [LL] [LL] [LL] [LL]
  253. // ->
  254. // luma0 = [00 LL] [00 LL] [00 LL] [00 LL] [00 LL] [00 LL] [00 LL] [00 LL]
  255. luma0 = _mm_cvtepu8_epi16(luma0);
  256. luma1 = _mm_cvtepu8_epi16(luma1);
  257. // Treat the 8 bytes of 8-bit chroma as 16-bit channels, this allows us to take both the
  258. // U and V together as one element. Using chroma twice here duplicates the values, as we
  259. // take element 0 from chroma, and then element 0 from chroma again, etc. We need to
  260. // duplicate chroma horitonally as chroma is half the width of luma.
  261. // chroma = [VV8 UU8] [VV7 UU7] [VV6 UU6] [VV5 UU5] [VV4 UU4] [VV3 UU3] [VV2 UU2] [VV1 UU1]
  262. // ->
  263. // chroma00 = [VV4 UU4] [VV4 UU4] [VV3 UU3] [VV3 UU3] [VV2 UU2] [VV2 UU2] [VV1 UU1] [VV1 UU1]
  264. // chroma01 = [VV8 UU8] [VV8 UU8] [VV7 UU7] [VV7 UU7] [VV6 UU6] [VV6 UU6] [VV5 UU5] [VV5 UU5]
  265. auto chroma00 = _mm_unpacklo_epi16(chroma, chroma);
  266. auto chroma01 = _mm_unpackhi_epi16(chroma, chroma);
  267. // Interleave the 16-bit luma and chroma.
  268. // luma0 = [008 LL8] [007 LL7] [006 LL6] [005 LL5] [004 LL4] [003 LL3] [002 LL2] [001 LL1]
  269. // chroma00 = [VV8 UU8] [VV7 UU7] [VV6 UU6] [VV5 UU5] [VV4 UU4] [VV3 UU3] [VV2 UU2] [VV1 UU1]
  270. // ->
  271. // yuv0 = [VV4 UU4 004 LL4] [VV3 UU3 003 LL3] [VV2 UU2 002 LL2] [VV1 UU1 001 LL1]
  272. // yuv1 = [VV8 UU8 008 LL8] [VV7 UU7 007 LL7] [VV6 UU6 006 LL6] [VV5 UU5 005 LL5]
  273. auto yuv0 = _mm_unpacklo_epi16(luma0, chroma00);
  274. auto yuv1 = _mm_unpackhi_epi16(luma0, chroma00);
  275. auto yuv2 = _mm_unpacklo_epi16(luma1, chroma01);
  276. auto yuv3 = _mm_unpackhi_epi16(luma1, chroma01);
  277. // Shuffle the luma/chroma into the channel ordering we actually want. The high byte of
  278. // the luma which is now a constant 0 after converting 8-bit -> 16-bit is used as the
  279. // alpha. Luma -> R, U -> G, V -> B, 0 -> A
  280. // yuv0 = [VV4 UU4 004 LL4] [VV3 UU3 003 LL3] [VV2 UU2 002 LL2] [VV1 UU1 001 LL1]
  281. // ->
  282. // yuv0 = [AA4 VV4 UU4 LL4] [AA3 VV3 UU3 LL3] [AA2 VV2 UU2 LL2] [AA1 VV1 UU1 LL1]
  283. yuv0 = _mm_shuffle_epi8(yuv0, shuffle_mask);
  284. yuv1 = _mm_shuffle_epi8(yuv1, shuffle_mask);
  285. yuv2 = _mm_shuffle_epi8(yuv2, shuffle_mask);
  286. yuv3 = _mm_shuffle_epi8(yuv3, shuffle_mask);
  287. // Extend the 8-bit channels we have into 16-bits, as that's the target surface format.
  288. // Since this turns just the low 8 bytes into 16 bytes, the second of
  289. // each operation here right shifts the register by 8 to get the high pixels.
  290. // yuv0 = [AA4] [VV4] [UU4] [LL4] [AA3] [VV3] [UU3] [LL3] [AA2] [VV2] [UU2] [LL2] [AA1] [VV1] [UU1] [LL1]
  291. // ->
  292. // yuv01 = [002 AA2] [002 VV2] [002 UU2] [002 LL2] [001 AA1] [001 VV1] [001 UU1] [001 LL1]
  293. // yuv23 = [004 AA4] [004 VV4] [004 UU4] [004 LL4] [003 AA3] [003 VV3] ]003 UU3] [003 LL3]
  294. auto yuv01 = _mm_cvtepu8_epi16(yuv0);
  295. auto yuv23 = _mm_cvtepu8_epi16(_mm_srli_si128(yuv0, 8));
  296. auto yuv45 = _mm_cvtepu8_epi16(yuv1);
  297. auto yuv67 = _mm_cvtepu8_epi16(_mm_srli_si128(yuv1, 8));
  298. auto yuv89 = _mm_cvtepu8_epi16(yuv2);
  299. auto yuv1011 = _mm_cvtepu8_epi16(_mm_srli_si128(yuv2, 8));
  300. auto yuv1213 = _mm_cvtepu8_epi16(yuv3);
  301. auto yuv1415 = _mm_cvtepu8_epi16(_mm_srli_si128(yuv3, 8));
  302. // Left-shift all 16-bit channels by 2, this is to get us into a 10-bit format instead
  303. // of 8, which is the format alpha is in, as well as other blending values.
  304. yuv01 = _mm_slli_epi16(yuv01, 2);
  305. yuv23 = _mm_slli_epi16(yuv23, 2);
  306. yuv45 = _mm_slli_epi16(yuv45, 2);
  307. yuv67 = _mm_slli_epi16(yuv67, 2);
  308. yuv89 = _mm_slli_epi16(yuv89, 2);
  309. yuv1011 = _mm_slli_epi16(yuv1011, 2);
  310. yuv1213 = _mm_slli_epi16(yuv1213, 2);
  311. yuv1415 = _mm_slli_epi16(yuv1415, 2);
  312. // OR in the planar alpha, this has already been duplicated and shifted into position,
  313. // and just fills in the AA channels with the actual alpha value.
  314. yuv01 = _mm_or_si128(yuv01, alpha);
  315. yuv23 = _mm_or_si128(yuv23, alpha);
  316. yuv45 = _mm_or_si128(yuv45, alpha);
  317. yuv67 = _mm_or_si128(yuv67, alpha);
  318. yuv89 = _mm_or_si128(yuv89, alpha);
  319. yuv1011 = _mm_or_si128(yuv1011, alpha);
  320. yuv1213 = _mm_or_si128(yuv1213, alpha);
  321. yuv1415 = _mm_or_si128(yuv1415, alpha);
  322. // Store out the pixels. One pixel is now 8 bytes, so each store is 2 pixels.
  323. // [AA AA] [VV VV] [UU UU] [LL LL] [AA AA] [VV VV] [UU UU] [LL LL]
  324. _mm_store_si128((__m128i*)&slot_surface[dst + x + 0], yuv01);
  325. _mm_store_si128((__m128i*)&slot_surface[dst + x + 2], yuv23);
  326. _mm_store_si128((__m128i*)&slot_surface[dst + x + 4], yuv45);
  327. _mm_store_si128((__m128i*)&slot_surface[dst + x + 6], yuv67);
  328. _mm_store_si128((__m128i*)&slot_surface[dst + x + 8], yuv89);
  329. _mm_store_si128((__m128i*)&slot_surface[dst + x + 10], yuv1011);
  330. _mm_store_si128((__m128i*)&slot_surface[dst + x + 12], yuv1213);
  331. _mm_store_si128((__m128i*)&slot_surface[dst + x + 14], yuv1415);
  332. // clang-format on
  333. }
  334. for (; x < in_luma_width; x++) {
  335. slot_surface[dst + x].r = static_cast<u16>(luma_buffer[src_luma + x] << 2);
  336. // Chroma samples are duplicated horizontally and vertically.
  337. if constexpr (Planar) {
  338. slot_surface[dst + x].g =
  339. static_cast<u16>(chroma_u_buffer[src_chroma + x / 2] << 2);
  340. slot_surface[dst + x].b =
  341. static_cast<u16>(chroma_v_buffer[src_chroma + x / 2] << 2);
  342. } else {
  343. slot_surface[dst + x].g =
  344. static_cast<u16>(chroma_u_buffer[src_chroma + (x & ~1) + 0] << 2);
  345. slot_surface[dst + x].b =
  346. static_cast<u16>(chroma_u_buffer[src_chroma + (x & ~1) + 1] << 2);
  347. }
  348. slot_surface[dst + x].a = alpha_linear;
  349. }
  350. }
  351. #else
  352. DecodeLinear();
  353. #endif
  354. }
  355. template <bool Planar, bool TopField>
  356. void Vic::ReadInterlacedY8__V8U8_N420(const SlotStruct& slot, std::span<const PlaneOffsets> offsets,
  357. std::shared_ptr<const FFmpeg::Frame> frame) {
  358. if constexpr (!Planar) {
  359. ReadProgressiveY8__V8U8_N420<Planar, true>(slot, offsets, std::move(frame));
  360. return;
  361. }
  362. const auto out_luma_width{slot.surface_config.slot_surface_width + 1};
  363. const auto out_luma_height{(slot.surface_config.slot_surface_height + 1) * 2};
  364. const auto out_luma_stride{out_luma_width};
  365. slot_surface.resize_destructive(out_luma_width * out_luma_height);
  366. const auto in_luma_width{std::min(frame->GetWidth(), static_cast<s32>(out_luma_width))};
  367. [[maybe_unused]] const auto in_luma_height{
  368. std::min(frame->GetHeight(), static_cast<s32>(out_luma_height))};
  369. const auto in_luma_stride{frame->GetStride(0)};
  370. [[maybe_unused]] const auto in_chroma_width{(frame->GetWidth() + 1) / 2};
  371. const auto in_chroma_height{(frame->GetHeight() + 1) / 2};
  372. const auto in_chroma_stride{frame->GetStride(1)};
  373. const auto* luma_buffer{frame->GetPlane(0)};
  374. const auto* chroma_u_buffer{frame->GetPlane(1)};
  375. const auto* chroma_v_buffer{frame->GetPlane(2)};
  376. LOG_TRACE(HW_GPU,
  377. "Reading frame"
  378. "\ninput luma {}x{} stride {} chroma {}x{} stride {}\n"
  379. "output luma {}x{} stride {} chroma {}x{} stride {}",
  380. in_luma_width, in_luma_height, in_luma_stride, in_chroma_width, in_chroma_height,
  381. in_chroma_stride, out_luma_width, out_luma_height, out_luma_stride,
  382. out_luma_width / 2, out_luma_height / 2, out_luma_stride);
  383. [[maybe_unused]] auto DecodeLinear = [&]() {
  384. auto DecodeBobField = [&]() {
  385. const auto alpha{static_cast<u16>(slot.config.planar_alpha.Value())};
  386. for (s32 y = static_cast<s32>(TopField == false); y < in_chroma_height * 2; y += 2) {
  387. const auto src_luma{y * in_luma_stride};
  388. const auto src_chroma{(y / 2) * in_chroma_stride};
  389. const auto dst{y * out_luma_stride};
  390. for (s32 x = 0; x < in_luma_width; x++) {
  391. slot_surface[dst + x].r = static_cast<u16>(luma_buffer[src_luma + x] << 2);
  392. if constexpr (Planar) {
  393. slot_surface[dst + x].g =
  394. static_cast<u16>(chroma_u_buffer[src_chroma + x / 2] << 2);
  395. slot_surface[dst + x].b =
  396. static_cast<u16>(chroma_v_buffer[src_chroma + x / 2] << 2);
  397. } else {
  398. slot_surface[dst + x].g =
  399. static_cast<u16>(chroma_u_buffer[src_chroma + (x & ~1) + 0] << 2);
  400. slot_surface[dst + x].b =
  401. static_cast<u16>(chroma_u_buffer[src_chroma + (x & ~1) + 1] << 2);
  402. }
  403. slot_surface[dst + x].a = alpha;
  404. }
  405. s32 other_line{};
  406. if constexpr (TopField) {
  407. other_line = (y + 1) * out_luma_stride;
  408. } else {
  409. other_line = (y - 1) * out_luma_stride;
  410. }
  411. std::memcpy(&slot_surface[other_line], &slot_surface[dst],
  412. out_luma_width * sizeof(Pixel));
  413. }
  414. };
  415. switch (slot.config.deinterlace_mode) {
  416. case DXVAHD_DEINTERLACE_MODE_PRIVATE::WEAVE:
  417. // Due to the fact that we do not write to memory in nvdec, we cannot use Weave as it
  418. // relies on the previous frame.
  419. DecodeBobField();
  420. break;
  421. case DXVAHD_DEINTERLACE_MODE_PRIVATE::BOB_FIELD:
  422. DecodeBobField();
  423. break;
  424. case DXVAHD_DEINTERLACE_MODE_PRIVATE::DISI1:
  425. // Due to the fact that we do not write to memory in nvdec, we cannot use DISI1 as it
  426. // relies on previous/next frames.
  427. DecodeBobField();
  428. break;
  429. default:
  430. UNIMPLEMENTED_MSG("Deinterlace mode {} not implemented!",
  431. static_cast<s32>(slot.config.deinterlace_mode.Value()));
  432. break;
  433. }
  434. };
  435. DecodeLinear();
  436. }
  437. template <bool Planar>
  438. void Vic::ReadY8__V8U8_N420(const SlotStruct& slot, std::span<const PlaneOffsets> offsets,
  439. std::shared_ptr<const FFmpeg::Frame> frame) {
  440. switch (slot.config.frame_format) {
  441. case DXVAHD_FRAME_FORMAT::PROGRESSIVE:
  442. ReadProgressiveY8__V8U8_N420<Planar>(slot, offsets, std::move(frame));
  443. break;
  444. case DXVAHD_FRAME_FORMAT::TOP_FIELD:
  445. ReadInterlacedY8__V8U8_N420<Planar, true>(slot, offsets, std::move(frame));
  446. break;
  447. case DXVAHD_FRAME_FORMAT::BOTTOM_FIELD:
  448. ReadInterlacedY8__V8U8_N420<Planar, false>(slot, offsets, std::move(frame));
  449. break;
  450. default:
  451. LOG_ERROR(HW_GPU, "Unknown deinterlace format {}",
  452. static_cast<s32>(slot.config.frame_format.Value()));
  453. break;
  454. }
  455. }
  456. void Vic::Blend(const ConfigStruct& config, const SlotStruct& slot) {
  457. constexpr auto add_one([](u32 v) -> u32 { return v != 0 ? v + 1 : 0; });
  458. auto source_left{add_one(static_cast<u32>(slot.config.source_rect_left.Value()))};
  459. auto source_right{add_one(static_cast<u32>(slot.config.source_rect_right.Value()))};
  460. auto source_top{add_one(static_cast<u32>(slot.config.source_rect_top.Value()))};
  461. auto source_bottom{add_one(static_cast<u32>(slot.config.source_rect_bottom.Value()))};
  462. const auto dest_left{add_one(static_cast<u32>(slot.config.dest_rect_left.Value()))};
  463. const auto dest_right{add_one(static_cast<u32>(slot.config.dest_rect_right.Value()))};
  464. const auto dest_top{add_one(static_cast<u32>(slot.config.dest_rect_top.Value()))};
  465. const auto dest_bottom{add_one(static_cast<u32>(slot.config.dest_rect_bottom.Value()))};
  466. auto rect_left{add_one(config.output_config.target_rect_left.Value())};
  467. auto rect_right{add_one(config.output_config.target_rect_right.Value())};
  468. auto rect_top{add_one(config.output_config.target_rect_top.Value())};
  469. auto rect_bottom{add_one(config.output_config.target_rect_bottom.Value())};
  470. rect_left = std::max(rect_left, dest_left);
  471. rect_right = std::min(rect_right, dest_right);
  472. rect_top = std::max(rect_top, dest_top);
  473. rect_bottom = std::min(rect_bottom, dest_bottom);
  474. source_left = std::max(source_left, rect_left);
  475. source_right = std::min(source_right, rect_right);
  476. source_top = std::max(source_top, rect_top);
  477. source_bottom = std::min(source_bottom, rect_bottom);
  478. if (source_left >= source_right || source_top >= source_bottom) {
  479. return;
  480. }
  481. const auto out_surface_width{config.output_surface_config.out_surface_width + 1};
  482. [[maybe_unused]] const auto out_surface_height{config.output_surface_config.out_surface_height +
  483. 1};
  484. const auto in_surface_width{slot.surface_config.slot_surface_width + 1};
  485. source_bottom = std::min(source_bottom, out_surface_height);
  486. source_right = std::min(source_right, out_surface_width);
  487. // TODO Alpha blending. No games I've seen use more than a single surface or supply an alpha
  488. // below max, so it's ignored for now.
  489. if (!slot.color_matrix.matrix_enable) {
  490. const auto copy_width = std::min(source_right - source_left, rect_right - rect_left);
  491. for (u32 y = source_top; y < source_bottom; y++) {
  492. const auto dst_line = y * out_surface_width;
  493. const auto src_line = y * in_surface_width;
  494. std::memcpy(&output_surface[dst_line + rect_left],
  495. &slot_surface[src_line + source_left], copy_width * sizeof(Pixel));
  496. }
  497. } else {
  498. // clang-format off
  499. // Colour conversion is enabled, this is a 3x4 * 4x1 matrix multiplication, resulting in a 3x1 matrix.
  500. // | r0c0 r0c1 r0c2 r0c3 | | R | | R |
  501. // | r1c0 r1c1 r1c2 r1c3 | * | G | = | G |
  502. // | r2c0 r2c1 r2c2 r2c3 | | B | | B |
  503. // | 1 |
  504. // clang-format on
  505. [[maybe_unused]] auto DecodeLinear = [&]() {
  506. const auto r0c0 = static_cast<s32>(slot.color_matrix.matrix_coeff00.Value());
  507. const auto r0c1 = static_cast<s32>(slot.color_matrix.matrix_coeff01.Value());
  508. const auto r0c2 = static_cast<s32>(slot.color_matrix.matrix_coeff02.Value());
  509. const auto r0c3 = static_cast<s32>(slot.color_matrix.matrix_coeff03.Value());
  510. const auto r1c0 = static_cast<s32>(slot.color_matrix.matrix_coeff10.Value());
  511. const auto r1c1 = static_cast<s32>(slot.color_matrix.matrix_coeff11.Value());
  512. const auto r1c2 = static_cast<s32>(slot.color_matrix.matrix_coeff12.Value());
  513. const auto r1c3 = static_cast<s32>(slot.color_matrix.matrix_coeff13.Value());
  514. const auto r2c0 = static_cast<s32>(slot.color_matrix.matrix_coeff20.Value());
  515. const auto r2c1 = static_cast<s32>(slot.color_matrix.matrix_coeff21.Value());
  516. const auto r2c2 = static_cast<s32>(slot.color_matrix.matrix_coeff22.Value());
  517. const auto r2c3 = static_cast<s32>(slot.color_matrix.matrix_coeff23.Value());
  518. const auto shift = static_cast<s32>(slot.color_matrix.matrix_r_shift.Value());
  519. const auto clamp_min = static_cast<s32>(slot.config.soft_clamp_low.Value());
  520. const auto clamp_max = static_cast<s32>(slot.config.soft_clamp_high.Value());
  521. auto MatMul = [&](const Pixel& in_pixel) -> std::tuple<s32, s32, s32, s32> {
  522. auto r = static_cast<s32>(in_pixel.r);
  523. auto g = static_cast<s32>(in_pixel.g);
  524. auto b = static_cast<s32>(in_pixel.b);
  525. r = in_pixel.r * r0c0 + in_pixel.g * r0c1 + in_pixel.b * r0c2;
  526. g = in_pixel.r * r1c0 + in_pixel.g * r1c1 + in_pixel.b * r1c2;
  527. b = in_pixel.r * r2c0 + in_pixel.g * r2c1 + in_pixel.b * r2c2;
  528. r >>= shift;
  529. g >>= shift;
  530. b >>= shift;
  531. r += r0c3;
  532. g += r1c3;
  533. b += r2c3;
  534. r >>= 8;
  535. g >>= 8;
  536. b >>= 8;
  537. return {r, g, b, static_cast<s32>(in_pixel.a)};
  538. };
  539. for (u32 y = source_top; y < source_bottom; y++) {
  540. const auto src{y * in_surface_width + source_left};
  541. const auto dst{y * out_surface_width + rect_left};
  542. for (u32 x = source_left; x < source_right; x++) {
  543. auto [r, g, b, a] = MatMul(slot_surface[src + x]);
  544. r = std::clamp(r, clamp_min, clamp_max);
  545. g = std::clamp(g, clamp_min, clamp_max);
  546. b = std::clamp(b, clamp_min, clamp_max);
  547. a = std::clamp(a, clamp_min, clamp_max);
  548. output_surface[dst + x] = {static_cast<u16>(r), static_cast<u16>(g),
  549. static_cast<u16>(b), static_cast<u16>(a)};
  550. }
  551. }
  552. };
  553. #if defined(ARCHITECTURE_x86_64)
  554. if (!has_sse41) {
  555. DecodeLinear();
  556. return;
  557. }
  558. #endif
  559. #if defined(ARCHITECTURE_x86_64) || defined(ARCHITECTURE_arm64)
  560. // Fill the columns, e.g
  561. // c0 = [00 00 00 00] [r2c0 r2c0 r2c0 r2c0] [r1c0 r1c0 r1c0 r1c0] [r0c0 r0c0 r0c0 r0c0]
  562. const auto c0 = _mm_set_epi32(0, static_cast<s32>(slot.color_matrix.matrix_coeff20.Value()),
  563. static_cast<s32>(slot.color_matrix.matrix_coeff10.Value()),
  564. static_cast<s32>(slot.color_matrix.matrix_coeff00.Value()));
  565. const auto c1 = _mm_set_epi32(0, static_cast<s32>(slot.color_matrix.matrix_coeff21.Value()),
  566. static_cast<s32>(slot.color_matrix.matrix_coeff11.Value()),
  567. static_cast<s32>(slot.color_matrix.matrix_coeff01.Value()));
  568. const auto c2 = _mm_set_epi32(0, static_cast<s32>(slot.color_matrix.matrix_coeff22.Value()),
  569. static_cast<s32>(slot.color_matrix.matrix_coeff12.Value()),
  570. static_cast<s32>(slot.color_matrix.matrix_coeff02.Value()));
  571. const auto c3 = _mm_set_epi32(0, static_cast<s32>(slot.color_matrix.matrix_coeff23.Value()),
  572. static_cast<s32>(slot.color_matrix.matrix_coeff13.Value()),
  573. static_cast<s32>(slot.color_matrix.matrix_coeff03.Value()));
  574. // Set the matrix right-shift as a single element.
  575. const auto shift =
  576. _mm_set_epi32(0, 0, 0, static_cast<s32>(slot.color_matrix.matrix_r_shift.Value()));
  577. // Set every 16-bit value to the soft clamp values for clamping every 16-bit channel.
  578. const auto clamp_min = _mm_set1_epi16(static_cast<u16>(slot.config.soft_clamp_low.Value()));
  579. const auto clamp_max =
  580. _mm_set1_epi16(static_cast<u16>(slot.config.soft_clamp_high.Value()));
  581. // clang-format off
  582. auto MatMul = [](__m128i& p, const __m128i& col0, const __m128i& col1, const __m128i& col2,
  583. const __m128i& col3, const __m128i& trm_shift) -> __m128i {
  584. // Duplicate the 32-bit channels, e.g
  585. // p = [AA AA AA AA] [BB BB BB BB] [GG GG GG GG] [RR RR RR RR]
  586. // ->
  587. // r = [RR4 RR4 RR4 RR4] [RR3 RR3 RR3 RR3] [RR2 RR2 RR2 RR2] [RR1 RR1 RR1 RR1]
  588. auto r = _mm_shuffle_epi32(p, 0x0);
  589. auto g = _mm_shuffle_epi32(p, 0x55);
  590. auto b = _mm_shuffle_epi32(p, 0xAA);
  591. // Multiply the rows and columns c0 * r, c1 * g, c2 * b, e.g
  592. // r = [RR4 RR4 RR4 RR4] [ RR3 RR3 RR3 RR3] [ RR2 RR2 RR2 RR2] [ RR1 RR1 RR1 RR1]
  593. // *
  594. // c0 = [ 00 00 00 00] [r2c0 r2c0 r2c0 r2c0] [r1c0 r1c0 r1c0 r1c0] [r0c0 r0c0 r0c0 r0c0]
  595. r = _mm_mullo_epi32(r, col0);
  596. g = _mm_mullo_epi32(g, col1);
  597. b = _mm_mullo_epi32(b, col2);
  598. // Add them all together vertically, such that the 32-bit element
  599. // out[0] = (r[0] * c0[0]) + (g[0] * c1[0]) + (b[0] * c2[0])
  600. auto out = _mm_add_epi32(_mm_add_epi32(r, g), b);
  601. // Shift the result by r_shift, as the TRM says
  602. out = _mm_sra_epi32(out, trm_shift);
  603. // Add the final column. Because the 4x1 matrix has this row as 1, there's no need to
  604. // multiply by it, and as per the TRM this column ignores r_shift, so it's just added
  605. // here after shifting.
  606. out = _mm_add_epi32(out, col3);
  607. // Shift the result back from S12.8 to integer values
  608. return _mm_srai_epi32(out, 8);
  609. };
  610. for (u32 y = source_top; y < source_bottom; y++) {
  611. const auto src{y * in_surface_width + source_left};
  612. const auto dst{y * out_surface_width + rect_left};
  613. for (u32 x = source_left; x < source_right; x += 8) {
  614. // clang-format off
  615. // Prefetch the next iteration's memory
  616. _mm_prefetch((const char*)&slot_surface[src + x + 8], _MM_HINT_T0);
  617. // Load in pixels
  618. // p01 = [AA AA] [BB BB] [GG GG] [RR RR] [AA AA] [BB BB] [GG GG] [RR RR]
  619. auto p01 = _mm_load_si128((__m128i*)&slot_surface[src + x + 0]);
  620. auto p23 = _mm_load_si128((__m128i*)&slot_surface[src + x + 2]);
  621. auto p45 = _mm_load_si128((__m128i*)&slot_surface[src + x + 4]);
  622. auto p67 = _mm_load_si128((__m128i*)&slot_surface[src + x + 6]);
  623. // Convert the 16-bit channels into 32-bit (unsigned), as the matrix values are
  624. // 32-bit and to avoid overflow.
  625. // p01 = [AA2 AA2] [BB2 BB2] [GG2 GG2] [RR2 RR2] [AA1 AA1] [BB1 BB1] [GG1 GG1] [RR1 RR1]
  626. // ->
  627. // p01_lo = [001 001 AA1 AA1] [001 001 BB1 BB1] [001 001 GG1 GG1] [001 001 RR1 RR1]
  628. // p01_hi = [002 002 AA2 AA2] [002 002 BB2 BB2] [002 002 GG2 GG2] [002 002 RR2 RR2]
  629. auto p01_lo = _mm_cvtepu16_epi32(p01);
  630. auto p01_hi = _mm_cvtepu16_epi32(_mm_srli_si128(p01, 8));
  631. auto p23_lo = _mm_cvtepu16_epi32(p23);
  632. auto p23_hi = _mm_cvtepu16_epi32(_mm_srli_si128(p23, 8));
  633. auto p45_lo = _mm_cvtepu16_epi32(p45);
  634. auto p45_hi = _mm_cvtepu16_epi32(_mm_srli_si128(p45, 8));
  635. auto p67_lo = _mm_cvtepu16_epi32(p67);
  636. auto p67_hi = _mm_cvtepu16_epi32(_mm_srli_si128(p67, 8));
  637. // Matrix multiply the pixel, doing the colour conversion.
  638. auto out0 = MatMul(p01_lo, c0, c1, c2, c3, shift);
  639. auto out1 = MatMul(p01_hi, c0, c1, c2, c3, shift);
  640. auto out2 = MatMul(p23_lo, c0, c1, c2, c3, shift);
  641. auto out3 = MatMul(p23_hi, c0, c1, c2, c3, shift);
  642. auto out4 = MatMul(p45_lo, c0, c1, c2, c3, shift);
  643. auto out5 = MatMul(p45_hi, c0, c1, c2, c3, shift);
  644. auto out6 = MatMul(p67_lo, c0, c1, c2, c3, shift);
  645. auto out7 = MatMul(p67_hi, c0, c1, c2, c3, shift);
  646. // Pack the 32-bit channel pixels back into 16-bit using unsigned saturation
  647. // out0 = [001 001 AA1 AA1] [001 001 BB1 BB1] [001 001 GG1 GG1] [001 001 RR1 RR1]
  648. // out1 = [002 002 AA2 AA2] [002 002 BB2 BB2] [002 002 GG2 GG2] [002 002 RR2 RR2]
  649. // ->
  650. // done0 = [AA2 AA2] [BB2 BB2] [GG2 GG2] [RR2 RR2] [AA1 AA1] [BB1 BB1] [GG1 GG1] [RR1 RR1]
  651. auto done0 = _mm_packus_epi32(out0, out1);
  652. auto done1 = _mm_packus_epi32(out2, out3);
  653. auto done2 = _mm_packus_epi32(out4, out5);
  654. auto done3 = _mm_packus_epi32(out6, out7);
  655. // Blend the original alpha back into the pixel, as the matrix multiply gives us a
  656. // 3-channel output, not 4.
  657. // 0x88 = b10001000, taking RGB from the first argument, A from the second argument.
  658. // done0 = [002 002] [BB2 BB2] [GG2 GG2] [RR2 RR2] [001 001] [BB1 BB1] [GG1 GG1] [RR1 RR1]
  659. // ->
  660. // done0 = [AA2 AA2] [BB2 BB2] [GG2 GG2] [RR2 RR2] [AA1 AA1] [BB1 BB1] [GG1 GG1] [RR1 RR1]
  661. done0 = _mm_blend_epi16(done0, p01, 0x88);
  662. done1 = _mm_blend_epi16(done1, p23, 0x88);
  663. done2 = _mm_blend_epi16(done2, p45, 0x88);
  664. done3 = _mm_blend_epi16(done3, p67, 0x88);
  665. // Clamp the 16-bit channels to the soft-clamp min/max.
  666. done0 = _mm_max_epu16(done0, clamp_min);
  667. done1 = _mm_max_epu16(done1, clamp_min);
  668. done2 = _mm_max_epu16(done2, clamp_min);
  669. done3 = _mm_max_epu16(done3, clamp_min);
  670. done0 = _mm_min_epu16(done0, clamp_max);
  671. done1 = _mm_min_epu16(done1, clamp_max);
  672. done2 = _mm_min_epu16(done2, clamp_max);
  673. done3 = _mm_min_epu16(done3, clamp_max);
  674. // Store the pixels to the output surface.
  675. _mm_store_si128((__m128i*)&output_surface[dst + x + 0], done0);
  676. _mm_store_si128((__m128i*)&output_surface[dst + x + 2], done1);
  677. _mm_store_si128((__m128i*)&output_surface[dst + x + 4], done2);
  678. _mm_store_si128((__m128i*)&output_surface[dst + x + 6], done3);
  679. }
  680. }
  681. // clang-format on
  682. #else
  683. DecodeLinear();
  684. #endif
  685. }
  686. }
  687. void Vic::WriteY8__V8U8_N420(const OutputSurfaceConfig& output_surface_config) {
  688. constexpr u32 BytesPerPixel = 1;
  689. auto surface_width{output_surface_config.out_surface_width + 1};
  690. auto surface_height{output_surface_config.out_surface_height + 1};
  691. const auto surface_stride{surface_width};
  692. const auto out_luma_width = output_surface_config.out_luma_width + 1;
  693. const auto out_luma_height = output_surface_config.out_luma_height + 1;
  694. const auto out_luma_stride = Common::AlignUp(out_luma_width * BytesPerPixel, 0x10);
  695. const auto out_luma_size = out_luma_height * out_luma_stride;
  696. const auto out_chroma_width = output_surface_config.out_chroma_width + 1;
  697. const auto out_chroma_height = output_surface_config.out_chroma_height + 1;
  698. const auto out_chroma_stride = Common::AlignUp(out_chroma_width * BytesPerPixel * 2, 0x10);
  699. const auto out_chroma_size = out_chroma_height * out_chroma_stride;
  700. surface_width = std::min(surface_width, out_luma_width);
  701. surface_height = std::min(surface_height, out_luma_height);
  702. [[maybe_unused]] auto DecodeLinear = [&](std::span<u8> out_luma, std::span<u8> out_chroma) {
  703. for (u32 y = 0; y < surface_height; ++y) {
  704. const auto src_luma = y * surface_stride;
  705. const auto dst_luma = y * out_luma_stride;
  706. const auto src_chroma = y * surface_stride;
  707. const auto dst_chroma = (y / 2) * out_chroma_stride;
  708. for (u32 x = 0; x < surface_width; x += 2) {
  709. out_luma[dst_luma + x + 0] =
  710. static_cast<u8>(output_surface[src_luma + x + 0].r >> 2);
  711. out_luma[dst_luma + x + 1] =
  712. static_cast<u8>(output_surface[src_luma + x + 1].r >> 2);
  713. out_chroma[dst_chroma + x + 0] =
  714. static_cast<u8>(output_surface[src_chroma + x].g >> 2);
  715. out_chroma[dst_chroma + x + 1] =
  716. static_cast<u8>(output_surface[src_chroma + x].b >> 2);
  717. }
  718. }
  719. };
  720. auto Decode = [&](std::span<u8> out_luma, std::span<u8> out_chroma) {
  721. #if defined(ARCHITECTURE_x86_64)
  722. if (!has_sse41) {
  723. DecodeLinear(out_luma, out_chroma);
  724. return;
  725. }
  726. #endif
  727. #if defined(ARCHITECTURE_x86_64) || defined(ARCHITECTURE_arm64)
  728. // luma_mask = [00 00] [00 00] [00 00] [FF FF] [00 00] [00 00] [00 00] [FF FF]
  729. const auto luma_mask = _mm_set_epi16(0, 0, 0, -1, 0, 0, 0, -1);
  730. const auto sse_aligned_width = Common::AlignDown(surface_width, 16);
  731. for (u32 y = 0; y < surface_height; ++y) {
  732. const auto src = y * surface_stride;
  733. const auto dst_luma = y * out_luma_stride;
  734. const auto dst_chroma = (y / 2) * out_chroma_stride;
  735. u32 x = 0;
  736. for (; x < sse_aligned_width; x += 16) {
  737. // clang-format off
  738. // Prefetch the next cache lines, 2 per iteration
  739. _mm_prefetch((const char*)&output_surface[src + x + 16], _MM_HINT_T0);
  740. _mm_prefetch((const char*)&output_surface[src + x + 24], _MM_HINT_T0);
  741. // Load the 64-bit pixels, 2 per variable.
  742. auto pixel01 = _mm_load_si128((__m128i*)&output_surface[src + x + 0]);
  743. auto pixel23 = _mm_load_si128((__m128i*)&output_surface[src + x + 2]);
  744. auto pixel45 = _mm_load_si128((__m128i*)&output_surface[src + x + 4]);
  745. auto pixel67 = _mm_load_si128((__m128i*)&output_surface[src + x + 6]);
  746. auto pixel89 = _mm_load_si128((__m128i*)&output_surface[src + x + 8]);
  747. auto pixel1011 = _mm_load_si128((__m128i*)&output_surface[src + x + 10]);
  748. auto pixel1213 = _mm_load_si128((__m128i*)&output_surface[src + x + 12]);
  749. auto pixel1415 = _mm_load_si128((__m128i*)&output_surface[src + x + 14]);
  750. // Split out the luma of each pixel using the luma_mask above.
  751. // pixel01 = [AA2 AA2] [VV2 VV2] [UU2 UU2] [LL2 LL2] [AA1 AA1] [VV1 VV1] [UU1 UU1] [LL1 LL1]
  752. // ->
  753. // l01 = [002 002] [002 002] [002 002] [LL2 LL2] [001 001] [001 001] [001 001] [LL1 LL1]
  754. auto l01 = _mm_and_si128(pixel01, luma_mask);
  755. auto l23 = _mm_and_si128(pixel23, luma_mask);
  756. auto l45 = _mm_and_si128(pixel45, luma_mask);
  757. auto l67 = _mm_and_si128(pixel67, luma_mask);
  758. auto l89 = _mm_and_si128(pixel89, luma_mask);
  759. auto l1011 = _mm_and_si128(pixel1011, luma_mask);
  760. auto l1213 = _mm_and_si128(pixel1213, luma_mask);
  761. auto l1415 = _mm_and_si128(pixel1415, luma_mask);
  762. // Pack 32-bit elements from 2 registers down into 16-bit elements in 1 register.
  763. // l01 = [002 002 002 002] [002 002 LL2 LL2] [001 001 001 001] [001 001 LL1 LL1]
  764. // l23 = [004 004 004 004] [004 004 LL4 LL4] [003 003 003 003] [003 003 LL3 LL3]
  765. // ->
  766. // l0123 = [004 004] [LL4 LL4] [003 003] [LL3 LL3] [002 002] [LL2 LL2] [001 001] [LL1 LL1]
  767. auto l0123 = _mm_packus_epi32(l01, l23);
  768. auto l4567 = _mm_packus_epi32(l45, l67);
  769. auto l891011 = _mm_packus_epi32(l89, l1011);
  770. auto l12131415 = _mm_packus_epi32(l1213, l1415);
  771. // Pack 32-bit elements from 2 registers down into 16-bit elements in 1 register.
  772. // l0123 = [004 004 LL4 LL4] [003 003 LL3 LL3] [002 002 LL2 LL2] [001 001 LL1 LL1]
  773. // l4567 = [008 008 LL8 LL8] [007 007 LL7 LL7] [006 006 LL6 LL6] [005 005 LL5 LL5]
  774. // ->
  775. // luma_lo = [LL8 LL8] [LL7 LL7] [LL6 LL6] [LL5 LL5] [LL4 LL4] [LL3 LL3] [LL2 LL2] [LL1 LL1]
  776. auto luma_lo = _mm_packus_epi32(l0123, l4567);
  777. auto luma_hi = _mm_packus_epi32(l891011, l12131415);
  778. // Right-shift the 16-bit elements by 2, un-doing the left shift by 2 on read
  779. // and bringing the range back to 8-bit.
  780. luma_lo = _mm_srli_epi16(luma_lo, 2);
  781. luma_hi = _mm_srli_epi16(luma_hi, 2);
  782. // Pack with unsigned saturation the 16-bit values in 2 registers into 8-bit values in 1 register.
  783. // luma_lo = [LL8 LL8] [LL7 LL7] [LL6 LL6] [LL5 LL5] [LL4 LL4] [LL3 LL3] [LL2 LL2] [LL1 LL1]
  784. // luma_hi = [LL16 LL16] [LL15 LL15] [LL14 LL14] [LL13 LL13] [LL12 LL12] [LL11 LL11] [LL10 LL10] [LL9 LL9]
  785. // ->
  786. // luma = [LL16] [LL15] [LL14] [LL13] [LL12] [LL11] [LL10] [LL9] [LL8] [LL7] [LL6] [LL5] [LL4] [LL3] [LL2] [LL1]
  787. auto luma = _mm_packus_epi16(luma_lo, luma_hi);
  788. // Store the 16 bytes of luma
  789. _mm_store_si128((__m128i*)&out_luma[dst_luma + x], luma);
  790. if (y % 2 == 0) {
  791. // Chroma, done every other line as it's half the height of luma.
  792. // Shift the register right by 2 bytes (not bits), to kick out the 16-bit luma.
  793. // We can do this instead of &'ing a mask and then shifting.
  794. // pixel01 = [AA2 AA2] [VV2 VV2] [UU2 UU2] [LL2 LL2] [AA1 AA1] [VV1 VV1] [UU1 UU1] [LL1 LL1]
  795. // ->
  796. // c01 = [ 00 00] [AA2 AA2] [VV2 VV2] [UU2 UU2] [LL2 LL2] [AA1 AA1] [VV1 VV1] [UU1 UU1]
  797. auto c01 = _mm_srli_si128(pixel01, 2);
  798. auto c23 = _mm_srli_si128(pixel23, 2);
  799. auto c45 = _mm_srli_si128(pixel45, 2);
  800. auto c67 = _mm_srli_si128(pixel67, 2);
  801. auto c89 = _mm_srli_si128(pixel89, 2);
  802. auto c1011 = _mm_srli_si128(pixel1011, 2);
  803. auto c1213 = _mm_srli_si128(pixel1213, 2);
  804. auto c1415 = _mm_srli_si128(pixel1415, 2);
  805. // Interleave the lower 8 bytes as 32-bit elements from 2 registers into 1 register.
  806. // This has the effect of skipping every other chroma value horitonally,
  807. // notice the high pixels UU2/UU4 are skipped.
  808. // This is intended as N420 chroma width is half the luma width.
  809. // c01 = [ 00 00 AA2 AA2] [VV2 VV2 UU2 UU2] [LL2 LL2 AA1 AA1] [VV1 VV1 UU1 UU1]
  810. // c23 = [ 00 00 AA4 AA4] [VV4 VV4 UU4 UU4] [LL4 LL4 AA3 AA3] [VV3 VV3 UU3 UU3]
  811. // ->
  812. // c0123 = [LL4 LL4 AA3 AA3] [LL2 LL2 AA1 AA1] [VV3 VV3 UU3 UU3] [VV1 VV1 UU1 UU1]
  813. auto c0123 = _mm_unpacklo_epi32(c01, c23);
  814. auto c4567 = _mm_unpacklo_epi32(c45, c67);
  815. auto c891011 = _mm_unpacklo_epi32(c89, c1011);
  816. auto c12131415 = _mm_unpacklo_epi32(c1213, c1415);
  817. // Interleave the low 64-bit elements from 2 registers into 1.
  818. // c0123 = [LL4 LL4 AA3 AA3 LL2 LL2 AA1 AA1] [VV3 VV3 UU3 UU3 VV1 VV1 UU1 UU1]
  819. // c4567 = [LL8 LL8 AA7 AA7 LL6 LL6 AA5 AA5] [VV7 VV7 UU7 UU7 VV5 VV5 UU5 UU5]
  820. // ->
  821. // chroma_lo = [VV7 VV7 UU7 UU7 VV5 VV5 UU5 UU5] [VV3 VV3 UU3 UU3 VV1 VV1 UU1 UU1]
  822. auto chroma_lo = _mm_unpacklo_epi64(c0123, c4567);
  823. auto chroma_hi = _mm_unpacklo_epi64(c891011, c12131415);
  824. // Right-shift the 16-bit elements by 2, un-doing the left shift by 2 on read
  825. // and bringing the range back to 8-bit.
  826. chroma_lo = _mm_srli_epi16(chroma_lo, 2);
  827. chroma_hi = _mm_srli_epi16(chroma_hi, 2);
  828. // Pack with unsigned saturation the 16-bit elements from 2 registers into 8-bit elements in 1 register.
  829. // chroma_lo = [ VV7 VV7] [ UU7 UU7] [ VV5 VV5] [ UU5 UU5] [ VV3 VV3] [ UU3 UU3] [VV1 VV1] [UU1 UU1]
  830. // chroma_hi = [VV15 VV15] [UU15 UU15] [VV13 VV13] [UU13 UU13] [VV11 VV11] [UU11 UU11] [VV9 VV9] [UU9 UU9]
  831. // ->
  832. // chroma = [VV15] [UU15] [VV13] [UU13] [VV11] [UU11] [VV9] [UU9] [VV7] [UU7] [VV5] [UU5] [VV3] [UU3] [VV1] [UU1]
  833. auto chroma = _mm_packus_epi16(chroma_lo, chroma_hi);
  834. // Store the 16 bytes of chroma.
  835. _mm_store_si128((__m128i*)&out_chroma[dst_chroma + x + 0], chroma);
  836. }
  837. // clang-format on
  838. }
  839. const auto src_chroma = y * surface_stride;
  840. for (; x < surface_width; x += 2) {
  841. out_luma[dst_luma + x + 0] = static_cast<u8>(output_surface[src + x + 0].r >> 2);
  842. out_luma[dst_luma + x + 1] = static_cast<u8>(output_surface[src + x + 1].r >> 2);
  843. out_chroma[dst_chroma + x + 0] =
  844. static_cast<u8>(output_surface[src_chroma + x].g >> 2);
  845. out_chroma[dst_chroma + x + 1] =
  846. static_cast<u8>(output_surface[src_chroma + x].b >> 2);
  847. }
  848. }
  849. #else
  850. DecodeLinear(out_luma, out_chroma);
  851. #endif
  852. };
  853. switch (output_surface_config.out_block_kind) {
  854. case BLK_KIND::GENERIC_16Bx2: {
  855. const u32 block_height = static_cast<u32>(output_surface_config.out_block_height);
  856. const auto out_luma_swizzle_size = Texture::CalculateSize(
  857. true, BytesPerPixel, out_luma_width, out_luma_height, 1, block_height, 0);
  858. const auto out_chroma_swizzle_size = Texture::CalculateSize(
  859. true, BytesPerPixel * 2, out_chroma_width, out_chroma_height, 1, block_height, 0);
  860. LOG_TRACE(
  861. HW_GPU,
  862. "Writing Y8__V8U8_N420 swizzled frame\n"
  863. "\tinput surface {}x{} stride {} size 0x{:X}\n"
  864. "\toutput luma {}x{} stride {} size 0x{:X} block height {} swizzled size 0x{:X}\n",
  865. "\toutput chroma {}x{} stride {} size 0x{:X} block height {} swizzled size 0x{:X}",
  866. surface_width, surface_height, surface_stride * BytesPerPixel,
  867. surface_stride * surface_height * BytesPerPixel, out_luma_width, out_luma_height,
  868. out_luma_stride, out_luma_size, block_height, out_luma_swizzle_size, out_chroma_width,
  869. out_chroma_height, out_chroma_stride, out_chroma_size, block_height,
  870. out_chroma_swizzle_size);
  871. luma_scratch.resize_destructive(out_luma_size);
  872. chroma_scratch.resize_destructive(out_chroma_size);
  873. Decode(luma_scratch, chroma_scratch);
  874. Tegra::Memory::GpuGuestMemoryScoped<u8, Core::Memory::GuestMemoryFlags::SafeWrite> out_luma(
  875. memory_manager, regs.output_surface.luma.Address(), out_luma_swizzle_size,
  876. &swizzle_scratch);
  877. if (block_height == 1) {
  878. SwizzleSurface(out_luma, out_luma_stride, luma_scratch, out_luma_stride,
  879. out_luma_height);
  880. } else {
  881. Texture::SwizzleTexture(out_luma, luma_scratch, BytesPerPixel, out_luma_width,
  882. out_luma_height, 1, block_height, 0, 1);
  883. }
  884. Tegra::Memory::GpuGuestMemoryScoped<u8, Core::Memory::GuestMemoryFlags::SafeWrite>
  885. out_chroma(memory_manager, regs.output_surface.chroma_u.Address(),
  886. out_chroma_swizzle_size, &swizzle_scratch);
  887. if (block_height == 1) {
  888. SwizzleSurface(out_chroma, out_chroma_stride, chroma_scratch, out_chroma_stride,
  889. out_chroma_height);
  890. } else {
  891. Texture::SwizzleTexture(out_chroma, chroma_scratch, BytesPerPixel, out_chroma_width,
  892. out_chroma_height, 1, block_height, 0, 1);
  893. }
  894. } break;
  895. case BLK_KIND::PITCH: {
  896. LOG_TRACE(
  897. HW_GPU,
  898. "Writing Y8__V8U8_N420 swizzled frame\n"
  899. "\tinput surface {}x{} stride {} size 0x{:X}\n"
  900. "\toutput luma {}x{} stride {} size 0x{:X} block height {} swizzled size 0x{:X}\n",
  901. "\toutput chroma {}x{} stride {} size 0x{:X} block height {} swizzled size 0x{:X}",
  902. surface_width, surface_height, surface_stride * BytesPerPixel,
  903. surface_stride * surface_height * BytesPerPixel, out_luma_width, out_luma_height,
  904. out_luma_stride, out_luma_size, out_chroma_width, out_chroma_height, out_chroma_stride,
  905. out_chroma_size);
  906. // Unfortunately due to a driver bug or game bug, the chroma address can be not
  907. // appropriately spaced from the luma, so the luma of size out_stride * height runs into the
  908. // top of the chroma buffer. Unfortunately that removes an optimisation here where we could
  909. // create guest spans and decode into game memory directly to avoid the memory copy from
  910. // scratch to game. Due to this bug, we must write the luma first, and then the chroma
  911. // afterwards to re-overwrite the luma being too large.
  912. luma_scratch.resize_destructive(out_luma_size);
  913. chroma_scratch.resize_destructive(out_chroma_size);
  914. Decode(luma_scratch, chroma_scratch);
  915. memory_manager.WriteBlock(regs.output_surface.luma.Address(), luma_scratch.data(),
  916. out_luma_size);
  917. memory_manager.WriteBlock(regs.output_surface.chroma_u.Address(), chroma_scratch.data(),
  918. out_chroma_size);
  919. } break;
  920. default:
  921. UNREACHABLE();
  922. break;
  923. }
  924. }
  925. template <VideoPixelFormat Format>
  926. void Vic::WriteABGR(const OutputSurfaceConfig& output_surface_config) {
  927. constexpr u32 BytesPerPixel = 4;
  928. auto surface_width{output_surface_config.out_surface_width + 1};
  929. auto surface_height{output_surface_config.out_surface_height + 1};
  930. const auto surface_stride{surface_width};
  931. const auto out_luma_width = output_surface_config.out_luma_width + 1;
  932. const auto out_luma_height = output_surface_config.out_luma_height + 1;
  933. const auto out_luma_stride = Common ::AlignUp(out_luma_width * BytesPerPixel, 0x10);
  934. const auto out_luma_size = out_luma_height * out_luma_stride;
  935. surface_width = std::min(surface_width, out_luma_width);
  936. surface_height = std::min(surface_height, out_luma_height);
  937. [[maybe_unused]] auto DecodeLinear = [&](std::span<u8> out_buffer) {
  938. for (u32 y = 0; y < surface_height; y++) {
  939. const auto src = y * surface_stride;
  940. const auto dst = y * out_luma_stride;
  941. for (u32 x = 0; x < surface_width; x++) {
  942. if constexpr (Format == VideoPixelFormat::A8R8G8B8) {
  943. out_buffer[dst + x * 4 + 0] = static_cast<u8>(output_surface[src + x].b >> 2);
  944. out_buffer[dst + x * 4 + 1] = static_cast<u8>(output_surface[src + x].g >> 2);
  945. out_buffer[dst + x * 4 + 2] = static_cast<u8>(output_surface[src + x].r >> 2);
  946. out_buffer[dst + x * 4 + 3] = static_cast<u8>(output_surface[src + x].a >> 2);
  947. } else {
  948. out_buffer[dst + x * 4 + 0] = static_cast<u8>(output_surface[src + x].r >> 2);
  949. out_buffer[dst + x * 4 + 1] = static_cast<u8>(output_surface[src + x].g >> 2);
  950. out_buffer[dst + x * 4 + 2] = static_cast<u8>(output_surface[src + x].b >> 2);
  951. out_buffer[dst + x * 4 + 3] = static_cast<u8>(output_surface[src + x].a >> 2);
  952. }
  953. }
  954. }
  955. };
  956. auto Decode = [&](std::span<u8> out_buffer) {
  957. #if defined(ARCHITECTURE_x86_64)
  958. if (!has_sse41) {
  959. DecodeLinear(out_buffer);
  960. return;
  961. }
  962. #endif
  963. #if defined(ARCHITECTURE_x86_64) || defined(ARCHITECTURE_arm64)
  964. constexpr size_t SseAlignment = 16;
  965. const auto sse_aligned_width = Common::AlignDown(surface_width, SseAlignment);
  966. for (u32 y = 0; y < surface_height; y++) {
  967. const auto src = y * surface_stride;
  968. const auto dst = y * out_luma_stride;
  969. u32 x = 0;
  970. for (; x < sse_aligned_width; x += SseAlignment) {
  971. // clang-format off
  972. // Prefetch the next 2 cache lines
  973. _mm_prefetch((const char*)&output_surface[src + x + 16], _MM_HINT_T0);
  974. _mm_prefetch((const char*)&output_surface[src + x + 24], _MM_HINT_T0);
  975. // Load the pixels, 16-bit channels, 8 bytes per pixel, e.g
  976. // pixel01 = [AA AA BB BB GG GG RR RR AA AA BB BB GG GG RR RR
  977. auto pixel01 = _mm_load_si128((__m128i*)&output_surface[src + x + 0]);
  978. auto pixel23 = _mm_load_si128((__m128i*)&output_surface[src + x + 2]);
  979. auto pixel45 = _mm_load_si128((__m128i*)&output_surface[src + x + 4]);
  980. auto pixel67 = _mm_load_si128((__m128i*)&output_surface[src + x + 6]);
  981. auto pixel89 = _mm_load_si128((__m128i*)&output_surface[src + x + 8]);
  982. auto pixel1011 = _mm_load_si128((__m128i*)&output_surface[src + x + 10]);
  983. auto pixel1213 = _mm_load_si128((__m128i*)&output_surface[src + x + 12]);
  984. auto pixel1415 = _mm_load_si128((__m128i*)&output_surface[src + x + 14]);
  985. // Right-shift the channels by 16 to un-do the left shit on read and bring the range
  986. // back to 8-bit.
  987. pixel01 = _mm_srli_epi16(pixel01, 2);
  988. pixel23 = _mm_srli_epi16(pixel23, 2);
  989. pixel45 = _mm_srli_epi16(pixel45, 2);
  990. pixel67 = _mm_srli_epi16(pixel67, 2);
  991. pixel89 = _mm_srli_epi16(pixel89, 2);
  992. pixel1011 = _mm_srli_epi16(pixel1011, 2);
  993. pixel1213 = _mm_srli_epi16(pixel1213, 2);
  994. pixel1415 = _mm_srli_epi16(pixel1415, 2);
  995. // Pack with unsigned saturation 16-bit channels from 2 registers into 8-bit channels in 1 register.
  996. // pixel01 = [AA2 AA2] [BB2 BB2] [GG2 GG2] [RR2 RR2] [AA1 AA1] [BB1 BB1] [GG1 GG1] [RR1 RR1]
  997. // pixel23 = [AA4 AA4] [BB4 BB4] [GG4 GG4] [RR4 RR4] [AA3 AA3] [BB3 BB3] [GG3 GG3] [RR3 RR3]
  998. // ->
  999. // pixels0_lo = [AA4] [BB4] [GG4] [RR4] [AA3] [BB3] [GG3] [RR3] [AA2] [BB2] [GG2] [RR2] [AA1] [BB1] [GG1] [RR1]
  1000. auto pixels0_lo = _mm_packus_epi16(pixel01, pixel23);
  1001. auto pixels0_hi = _mm_packus_epi16(pixel45, pixel67);
  1002. auto pixels1_lo = _mm_packus_epi16(pixel89, pixel1011);
  1003. auto pixels1_hi = _mm_packus_epi16(pixel1213, pixel1415);
  1004. if constexpr (Format == VideoPixelFormat::A8R8G8B8) {
  1005. const auto shuffle =
  1006. _mm_set_epi8(15, 12, 13, 14, 11, 8, 9, 10, 7, 4, 5, 6, 3, 0, 1, 2);
  1007. // Our pixels are ABGR (big-endian) by default, if ARGB is needed, we need to shuffle.
  1008. // pixels0_lo = [AA4 BB4 GG4 RR4] [AA3 BB3 GG3 RR3] [AA2 BB2 GG2 RR2] [AA1 BB1 GG1 RR1]
  1009. // ->
  1010. // pixels0_lo = [AA4 RR4 GG4 BB4] [AA3 RR3 GG3 BB3] [AA2 RR2 GG2 BB2] [AA1 RR1 GG1 BB1]
  1011. pixels0_lo = _mm_shuffle_epi8(pixels0_lo, shuffle);
  1012. pixels0_hi = _mm_shuffle_epi8(pixels0_hi, shuffle);
  1013. pixels1_lo = _mm_shuffle_epi8(pixels1_lo, shuffle);
  1014. pixels1_hi = _mm_shuffle_epi8(pixels1_hi, shuffle);
  1015. }
  1016. // Store the pixels
  1017. _mm_store_si128((__m128i*)&out_buffer[dst + x * 4 + 0], pixels0_lo);
  1018. _mm_store_si128((__m128i*)&out_buffer[dst + x * 4 + 16], pixels0_hi);
  1019. _mm_store_si128((__m128i*)&out_buffer[dst + x * 4 + 32], pixels1_lo);
  1020. _mm_store_si128((__m128i*)&out_buffer[dst + x * 4 + 48], pixels1_hi);
  1021. // clang-format on
  1022. }
  1023. for (; x < surface_width; x++) {
  1024. if constexpr (Format == VideoPixelFormat::A8R8G8B8) {
  1025. out_buffer[dst + x * 4 + 0] = static_cast<u8>(output_surface[src + x].b >> 2);
  1026. out_buffer[dst + x * 4 + 1] = static_cast<u8>(output_surface[src + x].g >> 2);
  1027. out_buffer[dst + x * 4 + 2] = static_cast<u8>(output_surface[src + x].r >> 2);
  1028. out_buffer[dst + x * 4 + 3] = static_cast<u8>(output_surface[src + x].a >> 2);
  1029. } else {
  1030. out_buffer[dst + x * 4 + 0] = static_cast<u8>(output_surface[src + x].r >> 2);
  1031. out_buffer[dst + x * 4 + 1] = static_cast<u8>(output_surface[src + x].g >> 2);
  1032. out_buffer[dst + x * 4 + 2] = static_cast<u8>(output_surface[src + x].b >> 2);
  1033. out_buffer[dst + x * 4 + 3] = static_cast<u8>(output_surface[src + x].a >> 2);
  1034. }
  1035. }
  1036. }
  1037. #else
  1038. DecodeLinear(out_buffer);
  1039. #endif
  1040. };
  1041. switch (output_surface_config.out_block_kind) {
  1042. case BLK_KIND::GENERIC_16Bx2: {
  1043. const u32 block_height = static_cast<u32>(output_surface_config.out_block_height);
  1044. const auto out_swizzle_size = Texture::CalculateSize(true, BytesPerPixel, out_luma_width,
  1045. out_luma_height, 1, block_height, 0);
  1046. LOG_TRACE(
  1047. HW_GPU,
  1048. "Writing ABGR swizzled frame\n"
  1049. "\tinput surface {}x{} stride {} size 0x{:X}\n"
  1050. "\toutput surface {}x{} stride {} size 0x{:X} block height {} swizzled size 0x{:X}",
  1051. surface_width, surface_height, surface_stride * BytesPerPixel,
  1052. surface_stride * surface_height * BytesPerPixel, out_luma_width, out_luma_height,
  1053. out_luma_stride, out_luma_size, block_height, out_swizzle_size);
  1054. luma_scratch.resize_destructive(out_luma_size);
  1055. Decode(luma_scratch);
  1056. Tegra::Memory::GpuGuestMemoryScoped<u8, Core::Memory::GuestMemoryFlags::SafeWrite> out_luma(
  1057. memory_manager, regs.output_surface.luma.Address(), out_swizzle_size, &swizzle_scratch);
  1058. if (block_height == 1) {
  1059. SwizzleSurface(out_luma, out_luma_stride, luma_scratch, out_luma_stride,
  1060. out_luma_height);
  1061. } else {
  1062. Texture::SwizzleTexture(out_luma, luma_scratch, BytesPerPixel, out_luma_width,
  1063. out_luma_height, 1, block_height, 0, 1);
  1064. }
  1065. } break;
  1066. case BLK_KIND::PITCH: {
  1067. LOG_TRACE(HW_GPU,
  1068. "Writing ABGR pitch frame\n"
  1069. "\tinput surface {}x{} stride {} size 0x{:X}"
  1070. "\toutput surface {}x{} stride {} size 0x{:X}",
  1071. surface_width, surface_height, surface_stride,
  1072. surface_stride * surface_height * BytesPerPixel, out_luma_width, out_luma_height,
  1073. out_luma_stride, out_luma_size);
  1074. luma_scratch.resize_destructive(out_luma_size);
  1075. Tegra::Memory::GpuGuestMemoryScoped<u8, Core::Memory::GuestMemoryFlags::SafeWrite> out_luma(
  1076. memory_manager, regs.output_surface.luma.Address(), out_luma_size, &luma_scratch);
  1077. Decode(out_luma);
  1078. } break;
  1079. default:
  1080. UNREACHABLE();
  1081. break;
  1082. }
  1083. }
  1084. } // namespace Tegra::Host1x