Project MTP dev log index

Comments (Github discussion)

Project MTP dev log (December 2023)

December 2023

Currently working on resolving technical debt and porting my Ebitengine platformer game to new engine.

It’s going great so far!

Sampler objects

N64/PS1 switch?

Started using sampler objects. They’re fantastic. Previously I used what many people do in OpenGL tutorials, the sampling state was hardcoded into each texture like this:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
... // etc.

But sometimes you need to sample it differently. And different systems might want to sample the same texture in a different way (some with linear filtering, some with nearest). Having to manually set nearest filtering for UI textures was also annoying.

Sampler objects to the rescue! Now I can just do this:

const auto& sm = Engine::getShaderManager();
const auto& nearestSampler = sm.getDefaultNearestSampler();
const auto& shader = sm.getShader(shaderName);
shader.setSamplerUniform("tex", prevTarget.getTexture(), 0, nearestSampler);

or create a sampler like this:

const auto& sampler =  sm.getSampler({
    .filter =
        {
            .min = edge::FilterType::Linear,
            .mag = edge::FilterType::Linear,
        },
    .wrap = SamplerObject::WrapParams::ClampToEdge,
    .anisotropy = 8.f,
});

(all sampler objects get cached so that sampler object don’t get re-created over and over)