GeezerVison is showing github.com — Custom sampler for ImGui::Image() in DX12 backend · Issue #9173 · ocornut/imgui
Custom sampler for ImGui::Image() in DX12 backend #9173DescriptionVersion/Branch of Dear ImGui:
v1.92.6 master
Back-ends:
imgui_impl_win32.cpp + imgui_impl_dx12.cpp
Compiler, OS:
Windows 11 + MSVC 2022
Full config/build information:
Dear ImGui 1.92.6 WIP (19259)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1944
define: _MSVC_LANG=201402
IM_ASSERT: runs expression: OK. expand size: OK
--------------------------------
io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_dx12
io.ConfigFlags: 0x00000003
NavEnableKeyboard
NavEnableGamepad
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x0000001E
HasMouseCursors
HasSetMousePos
RendererHasVtxOffset
RendererHasTextures
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,128
io.Fonts->FontLoaderName: stb_truetype
io.DisplaySize: 1264.00,761.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:
Hello, I am sorry for bringing up the topic of how to use nearest sampling in ImGui::Image() again, but it is relatively easy in DX11, but I can't wrap my head around in how to do it in a DX12 backend.
I am new to DX12 and may be wrong, but it seems that in function ImGui_ImplDX12_CreateDeviceObjects from imgui_impl_dx12.cpp while creating the root signature, static samplers are used with D3D12_STATIC_SAMPLER_DESC, so that if I want to use custom sampler, I would have to create a whole new root signature and swap it in an active PSO.
Could there be another way?
Thanks in advance, feel free to close if you think the issue is irrelevant.
Screenshots/Video:
No response
Minimal, Complete and Verifiable Example code:
Not fully complete code, general idea
// Create Sampler Heap with one nearest filtering sampler
{
D3D12_DESCRIPTOR_HEAP_DESC desc = {};
desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER;
desc.NumDescriptors = 1;
desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
if (FAILED(g_pd3dDevice->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&g_pd3dSamplerDescHeap)))) return false;
D3D12_SAMPLER_DESC samplerDesc = {};
samplerDesc.Filter = D3D12_FILTER_MIN_MAG_MIP_POINT;
samplerDesc.AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
samplerDesc.AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
samplerDesc.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP;
samplerDesc.MinLOD = 0;
samplerDesc.MaxLOD = D3D12_FLOAT32_MAX;
samplerDesc.MipLODBias = 0.0f;
samplerDesc.MaxAnisotropy = 1;
g_pd3dDevice->CreateSampler(&samplerDesc, g_pd3dSamplerDescHeap->GetCPUDescriptorHandleForHeapStart());
}
void ImDrawCallback_ImplDX12_SetSampler(const ImDrawList* parent_list, const ImDrawCmd* cmd) {
ImGui_ImplDX12_RenderState* state = (ImGui_ImplDX12_RenderState*)ImGui::GetPlatformIO().Renderer_RenderState;
D3DContext& d3d_ctx = D3DContext::Get(); // my singleton class with d3d objects
ID3D12DescriptorHeap* desc_heap[] = {d3d_ctx.g_pd3dSrvDescHeap.Get(), d3d_ctx.g_pd3dSamplerDescHeap.Get()};
state->CommandList->SetDescriptorHeaps(2, desc_heap);
D3D12_GPU_DESCRIPTOR_HANDLE handle = d3d_ctx.g_pd3dSamplerDescHeap->GetGPUDescriptorHandleForHeapStart();
state->CommandList->SetGraphicsRootDescriptorTable(2, handle); // I want to be able to set sampler in a table
}
ImGui::GetWindowDrawList()->AddCallback(
ImDrawCallback_ImplDX12_SetSampler, nullptr); // Set custom sampler
ImGui::Image((ImTextureID)texture_srv_gpu_handle.ptr, ImVec2(scale * (float)dims.x, scale * (float) dims.y));
ImGui::GetWindowDrawList()->AddCallback(ImDrawCallback_ResetRenderState, NULL); // Restore sampler
Activityocornut commented on Jan 17, 2026 I have some code somewhere I’ll try to dig it out on Monday. My goal is to provide a backend agnostic way of switching between linear and point sampling. ocornut commented on Jan 19, 2026 I have pushed b933599 which include code extracted from a larger work in progress branch to design a way to change sampler in a backend agnostic way. I have 90% of the code for this but there are tricky API design problems I haven't got around to solve.
On the DX12 implementation I couldn't easily implement it myself (same problem as you, not overly familiar with DX12) so @thedmd kindly did it and you can check his approach in b933599.
I did not add the new functions in the public API intentionally: for now you'll need to forward declare them. When the better API land I'll notify you here. For now you can do:
// Forward declare
void ImGui_ImplDX12_SetupSamplerLinear(ID3D12GraphicsCommandList* command_list);
void ImGui_ImplDX12_SetupSamplerNearest(ID3D12GraphicsCommandList* command_list);
// Then, from draw callback code:
ImGui_ImplDX12_RenderState* render_state = (ImGui_ImplDX12_RenderState*)ImGui::GetPlatformIO().Renderer_RenderState;
ImGui_ImplDX12_SetupSamplerNearest(render_state->CommandList); zarond commented on Jan 19, 2026 Thank you, it works well!
 ocornut commented on Jan 19, 2026 Great, I'll close this for now. When I add the backend-agnostic version in public API i'll post here. 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(...);
