Skip to content

Cache shared horizontal sprite flips - #287

Open
fderop wants to merge 1 commit into
cortex-command-community:developmentfrom
fderop:perf/cache-flipped-sprites
Open

fderop wants to merge 1 commit into
cortex-command-community:developmentfrom
fderop:perf/cache-flipped-sprites

Conversation

@fderop

@fderop fderop commented Oct 2, 2026 •

Copy link
Copy Markdown

Flipped sprites rebuilt the same scratch image on every draw, even when many objects shared one source bitmap.
ContentFile now creates that flip once and reuses it:

BITMAP* ContentFile::GetFlippedBitmap(BITMAP* bitmap) {
	auto entry = s_FlippedBitmaps.find(bitmap);
	if (entry == s_FlippedBitmaps.end()) {
		return nullptr;
	}
	if (!entry->second) {
		entry->second = create_bitmap_ex(8, bitmap->w, bitmap->h);
		clear_to_color(entry->second, ColorKeys::g_MaskColor);
		draw_sprite_h_flip(entry->second, bitmap, 0, 0);
	}
	return entry->second;
}

Only content-owned, unmodified color sprites with matching dimensions use the cache. Edited sprites and clones retain the scratch path.
Reload, ownership transfer, and cleanup invalidate cached flips.
Transparent GPU draws skip the unused CPU flip preparation.

All 1,096 CPU pixel cases and six GPU cases match baseline.
Three process pairs show 24.9–35.5% less repeated draw time and 21.1% less gameplay color-drawing time.
Overall simulation results remain inconclusive, with slightly worse p95 timing.

First draws of 512 unique assets cost 4.6–6.0% more.
The cache adds one image per flipped source: about 2.30 MiB for 512 distinct 64×64 sprites, excluding map and allocator overhead.
These results do not establish an FPS gain.

Production changes touch MOSRotating.cpp and ContentFile.cpp/.h. Pixel checks, raw results, and reproduction instructions.

@fderop
fderop marked this pull request as ready for review October 2, 2026 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant