diff --git a/Makefile b/Makefile index 3a59eae..024ee56 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ CXXFLAGS := $(CFLAGS) -std=c++23 -fno-rtti ASFLAGS := -g $(ARCH) LDFLAGS = -g $(ARCH) $(RPXSPECS) --entry=_start -Wl,-Map,$(notdir $*.map) -LIBS := -lpng -lturbojpeg -lwut -lz +LIBS := -lpng -lturbojpeg -lwebp -lwut -lz ifeq ($(DEBUG),1) CXXFLAGS += -DDEBUG -g diff --git a/source/gfx/SplashScreenDrawer.cpp b/source/gfx/SplashScreenDrawer.cpp index e9f9793..45e4aff 100644 --- a/source/gfx/SplashScreenDrawer.cpp +++ b/source/gfx/SplashScreenDrawer.cpp @@ -3,6 +3,7 @@ #include "PNGTexture.h" #include "ShaderSerializer.h" #include "TGATexture.h" +#include "WEBPTexture.h" #include "gfx.h" #include "utils/logger.h" #include "utils/utils.h" @@ -137,6 +138,8 @@ static GX2Texture *LoadImageAsTexture(const std::filesystem::path &filename) { return JPEG_LoadTexture(buffer); } else if (ext == ".tga") { return TGA_LoadTexture(buffer); + } else if (ext == ".webp") { + return WEBP_LoadTexture(buffer); } } return nullptr; @@ -153,6 +156,9 @@ SplashScreenDrawer::SplashScreenDrawer(const std::filesystem::path &splash_base_ if (!mTexture) { mTexture = LoadImageAsTexture(splash_base_path / "splash.tga"); } + if (!mTexture) { + mTexture = LoadImageAsTexture(splash_base_path / "splash.webp"); + } if (!mTexture) { // try to load a random one from "splashes/*" try { @@ -162,7 +168,11 @@ SplashScreenDrawer::SplashScreenDrawer(const std::filesystem::path &splash_base_ continue; } auto ext = ToLower(entry.path().extension()); - if (ext == ".png" || ext == ".tga" || ext == ".jpg" || ext == ".jpeg") { + if (ext == ".png" || + ext == ".tga" || + ext == ".jpg" || + ext == ".jpeg" || + ext == ".webp") { candidates.push_back(entry.path()); } } diff --git a/source/gfx/WEBPTexture.cpp b/source/gfx/WEBPTexture.cpp new file mode 100644 index 0000000..262045b --- /dev/null +++ b/source/gfx/WEBPTexture.cpp @@ -0,0 +1,73 @@ +#include "WEBPTexture.h" +#include "utils/logger.h" +#include +#include +#include +#include + +GX2Texture *WEBP_LoadTexture(std::span data) { + GX2Texture *texture = nullptr; + int width, height; + + if (!WebPGetInfo(data.data(), data.size(), &width, &height)) { + DEBUG_FUNCTION_LINE_ERR("Failed to parse WEBP header\n"); + goto error; + } + + texture = static_cast(std::malloc(sizeof(GX2Texture))); + if (!texture) { + DEBUG_FUNCTION_LINE_ERR("Failed to allocate texture\n"); + goto error; + } + + std::memset(texture, 0, sizeof(GX2Texture)); + texture->surface.width = width; + texture->surface.height = height; + texture->surface.depth = 1; + texture->surface.mipLevels = 1; + texture->surface.format = GX2_SURFACE_FORMAT_UNORM_R8_G8_B8_A8; + texture->surface.aa = GX2_AA_MODE1X; + texture->surface.use = GX2_SURFACE_USE_TEXTURE; + texture->surface.dim = GX2_SURFACE_DIM_TEXTURE_2D; + texture->surface.tileMode = GX2_TILE_MODE_LINEAR_ALIGNED; + texture->surface.swizzle = 0; + texture->viewFirstMip = 0; + texture->viewNumMips = 1; + texture->viewFirstSlice = 0; + texture->viewNumSlices = 1; + texture->compMap = 0x0010203; + GX2CalcSurfaceSizeAndAlignment(&texture->surface); + GX2InitTextureRegs(texture); + + if (texture->surface.imageSize == 0) { + DEBUG_FUNCTION_LINE_ERR("Texture is empty\n"); + goto error; + } + + texture->surface.image = std::aligned_alloc(texture->surface.alignment, + texture->surface.imageSize); + if (!texture->surface.image) { + DEBUG_FUNCTION_LINE_ERR("Failed to allocate surface for texture\n"); + goto error; + } + + if (!WebPDecodeRGBAInto(data.data(), data.size(), + reinterpret_cast(texture->surface.image), + texture->surface.imageSize, + texture->surface.pitch * 4)) { + DEBUG_FUNCTION_LINE_ERR("Failed to decode WEBP image\n"); + goto error; + } + + GX2Invalidate(GX2_INVALIDATE_MODE_CPU | GX2_INVALIDATE_MODE_TEXTURE, + texture->surface.image, texture->surface.imageSize); + + return texture; + +error: + if (texture) { + std::free(texture->surface.image); + } + std::free(texture); + return nullptr; +} diff --git a/source/gfx/WEBPTexture.h b/source/gfx/WEBPTexture.h new file mode 100644 index 0000000..b3180f0 --- /dev/null +++ b/source/gfx/WEBPTexture.h @@ -0,0 +1,7 @@ +#pragma once + +#include +#include +#include + +GX2Texture *WEBP_LoadTexture(std::span data); diff --git a/source/gfx/gfx.c b/source/gfx/gfx.c index 5bc9078..df33e02 100644 --- a/source/gfx/gfx.c +++ b/source/gfx/gfx.c @@ -200,7 +200,7 @@ static BOOL initBucketHeap() { sGfxHeapForeground = MEMCreateExpHeapEx(base, size, 0); if (!sGfxHeapForeground) { - WHBLogPrintf("%s: MEMCreateExpHeapEx(0x%08X, 0x%X, 0)", __FUNCTION__, base, size); + WHBLogPrintf("%s: MEMCreateExpHeapEx(%p, 0x%X, 0)", __FUNCTION__, base, size); return FALSE; } @@ -476,4 +476,4 @@ BOOL GfxInitShaderAttribute(WHBGfxShaderGroup *group, attrib->mask = GfxGetAttribFormatSel(format); attrib->endianSwap = GX2_ENDIAN_SWAP_DEFAULT; return TRUE; -} \ No newline at end of file +}