foxygit / fake-08-xbox360 Log in
commit 59df35b7430831f5531a83a63e22dc5deaa57a29
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Sun Sep 27 16:01:33 2020 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Sun Sep 27 16:01:33 2020 -0600

    Small optimizations

    I think there has been some performance regression with compatibility
    improvements, and I don't think these tweaks make up all the ground, but
    they seem to get close. Use bitwise operators when getting pixel nibbels
    and only get palette color array once.
---
 platform/3ds/source/3dsHost.cpp       | 50 +++++++++++++++++++++++++----------
 platform/switch/source/SwitchHost.cpp | 10 ++++---
 source/host.h                         |  4 +--
 source/hostVmShared.h                 |  6 +++++
 source/main.cpp                       | 10 +++----
 source/nibblehelpers.cpp              | 10 ++++---
 test/Makefile                         |  8 ++++--
 test/nibblehelperstests.cpp           | 20 ++++++++++++++
 8 files changed, 89 insertions(+), 29 deletions(-)

diff --git a/platform/3ds/source/3dsHost.cpp b/platform/3ds/source/3dsHost.cpp
index 348acc0..624d82f 100644
--- a/platform/3ds/source/3dsHost.cpp
+++ b/platform/3ds/source/3dsHost.cpp
@@ -15,6 +15,7 @@ namespace fs = std::filesystem;
 #include "../../../source/host.h"
 #include "../../../source/hostVmShared.h"
 #include "../../../source/nibblehelpers.h"
+#include "../../../source/PicoRam.h"

 #define SCREEN_WIDTH 400;
 #define SCREEN_HEIGHT 240;
@@ -36,8 +37,10 @@ const int __3ds_BottomScreenHeight = SCREEN_2_HEIGHT;
 const int PicoScreenWidth = 128;
 const int PicoScreenHeight = 128;

+const int PicoFbLength = 128 * 64;

-StretchOption stretch = StretchAndOverflow;
+
+StretchOption stretch = PixelPerfect;
 u64 last_time;
 u64 now_time;
 u64 frame_time;
@@ -46,6 +49,9 @@ double targetFrameTimeMs;
 u32 currKDown;
 u32 currKHeld;

