[nostalgia/core/userland] Make palettes swappable in GL renderer
This commit is contained in:
parent
461e3d61ef
commit
79d255b63f
@ -38,16 +38,18 @@ ox::Error loadBgTileSheet(Context *ctx,
|
|||||||
if (bytesPerTile == 64) { // 8 BPP
|
if (bytesPerTile == 64) { // 8 BPP
|
||||||
pixels.resize(tilesheet->pixels.size());
|
pixels.resize(tilesheet->pixels.size());
|
||||||
for (std::size_t i = 0; i < tilesheet->pixels.size(); ++i) {
|
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
|
} else { // 4 BPP
|
||||||
pixels.resize(tilesheet->pixels.size() * 2);
|
pixels.resize(tilesheet->pixels.size() * 2);
|
||||||
for (std::size_t i = 0; i < tilesheet->pixels.size(); ++i) {
|
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 + 0] = tilesheet->pixels[i] & 0xF;
|
||||||
pixels[i * 2 + 1] = toColor32(palette->colors[tilesheet->pixels[i] >> 4]);
|
pixels[i * 2 + 1] = tilesheet->pixels[i] >> 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
renderer::loadBgTexture(ctx->rendererData<void>(), cbb, pixels.data(), width, height);
|
const auto rd = ctx->rendererData<void>();
|
||||||
|
renderer::loadBgTexture(rd, cbb, pixels.data(), width, height);
|
||||||
|
renderer::loadBgPalette(rd, *palette);
|
||||||
return OxError(0);
|
return OxError(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,12 +8,18 @@
|
|||||||
|
|
||||||
#include <nostalgia/core/context.hpp>
|
#include <nostalgia/core/context.hpp>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
class Palette;
|
||||||
|
}
|
||||||
|
|
||||||
namespace nostalgia::core::renderer {
|
namespace nostalgia::core::renderer {
|
||||||
|
|
||||||
ox::Error init(Context *ctx, void **rendererData) noexcept;
|
ox::Error init(Context *ctx, void **rendererData) noexcept;
|
||||||
|
|
||||||
void shutdown(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;
|
void loadBgTexture(void *rendererData, unsigned cbb, void *pixels, int w, int h) noexcept;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -47,6 +47,8 @@ struct GlImplData {
|
|||||||
uint64_t draws = 0;
|
uint64_t draws = 0;
|
||||||
ox::Array<CBB, 4> cbbs;
|
ox::Array<CBB, 4> cbbs;
|
||||||
ox::Array<Background, 4> backgrounds;
|
ox::Array<Background, 4> backgrounds;
|
||||||
|
static constexpr std::size_t ColorCnt = 256;
|
||||||
|
ox::Array<GLfloat, ColorCnt * 3> palette{};
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr const GLchar *bgvshad = R"(
|
constexpr const GLchar *bgvshad = R"(
|
||||||
@ -65,9 +67,11 @@ constexpr const GLchar *bgfshad = R"(
|
|||||||
out vec4 outColor;
|
out vec4 outColor;
|
||||||
in vec2 fTexCoord;
|
in vec2 fTexCoord;
|
||||||
uniform sampler2D image;
|
uniform sampler2D image;
|
||||||
|
uniform vec3 fPalette[256];
|
||||||
void main() {
|
void main() {
|
||||||
//outColor = vec4(0.0, 0.7, 1.0, 1.0);
|
int idx = int(texture(image, fTexCoord).rgb.r * 256);
|
||||||
outColor = texture(image, fTexCoord);
|
outColor = vec4(fPalette[idx], 1.0);
|
||||||
|
//outColor = vec4(0.0, 0.7, 1.0, 1.0);
|
||||||
})";
|
})";
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
@ -175,7 +179,9 @@ static void drawBackground(CBB *cbb) noexcept {
|
|||||||
static void drawBackgrounds(GlImplData *id) noexcept {
|
static void drawBackgrounds(GlImplData *id) noexcept {
|
||||||
// load background shader and its uniforms
|
// load background shader and its uniforms
|
||||||
glUseProgram(id->bgShader);
|
glUseProgram(id->bgShader);
|
||||||
|
const auto uniformPalette = static_cast<GLint>(glGetUniformLocation(id->bgShader, "fPalette"));
|
||||||
const auto uniformTileHeight = static_cast<GLint>(glGetUniformLocation(id->bgShader, "vTileHeight"));
|
const auto uniformTileHeight = static_cast<GLint>(glGetUniformLocation(id->bgShader, "vTileHeight"));
|
||||||
|
glUniform3fv(uniformPalette, GlImplData::ColorCnt, id->palette.data());
|
||||||
for (auto &bg : id->backgrounds) {
|
for (auto &bg : id->backgrounds) {
|
||||||
if (bg.enabled) {
|
if (bg.enabled) {
|
||||||
auto &cbb = id->cbbs[bg.cbbIdx];
|
auto &cbb = id->cbbs[bg.cbbIdx];
|
||||||
@ -205,6 +211,15 @@ void shutdown(Context*, void *rendererData) noexcept {
|
|||||||
ox::safeDelete(id);
|
ox::safeDelete(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void loadBgPalette(void *rendererData, const Palette &pal) noexcept {
|
||||||
|
const auto id = static_cast<GlImplData*>(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 {
|
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);
|
oxTracef("nostalgia::core::gfx::gl", "loadBgTexture: { cbbIdx: {}, w: {}, h: {} }", cbbIdx, w, h);
|
||||||
const auto id = static_cast<GlImplData*>(rendererData);
|
const auto id = static_cast<GlImplData*>(rendererData);
|
||||||
|
Loading…
Reference in New Issue
Block a user