Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion source/gfx/SplashScreenDrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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());
}
}
Expand Down
73 changes: 73 additions & 0 deletions source/gfx/WEBPTexture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "WEBPTexture.h"
#include "utils/logger.h"
#include <cstdlib>
#include <cstring>
#include <gx2/mem.h>
#include <webp/decode.h>

GX2Texture *WEBP_LoadTexture(std::span<uint8_t> 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<GX2Texture *>(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<uint8_t *>(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;
}
7 changes: 7 additions & 0 deletions source/gfx/WEBPTexture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <cstdint>
#include <gx2/texture.h>
#include <span>

GX2Texture *WEBP_LoadTexture(std::span<uint8_t> data);
4 changes: 2 additions & 2 deletions source/gfx/gfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -476,4 +476,4 @@ BOOL GfxInitShaderAttribute(WHBGfxShaderGroup *group,
attrib->mask = GfxGetAttribFormatSel(format);
attrib->endianSwap = GX2_ENDIAN_SWAP_DEFAULT;
return TRUE;
}
}
Loading