+Color* _paletteColors;
+Bgr24Col _bgrColors[16];
+
 uint8_t ConvertInputToP8(u32 input){
 	uint8_t result = 0;
 	if (input & KEY_LEFT){
@@ -172,7 +178,7 @@ void audioSetup(){
 Host::Host() { }


-void Host::oneTimeSetup(){
+void Host::oneTimeSetup(Color* paletteColors){
     osSetSpeedupEnable(true);

     audioSetup();
@@ -183,6 +189,15 @@ void Host::oneTimeSetup(){
     now_time = 0;
     frame_time = 0;
     targetFrameTimeMs = 0;
+
+    _paletteColors = paletteColors;
+    for(int i = 0; i < 16; i++){
+        _bgrColors[i] = {
+            _paletteColors[i].Blue,
+            _paletteColors[i].Green,
+            _paletteColors[i].Red
+        };
+    }
 }

 void Host::oneTimeCleanup(){
@@ -251,7 +266,7 @@ void Host::waitForTargetFps(){
 }


-void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteColors){
+void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap){
     int bgcolor = 0;
 	uint8_t* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
 	//clear whole top framebuffer
@@ -267,17 +282,24 @@ void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteC
 	if (stretch == PixelPerfect) {
 		int xOffset = __3ds_TopScreenWidth / 2 - PicoScreenWidth / 2;
         int yOffset = __3ds_TopScreenHeight / 2 - PicoScreenHeight / 2;
-        //todo: test if it is faster to convert colors to uint24_ts and write one instead of 3 (assuming these are )
-        for(x = 0; x < 128; x++) {
+
+       for(x = 0; x < 64; x++) {
             for(y = 0; y < 128; y++) {
-                uint8_t c = getPixelNibble(x, y, picoFb);
-                Color col = paletteColors[screenPaletteMap[c]];
+                int x1 = x << 1;
+                int x2 = x1 + 1;
+                uint8_t lc = getPixelNibble(x1, y, picoFb);
+                Bgr24Col lcol = _bgrColors[screenPaletteMap[lc]];

-                int pixIdx = (((x + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)))*3;
+                int pixIdx = (((x1 + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)));

-                fb[pixIdx + 0] = col.Blue;
-                fb[pixIdx + 1] = col.Green;
-                fb[pixIdx + 2] = col.Red;
+                ((Bgr24Col*)fb)[pixIdx] = lcol;
+
+                uint8_t rc = getPixelNibble(x2, y, picoFb);
+                Bgr24Col rcol = _bgrColors[screenPaletteMap[rc]];
+
+                pixIdx = (((x2 + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)));
+
+                ((Bgr24Col*)fb)[pixIdx] = rcol;
             }
         }
 	}
@@ -293,7 +315,7 @@ void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteC
                 int picoX = (int)(x / ratio);
                 int picoY = (int)(y / ratio);
                 uint8_t c = getPixelNibble(picoX, picoY, picoFb);
-                Color col = paletteColors[screenPaletteMap[c]];
+                Color col = _paletteColors[screenPaletteMap[c]];

                 int pixIdx = (((x + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)))*3;

@@ -317,7 +339,7 @@ void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteC
                 int picoX = (int)(x / ratio);
                 int picoY = (int)(y / ratio);
                 uint8_t c = getPixelNibble(picoX, picoY, picoFb);
-                Color col = paletteColors[screenPaletteMap[c]];
+                Color col = _paletteColors[screenPaletteMap[c]];

                 int pixIdx = (((x + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)))*3;

@@ -337,7 +359,7 @@ void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteC
                 int picoX = (int)(x / ratio);
                 int picoY = (int)((y + __3ds_TopScreenHeight) / ratio);
                 uint8_t c = getPixelNibble(picoX, picoY, picoFb);
-                Color col = paletteColors[screenPaletteMap[c]];
+                Color col = _paletteColors[screenPaletteMap[c]];

                 int pixIdx = (((x + xOffset)*__3ds_BottomScreenHeight)+ ((__3ds_BottomScreenHeight - 1) - (y + yOffset)))*3;

diff --git a/platform/switch/source/SwitchHost.cpp b/platform/switch/source/SwitchHost.cpp
index 6ca09fd..e52adae 100644
--- a/platform/switch/source/SwitchHost.cpp
+++ b/platform/switch/source/SwitchHost.cpp
@@ -39,6 +39,8 @@ u32 currKHeld;

 Framebuffer fb;

+Color* _paletteColors;
+
 uint8_t ConvertInputToP8(u32 input){
 	uint8_t result = 0;
 	if (input & KEY_LEFT){
@@ -167,7 +169,7 @@ void audioSetup(){
 Host::Host() { }


-void Host::oneTimeSetup(){
+void Host::oneTimeSetup(Color* paletteColors){

     audioSetup();

@@ -180,6 +182,8 @@ void Host::oneTimeSetup(){
     now_time = 0;
     frame_time = 0;
     targetFrameTimeMs = 0;
+
+    _paletteColors = paletteColors;
 }

 void Host::oneTimeCleanup(){
@@ -248,7 +252,7 @@ void Host::waitForTargetFps(){
 }


-void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteColors){
+void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap){
 	u32 stride;
     u32* framebuf = (u32*) framebufferBegin(&fb, &stride);

@@ -277,7 +281,7 @@ void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteC
             int picoY = (int)(y / ratio);
             uint8_t c = getPixelNibble(picoX, picoY, picoFb);
             //uint8_t c = picoFb[x*128 + y];
-            Color col = paletteColors[screenPaletteMap[c]];
+            Color col = _paletteColors[screenPaletteMap[c]];

             u32 pos = (yOffset + y) * stride / sizeof(u32) + (xOffset + x);
             framebuf[pos] = RGBA8_MAXALPHA(col.Red, col.Green, col.Blue);
diff --git a/source/host.h b/source/host.h
index 2f5e957..6eaa8ec 100644
--- a/source/host.h
+++ b/source/host.h
@@ -16,7 +16,7 @@ class Host {
     public:
     Host();

-    void oneTimeSetup();
+    void oneTimeSetup(Color* paletteColors);

     void setTargetFps(int targetFps);

@@ -32,7 +32,7 @@ class Host {

     void waitForTargetFps();

-    void drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteColors);
+    void drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap);

     bool shouldFillAudioBuff();
     void* getAudioBufferPointer();
diff --git a/source/hostVmShared.h b/source/hostVmShared.h
index 2e40471..0bda3e6 100644
--- a/source/hostVmShared.h
+++ b/source/hostVmShared.h
@@ -21,3 +21,9 @@ struct Color {
 	uint8_t Blue;
 	uint8_t Alpha;
 };
+
+struct Bgr24Col {
+	uint8_t Blue;
+	uint8_t Green;
+	uint8_t Red;
+};
diff --git a/source/main.cpp b/source/main.cpp
index a3e0193..54d19af 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -16,10 +16,11 @@ int main(int argc, char* argv[])
 {
 	Logger::Initialize();

-	Host *host = new Host();
-	host->oneTimeSetup();
-
 	Vm *vm = new Vm();
+
+	Host *host = new Host();
+	host->oneTimeSetup(vm->GetPaletteColors());
+
 	Logger::Write("initialized Vm and host\n");

 	Logger::Write("Setting cart list on vm\n");
@@ -52,9 +53,8 @@ int main(int argc, char* argv[])

 		uint8_t* picoFb = vm->GetPicoInteralFb();
 		uint8_t* screenPaletteMap = vm->GetScreenPaletteMap();
-		Color* paletteColors = vm->GetPaletteColors();

-		host->drawFrame(picoFb, screenPaletteMap, paletteColors);
+		host->drawFrame(picoFb, screenPaletteMap);

 		if (host->shouldFillAudioBuff()) {
 			vm->FillAudioBuffer(host->getAudioBufferPointer(), 0, host->getAudioBufferSize());
diff --git a/source/nibblehelpers.cpp b/source/nibblehelpers.cpp
index 44718a8..02b265a 100644
--- a/source/nibblehelpers.cpp
+++ b/source/nibblehelpers.cpp
@@ -1,17 +1,21 @@

 #include <string>

+#include "hostVmShared.h"
 #include "nibblehelpers.h"

 int getCombinedIdx(int x, int y){
-    return y * 64 + (x / 2);
+	//bit shifting might be faster? trying to optimize
+    //return y * 64 + (x / 2);
+	return (y << 6) + (x >> 1);
 }

 void setPixelNibble(int x, int y, uint8_t value, uint8_t* targetBuffer) {
     int combinedIdx = getCombinedIdx(x, y);

 	uint8_t currentByte = targetBuffer[combinedIdx];
-	uint8_t mask = x % 2 == 0 ? 0x0f : 0xf0;
+	//get mask for even or odd
+	uint8_t mask = (BITMASK(0) & x) == 0 ? 0x0f : 0xf0;
 	value = mask == 0xf0 ? value << 4 : value;

 	targetBuffer[combinedIdx] = (currentByte & ~mask) | (value & mask);
@@ -22,7 +26,7 @@ uint8_t getPixelNibble(int x, int y, uint8_t* targetBuffer) {

 	uint8_t combinedPix = targetBuffer[combinedIdx];

-	uint8_t c = x % 2 == 0
+	uint8_t c = (BITMASK(0) & x) == 0
 		? combinedPix & 0x0f //just first 4 bits
 		: combinedPix >> 4;  //just last 4 bits

diff --git a/test/Makefile b/test/Makefile
index cc72cea..6b3077e 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -7,17 +7,18 @@ all: runtests


 testrunner.a: testroot.o \
-	fontdatatests.o graphicstests.o audiotests.o cartpatchertests.o \
+	fontdatatests.o nibblehelperstests.o graphicstests.o audiotests.o cartpatchertests.o \
 	carttests.o endtoendtests.o vmtests.o
 	g++ -I$(CURDIR)/include -L$(CURDIR)/lib \
 	testroot.o \
 	fontdata.o fontdatatests.o \
-	graphics.o graphicstests.o stringToDataHelpers.o nibblehelpers.o \
+	graphics.o graphicstests.o stringToDataHelpers.o \
 	Audio.o synth.o audiotests.o \
 	cartPatcher.o cartpatchertests.o \
 	cart.o carttests.o nooplogger.o lodepng.o emojiconversion.o filehelpers.o utf8-util.o \
 	vm.o endtoendtests.o picoluaapi.o Input.o \
 	vmtests.o \
+	nibblehelpers.o nibblehelperstests.o \
 	-llua -ldl \
 	-o testrunner.a

@@ -27,6 +28,9 @@ testroot.o: testroot.cpp doctest.h
 fontdatatests.o: fontdatatests.cpp ../source/fontdata.cpp ../source/fontdata.h
 	g++ $(CFLAGS) fontdatatests.cpp ../source/fontdata.cpp

+nibblehelperstests.o: nibblehelperstests.cpp ../source/nibblehelpers.cpp ../source/nibblehelpers.h
+	g++ $(CFLAGS) nibblehelperstests.cpp ../source/nibblehelpers.cpp
+
 graphicstests.o: fontdatatests.o graphicstests.cpp ../source/graphics.cpp ../source/graphics.h \
 	../source/fontdata.cpp ../source/fontdata.h \
 	../source/PicoRam.h ../source/hostVmShared.h \
diff --git a/test/nibblehelperstests.cpp b/test/nibblehelperstests.cpp
new file mode 100644
index 0000000..ebd4179
--- /dev/null
+++ b/test/nibblehelperstests.cpp
@@ -0,0 +1,20 @@
+#include "doctest.h"
+#include "../source/nibblehelpers.h"
+
+TEST_CASE("Get Combined pixel index") {
+    SUBCASE("0,0") {
+        CHECK_EQ(getCombinedIdx(0, 0), 0);
+    }
+    SUBCASE("1,0") {
+        CHECK_EQ(getCombinedIdx(1, 0), 0);
+    }
+    SUBCASE("127, 127") {
+        CHECK_EQ(getCombinedIdx(127, 127), 8191);
+    }
+    SUBCASE("126, 127") {
+        CHECK_EQ(getCombinedIdx(127, 127), 8191);
+    }
+    SUBCASE("37, 99") {
+        CHECK_EQ(getCombinedIdx(37, 99), 6354);
+    }
+}