GeezerVison is showing github.com — Custom Sampler after ImTextureID change from SDL_GPUTextureSamplerBinding* to SDL_GPUTexture* · Issue #8926 · ocornut/imgui
Custom Sampler after ImTextureID change from SDL_GPUTextureSamplerBinding* to SDL_GPUTexture* #8926DescriptionVersion/Branch of Dear ImGui:
Version 1.92+, Branch: docking
Back-ends:
imgui_impl_sdl3 + imgui_impl_sdlgpu3
Compiler, OS:
MSVC 2022, Windows 11
Full config/build information:
Dear ImGui 1.92.2b (19222)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1944
define: _MSVC_LANG=202002
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: imgui_impl_sdl3 (3.2.22; 3.2.22)
io.BackendRendererName: imgui_impl_sdlgpu3
io.ConfigFlags: 0x00000081
NavEnableKeyboard
DockingEnable
io.ConfigViewportsNoDecoration
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x0000141E
HasMouseCursors
HasSetMousePos
PlatformHasViewports
RendererHasVtxOffset
RendererHasTextures
RendererHasViewports
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,128
io.Fonts->FontLoaderName: stb_truetype
io.DisplaySize: 3840.00,2054.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00
Details:
My Issue/Question:
After updating to the recent imgui version 1.92.2b I noticed the breaking changes to ImTextureID that now uses SDL_GPUTexture* instead of SDL_GPUTextureSamplerBinding* as it was before. I adjusted my codebase accordingly but noticed a slight visual difference. To give some context I am working on a 3D level editor for a custom game engine and I am rendering the 3D scene viewport to a framebuffer texture that is then drawn to an imgui window using ImGui::Image nothing fancy, just the standard approach.
The issue with the update I am facing now is that previously I was able to configure the sampler myself and set it to exact the same sampler setup as the one used to draw the framebuffer of the actual game. This uses nearest neighbour for mig/mag and filtering which gives the pixelated look we aim for.
I noticed the beta section in imgui_impl_sdlgpu3.h:
// [BETA] Selected render state data shared with callbacks.
// This is temporarily stored in GetPlatformIO().Renderer_RenderState during the ImGui_ImplSDLGPU3_RenderDrawData() call.
// (Please open an issue if you feel you need access to more data)
struct ImGui_ImplSDLGPU3_RenderState
{
SDL_GPUDevice* Device;
SDL_GPUSampler* SamplerDefault; // Default sampler (bilinear filtering)
SDL_GPUSampler* SamplerCurrent; // Current sampler (may be changed by callback)
};
Accessing SamplerDefault via PlatformIO works but writing to it does not.
Renderer_RenderState is nullptr when getting the PlatformIO before the first call to ImGui_ImplSDLGPU3_RenderDrawData.
The render state pointer is not null after render draw data - but writing to SamplerDefault then does not have an effect and it looks like it gets overwritten during the next frame.
Is there a workaround to get own samplers to work or am I missing something else?
Screenshots/Video:
No response
Minimal, Complete and Verifiable Example code:
No response Activityocornut commented on Sep 8, 2025 Hello,
Thanks for asking.
The idea is that you should write to SamplerCurrent during rendering by registering callbacks using ImDrawList::AddCallback():
See this example for DX11:
https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples#modifying-render-state
ImGui::GetWindowDrawList()->AddCallback(ImDrawCallback_ImplDX11_SetSampler, g_SamplerPoint); // Set custom sampler
ImGui::Image((ImTextureID)(intptr_t)my_texture, ImVec2((float)my_image_width * 4, (float)my_image_height * 4));
ImGui::GetWindowDrawList()->AddCallback(ImDrawCallback_ImplDX11_SetSampler, NULL); // Restore sampler
Arguably, switching between bilinear and point filtering has been 90%+ of requests requiring to use callbacks and I believe we should be providing helpers to do so in all backends. itsdanott commented on Sep 8, 2025 ContributorAuthor More actions Hi thanks for your reply. Ah that makes sense - now it works!
I wasn't familiar with the draw callbacks - should have scanned the docs and issues as the comment in the header was giving a lead to the callbacks.
For anyone running in the same issue here's the callback function adjusted for SDL_GPU:
void ImDrawCallback_ImplSDLGPU3_SetSampler(const ImDrawList* parent_list, const ImDrawCmd* cmd) {
auto* state = static_cast<ImGui_ImplSDLGPU3_RenderState*>(ImGui::GetPlatformIO().Renderer_RenderState);
SDL_GPUSampler* sampler = cmd->UserCallbackData ? static_cast<SDL_GPUSampler*>(cmd->UserCallbackData) : state->SamplerDefault;
state->SamplerCurrent = sampler;
}
And yes - having a helper for setting the filter mode would be handy but this callback solution is also fine I in my opinion. ocornut commented on Sep 8, 2025 Out of curiosity I spent the last hour working on a backend agnostic way to handle this.
I came upon a few problems:
[1] I presently don't know how to add support for DX12 and Vulkan. If someone if familiar with those API i'll show you my WIP branch.
[2] OpenGL needs a 3.3 or ES3 context to support glBindSampler() which is the easier way to map an hypothetical "change current sampler" into OpenGL logic. It may be a reasonable constraint vs calling glTexParameter() for every draw call which would override user data.
[3] One issue with adding ImDrawCallback_SetSamplerLinear ImDrawCallback_SetSamplerNearest is that backend failure to support them would lead to a crash which may be misunderstood. But otherwise they are easy to use. The ultimate plan was to expose custom callbacks directly in ImGuiPlatformIO, ec..
ImDrawCallback Renderer_DrawCallback_ResetDefaultState;
ImDrawCallback Renderer_DrawCallback_SetSamplerLinear;
ImDrawCallback Renderer_DrawCallback_SetSamplerNearest;
Except the issue is that the current signature for ImDrawCallback makes it hard for some backends to support this.
Changing callback signature would be build break for MANY users, albeit easy to fix but not great.
My plan instead was to transition to a "v2" signature for draw callbacks..
// ImDrawCallbackV2: Draw callbacks for advanced users. New signature introduced 2025/09.
struct ImDrawCallbackData
{
ImDrawData* DrawData;
const ImDrawList* DrawList;
const ImDrawCmd* DrawCmd;
void* RendererRenderState; // Renderer Backend specific ImGui_ImplXXXX_RenderState struct (exposed render state).
void* RendererUserData; // Renderer Backend specific ImGui_ImplXXXX_Data struct (generally internal stuff).
};
typedef void (*ImDrawCallbackV2)(ImDrawCallbackData* data);
struct ImDrawCmd
{
...
ImDrawCallback UserCallback; // 4-8 // If != NULL, call the function instead of rendering vertices. clip_rect and texture_id will be set normally.
ImDrawCallbackV2 UserCallbackV2; // 4-8 // If != NULL, call the function instead of rendering vertices. ".
};
Bit fugly to have both but it allows a much smoother transition. itsdanott commented on Sep 9, 2025 ContributorAuthor More actions Interesting - the V2 callback looks promising and the potential crash for unsupported backends might be fine if it's made very clear that this can cause issues when using with certain backends.
Another thought I had was what if it was possible via the API to set the default sampler once during initialization? So you wouldn't even need to use the draw commands.
This could be achieved for instance via an optional variable in the ImGui_ImplXXX_InitInfo struct that is passed to ImGui_ImplXXX_Init.
EDIT: I am not entirely sure this would conflict with other parts of imgui though e.g. font / ui element rendering. ocornut commented on Apr 23, 2026 In the latest version it is now possible to switch to Point/Nearest sampling in a backend-agnostic way:
draw_list->AddCallback(ImGui::GetPlatformIO().DrawCallback_SetSamplerNearest);
draw_list->AddImage(...);
In addition, the samplers added to ImGui_ImplSDLGPU3_RenderState were removed, since they were only added to facilitate this exact use which is now simpler.
See #9378 for details.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|