easy-gl › UniformCache

UniformCache

A lightweight cache around Program::uniform_location(). Avoids repeated string lookups by storing results in an unordered_map. Constructed from a const Program&; the program must remain alive for the lifetime of the cache.

Without caching, every call to uniform_location("name") issues a driver call. UniformCache performs the lookup once and returns the cached integer on subsequent accesses via operator[].

Basic usage

easygl::Program prog(vertSrc, fragSrc);
easygl::UniformCache uniforms(prog);

// First call queries the driver and caches; subsequent calls return the cached value:
prog.set_uniform(uniforms["uColor"], 1.0f, 0.5f, 0.2f, 1.0f);
prog.set_uniform(uniforms["uMVP"],   /* ... */);

// Locations not found in the shader return -1 (same as Program::uniform_location)

Invalidating after relink

When a program is recompiled, uniform locations may change. Call invalidate() to clear the cache so the next access re-queries the driver:

prog.compile_from_sources(newVertSrc, newFragSrc);
uniforms.invalidate(); // clear all cached locations

// Next access re-queries the newly linked program:
prog.set_uniform(uniforms["uColor"], 1.0f, 1.0f, 1.0f, 1.0f);
The cache holds a pointer to the program. Do not destroy the program while the UniformCache is still in use.

Method reference

MethodDescription
UniformCache(const Program&)Construct the cache bound to a program.
operator[](name)Return the cached location for name, querying the program on first access.
invalidate()Clear all cached locations so next access re-queries the driver.