From 8f2c8771973c510f286c040ffec538d0c44be13c Mon Sep 17 00:00:00 2001 From: Tobi-D7 Date: Mon, 25 Jul 2022 11:31:41 +0200 Subject: [PATCH] Implement Benchm,arking System for BitmapPrinter --- .vscode/settings.json | 43 +++++++++++ BuildAndInstall.sh | 4 + include/renderd7/renderd7.hpp | 28 ++++++- source/renderd7.cpp | 141 +++++++++++++++++++++++++++++++--- 4 files changed, 206 insertions(+), 10 deletions(-) create mode 100644 .vscode/settings.json create mode 100755 BuildAndInstall.sh diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..272fbc3 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,43 @@ +{ + "files.associations": { + "array": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "numeric": "cpp", + "type_traits": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "optional": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "typeinfo": "cpp", + "utility": "cpp" + } +} \ No newline at end of file diff --git a/BuildAndInstall.sh b/BuildAndInstall.sh new file mode 100755 index 0000000..768e895 --- /dev/null +++ b/BuildAndInstall.sh @@ -0,0 +1,4 @@ +export DEVKITARM=/opt/devkitpro/devkitARM/ +export DEVKITPRO=/opt/devkitpro/ +make +make install \ No newline at end of file diff --git a/include/renderd7/renderd7.hpp b/include/renderd7/renderd7.hpp index 83ba7b9..d823825 100644 --- a/include/renderd7/renderd7.hpp +++ b/include/renderd7/renderd7.hpp @@ -433,10 +433,20 @@ namespace RenderD7 void SavePng(std::string name); void CreateScreen(C3D_RenderTarget *target); - void DrawScreen(int framerate); + void DrawScreenDirectF(int framerate); + void DrawScreenDirect(); + void DrawScreenF(int framerate); + void DrawScreen(); + void UpdateScreenF(int framerate); + void UpdateScreen(); void Clear(u8 b = 0, u8 g = 0, u8 r = 0, u8 a = 255); void ClearBlank(); RenderD7::Image GetImage(); + /// Test to Find out The Best Settings for BitmapPrinter + void Benchmark(); + /// Setup the Benchmark + /// \param framerate The Fps of the ScreenUpdates + void SetupBenchmark(int framerate); private: int frame = 0; RenderD7::Image renderframe; @@ -444,5 +454,21 @@ namespace RenderD7 C3D_RenderTarget* targetr; BMP bitmap = NULL; BMP blank = NULL; + //Benchmark Stuff; + bool benchmark = false; + bool setupbenchmark; + float frametime = 0; + uint64_t lastTime = 0; + float dtt = 0.f; + float timer = 0; + float mhdtt = 0; + + float fpsClock = 0.f; + int frameCounter = 0, fps = 0; + + std::vector hdttt; + std::vector fpscountc; + int renderedframes = 0; + int testfps = 60; }; } /// RenderD7 diff --git a/source/renderd7.cpp b/source/renderd7.cpp index 8330dff..fcc2990 100644 --- a/source/renderd7.cpp +++ b/source/renderd7.cpp @@ -1627,7 +1627,7 @@ void RenderD7::BitmapPrinter::DrawPixel(int x, int y, u8 b, u8 g, u8 r, u8 a) } void RenderD7::BitmapPrinter::DrawRect(int x, int y, int w, int h, u8 line_w, u8 b, u8 g, u8 r, u8 a) { - unsigned error = bitmap.draw_rectangle(x, bitmap.bmp_info_header.height - w - y, w, h, b, g, r, a, line_w); + unsigned error = bitmap.draw_rectangle(x, bitmap.bmp_info_header.height - y - h, w, h, b, g, r, a, line_w); if (error) { RenderD7::AddOvl(std::make_unique("BitmapPrinter->Rect", "Error Code: " + std::to_string(error))); @@ -1636,7 +1636,7 @@ void RenderD7::BitmapPrinter::DrawRect(int x, int y, int w, int h, u8 line_w, u8 void RenderD7::BitmapPrinter::DrawRectFilled(int x, int y, int w, int h, u8 b, u8 g, u8 r, u8 a) { - unsigned error = bitmap.fill_region(x, bitmap.bmp_info_header.height - w - y, w, h, b, g, r, a); + unsigned error = bitmap.fill_region(x, bitmap.bmp_info_header.height - h - y, w, h, b, g, r, a); if (error) { RenderD7::AddOvl(std::make_unique("BitmapPrinter->RectF", "Error Code: " + std::to_string(error))); @@ -1684,26 +1684,33 @@ void RenderD7::BitmapPrinter::CreateScreen(C3D_RenderTarget *target) renderframe.LoadPFromBuffer(BitmapConverter::ConvertData(bitmap.DATA())); } -void RenderD7::BitmapPrinter::DrawScreen(int framerate) +void RenderD7::BitmapPrinter::DrawScreenDirectF(int framerate) { if (isscreen) { if(frame == (60/framerate)){ RenderD7::OnScreen(targetr); - RenderD7::Image *img = new RenderD7::Image(); - img->Unload(); - //img->LoadFromBitmap(bitmap); renderframe.Unload(); renderframe.LoadFromBitmap(bitmap); - renderframe.img = img->Get(); - img->Unload(); - delete img; + frame = 0; } renderframe.Draw(0, 0); frame++; } } + +void RenderD7::BitmapPrinter::DrawScreenDirect() +{ + if (isscreen) + { + RenderD7::OnScreen(targetr); + renderframe.Unload(); + renderframe.LoadFromBitmap(bitmap); + renderframe.Draw(0, 0); + } +} + RenderD7::Image RenderD7::BitmapPrinter::GetImage() { RenderD7::Image img; @@ -1727,4 +1734,120 @@ void RenderD7::BitmapPrinter::Clear(u8 b, u8 g, u8 r, u8 a) void RenderD7::BitmapPrinter::ClearBlank() { bitmap = blank; +} + +void RenderD7::BitmapPrinter::DrawScreenF(int framerate) +{ + if (isscreen) + { + if(frame == (60/framerate)){ + RenderD7::OnScreen(targetr); + frame = 0; + } + + renderframe.Draw(0, 0); + frame++; + } +} +void RenderD7::BitmapPrinter::DrawScreen() +{ + if (isscreen) + { + RenderD7::OnScreen(targetr); + renderframe.Draw(0, 0); + } +} +void RenderD7::BitmapPrinter::UpdateScreenF(int framerate) +{ + if (isscreen) + { + if(frame == (60/framerate)){ + renderframe.Unload(); + renderframe.LoadFromBitmap(bitmap); + frame = 0; + } + frame++; + } +} +void RenderD7::BitmapPrinter::UpdateScreen() +{ + if (isscreen) + { + renderframe.Unload(); + renderframe.LoadFromBitmap(bitmap); + } +} + +#define TICKS_PER_MSEC 268111.856 + +void RenderD7::BitmapPrinter::Benchmark() +{ + if(setupbenchmark) + { + frametime = 0; + renderedframes = 0; + timer = 0; + setupbenchmark = false; + lastTime = svcGetSystemTick(); + } + if(benchmark) + { + if(timer >= 60) + { + std::string renderedf = std::to_string(renderedframes); + std::string avgdtt = std::to_string(mhdtt); + float alldtt = 0; + for (size_t i = 0; i < hdttt.size(); i++) + { + alldtt += hdttt[i]; + } + float allfps = 0; + for (size_t f = 0; f < fpscountc.size(); f++) + { + allfps += fpscountc[f]; + } + + + std::string avgcpu = std::to_string((alldtt/(float)hdttt.size())); + std::string avgfps = std::to_string((allfps/(int)fpscountc.size())); + + std::string resultt = "Frames Rendered: " + renderedf + "\nMax Cpu Time: " + avgdtt + "\nAvg Cpu Time: " + avgcpu + "\nAvg Fps: " + avgfps; + RenderD7::Error::DisplayError("Result", resultt, 30); + benchmark = false; + } + uint64_t currentTime = svcGetSystemTick(); + dtt = ((float)(currentTime / (float)TICKS_PER_MSEC) - (float)(lastTime / (float)TICKS_PER_MSEC)) / 1000.f; + lastTime = currentTime; + last_time = currentTime; + frameCounter++; + fpsClock += dtt; + if (fpsClock >= 1.f) { + fps = frameCounter; + frameCounter = 0; + fpsClock = 0.f; + } + + this->ClearBlank(); + this->DrawRectFilled(0, 0, this->bitmap.bmp_info_header.width, this->bitmap.bmp_info_header.width, 255, 255, 255, 255); + this->DrawRect(5, 5, this->bitmap.bmp_info_header.width - 10, this->bitmap.bmp_info_header.height - 10, 5, 0, 0, 0, 0); + this->UpdateScreenF(testfps); + this->DrawScreen(); + renderedframes++; + timer+= 1*dtt; + float hdtt = C3D_GetProcessingTime(); + hdttt.push_back(hdtt); + if(mhdtt < hdtt) + { + mhdtt = C3D_GetProcessingTime(); + } + RenderD7::DrawText(0, 0, 0.5f, RenderD7::Color::Hex("#ff0000"), "Time: " + std::to_string(timer)); + RenderD7::DrawText(0, 20, 0.5f, RenderD7::Color::Hex("#ff0000"), "Fps: " + std::to_string(fps)); + } +} + +void RenderD7::BitmapPrinter::SetupBenchmark(int framerate) +{ + benchmark = true; + setupbenchmark = true; + this->testfps = framerate; } \ No newline at end of file