foxygit / fake-08-xbox360 Log in
commit 151270045bb3cb28dee1ab0b98ec691853a429ef
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Wed Apr 21 19:21:26 2021 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Wed Apr 21 19:21:26 2021 -0600

    building with citro2d rendering

    But displaying rotated and garbled
---
 platform/3ds/Makefile           |   4 +-
 platform/3ds/source/3dsHost.cpp | 266 +++++++++++++---------------------------
 2 files changed, 85 insertions(+), 185 deletions(-)

diff --git a/platform/3ds/Makefile b/platform/3ds/Makefile
index 5acc410..017346d 100644
--- a/platform/3ds/Makefile
+++ b/platform/3ds/Makefile
@@ -66,7 +66,7 @@ CC = $(CXX)

 ARCH	:=	-march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft

-CFLAGS	:=	-g -Wall -Ofast -mword-relocations \
+CFLAGS	:=	-g -Wall -O2 -mword-relocations \
 			-fomit-frame-pointer -ffunction-sections \
 			$(ARCH)

@@ -77,7 +77,7 @@ CXXFLAGS	:= $(CFLAGS) -fno-rtti -fexceptions -std=gnu++11
 ASFLAGS	:=	-g $(ARCH)
 LDFLAGS	=	-specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)

-LIBS	:= -lctru -lm
+LIBS	:= -lcitro2d -lcitro3d -lctru -lm

 #---------------------------------------------------------------------------------
 # list of directories containing libraries, this must be the top level containing
diff --git a/platform/3ds/source/3dsHost.cpp b/platform/3ds/source/3dsHost.cpp
index 6e708a3..cab9464 100644
--- a/platform/3ds/source/3dsHost.cpp
+++ b/platform/3ds/source/3dsHost.cpp
@@ -1,4 +1,8 @@
 #include <3ds.h>
+#include <citro3d.h>
+#include <citro2d.h>
+
+
 #include <stdio.h>
 #include <string.h>
 #include <dirent.h>
@@ -37,7 +41,6 @@ const int PicoScreenHeight = 128;

 const int PicoFbLength = 128 * 64;

-int numFramesToClear;
 u64 last_time;
 u64 now_time;
 u64 frame_time;
@@ -53,6 +56,24 @@ Color* _paletteColors;
 uint16_t _rgb565Colors[144];
 Audio* _audio;

