From 79d255b63fc8c87dc0615319f8d81eea795e4fdb Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 11 Dec 2022 08:38:38 -0600 Subject: [PATCH] [nostalgia/core/userland] Make palettes swappable in GL renderer --- src/nostalgia/core/userland/gfx.cpp | 10 ++++++---- src/nostalgia/core/userland/gfx.hpp | 6 ++++++ src/nostalgia/core/userland/gfx_opengl.cpp | 19 +++++++++++++++++-- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/nostalgia/core/userland/gfx.cpp b/src/nostalgia/core/userland/gfx.cpp index e75d87dd..84ca2600 100644 --- a/src/nostalgia/core/userland/gfx.cpp +++ b/src/nostalgia/core/userland/gfx.cpp @@ -38,16 +38,18 @@ ox::Error loadBgTileSheet(Context *ctx, if (bytesPerTile == 64) { // 8 BPP pixels.resize(tilesheet->pixels.size()); for (std::size_t i = 0; i < tilesheet->pixels.size(); ++i) { - pixels[i] = toColor32(palette->colors[tilesheet->pixels[i]]); + pixels[i] = tilesheet->pixels[i]; } } else { // 4 BPP pixels.resize(tilesheet->pixels.size() * 2); for (std::size_t i = 0; i < tilesheet->pixels.size(); ++i) { - pixels[i * 2 + 0] = toColor32(palette->colors[tilesheet->pixels[i] & 0xF]); - pixels[i * 2 + 1] = toColor32(palette->colors[tilesheet->pixels[i] >> 4]); + pixels[i * 2 + 0] = tilesheet->pixels[i] & 0xF; + pixels[i * 2 + 1] = tilesheet->pixels[i] >> 4; } } - renderer::loadBgTexture(ctx->rendererData(), cbb, pixels.data(), width, height); + const auto rd = ctx->rendererData(); + renderer::loadBgTexture(rd, cbb, pixels.data(), width, height); + renderer::loadBgPalette(rd, *palette); return OxError(0); } diff --git a/src/nostalgia/core/userland/gfx.hpp b/src/nostalgia/core/userland/gfx.hpp index 6e3831c1..92925f5f 100644 --- a/src/nostalgia/core/userland/gfx.hpp +++ b/src/nostalgia/core/userland/gfx.hpp @@ -8,12 +8,18 @@ #include +namespace { +class Palette; +} + namespace nostalgia::core::renderer { ox::Error init(Context *ctx, void **rendererData) noexcept; void shutdown(Context *ctx, void *rendererData) noexcept; +void loadBgPalette(void *rendererData, const Palette &pal) noexcept; + void loadBgTexture(void *rendererData, unsigned cbb, void *pixels, int w, int h) noexcept; } diff --git a/src/nostalgia/core/userland/gfx_opengl.cpp b/src/nostalgia/core/userland/gfx_opengl.cpp index 36f26a8f..b4db9534 100644 --- a/src/nostalgia/core/userland/gfx_opengl.cpp +++ b/src/nostalgia/core/userland/gfx_opengl.cpp @@ -47,6 +47,8 @@ struct GlImplData { uint64_t draws = 0; ox::Array cbbs; ox::Array backgrounds; + static constexpr std::size_t ColorCnt = 256; + ox::Array palette{}; }; constexpr const GLchar *bgvshad = R"( @@ -65,9 +67,11 @@ constexpr const GLchar *bgfshad = R"( out vec4 outColor; in vec2 fTexCoord; uniform sampler2D image; + uniform vec3 fPalette[256]; void main() { - //outColor = vec4(0.0, 0.7, 1.0, 1.0); - outColor = texture(image, fTexCoord); + int idx = int(texture(image, fTexCoord).rgb.r * 256); + outColor = vec4(fPalette[idx], 1.0); + //outColor = vec4(0.0, 0.7, 1.0, 1.0); })"; [[nodiscard]] @@ -175,7 +179,9 @@ static void drawBackground(CBB *cbb) noexcept { static void drawBackgrounds(GlImplData *id) noexcept { // load background shader and its uniforms glUseProgram(id->bgShader); + const auto uniformPalette = static_cast(glGetUniformLocation(id->bgShader, "fPalette")); const auto uniformTileHeight = static_cast(glGetUniformLocation(id->bgShader, "vTileHeight")); + glUniform3fv(uniformPalette, GlImplData::ColorCnt, id->palette.data()); for (auto &bg : id->backgrounds) { if (bg.enabled) { auto &cbb = id->cbbs[bg.cbbIdx]; @@ -205,6 +211,15 @@ void shutdown(Context*, void *rendererData) noexcept { ox::safeDelete(id); } +void loadBgPalette(void *rendererData, const Palette &pal) noexcept { + const auto id = static_cast(rendererData); + for (auto i = 0u; const auto c : pal.colors) { + id->palette[i++] = redf(c); + id->palette[i++] = greenf(c); + id->palette[i++] = bluef(c); + } +} + void loadBgTexture(void *rendererData, unsigned cbbIdx, void *pixels, int w, int h) noexcept { oxTracef("nostalgia::core::gfx::gl", "loadBgTexture: { cbbIdx: {}, w: {}, h: {} }", cbbIdx, w, h); const auto id = static_cast(rendererData);