I have made both ImGui_ImplDX12_SetupSamplerLinear() and ImGui_ImplDX12_SetupSamplerNearest() function mentioned here static, to favor using the new general and simpler method.
See #9378 for details. zarond commented on Apr 24, 2026 That is a great feature! I am updating to latest ImGui right now.
However, I have a problem when using nearest filtering with Intel UHD Graphics - the image doesn't render at all. Before this update, when using forward declared functions, there was this bug too, but it also broke all the UI when using integrated GPU, now only the image doesn't appear (I think the buggy behavior resets on ResetRenderState callback, before I was using SetSamplerLinear). On discrete GPU, there is no issues at all. This all might as well be a Intel Graphics thing.
My code:
if (framebuffer.nearest_filtering) {
auto& platform_io = ImGui::GetPlatformIO();
ImGui::GetWindowDrawList()->AddCallback(platform_io.DrawCallback_SetSamplerNearest);
ImGui::Image((ImTextureID)texture_srv_gpu_handle.ptr, ImVec2(scale * (float)dims.x, scale * (float)dims.y));
ImGui::GetWindowDrawList()->AddCallback(platform_io.DrawCallback_ResetRenderState);
} else {
ImGui::Image((ImTextureID)texture_srv_gpu_handle.ptr, ImVec2(scale * (float)dims.x, scale * (float)dims.y));
} ocornut commented on Apr 24, 2026 zarond commented on Apr 24, 2026
- No, setting DrawCallback_SetSamplerLinear doesn't break rendering on integrated Intel GPU
- Yes for example_win32_directx12. I've made these changes in imgui_demo.cpp in DemoWindowWidgetsImages()
And it doesn't render the image, leaves empty space. There is a corresponding draw command in the list.
-
It doesn't trigger any messages with SetBreakOnSeverity for D3D12_MESSAGE_SEVERITY_WARNING, D3D12_MESSAGE_SEVERITY_ERROR, D3D12_MESSAGE_SEVERITY_CORRUPTION
-
I will look into it more, for now I see that there is a rendering command where image should be drawn
 zarond commented on Apr 24, 2026
- Could the issue be that it first sets pipeline state and root signature for default (linear), and then ignores it and sets it again, this time with different pipeline state and root signature?
 zarond commented on Apr 24, 2026 Works fine if I use SetSamplerLinear, maybe because the pipeline state and signature are the same objects

MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|