A queue of VRAM uploads, filled during the frame and sent in VBlank. More...
#include <snes/types.h>Go to the source code of this file.
Macros | |
| #define | VRAM_QUEUE_COLUMN 0x81 |
| Step after each word: 32 words on (a column of a 32-wide tilemap). | |
| #define | VRAM_QUEUE_ENTRY_COST 80 |
| What one entry costs the flush beyond its bytes, in bytes. | |
| #define | VRAM_QUEUE_MAX 32 |
| Transfers the queue holds. | |
| #define | VRAM_QUEUE_ROW 0x80 |
| Step after each word: the next VRAM word (a row, tiles). | |
| #define | VRAM_QUEUE_UNTIL_COST 64 |
| Bytes of the budget vramQueueFlushUntil() spends before its first transfer. | |
| #define | vramQueueCost() |
| VBlank cost of what is queued, in bytes: each entry's size plus VRAM_QUEUE_ENTRY_COST. | |
| #define | vramQueueFree() |
| Entries still free. | |
Functions | |
| void | vramQueueFlush (void) |
| Send every queued transfer to VRAM and empty the queue. | |
| u16 | vramQueueFlushUntil (u16 line) |
| Send the queued transfers that end before a scanline; keep the rest. | |
| u16 | vramQueuePush (const u8 *src, u16 addr, u16 size, u16 step) |
| Note one transfer for the next vramQueueFlush(). | |
| u16 | vramQueuePushSprite (const u8 *src, u16 addr, u16 size_px) |
| Note one frame of a streamed sprite: all its strips in one call. | |
| u16 | vramQueuePushSpriteWithin (const u8 *src, u16 addr, u16 size_px, u16 budget) |
| vramQueuePushSprite(), if the whole frame fits a budget you give | |
| u16 | vramQueuePushWithin (const u8 *src, u16 addr, u16 size, u16 step, u16 budget) |
| vramQueuePush(), if it fits a budget you give: the guard written once | |
Variables | |
| u16 | vram_queue_addr [32] |
| u16 | vram_queue_bank [32] |
| u16 | vram_queue_cost |
| u16 | vram_queue_count |
| u16 | vram_queue_cum [32] |
| u16 | vram_queue_refused |
| Pushes refused since power-on (queue full): 0 on a healthy run. Counted whether or not the caller reads the return value; a test reads it from WRAM. Wraps at 65536. | |
| u16 | vram_queue_size [32] |
| u16 | vram_queue_src [32] |
| u16 | vram_queue_step [32] |
A queue of VRAM uploads, filled during the frame and sent in VBlank.
A game that streams graphics during play — sprite frames, the rows and columns of a scrolling map — makes several small VRAM transfers in each VBlank. One dmaCopyVram() call per transfer costs about as much to set up from C as it does to run for 128 bytes, and the VBlank is short. Here a transfer is noted while the frame is computed, when time is not short, and all of them are sent in VBlank by one assembly routine:
What it buys is VBlank time, not time. Noting an entry is a call too, so in total the queue costs more than the dmaCopyVram() calls it replaces. But six 128-byte transfers called directly put 15,900 master cycles in the VBlank, and queued only 10,600 — 8 per byte plus about 740 per entry, the call to the flush included (devtools/libbench, rows vramc and vramq).
The budget is yours to keep. A VBlank is about 49,000 master cycles and the NMI handler takes the first 5,000 to 8,000 of them. What does not fit lands in active display, where the PPU ignores it: no error, missing tiles. vramQueueCost() says what the entries already queued will take, whichever module queued them.
Request and first user: issue #165.
| #define VRAM_QUEUE_COLUMN 0x81 |
Step after each word: 32 words on (a column of a 32-wide tilemap).
| #define VRAM_QUEUE_ENTRY_COST 80 |
What one entry costs the flush beyond its bytes, in bytes.
The flush spends 8 master cycles per byte and, in its loop, 619 per entry (SlowROM; 560 with USE_FASTROM): 619 / 8 is 77 bytes' worth, rounded up.
| #define VRAM_QUEUE_MAX 32 |
Transfers the queue holds.
32 unless the project's Makefile says otherwise (VRAM_QUEUE_MAX := 64, 1 to 255): the build gives the C files this value and sizes the queue's arrays to it (templates/vramqueue_ram.asm). Each entry takes 12 bytes of the C RAM band ($0000-$1FFF). vramQueueFlush() sends every entry in one call; vramQueueFlushUntil() sends what ends before a given scanline.
| #define VRAM_QUEUE_ROW 0x80 |
Step after each word: the next VRAM word (a row, tiles).
| #define VRAM_QUEUE_UNTIL_COST 64 |
Bytes of the budget vramQueueFlushUntil() spends before its first transfer.
| #define vramQueueCost | ( | ) |
VBlank cost of what is queued, in bytes: each entry's size plus VRAM_QUEUE_ENTRY_COST.
Kept by vramQueuePush() and vramQueuePushSprite(), set to 0 by vramQueueFlush(), to the cost of what is left by vramQueueFlushUntil(); it stops at 0xFFFF rather than wrap. Multiply by 8 for master cycles. A module that streams what is left of the VBlank checks it against its own budget before each push:
A count written by hand (vram_queue_count = 0) leaves it as it was until the next flush.
| #define vramQueueFree | ( | ) |
Entries still free.
| void vramQueueFlush | ( | void | ) |
Send every queued transfer to VRAM and empty the queue.
One DMA per entry, on channel 0. Call it in VBlank — right after WaitForVBlank() — or in force blank: it writes VRAM. With an empty queue it returns at once. VMAIN is left at $80, as the rest of the library leaves it.
Send the queued transfers that end before a scanline; keep the rest.
Reads the vertical counter once, turns the lines left before line into a budget (165 bytes of vramQueueCost() a line: 1324 master cycles, the DRAM refresh taken out), and sends the entries in queue order while their running cost fits. The first one that does not fit stops it: the entries left move to the head of the queue in the same order and go first at the next flush, so a VRAM address pushed twice still receives the second transfer last. Use it to fill every VBlank whatever the NMI took, and push beyond a safe budget.
| line | Scanline by whose start the transfers must be over |
Note one transfer for the next vramQueueFlush().
| src | Source bytes (ROM, plain RAM or FAR RAM) The queue keeps the POINTER: the bytes are read at the flush, not here. They must not change before vramQueueFlush(); a buffer filled again before the flush sends its last contents for every entry that points to it. |
| addr | VRAM word address |
| size | Bytes (even; an entry of 0 bytes is skipped by the flush) |
| step | VRAM_QUEUE_ROW or VRAM_QUEUE_COLUMN |
Note one frame of a streamed sprite: all its strips in one call.
A sprite frame in OBJ VRAM is size_px / 8 rows of tiles; each row is a strip of size_px * 4 bytes (at 4 bpp), and the rows are 16 tiles apart: 512 bytes in the sheet, 256 words in VRAM. That is the layout opensnes-sprite sheet writes (a 128-pixel-wide raster) and the one OBJ VRAM expects. This notes the 1, 2, 4 or 8 strips of one frame.
| src | First byte of the frame in the sheet (its top-left tile). The frame must not straddle a bank boundary. Read at the flush, like vramQueuePush(): the bytes must not change before vramQueueFlush(). |
| addr | VRAM word address of the frame's top-left tile |
| size_px | Width and height of the sprite: 8, 16, 32 or 64 |
vramQueuePushSprite(), if the whole frame fits a budget you give
Notes every strip of the frame only when vramQueueCost() + (size_px / 8)
and all the strips fit in the queue (288 for a 16x16 frame, 832 for a 32x32 one). Otherwise returns 0 and changes nothing, and does not count a refusal (vram_queue_refused is for a queue that was full, not for a frame that waits for the next one): that is what sets it apart from vramQueuePushSprite(), which counts its refusals. A sum past $FFFF is a refusal, never a wrap. The budget is yours: deciding what each module may spend stays with the game; this only does the test and the push together.
| src,addr,size_px | As vramQueuePushSprite() |
| budget | Most the queue may cost, in vramQueueCost()'s unit (bytes + VRAM_QUEUE_ENTRY_COST an entry) |
vramQueuePush(), if it fits a budget you give: the guard written once
Notes the transfer only when vramQueueCost() + size + VRAM_QUEUE_ENTRY_COST <= budget and the queue has a free entry. Otherwise returns 0 and changes nothing, and does not count a refusal (vram_queue_refused is for a queue that was full, not for a transfer that waits for the next frame). The budget is yours: deciding what each module may spend stays with the game; this only does the test and the push together.
| src,addr,size,step | As vramQueuePush() |
| budget | Most the queue may cost, in vramQueueCost()'s unit (bytes + VRAM_QUEUE_ENTRY_COST an entry) |
|
extern |
|
extern |
|
extern |
|
extern |
|
extern |
|
extern |
Pushes refused since power-on (queue full): 0 on a healthy run. Counted whether or not the caller reads the return value; a test reads it from WRAM. Wraps at 65536.
|
extern |
|
extern |
|
extern |