From 07026d593c64bdd62ec21a6ac658fb42ce0bfd6f Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 11 Jul 2025 19:24:26 +1000 Subject: [PATCH] Refactor CopyTextureData to allow customising rows/pixelsPerRow --- src/Graphics_D3D9.c | 8 ++++++-- src/Graphics_N64.c | 20 +++++--------------- src/Graphics_SoftGPU.c | 11 +++++++---- src/Graphics_WiiU.c | 10 ++++++---- src/Graphics_Xbox360.c | 5 +++-- src/_GLShared.h | 5 +++-- src/_GraphicsBase.h | 12 +++++++----- src/ps2/Graphics_PS2.c | 10 ++++++---- src/psp/Graphics_PSP.c | 11 +++++++---- 9 files changed, 50 insertions(+), 42 deletions(-) diff --git a/src/Graphics_D3D9.c b/src/Graphics_D3D9.c index b42b34fc2..861ea6080 100644 --- a/src/Graphics_D3D9.c +++ b/src/Graphics_D3D9.c @@ -260,7 +260,9 @@ static void D3D9_SetTextureData(IDirect3DTexture9* texture, struct Bitmap* bmp, cc_result res = IDirect3DTexture9_LockRect(texture, lvl, &rect, NULL, 0); if (res) Process_Abort2(res, "D3D9_LockTextureData"); - CopyTextureData(rect.pBits, rect.Pitch, bmp, rowWidth * BITMAPCOLOR_SIZE); + CopyPixels(rect.pBits, rect.Pitch, + bmp->scan0, rowWidth * BITMAPCOLOR_SIZE, + bmp->width, bmp->height); res = IDirect3DTexture9_UnlockRect(texture, lvl); if (res) Process_Abort2(res, "D3D9_UnlockTextureData"); @@ -276,7 +278,9 @@ static void D3D9_SetTexturePartData(IDirect3DTexture9* texture, int x, int y, co res = IDirect3DTexture9_LockRect(texture, lvl, &rect, &part, 0); if (res) Process_Abort2(res, "D3D9_LockTexturePartData"); - CopyTextureData(rect.pBits, rect.Pitch, bmp, rowWidth * BITMAPCOLOR_SIZE); + CopyPixels(rect.pBits, rect.Pitch, + bmp->scan0, rowWidth * BITMAPCOLOR_SIZE, + bmp->width, bmp->height); res = IDirect3DTexture9_UnlockRect(texture, lvl); if (res) Process_Abort2(res, "D3D9_UnlockTexturePartData"); diff --git a/src/Graphics_N64.c b/src/Graphics_N64.c index 5b011cb6e..6b7a9061e 100644 --- a/src/Graphics_N64.c +++ b/src/Graphics_N64.c @@ -96,7 +96,7 @@ void Gfx_BeginFrame(void) { surface_t* disp = display_get(); rdpq_attach(disp, &zbuffer); - Platform_LogConst("GFX ctx beg"); + Platform_LogConst("== BEGIN frame"); } extern void __rdpq_autosync_change(int mode); @@ -120,9 +120,7 @@ void Gfx_ClearColor(PackedCol color) { } void Gfx_EndFrame(void) { - Platform_LogConst("GFX ctx end"); rdpq_detach_show(); - //Platform_LogConst("GFX END"); //rspq_profile_dump(); //rspq_profile_next_frame(); @@ -154,9 +152,7 @@ void Gfx_BindTexture(GfxResourceID texId) { static void UploadTexture(CCTexture* tex, rdpq_texparms_t* params) { rspq_block_begin(); - rdpq_tex_multi_begin(); rdpq_tex_upload(TILE0, &tex->surface, params); - rdpq_tex_multi_end(); tex->upload_block = rspq_block_end(); } @@ -189,8 +185,9 @@ GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, } } else { // 32 bpp can just be copied straight across - CopyTextureData(fb->buffer, fb->stride, - bmp, rowWidth * BITMAPCOLOR_SIZE); + CopyPixels(fb->buffer, fb->stride, + bmp->scan0, rowWidth * BITMAPCOLOR_SIZE, + bmp->width, bmp->height); } rdpq_texparms_t params = @@ -205,6 +202,7 @@ GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, int rowWidth, cc_bool mipmaps) { CCTexture* tex = (CCTexture*)texId; surface_t* fb = &tex->surface; + cc_uint32* src = (cc_uint32*)part->scan0 + x; cc_uint8* dst = (cc_uint8*)fb->buffer + (x * 4) + (y * fb->stride); @@ -214,14 +212,6 @@ void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, i src + srcY * rowWidth, part->width * 4); } - - rdpq_texparms_t params = (rdpq_texparms_t){ - .s.repeats = REPEAT_INFINITE, - .t.repeats = REPEAT_INFINITE, - }; - - rdpq_call_deferred((void (*)(void*))rspq_block_free, tex->upload_block); - UploadTexture(tex, ¶ms); } void Gfx_DeleteTexture(GfxResourceID* texId) { diff --git a/src/Graphics_SoftGPU.c b/src/Graphics_SoftGPU.c index f0d40f27a..ded68ac82 100644 --- a/src/Graphics_SoftGPU.c +++ b/src/Graphics_SoftGPU.c @@ -106,8 +106,10 @@ GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, tex->width = bmp->width; tex->height = bmp->height; - CopyTextureData(tex->pixels, bmp->width * BITMAPCOLOR_SIZE, - bmp, rowWidth * BITMAPCOLOR_SIZE); + + CopyPixels(tex->pixels, bmp->width * BITMAPCOLOR_SIZE, + bmp->scan0, rowWidth * BITMAPCOLOR_SIZE, + bmp->width, bmp->height); return tex; } @@ -115,8 +117,9 @@ void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, i CCTexture* tex = (CCTexture*)texId; BitmapCol* dst = (tex->pixels + x) + y * tex->width; - CopyTextureData(dst, tex->width * BITMAPCOLOR_SIZE, - part, rowWidth * BITMAPCOLOR_SIZE); + CopyPixels(dst, tex->width * BITMAPCOLOR_SIZE, + part->scan0, rowWidth * BITMAPCOLOR_SIZE, + bmp->width, bmp->height); } void Gfx_EnableMipmaps(void) { } diff --git a/src/Graphics_WiiU.c b/src/Graphics_WiiU.c index 2ee95a51d..fec7b881f 100644 --- a/src/Graphics_WiiU.c +++ b/src/Graphics_WiiU.c @@ -207,8 +207,9 @@ GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, tex->surface.image = MEMAllocFromDefaultHeapEx(tex->surface.imageSize, tex->surface.alignment); if (!tex->surface.image) { Mem_Free(tex); return NULL; } - CopyTextureData(tex->surface.image, tex->surface.pitch << 2, - bmp, rowWidth * BITMAPCOLOR_SIZE); + CopyPixels(tex->surface.image, tex->surface.pitch << 2, + bmp->scan0, rowWidth * BITMAPCOLOR_SIZE, + bmp->width, bmp->height); GX2Invalidate(GX2_INVALIDATE_MODE_CPU_TEXTURE, tex->surface.image, tex->surface.imageSize); return tex; } @@ -217,8 +218,9 @@ void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, i GX2Texture* tex = (GX2Texture*)texId; uint32_t* dst = (uint32_t*)tex->surface.image + (y * tex->surface.pitch) + x; - CopyTextureData(dst, tex->surface.pitch << 2, - part, rowWidth * BITMAPCOLOR_SIZE); + CopyPixels(dst, tex->surface.pitch << 2, + part->scan0, rowWidth * BITMAPCOLOR_SIZE, + part->width, part->height); GX2Invalidate(GX2_INVALIDATE_MODE_CPU_TEXTURE, tex->surface.image, tex->surface.imageSize); } diff --git a/src/Graphics_Xbox360.c b/src/Graphics_Xbox360.c index 9c1e6f8d0..38a9cba8d 100644 --- a/src/Graphics_Xbox360.c +++ b/src/Graphics_Xbox360.c @@ -96,8 +96,9 @@ static void Gfx_RestoreState(void) { static void SetTextureData(struct XenosSurface* xtex, int x, int y, const struct Bitmap* bmp, int rowWidth, int lvl) { void* dst = Xe_Surface_LockRect(xe, xtex, x, y, bmp->width, bmp->height, XE_LOCK_WRITE); - CopyTextureData(dst, bmp->width * BITMAPCOLOR_SIZE, - bmp, rowWidth * BITMAPCOLOR_SIZE); + CopyPixels(dst, bmp->width * BITMAPCOLOR_SIZE, + bmp->scan0, rowWidth * BITMAPCOLOR_SIZE, + bmp->width, bmp->height); Xe_Surface_Unlock(xe, xtex); } diff --git a/src/_GLShared.h b/src/_GLShared.h index 55845f95a..978de97ec 100644 --- a/src/_GLShared.h +++ b/src/_GLShared.h @@ -163,8 +163,9 @@ static CC_NOINLINE void UpdateTextureSlow(int x, int y, struct Bitmap* part, int ptr = Mem_Alloc(count, 4, "Gfx_UpdateTexture temp"); } - CopyTextureData(ptr, part->width * BITMAPCOLOR_SIZE, - part, rowWidth * BITMAPCOLOR_SIZE); + CopyPixels(ptr, part->width * BITMAPCOLOR_SIZE, + part->scan0, rowWidth * BITMAPCOLOR_SIZE, + part->width, part->height); if (full) { CallTexImage2D(0, part->width, part->height, ptr); diff --git a/src/_GraphicsBase.h b/src/_GraphicsBase.h index aba23fff6..a4b404196 100644 --- a/src/_GraphicsBase.h +++ b/src/_GraphicsBase.h @@ -321,17 +321,19 @@ void Gfx_UpdateTexturePart(GfxResourceID texId, int x, int y, struct Bitmap* par Gfx_UpdateTexture(texId, x, y, part, part->width, mipmaps); } -static CC_INLINE void CopyTextureData(void* dst, int dstStride, const struct Bitmap* src, int srcStride) { - cc_uint8* src_ = (cc_uint8*)src->scan0; +static CC_INLINE void CopyPixels(void* dst, int dstStride, + const void* src, int srcStride, + int pixelsPerRow, int rows) { + cc_uint8* src_ = (cc_uint8*)src; cc_uint8* dst_ = (cc_uint8*)dst; int y; if (srcStride == dstStride) { - Mem_Copy(dst_, src_, Bitmap_DataSize(src->width, src->height)); + Mem_Copy(dst_, src_, Bitmap_DataSize(pixelsPerRow, rows)); } else { /* Have to copy scanline by scanline */ - for (y = 0; y < src->height; y++) { - Mem_Copy(dst_, src_, src->width * BITMAPCOLOR_SIZE); + for (y = 0; y < rows; y++) { + Mem_Copy(dst_, src_, pixelsPerRow * BITMAPCOLOR_SIZE); src_ += srcStride; dst_ += dstStride; } diff --git a/src/ps2/Graphics_PS2.c b/src/ps2/Graphics_PS2.c index 226da92e6..73200cd7a 100644 --- a/src/ps2/Graphics_PS2.c +++ b/src/ps2/Graphics_PS2.c @@ -377,8 +377,9 @@ GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, ConvertTexture_Palette((cc_uint8*)tex->pixels, bmp, rowWidth, palette, pal_count); } else { tex->format = GS_PSM_32; - CopyTextureData(tex->pixels, bmp->width * BITMAPCOLOR_SIZE, - bmp, rowWidth * BITMAPCOLOR_SIZE); + CopyPixels(tex->pixels, bmp->width * BITMAPCOLOR_SIZE, + bmp->scan0, rowWidth * BITMAPCOLOR_SIZE, + bmp->width, bmp->height); } return tex; } @@ -436,8 +437,9 @@ void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, i CCTexture* tex = (CCTexture*)texId; BitmapCol* dst = (tex->pixels + x) + y * tex->width; - CopyTextureData(dst, tex->width * BITMAPCOLOR_SIZE, - part, rowWidth * BITMAPCOLOR_SIZE); + CopyPixels(dst, tex->width * BITMAPCOLOR_SIZE, + part->scan0, rowWidth * BITMAPCOLOR_SIZE, + part->width, part->height); } void Gfx_EnableMipmaps(void) { } diff --git a/src/psp/Graphics_PSP.c b/src/psp/Graphics_PSP.c index d93c2556d..3c5902df1 100644 --- a/src/psp/Graphics_PSP.c +++ b/src/psp/Graphics_PSP.c @@ -118,8 +118,10 @@ GfxResourceID Gfx_AllocTexture(struct Bitmap* bmp, int rowWidth, cc_uint8 flags, tex->width = bmp->width; tex->height = bmp->height; - CopyTextureData(tex->pixels, bmp->width * BITMAPCOLOR_SIZE, - bmp, rowWidth * BITMAPCOLOR_SIZE); + + CopyPixels(tex->pixels, bmp->width * BITMAPCOLOR_SIZE, + bmp->scan0, rowWidth * BITMAPCOLOR_SIZE, + bmp->width, bmp->height); return tex; } @@ -127,8 +129,9 @@ void Gfx_UpdateTexture(GfxResourceID texId, int x, int y, struct Bitmap* part, i CCTexture* tex = (CCTexture*)texId; BitmapCol* dst = (tex->pixels + x) + y * tex->width; - CopyTextureData(dst, tex->width * BITMAPCOLOR_SIZE, - part, rowWidth * BITMAPCOLOR_SIZE); + CopyPixels(dst, tex->width * BITMAPCOLOR_SIZE, + part->scan0, rowWidth * BITMAPCOLOR_SIZE, + part->width, part->height); // TODO: Do line by line and only invalidate the actually changed parts of lines? sceKernelDcacheWritebackInvalidateRange(dst, (tex->width * part->height) * 4); }