cache sprite srcrects

This commit is contained in:
veclav talica 2024-10-15 18:43:02 +03:00
parent 768daf1f54
commit f2bbc1863e
2 changed files with 11 additions and 3 deletions

View File

@ -130,6 +130,10 @@ void render_sprite_batch(const Primitive2D primitives[],
const Rect dims = const Rect dims =
textures_get_dims(&ctx.texture_cache, primitives->sprite.texture_key); textures_get_dims(&ctx.texture_cache, primitives->sprite.texture_key);
/* cached srcrect */
Rect cached_srcrect;
TextureKey cached_srcrect_key = TEXTURE_KEY_INVALID;
/* vertex population over a vertex buffer builder interface */ /* vertex population over a vertex buffer builder interface */
{ {
VertexBufferBuilder payload = build_vertex_buffer(vertex_array, get_quad_payload_size(batch) * batch.size); VertexBufferBuilder payload = build_vertex_buffer(vertex_array, get_quad_payload_size(batch) * batch.size);
@ -139,9 +143,12 @@ void render_sprite_batch(const Primitive2D primitives[],
const size_t cur = batch.mode == TEXTURE_MODE_GHOSTLY ? i : batch.size - i - 1; const size_t cur = batch.mode == TEXTURE_MODE_GHOSTLY ? i : batch.size - i - 1;
const SpritePrimitive sprite = primitives[cur].sprite; const SpritePrimitive sprite = primitives[cur].sprite;
/* TODO: try caching it */ if (primitives[cur].sprite.texture_key.id != cached_srcrect_key.id) {
const Rect srcrect = cached_srcrect = textures_get_srcrect(&ctx.texture_cache, primitives[cur].sprite.texture_key);
textures_get_srcrect(&ctx.texture_cache, primitives[cur].sprite.texture_key); cached_srcrect_key = primitives[cur].sprite.texture_key;
}
Rect const srcrect = cached_srcrect;
Vec2 uv0, uv1, uv2, uv3; Vec2 uv0, uv1, uv2, uv3;

View File

@ -50,6 +50,7 @@ typedef struct TextureCache {
typedef struct TextureKey { uint16_t id; } TextureKey; typedef struct TextureKey { uint16_t id; } TextureKey;
/* tests whether given key structure corresponds to any texture */ /* tests whether given key structure corresponds to any texture */
#define TEXTURE_KEY_INVALID (TextureKey) { (uint16_t)-1 }
#define m_texture_key_is_valid(p_key) ((p_key).id != (uint16_t)-1) #define m_texture_key_is_valid(p_key) ((p_key).id != (uint16_t)-1)
void textures_cache_init(struct TextureCache *cache, SDL_Window *window); void textures_cache_init(struct TextureCache *cache, SDL_Window *window);