Module rusttype::gpu_cache [] [src]

This module provides capabilities for managing a cache of rendered glyphs in GPU memory, with the goal of minimisng the size and frequency of glyph uploads to GPU memory from the CPU.

Typical applications that render directly with hardware graphics APIs (e.g. games) need text rendering. There is not yet a performant solution for high quality text rendering directly on the GPU that isn't experimental research work. Quality is often critical for legibility, so many applications use text or individual characters that have been rendered on the CPU. This is done either ahead-of-time, giving a fixed set of fonts, characters, and sizes that can be used at runtime, or dynamically as text is required. This latter scenario is more flexible and the focus of this module. To minimise the CPU load and texture upload bandwidth saturation, recently used glyphs should be cached on the GPU for use by future frames. This module provides a mechanism for maintaining such a cache in the form of a single packed 2D GPU texture. When a rendered glyph is requested, it is either retrieved from its location in the texture if it is present or room is made in the cache (if necessary), the CPU renders the glyph then it is uploaded into a gap in the texture to be available for GPU rendering. This cache uses a Least Recently Used (LRU) cache eviction scheme - glyphs in the cache that have not been used recently are as a rule of thumb not likely to be used again soon, so they are the best candidates for eviction to make room for required glyphs.

The API for the cache does not assume a particular graphics API. The intended usage is to queue up glyphs that need to be present for the current frame using Cache::queue_glyph, update the cache to ensure that the queued glyphs are present using Cache::cache_queued (providing a function for uploading pixel data), then when it's time to render call Cache::rect_for to get the UV coordinates in the cache texture for each glyph. For a concrete use case see the gpu_cache example.

Structs

Cache

An implementation of a dynamic GPU glyph cache. See the module documentation for more information.

Enums

CacheReadErr

Returned from Cache::rect_for.

CacheWriteErr

Returned from Cache::cache_queued.