GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #9173

New issue
Copy link
New issue
Copy link
Closed
Closed
Custom sampler for ImGui::Image() in DX12 backend#9173
Copy link
Labels
backendsdx12

Description

@zarond
zarond
opened on Jan 16, 2026
Issue body actions

Version/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

Activity

  1. ocornut commented on Jan 17, 2026

    @ocornut
    ocornut
    on Jan 17, 2026
    Owner
    More actions

    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.

    Reacted by Artur Makoev
  2. ocornut
    added
    backends
    dx12
    on Jan 17, 2026
  3. ocornut
    added a commit that references this issue on Jan 19, 2026

    Backends: DirectX12: added helpers to switch to Linear sampler (yet n…

    b933599
  4. ocornut commented on Jan 19, 2026

    @ocornut
    ocornut
    on Jan 19, 2026
    Owner
    More actions

    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);
  5. ocornut
    added a commit that references this issue on Jan 19, 2026

    Backends: DirectX10, DirectX11, SDLGPU3: added nearest sampler in ImG…

    8b86c93
  6. zarond commented on Jan 19, 2026

    @zarond
    zarond
    on Jan 19, 2026
    Author
    More actions

    Thank you, it works well!
    Image

    Reacted by omar
  7. ocornut commented on Jan 19, 2026

    @ocornut
    ocornut
    on Jan 19, 2026
    Owner
    More actions

    Great, I'll close this for now. When I add the backend-agnostic version in public API i'll post here.

  8. ocornut
    closed this as completedon Jan 19, 2026
  9. ocornut commented on Apr 23, 2026

    @ocornut
    ocornut
    on Apr 23, 2026
    Owner
    More actions

    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.

    Reacted by Artur Makoev
  10. zarond commented on Apr 24, 2026

    @zarond
    zarond
    on Apr 24, 2026
    Author
    More actions

    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));
    }

  11. ocornut commented on Apr 24, 2026

    @ocornut
    ocornut
    on Apr 24, 2026
    Owner
    More actions
      1. Can you clarify if trying to use DrawCallback_SetSamplerLinear also breaks rendering?
      1. Does it reproduce on our vanilla example_win32_directx12/ example? You may use https://github.com/ocornut/imgui/wiki/Image-Loading-and-Displaying-Examples#example-for-directx12-users as base code to integrate?
      1. Can you enable DX12 debug layer and try to investigate?
      1. Can you use RenderDoc to further investigate?

    Thank you!

  12. zarond commented on Apr 24, 2026

    @zarond
    zarond
    on Apr 24, 2026
    Author
    More actions
    1. No, setting DrawCallback_SetSamplerLinear doesn't break rendering on integrated Intel GPU
    2. Yes for example_win32_directx12. I've made these changes in imgui_demo.cpp in DemoWindowWidgetsImages()
    Image

    And it doesn't render the image, leaves empty space. There is a corresponding draw command in the list.

    Image
    1. It doesn't trigger any messages with SetBreakOnSeverity for D3D12_MESSAGE_SEVERITY_WARNING, D3D12_MESSAGE_SEVERITY_ERROR, D3D12_MESSAGE_SEVERITY_CORRUPTION

    2. I will look into it more, for now I see that there is a rendering command where image should be drawn

    Image
  13. zarond commented on Apr 24, 2026

    @zarond
    zarond
    on Apr 24, 2026
    Author
    More actions
    1. 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?
    Image
  14. zarond commented on Apr 24, 2026

    @zarond
    zarond
    on Apr 24, 2026
    Author
    More actions

    Works fine if I use SetSamplerLinear, maybe because the pipeline state and signature are the same objects

    Image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendsdx12

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.