Test paletted stuff
Some checks failed
Build Dusk / run-tests (push) Failing after 26s
Build Dusk / build-linux (push) Failing after 25s
Build Dusk / build-psp (push) Failing after 18s
Build Dusk / build-gamecube (push) Failing after 18s
Build Dusk / build-wii (push) Failing after 18s

This commit is contained in:
2026-03-26 14:48:20 -05:00
parent 98947dea26
commit 407620387d
10 changed files with 138 additions and 33 deletions

View File

@@ -50,7 +50,10 @@ errorret_t assetTextureLoad(assetentire_t entire) {
assetData->height,
TEXTURE_FORMAT_PALETTE,
(texturedata_t){
.paletteData = assetData->palette
.paletted = {
.indices = NULL,
.palette = NULL
}
}
);

View File

@@ -38,16 +38,27 @@ errorret_t displayInit(void) {
errorChain(textInit());
errorChain(screenInit());
color_t pixels[2 * 2] = {
COLOR_RED, COLOR_GREEN,
COLOR_BLUE, COLOR_WHITE
PALETTES[0].colors[0] = COLOR_RED;
PALETTES[0].colors[1] = COLOR_GREEN;
PALETTES[0].colors[2] = COLOR_BLUE;
PALETTES[0].colors[3] = COLOR_WHITE;
PALETTES[0].count = 4;
uint8_t indices[4 * 4] = {
0, 1, 2, 3,
3, 2, 1, 0,
0, 1, 2, 3,
3, 2, 1, 0,
};
errorChain(textureInit(
&TEXTURE,
2, 2,
TEXTURE_FORMAT_RGBA,
4, 4,
TEXTURE_FORMAT_PALETTE,
(texturedata_t){
.rgbaColors = pixels
.paletted = {
.indices = indices,
.palette = &PALETTES[0]
}
}
));
@@ -79,9 +90,9 @@ errorret_t displayUpdate(void) {
// camera.orthographic.top = SCREEN.height;
// camera.orthographic.bottom = 0.0f;
cameraInitPerspective(&camera);
camera.lookat.position[0] = 10.0f;
camera.lookat.position[1] = 10.0f;
camera.lookat.position[2] = 10.0f;
camera.lookat.position[0] = 3.0f;
camera.lookat.position[1] = 3.0f;
camera.lookat.position[2] = 3.0f;
mat4 proj, view, model;
cameraGetProjectionMatrix(&camera, proj);

View File

@@ -8,4 +8,5 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
tileset.c
texture.c
palette.c
)

View File

@@ -0,0 +1,10 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "palette.h"
palette_t PALETTES[PALETTE_COUNT];

View File

@@ -0,0 +1,19 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/color.h"
#define PALETTE_COLOR_COUNT 256
#define PALETTE_COUNT 6
typedef struct {
color_t colors[PALETTE_COLOR_COUNT];
uint8_t count;
} palette_t;
extern palette_t PALETTES[PALETTE_COUNT];

View File

@@ -7,7 +7,7 @@
#pragma once
#include "error/error.h"
#include "display/color.h"
#include "display/texture/palette.h"
#include "display/texture/textureplatform.h"
#ifndef textureInitPlatform
@@ -20,8 +20,13 @@
typedef textureformatplatform_t textureformat_t;
typedef textureplatform_t texture_t;
typedef struct {
uint8_t *indices;
palette_t *palette;
} texturepalettedata_t;
typedef union texturedata_u {
uint8_t *paletteData;
texturepalettedata_t paletted;
color_t *rgbaColors;
} texturedata_t;

View File

@@ -224,6 +224,33 @@ errorret_t shaderSetTextureGL(
errorChain(errorGLCheck());
glUniform1i(location, 0);
errorChain(errorGLCheck());
if(texture->format == TEXTURE_FORMAT_PALETTE) {
shaderParamGetLocationGL(shader, "u_ColorCount", &location);
glUniform1i(location, texture->palette->count);
errorChain(errorGLCheck());
shaderParamGetLocationGL(shader, "u_Colors", &location);
GLuint paletteData[texture->palette->count];
for(size_t i = 0; i < texture->palette->count; i++) {
color_t color = texture->palette->colors[i];
paletteData[i] = (
((uint32_t)color.r << 24) |
((uint32_t)color.g << 16) |
((uint32_t)color.b << 8) |
((uint32_t)color.a << 0)
);
}
glUniform1uiv(location, texture->palette->count, paletteData);
errorChain(errorGLCheck());
}
// PALETTE TEST
// errorChain(shaderParamGetLocationGL(shader, "u_Palette", &location));
// glActiveTexture(GL_TEXTURE1);
// errorChain(errorGLCheck());
#endif
errorOk();

View File

@@ -13,28 +13,39 @@
shaderdefinition_t SHADER_UNLIT_DEFINITION = {
.vert =
"#version 330 core\n"
"layout(location = 0) in vec3 aPos;\n"
"layout(location = 1) in vec2 aTexCoord;\n"
"layout(location = 2) in vec4 aColor;\n"
"layout(location = 0) in vec3 a_Pos;\n"
"layout(location = 1) in vec2 a_TexCoord;\n"
"layout(location = 2) in vec4 a_Color;\n"
"uniform mat4 u_Proj;\n"
"uniform mat4 u_View;\n"
"uniform mat4 u_Model;\n"
"out vec4 v_Color;\n"
"out vec2 v_TexCoord;\n"
"void main() {\n"
" gl_Position = u_Proj * u_View * u_Model * vec4(aPos, 1.0);\n"
" v_Color = aColor;\n"
" v_TexCoord = aTexCoord;\n"
" gl_Position = u_Proj * u_View * u_Model * vec4(a_Pos, 1.0);\n"
" v_Color = a_Color;\n"
" v_TexCoord = a_TexCoord;\n"
"}\n",
.frag =
"#version 330 core\n"
"uniform sampler2D u_Texture;\n"
"uniform uint u_Colors[256];\n"
"uniform int u_ColorCount;\n"
"in vec4 v_Color;\n"
"in vec2 v_TexCoord;\n"
"out vec4 FragColor;\n"
"void main() {\n"
" FragColor = texture(u_Texture, v_TexCoord) * v_Color;\n"
" vec4 texColor = texture(u_Texture, v_TexCoord);\n"
" uint index = uint(floor(texColor.r * 255.0));\n"
" uint palColor = u_Colors[index];\n"
" float r = float((palColor >> 24) & 0xFFu) / 255.0;\n"
" float g = float((palColor >> 16) & 0xFFu) / 255.0;\n"
" float b = float((palColor >> 8) & 0xFFu) / 255.0;\n"
" float a = float((palColor >> 0) & 0xFFu) / 255.0;\n"
" vec4 paletteColor = vec4(r, g, b, a);\n"
" FragColor = paletteColor;\n"
// " FragColor = paletteColor * v_Color;\n"
"}\n"
};
#endif

View File

@@ -8,6 +8,7 @@
#include "display/texture/texture.h"
#include "assert/assert.h"
#include "error/errorgl.h"
#include "util/memory.h"
errorret_t textureInitGL(
texturegl_t *texture,
@@ -29,20 +30,32 @@ errorret_t textureInitGL(
break;
case TEXTURE_FORMAT_PALETTE:
assertNotNull(data.paletteData, "Palette texture data cannot be NULL");
glTexImage2D(
GL_TEXTURE_2D,
0, GL_COLOR_INDEX8_EXT,
width, height,
0, GL_COLOR_INDEX8_EXT,
GL_UNSIGNED_BYTE, (void*)data.paletteData
);
errorChain(errorGLCheck());
// glColorTableEXT(
// GL_TEXTURE_2D, GL_RGBA, data.palette.palette->colorCount, GL_RGBA,
// GL_UNSIGNED_BYTE, (const void*)data.palette.palette->colors
// );
assertNotNull(data.paletted.indices, "Palette indices cannot be NULL");
assertNotNull(data.paletted.palette, "Palette colors cannot be NULL");
#ifdef DUSK_OPENGL_LEGACY
glTexImage2D(
GL_TEXTURE_2D,
0, GL_COLOR_INDEX8_EXT,
width, height,
0, GL_COLOR_INDEX8_EXT,
GL_UNSIGNED_BYTE, (void*)data.paletted.indices
);
errorChain(errorGLCheck());
glColorTableEXT(
GL_TEXTURE_2D, GL_RGBA, data.paletted.palette->count, GL_RGBA,
GL_UNSIGNED_BYTE, (const void*)data.paletted.palette->colors
);
errorChain(errorGLCheck());
#else
// For modern systems we send to only the R channel and the shader does
// the rest.
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RED, width, height, 0,
GL_RED, GL_UNSIGNED_BYTE, (void*)data.paletted.indices
);
errorChain(errorGLCheck());
texture->palette = data.paletted.palette;
#endif
break;
default:
@@ -69,5 +82,6 @@ errorret_t textureDisposeGL(texturegl_t *texture) {
glDeleteTextures(1, &texture->id);
errorChain(errorGLCheck());
errorOk();
}

View File

@@ -20,6 +20,10 @@ typedef struct {
textureformatgl_t format;
int32_t width;
int32_t height;
union {
palette_t *palette;
};
} texturegl_t;
/**