| 1 |
// dear imgui: Renderer Backend for DirectX9 |
| 2 |
// This needs to be used along with a Platform Backend (e.g. Win32) |
| 3 |
|
| 4 |
// Implemented features: |
| 5 |
// [X] Renderer: User texture binding. Use 'LPDIRECT3DTEXTURE9' as texture identifier. Read the FAQ about ImTextureID/ImTextureRef! |
| 6 |
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset). |
| 7 |
// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures). |
| 8 |
// [X] Renderer: IMGUI_USE_BGRA_PACKED_COLOR support, as this is the optimal color encoding for DirectX9. |
| 9 |
// [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. |
| 10 |
|
| 11 |
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
| 12 |
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
| 13 |
// Learn about Dear ImGui: |
| 14 |
// - FAQ https://dearimgui.com/faq |
| 15 |
// - Getting Started https://dearimgui.com/getting-started |
| 16 |
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
| 17 |
// - Introduction, links and more at the top of imgui.cpp |
| 18 |
|
| 19 |
// CHANGELOG |
| 20 |
// (minor and older changes stripped away, please see git history for details) |
| 21 |
// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface. |
| 22 |
// 2025-06-11: DirectX9: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. |
| 23 |
// 2024-10-07: DirectX9: Changed default texture sampler to Clamp instead of Repeat/Wrap. |
| 24 |
// 2024-02-12: DirectX9: Using RGBA format when supported by the driver to avoid CPU side conversion. (#6575) |
| 25 |
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. |
| 26 |
// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX). |
| 27 |
// 2021-06-25: DirectX9: Explicitly disable texture state stages after >= 1. |
| 28 |
// 2021-05-19: DirectX9: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement) |
| 29 |
// 2021-04-23: DirectX9: Explicitly setting up more graphics states to increase compatibility with unusual non-default states. |
| 30 |
// 2021-03-18: DirectX9: Calling IDirect3DStateBlock9::Capture() after CreateStateBlock() as a workaround for state restoring issues (see #3857). |
| 31 |
// 2021-03-03: DirectX9: Added support for IMGUI_USE_BGRA_PACKED_COLOR in user's imconfig file. |
| 32 |
// 2021-02-18: DirectX9: Change blending equation to preserve alpha in output buffer. |
| 33 |
// 2019-05-29: DirectX9: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag. |
| 34 |
// 2019-04-30: DirectX9: Added support for special ImDrawCallback_ResetRenderState callback to reset render state. |
| 35 |
// 2019-03-29: Misc: Fixed erroneous assert in ImGui_ImplDX9_InvalidateDeviceObjects(). |
| 36 |
// 2019-01-16: Misc: Disabled fog before drawing UI's. Fixes issue #2288. |
| 37 |
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window. |
| 38 |
// 2018-06-08: Misc: Extracted imgui_impl_dx9.cpp/.h away from the old combined DX9+Win32 example. |
| 39 |
// 2018-06-08: DirectX9: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle. |
| 40 |
// 2018-05-07: Render: Saving/restoring Transform because they don't seem to be included in the StateBlock. Setting shading mode to Gouraud. |
| 41 |
// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX9_RenderDrawData() in the .h file so you can call it yourself. |
| 42 |
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves. |
| 43 |
|
| 44 |
#include "imgui.h" |
| 45 |
#ifndef IMGUI_DISABLE |
| 46 |
#include "imgui_impl_dx9.h" |
| 47 |
|
| 48 |
// DirectX |
| 49 |
#include <d3d9.h> |
| 50 |
|
| 51 |
// Clang/GCC warnings with -Weverything |
| 52 |
#if defined(__clang__) |
| 53 |
#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast // yes, they are more terse. |
| 54 |
#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness |
| 55 |
#endif |
| 56 |
|
| 57 |
// DirectX data |
| 58 |
struct ImGui_ImplDX9_Data |
| 59 |
{ |
| 60 |
LPDIRECT3DDEVICE9 pd3dDevice; |
| 61 |
LPDIRECT3DVERTEXBUFFER9 pVB; |
| 62 |
LPDIRECT3DINDEXBUFFER9 pIB; |
| 63 |
int VertexBufferSize; |
| 64 |
int IndexBufferSize; |
| 65 |
bool HasRgbaSupport; |
| 66 |
|
| 67 |
ImGui_ImplDX9_Data() { memset((void*)this, 0, sizeof(*this)); VertexBufferSize = 5000; IndexBufferSize = 10000; } |
| 68 |
}; |
| 69 |
|
| 70 |
struct CUSTOMVERTEX |
| 71 |
{ |
| 72 |
float pos[3]; |
| 73 |
D3DCOLOR col; |
| 74 |
float uv[2]; |
| 75 |
}; |
| 76 |
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1) |
| 77 |
|
| 78 |
#ifdef IMGUI_USE_BGRA_PACKED_COLOR |
| 79 |
#define IMGUI_COL_TO_DX9_ARGB(_COL) (_COL) |
| 80 |
#else |
| 81 |
#define IMGUI_COL_TO_DX9_ARGB(_COL) (((_COL) & 0xFF00FF00) | (((_COL) & 0xFF0000) >> 16) | (((_COL) & 0xFF) << 16)) |
| 82 |
#endif |
| 83 |
|
| 84 |
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts |
| 85 |
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. |
| 86 |
static ImGui_ImplDX9_Data* ImGui_ImplDX9_GetBackendData() |
| 87 |
{ |
| 88 |
return ImGui::GetCurrentContext() ? (ImGui_ImplDX9_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; |
| 89 |
} |
| 90 |
|
| 91 |
// Forward Declarations |
| 92 |
static void ImGui_ImplDX9_InitMultiViewportSupport(); |
| 93 |
static void ImGui_ImplDX9_ShutdownMultiViewportSupport(); |
| 94 |
static void ImGui_ImplDX9_CreateDeviceObjectsForPlatformWindows(); |
| 95 |
static void ImGui_ImplDX9_InvalidateDeviceObjectsForPlatformWindows(); |
| 96 |
|
| 97 |
// Functions |
| 98 |
static void ImGui_ImplDX9_SetupRenderState(ImDrawData* draw_data) |
| 99 |
{ |
| 100 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 101 |
|
| 102 |
// Setup viewport |
| 103 |
D3DVIEWPORT9 vp; |
| 104 |
vp.X = vp.Y = 0; |
| 105 |
vp.Width = (DWORD)draw_data->DisplaySize.x; |
| 106 |
vp.Height = (DWORD)draw_data->DisplaySize.y; |
| 107 |
vp.MinZ = 0.0f; |
| 108 |
vp.MaxZ = 1.0f; |
| 109 |
|
| 110 |
LPDIRECT3DDEVICE9 device = bd->pd3dDevice; |
| 111 |
device->SetViewport(&vp); |
| 112 |
|
| 113 |
// Setup render state: fixed-pipeline, alpha-blending, no face culling, no depth testing, shade mode (for gradient), bilinear sampling. |
| 114 |
device->SetPixelShader(nullptr); |
| 115 |
device->SetVertexShader(nullptr); |
| 116 |
device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); |
| 117 |
device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD); |
| 118 |
device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); |
| 119 |
device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); |
| 120 |
device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 121 |
device->SetRenderState(D3DRS_ZENABLE, FALSE); |
| 122 |
device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 123 |
device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); |
| 124 |
device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); |
| 125 |
device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); |
| 126 |
device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 127 |
device->SetRenderState(D3DRS_SRCBLENDALPHA, D3DBLEND_ONE); |
| 128 |
device->SetRenderState(D3DRS_DESTBLENDALPHA, D3DBLEND_INVSRCALPHA); |
| 129 |
device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE); |
| 130 |
device->SetRenderState(D3DRS_FOGENABLE, FALSE); |
| 131 |
device->SetRenderState(D3DRS_RANGEFOGENABLE, FALSE); |
| 132 |
device->SetRenderState(D3DRS_SPECULARENABLE, FALSE); |
| 133 |
device->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 134 |
device->SetRenderState(D3DRS_CLIPPING, TRUE); |
| 135 |
device->SetRenderState(D3DRS_LIGHTING, FALSE); |
| 136 |
device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); |
| 137 |
device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); |
| 138 |
device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); |
| 139 |
device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); |
| 140 |
device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); |
| 141 |
device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE); |
| 142 |
device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); |
| 143 |
device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); |
| 144 |
device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); |
| 145 |
device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); |
| 146 |
device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); |
| 147 |
device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); |
| 148 |
|
| 149 |
// Setup orthographic projection matrix |
| 150 |
// 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. |
| 151 |
// Being agnostic of whether <d3dx9.h> or <DirectXMath.h> can be used, we aren't relying on D3DXMatrixIdentity()/D3DXMatrixOrthoOffCenterLH() or DirectX::XMMatrixIdentity()/DirectX::XMMatrixOrthographicOffCenterLH() |
| 152 |
{ |
| 153 |
float L = draw_data->DisplayPos.x + 0.5f; |
| 154 |
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x + 0.5f; |
| 155 |
float T = draw_data->DisplayPos.y + 0.5f; |
| 156 |
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y + 0.5f; |
| 157 |
D3DMATRIX mat_identity = { { { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } } }; |
| 158 |
D3DMATRIX mat_projection = |
| 159 |
{ { { |
| 160 |
2.0f/(R-L), 0.0f, 0.0f, 0.0f, |
| 161 |
0.0f, 2.0f/(T-B), 0.0f, 0.0f, |
| 162 |
0.0f, 0.0f, 0.5f, 0.0f, |
| 163 |
(L+R)/(L-R), (T+B)/(B-T), 0.5f, 1.0f |
| 164 |
} } }; |
| 165 |
device->SetTransform(D3DTS_WORLD, &mat_identity); |
| 166 |
device->SetTransform(D3DTS_VIEW, &mat_identity); |
| 167 |
device->SetTransform(D3DTS_PROJECTION, &mat_projection); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
// Render function. |
| 172 |
void ImGui_ImplDX9_RenderDrawData(ImDrawData* draw_data) |
| 173 |
{ |
| 174 |
// Avoid rendering when minimized |
| 175 |
if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f) |
| 176 |
return; |
| 177 |
|
| 178 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 179 |
LPDIRECT3DDEVICE9 device = bd->pd3dDevice; |
| 180 |
|
| 181 |
// Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do. |
| 182 |
// (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates). |
| 183 |
if (draw_data->Textures != nullptr) |
| 184 |
for (ImTextureData* tex : *draw_data->Textures) |
| 185 |
if (tex->Status != ImTextureStatus_OK) |
| 186 |
ImGui_ImplDX9_UpdateTexture(tex); |
| 187 |
|
| 188 |
// Create and grow buffers if needed |
| 189 |
if (!bd->pVB || bd->VertexBufferSize < draw_data->TotalVtxCount) |
| 190 |
{ |
| 191 |
if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; } |
| 192 |
bd->VertexBufferSize = draw_data->TotalVtxCount + 5000; |
| 193 |
if (device->CreateVertexBuffer(bd->VertexBufferSize * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &bd->pVB, nullptr) < 0) |
| 194 |
return; |
| 195 |
} |
| 196 |
if (!bd->pIB || bd->IndexBufferSize < draw_data->TotalIdxCount) |
| 197 |
{ |
| 198 |
if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; } |
| 199 |
bd->IndexBufferSize = draw_data->TotalIdxCount + 10000; |
| 200 |
if (device->CreateIndexBuffer(bd->IndexBufferSize * sizeof(unsigned short), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &bd->pIB, nullptr) < 0) |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
// Backup the DX9 state |
| 205 |
/* |
| 206 |
IDirect3DStateBlock9* state_block = nullptr; |
| 207 |
if (device->CreateStateBlock(D3DSBT_ALL, &state_block) < 0) |
| 208 |
return; |
| 209 |
if (state_block->Capture() < 0) |
| 210 |
{ |
| 211 |
state_block->Release(); |
| 212 |
return; |
| 213 |
}*/ |
| 214 |
|
| 215 |
// Backup the DX9 transform (DX9 documentation suggests that it is included in the StateBlock but it doesn't appear to) |
| 216 |
/* |
| 217 |
D3DMATRIX last_world, last_view, last_projection; |
| 218 |
device->GetTransform(D3DTS_WORLD, &last_world); |
| 219 |
device->GetTransform(D3DTS_VIEW, &last_view); |
| 220 |
device->GetTransform(D3DTS_PROJECTION, &last_projection); |
| 221 |
*/ |
| 222 |
|
| 223 |
// Allocate buffers |
| 224 |
CUSTOMVERTEX* vtx_dst; |
| 225 |
unsigned short* idx_dst; |
| 226 |
if (bd->pVB->Lock(0, (UINT)(draw_data->TotalVtxCount * sizeof(CUSTOMVERTEX)), (void**)&vtx_dst, D3DLOCK_DISCARD) < 0) |
| 227 |
{ |
| 228 |
return; |
| 229 |
} |
| 230 |
if (bd->pIB->Lock(0, (UINT)(draw_data->TotalIdxCount * sizeof(unsigned short)), (void**)&idx_dst, D3DLOCK_DISCARD) < 0) |
| 231 |
{ |
| 232 |
bd->pVB->Unlock(); |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
// Copy and convert all vertices into a single contiguous buffer, convert colors to DX9 default format. |
| 237 |
// FIXME-OPT: This is a minor waste of resource, the ideal is to use imconfig.h and |
| 238 |
// 1) to avoid repacking colors: #define IMGUI_USE_BGRA_PACKED_COLOR |
| 239 |
// 2) to avoid repacking vertices: #define IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT struct ImDrawVert { ImVec2 pos; float z; ImU32 col; ImVec2 uv; } |
| 240 |
for (const ImDrawList* draw_list : draw_data->CmdLists) |
| 241 |
{ |
| 242 |
const ImDrawVert* vtx_src = draw_list->VtxBuffer.Data; |
| 243 |
for (int i = 0; i < draw_list->VtxBuffer.Size; i++) |
| 244 |
{ |
| 245 |
vtx_dst->pos[0] = vtx_src->pos.x; |
| 246 |
vtx_dst->pos[1] = vtx_src->pos.y; |
| 247 |
vtx_dst->pos[2] = 0.0f; |
| 248 |
vtx_dst->col = IMGUI_COL_TO_DX9_ARGB(vtx_src->col); |
| 249 |
vtx_dst->uv[0] = vtx_src->uv.x; |
| 250 |
vtx_dst->uv[1] = vtx_src->uv.y; |
| 251 |
vtx_dst++; |
| 252 |
vtx_src++; |
| 253 |
} |
| 254 |
// tildearrow: this workaround allows Furnace to run on buggy VIA drivers |
| 255 |
if (sizeof(ImDrawIdx) == sizeof(unsigned short)) { |
| 256 |
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx)); |
| 257 |
} else { |
| 258 |
// slower, but works on VIA |
| 259 |
for (int i=0; i<draw_list->IdxBuffer.Size; i++) { |
| 260 |
idx_dst[i]=draw_list->IdxBuffer.Data[i]; |
| 261 |
} |
| 262 |
} |
| 263 |
idx_dst += draw_list->IdxBuffer.Size; |
| 264 |
} |
| 265 |
bd->pVB->Unlock(); |
| 266 |
bd->pIB->Unlock(); |
| 267 |
device->SetStreamSource(0, bd->pVB, 0, sizeof(CUSTOMVERTEX)); |
| 268 |
device->SetIndices(bd->pIB); |
| 269 |
device->SetFVF(D3DFVF_CUSTOMVERTEX); |
| 270 |
|
| 271 |
// Setup desired DX state |
| 272 |
ImGui_ImplDX9_SetupRenderState(draw_data); |
| 273 |
|
| 274 |
// Render command lists |
| 275 |
// (Because we merged all buffers into a single one, we maintain our own offset into them) |
| 276 |
int global_vtx_offset = 0; |
| 277 |
int global_idx_offset = 0; |
| 278 |
ImVec2 clip_off = draw_data->DisplayPos; |
| 279 |
for (const ImDrawList* draw_list : draw_data->CmdLists) |
| 280 |
{ |
| 281 |
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++) |
| 282 |
{ |
| 283 |
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i]; |
| 284 |
if (pcmd->UserCallback != nullptr) |
| 285 |
{ |
| 286 |
// User callback, registered via ImDrawList::AddCallback() |
| 287 |
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.) |
| 288 |
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) |
| 289 |
ImGui_ImplDX9_SetupRenderState(draw_data); |
| 290 |
else |
| 291 |
pcmd->UserCallback(draw_list, pcmd); |
| 292 |
} |
| 293 |
else |
| 294 |
{ |
| 295 |
// Project scissor/clipping rectangles into framebuffer space |
| 296 |
ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y); |
| 297 |
ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y); |
| 298 |
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y) |
| 299 |
continue; |
| 300 |
|
| 301 |
// Apply scissor/clipping rectangle |
| 302 |
const RECT r = { (LONG)clip_min.x, (LONG)clip_min.y, (LONG)clip_max.x, (LONG)clip_max.y }; |
| 303 |
device->SetScissorRect(&r); |
| 304 |
|
| 305 |
// Bind texture, Draw |
| 306 |
const LPDIRECT3DTEXTURE9 texture = (LPDIRECT3DTEXTURE9)pcmd->GetTexID(); |
| 307 |
device->SetTexture(0, texture); |
| 308 |
device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, pcmd->VtxOffset + global_vtx_offset, 0, (UINT)draw_list->VtxBuffer.Size, pcmd->IdxOffset + global_idx_offset, pcmd->ElemCount / 3); |
| 309 |
} |
| 310 |
} |
| 311 |
global_idx_offset += draw_list->IdxBuffer.Size; |
| 312 |
global_vtx_offset += draw_list->VtxBuffer.Size; |
| 313 |
} |
| 314 |
|
| 315 |
// When using multi-viewports, it appears that there's an odd logic in DirectX9 which prevent subsequent windows |
| 316 |
// from rendering until the first window submits at least one draw call, even once. That's our workaround. (see #2560) |
| 317 |
if (global_vtx_offset == 0) |
| 318 |
bd->pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 0, 0, 0); |
| 319 |
|
| 320 |
// Restore the DX9 transform |
| 321 |
// don't. I like this transform. |
| 322 |
/* |
| 323 |
device->SetTransform(D3DTS_WORLD, &last_world); |
| 324 |
device->SetTransform(D3DTS_VIEW, &last_view); |
| 325 |
device->SetTransform(D3DTS_PROJECTION, &last_projection); |
| 326 |
*/ |
| 327 |
|
| 328 |
// Restore the DX9 state |
| 329 |
/* |
| 330 |
state_block->Apply(); |
| 331 |
state_block->Release(); |
| 332 |
*/ |
| 333 |
} |
| 334 |
|
| 335 |
static bool ImGui_ImplDX9_CheckFormatSupport(LPDIRECT3DDEVICE9 pDevice, D3DFORMAT format) |
| 336 |
{ |
| 337 |
LPDIRECT3D9 pd3d = nullptr; |
| 338 |
if (pDevice->GetDirect3D(&pd3d) != D3D_OK) |
| 339 |
return false; |
| 340 |
D3DDEVICE_CREATION_PARAMETERS param = {}; |
| 341 |
D3DDISPLAYMODE mode = {}; |
| 342 |
if (pDevice->GetCreationParameters(¶m) != D3D_OK || pDevice->GetDisplayMode(0, &mode) != D3D_OK) |
| 343 |
{ |
| 344 |
pd3d->Release(); |
| 345 |
return false; |
| 346 |
} |
| 347 |
// Font texture should support linear filter, color blend and write to render-target |
| 348 |
bool support = (pd3d->CheckDeviceFormat(param.AdapterOrdinal, param.DeviceType, mode.Format, D3DUSAGE_DYNAMIC | D3DUSAGE_QUERY_FILTER | D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, D3DRTYPE_TEXTURE, format)) == D3D_OK; |
| 349 |
pd3d->Release(); |
| 350 |
return support; |
| 351 |
} |
| 352 |
|
| 353 |
bool ImGui_ImplDX9_Init(IDirect3DDevice9* device) |
| 354 |
{ |
| 355 |
ImGuiIO& io = ImGui::GetIO(); |
| 356 |
IMGUI_CHECKVERSION(); |
| 357 |
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); |
| 358 |
|
| 359 |
// Setup backend capabilities flags |
| 360 |
ImGui_ImplDX9_Data* bd = IM_NEW(ImGui_ImplDX9_Data)(); |
| 361 |
io.BackendRendererUserData = (void*)bd; |
| 362 |
io.BackendRendererName = "imgui_impl_dx9"; |
| 363 |
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. |
| 364 |
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render. |
| 365 |
io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional) |
| 366 |
|
| 367 |
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 368 |
platform_io.Renderer_TextureMaxWidth = platform_io.Renderer_TextureMaxHeight = 4096; |
| 369 |
|
| 370 |
bd->pd3dDevice = device; |
| 371 |
bd->pd3dDevice->AddRef(); |
| 372 |
bd->HasRgbaSupport = ImGui_ImplDX9_CheckFormatSupport(bd->pd3dDevice, D3DFMT_A8B8G8R8); |
| 373 |
|
| 374 |
ImGui_ImplDX9_InitMultiViewportSupport(); |
| 375 |
|
| 376 |
return true; |
| 377 |
} |
| 378 |
|
| 379 |
void ImGui_ImplDX9_Shutdown() |
| 380 |
{ |
| 381 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 382 |
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?"); |
| 383 |
ImGuiIO& io = ImGui::GetIO(); |
| 384 |
|
| 385 |
ImGui_ImplDX9_ShutdownMultiViewportSupport(); |
| 386 |
ImGui_ImplDX9_InvalidateDeviceObjects(); |
| 387 |
if (bd->pd3dDevice) { bd->pd3dDevice->Release(); } |
| 388 |
io.BackendRendererName = nullptr; |
| 389 |
io.BackendRendererUserData = nullptr; |
| 390 |
io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports); |
| 391 |
IM_DELETE(bd); |
| 392 |
} |
| 393 |
|
| 394 |
// Convert RGBA32 to BGRA32 (because RGBA32 is not well supported by DX9 devices) |
| 395 |
static void ImGui_ImplDX9_CopyTextureRegion(bool tex_use_colors, const ImU32* src, int src_pitch, ImU32* dst, int dst_pitch, int w, int h) |
| 396 |
{ |
| 397 |
#ifndef IMGUI_USE_BGRA_PACKED_COLOR |
| 398 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 399 |
const bool convert_rgba_to_bgra = (!bd->HasRgbaSupport && tex_use_colors); |
| 400 |
#else |
| 401 |
const bool convert_rgba_to_bgra = false; |
| 402 |
IM_UNUSED(tex_use_colors); |
| 403 |
#endif |
| 404 |
for (int y = 0; y < h; y++) |
| 405 |
{ |
| 406 |
const ImU32* src_p = (const ImU32*)(const void*)((const unsigned char*)src + src_pitch * y); |
| 407 |
ImU32* dst_p = (ImU32*)(void*)((unsigned char*)dst + dst_pitch * y); |
| 408 |
if (convert_rgba_to_bgra) |
| 409 |
for (int x = w; x > 0; x--, src_p++, dst_p++) // Convert copy |
| 410 |
*dst_p = IMGUI_COL_TO_DX9_ARGB(*src_p); |
| 411 |
else |
| 412 |
memcpy(dst_p, src_p, w * 4); // Raw copy |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
void ImGui_ImplDX9_UpdateTexture(ImTextureData* tex) |
| 417 |
{ |
| 418 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 419 |
|
| 420 |
if (tex->Status == ImTextureStatus_WantCreate) |
| 421 |
{ |
| 422 |
// Create and upload new texture to graphics system |
| 423 |
//IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height); |
| 424 |
IM_ASSERT(tex->TexID == ImTextureID_Invalid && tex->BackendUserData == nullptr); |
| 425 |
IM_ASSERT(tex->Format == ImTextureFormat_RGBA32); |
| 426 |
LPDIRECT3DTEXTURE9 dx_tex = nullptr; |
| 427 |
HRESULT hr = bd->pd3dDevice->CreateTexture(tex->Width, tex->Height, 1, D3DUSAGE_DYNAMIC, bd->HasRgbaSupport ? D3DFMT_A8B8G8R8 : D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &dx_tex, nullptr); |
| 428 |
if (hr < 0) |
| 429 |
{ |
| 430 |
IM_ASSERT(hr >= 0 && "Backend failed to create texture!"); |
| 431 |
return; |
| 432 |
} |
| 433 |
|
| 434 |
D3DLOCKED_RECT locked_rect; |
| 435 |
if (dx_tex->LockRect(0, &locked_rect, nullptr, 0) == D3D_OK) |
| 436 |
{ |
| 437 |
ImGui_ImplDX9_CopyTextureRegion(tex->UseColors, (ImU32*)tex->GetPixels(), tex->Width * 4, (ImU32*)locked_rect.pBits, (ImU32)locked_rect.Pitch, tex->Width, tex->Height); |
| 438 |
dx_tex->UnlockRect(0); |
| 439 |
} |
| 440 |
|
| 441 |
// Store identifiers |
| 442 |
tex->SetTexID((ImTextureID)(intptr_t)dx_tex); |
| 443 |
tex->SetStatus(ImTextureStatus_OK); |
| 444 |
} |
| 445 |
else if (tex->Status == ImTextureStatus_WantUpdates) |
| 446 |
{ |
| 447 |
// Update selected blocks. We only ever write to textures regions which have never been used before! |
| 448 |
// This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region. |
| 449 |
LPDIRECT3DTEXTURE9 backend_tex = (LPDIRECT3DTEXTURE9)(intptr_t)tex->TexID; |
| 450 |
RECT update_rect = { (LONG)tex->UpdateRect.x, (LONG)tex->UpdateRect.y, (LONG)(tex->UpdateRect.x + tex->UpdateRect.w), (LONG)(tex->UpdateRect.y + tex->UpdateRect.h) }; |
| 451 |
D3DLOCKED_RECT locked_rect; |
| 452 |
if (backend_tex->LockRect(0, &locked_rect, &update_rect, 0) == D3D_OK) |
| 453 |
for (ImTextureRect& r : tex->Updates) |
| 454 |
ImGui_ImplDX9_CopyTextureRegion(tex->UseColors, (ImU32*)tex->GetPixelsAt(r.x, r.y), tex->Width * 4, |
| 455 |
(ImU32*)locked_rect.pBits + (r.x - update_rect.left) + (r.y - update_rect.top) * (locked_rect.Pitch / 4), (int)locked_rect.Pitch, r.w, r.h); |
| 456 |
backend_tex->UnlockRect(0); |
| 457 |
tex->SetStatus(ImTextureStatus_OK); |
| 458 |
} |
| 459 |
else if (tex->Status == ImTextureStatus_WantDestroy) |
| 460 |
{ |
| 461 |
LPDIRECT3DTEXTURE9 backend_tex = (LPDIRECT3DTEXTURE9)tex->TexID; |
| 462 |
if (backend_tex == nullptr) |
| 463 |
return; |
| 464 |
IM_ASSERT(tex->TexID == (ImTextureID)(intptr_t)backend_tex); |
| 465 |
backend_tex->Release(); |
| 466 |
|
| 467 |
// Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running) |
| 468 |
tex->SetTexID(ImTextureID_Invalid); |
| 469 |
tex->SetStatus(ImTextureStatus_Destroyed); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
bool ImGui_ImplDX9_CreateDeviceObjects() |
| 474 |
{ |
| 475 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 476 |
if (!bd || !bd->pd3dDevice) |
| 477 |
return false; |
| 478 |
ImGui_ImplDX9_CreateDeviceObjectsForPlatformWindows(); |
| 479 |
return true; |
| 480 |
} |
| 481 |
|
| 482 |
void ImGui_ImplDX9_InvalidateDeviceObjects() |
| 483 |
{ |
| 484 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 485 |
if (!bd || !bd->pd3dDevice) |
| 486 |
return; |
| 487 |
|
| 488 |
// Destroy all textures |
| 489 |
for (ImTextureData* tex : ImGui::GetPlatformIO().Textures) |
| 490 |
if (tex->RefCount == 1) |
| 491 |
{ |
| 492 |
tex->SetStatus(ImTextureStatus_WantDestroy); |
| 493 |
ImGui_ImplDX9_UpdateTexture(tex); |
| 494 |
} |
| 495 |
if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; } |
| 496 |
if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; } |
| 497 |
ImGui_ImplDX9_InvalidateDeviceObjectsForPlatformWindows(); |
| 498 |
} |
| 499 |
|
| 500 |
void ImGui_ImplDX9_NewFrame() |
| 501 |
{ |
| 502 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 503 |
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplDX9_Init()?"); |
| 504 |
IM_UNUSED(bd); |
| 505 |
} |
| 506 |
|
| 507 |
//-------------------------------------------------------------------------------------------------------- |
| 508 |
// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT |
| 509 |
// This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously. |
| 510 |
// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first.. |
| 511 |
//-------------------------------------------------------------------------------------------------------- |
| 512 |
|
| 513 |
// Helper structure we store in the void* RendererUserData field of each ImGuiViewport to easily retrieve our backend data. |
| 514 |
struct ImGui_ImplDX9_ViewportData |
| 515 |
{ |
| 516 |
IDirect3DSwapChain9* SwapChain; |
| 517 |
D3DPRESENT_PARAMETERS d3dpp; |
| 518 |
|
| 519 |
ImGui_ImplDX9_ViewportData() { SwapChain = nullptr; ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS)); } |
| 520 |
~ImGui_ImplDX9_ViewportData() { IM_ASSERT(SwapChain == nullptr); } |
| 521 |
}; |
| 522 |
|
| 523 |
static void ImGui_ImplDX9_CreateWindow(ImGuiViewport* viewport) |
| 524 |
{ |
| 525 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 526 |
ImGui_ImplDX9_ViewportData* vd = IM_NEW(ImGui_ImplDX9_ViewportData)(); |
| 527 |
viewport->RendererUserData = vd; |
| 528 |
|
| 529 |
// PlatformHandleRaw should always be a HWND, whereas PlatformHandle might be a higher-level handle (e.g. GLFWWindow*, SDL's WindowID). |
| 530 |
// Some backends will leave PlatformHandleRaw == 0, in which case we assume PlatformHandle will contain the HWND. |
| 531 |
HWND hwnd = viewport->PlatformHandleRaw ? (HWND)viewport->PlatformHandleRaw : (HWND)viewport->PlatformHandle; |
| 532 |
IM_ASSERT(hwnd != 0); |
| 533 |
|
| 534 |
ZeroMemory(&vd->d3dpp, sizeof(D3DPRESENT_PARAMETERS)); |
| 535 |
vd->d3dpp.Windowed = TRUE; |
| 536 |
vd->d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; |
| 537 |
vd->d3dpp.BackBufferWidth = (UINT)viewport->Size.x; |
| 538 |
vd->d3dpp.BackBufferHeight = (UINT)viewport->Size.y; |
| 539 |
vd->d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; |
| 540 |
vd->d3dpp.hDeviceWindow = hwnd; |
| 541 |
vd->d3dpp.EnableAutoDepthStencil = FALSE; |
| 542 |
vd->d3dpp.AutoDepthStencilFormat = D3DFMT_D16; |
| 543 |
vd->d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Present without vsync |
| 544 |
|
| 545 |
HRESULT hr = bd->pd3dDevice->CreateAdditionalSwapChain(&vd->d3dpp, &vd->SwapChain); IM_UNUSED(hr); |
| 546 |
IM_ASSERT(hr == D3D_OK); |
| 547 |
IM_ASSERT(vd->SwapChain != nullptr); |
| 548 |
} |
| 549 |
|
| 550 |
static void ImGui_ImplDX9_DestroyWindow(ImGuiViewport* viewport) |
| 551 |
{ |
| 552 |
// The main viewport (owned by the application) will always have RendererUserData == 0 since we didn't create the data for it. |
| 553 |
if (ImGui_ImplDX9_ViewportData* vd = (ImGui_ImplDX9_ViewportData*)viewport->RendererUserData) |
| 554 |
{ |
| 555 |
if (vd->SwapChain) |
| 556 |
vd->SwapChain->Release(); |
| 557 |
vd->SwapChain = nullptr; |
| 558 |
ZeroMemory(&vd->d3dpp, sizeof(D3DPRESENT_PARAMETERS)); |
| 559 |
IM_DELETE(vd); |
| 560 |
} |
| 561 |
viewport->RendererUserData = nullptr; |
| 562 |
} |
| 563 |
|
| 564 |
static void ImGui_ImplDX9_SetWindowSize(ImGuiViewport* viewport, ImVec2 size) |
| 565 |
{ |
| 566 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 567 |
ImGui_ImplDX9_ViewportData* vd = (ImGui_ImplDX9_ViewportData*)viewport->RendererUserData; |
| 568 |
if (vd->SwapChain) |
| 569 |
{ |
| 570 |
vd->SwapChain->Release(); |
| 571 |
vd->SwapChain = nullptr; |
| 572 |
vd->d3dpp.BackBufferWidth = (UINT)size.x; |
| 573 |
vd->d3dpp.BackBufferHeight = (UINT)size.y; |
| 574 |
HRESULT hr = bd->pd3dDevice->CreateAdditionalSwapChain(&vd->d3dpp, &vd->SwapChain); IM_UNUSED(hr); |
| 575 |
IM_ASSERT(hr == D3D_OK); |
| 576 |
} |
| 577 |
} |
| 578 |
|
| 579 |
static void ImGui_ImplDX9_RenderWindow(ImGuiViewport* viewport, void*) |
| 580 |
{ |
| 581 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 582 |
ImGui_ImplDX9_ViewportData* vd = (ImGui_ImplDX9_ViewportData*)viewport->RendererUserData; |
| 583 |
ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f); |
| 584 |
|
| 585 |
LPDIRECT3DSURFACE9 render_target = nullptr; |
| 586 |
LPDIRECT3DSURFACE9 last_render_target = nullptr; |
| 587 |
LPDIRECT3DSURFACE9 last_depth_stencil = nullptr; |
| 588 |
vd->SwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &render_target); |
| 589 |
bd->pd3dDevice->GetRenderTarget(0, &last_render_target); |
| 590 |
bd->pd3dDevice->GetDepthStencilSurface(&last_depth_stencil); |
| 591 |
bd->pd3dDevice->SetRenderTarget(0, render_target); |
| 592 |
bd->pd3dDevice->SetDepthStencilSurface(nullptr); |
| 593 |
|
| 594 |
if (!(viewport->Flags & ImGuiViewportFlags_NoRendererClear)) |
| 595 |
{ |
| 596 |
D3DCOLOR clear_col_dx = D3DCOLOR_RGBA((int)(clear_color.x*255.0f), (int)(clear_color.y*255.0f), (int)(clear_color.z*255.0f), (int)(clear_color.w*255.0f)); |
| 597 |
bd->pd3dDevice->Clear(0, nullptr, D3DCLEAR_TARGET, clear_col_dx, 1.0f, 0); |
| 598 |
} |
| 599 |
|
| 600 |
ImGui_ImplDX9_RenderDrawData(viewport->DrawData); |
| 601 |
|
| 602 |
// Restore render target |
| 603 |
bd->pd3dDevice->SetRenderTarget(0, last_render_target); |
| 604 |
bd->pd3dDevice->SetDepthStencilSurface(last_depth_stencil); |
| 605 |
render_target->Release(); |
| 606 |
last_render_target->Release(); |
| 607 |
if (last_depth_stencil) last_depth_stencil->Release(); |
| 608 |
} |
| 609 |
|
| 610 |
static void ImGui_ImplDX9_SwapBuffers(ImGuiViewport* viewport, void*) |
| 611 |
{ |
| 612 |
ImGui_ImplDX9_ViewportData* vd = (ImGui_ImplDX9_ViewportData*)viewport->RendererUserData; |
| 613 |
HRESULT hr = vd->SwapChain->Present(nullptr, nullptr, vd->d3dpp.hDeviceWindow, nullptr, 0); |
| 614 |
// Let main application handle D3DERR_DEVICELOST by resetting the device. |
| 615 |
IM_ASSERT(SUCCEEDED(hr) || hr == D3DERR_DEVICELOST); |
| 616 |
} |
| 617 |
|
| 618 |
static void ImGui_ImplDX9_InitMultiViewportSupport() |
| 619 |
{ |
| 620 |
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 621 |
platform_io.Renderer_CreateWindow = ImGui_ImplDX9_CreateWindow; |
| 622 |
platform_io.Renderer_DestroyWindow = ImGui_ImplDX9_DestroyWindow; |
| 623 |
platform_io.Renderer_SetWindowSize = ImGui_ImplDX9_SetWindowSize; |
| 624 |
platform_io.Renderer_RenderWindow = ImGui_ImplDX9_RenderWindow; |
| 625 |
platform_io.Renderer_SwapBuffers = ImGui_ImplDX9_SwapBuffers; |
| 626 |
} |
| 627 |
|
| 628 |
static void ImGui_ImplDX9_ShutdownMultiViewportSupport() |
| 629 |
{ |
| 630 |
ImGui::DestroyPlatformWindows(); |
| 631 |
} |
| 632 |
|
| 633 |
static void ImGui_ImplDX9_CreateDeviceObjectsForPlatformWindows() |
| 634 |
{ |
| 635 |
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 636 |
for (int i = 1; i < platform_io.Viewports.Size; i++) |
| 637 |
if (!platform_io.Viewports[i]->RendererUserData) |
| 638 |
ImGui_ImplDX9_CreateWindow(platform_io.Viewports[i]); |
| 639 |
} |
| 640 |
|
| 641 |
static void ImGui_ImplDX9_InvalidateDeviceObjectsForPlatformWindows() |
| 642 |
{ |
| 643 |
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 644 |
for (int i = 1; i < platform_io.Viewports.Size; i++) |
| 645 |
if (platform_io.Viewports[i]->RendererUserData) |
| 646 |
ImGui_ImplDX9_DestroyWindow(platform_io.Viewports[i]); |
| 647 |
} |
| 648 |
|
| 649 |
//----------------------------------------------------------------------------- |
| 650 |
|
| 651 |
#endif // #ifndef IMGUI_DISABLE |