|
16 | 16 |
|
17 | 17 | // CHANGELOG
|
18 | 18 | // (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. |
19 | 20 | // 2024-10-07: DirectX11: Changed default texture sampler to Clamp instead of Repeat/Wrap.
|
20 | 21 | // 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.
|
21 | 22 | // 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
|
98 | 99 | vp.TopLeftX = vp.TopLeftY = 0;
|
99 | 100 | device_ctx->RSSetViewports(1, &vp);
|
100 | 101 |
|
| 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 | + |
101 | 123 | // Setup shader and vertex buffers
|
102 | 124 | unsigned int stride = sizeof(ImDrawVert);
|
103 | 125 | unsigned int offset = 0;
|
@@ -179,28 +201,6 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
|
179 | 201 | device->Unmap(bd->pVB, 0);
|
180 | 202 | device->Unmap(bd->pIB, 0);
|
181 | 203 |
|
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 |
| - |
204 | 204 | // Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!)
|
205 | 205 | struct BACKUP_DX11_STATE
|
206 | 206 | {
|
@@ -255,6 +255,7 @@ void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
|
255 | 255 | render_state.Device = bd->pd3dDevice;
|
256 | 256 | render_state.DeviceContext = bd->pd3dDeviceContext;
|
257 | 257 | render_state.SamplerDefault = bd->pFontSampler;
|
| 258 | + render_state.VertexConstantBuffer = bd->pVertexConstantBuffer; |
258 | 259 | platform_io.Renderer_RenderState = &render_state;
|
259 | 260 |
|
260 | 261 | // Render command lists
|
|
0 commit comments