From 1bc589b8e56f896c10feb695159cc17fdfa3b9e4 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 29 Mar 2024 19:12:42 +1100 Subject: [PATCH] 3DS: Fix never freeing textures, oops Also redesign PNG encoding API to support a context parameter --- src/Bitmap.c | 6 +++--- src/Bitmap.h | 4 ++-- src/Graphics_3DS.c | 4 ++-- src/Graphics_D3D11.c | 13 ++++--------- src/Graphics_D3D9.c | 4 ++-- src/Platform_GCWii.c | 17 +++++++---------- src/Resources.c | 4 ++-- src/Window_GCWii.c | 10 +++++----- src/_GLShared.h | 4 ++-- 9 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/Bitmap.c b/src/Bitmap.c index 043423fae..6f319a426 100644 --- a/src/Bitmap.c +++ b/src/Bitmap.c @@ -645,9 +645,9 @@ static void Png_EncodeRow(const cc_uint8* cur, const cc_uint8* prior, cc_uint8* best[0] = bestFilter; } -static BitmapCol* DefaultGetRow(struct Bitmap* bmp, int y) { return Bitmap_GetRow(bmp, y); } +static BitmapCol* DefaultGetRow(struct Bitmap* bmp, int y, void* ctx) { return Bitmap_GetRow(bmp, y); } cc_result Png_Encode(struct Bitmap* bmp, struct Stream* stream, - Png_RowGetter getRow, cc_bool alpha) { + Png_RowGetter getRow, cc_bool alpha, void* ctx) { cc_uint8 tmp[32]; /* TODO: This should be * 4 for alpha (should switch to mem_alloc though) */ cc_uint8 prevLine[PNG_MAX_DIMS * 3], curLine[PNG_MAX_DIMS * 3]; @@ -691,7 +691,7 @@ cc_result Png_Encode(struct Bitmap* bmp, struct Stream* stream, Mem_Set(prevLine, 0, lineSize); for (y = 0; y < bmp->height; y++) { - BitmapCol* src = getRow(bmp, y); + BitmapCol* src = getRow(bmp, y, ctx); cc_uint8* prev = (y & 1) == 0 ? prevLine : curLine; cc_uint8* cur = (y & 1) == 0 ? curLine : prevLine; diff --git a/src/Bitmap.h b/src/Bitmap.h index ca7cc6d81..c8688cc09 100644 --- a/src/Bitmap.h +++ b/src/Bitmap.h @@ -89,7 +89,7 @@ CC_API void Bitmap_Scale(struct Bitmap* dst, struct Bitmap* src, /* Whether data starts with PNG format signature/identifier. */ cc_bool Png_Detect(const cc_uint8* data, cc_uint32 len); -typedef BitmapCol* (*Png_RowGetter)(struct Bitmap* bmp, int row); +typedef BitmapCol* (*Png_RowGetter)(struct Bitmap* bmp, int row, void* ctx); /* Decodes a bitmap in PNG format. Partially based off information from https://handmade.network/forums/wip/t/2363-implementing_a_basic_png_reader_the_handmade_way @@ -100,5 +100,5 @@ CC_API cc_result Png_Decode(struct Bitmap* bmp, struct Stream* stream); /* getRow is optional. Can be used to modify how rows are encoded. (e.g. flip image) */ /* if alpha is non-zero, RGBA channels are saved, otherwise only RGB channels are. */ cc_result Png_Encode(struct Bitmap* bmp, struct Stream* stream, - Png_RowGetter getRow, cc_bool alpha); + Png_RowGetter getRow, cc_bool alpha, void* ctx); #endif diff --git a/src/Graphics_3DS.c b/src/Graphics_3DS.c index eff743854..cba33088b 100644 --- a/src/Graphics_3DS.c +++ b/src/Graphics_3DS.c @@ -445,7 +445,7 @@ void Gfx_EndFrame(void) { if (gfx_minFrameMs) LimitFPS(); GPUBuffers_DeleteUnreferenced(); - //GPUTextures_DeleteUnreferenced(); + GPUTextures_DeleteUnreferenced(); frameCounter++; } @@ -853,4 +853,4 @@ void Gfx_Draw2DTexture(const struct Texture* tex, PackedCol color) { C3D_ImmSendAttrib(v[0].U, v[0].V, 0.0f, 0.0f); C3D_ImmDrawEnd(); } -#endif \ No newline at end of file +#endif diff --git a/src/Graphics_D3D11.c b/src/Graphics_D3D11.c index bcb596f09..4954db3a6 100644 --- a/src/Graphics_D3D11.c +++ b/src/Graphics_D3D11.c @@ -1055,13 +1055,8 @@ void Gfx_DepthOnlyRendering(cc_bool depthOnly) { /*########################################################################################################################* *-----------------------------------------------------------Misc----------------------------------------------------------* *#########################################################################################################################*/ -static BitmapCol* D3D11_GetRow(struct Bitmap* bmp, int y) { - // You were expecting a BitmapCol*, but it was me, D3D11_MAPPED_SUBRESOURCE*! - // This is necessary because the stride of the mapped backbuffer often doesn't equal width of the bitmap - // e.g. with backbuffer width of 854, stride is 3456 bytes instead of expected 3416 (854*4) - // Therefore have to calculate row address manually instead of using Bitmap_GetRow - D3D11_MAPPED_SUBRESOURCE* buffer = (D3D11_MAPPED_SUBRESOURCE*)bmp->scan0; - +static BitmapCol* D3D11_GetRow(struct Bitmap* bmp, int y, void* ctx) { + D3D11_MAPPED_SUBRESOURCE* buffer = (D3D11_MAPPED_SUBRESOURCE*)ctx; char* row = (char*)buffer->pData + y * buffer->RowPitch; return (BitmapCol*)row; } @@ -1094,8 +1089,8 @@ cc_result Gfx_TakeScreenshot(struct Stream* output) { hr = ID3D11DeviceContext_Map(context, tmp, 0, D3D11_MAP_READ, 0, &buffer); if (hr) goto finished; { - Bitmap_Init(bmp, desc.Width, desc.Height, (BitmapCol*)&buffer); - hr = Png_Encode(&bmp, output, D3D11_GetRow, false); + Bitmap_Init(bmp, desc.Width, desc.Height, NULL); + hr = Png_Encode(&bmp, output, D3D11_GetRow, false, &buffer); } ID3D11DeviceContext_Unmap(context, tmp, 0); diff --git a/src/Graphics_D3D9.c b/src/Graphics_D3D9.c index cd980b9ae..8ec050be2 100644 --- a/src/Graphics_D3D9.c +++ b/src/Graphics_D3D9.c @@ -335,7 +335,7 @@ static GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, cc_uint8 flags, cc_boo int mipmapsLevels = CalcMipmapsLevels(bmp->width, bmp->height); int levels = 1 + (mipmaps ? mipmapsLevels : 0); - + if (flags & TEXTURE_FLAG_MANAGED) { while ((res = IDirect3DDevice9_CreateTexture(device, bmp->width, bmp->height, levels, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &tex, NULL))) @@ -812,7 +812,7 @@ cc_result Gfx_TakeScreenshot(struct Stream* output) { if (res) goto finished; { Bitmap_Init(bmp, desc.Width, desc.Height, (BitmapCol*)rect.pBits); - res = Png_Encode(&bmp, output, NULL, false); + res = Png_Encode(&bmp, output, NULL, false, NULL); if (res) { IDirect3DSurface9_UnlockRect(temp); goto finished; } } res = IDirect3DSurface9_UnlockRect(temp); diff --git a/src/Platform_GCWii.c b/src/Platform_GCWii.c index d3224198e..33519a7e8 100644 --- a/src/Platform_GCWii.c +++ b/src/Platform_GCWii.c @@ -65,24 +65,21 @@ static void LogOverEXI(char* msg, int len) { EXI_Unlock( EXI_CHANNEL_0); } -// dolphin recognises this function name (if loaded as .elf), and will patch it -// to also log the message to dolphin's console at OSREPORT-HLE log level -void CC_NOINLINE __write_console(int fd, const char* msg, const u32* size) { - write(STDOUT_FILENO, msg, *size); // this can be intercepted by libogc debug console -} void Platform_Log(const char* msg, int len) { char tmp[256 + 1]; len = min(len, 256); - // See EXI_DeviceIPL.cpp, \r is what triggers message to be logged + // See EXI_DeviceIPL.cpp in Dolphin, \r is what triggers buffered message to be logged Mem_Copy(tmp, msg, len); tmp[len] = '\r'; - LogOverEXI(buffer, size); + LogOverEXI(tmp, len + 1); } +#define GCWII_EPOCH_ADJUST 946684800ULL // GameCube/Wii time epoch is year 2000, not 1970 + TimeMS DateTime_CurrentUTC(void) { - struct timeval cur; - gettimeofday(&cur, NULL); - return (cc_uint64)cur.tv_sec + UNIX_EPOCH_SECONDS; + u64 raw = gettime(); + u64 secs = ticks_to_secs(raw); + return secs + UNIX_EPOCH_SECONDS + GCWII_EPOCH_ADJUST; } void DateTime_CurrentLocal(struct DateTime* t) { diff --git a/src/Resources.c b/src/Resources.c index 8420cfb30..a81710053 100644 --- a/src/Resources.c +++ b/src/Resources.c @@ -592,8 +592,8 @@ static cc_result ZipWriter_WritePng(struct Stream* dst, struct ResourceZipEntry* struct Bitmap* src = &e->value.bmp; cc_result res; - if ((res = ZipWriter_LocalFile(dst, e))) return res; - if ((res = Png_Encode(src, dst, NULL, true))) return res; + if ((res = ZipWriter_LocalFile(dst, e))) return res; + if ((res = Png_Encode(src, dst, NULL, true, NULL))) return res; return ZipWriter_FixupLocalFile(dst, e); } diff --git a/src/Window_GCWii.c b/src/Window_GCWii.c index e05826e2c..1692ac4bd 100644 --- a/src/Window_GCWii.c +++ b/src/Window_GCWii.c @@ -459,13 +459,13 @@ static u32 CvtRGB (u8 r1, u8 g1, u8 b1, u8 r2, u8 g2, u8 b2) { int y1, cb1, cr1, y2, cb2, cr2, cb, cr; - y1 = (299 * r1 + 587 * g1 + 114 * b1) / 1000; + y1 = (299 * r1 + 587 * g1 + 114 * b1) / 1000; cb1 = (-16874 * r1 - 33126 * g1 + 50000 * b1 + 12800000) / 100000; - cr1 = (50000 * r1 - 41869 * g1 - 8131 * b1 + 12800000) / 100000; + cr1 = (50000 * r1 - 41869 * g1 - 8131 * b1 + 12800000) / 100000; - y2 = (299 * r2 + 587 * g2 + 114 * b2) / 1000; + y2 = (299 * r2 + 587 * g2 + 114 * b2) / 1000; cb2 = (-16874 * r2 - 33126 * g2 + 50000 * b2 + 12800000) / 100000; - cr2 = (50000 * r2 - 41869 * g2 - 8131 * b2 + 12800000) / 100000; + cr2 = (50000 * r2 - 41869 * g2 - 8131 * b2 + 12800000) / 100000; cb = (cb1 + cb2) >> 1; cr = (cr1 + cr2) >> 1; @@ -538,4 +538,4 @@ cc_result Window_OpenFileDialog(const struct OpenFileDialogArgs* args) { cc_result Window_SaveFileDialog(const struct SaveFileDialogArgs* args) { return ERR_NOT_SUPPORTED; } -#endif \ No newline at end of file +#endif diff --git a/src/_GLShared.h b/src/_GLShared.h index c6d7fa10d..ca82a6ace 100644 --- a/src/_GLShared.h +++ b/src/_GLShared.h @@ -232,7 +232,7 @@ void Gfx_CalcPerspectiveMatrix(struct Matrix* matrix, float fov, float aspect, f /*########################################################################################################################* *-----------------------------------------------------------Misc----------------------------------------------------------* *#########################################################################################################################*/ -static BitmapCol* GL_GetRow(struct Bitmap* bmp, int y) { +static BitmapCol* GL_GetRow(struct Bitmap* bmp, int y, void* ctx) { /* OpenGL stores bitmap in bottom-up order, so flip order when saving */ return Bitmap_GetRow(bmp, (bmp->height - 1) - y); } @@ -249,7 +249,7 @@ cc_result Gfx_TakeScreenshot(struct Stream* output) { if (!bmp.scan0) return ERR_OUT_OF_MEMORY; glReadPixels(0, 0, bmp.width, bmp.height, PIXEL_FORMAT, TRANSFER_FORMAT, bmp.scan0); - res = Png_Encode(&bmp, output, GL_GetRow, false); + res = Png_Encode(&bmp, output, GL_GetRow, false, NULL); Mem_Free(bmp.scan0); return res; }