Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defrag the sprite batcher before every flush #275

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bullno1
Copy link
Contributor

@bullno1 bullno1 commented Mar 14, 2025

This ensures that the atlas is defragged before every flush.
Both RAM and VRAM usage are reduced significantly.
No change to the cycling flag is needed.

However, I have no idea how this affects performance since there is another per-frame defrag.

@bullno1 bullno1 changed the title Defrag the sprite batcher before every flush WIP: Defrag the sprite batcher before every flush Mar 14, 2025
@bullno1 bullno1 marked this pull request as draft March 14, 2025 01:52
@bullno1 bullno1 changed the title WIP: Defrag the sprite batcher before every flush Defrag the sprite batcher before every flush Mar 14, 2025
@bullno1
Copy link
Contributor Author

bullno1 commented Mar 14, 2025

This seems to work but I think it should be a dirty flag instead?

Like:

  • If there is a new sprite, mark the batcher as dirty.
  • If there is a flush, defrag if dirty and clear the dirty flag.
  • Per frame, defrag on tick if not already.

@pusewicz
Copy link
Contributor

This branch brings my memory usage in the demo to normal values (650MB) which I think is fine (it always seems to be more on macOS). The CPU is on ~100% though.

@bullno1
Copy link
Contributor Author

bullno1 commented Mar 14, 2025

If you turn on vsync (cf_app_set_vsync(true)), does CPU usage drop?

Also, I suspect that calling defrag so often is suboptimal.
Will look into the dirty flag idea later.

@pusewicz
Copy link
Contributor

Much better, but still it's higher at around ~30%.

Here's htop:

image

Here's my Activity Monitor:

image

But you know what, even on my branch the CPU usage is the same. Also even with vsync false I see it at ~65%. I guess depends what display I run it on.

To sum up, the CPU usage seems to be the same before and after the change.

@bullno1
Copy link
Contributor Author

bullno1 commented Mar 14, 2025

16 threads....

Yeah, it's probably your driver.

@pusewicz
Copy link
Contributor

I don't think the thread count is an issue. But again, I think this change works.

@RandyGaul
Copy link
Owner

RandyGaul commented Mar 14, 2025

Defrag is designed to be called per frame. Internally it won’t do work until needed, and the work is almost always worth doing.

@bullno1
Copy link
Contributor Author

bullno1 commented Mar 14, 2025

So calling it before every flush is fine?

@bullno1 bullno1 marked this pull request as ready for review March 14, 2025 17:24
@RandyGaul
Copy link
Owner

RandyGaul commented Mar 14, 2025

Yes. There was an issue with imgui though, needing to defrag/flush after drawing, but there's an if-statement and dirty-flag for this already. The delay was needed to avoid invalidating texture handles for ImGui to draw CF sprites, this let CF inject it's sprites for rendering within Dear ImGui.

@bullno1
Copy link
Contributor Author

bullno1 commented Mar 19, 2025

How to approach this though? Can the handles in dearimgui be "virtualized" or indirectly looked up somehow?

Because as I understand it, if defrag is delayed before flush it leads to this problem.

Alternatively, dearimgui can be treated as "debug tool" and its rendering is delayed in the case of a flush ... somehow and its entire command queue just get discarded.

@RandyGaul
Copy link
Owner

Did you test with imgui?

@bullno1
Copy link
Contributor Author

bullno1 commented Mar 20, 2025

Only the samples but none of them render images

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.

3 participants