commit c28b1d555fb8c32bd3f0525f85f66b3b0502f447
parent 2e66dd513ddd7d58cd459599442408d4636ef006
Author: bsandro <email@bsandro.tech>
Date: Mon, 10 Nov 2025 01:47:57 +0200
astc routine macro
Diffstat:
| M | texture.c | | | 38 | +++++++++++++++++++++++++++++++++++--- |
1 file changed, 35 insertions(+), 3 deletions(-)
diff --git a/texture.c b/texture.c
@@ -120,6 +120,28 @@ static GLuint mkTexture(const char *filename) {
return tex;
}
+#define CHECK_ASTC_FORMAT(bx, by) if (x==bx&&y==by) return GL_COMPRESSED_RGBA_ASTC_##bx##x##by##_KHR;
+
+GLenum get_astc_format(uint8_t x, uint8_t y) {
+ CHECK_ASTC_FORMAT(4,4)
+ CHECK_ASTC_FORMAT(5,4)
+ CHECK_ASTC_FORMAT(5,5)
+ CHECK_ASTC_FORMAT(6,5)
+ CHECK_ASTC_FORMAT(6,6)
+ CHECK_ASTC_FORMAT(8,5)
+ CHECK_ASTC_FORMAT(8,6)
+ CHECK_ASTC_FORMAT(8,8)
+ CHECK_ASTC_FORMAT(10,5)
+ CHECK_ASTC_FORMAT(10,6)
+ CHECK_ASTC_FORMAT(10,8)
+ CHECK_ASTC_FORMAT(10,10)
+ CHECK_ASTC_FORMAT(12,10)
+ CHECK_ASTC_FORMAT(12,12)
+
+ printf("invalid ASTC block size\n");
+ exit(-1);
+}
+
static GLuint mkTextureAstc(const char *filename) {
FILE *f = fopen(filename, "rb");
if (f==NULL) {
@@ -131,6 +153,8 @@ static GLuint mkTextureAstc(const char *filename) {
int64_t fsize = ftell(f);
rewind(f);
+ printf("astc file size: %lld bytes\n", fsize);
+
uint8_t *data = malloc(fsize);
if (fread(data, 1, fsize, f) != fsize) {
printf("error reading file %s\n", filename);
@@ -138,31 +162,39 @@ static GLuint mkTextureAstc(const char *filename) {
}
const astc_header *astc_data = (astc_header *)data;
+ printf("astc block: %dx%d\n", astc_data->blockdim_x, astc_data->blockdim_y);
+ GLenum astc_format = get_astc_format(astc_data->blockdim_x, astc_data->blockdim_y);
/* Merge x,y,z-sizes from 3 chars into one integer value. */
GLsizei xsize = astc_data->xsize[0] + (astc_data->xsize[1] << 8) + (astc_data->xsize[2] << 16);
GLsizei ysize = astc_data->ysize[0] + (astc_data->ysize[1] << 8) + (astc_data->ysize[2] << 16);
GLsizei zsize = astc_data->zsize[0] + (astc_data->zsize[1] << 8) + (astc_data->zsize[2] << 16);
+ printf("astc dimensions: %dx%d\n", xsize, ysize);
+
/* Compute number of blocks in each direction. */
GLsizei xblocks = (xsize + astc_data->blockdim_x - 1) / astc_data->blockdim_x;
GLsizei yblocks = (ysize + astc_data->blockdim_y - 1) / astc_data->blockdim_y;
GLsizei zblocks = (zsize + astc_data->blockdim_z - 1) / astc_data->blockdim_z;
+ // maybe we can do just fsize-16 here
GLsizei astc_size = xblocks * yblocks * zblocks << 4;
+ printf("astc data size: %lld bytes\n", astc_size);
+
GLuint tex;
GL_CHECK(glGenTextures(1, &tex));
GL_CHECK(glBindTexture(GL_TEXTURE_2D, tex));
- GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT));
- GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT));
+ //GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
+ //GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
- GL_CHECK(glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_ASTC_6x6_KHR, xsize, ysize, 0, astc_size, (const GLvoid *)astc_data));
+ GL_CHECK(glCompressedTexImage2D(GL_TEXTURE_2D, 0, astc_format, xsize, ysize, 0, astc_size, (const GLvoid *)astc_data));
//GL_CHECK(glGenerateMipmap(GL_TEXTURE_2D));
GL_CHECK(glEnable(GL_TEXTURE_2D));
fclose(f);
+ free(data);
return tex;
}