Skip to content

Commit af271e7

Browse files
committed
Backends: DX11: Expose vertex constant buffer in ImGui_ImplDX11_RenderState.
DX9 and DX12 users can already alter the projection matrix (in undocumented ways, but possible)
1 parent 93a9307 commit af271e7

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

backends/imgui_impl_dx11.cpp

+23-22
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
// CHANGELOG
1818
// (minor and older changes stripped away, please see git history for details)
19+
// 2025-01-06: DirectX11: Expose VertexConstantBuffer in ImGui_ImplDX11_RenderState. Reset projection matrix in ImDrawCallback_ResetRenderState handler.
1920
// 2024-10-07: DirectX11: Changed default texture sampler to Clamp instead of Repeat/Wrap.
2021
// 2024-10-07: DirectX11: Expose selected render state in ImGui_ImplDX11_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks.
2122
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
@@ -98,6 +99,27 @@ static void ImGui_ImplDX11_SetupRenderState(ImDrawData* draw_data, ID3D11DeviceC
9899
vp.TopLeftX = vp.TopLeftY = 0;
99100
device_ctx->RSSetViewports(1, &vp);
100101

102+
// Setup orthographic projection matrix into our constant buffer
103+
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
104+
D3D11_MAPPED_SUBRESOURCE mapped_resource;
105+
if (device_ctx->Map(bd->pVertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_resource) == S_OK)
106+
{
107+
VERTEX_CONSTANT_BUFFER_DX11* constant_buffer = (VERTEX_CONSTANT_BUFFER_DX11*)mapped_resource.pData;
108+
float L = draw_data->DisplayPos.x;
109+
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
110+
float T = draw_data->DisplayPos.y;
111+
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
112+
float mvp[4][4] =
113+
{
114+
{ 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
115+
{ 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
116+
{ 0.0f, 0.0f, 0.5f, 0.0f },
117+
{ (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
118+
};
119+
memcpy(&constant_buffer->mvp, mvp, sizeof(mvp));
120+
device_ctx->Unmap(bd->pVertexConstantBuffer, 0);
121+
}
122+
101123
// Setup shader and vertex buffers
102124
unsigned int stride = sizeof(ImDrawVert);
103125
unsigned int offset = 0;
@@ -179,28 +201,6 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
179201
device->Unmap(bd->pVB, 0);
180202
device->Unmap(bd->pIB, 0);
181203

182-
// Setup orthographic projection matrix into our constant buffer
183-
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
184-
{
185-
D3D11_MAPPED_SUBRESOURCE mapped_resource;
186-
if (device->Map(bd->pVertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_resource) != S_OK)
187-
return;
188-
VERTEX_CONSTANT_BUFFER_DX11* constant_buffer = (VERTEX_CONSTANT_BUFFER_DX11*)mapped_resource.pData;
189-
float L = draw_data->DisplayPos.x;
190-
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
191-
float T = draw_data->DisplayPos.y;
192-
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
193-
float mvp[4][4] =
194-
{
195-
{ 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
196-
{ 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
197-
{ 0.0f, 0.0f, 0.5f, 0.0f },
198-
{ (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
199-
};
200-
memcpy(&constant_buffer->mvp, mvp, sizeof(mvp));
201-
device->Unmap(bd->pVertexConstantBuffer, 0);
202-
}
203-
204204
// Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!)
205205
struct BACKUP_DX11_STATE
206206
{
@@ -255,6 +255,7 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
255255
render_state.Device = bd->pd3dDevice;
256256
render_state.DeviceContext = bd->pd3dDeviceContext;
257257
render_state.SamplerDefault = bd->pFontSampler;
258+
render_state.VertexConstantBuffer = bd->pVertexConstantBuffer;
258259
platform_io.Renderer_RenderState = &render_state;
259260

260261
// Render command lists

backends/imgui_impl_dx11.h

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
struct ID3D11Device;
2222
struct ID3D11DeviceContext;
2323
struct ID3D11SamplerState;
24+
struct ID3D11Buffer;
2425

2526
// Follow "Getting Started" link and check examples/ folder to learn about using backends!
2627
IMGUI_IMPL_API bool ImGui_ImplDX11_Init(ID3D11Device* device, ID3D11DeviceContext* device_context);
@@ -40,6 +41,7 @@ struct ImGui_ImplDX11_RenderState
4041
ID3D11Device* Device;
4142
ID3D11DeviceContext* DeviceContext;
4243
ID3D11SamplerState* SamplerDefault;
44+
ID3D11Buffer* VertexConstantBuffer;
4345
};
4446

4547
#endif // #ifndef IMGUI_DISABLE

docs/CHANGELOG.txt

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Other changes:
7272
platforms not supporting VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR. (#8222) [@Zer0xFF]
7373
- Backends: Vulkan: Added a few more ImGui_ImplVulkanH_XXX helper functions
7474
primarily for the purpose of making our examples simpler.
75+
- Backends: DX11: Expose vertex constant buffer in ImGui_ImplDX11_RenderState.
76+
Reset projection matrix in ImDrawCallback_ResetRenderState handlers. (#6969, #5834, #7468, #3590)
7577
- Examples: Added Win32+Vulkan example for completeness. (#8180) [@jristic]
7678

7779

0 commit comments

Comments
 (0)