+static C2D_Image pico_image;
+
+C3D_Tex *pico_tex;
+Tex3DS_SubTexture *pico_subtex;
+
+C3D_RenderTarget* topTarget;
+C3D_RenderTarget* bottomTarget;
+
+u16* pico_pixel_buffer;
+
+const GPU_TEXCOLOR texColor = GPU_RGB565;
+
+#define CLEAR_COLOR 0xFF000000
+#define BYTES_PER_PIXEL 2
+size_t pico_rgba_buffer_size = 128*128*BYTES_PER_PIXEL;
+
+u32 clrRec1;
+
 uint8_t ConvertInputToP8(u32 input){
 	uint8_t result = 0;
 	if (input & KEY_LEFT){
@@ -90,11 +111,6 @@ uint8_t ConvertInputToP8(u32 input){
 	return result;
 }

-void postFlipFunction(){
-    gfxFlushBuffers();
-	gfxSwapBuffers();
-	gspWaitForVBlank();
-}


 void init_fill_buffer(void *audioBuffer,size_t offset, size_t size) {
@@ -217,6 +233,12 @@ void Host::oneTimeSetup(Color* paletteColors, Audio* audio){
     audioSetup();

     gfxInitDefault();
+    C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
+	C2D_Init(C2D_DEFAULT_MAX_OBJECTS);
+	C2D_Prepare();
+
+    topTarget = C2D_CreateScreenTarget(GFX_TOP, GFX_LEFT);
+    bottomTarget = C2D_CreateScreenTarget(GFX_BOTTOM, GFX_LEFT);

     last_time = 0;
     now_time = 0;
@@ -228,11 +250,27 @@ void Host::oneTimeSetup(Color* paletteColors, Audio* audio){
         _rgb565Colors[i] = (((_paletteColors[i].Red & 0xf8)<<8) + ((_paletteColors[i].Green & 0xfc)<<3)+(_paletteColors[i].Blue>>3));
     }

-    //set to RGB565 for 2 bytes per pixel instead of 3
-    gfxSetScreenFormat(GFX_TOP, GSP_RGB565_OES);
-    gfxSetScreenFormat(GFX_BOTTOM, GSP_RGB565_OES);
+    pico_tex = (C3D_Tex*)linearAlloc(sizeof(C3D_Tex));
+	//people on homebrew discord said should use this
+	C3D_TexInitVRAM(pico_tex, 128, 128, texColor);
+	//can't tell a difference, but I'd expect vram to be better? maybe not if transferring often
+	//C3D_TexInit(pico_tex, 128, 128, GPU_RGBA8);
+	C3D_TexSetFilter(pico_tex, GPU_NEAREST, GPU_NEAREST);
+
+	pico_subtex = (Tex3DS_SubTexture*)linearAlloc(sizeof(Tex3DS_SubTexture));
+	pico_subtex->width = 128;
+	pico_subtex->height = 128;
+	pico_subtex->left = 0.0f;
+	pico_subtex->top = 1.0f;
+	pico_subtex->right = 1.0f;
+	pico_subtex->bottom = 0.0f;
+
+	pico_image.tex = pico_tex;
+	pico_image.subtex = pico_subtex;

-    numFramesToClear = 2;
+	pico_pixel_buffer = (u16*)linearAlloc(pico_rgba_buffer_size);
+
+    clrRec1 = C2D_Color32(0x9A, 0x6C, 0xB9, 0xFF);

     loadSettingsIni();

@@ -255,6 +293,14 @@ void Host::oneTimeCleanup(){

     audioCleanup();

+    C3D_TexDelete(pico_tex);
+
+	linearFree(pico_tex);
+	linearFree(pico_subtex);
+    linearFree(pico_pixel_buffer);
+
+    C2D_Fini();
+	C3D_Fini();
 	gfxExit();
 }

@@ -300,7 +346,6 @@ void Host::changeStretch(){
             scaleY = 0.53;
         }

-        numFramesToClear = 2;
     }
 }

@@ -360,190 +405,45 @@ void Host::waitForTargetFps(){


 void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap){
-
-	uint16_t* fb = (uint16_t*)gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
+    int pixIdx = 0, x = 0, y = 0;
+    for(x = 0; x < 64; x++) {
+        for(y = 0; y < 128; y++) {
+            int x1 = x << 1;
+            int x2 = x1 + 1;
+            uint8_t lc = getPixelNibble(x1, y, picoFb);
+            uint16_t lcol = _rgb565Colors[screenPaletteMap[lc]];

-	//bottom buffer in case overflow rendering is being used
-	uint16_t* fbb = (uint16_t*)gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
+            pico_pixel_buffer[pixIdx++] = lcol;

-    if (numFramesToClear > 0) {
-        //clear framebuffers so we don't leave pixels around
-        memset(fb, 0, __3ds_TopScreenWidth*__3ds_TopScreenHeight*2);
-        memset(fbb, 0, __3ds_BottomScreenWidth*__3ds_BottomScreenHeight*2);
-
-        numFramesToClear--;
-    }
+            uint8_t rc = getPixelNibble(x2, y, picoFb);
+            uint16_t rcol = _rgb565Colors[screenPaletteMap[rc]];

-    int x, y;
-
-    //these could be combined to shorten this method
-	if (stretch == PixelPerfect) {
-		int xOffset = __3ds_TopScreenWidth / 2 - PicoScreenWidth / 2;
-        int yOffset = __3ds_TopScreenHeight / 2 - PicoScreenHeight / 2;
-
-       for(x = 0; x < 64; x++) {
-            for(y = 0; y < 128; y++) {
-                int x1 = x << 1;
-                int x2 = x1 + 1;
-                uint8_t lc = getPixelNibble(x1, y, picoFb);
-                uint16_t lcol = _rgb565Colors[screenPaletteMap[lc]];
-
-                int pixIdx = (((x1 + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)));
-
-                fb[pixIdx] = lcol;
-
-                uint8_t rc = getPixelNibble(x2, y, picoFb);
-                uint16_t rcol = _rgb565Colors[screenPaletteMap[rc]];
-
-                pixIdx = (((x2 + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)));
-
-                fb[pixIdx] = rcol;
-            }
-        }
-	}
-	else if (stretch == StretchToFit) {
-		float ratio = (float)__3ds_TopScreenHeight / (float)PicoScreenHeight;
-        int stretchedWidth = PicoScreenWidth * ratio;
-
-        int xOffset = __3ds_TopScreenWidth / 2 - stretchedWidth / 2;
-        int yOffset = 0;
-
-        for(x = 0; x < stretchedWidth; x++) {
-            for(y = 0; y < __3ds_TopScreenHeight; y++) {
-                int picoX = (int)(x / ratio);
-                int picoY = (int)(y / ratio);
-                uint8_t c = getPixelNibble(picoX, picoY, picoFb);
-                uint16_t col = _rgb565Colors[screenPaletteMap[c]];
-
-                int pixIdx = (((x + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)));
-
-                fb[pixIdx] = col;
-            }
+            pico_pixel_buffer[pixIdx++] = rcol;
         }
-	}
-    else if (stretch == AltScreenPixelPerfect) {
-		int xOffset = __3ds_BottomScreenWidth / 2 - PicoScreenWidth / 2;
-        int yOffset = __3ds_BottomScreenHeight / 2 - PicoScreenHeight / 2;
+    }

-       for(x = 0; x < 64; x++) {
-            for(y = 0; y < 128; y++) {
-                int x1 = x << 1;
-                int x2 = x1 + 1;
-                uint8_t lc = getPixelNibble(x1, y, picoFb);
-                uint16_t lcol = _rgb565Colors[screenPaletteMap[lc]];
+    GSPGPU_FlushDataCache(pico_pixel_buffer, pico_rgba_buffer_size);

-                int pixIdx = (((x1 + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)));
+	C3D_SyncDisplayTransfer(
+        (u32*)pico_pixel_buffer, GX_BUFFER_DIM(128, 128),
+        (u32*)(pico_tex->data), GX_BUFFER_DIM(128, 128),
+		(GX_TRANSFER_FLIP_VERT(1) | GX_TRANSFER_OUT_TILED(1) | GX_TRANSFER_RAW_COPY(0) |
+        GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGB565) | GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGB565) |
+        GX_TRANSFER_SCALING(GX_TRANSFER_SCALE_NO))
+    );

-                fbb[pixIdx] = lcol;
+    C3D_FrameBegin(C3D_FRAME_SYNCDRAW);

-                uint8_t rc = getPixelNibble(x2, y, picoFb);
-                uint16_t rcol = _rgb565Colors[screenPaletteMap[rc]];
+		C2D_TargetClear(topTarget, CLEAR_COLOR);
+		C2D_SceneBegin(topTarget);

-                pixIdx = (((x2 + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)));
+		C2D_DrawRectangle(0, 128, 0, 64, 64, clrRec1, clrRec1, clrRec1, clrRec1);

-                fbb[pixIdx] = rcol;
-            }
-        }
-	}
-    else if (stretch == AltScreenStretch) {
-		float ratio = (float)__3ds_BottomScreenHeight / (float)PicoScreenHeight;
-        int stretchedWidth = PicoScreenWidth * ratio;
-
-        int xOffset = __3ds_BottomScreenWidth / 2 - stretchedWidth / 2;
-        int yOffset = 0;
-
-        for(x = 0; x < stretchedWidth; x++) {
-            for(y = 0; y < __3ds_TopScreenHeight; y++) {
-                int picoX = (int)(x / ratio);
-                int picoY = (int)(y / ratio);
-                uint8_t c = getPixelNibble(picoX, picoY, picoFb);
-                uint16_t col = _rgb565Colors[screenPaletteMap[c]];
-
-                int pixIdx = (((x + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)));
-
-                fbb[pixIdx] = col;
-            }
-        }
-	}
-	else {
-		//assume landscape, hardcoded double for now (3ds)
-        int ratio = 2;
-        int stretchedWidth = PicoScreenWidth * ratio;
-
-        int xOffset = __3ds_TopScreenWidth / 2 - stretchedWidth / 2;
-        int yOffset = 0;
-
-        //trying to optimize by looping over pico pixels, not 3ds pixels
-        for(x = 0; x < 64; x++) {
-            for(y = 0; y < 120; y++) {
-                int picox1 = x << 1;
-                int picox2 = picox1 + 1;
-                uint8_t lc = getPixelNibble(picox1, y, picoFb);
-                uint16_t lcol = _rgb565Colors[screenPaletteMap[lc]];
-
-                int pixIdx = (((picox1*2 + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y*2 + yOffset)));
-                int pixIdx2 = (((picox1*2 + xOffset + 1)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y*2 + yOffset)));
-                int pixIdx3 = (((picox1*2 + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y*2 + yOffset + 1)));
-                int pixIdx4 = (((picox1*2 + xOffset + 1)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y*2 + yOffset + 1)));
-
-                fb[pixIdx] = lcol;
-                fb[pixIdx2] = lcol;
-                fb[pixIdx3] = lcol;
-                fb[pixIdx4] = lcol;
-
-
-                uint8_t rc = getPixelNibble(picox2, y, picoFb);
-                uint16_t rcol = _rgb565Colors[screenPaletteMap[rc]];
-
-                pixIdx = (((picox2*2 + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y*2 + yOffset)));
-                pixIdx2 = (((picox2*2 + xOffset + 1)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y*2 + yOffset)));
-                pixIdx3 = (((picox2*2 + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y*2 + yOffset + 1)));
-                pixIdx4 = (((picox2*2 + xOffset + 1)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y*2 + yOffset + 1)));
-
-                fb[pixIdx] = rcol;
-                fb[pixIdx2] = rcol;
-                fb[pixIdx3] = rcol;
-                fb[pixIdx4] = rcol;
-            }
-        }
+		C2D_DrawImageAt(pico_image, 72, 0, .5, NULL, 2.0f, 2.0f);

