| 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 ImTextureID. Read the FAQ about ImTextureID! |
| 6 |
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset). |
| 7 |
// [X] Renderer: IMGUI_USE_BGRA_PACKED_COLOR support, as this is the optimal color encoding for DirectX9. |
| 8 |
|
| 9 |
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
| 10 |
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
| 11 |
// Learn about Dear ImGui: |
| 12 |
// - FAQ https://dearimgui.com/faq |
| 13 |
// - Getting Started https://dearimgui.com/getting-started |
| 14 |
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
| 15 |
// - Introduction, links and more at the top of imgui.cpp |
| 16 |
|
| 17 |
// CHANGELOG |
| 18 |
// (minor and older changes stripped away, please see git history for details) |
| 19 |
// 2024-10-07: DirectX9: Changed default texture sampler to Clamp instead of Repeat/Wrap. |
| 20 |
// 2024-02-12: DirectX9: Using RGBA format when supported by the driver to avoid CPU side conversion. (#6575) |
| 21 |
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. |
| 22 |
// 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). |
| 23 |
// 2021-06-25: DirectX9: Explicitly disable texture state stages after >= 1. |
| 24 |
// 2021-05-19: DirectX9: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement) |
| 25 |
// 2021-04-23: DirectX9: Explicitly setting up more graphics states to increase compatibility with unusual non-default states. |
| 26 |
// 2021-03-18: DirectX9: Calling IDirect3DStateBlock9::Capture() after CreateStateBlock() as a workaround for state restoring issues (see #3857). |
| 27 |
// 2021-03-03: DirectX9: Added support for IMGUI_USE_BGRA_PACKED_COLOR in user's imconfig file. |
| 28 |
// 2021-02-18: DirectX9: Change blending equation to preserve alpha in output buffer. |
| 29 |
// 2019-05-29: DirectX9: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag. |
| 30 |
// 2019-04-30: DirectX9: Added support for special ImDrawCallback_ResetRenderState callback to reset render state. |
| 31 |
// 2019-03-29: Misc: Fixed erroneous assert in ImGui_ImplDX9_InvalidateDeviceObjects(). |
| 32 |
// 2019-01-16: Misc: Disabled fog before drawing UI's. Fixes issue #2288. |
| 33 |
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window. |
| 34 |
// 2018-06-08: Misc: Extracted imgui_impl_dx9.cpp/.h away from the old combined DX9+Win32 example. |
| 35 |
// 2018-06-08: DirectX9: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle. |
| 36 |
// 2018-05-07: Render: Saving/restoring Transform because they don't seem to be included in the StateBlock. Setting shading mode to Gouraud. |
| 37 |
// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX9_RenderDrawData() in the .h file so you can call it yourself. |
| 38 |
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves. |
| 39 |
|
| 40 |
#include "imgui.h" |
| 41 |
#ifndef IMGUI_DISABLE |
| 42 |
#include "imgui_impl_dx9.h" |
| 43 |
|
| 44 |
// DirectX |
| 45 |
#include <d3d9.h> |
| 46 |
|
| 47 |
// DirectX data |
| 48 |
struct ImGui_ImplDX9_Data |
| 49 |
{ |
| 50 |
LPDIRECT3DDEVICE9 pd3dDevice; |
| 51 |
LPDIRECT3DVERTEXBUFFER9 pVB; |
| 52 |
LPDIRECT3DINDEXBUFFER9 pIB; |
| 53 |
LPDIRECT3DTEXTURE9 FontTexture; |
| 54 |
int VertexBufferSize; |
| 55 |
int IndexBufferSize; |
| 56 |
bool HasRgbaSupport; |
| 57 |
|
| 58 |
ImGui_ImplDX9_Data() { memset((void*)this, 0, sizeof(*this)); VertexBufferSize = 5000; IndexBufferSize = 10000; } |
| 59 |
}; |
| 60 |
|
| 61 |
struct CUSTOMVERTEX |
| 62 |
{ |
| 63 |
float pos[3]; |
| 64 |
D3DCOLOR col; |
| 65 |
float uv[2]; |
| 66 |
}; |
| 67 |
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1) |
| 68 |
|
| 69 |
#ifdef IMGUI_USE_BGRA_PACKED_COLOR |
| 70 |
#define IMGUI_COL_TO_DX9_ARGB(_COL) (_COL) |
| 71 |
#else |
| 72 |
#define IMGUI_COL_TO_DX9_ARGB(_COL) (((_COL) & 0xFF00FF00) | (((_COL) & 0xFF0000) >> 16) | (((_COL) & 0xFF) << 16)) |
| 73 |
#endif |
| 74 |
|
| 75 |
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts |
| 76 |
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. |
| 77 |
static ImGui_ImplDX9_Data* ImGui_ImplDX9_GetBackendData() |
| 78 |
{ |
| 79 |
return ImGui::GetCurrentContext() ? (ImGui_ImplDX9_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; |
| 80 |
} |
| 81 |
|
| 82 |
// Functions |
| 83 |
static void ImGui_ImplDX9_SetupRenderState(ImDrawData* draw_data) |
| 84 |
{ |
| 85 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 86 |
|
| 87 |
// Setup viewport |
| 88 |
D3DVIEWPORT9 vp; |
| 89 |
vp.X = vp.Y = 0; |
| 90 |
vp.Width = (DWORD)draw_data->DisplaySize.x; |
| 91 |
vp.Height = (DWORD)draw_data->DisplaySize.y; |
| 92 |
vp.MinZ = 0.0f; |
| 93 |
vp.MaxZ = 1.0f; |
| 94 |
|
| 95 |
LPDIRECT3DDEVICE9 device = bd->pd3dDevice; |
| 96 |
device->SetViewport(&vp); |
| 97 |
|
| 98 |
// Setup render state: fixed-pipeline, alpha-blending, no face culling, no depth testing, shade mode (for gradient), bilinear sampling. |
| 99 |
device->SetPixelShader(nullptr); |
| 100 |
device->SetVertexShader(nullptr); |
| 101 |
device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); |
| 102 |
device->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD); |
| 103 |
device->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); |
| 104 |
device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); |
| 105 |
device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); |
| 106 |
device->SetRenderState(D3DRS_ZENABLE, FALSE); |
| 107 |
device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); |
| 108 |
device->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); |
| 109 |
device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); |
| 110 |
device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); |
| 111 |
device->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); |
| 112 |
device->SetRenderState(D3DRS_SRCBLENDALPHA, D3DBLEND_ONE); |
| 113 |
device->SetRenderState(D3DRS_DESTBLENDALPHA, D3DBLEND_INVSRCALPHA); |
| 114 |
device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE); |
| 115 |
device->SetRenderState(D3DRS_FOGENABLE, FALSE); |
| 116 |
device->SetRenderState(D3DRS_RANGEFOGENABLE, FALSE); |
| 117 |
device->SetRenderState(D3DRS_SPECULARENABLE, FALSE); |
| 118 |
device->SetRenderState(D3DRS_STENCILENABLE, FALSE); |
| 119 |
device->SetRenderState(D3DRS_CLIPPING, TRUE); |
| 120 |
device->SetRenderState(D3DRS_LIGHTING, FALSE); |
| 121 |
device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); |
| 122 |
device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); |
| 123 |
device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); |
| 124 |
device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); |
| 125 |
device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); |
| 126 |
device->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE); |
| 127 |
device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); |
| 128 |
device->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE); |
| 129 |
device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); |
| 130 |
device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); |
| 131 |
device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); |
| 132 |
device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); |
| 133 |
|
| 134 |
// Setup orthographic projection matrix |
| 135 |
// 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. |
| 136 |
// Being agnostic of whether <d3dx9.h> or <DirectXMath.h> can be used, we aren't relying on D3DXMatrixIdentity()/D3DXMatrixOrthoOffCenterLH() or DirectX::XMMatrixIdentity()/DirectX::XMMatrixOrthographicOffCenterLH() |
| 137 |
{ |
| 138 |
float L = draw_data->DisplayPos.x + 0.5f; |
| 139 |
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x + 0.5f; |
| 140 |
float T = draw_data->DisplayPos.y + 0.5f; |
| 141 |
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y + 0.5f; |
| 142 |
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 } } }; |
| 143 |
D3DMATRIX mat_projection = |
| 144 |
{ { { |
| 145 |
2.0f/(R-L), 0.0f, 0.0f, 0.0f, |
| 146 |
0.0f, 2.0f/(T-B), 0.0f, 0.0f, |
| 147 |
0.0f, 0.0f, 0.5f, 0.0f, |
| 148 |
(L+R)/(L-R), (T+B)/(B-T), 0.5f, 1.0f |
| 149 |
} } }; |
| 150 |
device->SetTransform(D3DTS_WORLD, &mat_identity); |
| 151 |
device->SetTransform(D3DTS_VIEW, &mat_identity); |
| 152 |
device->SetTransform(D3DTS_PROJECTION, &mat_projection); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
// Render function. |
| 157 |
void ImGui_ImplDX9_RenderDrawData(ImDrawData* draw_data) |
| 158 |
{ |
| 159 |
// Avoid rendering when minimized |
| 160 |
if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f) |
| 161 |
return; |
| 162 |
|
| 163 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 164 |
LPDIRECT3DDEVICE9 device = bd->pd3dDevice; |
| 165 |
|
| 166 |
// Create and grow buffers if needed |
| 167 |
if (!bd->pVB || bd->VertexBufferSize < draw_data->TotalVtxCount) |
| 168 |
{ |
| 169 |
if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; } |
| 170 |
bd->VertexBufferSize = draw_data->TotalVtxCount + 5000; |
| 171 |
if (device->CreateVertexBuffer(bd->VertexBufferSize * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &bd->pVB, nullptr) < 0) |
| 172 |
return; |
| 173 |
} |
| 174 |
if (!bd->pIB || bd->IndexBufferSize < draw_data->TotalIdxCount) |
| 175 |
{ |
| 176 |
if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; } |
| 177 |
bd->IndexBufferSize = draw_data->TotalIdxCount + 10000; |
| 178 |
if (device->CreateIndexBuffer(bd->IndexBufferSize * sizeof(ImDrawIdx), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, sizeof(ImDrawIdx) == 2 ? D3DFMT_INDEX16 : D3DFMT_INDEX32, D3DPOOL_DEFAULT, &bd->pIB, nullptr) < 0) |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
// Backup the DX9 state |
| 183 |
IDirect3DStateBlock9* state_block = nullptr; |
| 184 |
if (device->CreateStateBlock(D3DSBT_ALL, &state_block) < 0) |
| 185 |
return; |
| 186 |
if (state_block->Capture() < 0) |
| 187 |
{ |
| 188 |
state_block->Release(); |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
// Backup the DX9 transform (DX9 documentation suggests that it is included in the StateBlock but it doesn't appear to) |
| 193 |
D3DMATRIX last_world, last_view, last_projection; |
| 194 |
device->GetTransform(D3DTS_WORLD, &last_world); |
| 195 |
device->GetTransform(D3DTS_VIEW, &last_view); |
| 196 |
device->GetTransform(D3DTS_PROJECTION, &last_projection); |
| 197 |
|
| 198 |
// Allocate buffers |
| 199 |
CUSTOMVERTEX* vtx_dst; |
| 200 |
ImDrawIdx* idx_dst; |
| 201 |
if (bd->pVB->Lock(0, (UINT)(draw_data->TotalVtxCount * sizeof(CUSTOMVERTEX)), (void**)&vtx_dst, D3DLOCK_DISCARD) < 0) |
| 202 |
{ |
| 203 |
state_block->Release(); |
| 204 |
return; |
| 205 |
} |
| 206 |
if (bd->pIB->Lock(0, (UINT)(draw_data->TotalIdxCount * sizeof(ImDrawIdx)), (void**)&idx_dst, D3DLOCK_DISCARD) < 0) |
| 207 |
{ |
| 208 |
bd->pVB->Unlock(); |
| 209 |
state_block->Release(); |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
// Copy and convert all vertices into a single contiguous buffer, convert colors to DX9 default format. |
| 214 |
// FIXME-OPT: This is a minor waste of resource, the ideal is to use imconfig.h and |
| 215 |
// 1) to avoid repacking colors: #define IMGUI_USE_BGRA_PACKED_COLOR |
| 216 |
// 2) to avoid repacking vertices: #define IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT struct ImDrawVert { ImVec2 pos; float z; ImU32 col; ImVec2 uv; } |
| 217 |
for (int n = 0; n < draw_data->CmdListsCount; n++) |
| 218 |
{ |
| 219 |
const ImDrawList* draw_list = draw_data->CmdLists[n]; |
| 220 |
const ImDrawVert* vtx_src = draw_list->VtxBuffer.Data; |
| 221 |
for (int i = 0; i < draw_list->VtxBuffer.Size; i++) |
| 222 |
{ |
| 223 |
vtx_dst->pos[0] = vtx_src->pos.x; |
| 224 |
vtx_dst->pos[1] = vtx_src->pos.y; |
| 225 |
vtx_dst->pos[2] = 0.0f; |
| 226 |
vtx_dst->col = IMGUI_COL_TO_DX9_ARGB(vtx_src->col); |
| 227 |
vtx_dst->uv[0] = vtx_src->uv.x; |
| 228 |
vtx_dst->uv[1] = vtx_src->uv.y; |
| 229 |
vtx_dst++; |
| 230 |
vtx_src++; |
| 231 |
} |
| 232 |
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx)); |
| 233 |
idx_dst += draw_list->IdxBuffer.Size; |
| 234 |
} |
| 235 |
bd->pVB->Unlock(); |
| 236 |
bd->pIB->Unlock(); |
| 237 |
device->SetStreamSource(0, bd->pVB, 0, sizeof(CUSTOMVERTEX)); |
| 238 |
device->SetIndices(bd->pIB); |
| 239 |
device->SetFVF(D3DFVF_CUSTOMVERTEX); |
| 240 |
|
| 241 |
// Setup desired DX state |
| 242 |
ImGui_ImplDX9_SetupRenderState(draw_data); |
| 243 |
|
| 244 |
// Render command lists |
| 245 |
// (Because we merged all buffers into a single one, we maintain our own offset into them) |
| 246 |
int global_vtx_offset = 0; |
| 247 |
int global_idx_offset = 0; |
| 248 |
ImVec2 clip_off = draw_data->DisplayPos; |
| 249 |
for (int n = 0; n < draw_data->CmdListsCount; n++) |
| 250 |
{ |
| 251 |
const ImDrawList* draw_list = draw_data->CmdLists[n]; |
| 252 |
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++) |
| 253 |
{ |
| 254 |
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i]; |
| 255 |
if (pcmd->UserCallback != nullptr) |
| 256 |
{ |
| 257 |
// User callback, registered via ImDrawList::AddCallback() |
| 258 |
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.) |
| 259 |
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) |
| 260 |
ImGui_ImplDX9_SetupRenderState(draw_data); |
| 261 |
else |
| 262 |
pcmd->UserCallback(draw_list, pcmd); |
| 263 |
} |
| 264 |
else |
| 265 |
{ |
| 266 |
// Project scissor/clipping rectangles into framebuffer space |
| 267 |
ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y); |
| 268 |
ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y); |
| 269 |
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y) |
| 270 |
continue; |
| 271 |
|
| 272 |
// Apply scissor/clipping rectangle |
| 273 |
const RECT r = { (LONG)clip_min.x, (LONG)clip_min.y, (LONG)clip_max.x, (LONG)clip_max.y }; |
| 274 |
device->SetScissorRect(&r); |
| 275 |
|
| 276 |
// Bind texture, Draw |
| 277 |
const LPDIRECT3DTEXTURE9 texture = (LPDIRECT3DTEXTURE9)pcmd->GetTexID(); |
| 278 |
device->SetTexture(0, texture); |
| 279 |
device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, pcmd->VtxOffset + global_vtx_offset, 0, (UINT)draw_list->VtxBuffer.Size, pcmd->IdxOffset + global_idx_offset, pcmd->ElemCount / 3); |
| 280 |
} |
| 281 |
} |
| 282 |
global_idx_offset += draw_list->IdxBuffer.Size; |
| 283 |
global_vtx_offset += draw_list->VtxBuffer.Size; |
| 284 |
} |
| 285 |
|
| 286 |
// Restore the DX9 transform |
| 287 |
device->SetTransform(D3DTS_WORLD, &last_world); |
| 288 |
device->SetTransform(D3DTS_VIEW, &last_view); |
| 289 |
device->SetTransform(D3DTS_PROJECTION, &last_projection); |
| 290 |
|
| 291 |
// Restore the DX9 state |
| 292 |
state_block->Apply(); |
| 293 |
state_block->Release(); |
| 294 |
} |
| 295 |
|
| 296 |
static bool ImGui_ImplDX9_CheckFormatSupport(LPDIRECT3DDEVICE9 pDevice, D3DFORMAT format) |
| 297 |
{ |
| 298 |
LPDIRECT3D9 pd3d = nullptr; |
| 299 |
if (pDevice->GetDirect3D(&pd3d) != D3D_OK) |
| 300 |
return false; |
| 301 |
D3DDEVICE_CREATION_PARAMETERS param = {}; |
| 302 |
D3DDISPLAYMODE mode = {}; |
| 303 |
if (pDevice->GetCreationParameters(¶m) != D3D_OK || pDevice->GetDisplayMode(0, &mode) != D3D_OK) |
| 304 |
{ |
| 305 |
pd3d->Release(); |
| 306 |
return false; |
| 307 |
} |
| 308 |
// Font texture should support linear filter, color blend and write to render-target |
| 309 |
bool support = (pd3d->CheckDeviceFormat(param.AdapterOrdinal, param.DeviceType, mode.Format, D3DUSAGE_DYNAMIC | D3DUSAGE_QUERY_FILTER | D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING, D3DRTYPE_TEXTURE, format)) == D3D_OK; |
| 310 |
pd3d->Release(); |
| 311 |
return support; |
| 312 |
} |
| 313 |
|
| 314 |
bool ImGui_ImplDX9_Init(IDirect3DDevice9* device) |
| 315 |
{ |
| 316 |
ImGuiIO& io = ImGui::GetIO(); |
| 317 |
IMGUI_CHECKVERSION(); |
| 318 |
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); |
| 319 |
|
| 320 |
// Setup backend capabilities flags |
| 321 |
ImGui_ImplDX9_Data* bd = IM_NEW(ImGui_ImplDX9_Data)(); |
| 322 |
io.BackendRendererUserData = (void*)bd; |
| 323 |
io.BackendRendererName = "imgui_impl_dx9"; |
| 324 |
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. |
| 325 |
|
| 326 |
bd->pd3dDevice = device; |
| 327 |
bd->pd3dDevice->AddRef(); |
| 328 |
bd->HasRgbaSupport = ImGui_ImplDX9_CheckFormatSupport(bd->pd3dDevice, D3DFMT_A8B8G8R8); |
| 329 |
|
| 330 |
return true; |
| 331 |
} |
| 332 |
|
| 333 |
void ImGui_ImplDX9_Shutdown() |
| 334 |
{ |
| 335 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 336 |
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?"); |
| 337 |
ImGuiIO& io = ImGui::GetIO(); |
| 338 |
|
| 339 |
ImGui_ImplDX9_InvalidateDeviceObjects(); |
| 340 |
if (bd->pd3dDevice) { bd->pd3dDevice->Release(); } |
| 341 |
io.BackendRendererName = nullptr; |
| 342 |
io.BackendRendererUserData = nullptr; |
| 343 |
io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset; |
| 344 |
IM_DELETE(bd); |
| 345 |
} |
| 346 |
|
| 347 |
// Convert RGBA32 to BGRA32 (because RGBA32 is not well supported by DX9 devices) |
| 348 |
static void ImGui_ImplDX9_CopyTextureRegion(bool tex_use_colors, ImU32* src, int src_pitch, ImU32* dst, int dst_pitch, int w, int h) |
| 349 |
{ |
| 350 |
#ifndef IMGUI_USE_BGRA_PACKED_COLOR |
| 351 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 352 |
const bool convert_rgba_to_bgra = (!bd->HasRgbaSupport && tex_use_colors); |
| 353 |
#else |
| 354 |
const bool convert_rgba_to_bgra = false; |
| 355 |
IM_UNUSED(tex_use_colors); |
| 356 |
#endif |
| 357 |
for (int y = 0; y < h; y++) |
| 358 |
{ |
| 359 |
ImU32* src_p = (ImU32*)((unsigned char*)src + src_pitch * y); |
| 360 |
ImU32* dst_p = (ImU32*)((unsigned char*)dst + dst_pitch * y); |
| 361 |
if (convert_rgba_to_bgra) |
| 362 |
for (int x = w; x > 0; x--, src_p++, dst_p++) // Convert copy |
| 363 |
*dst_p = IMGUI_COL_TO_DX9_ARGB(*src_p); |
| 364 |
else |
| 365 |
memcpy(dst_p, src_p, w * 4); // Raw copy |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
static bool ImGui_ImplDX9_CreateFontsTexture() |
| 370 |
{ |
| 371 |
// Build texture atlas |
| 372 |
ImGuiIO& io = ImGui::GetIO(); |
| 373 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 374 |
unsigned char* pixels; |
| 375 |
int width, height, bytes_per_pixel; |
| 376 |
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height, &bytes_per_pixel); |
| 377 |
|
| 378 |
// Upload texture to graphics system |
| 379 |
bd->FontTexture = nullptr; |
| 380 |
if (bd->pd3dDevice->CreateTexture(width, height, 1, D3DUSAGE_DYNAMIC, bd->HasRgbaSupport ? D3DFMT_A8B8G8R8 : D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &bd->FontTexture, nullptr) < 0) |
| 381 |
return false; |
| 382 |
D3DLOCKED_RECT tex_locked_rect; |
| 383 |
if (bd->FontTexture->LockRect(0, &tex_locked_rect, nullptr, 0) != D3D_OK) |
| 384 |
return false; |
| 385 |
ImGui_ImplDX9_CopyTextureRegion(io.Fonts->TexPixelsUseColors, (ImU32*)pixels, width * bytes_per_pixel, (ImU32*)tex_locked_rect.pBits, (int)tex_locked_rect.Pitch, width, height); |
| 386 |
bd->FontTexture->UnlockRect(0); |
| 387 |
|
| 388 |
// Store our identifier |
| 389 |
io.Fonts->SetTexID((ImTextureID)bd->FontTexture); |
| 390 |
return true; |
| 391 |
} |
| 392 |
|
| 393 |
bool ImGui_ImplDX9_CreateDeviceObjects() |
| 394 |
{ |
| 395 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 396 |
if (!bd || !bd->pd3dDevice) |
| 397 |
return false; |
| 398 |
if (!ImGui_ImplDX9_CreateFontsTexture()) |
| 399 |
return false; |
| 400 |
return true; |
| 401 |
} |
| 402 |
|
| 403 |
void ImGui_ImplDX9_InvalidateDeviceObjects() |
| 404 |
{ |
| 405 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 406 |
if (!bd || !bd->pd3dDevice) |
| 407 |
return; |
| 408 |
if (bd->pVB) { bd->pVB->Release(); bd->pVB = nullptr; } |
| 409 |
if (bd->pIB) { bd->pIB->Release(); bd->pIB = nullptr; } |
| 410 |
if (bd->FontTexture) { bd->FontTexture->Release(); bd->FontTexture = nullptr; ImGui::GetIO().Fonts->SetTexID(0); } // We copied bd->pFontTextureView to io.Fonts->TexID so let's clear that as well. |
| 411 |
} |
| 412 |
|
| 413 |
void ImGui_ImplDX9_NewFrame() |
| 414 |
{ |
| 415 |
ImGui_ImplDX9_Data* bd = ImGui_ImplDX9_GetBackendData(); |
| 416 |
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplDX9_Init()?"); |
| 417 |
|
| 418 |
if (!bd->FontTexture) |
| 419 |
ImGui_ImplDX9_CreateDeviceObjects(); |
| 420 |
} |
| 421 |
|
| 422 |
//----------------------------------------------------------------------------- |
| 423 |
|
| 424 |
#endif // #ifndef IMGUI_DISABLE |