imgui_impl_dx11.cpp
Go to the documentation of this file.
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!
6 // [X] Renderer: Support for large meshes (64k+ vertices) with 16-bit indices.
7 
8 // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
9 // If you are new to dear imgui, read examples/README.txt and read the documentation at the top of imgui.cpp
10 // https://github.com/ocornut/imgui
11 
12 // CHANGELOG
13 // (minor and older changes stripped away, please see git history for details)
14 // 2019-08-01: DirectX11: Fixed code querying the Geometry Shader state (would generally error with Debug layer enabled).
15 // 2019-07-21: DirectX11: Backup, clear and restore Geometry Shader is any is bound when calling ImGui_ImplDX10_RenderDrawData. Clearing Hull/Domain/Compute shaders without backup/restore.
16 // 2019-05-29: DirectX11: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
17 // 2019-04-30: DirectX11: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
18 // 2018-12-03: Misc: Added #pragma comment statement to automatically link with d3dcompiler.lib when using D3DCompile().
19 // 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
20 // 2018-08-01: DirectX11: Querying for IDXGIFactory instead of IDXGIFactory1 to increase compatibility.
21 // 2018-07-13: DirectX11: Fixed unreleased resources in Init and Shutdown functions.
22 // 2018-06-08: Misc: Extracted imgui_impl_dx11.cpp/.h away from the old combined DX11+Win32 example.
23 // 2018-06-08: DirectX11: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
24 // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX11_RenderDrawData() in the .h file so you can call it yourself.
25 // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
26 // 2016-05-07: DirectX11: Disabling depth-write.
27 
28 #include "imgui.h"
29 #include "imgui_impl_dx11.h"
30 
31 // DirectX
32 #include <stdio.h>
33 #include <d3d11.h>
34 #include <d3dcompiler.h>
35 #ifdef _MSC_VER
36 #pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below.
37 #endif
38 
39 // DirectX data
40 static ID3D11Device* g_pd3dDevice = NULL;
41 static ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
42 static IDXGIFactory* g_pFactory = NULL;
43 static ID3D11Buffer* g_pVB = NULL;
44 static ID3D11Buffer* g_pIB = NULL;
45 static ID3D10Blob* g_pVertexShaderBlob = NULL;
46 static ID3D11VertexShader* g_pVertexShader = NULL;
47 static ID3D11InputLayout* g_pInputLayout = NULL;
48 static ID3D11Buffer* g_pVertexConstantBuffer = NULL;
49 static ID3D10Blob* g_pPixelShaderBlob = NULL;
50 static ID3D11PixelShader* g_pPixelShader = NULL;
51 static ID3D11SamplerState* g_pFontSampler = NULL;
52 static ID3D11ShaderResourceView*g_pFontTextureView = NULL;
53 static ID3D11RasterizerState* g_pRasterizerState = NULL;
54 static ID3D11BlendState* g_pBlendState = NULL;
55 static ID3D11DepthStencilState* g_pDepthStencilState = NULL;
56 static int g_VertexBufferSize = 5000, g_IndexBufferSize = 10000;
57 
59 {
60  float mvp[4][4];
61 };
62 
63 static void ImGui_ImplDX11_SetupRenderState(ImDrawData* draw_data, ID3D11DeviceContext* ctx)
64 {
65  // Setup viewport
66  D3D11_VIEWPORT vp;
67  memset(&vp, 0, sizeof(D3D11_VIEWPORT));
68  vp.Width = draw_data->DisplaySize.x;
69  vp.Height = draw_data->DisplaySize.y;
70  vp.MinDepth = 0.0f;
71  vp.MaxDepth = 1.0f;
72  vp.TopLeftX = vp.TopLeftY = 0;
73  ctx->RSSetViewports(1, &vp);
74 
75  // Setup shader and vertex buffers
76  unsigned int stride = sizeof(ImDrawVert);
77  unsigned int offset = 0;
78  ctx->IASetInputLayout(g_pInputLayout);
79  ctx->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset);
80  ctx->IASetIndexBuffer(g_pIB, sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);
81  ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
82  ctx->VSSetShader(g_pVertexShader, NULL, 0);
83  ctx->VSSetConstantBuffers(0, 1, &g_pVertexConstantBuffer);
84  ctx->PSSetShader(g_pPixelShader, NULL, 0);
85  ctx->PSSetSamplers(0, 1, &g_pFontSampler);
86  ctx->GSSetShader(NULL, NULL, 0);
87  ctx->HSSetShader(NULL, NULL, 0); // In theory we should backup and restore this as well.. very infrequently used..
88  ctx->DSSetShader(NULL, NULL, 0); // In theory we should backup and restore this as well.. very infrequently used..
89  ctx->CSSetShader(NULL, NULL, 0); // In theory we should backup and restore this as well.. very infrequently used..
90 
91  // Setup blend state
92  const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
93  ctx->OMSetBlendState(g_pBlendState, blend_factor, 0xffffffff);
94  ctx->OMSetDepthStencilState(g_pDepthStencilState, 0);
95  ctx->RSSetState(g_pRasterizerState);
96 }
97 
98 // Render function
99 // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
101 {
102  // Avoid rendering when minimized
103  if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
104  return;
105 
106  ID3D11DeviceContext* ctx = g_pd3dDeviceContext;
107 
108  // Create and grow vertex/index buffers if needed
109  if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
110  {
111  if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
112  g_VertexBufferSize = draw_data->TotalVtxCount + 5000;
113  D3D11_BUFFER_DESC desc;
114  memset(&desc, 0, sizeof(D3D11_BUFFER_DESC));
115  desc.Usage = D3D11_USAGE_DYNAMIC;
116  desc.ByteWidth = g_VertexBufferSize * sizeof(ImDrawVert);
117  desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
118  desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
119  desc.MiscFlags = 0;
120  if (g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVB) < 0)
121  return;
122  }
123  if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
124  {
125  if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
126  g_IndexBufferSize = draw_data->TotalIdxCount + 10000;
127  D3D11_BUFFER_DESC desc;
128  memset(&desc, 0, sizeof(D3D11_BUFFER_DESC));
129  desc.Usage = D3D11_USAGE_DYNAMIC;
130  desc.ByteWidth = g_IndexBufferSize * sizeof(ImDrawIdx);
131  desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
132  desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
133  if (g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pIB) < 0)
134  return;
135  }
136 
137  // Upload vertex/index data into a single contiguous GPU buffer
138  D3D11_MAPPED_SUBRESOURCE vtx_resource, idx_resource;
139  if (ctx->Map(g_pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &vtx_resource) != S_OK)
140  return;
141  if (ctx->Map(g_pIB, 0, D3D11_MAP_WRITE_DISCARD, 0, &idx_resource) != S_OK)
142  return;
143  ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource.pData;
144  ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource.pData;
145  for (int n = 0; n < draw_data->CmdListsCount; n++)
146  {
147  const ImDrawList* cmd_list = draw_data->CmdLists[n];
148  memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
149  memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
150  vtx_dst += cmd_list->VtxBuffer.Size;
151  idx_dst += cmd_list->IdxBuffer.Size;
152  }
153  ctx->Unmap(g_pVB, 0);
154  ctx->Unmap(g_pIB, 0);
155 
156  // Setup orthographic projection matrix into our constant buffer
157  // 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.
158  {
159  D3D11_MAPPED_SUBRESOURCE mapped_resource;
160  if (ctx->Map(g_pVertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_resource) != S_OK)
161  return;
162  VERTEX_CONSTANT_BUFFER* constant_buffer = (VERTEX_CONSTANT_BUFFER*)mapped_resource.pData;
163  float L = draw_data->DisplayPos.x;
164  float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
165  float T = draw_data->DisplayPos.y;
166  float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
167  float mvp[4][4] =
168  {
169  { 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
170  { 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
171  { 0.0f, 0.0f, 0.5f, 0.0f },
172  { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
173  };
174  memcpy(&constant_buffer->mvp, mvp, sizeof(mvp));
175  ctx->Unmap(g_pVertexConstantBuffer, 0);
176  }
177 
178  // Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!)
179  struct BACKUP_DX11_STATE
180  {
181  UINT ScissorRectsCount, ViewportsCount;
182  D3D11_RECT ScissorRects[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
183  D3D11_VIEWPORT Viewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
184  ID3D11RasterizerState* RS;
185  ID3D11BlendState* BlendState;
186  FLOAT BlendFactor[4];
187  UINT SampleMask;
188  UINT StencilRef;
189  ID3D11DepthStencilState* DepthStencilState;
190  ID3D11ShaderResourceView* PSShaderResource;
191  ID3D11SamplerState* PSSampler;
192  ID3D11PixelShader* PS;
193  ID3D11VertexShader* VS;
194  ID3D11GeometryShader* GS;
195  UINT PSInstancesCount, VSInstancesCount, GSInstancesCount;
196  ID3D11ClassInstance *PSInstances[256], *VSInstances[256], *GSInstances[256]; // 256 is max according to PSSetShader documentation
197  D3D11_PRIMITIVE_TOPOLOGY PrimitiveTopology;
198  ID3D11Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer;
199  UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset;
200  DXGI_FORMAT IndexBufferFormat;
201  ID3D11InputLayout* InputLayout;
202  };
203  BACKUP_DX11_STATE old;
204  old.ScissorRectsCount = old.ViewportsCount = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
205  ctx->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects);
206  ctx->RSGetViewports(&old.ViewportsCount, old.Viewports);
207  ctx->RSGetState(&old.RS);
208  ctx->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask);
209  ctx->OMGetDepthStencilState(&old.DepthStencilState, &old.StencilRef);
210  ctx->PSGetShaderResources(0, 1, &old.PSShaderResource);
211  ctx->PSGetSamplers(0, 1, &old.PSSampler);
212  old.PSInstancesCount = old.VSInstancesCount = old.GSInstancesCount = 256;
213  ctx->PSGetShader(&old.PS, old.PSInstances, &old.PSInstancesCount);
214  ctx->VSGetShader(&old.VS, old.VSInstances, &old.VSInstancesCount);
215  ctx->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer);
216  ctx->GSGetShader(&old.GS, old.GSInstances, &old.GSInstancesCount);
217 
218  ctx->IAGetPrimitiveTopology(&old.PrimitiveTopology);
219  ctx->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset);
220  ctx->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset);
221  ctx->IAGetInputLayout(&old.InputLayout);
222 
223  // Setup desired DX state
224  ImGui_ImplDX11_SetupRenderState(draw_data, ctx);
225 
226  // Render command lists
227  // (Because we merged all buffers into a single one, we maintain our own offset into them)
228  int global_idx_offset = 0;
229  int global_vtx_offset = 0;
230  ImVec2 clip_off = draw_data->DisplayPos;
231  for (int n = 0; n < draw_data->CmdListsCount; n++)
232  {
233  const ImDrawList* cmd_list = draw_data->CmdLists[n];
234  for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
235  {
236  const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
237  if (pcmd->UserCallback != NULL)
238  {
239  // User callback, registered via ImDrawList::AddCallback()
240  // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
242  ImGui_ImplDX11_SetupRenderState(draw_data, ctx);
243  else
244  pcmd->UserCallback(cmd_list, pcmd);
245  }
246  else
247  {
248  // Apply scissor/clipping rectangle
249  const D3D11_RECT r = { (LONG)(pcmd->ClipRect.x - clip_off.x), (LONG)(pcmd->ClipRect.y - clip_off.y), (LONG)(pcmd->ClipRect.z - clip_off.x), (LONG)(pcmd->ClipRect.w - clip_off.y) };
250  ctx->RSSetScissorRects(1, &r);
251 
252  // Bind texture, Draw
253  ID3D11ShaderResourceView* texture_srv = (ID3D11ShaderResourceView*)pcmd->TextureId;
254  ctx->PSSetShaderResources(0, 1, &texture_srv);
255  ctx->DrawIndexed(pcmd->ElemCount, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset);
256  }
257  }
258  global_idx_offset += cmd_list->IdxBuffer.Size;
259  global_vtx_offset += cmd_list->VtxBuffer.Size;
260  }
261 
262  // Restore modified DX state
263  ctx->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects);
264  ctx->RSSetViewports(old.ViewportsCount, old.Viewports);
265  ctx->RSSetState(old.RS); if (old.RS) old.RS->Release();
266  ctx->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask); if (old.BlendState) old.BlendState->Release();
267  ctx->OMSetDepthStencilState(old.DepthStencilState, old.StencilRef); if (old.DepthStencilState) old.DepthStencilState->Release();
268  ctx->PSSetShaderResources(0, 1, &old.PSShaderResource); if (old.PSShaderResource) old.PSShaderResource->Release();
269  ctx->PSSetSamplers(0, 1, &old.PSSampler); if (old.PSSampler) old.PSSampler->Release();
270  ctx->PSSetShader(old.PS, old.PSInstances, old.PSInstancesCount); if (old.PS) old.PS->Release();
271  for (UINT i = 0; i < old.PSInstancesCount; i++) if (old.PSInstances[i]) old.PSInstances[i]->Release();
272  ctx->VSSetShader(old.VS, old.VSInstances, old.VSInstancesCount); if (old.VS) old.VS->Release();
273  ctx->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer); if (old.VSConstantBuffer) old.VSConstantBuffer->Release();
274  ctx->GSSetShader(old.GS, old.GSInstances, old.GSInstancesCount); if (old.GS) old.GS->Release();
275  for (UINT i = 0; i < old.VSInstancesCount; i++) if (old.VSInstances[i]) old.VSInstances[i]->Release();
276  ctx->IASetPrimitiveTopology(old.PrimitiveTopology);
277  ctx->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset); if (old.IndexBuffer) old.IndexBuffer->Release();
278  ctx->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); if (old.VertexBuffer) old.VertexBuffer->Release();
279  ctx->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
280 }
281 
283 {
284  // Build texture atlas
285  ImGuiIO& io = ImGui::GetIO();
286  unsigned char* pixels;
287  int width, height;
289 
290  // Upload texture to graphics system
291  {
292  D3D11_TEXTURE2D_DESC desc;
293  ZeroMemory(&desc, sizeof(desc));
294  desc.Width = width;
295  desc.Height = height;
296  desc.MipLevels = 1;
297  desc.ArraySize = 1;
298  desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
299  desc.SampleDesc.Count = 1;
300  desc.Usage = D3D11_USAGE_DEFAULT;
301  desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
302  desc.CPUAccessFlags = 0;
303 
304  ID3D11Texture2D *pTexture = NULL;
305  D3D11_SUBRESOURCE_DATA subResource;
306  subResource.pSysMem = pixels;
307  subResource.SysMemPitch = desc.Width * 4;
308  subResource.SysMemSlicePitch = 0;
309  g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);
310 
311  // Create texture view
312  D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
313  ZeroMemory(&srvDesc, sizeof(srvDesc));
314  srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
315  srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
316  srvDesc.Texture2D.MipLevels = desc.MipLevels;
317  srvDesc.Texture2D.MostDetailedMip = 0;
318  g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &g_pFontTextureView);
319  pTexture->Release();
320  }
321 
322  // Store our identifier
324 
325  // Create texture sampler
326  {
327  D3D11_SAMPLER_DESC desc;
328  ZeroMemory(&desc, sizeof(desc));
329  desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
330  desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
331  desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
332  desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
333  desc.MipLODBias = 0.f;
334  desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
335  desc.MinLOD = 0.f;
336  desc.MaxLOD = 0.f;
337  g_pd3dDevice->CreateSamplerState(&desc, &g_pFontSampler);
338  }
339 }
340 
342 {
343  if (!g_pd3dDevice)
344  return false;
345  if (g_pFontSampler)
347 
348  // By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
349  // If you would like to use this DX11 sample code but remove this dependency you can:
350  // 1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [preferred solution]
351  // 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.
352  // See https://github.com/ocornut/imgui/pull/638 for sources and details.
353 
354  // Create the vertex shader
355  {
356  static const char* vertexShader =
357  "cbuffer vertexBuffer : register(b0) \
358  {\
359  float4x4 ProjectionMatrix; \
360  };\
361  struct VS_INPUT\
362  {\
363  float2 pos : POSITION;\
364  float4 col : COLOR0;\
365  float2 uv : TEXCOORD0;\
366  };\
367  \
368  struct PS_INPUT\
369  {\
370  float4 pos : SV_POSITION;\
371  float4 col : COLOR0;\
372  float2 uv : TEXCOORD0;\
373  };\
374  \
375  PS_INPUT main(VS_INPUT input)\
376  {\
377  PS_INPUT output;\
378  output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
379  output.col = input.col;\
380  output.uv = input.uv;\
381  return output;\
382  }";
383 
384  D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_4_0", 0, 0, &g_pVertexShaderBlob, NULL);
385  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!
386  return false;
387  if (g_pd3dDevice->CreateVertexShader((DWORD*)g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), NULL, &g_pVertexShader) != S_OK)
388  return false;
389 
390  // Create the input layout
391  D3D11_INPUT_ELEMENT_DESC local_layout[] =
392  {
393  { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->pos), D3D11_INPUT_PER_VERTEX_DATA, 0 },
394  { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->uv), D3D11_INPUT_PER_VERTEX_DATA, 0 },
395  { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((ImDrawVert*)0)->col), D3D11_INPUT_PER_VERTEX_DATA, 0 },
396  };
397  if (g_pd3dDevice->CreateInputLayout(local_layout, 3, g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pInputLayout) != S_OK)
398  return false;
399 
400  // Create the constant buffer
401  {
402  D3D11_BUFFER_DESC desc;
403  desc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER);
404  desc.Usage = D3D11_USAGE_DYNAMIC;
405  desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
406  desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
407  desc.MiscFlags = 0;
408  g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVertexConstantBuffer);
409  }
410  }
411 
412  // Create the pixel shader
413  {
414  static const char* pixelShader =
415  "struct PS_INPUT\
416  {\
417  float4 pos : SV_POSITION;\
418  float4 col : COLOR0;\
419  float2 uv : TEXCOORD0;\
420  };\
421  sampler sampler0;\
422  Texture2D texture0;\
423  \
424  float4 main(PS_INPUT input) : SV_Target\
425  {\
426  float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
427  return out_col; \
428  }";
429 
430  D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_4_0", 0, 0, &g_pPixelShaderBlob, NULL);
431  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!
432  return false;
433  if (g_pd3dDevice->CreatePixelShader((DWORD*)g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize(), NULL, &g_pPixelShader) != S_OK)
434  return false;
435  }
436 
437  // Create the blending setup
438  {
439  D3D11_BLEND_DESC desc;
440  ZeroMemory(&desc, sizeof(desc));
441  desc.AlphaToCoverageEnable = false;
442  desc.RenderTarget[0].BlendEnable = true;
443  desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
444  desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
445  desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
446  desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
447  desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
448  desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
449  desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
450  g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState);
451  }
452 
453  // Create the rasterizer state
454  {
455  D3D11_RASTERIZER_DESC desc;
456  ZeroMemory(&desc, sizeof(desc));
457  desc.FillMode = D3D11_FILL_SOLID;
458  desc.CullMode = D3D11_CULL_NONE;
459  desc.ScissorEnable = true;
460  desc.DepthClipEnable = true;
461  g_pd3dDevice->CreateRasterizerState(&desc, &g_pRasterizerState);
462  }
463 
464  // Create depth-stencil State
465  {
466  D3D11_DEPTH_STENCIL_DESC desc;
467  ZeroMemory(&desc, sizeof(desc));
468  desc.DepthEnable = false;
469  desc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
470  desc.DepthFunc = D3D11_COMPARISON_ALWAYS;
471  desc.StencilEnable = false;
472  desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
473  desc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
474  desc.BackFace = desc.FrontFace;
475  g_pd3dDevice->CreateDepthStencilState(&desc, &g_pDepthStencilState);
476  }
477 
479 
480  return true;
481 }
482 
484 {
485  if (!g_pd3dDevice)
486  return;
487 
488  if (g_pFontSampler) { g_pFontSampler->Release(); g_pFontSampler = NULL; }
489  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.
490  if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
491  if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
492 
493  if (g_pBlendState) { g_pBlendState->Release(); g_pBlendState = NULL; }
496  if (g_pPixelShader) { g_pPixelShader->Release(); g_pPixelShader = NULL; }
499  if (g_pInputLayout) { g_pInputLayout->Release(); g_pInputLayout = NULL; }
500  if (g_pVertexShader) { g_pVertexShader->Release(); g_pVertexShader = NULL; }
502 }
503 
504 bool ImGui_ImplDX11_Init(ID3D11Device* device, ID3D11DeviceContext* device_context)
505 {
506  // Setup back-end capabilities flags
507  ImGuiIO& io = ImGui::GetIO();
508  io.BackendRendererName = "imgui_impl_dx11";
509  io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
510 
511  // Get factory from device
512  IDXGIDevice* pDXGIDevice = NULL;
513  IDXGIAdapter* pDXGIAdapter = NULL;
514  IDXGIFactory* pFactory = NULL;
515 
516  if (device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)) == S_OK)
517  if (pDXGIDevice->GetParent(IID_PPV_ARGS(&pDXGIAdapter)) == S_OK)
518  if (pDXGIAdapter->GetParent(IID_PPV_ARGS(&pFactory)) == S_OK)
519  {
520  g_pd3dDevice = device;
521  g_pd3dDeviceContext = device_context;
522  g_pFactory = pFactory;
523  }
524  if (pDXGIDevice) pDXGIDevice->Release();
525  if (pDXGIAdapter) pDXGIAdapter->Release();
526  g_pd3dDevice->AddRef();
527  g_pd3dDeviceContext->AddRef();
528 
529  return true;
530 }
531 
533 {
535  if (g_pFactory) { g_pFactory->Release(); g_pFactory = NULL; }
536  if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
538 }
539 
541 {
542  if (!g_pFontSampler)
544 }
g_pVB
static ID3D11Buffer * g_pVB
Definition: imgui_impl_dx11.cpp:43
g_pVertexConstantBuffer
static ID3D11Buffer * g_pVertexConstantBuffer
Definition: imgui_impl_dx11.cpp:48
ImGui_ImplDX11_InvalidateDeviceObjects
void ImGui_ImplDX11_InvalidateDeviceObjects()
Definition: imgui_impl_dx11.cpp:483
ImDrawIdx
unsigned short ImDrawIdx
Definition: imgui.h:1888
height
GLint GLsizei GLsizei height
Definition: glcorearb.h:2768
ImDrawCmd::VtxOffset
unsigned int VtxOffset
Definition: imgui.h:1876
ImDrawData::DisplayPos
ImVec2 DisplayPos
Definition: imgui.h:2075
ImGuiIO::Fonts
ImFontAtlas * Fonts
Definition: imgui.h:1435
ImDrawList::IdxBuffer
ImVector< ImDrawIdx > IdxBuffer
Definition: imgui.h:1965
ImFontAtlas::TexID
ImTextureID TexID
Definition: imgui.h:2247
g_pd3dDeviceContext
static ID3D11DeviceContext * g_pd3dDeviceContext
Definition: imgui_impl_dx11.cpp:41
ImDrawData::CmdListsCount
int CmdListsCount
Definition: imgui.h:2072
NULL
NULL
Definition: test_security_zap.cpp:405
ImVec4::z
float z
Definition: imgui.h:223
g_pBlendState
static ID3D11BlendState * g_pBlendState
Definition: imgui_impl_dx11.cpp:54
ImGui_ImplDX11_NewFrame
void ImGui_ImplDX11_NewFrame()
Definition: imgui_impl_dx11.cpp:540
VERTEX_CONSTANT_BUFFER
Definition: imgui_impl_dx10.cpp:57
ImDrawData::CmdLists
ImDrawList ** CmdLists
Definition: imgui.h:2071
g_pPixelShader
static ID3D11PixelShader * g_pPixelShader
Definition: imgui_impl_dx11.cpp:50
ImGui_ImplDX11_Shutdown
void ImGui_ImplDX11_Shutdown()
Definition: imgui_impl_dx11.cpp:532
desc
#define desc
Definition: extension_set.h:342
ImDrawList
Definition: imgui.h:1961
ImTextureID
void * ImTextureID
Definition: imgui.h:172
imgui_impl_dx11.h
imgui.h
ImVec2
Definition: imgui.h:208
ImGui::GetIO
IMGUI_API ImGuiIO & GetIO()
Definition: imgui.cpp:3286
T
#define T(upbtypeconst, upbtype, ctype, default_value)
ImDrawList::CmdBuffer
ImVector< ImDrawCmd > CmdBuffer
Definition: imgui.h:1964
ImGui_ImplDX11_CreateDeviceObjects
bool ImGui_ImplDX11_CreateDeviceObjects()
Definition: imgui_impl_dx11.cpp:341
ImDrawCmd
Definition: imgui.h:1871
ImDrawCmd::TextureId
ImTextureID TextureId
Definition: imgui.h:1875
ImDrawCmd::UserCallback
ImDrawCallback UserCallback
Definition: imgui.h:1878
ImGui_ImplDX11_CreateFontsTexture
static void ImGui_ImplDX11_CreateFontsTexture()
Definition: imgui_impl_dx11.cpp:282
ImVec4::y
float y
Definition: imgui.h:223
ImVector::Data
T * Data
Definition: imgui.h:1305
g_pFontTextureView
static ID3D11ShaderResourceView * g_pFontTextureView
Definition: imgui_impl_dx11.cpp:52
stride
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:3141
offset
GLintptr offset
Definition: glcorearb.h:2944
ImDrawData
Definition: imgui.h:2068
ImVector::Size
int Size
Definition: imgui.h:1303
ImDrawVert
Definition: imgui.h:1893
ImVec2::x
float x
Definition: imgui.h:210
ImDrawCmd::ClipRect
ImVec4 ClipRect
Definition: imgui.h:1874
g_pd3dDevice
static ID3D11Device * g_pd3dDevice
Definition: imgui_impl_dx11.cpp:40
g_pDepthStencilState
static ID3D11DepthStencilState * g_pDepthStencilState
Definition: imgui_impl_dx11.cpp:55
ImGui_ImplDX11_Init
bool ImGui_ImplDX11_Init(ID3D11Device *device, ID3D11DeviceContext *device_context)
Definition: imgui_impl_dx11.cpp:504
ImDrawData::TotalVtxCount
int TotalVtxCount
Definition: imgui.h:2074
ImVec4::x
float x
Definition: imgui.h:223
ImGuiBackendFlags_RendererHasVtxOffset
@ ImGuiBackendFlags_RendererHasVtxOffset
Definition: imgui.h:1080
g_VertexBufferSize
static int g_VertexBufferSize
Definition: imgui_impl_dx11.cpp:56
g_pPixelShaderBlob
static ID3D10Blob * g_pPixelShaderBlob
Definition: imgui_impl_dx11.cpp:49
n
GLdouble n
Definition: glcorearb.h:4153
ImDrawList::VtxBuffer
ImVector< ImDrawVert > VtxBuffer
Definition: imgui.h:1966
i
int i
Definition: gmock-matchers_test.cc:764
g_IndexBufferSize
static int g_IndexBufferSize
Definition: imgui_impl_dx11.cpp:56
g_pRasterizerState
static ID3D11RasterizerState * g_pRasterizerState
Definition: imgui_impl_dx11.cpp:53
ImGui_ImplDX11_SetupRenderState
static void ImGui_ImplDX11_SetupRenderState(ImDrawData *draw_data, ID3D11DeviceContext *ctx)
Definition: imgui_impl_dx11.cpp:63
g_pInputLayout
static ID3D11InputLayout * g_pInputLayout
Definition: imgui_impl_dx11.cpp:47
g_pFontSampler
static ID3D11SamplerState * g_pFontSampler
Definition: imgui_impl_dx11.cpp:51
g_pVertexShader
static ID3D11VertexShader * g_pVertexShader
Definition: imgui_impl_dx11.cpp:46
ImVec4::w
float w
Definition: imgui.h:223
ImDrawCallback_ResetRenderState
#define ImDrawCallback_ResetRenderState
Definition: imgui.h:1866
ImGuiIO::BackendRendererName
const char * BackendRendererName
Definition: imgui.h:1456
VERTEX_CONSTANT_BUFFER::mvp
float mvp[4][4]
Definition: imgui_impl_dx10.cpp:59
r
GLboolean r
Definition: glcorearb.h:3228
ImGui_ImplDX11_RenderDrawData
void ImGui_ImplDX11_RenderDrawData(ImDrawData *draw_data)
Definition: imgui_impl_dx11.cpp:100
ImGuiIO
Definition: imgui.h:1414
ImDrawData::TotalIdxCount
int TotalIdxCount
Definition: imgui.h:2073
ImDrawData::DisplaySize
ImVec2 DisplaySize
Definition: imgui.h:2076
f
GLfloat f
Definition: glcorearb.h:3964
ImDrawCmd::IdxOffset
unsigned int IdxOffset
Definition: imgui.h:1877
pixels
GLint GLint GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: glcorearb.h:2773
g_pIB
static ID3D11Buffer * g_pIB
Definition: imgui_impl_dx11.cpp:44
ImGuiIO::BackendFlags
ImGuiBackendFlags BackendFlags
Definition: imgui.h:1421
g_pVertexShaderBlob
static ID3D10Blob * g_pVertexShaderBlob
Definition: imgui_impl_dx11.cpp:45
g_pFactory
static IDXGIFactory * g_pFactory
Definition: imgui_impl_dx11.cpp:42
ImVec2::y
float y
Definition: imgui.h:210
width
GLint GLsizei width
Definition: glcorearb.h:2768
ImDrawCmd::ElemCount
unsigned int ElemCount
Definition: imgui.h:1873
ImFontAtlas::GetTexDataAsRGBA32
IMGUI_API void GetTexDataAsRGBA32(unsigned char **out_pixels, int *out_width, int *out_height, int *out_bytes_per_pixel=NULL)
Definition: imgui_draw.cpp:1679


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:54