From a0bb0d0114c2b228f2f1715b6fc53d99901b0193 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 29 Jun 2025 16:43:36 -0700 Subject: Add support for R8 linear textures to render baked AO --- src/asset/texture.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'src/asset/texture.c') diff --git a/src/asset/texture.c b/src/asset/texture.c index c790394..fb423cc 100644 --- a/src/asset/texture.c +++ b/src/asset/texture.c @@ -49,7 +49,7 @@ Texture* gfx_texture_load(GfxCore* gfxcore, const LoadTextureCmd* cmd) { assert(cmd->origin == AssetFromFile || cmd->origin == AssetFromMemory); assert(cmd->type == LoadTexture || cmd->type == LoadCubemap); - int width, height, components, old_components; + int width, height, components; unsigned char* pixels[6] = {0}; switch (cmd->origin) { @@ -64,7 +64,8 @@ Texture* gfx_texture_load(GfxCore* gfxcore, const LoadTextureCmd* cmd) { } break; } - case LoadCubemap: + case LoadCubemap: { + int old_components = 0; for (int i = 0; i < 6; ++i) { // Flip +Y and -Y textures vertically. stbi_set_flip_vertically_on_load(((i == 2) || (i == 3)) ? 1 : 0); @@ -76,9 +77,10 @@ Texture* gfx_texture_load(GfxCore* gfxcore, const LoadTextureCmd* cmd) { log_error("Failed to load texture file: %s", filepath); break; } - if (i > 0 && components != old_components) { - log_error("All textures in a cubemap must have the same number of " - "components"); + if ((i > 0) && (components != old_components)) { + log_error( + "All textures in a cubemap must have the same number of " + "components"); break; } if ((i != 2) && (i != 3)) { @@ -89,6 +91,7 @@ Texture* gfx_texture_load(GfxCore* gfxcore, const LoadTextureCmd* cmd) { } break; } + } break; case AssetFromMemory: // TODO: Load textures from memory. @@ -122,6 +125,25 @@ Texture* gfx_texture_load(GfxCore* gfxcore, const LoadTextureCmd* cmd) { } switch (components) { + case 1: + switch (cmd->colour_space) { + case LinearColourSpace: + desc.format = TextureR8; + break; + case sRGB: + // TODO: Gamma single-channel textures are not implemented yet. + // The caller should convert the single-channel to RGB and pass it down + // as sRGB. This is why the ChronographWatch currently appears red on the + // back. + log_error("Gamma colour space is not supported for 1-channel textures"); + // return 0; + desc.format = TextureR8; + break; + default: + log_error("Unsupported texture colour space: %d", cmd->colour_space); + return 0; + } + break; case 3: switch (cmd->colour_space) { case LinearColourSpace: -- cgit v1.2.3