From 7d265622774e4cdc1b3d784639b2ac21dce916d6 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 6 Jan 2024 11:07:55 +1100 Subject: [PATCH] N64: Try to avoid crash when although width * height * pixel size of a texture fits within 4096 bytes, after aligning each row to 8 bytes, row pitch * height does not fit within 4096 bytes --- src/Graphics_N64.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Graphics_N64.c b/src/Graphics_N64.c index 6daf9a4aa..2c165205f 100644 --- a/src/Graphics_N64.c +++ b/src/Graphics_N64.c @@ -121,12 +121,19 @@ typedef struct CCTexture { GLuint textureID; } CCTexture; +#define ALIGNUP8(size) (((size) + 7) & ~0x07) + // A8 B8 G8 R8 > A1 B5 G5 B5 #define To16BitPixel(src) \ ((src & 0x80) >> 7) | ((src & 0xF800) >> 10) | ((src & 0xF80000) >> 13) | ((src & 0xF8000000) >> 16); static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_bool mipmaps) { cc_bool bit16 = flags & TEXTURE_FLAG_LOWRES; + // rows are actually 8 byte aligned in TMEM https://github.com/DragonMinded/libdragon/blob/f360fa1bb1fb3ff3d98f4ab58692d40c828636c9/src/rdpq/rdpq_tex.c#L132 + // so even though width * height * pixel size may fit within 4096 bytes, after adjusting for 8 byte alignment, row pitch * height may exceed 4096 bytes + int pitch = bit16 ? ALIGNUP8(bmp->width * 2) : ALIGNUP8(bmp->width * 4); + if (pitch * bmp->height > 4096) return 0; + CCTexture* tex = Mem_Alloc(1, sizeof(CCTexture), "texture"); glGenTextures(1, &tex->textureID);