Refactor CopyTextureData to allow customising rows/pixelsPerRow

This commit is contained in:
UnknownShadow200 2025-07-11 19:24:26 +10:00
parent 3ca40dae17
commit 07026d593c
9 changed files with 50 additions and 42 deletions

View File

@ -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");

View File

@ -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, &params);
}
void Gfx_DeleteTexture(GfxResourceID* texId) {

View File

@ -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) { }

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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) { }

View File

@ -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);
}