Skip to content

Commit

Permalink
Draw call merge bugfix, added cf_draw_prefetch
Browse files Browse the repository at this point in the history
  • Loading branch information
RandyGaul committed Oct 19, 2024
1 parent 19b4e0e commit 8e7553e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ This is a list of all functions in Cute Framework organized by categories. This
- [cf_draw_pop_shader](/draw/cf_draw_pop_shader.md)
- [cf_draw_pop_vertex_attributes](/draw/cf_draw_pop_vertex_attributes.md)
- [cf_draw_pop_viewport](/draw/cf_draw_pop_viewport.md)
- [cf_draw_prefetch](/draw/cf_draw_prefetch.md)
- [cf_draw_projection](/draw/cf_draw_projection.md)
- [cf_draw_push](/draw/cf_draw_push.md)
- [cf_draw_push_alpha_discard](/draw/cf_draw_push_alpha_discard.md)
Expand Down
33 changes: 33 additions & 0 deletions docs/draw/cf_draw_prefetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[//]: # (This file is automatically generated by Cute Framework's docs parser.)
[//]: # (Do not edit this file by hand!)
[//]: # (See: https://github.com/RandyGaul/cute_framework/blob/master/samples/docs_parser.cpp)
[](../header.md ':include')

# cf_draw_prefetch

Category: [draw](/api_reference?id=draw)
GitHub: [cute_draw.h](https://github.com/RandyGaul/cute_framework/blob/master/include/cute_draw.h)
---

Prefetches a sprite.

```cpp
void cf_draw_prefetch(const CF_Sprite* sprite);
```
Parameters | Description
--- | ---
sprite | The sprite.
## Remarks
This function ensures the sprite is fully loaded into memory without actually rendering anything.
This is a good way to avoid disk io at inconvenient times.
## Related Pages
[cf_draw_sprite](/draw/cf_draw_sprite.md)
[cf_draw_quad](/draw/cf_draw_quad.md)
draw_look_at
cf_draw_to
[cf_app_draw_onto_screen](/app/cf_app_draw_onto_screen.md)
11 changes: 11 additions & 0 deletions include/cute_draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ extern "C" {
*/
CF_API void CF_CALL cf_draw_sprite(const CF_Sprite* sprite);

/**
* @function cf_draw_prefetch
* @category draw
* @brief Prefetches a sprite.
* @param sprite The sprite.
* @remarks This function ensures the sprite is fully loaded into memory without actually rendering anything.
* This is a good way to avoid disk io at inconvenient times.
* @related cf_draw_sprite cf_draw_quad draw_look_at cf_draw_to cf_app_draw_onto_screen
*/
CF_API void CF_CALL cf_draw_prefetch(const CF_Sprite* sprite);

/**
* @function cf_draw_quad
* @category draw
Expand Down
1 change: 1 addition & 0 deletions include/cute_sprite.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/*
Cute Framework
Copyright (C) 2024 Randy Gaul https://randygaul.github.io/
Expand Down
23 changes: 23 additions & 0 deletions src/cute_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,23 @@ void cf_draw_sprite(const CF_Sprite* sprite)
DRAW_PUSH_ITEM(s);
}

void cf_draw_prefetch(const CF_Sprite* sprite)
{
if (sprite->animation) {
for (int i = 0; i < hsize(sprite->animations); ++i) {
const CF_Animation* animation = sprite->animations[i];
for (int j = 0; j < asize(animation->frames); ++j) {
CF_Frame* frame = animation->frames + j;
spritebatch_prefetch(&draw->sb, frame->id, sprite->w, sprite->h);
}
}
} else if (sprite->easy_sprite_id >= CF_PREMADE_ID_RANGE_LO && sprite->easy_sprite_id <= CF_PREMADE_ID_RANGE_HI) {
spritebatch_prefetch(&draw->sb, sprite->easy_sprite_id, sprite->w, sprite->h);
} else {
spritebatch_prefetch(&draw->sb, sprite->easy_sprite_id, sprite->w, sprite->h);
}
}

static void s_draw_quad(CF_V2 p0, CF_V2 p1, CF_V2 p2, CF_V2 p3, float stroke, float radius, bool fill)
{
CF_M3x2 m = draw->mvp;
Expand Down Expand Up @@ -2755,6 +2772,7 @@ static void s_process_command(CF_Canvas canvas, CF_Command* cmd, CF_Command* nex

// Collate all of the drawable items into the spritebatch.
if (!cmd->items.count()) return;
draw->need_flush = true;
for (int j = 0; j < cmd->items.count(); ++j) {
spritebatch_push(&draw->sb, cmd->items[j]);
}
Expand Down Expand Up @@ -2787,6 +2805,7 @@ static void s_process_command(CF_Canvas canvas, CF_Command* cmd, CF_Command* nex

// Process the collated drawable items. Might get split up into multiple draw calls depending on
// the atlas compiler.
draw->need_flush = false;
spritebatch_flush(&draw->sb);
}

Expand Down Expand Up @@ -2818,6 +2837,10 @@ void cf_render_layers_to(CF_Canvas canvas, int layer_lo, int layer_hi, bool clea
if (clear && !draw->has_drawn_something) {
cf_clear_canvas(canvas);
}
if (draw->need_flush) {
draw->need_flush = false;
spritebatch_flush(&draw->sb);
}
draw->has_drawn_something = false;
cf_arena_reset(&draw->uniform_arena);
draw->verts.clear();
Expand Down
1 change: 1 addition & 0 deletions src/internal/cute_draw_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ struct CF_Draw
bool blit_init = false;
CF_Mesh blit_mesh = { 0 };
CF_VertexFn* vertex_fn = NULL;
bool need_flush = false;
bool has_drawn_something = false;
};

Expand Down

0 comments on commit 8e7553e

Please sign in to comment.