From f58abd9c3f4b61abee803974e15b51691f0bcd56 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Fri, 19 Mar 2021 19:22:08 -0500 Subject: [PATCH] [nostalgia/core] Cleanup and make FPS print a config.hpp option --- src/nostalgia/core/config.hpp | 1 + src/nostalgia/core/userland/gfx_opengl.cpp | 109 +++++++++++---------- src/nostalgia/core/userland/glutils.hpp | 5 +- 3 files changed, 63 insertions(+), 52 deletions(-) diff --git a/src/nostalgia/core/config.hpp b/src/nostalgia/core/config.hpp index bb783296..6beba067 100644 --- a/src/nostalgia/core/config.hpp +++ b/src/nostalgia/core/config.hpp @@ -16,6 +16,7 @@ namespace config { constexpr auto GbaSpriteBufferLen = 128; constexpr auto GbaEventLoopTimerBased = false; +constexpr auto UserlandFpsPrint = false; } diff --git a/src/nostalgia/core/userland/gfx_opengl.cpp b/src/nostalgia/core/userland/gfx_opengl.cpp index d48460ed..42b064d2 100644 --- a/src/nostalgia/core/userland/gfx_opengl.cpp +++ b/src/nostalgia/core/userland/gfx_opengl.cpp @@ -7,15 +7,12 @@ */ #include -#define NOST_FPS_PRINT -#ifdef NOST_FPS_PRINT -#include -#endif #include #include #include +#include #include #include "glutils.hpp" @@ -139,6 +136,58 @@ static void initBackgroundBufferset(Context *ctx, GLuint shader, Background *bg) ox::bit_cast(2 * sizeof(float))); } +static ox::Error loadTexture(Texture *tex, void *pixels) { + if (tex->texId == 0) { + glGenTextures(1, &tex->texId); + } + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, tex->texId); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + return OxError(0); +} + +static void tickFps(GlImplData *id) { + ++id->draws; + if (id->draws >= 500) { + using namespace std::chrono; + const auto now = duration_cast(system_clock::now().time_since_epoch()).count(); + const auto duration = static_cast(now - id->prevFpsCheckTime) / 1000.0; + const auto fps = static_cast(static_cast(id->draws) / duration); + if constexpr(config::UserlandFpsPrint) { + oxDebugf("FPS: {}", fps); + } + oxTracef("nostalgia::core::gfx::gl::fps", "FPS: {}", fps); + id->prevFpsCheckTime = now; + id->draws = 0; + } +} + +static void drawBackground(Background *bg) { + if (bg->enabled) { + glBindVertexArray(bg->vao); + if (bg->updated) { + bg->updated = false; + renderer::sendVbo(*bg); + } + glBindTexture(GL_TEXTURE_2D, bg->tex); + glDrawElements(GL_TRIANGLES, bg->bgEbos.size(), GL_UNSIGNED_INT, 0); + } +} + +static void drawBackgrounds(GlImplData *id) { + // load background shader and its uniforms + glUseProgram(id->bgShader); + const auto uniformTileHeight = static_cast(glGetUniformLocation(id->bgShader, "vTileHeight")); + for (auto &bg : id->backgrounds) { + glUniform1f(uniformTileHeight, 1.0f / static_cast(bg.tex.height / 8)); + drawBackground(&bg); + } +} + ox::Error init(Context *ctx) { const auto id = new GlImplData; ctx->setRendererData(id); @@ -161,28 +210,13 @@ ox::Error shutdown(Context *ctx) { return OxError(0); } -static ox::Error loadTexture(GLuint *texId, void *pixels, int w, int h) { - if (*texId == 0) { - glGenTextures(1, texId); - } - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, *texId); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - return OxError(0); -} - ox::Error loadBgTexture(Context *ctx, int section, void *pixels, int w, int h) { oxTracef("nostalgia::core::gfx::gl", "loadBgTexture: { section: {}, w: {}, h: {} }", section, w, h); const auto &id = ctx->rendererData(); auto &tex = id->backgrounds[static_cast(section)].tex; - tex.width = static_cast(w); - tex.height = static_cast(h); - const auto texId = &tex.texId; - return loadTexture(texId, pixels, w, h); + tex.width = w; + tex.height = h; + return loadTexture(&tex, pixels); } } @@ -216,37 +250,12 @@ void setBgStatus(Context *ctx, unsigned bg, bool status) { void draw(Context *ctx) { const auto id = ctx->rendererData(); - ++id->draws; - if (id->draws >= 500) { - using namespace std::chrono; - const auto now = duration_cast(system_clock::now().time_since_epoch()).count(); - const auto duration = static_cast(now - id->prevFpsCheckTime) / 1000.0; - const auto fps = static_cast(static_cast(id->draws) / duration); -#ifdef NOST_FPS_PRINT - std::cout << "FPS: " << fps << '\n'; -#endif - oxTracef("nostalgia::core::gfx::gl::fps", "FPS: {}", fps); - id->prevFpsCheckTime = now; - id->draws = 0; - } + renderer::tickFps(id); // clear screen glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); - // load background shader and its uniforms - glUseProgram(id->bgShader); - const auto uniformTileHeight = static_cast(glGetUniformLocation(id->bgShader, "vTileHeight")); - for (auto &bg : id->backgrounds) { - if (bg.enabled) { - glBindVertexArray(bg.vao); - if (bg.updated) { - bg.updated = false; - renderer::sendVbo(bg); - } - glBindTexture(GL_TEXTURE_2D, bg.tex); - glUniform1f(uniformTileHeight, 1.0f / static_cast(bg.tex.height / 8)); - glDrawElements(GL_TRIANGLES, bg.bgEbos.size(), GL_UNSIGNED_INT, 0); - } - } + // render + renderer::drawBackgrounds(id); } void clearTileLayer(Context *ctx, int layer) { diff --git a/src/nostalgia/core/userland/glutils.hpp b/src/nostalgia/core/userland/glutils.hpp index a556dc4e..49eaefd7 100644 --- a/src/nostalgia/core/userland/glutils.hpp +++ b/src/nostalgia/core/userland/glutils.hpp @@ -19,9 +19,10 @@ namespace nostalgia::core::renderer { struct Texture { + GLuint texId = 0; - GLuint width = 0; - GLuint height = 0; + GLsizei width = 0; + GLsizei height = 0; constexpr operator GLuint&() noexcept { return texId;