Skip to content

Bug: Text is re-rasterized and re-uploaded to the GPU every frame #52

Description

@kimkulling

Describe the bug
Renderer::drawText (sdl2_renderer.cpp:227-272) does this on every frame, for
every text widget:

  SDL_Surface *surfaceMessage = TTF_RenderText_Solid(...);      // CPU rasterize
  glyphs
  SDL_Texture *messageTexture = SDL_CreateTextureFromSurface(...); // upload to 
  GPU
  ...
  SDL_FreeSurface(surfaceMessage);
  SDL_DestroyTexture(messageTexture);                            // throw it all
  away

The demo has ~10 text widgets, so that's ~10 full glyph-rasterizations + GPU
texture allocations + frees per frame (thousands per second), even though the
text never changes. This is the classic SDL_ttf performance killer.

Fix: Cache the SDL_Texture (and its width/height) on the Widget, and only
regenerate when mText (or color/font) changes. The per-frame cost then drops
to a single SDL_RenderCopy. A dirty flag set whenever mText is mutated (e.g.
appendKeyToText/deleteKeyFromText at widgets.cpp:148-164) drives invalidation.

To Reproduce
Run the demo

Expected behavior
Use one rendertarget to draw the whole UI onto.

Screenshots
NA

Desktop (please complete the following information):

  • OS: Windows11, Linux

Smartphone (please complete the following information):
NA

Additional context
NA

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions