Swizzle graphics - Gemini-Loboto3/DC2-Mod-SDK GitHub Wiki

Most of the original game's textures are swizzled or, in other terms, arranged in blocks of 2048 bytes per slice. This is a trick used by the engine to move data to video ram in little chunks without having to hold the whole picture in ram before DMA takes place. On PC the game behaves differently and unswizzles data so that a whole texture can be reassembled from all the chunks.

This is the C++ unswizzling algorithm used to reassemble graphics in the correct order:

void UnswizzleGfx(DC2_ENTRY_GFX* gfx_p, u8* buf_p)
{
	// create temp copy buffer
	u8* buffer = new u8[gfx_p->size];
	memcpy(buffer, buf_p, gfx_p->size);

	u8* bcopy = buffer;
	int tw = gfx_p->w / 32;	// horizontal counter
	int bw = tw * 64;	// texture scanline size
	// rearrange swizzled texture
	for (int yi = 0; yi < gfx_p->h; yi += 32)
	{
		for (int xi = 0; xi < tw; xi++)
		{
			// get to current dest scanline chunk
			u8* scanline = &buf_p[yi * bw + xi * 64];
			// blit 32 scanlines
			for (int j = 0; j < 32; j++)
			{
				memcpy(scanline, bcopy, 64);
				bcopy += 64;		// next swizzled chunk
				scanline += bw;		// next pixel scanline
			}
		}
	}

	// remove temp copy
	delete[] buffer;
}
⚠️ **GitHub.com Fallback** ⚠️