From dedff4fd921b4bf32a68575f44b6c66fa3ad0746 Mon Sep 17 00:00:00 2001 From: Archez Date: Thu, 25 Apr 2024 18:00:27 -0400 Subject: [PATCH] add byteswap support to read framebuffer (#512) --- include/libultraship/libultra/gbi.h | 16 ++++++++-------- src/graphic/Fast3D/gfx_pc.cpp | 11 +++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/libultraship/libultra/gbi.h b/include/libultraship/libultra/gbi.h index 56b783e..1d2bae3 100644 --- a/include/libultraship/libultra/gbi.h +++ b/include/libultraship/libultra/gbi.h @@ -2680,14 +2680,14 @@ typedef union { } // Read the framebuffer's texture to a cpu memory location as RGBA16 -#define gDPReadFB(pkt, src, rgba16buf, ulx, uly, width, height) \ - { \ - Gfx *_g0 = (Gfx*)(pkt), *_g1 = (Gfx*)(pkt); \ - \ - _g0->words.w0 = _SHIFTL(G_READFB, 24, 8) | _SHIFTL(src, 0, 8); \ - _g0->words.w1 = (uintptr_t)rgba16buf; \ - _g1->words.w0 = _SHIFTL(uly, 16, 16) | _SHIFTL(ulx, 0, 16); \ - _g1->words.w1 = _SHIFTL(height, 16, 16) | _SHIFTL(width, 0, 16); \ +#define gDPReadFB(pkt, src, rgba16buf, ulx, uly, width, height, bswap) \ + { \ + Gfx *_g0 = (Gfx*)(pkt), *_g1 = (Gfx*)(pkt); \ + \ + _g0->words.w0 = _SHIFTL(G_READFB, 24, 8) | _SHIFTL(bswap, 8, 1) | _SHIFTL(src, 0, 8); \ + _g0->words.w1 = (uintptr_t)rgba16buf; \ + _g1->words.w0 = _SHIFTL(uly, 16, 16) | _SHIFTL(ulx, 0, 16); \ + _g1->words.w1 = _SHIFTL(height, 16, 16) | _SHIFTL(width, 0, 16); \ } #define gDPImageRectangle(pkt, x0, y0, s0, t0, x1, y1, s1, t1, tile, iw, ih) \ diff --git a/src/graphic/Fast3D/gfx_pc.cpp b/src/graphic/Fast3D/gfx_pc.cpp index 635a0d9..e7df37c 100644 --- a/src/graphic/Fast3D/gfx_pc.cpp +++ b/src/graphic/Fast3D/gfx_pc.cpp @@ -3306,6 +3306,7 @@ bool gfx_read_fb_handler_custom(Gfx** cmd0) { int32_t width, height, ulx, uly; uint16_t* rgba16Buffer = (uint16_t*)cmd->words.w1; int fbId = C0(0, 8); + bool bswap = C0(8, 1); ++(*cmd0); cmd = *cmd0; // Specifying the upper left origin value is unused and unsupported at the renderer level @@ -3316,6 +3317,16 @@ bool gfx_read_fb_handler_custom(Gfx** cmd0) { gfx_flush(); gfx_rapi->read_framebuffer_to_cpu(fbId, width, height, rgba16Buffer); + +#ifndef IS_BIGENDIAN + // byteswap the output to BE + if (bswap) { + for (size_t i = 0; i < width * height; i++) { + rgba16Buffer[i] = BE16SWAP(rgba16Buffer[i]); + } + } +#endif + return false; }