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