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