1 // ImGui Renderer for: DirectX10
2 // This needs to be used along with a Platform Binding (e.g. Win32)
3 
4 // Implemented features:
5 //  [X] Renderer: User texture binding. Use 'ID3D10ShaderResourceView*' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
6 
7 // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
8 // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp.
9 // https://github.com/ocornut/imgui
10 
11 // CHANGELOG
12 // (minor and older changes stripped away, please see git history for details)
13 //  2018-07-13: DirectX10: Fixed unreleased resources in Init and Shutdown functions.
14 //  2018-06-08: Misc: Extracted imgui_impl_dx10.cpp/.h away from the old combined DX10+Win32 example.
15 //  2018-06-08: DirectX10: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
16 //  2018-04-09: Misc: Fixed erroneous call to io.Fonts->ClearInputData() + ClearTexData() that was left in DX10 example but removed in 1.47 (Nov 2015) on other back-ends.
17 //  2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX10_RenderDrawData() in the .h file so you can call it yourself.
18 //  2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
19 //  2016-05-07: DirectX10: Disabling depth-write.
20 
21 #include "imgui.h"
22 #include "imgui_impl_dx10.h"
23 
24 // DirectX
25 #include <stdio.h>
26 #include <d3d10_1.h>
27 #include <d3d10.h>
28 #include <d3dcompiler.h>
29 
30 // DirectX data
31 static ID3D10Device*            g_pd3dDevice = NULL;
32 static IDXGIFactory*            g_pFactory = NULL;
33 static ID3D10Buffer*            g_pVB = NULL;
34 static ID3D10Buffer*            g_pIB = NULL;
35 static ID3D10Blob*              g_pVertexShaderBlob = NULL;
36 static ID3D10VertexShader*      g_pVertexShader = NULL;
37 static ID3D10InputLayout*       g_pInputLayout = NULL;
38 static ID3D10Buffer*            g_pVertexConstantBuffer = NULL;
39 static ID3D10Blob*              g_pPixelShaderBlob = NULL;
40 static ID3D10PixelShader*       g_pPixelShader = NULL;
41 static ID3D10SamplerState*      g_pFontSampler = NULL;
42 static ID3D10ShaderResourceView*g_pFontTextureView = NULL;
43 static ID3D10RasterizerState*   g_pRasterizerState = NULL;
44 static ID3D10BlendState*        g_pBlendState = NULL;
45 static ID3D10DepthStencilState* g_pDepthStencilState = NULL;
46 static int                      g_VertexBufferSize = 5000, g_IndexBufferSize = 10000;
47 
48 struct VERTEX_CONSTANT_BUFFER
49 {
50     float   mvp[4][4];
51 };
52 
53 // Render function
54 // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
ImGui_ImplDX10_RenderDrawData(ImDrawData * draw_data)55 void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data)
56 {
57     ID3D10Device* ctx = g_pd3dDevice;
58 
59     // Create and grow vertex/index buffers if needed
60     if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
61     {
62         if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
63         g_VertexBufferSize = draw_data->TotalVtxCount + 5000;
64         D3D10_BUFFER_DESC desc;
65         memset(&desc, 0, sizeof(D3D10_BUFFER_DESC));
66         desc.Usage = D3D10_USAGE_DYNAMIC;
67         desc.ByteWidth = g_VertexBufferSize * sizeof(ImDrawVert);
68         desc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
69         desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
70         desc.MiscFlags = 0;
71         if (ctx->CreateBuffer(&desc, NULL, &g_pVB) < 0)
72             return;
73     }
74 
75     if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
76     {
77         if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
78         g_IndexBufferSize = draw_data->TotalIdxCount + 10000;
79         D3D10_BUFFER_DESC desc;
80         memset(&desc, 0, sizeof(D3D10_BUFFER_DESC));
81         desc.Usage = D3D10_USAGE_DYNAMIC;
82         desc.ByteWidth = g_IndexBufferSize * sizeof(ImDrawIdx);
83         desc.BindFlags = D3D10_BIND_INDEX_BUFFER;
84         desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
85         if (ctx->CreateBuffer(&desc, NULL, &g_pIB) < 0)
86             return;
87     }
88 
89     // Copy and convert all vertices into a single contiguous buffer
90     ImDrawVert* vtx_dst = NULL;
91     ImDrawIdx* idx_dst = NULL;
92     g_pVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&vtx_dst);
93     g_pIB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&idx_dst);
94     for (int n = 0; n < draw_data->CmdListsCount; n++)
95     {
96         const ImDrawList* cmd_list = draw_data->CmdLists[n];
97         memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
98         memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
99         vtx_dst += cmd_list->VtxBuffer.Size;
100         idx_dst += cmd_list->IdxBuffer.Size;
101     }
102     g_pVB->Unmap();
103     g_pIB->Unmap();
104 
105     // Setup orthographic projection matrix into our constant buffer
106     // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
107     {
108         void* mapped_resource;
109         if (g_pVertexConstantBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &mapped_resource) != S_OK)
110             return;
111         VERTEX_CONSTANT_BUFFER* constant_buffer = (VERTEX_CONSTANT_BUFFER*)mapped_resource;
112         float L = draw_data->DisplayPos.x;
113         float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
114         float T = draw_data->DisplayPos.y;
115         float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
116         float mvp[4][4] =
117         {
118             { 2.0f/(R-L),   0.0f,           0.0f,       0.0f },
119             { 0.0f,         2.0f/(T-B),     0.0f,       0.0f },
120             { 0.0f,         0.0f,           0.5f,       0.0f },
121             { (R+L)/(L-R),  (T+B)/(B-T),    0.5f,       1.0f },
122         };
123         memcpy(&constant_buffer->mvp, mvp, sizeof(mvp));
124         g_pVertexConstantBuffer->Unmap();
125     }
126 
127     // Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!)
128     struct BACKUP_DX10_STATE
129     {
130         UINT                        ScissorRectsCount, ViewportsCount;
131         D3D10_RECT                  ScissorRects[D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
132         D3D10_VIEWPORT              Viewports[D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
133         ID3D10RasterizerState*      RS;
134         ID3D10BlendState*           BlendState;
135         FLOAT                       BlendFactor[4];
136         UINT                        SampleMask;
137         UINT                        StencilRef;
138         ID3D10DepthStencilState*    DepthStencilState;
139         ID3D10ShaderResourceView*   PSShaderResource;
140         ID3D10SamplerState*         PSSampler;
141         ID3D10PixelShader*          PS;
142         ID3D10VertexShader*         VS;
143         D3D10_PRIMITIVE_TOPOLOGY    PrimitiveTopology;
144         ID3D10Buffer*               IndexBuffer, *VertexBuffer, *VSConstantBuffer;
145         UINT                        IndexBufferOffset, VertexBufferStride, VertexBufferOffset;
146         DXGI_FORMAT                 IndexBufferFormat;
147         ID3D10InputLayout*          InputLayout;
148     };
149     BACKUP_DX10_STATE old;
150     old.ScissorRectsCount = old.ViewportsCount = D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
151     ctx->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects);
152     ctx->RSGetViewports(&old.ViewportsCount, old.Viewports);
153     ctx->RSGetState(&old.RS);
154     ctx->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask);
155     ctx->OMGetDepthStencilState(&old.DepthStencilState, &old.StencilRef);
156     ctx->PSGetShaderResources(0, 1, &old.PSShaderResource);
157     ctx->PSGetSamplers(0, 1, &old.PSSampler);
158     ctx->PSGetShader(&old.PS);
159     ctx->VSGetShader(&old.VS);
160     ctx->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer);
161     ctx->IAGetPrimitiveTopology(&old.PrimitiveTopology);
162     ctx->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset);
163     ctx->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset);
164     ctx->IAGetInputLayout(&old.InputLayout);
165 
166     // Setup viewport
167     D3D10_VIEWPORT vp;
168     memset(&vp, 0, sizeof(D3D10_VIEWPORT));
169     vp.Width = (UINT)draw_data->DisplaySize.x;
170     vp.Height = (UINT)draw_data->DisplaySize.y;
171     vp.MinDepth = 0.0f;
172     vp.MaxDepth = 1.0f;
173     vp.TopLeftX = vp.TopLeftY = 0;
174     ctx->RSSetViewports(1, &vp);
175 
176     // Bind shader and vertex buffers
177     unsigned int stride = sizeof(ImDrawVert);
178     unsigned int offset = 0;
179     ctx->IASetInputLayout(g_pInputLayout);
180     ctx->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset);
181     ctx->IASetIndexBuffer(g_pIB, sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);
182     ctx->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
183     ctx->VSSetShader(g_pVertexShader);
184     ctx->VSSetConstantBuffers(0, 1, &g_pVertexConstantBuffer);
185     ctx->PSSetShader(g_pPixelShader);
186     ctx->PSSetSamplers(0, 1, &g_pFontSampler);
187 
188     // Setup render state
189     const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
190     ctx->OMSetBlendState(g_pBlendState, blend_factor, 0xffffffff);
191     ctx->OMSetDepthStencilState(g_pDepthStencilState, 0);
192     ctx->RSSetState(g_pRasterizerState);
193 
194     // Render command lists
195     int vtx_offset = 0;
196     int idx_offset = 0;
197     ImVec2 pos = draw_data->DisplayPos;
198     for (int n = 0; n < draw_data->CmdListsCount; n++)
199     {
200         const ImDrawList* cmd_list = draw_data->CmdLists[n];
201         for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
202         {
203             const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
204             if (pcmd->UserCallback)
205             {
206                 // User callback (registered via ImDrawList::AddCallback)
207                 pcmd->UserCallback(cmd_list, pcmd);
208             }
209             else
210             {
211                 // Apply scissor/clipping rectangle
212                 const D3D10_RECT r = { (LONG)(pcmd->ClipRect.x - pos.x), (LONG)(pcmd->ClipRect.y - pos.y), (LONG)(pcmd->ClipRect.z - pos.x), (LONG)(pcmd->ClipRect.w - pos.y)};
213                 ctx->RSSetScissorRects(1, &r);
214 
215                 // Bind texture, Draw
216                 ID3D10ShaderResourceView* texture_srv = (ID3D10ShaderResourceView*)pcmd->TextureId;
217                 ctx->PSSetShaderResources(0, 1, &texture_srv);
218                 ctx->DrawIndexed(pcmd->ElemCount, idx_offset, vtx_offset);
219             }
220             idx_offset += pcmd->ElemCount;
221         }
222         vtx_offset += cmd_list->VtxBuffer.Size;
223     }
224 
225     // Restore modified DX state
226     ctx->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects);
227     ctx->RSSetViewports(old.ViewportsCount, old.Viewports);
228     ctx->RSSetState(old.RS); if (old.RS) old.RS->Release();
229     ctx->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask); if (old.BlendState) old.BlendState->Release();
230     ctx->OMSetDepthStencilState(old.DepthStencilState, old.StencilRef); if (old.DepthStencilState) old.DepthStencilState->Release();
231     ctx->PSSetShaderResources(0, 1, &old.PSShaderResource); if (old.PSShaderResource) old.PSShaderResource->Release();
232     ctx->PSSetSamplers(0, 1, &old.PSSampler); if (old.PSSampler) old.PSSampler->Release();
233     ctx->PSSetShader(old.PS); if (old.PS) old.PS->Release();
234     ctx->VSSetShader(old.VS); if (old.VS) old.VS->Release();
235     ctx->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer); if (old.VSConstantBuffer) old.VSConstantBuffer->Release();
236     ctx->IASetPrimitiveTopology(old.PrimitiveTopology);
237     ctx->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset); if (old.IndexBuffer) old.IndexBuffer->Release();
238     ctx->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); if (old.VertexBuffer) old.VertexBuffer->Release();
239     ctx->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
240 }
241 
ImGui_ImplDX10_CreateFontsTexture()242 static void ImGui_ImplDX10_CreateFontsTexture()
243 {
244     // Build texture atlas
245     ImGuiIO& io = ImGui::GetIO();
246     unsigned char* pixels;
247     int width, height;
248     io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
249 
250     // Upload texture to graphics system
251     {
252         D3D10_TEXTURE2D_DESC desc;
253         ZeroMemory(&desc, sizeof(desc));
254         desc.Width = width;
255         desc.Height = height;
256         desc.MipLevels = 1;
257         desc.ArraySize = 1;
258         desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
259         desc.SampleDesc.Count = 1;
260         desc.Usage = D3D10_USAGE_DEFAULT;
261         desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
262         desc.CPUAccessFlags = 0;
263 
264         ID3D10Texture2D *pTexture = NULL;
265         D3D10_SUBRESOURCE_DATA subResource;
266         subResource.pSysMem = pixels;
267         subResource.SysMemPitch = desc.Width * 4;
268         subResource.SysMemSlicePitch = 0;
269         g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);
270 
271         // Create texture view
272         D3D10_SHADER_RESOURCE_VIEW_DESC srv_desc;
273         ZeroMemory(&srv_desc, sizeof(srv_desc));
274         srv_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
275         srv_desc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
276         srv_desc.Texture2D.MipLevels = desc.MipLevels;
277         srv_desc.Texture2D.MostDetailedMip = 0;
278         g_pd3dDevice->CreateShaderResourceView(pTexture, &srv_desc, &g_pFontTextureView);
279         pTexture->Release();
280     }
281 
282     // Store our identifier
283     io.Fonts->TexID = (ImTextureID)g_pFontTextureView;
284 
285     // Create texture sampler
286     {
287         D3D10_SAMPLER_DESC desc;
288         ZeroMemory(&desc, sizeof(desc));
289         desc.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR;
290         desc.AddressU = D3D10_TEXTURE_ADDRESS_WRAP;
291         desc.AddressV = D3D10_TEXTURE_ADDRESS_WRAP;
292         desc.AddressW = D3D10_TEXTURE_ADDRESS_WRAP;
293         desc.MipLODBias = 0.f;
294         desc.ComparisonFunc = D3D10_COMPARISON_ALWAYS;
295         desc.MinLOD = 0.f;
296         desc.MaxLOD = 0.f;
297         g_pd3dDevice->CreateSamplerState(&desc, &g_pFontSampler);
298     }
299 }
300 
ImGui_ImplDX10_CreateDeviceObjects()301 bool    ImGui_ImplDX10_CreateDeviceObjects()
302 {
303     if (!g_pd3dDevice)
304         return false;
305     if (g_pFontSampler)
306         ImGui_ImplDX10_InvalidateDeviceObjects();
307 
308     // By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
309     // If you would like to use this DX10 sample code but remove this dependency you can:
310     //  1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [preferred solution]
311     //  2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.
312     // See https://github.com/ocornut/imgui/pull/638 for sources and details.
313 
314     // Create the vertex shader
315     {
316         static const char* vertexShader =
317             "cbuffer vertexBuffer : register(b0) \
318             {\
319             float4x4 ProjectionMatrix; \
320             };\
321             struct VS_INPUT\
322             {\
323             float2 pos : POSITION;\
324             float4 col : COLOR0;\
325             float2 uv  : TEXCOORD0;\
326             };\
327             \
328             struct PS_INPUT\
329             {\
330             float4 pos : SV_POSITION;\
331             float4 col : COLOR0;\
332             float2 uv  : TEXCOORD0;\
333             };\
334             \
335             PS_INPUT main(VS_INPUT input)\
336             {\
337             PS_INPUT output;\
338             output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
339             output.col = input.col;\
340             output.uv  = input.uv;\
341             return output;\
342             }";
343 
344         D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_4_0", 0, 0, &g_pVertexShaderBlob, NULL);
345         if (g_pVertexShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
346             return false;
347         if (g_pd3dDevice->CreateVertexShader((DWORD*)g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pVertexShader) != S_OK)
348             return false;
349 
350         // Create the input layout
351         D3D10_INPUT_ELEMENT_DESC local_layout[] =
352         {
353             { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT,   0, (size_t)(&((ImDrawVert*)0)->pos), D3D10_INPUT_PER_VERTEX_DATA, 0 },
354             { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,   0, (size_t)(&((ImDrawVert*)0)->uv),  D3D10_INPUT_PER_VERTEX_DATA, 0 },
355             { "COLOR",    0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((ImDrawVert*)0)->col), D3D10_INPUT_PER_VERTEX_DATA, 0 },
356         };
357         if (g_pd3dDevice->CreateInputLayout(local_layout, 3, g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pInputLayout) != S_OK)
358             return false;
359 
360         // Create the constant buffer
361         {
362             D3D10_BUFFER_DESC desc;
363             desc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER);
364             desc.Usage = D3D10_USAGE_DYNAMIC;
365             desc.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
366             desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
367             desc.MiscFlags = 0;
368             g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVertexConstantBuffer);
369         }
370     }
371 
372     // Create the pixel shader
373     {
374         static const char* pixelShader =
375             "struct PS_INPUT\
376             {\
377             float4 pos : SV_POSITION;\
378             float4 col : COLOR0;\
379             float2 uv  : TEXCOORD0;\
380             };\
381             sampler sampler0;\
382             Texture2D texture0;\
383             \
384             float4 main(PS_INPUT input) : SV_Target\
385             {\
386             float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
387             return out_col; \
388             }";
389 
390         D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_4_0", 0, 0, &g_pPixelShaderBlob, NULL);
391         if (g_pPixelShaderBlob == NULL)  // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
392             return false;
393         if (g_pd3dDevice->CreatePixelShader((DWORD*)g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize(), &g_pPixelShader) != S_OK)
394             return false;
395     }
396 
397     // Create the blending setup
398     {
399         D3D10_BLEND_DESC desc;
400         ZeroMemory(&desc, sizeof(desc));
401         desc.AlphaToCoverageEnable = false;
402         desc.BlendEnable[0] = true;
403         desc.SrcBlend = D3D10_BLEND_SRC_ALPHA;
404         desc.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
405         desc.BlendOp = D3D10_BLEND_OP_ADD;
406         desc.SrcBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA;
407         desc.DestBlendAlpha = D3D10_BLEND_ZERO;
408         desc.BlendOpAlpha = D3D10_BLEND_OP_ADD;
409         desc.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;
410         g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState);
411     }
412 
413     // Create the rasterizer state
414     {
415         D3D10_RASTERIZER_DESC desc;
416         ZeroMemory(&desc, sizeof(desc));
417         desc.FillMode = D3D10_FILL_SOLID;
418         desc.CullMode = D3D10_CULL_NONE;
419         desc.ScissorEnable = true;
420         desc.DepthClipEnable = true;
421         g_pd3dDevice->CreateRasterizerState(&desc, &g_pRasterizerState);
422     }
423 
424     // Create depth-stencil State
425     {
426         D3D10_DEPTH_STENCIL_DESC desc;
427         ZeroMemory(&desc, sizeof(desc));
428         desc.DepthEnable = false;
429         desc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ALL;
430         desc.DepthFunc = D3D10_COMPARISON_ALWAYS;
431         desc.StencilEnable = false;
432         desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
433         desc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS;
434         desc.BackFace = desc.FrontFace;
435         g_pd3dDevice->CreateDepthStencilState(&desc, &g_pDepthStencilState);
436     }
437 
438     ImGui_ImplDX10_CreateFontsTexture();
439 
440     return true;
441 }
442 
ImGui_ImplDX10_InvalidateDeviceObjects()443 void    ImGui_ImplDX10_InvalidateDeviceObjects()
444 {
445     if (!g_pd3dDevice)
446         return;
447 
448     if (g_pFontSampler) { g_pFontSampler->Release(); g_pFontSampler = NULL; }
449     if (g_pFontTextureView) { g_pFontTextureView->Release(); g_pFontTextureView = NULL; ImGui::GetIO().Fonts->TexID = NULL; } // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.
450     if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
451     if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
452 
453     if (g_pBlendState) { g_pBlendState->Release(); g_pBlendState = NULL; }
454     if (g_pDepthStencilState) { g_pDepthStencilState->Release(); g_pDepthStencilState = NULL; }
455     if (g_pRasterizerState) { g_pRasterizerState->Release(); g_pRasterizerState = NULL; }
456     if (g_pPixelShader) { g_pPixelShader->Release(); g_pPixelShader = NULL; }
457     if (g_pPixelShaderBlob) { g_pPixelShaderBlob->Release(); g_pPixelShaderBlob = NULL; }
458     if (g_pVertexConstantBuffer) { g_pVertexConstantBuffer->Release(); g_pVertexConstantBuffer = NULL; }
459     if (g_pInputLayout) { g_pInputLayout->Release(); g_pInputLayout = NULL; }
460     if (g_pVertexShader) { g_pVertexShader->Release(); g_pVertexShader = NULL; }
461     if (g_pVertexShaderBlob) { g_pVertexShaderBlob->Release(); g_pVertexShaderBlob = NULL; }
462 }
463 
ImGui_ImplDX10_Init(ID3D10Device * device)464 bool    ImGui_ImplDX10_Init(ID3D10Device* device)
465 {
466     // Get factory from device
467     IDXGIDevice* pDXGIDevice = NULL;
468     IDXGIAdapter* pDXGIAdapter = NULL;
469     IDXGIFactory* pFactory = NULL;
470 
471     if (device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)) == S_OK)
472         if (pDXGIDevice->GetParent(IID_PPV_ARGS(&pDXGIAdapter)) == S_OK)
473             if (pDXGIAdapter->GetParent(IID_PPV_ARGS(&pFactory)) == S_OK)
474             {
475                 g_pd3dDevice = device;
476                 g_pFactory = pFactory;
477             }
478     if (pDXGIDevice) pDXGIDevice->Release();
479     if (pDXGIAdapter) pDXGIAdapter->Release();
480 
481     return true;
482 }
483 
ImGui_ImplDX10_Shutdown()484 void ImGui_ImplDX10_Shutdown()
485 {
486     ImGui_ImplDX10_InvalidateDeviceObjects();
487     if (g_pFactory) { g_pFactory->Release(); g_pFactory = NULL; }
488     g_pd3dDevice = NULL;
489 }
490 
ImGui_ImplDX10_NewFrame()491 void ImGui_ImplDX10_NewFrame()
492 {
493     if (!g_pFontSampler)
494         ImGui_ImplDX10_CreateDeviceObjects();
495 }
496