Fix the stall model for a bloom render pass
Your task
Your renderer's bloom pass maps a parameter buffer the GPU may still be reading, and reads back a luminance value the GPU may not have written yet. Either one makes the CPU wait for the GPU, and the profiler shows a stall every frame. You model which frames stall for a given buffer setup.
An assistant wrote simulate_frame_sync(frame_count, ring_size, frames_in_flight, readback_delay). It is in the editor. It has a bug. Find it and fix it.
Input and output:
frame_count: how many frames to simulate, numbered from 0.ring_size: how many copies of the parameter buffer exist. Framefwrites copyf % ring_size.frames_in_flight: how far the GPU runs behind. Work from framegis finished once the CPU starts frameg + frames_in_flight.readback_delay: framefreads the luminance written by framef - readback_delay.- Return one boolean per frame: whether the CPU stalls on that frame.
Rules the tests check:
- If
frame_countis 0 or less, return[]. Aring_sizeorframes_in_flightbelow 1 counts as 1, and a negativereadback_delaycounts as 0. - Map stall: the first
ring_sizeframes write copies nobody has used yet, so they never wait. From framering_sizeon, framefstalls whenring_sizeis less thanframes_in_flight. - Readback stall: frame
fstalls whenf - readback_delayis 0 or more andreadback_delayis less thanframes_in_flight. Before that, no result exists yet, so there is nothing to wait for. - A frame stalls when either rule says so.
Examples
simulate_frame_sync(frame_count: int, ring_size: int, frames_in_flight: int, readback_delay: int) -> list[bool]Example 1
Input
simulate_frame_sync(4, 1, 2, 0)Output
[True, True, True, True]Example 2
Input
simulate_frame_sync(4, 2, 2, 2)Output
[False, False, False, False]