Pictures in the UI read as ImGui's colours are: an sRGB picture was turned linear and showed too dark (the logo in About, the Picture panel's paint) — a UNORM view on Vulkan, a skip-decode sampler on OpenGL 4.6 and 4.0, the texture's own setting before 3.3

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Nexoniarz 2026-10-20 19:01:07 +02:00
commit ffaa5e8001
6 changed files with 79 additions and 5 deletions

View file

@ -232,6 +232,12 @@ public:
gl::SamplerParameteri(clampSampler_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gl::SamplerParameteri(clampSampler_, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl::SamplerParameteri(clampSampler_, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl::CreateSamplers(1, &uiRawSampler_); // (UI pictures: sRGB read as plain numbers, as ImGui's colours are)
gl::SamplerParameteri(uiRawSampler_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
gl::SamplerParameteri(uiRawSampler_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gl::SamplerParameteri(uiRawSampler_, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl::SamplerParameteri(uiRawSampler_, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl::SamplerParameteri(uiRawSampler_, 0x8A48 /* GL_TEXTURE_SRGB_DECODE_EXT */, 0x8A4A /* GL_SKIP_DECODE_EXT */);
gl::CreateSamplers(1, &compareSampler_);
gl::SamplerParameteri(compareSampler_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gl::SamplerParameteri(compareSampler_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@ -320,7 +326,7 @@ public:
}
for (GLuint b : {textureTable_, shadowTable_, draws_, joints_, frame_, mirrorFrame_, lights_, views_, cells_, indices_, push_, empty_})
gl::DeleteBuffers(1, &b);
for (GLuint s : {repeatSampler_, clampSampler_, compareSampler_}) gl::DeleteSamplers(1, &s);
for (GLuint s : {repeatSampler_, clampSampler_, compareSampler_, uiRawSampler_}) gl::DeleteSamplers(1, &s);
for (GLuint p : {meshProgram_, skinnedProgram_, shadowMeshProgram_, shadowSkinnedProgram_, boxProgram_, tonemapProgram_, skyProgram_, hzbProgram_, ssaoProgram_, ssaoBlurProgram_, ssrProgram_, adaptProgram_, volumetricProgram_, spriteProgram_,
bloomDownProgram_, bloomUpProgram_, taaProgram_})
gl::DeleteProgram(p);
@ -358,6 +364,7 @@ public:
gl::TextureSubImage2D(t.texture, GLint(level), 0, 0, GLsizei(std::max(1u, w >> level)), GLsizei(std::max(1u, h >> level)), GL_RGBA,
GL_UNSIGNED_BYTE, mips[level].data());
}
t.srgb = srgb;
if (auto it = textures_.find(slot); it != textures_.end()) retiredTextures_.emplace_back(frameNumber_, it->second); // queued draws may use it
setHandles(slot, t, repeatSampler_, false); // like Vulkan: one repeating sampler for every texture
textures_[slot] = t;
@ -385,6 +392,13 @@ public:
const auto it = textures_.find(slot);
return it == textures_.end() ? 0 : uint64_t(it->second.texture);
}
UiRawRead uiRawRead(TextureId slot) override {
const auto it = textures_.find(slot);
if (it == textures_.end() || !it->second.srgb) return {};
// (A sampler, not the texture's own setting: that's fixed once it has a bindless handle.)
return {[](const ImDrawList*, const ImDrawCmd* c) { gl::BindSampler(0, GLuint(uintptr_t(c->UserCallbackData))); },
[](const ImDrawList*, const ImDrawCmd*) { gl::BindSampler(0, 0); }, reinterpret_cast<void*>(uintptr_t(uiRawSampler_))};
}
bool beginFrame(uint32_t& width, uint32_t& height) override {
int w = 0, h = 0;
@ -424,6 +438,7 @@ private:
struct Texture {
GLuint texture = 0;
GLuint64 handle = 0, shadowHandle = 0;
bool srgb = false;
};
struct Model {
GLuint vertices = 0, indices = 0, skin = 0;
@ -529,7 +544,7 @@ private:
SDL_Window* window_ = nullptr;
SDL_GLContext context_ = nullptr;
bool imgui_ = false;
GLuint repeatSampler_ = 0, clampSampler_ = 0, compareSampler_ = 0;
GLuint repeatSampler_ = 0, clampSampler_ = 0, compareSampler_ = 0, uiRawSampler_ = 0;
GLuint mirrorFrame_ = 0; // the mirror's pass's frame data
GLuint textureTable_ = 0, shadowTable_ = 0, draws_ = 0, joints_ = 0, frame_ = 0, lights_ = 0, views_ = 0, cells_ = 0, indices_ = 0;
GLuint push_ = 0, empty_ = 0, vao_ = 0;

View file

@ -100,7 +100,11 @@ namespace {
X(void, DrawBuffer, (GLenum)) \
X(void, DrawBuffers, (GLsizei, const GLenum*)) \
X(void, TexBuffer, (GLenum, GLenum, GLuint)) \
X(void, BindFragDataLocation, (GLuint, GLuint, const GLchar*))
X(void, BindFragDataLocation, (GLuint, GLuint, const GLchar*)) \
X(void, GenSamplers, (GLsizei, GLuint*)) \
X(void, DeleteSamplers, (GLsizei, const GLuint*)) \
X(void, BindSampler, (GLuint, GLuint)) \
X(void, SamplerParameteri, (GLuint, GLenum, GLint))
namespace glx {
#define KEEL_GLX_DECLARE(ret, name, params) using name##_t = ret(APIENTRY*) params; name##_t name = nullptr;
@ -499,6 +503,14 @@ public:
ImGui::GetIO().IniFilename = nullptr;
ImGui::StyleColorsDark();
imgui_ = ImGui_ImplSDL3_InitForOpenGL(window_, context_) && ImGui_ImplOpenGL3_Init(es_ ? "#version 300 es" : "#version 140");
if (glx::GenSamplers && glx::BindSampler && glx::SamplerParameteri) { // (3.3 on: UI pictures read as plain numbers, as ImGui's colours are)
glx::GenSamplers(1, &uiRawSampler_);
glx::SamplerParameteri(uiRawSampler_, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glx::SamplerParameteri(uiRawSampler_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glx::SamplerParameteri(uiRawSampler_, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glx::SamplerParameteri(uiRawSampler_, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glx::SamplerParameteri(uiRawSampler_, 0x8A48 /* GL_TEXTURE_SRGB_DECODE_EXT */, 0x8A4A /* GL_SKIP_DECODE_EXT */);
}
log::info("Fallback renderer: tier {} ({})", tier_, tier_ >= 2 ? "shadows, normal maps, highlights, sky reflections, 16 lights" : "lightmaps, sun shadows, sky");
return imgui_;
}
@ -515,6 +527,7 @@ public:
for (GLuint t : textures_)
if (t) glx::DeleteTextures(1, &t);
for (Model& m : models_) releaseModel(m);
if (uiRawSampler_) glx::DeleteSamplers(1, &uiRawSampler_);
releaseTargets();
for (GLuint* t : {&shadowMap_, &jointTexture_}) glx::DeleteTextures(1, t);
glx::DeleteFramebuffers(1, &shadowFbo_);
@ -538,7 +551,8 @@ public:
// No block compression here (old GPUs): the renderer decodes BC7 to RGBA8 first.
void createTexture(TextureId slot, uint32_t w, uint32_t h, bool srgb, const std::vector<std::vector<uint8_t>>& mips,
TexFormat = TexFormat::RGBA8) override {
if (slot >= textures_.size()) textures_.resize(slot + 1, 0);
if (slot >= textures_.size()) textures_.resize(slot + 1, 0), srgb_.resize(slot + 1, false);
srgb_[slot] = srgb;
GLuint& t = textures_[slot];
if (!t) glx::GenTextures(1, &t);
glx::BindTexture(GL_TEXTURE_2D, t);
@ -585,6 +599,22 @@ public:
}
uint64_t uiTexture(TextureId slot) override { return slot < textures_.size() ? uint64_t(textures_[slot]) : 0; }
// (A sampler object where there are any; else the texture's own setting, switched for the draw and back.)
UiRawRead uiRawRead(TextureId slot) override {
if (slot >= textures_.size() || !srgb_[slot]) return {};
if (uiRawSampler_)
return {[](const ImDrawList*, const ImDrawCmd* c) { glx::BindSampler(0, GLuint(uintptr_t(c->UserCallbackData))); },
[](const ImDrawList*, const ImDrawCmd*) { glx::BindSampler(0, 0); }, reinterpret_cast<void*>(uintptr_t(uiRawSampler_))};
return {[](const ImDrawList*, const ImDrawCmd* c) {
glx::BindTexture(GL_TEXTURE_2D, GLuint(uintptr_t(c->UserCallbackData)));
glx::TexParameteri(GL_TEXTURE_2D, 0x8A48 /* GL_TEXTURE_SRGB_DECODE_EXT */, 0x8A4A /* GL_SKIP_DECODE_EXT */);
},
[](const ImDrawList*, const ImDrawCmd* c) {
glx::BindTexture(GL_TEXTURE_2D, GLuint(uintptr_t(c->UserCallbackData)));
glx::TexParameteri(GL_TEXTURE_2D, 0x8A48 /* GL_TEXTURE_SRGB_DECODE_EXT */, 0x8A49 /* GL_DECODE_EXT */);
},
reinterpret_cast<void*>(uintptr_t(textures_[slot]))};
}
bool beginFrame(uint32_t& width, uint32_t& height) override {
int w = 0, h = 0;
@ -722,6 +752,8 @@ private:
bool imgui_ = false;
Program mesh_, depth_, sky_, box_, tonemap_, sprite_;
std::vector<GLuint> textures_;
std::vector<bool> srgb_;
GLuint uiRawSampler_ = 0;
std::vector<Model> models_;
GLuint frameUbo_ = 0, jointBuffer_ = 0, jointTexture_ = 0, emptyVao_ = 0;
GLuint shadowMap_ = 0, shadowFbo_ = 0;

View file

@ -34,6 +34,7 @@
GLenum format, GLsizei size, const void* data)) \
X(void, CreateSamplers, (GLsizei n, GLuint* samplers)) \
X(void, DeleteSamplers, (GLsizei n, const GLuint* samplers)) \
X(void, BindSampler, (GLuint unit, GLuint sampler)) \
X(void, SamplerParameteri, (GLuint sampler, GLenum pname, GLint param)) \
X(void, SamplerParameterf, (GLuint sampler, GLenum pname, GLfloat param)) \
X(GLuint64, GetTextureSamplerHandleARB, (GLuint texture, GLuint sampler)) \