1 // dear imgui: Renderer for DirectX11
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 'ID3D11ShaderResourceView*' 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-08-01: DirectX11: Querying for IDXGIFactory instead of IDXGIFactory1 to increase compatibility.
14 // 2018-07-13: DirectX11: Fixed unreleased resources in Init and Shutdown functions.
15 // 2018-06-08: Misc: Extracted imgui_impl_dx11.cpp/.h away from the old combined DX11+Win32 example.
16 // 2018-06-08: DirectX11: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
17 // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX11_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: DirectX11: Disabling depth-write.
20
21 #include "imgui.h"
22 #include "imgui_impl_dx11.h"
23
24 // DirectX
25 #include <stdio.h>
26 #include <d3d11.h>
27 #include <d3dcompiler.h>
28
29 // DirectX data
30 static ID3D11Device* g_pd3dDevice = NULL;
31 static ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
32 static IDXGIFactory* g_pFactory = NULL;
33 static ID3D11Buffer* g_pVB = NULL;
34 static ID3D11Buffer* g_pIB = NULL;
35 static ID3D10Blob* g_pVertexShaderBlob = NULL;
36 static ID3D11VertexShader* g_pVertexShader = NULL;
37 static ID3D11InputLayout* g_pInputLayout = NULL;
38 static ID3D11Buffer* g_pVertexConstantBuffer = NULL;
39 static ID3D10Blob* g_pPixelShaderBlob = NULL;
40 static ID3D11PixelShader* g_pPixelShader = NULL;
41 static ID3D11SamplerState* g_pFontSampler = NULL;
42 static ID3D11ShaderResourceView*g_pFontTextureView = NULL;
43 static ID3D11RasterizerState* g_pRasterizerState = NULL;
44 static ID3D11BlendState* g_pBlendState = NULL;
45 static ID3D11DepthStencilState* 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_ImplDX11_RenderDrawData(ImDrawData * draw_data)55 void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
56 {
57 ID3D11DeviceContext* ctx = g_pd3dDeviceContext;
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 D3D11_BUFFER_DESC desc;
65 memset(&desc, 0, sizeof(D3D11_BUFFER_DESC));
66 desc.Usage = D3D11_USAGE_DYNAMIC;
67 desc.ByteWidth = g_VertexBufferSize * sizeof(ImDrawVert);
68 desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
69 desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
70 desc.MiscFlags = 0;
71 if (g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVB) < 0)
72 return;
73 }
74 if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
75 {
76 if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
77 g_IndexBufferSize = draw_data->TotalIdxCount + 10000;
78 D3D11_BUFFER_DESC desc;
79 memset(&desc, 0, sizeof(D3D11_BUFFER_DESC));
80 desc.Usage = D3D11_USAGE_DYNAMIC;
81 desc.ByteWidth = g_IndexBufferSize * sizeof(ImDrawIdx);
82 desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
83 desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
84 if (g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pIB) < 0)
85 return;
86 }
87
88 // Copy and convert all vertices into a single contiguous buffer
89 D3D11_MAPPED_SUBRESOURCE vtx_resource, idx_resource;
90 if (ctx->Map(g_pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &vtx_resource) != S_OK)
91 return;
92 if (ctx->Map(g_pIB, 0, D3D11_MAP_WRITE_DISCARD, 0, &idx_resource) != S_OK)
93 return;
94 ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource.pData;
95 ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource.pData;
96 for (int n = 0; n < draw_data->CmdListsCount; n++)
97 {
98 const ImDrawList* cmd_list = draw_data->CmdLists[n];
99 memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
100 memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
101 vtx_dst += cmd_list->VtxBuffer.Size;
102 idx_dst += cmd_list->IdxBuffer.Size;
103 }
104 ctx->Unmap(g_pVB, 0);
105 ctx->Unmap(g_pIB, 0);
106
107 // Setup orthographic projection matrix into our constant buffer
108 // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right).
109 {
110 D3D11_MAPPED_SUBRESOURCE mapped_resource;
111 if (ctx->Map(g_pVertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_resource) != S_OK)
112 return;
113 VERTEX_CONSTANT_BUFFER* constant_buffer = (VERTEX_CONSTANT_BUFFER*)mapped_resource.pData;
114 float L = draw_data->DisplayPos.x;
115 float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
116 float T = draw_data->DisplayPos.y;
117 float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
118 float mvp[4][4] =
119 {
120 { 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
121 { 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
122 { 0.0f, 0.0f, 0.5f, 0.0f },
123 { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
124 };
125 memcpy(&constant_buffer->mvp, mvp, sizeof(mvp));
126 ctx->Unmap(g_pVertexConstantBuffer, 0);
127 }
128
129 // Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!)
130 struct BACKUP_DX11_STATE
131 {
132 UINT ScissorRectsCount, ViewportsCount;
133 D3D11_RECT ScissorRects[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
134 D3D11_VIEWPORT Viewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
135 ID3D11RasterizerState* RS;
136 ID3D11BlendState* BlendState;
137 FLOAT BlendFactor[4];
138 UINT SampleMask;
139 UINT StencilRef;
140 ID3D11DepthStencilState* DepthStencilState;
141 ID3D11ShaderResourceView* PSShaderResource;
142 ID3D11SamplerState* PSSampler;
143 ID3D11PixelShader* PS;
144 ID3D11VertexShader* VS;
145 UINT PSInstancesCount, VSInstancesCount;
146 ID3D11ClassInstance* PSInstances[256], *VSInstances[256]; // 256 is max according to PSSetShader documentation
147 D3D11_PRIMITIVE_TOPOLOGY PrimitiveTopology;
148 ID3D11Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer;
149 UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset;
150 DXGI_FORMAT IndexBufferFormat;
151 ID3D11InputLayout* InputLayout;
152 };
153 BACKUP_DX11_STATE old;
154 old.ScissorRectsCount = old.ViewportsCount = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
155 ctx->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects);
156 ctx->RSGetViewports(&old.ViewportsCount, old.Viewports);
157 ctx->RSGetState(&old.RS);
158 ctx->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask);
159 ctx->OMGetDepthStencilState(&old.DepthStencilState, &old.StencilRef);
160 ctx->PSGetShaderResources(0, 1, &old.PSShaderResource);
161 ctx->PSGetSamplers(0, 1, &old.PSSampler);
162 old.PSInstancesCount = old.VSInstancesCount = 256;
163 ctx->PSGetShader(&old.PS, old.PSInstances, &old.PSInstancesCount);
164 ctx->VSGetShader(&old.VS, old.VSInstances, &old.VSInstancesCount);
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 D3D11_VIEWPORT vp;
173 memset(&vp, 0, sizeof(D3D11_VIEWPORT));
174 vp.Width = draw_data->DisplaySize.x;
175 vp.Height = 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(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
188 ctx->VSSetShader(g_pVertexShader, NULL, 0);
189 ctx->VSSetConstantBuffers(0, 1, &g_pVertexConstantBuffer);
190 ctx->PSSetShader(g_pPixelShader, NULL, 0);
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 D3D11_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 ID3D11ShaderResourceView* texture_srv = (ID3D11ShaderResourceView*)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, old.PSInstances, old.PSInstancesCount); if (old.PS) old.PS->Release();
239 for (UINT i = 0; i < old.PSInstancesCount; i++) if (old.PSInstances[i]) old.PSInstances[i]->Release();
240 ctx->VSSetShader(old.VS, old.VSInstances, old.VSInstancesCount); if (old.VS) old.VS->Release();
241 ctx->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer); if (old.VSConstantBuffer) old.VSConstantBuffer->Release();
242 for (UINT i = 0; i < old.VSInstancesCount; i++) if (old.VSInstances[i]) old.VSInstances[i]->Release();
243 ctx->IASetPrimitiveTopology(old.PrimitiveTopology);
244 ctx->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset); if (old.IndexBuffer) old.IndexBuffer->Release();
245 ctx->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); if (old.VertexBuffer) old.VertexBuffer->Release();
246 ctx->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
247 }
248
ImGui_ImplDX11_CreateFontsTexture()249 static void ImGui_ImplDX11_CreateFontsTexture()
250 {
251 // Build texture atlas
252 ImGuiIO& io = ImGui::GetIO();
253 unsigned char* pixels;
254 int width, height;
255 io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
256
257 // Upload texture to graphics system
258 {
259 D3D11_TEXTURE2D_DESC desc;
260 ZeroMemory(&desc, sizeof(desc));
261 desc.Width = width;
262 desc.Height = height;
263 desc.MipLevels = 1;
264 desc.ArraySize = 1;
265 desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
266 desc.SampleDesc.Count = 1;
267 desc.Usage = D3D11_USAGE_DEFAULT;
268 desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
269 desc.CPUAccessFlags = 0;
270
271 ID3D11Texture2D *pTexture = NULL;
272 D3D11_SUBRESOURCE_DATA subResource;
273 subResource.pSysMem = pixels;
274 subResource.SysMemPitch = desc.Width * 4;
275 subResource.SysMemSlicePitch = 0;
276 g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);
277
278 // Create texture view
279 D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
280 ZeroMemory(&srvDesc, sizeof(srvDesc));
281 srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
282 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
283 srvDesc.Texture2D.MipLevels = desc.MipLevels;
284 srvDesc.Texture2D.MostDetailedMip = 0;
285 g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &g_pFontTextureView);
286 pTexture->Release();
287 }
288
289 // Store our identifier
290 io.Fonts->TexID = (ImTextureID)g_pFontTextureView;
291
292 // Create texture sampler
293 {
294 D3D11_SAMPLER_DESC desc;
295 ZeroMemory(&desc, sizeof(desc));
296 desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
297 desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
298 desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
299 desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
300 desc.MipLODBias = 0.f;
301 desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
302 desc.MinLOD = 0.f;
303 desc.MaxLOD = 0.f;
304 g_pd3dDevice->CreateSamplerState(&desc, &g_pFontSampler);
305 }
306 }
307
ImGui_ImplDX11_CreateDeviceObjects()308 bool ImGui_ImplDX11_CreateDeviceObjects()
309 {
310 if (!g_pd3dDevice)
311 return false;
312 if (g_pFontSampler)
313 ImGui_ImplDX11_InvalidateDeviceObjects();
314
315 // By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
316 // If you would like to use this DX11 sample code but remove this dependency you can:
317 // 1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [preferred solution]
318 // 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.
319 // See https://github.com/ocornut/imgui/pull/638 for sources and details.
320
321 // Create the vertex shader
322 {
323 static const char* vertexShader =
324 "cbuffer vertexBuffer : register(b0) \
325 {\
326 float4x4 ProjectionMatrix; \
327 };\
328 struct VS_INPUT\
329 {\
330 float2 pos : POSITION;\
331 float4 col : COLOR0;\
332 float2 uv : TEXCOORD0;\
333 };\
334 \
335 struct PS_INPUT\
336 {\
337 float4 pos : SV_POSITION;\
338 float4 col : COLOR0;\
339 float2 uv : TEXCOORD0;\
340 };\
341 \
342 PS_INPUT main(VS_INPUT input)\
343 {\
344 PS_INPUT output;\
345 output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
346 output.col = input.col;\
347 output.uv = input.uv;\
348 return output;\
349 }";
350
351 D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_4_0", 0, 0, &g_pVertexShaderBlob, NULL);
352 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!
353 return false;
354 if (g_pd3dDevice->CreateVertexShader((DWORD*)g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), NULL, &g_pVertexShader) != S_OK)
355 return false;
356
357 // Create the input layout
358 D3D11_INPUT_ELEMENT_DESC local_layout[] =
359 {
360 { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->pos), D3D11_INPUT_PER_VERTEX_DATA, 0 },
361 { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->uv), D3D11_INPUT_PER_VERTEX_DATA, 0 },
362 { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((ImDrawVert*)0)->col), D3D11_INPUT_PER_VERTEX_DATA, 0 },
363 };
364 if (g_pd3dDevice->CreateInputLayout(local_layout, 3, g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pInputLayout) != S_OK)
365 return false;
366
367 // Create the constant buffer
368 {
369 D3D11_BUFFER_DESC desc;
370 desc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER);
371 desc.Usage = D3D11_USAGE_DYNAMIC;
372 desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
373 desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
374 desc.MiscFlags = 0;
375 g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVertexConstantBuffer);
376 }
377 }
378
379 // Create the pixel shader
380 {
381 static const char* pixelShader =
382 "struct PS_INPUT\
383 {\
384 float4 pos : SV_POSITION;\
385 float4 col : COLOR0;\
386 float2 uv : TEXCOORD0;\
387 };\
388 sampler sampler0;\
389 Texture2D texture0;\
390 \
391 float4 main(PS_INPUT input) : SV_Target\
392 {\
393 float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
394 return out_col; \
395 }";
396
397 D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_4_0", 0, 0, &g_pPixelShaderBlob, NULL);
398 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!
399 return false;
400 if (g_pd3dDevice->CreatePixelShader((DWORD*)g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize(), NULL, &g_pPixelShader) != S_OK)
401 return false;
402 }
403
404 // Create the blending setup
405 {
406 D3D11_BLEND_DESC desc;
407 ZeroMemory(&desc, sizeof(desc));
408 desc.AlphaToCoverageEnable = false;
409 desc.RenderTarget[0].BlendEnable = true;
410 desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
411 desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
412 desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
413 desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
414 desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
415 desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
416 desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
417 g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState);
418 }
419
420 // Create the rasterizer state
421 {
422 D3D11_RASTERIZER_DESC desc;
423 ZeroMemory(&desc, sizeof(desc));
424 desc.FillMode = D3D11_FILL_SOLID;
425 desc.CullMode = D3D11_CULL_NONE;
426 desc.ScissorEnable = true;
427 desc.DepthClipEnable = true;
428 g_pd3dDevice->CreateRasterizerState(&desc, &g_pRasterizerState);
429 }
430
431 // Create depth-stencil State
432 {
433 D3D11_DEPTH_STENCIL_DESC desc;
434 ZeroMemory(&desc, sizeof(desc));
435 desc.DepthEnable = false;
436 desc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
437 desc.DepthFunc = D3D11_COMPARISON_ALWAYS;
438 desc.StencilEnable = false;
439 desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
440 desc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
441 desc.BackFace = desc.FrontFace;
442 g_pd3dDevice->CreateDepthStencilState(&desc, &g_pDepthStencilState);
443 }
444
445 ImGui_ImplDX11_CreateFontsTexture();
446
447 return true;
448 }
449
ImGui_ImplDX11_InvalidateDeviceObjects()450 void ImGui_ImplDX11_InvalidateDeviceObjects()
451 {
452 if (!g_pd3dDevice)
453 return;
454
455 if (g_pFontSampler) { g_pFontSampler->Release(); g_pFontSampler = NULL; }
456 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.
457 if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
458 if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
459
460 if (g_pBlendState) { g_pBlendState->Release(); g_pBlendState = NULL; }
461 if (g_pDepthStencilState) { g_pDepthStencilState->Release(); g_pDepthStencilState = NULL; }
462 if (g_pRasterizerState) { g_pRasterizerState->Release(); g_pRasterizerState = NULL; }
463 if (g_pPixelShader) { g_pPixelShader->Release(); g_pPixelShader = NULL; }
464 if (g_pPixelShaderBlob) { g_pPixelShaderBlob->Release(); g_pPixelShaderBlob = NULL; }
465 if (g_pVertexConstantBuffer) { g_pVertexConstantBuffer->Release(); g_pVertexConstantBuffer = NULL; }
466 if (g_pInputLayout) { g_pInputLayout->Release(); g_pInputLayout = NULL; }
467 if (g_pVertexShader) { g_pVertexShader->Release(); g_pVertexShader = NULL; }
468 if (g_pVertexShaderBlob) { g_pVertexShaderBlob->Release(); g_pVertexShaderBlob = NULL; }
469 }
470
ImGui_ImplDX11_Init(ID3D11Device * device,ID3D11DeviceContext * device_context)471 bool ImGui_ImplDX11_Init(ID3D11Device* device, ID3D11DeviceContext* device_context)
472 {
473 // Get factory from device
474 IDXGIDevice* pDXGIDevice = NULL;
475 IDXGIAdapter* pDXGIAdapter = NULL;
476 IDXGIFactory* pFactory = NULL;
477
478 if (device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)) == S_OK)
479 if (pDXGIDevice->GetParent(IID_PPV_ARGS(&pDXGIAdapter)) == S_OK)
480 if (pDXGIAdapter->GetParent(IID_PPV_ARGS(&pFactory)) == S_OK)
481 {
482 g_pd3dDevice = device;
483 g_pd3dDeviceContext = device_context;
484 g_pFactory = pFactory;
485 }
486 if (pDXGIDevice) pDXGIDevice->Release();
487 if (pDXGIAdapter) pDXGIAdapter->Release();
488
489 return true;
490 }
491
ImGui_ImplDX11_Shutdown()492 void ImGui_ImplDX11_Shutdown()
493 {
494 ImGui_ImplDX11_InvalidateDeviceObjects();
495 if (g_pFactory) { g_pFactory->Release(); g_pFactory = NULL; }
496 g_pd3dDevice = NULL;
497 g_pd3dDeviceContext = NULL;
498 }
499
ImGui_ImplDX11_NewFrame()500 void ImGui_ImplDX11_NewFrame()
501 {
502 if (!g_pFontSampler)
503 ImGui_ImplDX11_CreateDeviceObjects();
504 }
505