-        xOffset = __3ds_BottomScreenWidth / 2 - stretchedWidth / 2;
-
-        for(x = 0; x < 64; x++) {
-            for(y = 0; y < 8; y++) {
-                int picox1 = x << 1;
-                int picox2 = picox1 + 1;
-                int picoY = 120 + y;
-                uint8_t lc = getPixelNibble(picox1, picoY, picoFb);
-                uint16_t lcol = _rgb565Colors[screenPaletteMap[lc]];
-
-                int pixIdx = (((picox1*2 + xOffset)*__3ds_BottomScreenHeight)+ ((__3ds_BottomScreenHeight - 1) - (y*2 + yOffset)));
-                int pixIdx2 = (((picox1*2 + xOffset + 1)*__3ds_BottomScreenHeight)+ ((__3ds_BottomScreenHeight - 1) - (y*2 + yOffset)));
-                int pixIdx3 = (((picox1*2 + xOffset)*__3ds_BottomScreenHeight)+ ((__3ds_BottomScreenHeight - 1) - (y*2 + yOffset + 1)));
-                int pixIdx4 = (((picox1*2 + xOffset + 1)*__3ds_BottomScreenHeight)+ ((__3ds_BottomScreenHeight - 1) - (y*2 + yOffset + 1)));
-
-                fbb[pixIdx] = lcol;
-                fbb[pixIdx2] = lcol;
-                fbb[pixIdx3] = lcol;
-                fbb[pixIdx4] = lcol;
-
-                uint8_t rc = getPixelNibble(picox2, picoY, picoFb);
-                uint16_t rcol = _rgb565Colors[screenPaletteMap[rc]];
-
-                pixIdx = (((picox2*2 + xOffset)*__3ds_BottomScreenHeight)+ ((__3ds_BottomScreenHeight - 1) - (y*2 + yOffset)));
-                pixIdx2 = (((picox2*2 + xOffset + 1)*__3ds_BottomScreenHeight)+ ((__3ds_BottomScreenHeight - 1) - (y*2 + yOffset)));
-                pixIdx3 = (((picox2*2 + xOffset)*__3ds_BottomScreenHeight)+ ((__3ds_BottomScreenHeight - 1) - (y*2 + yOffset + 1)));
-                pixIdx4 = (((picox2*2 + xOffset + 1)*__3ds_BottomScreenHeight)+ ((__3ds_BottomScreenHeight - 1) - (y*2 + yOffset + 1)));
-
-                fbb[pixIdx] = rcol;
-                fbb[pixIdx2] = rcol;
-                fbb[pixIdx3] = rcol;
-                fbb[pixIdx4] = rcol;
-            }
-        }
-	}
+		//C2D_Flush();

-    postFlipFunction();
+	C3D_FrameEnd(0);
 }

 bool Host::shouldFillAudioBuff(){