imgui_impl_dx11.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. // ImGui Win32 + DirectX11 binding
  2. // You can copy and use unmodified imgui_impl_* files in your project.
  3. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  4. // See main.cpp for an example of using this.
  5. // https://github.com/ocornut/imgui
  6. #include <imgui.h>
  7. #include "imgui_impl_dx11.h"
  8. // DirectX
  9. #include <d3d11.h>
  10. #include <d3dcompiler.h>
  11. #define DIRECTINPUT_VERSION 0x0800
  12. #include <dinput.h>
  13. // Data
  14. static INT64 g_Time = 0;
  15. static INT64 g_TicksPerSecond = 0;
  16. static HWND g_hWnd = 0;
  17. static ID3D11Device* g_pd3dDevice = NULL;
  18. static ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
  19. static ID3D11Buffer* g_pVB = NULL;
  20. static ID3D11Buffer* g_pIB = NULL;
  21. static ID3D10Blob * g_pVertexShaderBlob = NULL;
  22. static ID3D11VertexShader* g_pVertexShader = NULL;
  23. static ID3D11InputLayout* g_pInputLayout = NULL;
  24. static ID3D11Buffer* g_pVertexConstantBuffer = NULL;
  25. static ID3D10Blob * g_pPixelShaderBlob = NULL;
  26. static ID3D11PixelShader* g_pPixelShader = NULL;
  27. static ID3D11SamplerState* g_pFontSampler = NULL;
  28. static ID3D11ShaderResourceView*g_pFontTextureView = NULL;
  29. static ID3D11RasterizerState* g_pRasterizerState = NULL;
  30. static ID3D11BlendState* g_pBlendState = NULL;
  31. static int g_VertexBufferSize = 5000, g_IndexBufferSize = 10000;
  32. struct VERTEX_CONSTANT_BUFFER
  33. {
  34. float mvp[4][4];
  35. };
  36. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  37. // If text or lines are blurry when integrating ImGui in your engine:
  38. // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
  39. void ImGui_ImplDX11_RenderDrawLists(ImDrawData* draw_data)
  40. {
  41. // Create and grow vertex/index buffers if needed
  42. if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
  43. {
  44. if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
  45. g_VertexBufferSize = draw_data->TotalVtxCount + 5000;
  46. D3D11_BUFFER_DESC desc;
  47. memset(&desc, 0, sizeof(D3D11_BUFFER_DESC));
  48. desc.Usage = D3D11_USAGE_DYNAMIC;
  49. desc.ByteWidth = g_VertexBufferSize * sizeof(ImDrawVert);
  50. desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  51. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  52. desc.MiscFlags = 0;
  53. if (g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVB) < 0)
  54. return;
  55. }
  56. if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
  57. {
  58. if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
  59. g_IndexBufferSize = draw_data->TotalIdxCount + 10000;
  60. D3D11_BUFFER_DESC bufferDesc;
  61. memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC));
  62. bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
  63. bufferDesc.ByteWidth = g_IndexBufferSize * sizeof(ImDrawIdx);
  64. bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  65. bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  66. if (g_pd3dDevice->CreateBuffer(&bufferDesc, NULL, &g_pIB) < 0)
  67. return;
  68. }
  69. // Copy and convert all vertices into a single contiguous buffer
  70. D3D11_MAPPED_SUBRESOURCE vtx_resource, idx_resource;
  71. if (g_pd3dDeviceContext->Map(g_pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &vtx_resource) != S_OK)
  72. return;
  73. if (g_pd3dDeviceContext->Map(g_pIB, 0, D3D11_MAP_WRITE_DISCARD, 0, &idx_resource) != S_OK)
  74. return;
  75. ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource.pData;
  76. ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource.pData;
  77. for (int n = 0; n < draw_data->CmdListsCount; n++)
  78. {
  79. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  80. memcpy(vtx_dst, &cmd_list->VtxBuffer[0], cmd_list->VtxBuffer.size() * sizeof(ImDrawVert));
  81. memcpy(idx_dst, &cmd_list->IdxBuffer[0], cmd_list->IdxBuffer.size() * sizeof(ImDrawIdx));
  82. vtx_dst += cmd_list->VtxBuffer.size();
  83. idx_dst += cmd_list->IdxBuffer.size();
  84. }
  85. g_pd3dDeviceContext->Unmap(g_pVB, 0);
  86. g_pd3dDeviceContext->Unmap(g_pIB, 0);
  87. // Setup orthographic projection matrix into our constant buffer
  88. {
  89. D3D11_MAPPED_SUBRESOURCE mappedResource;
  90. if (g_pd3dDeviceContext->Map(g_pVertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource) != S_OK)
  91. return;
  92. VERTEX_CONSTANT_BUFFER* pConstantBuffer = (VERTEX_CONSTANT_BUFFER*)mappedResource.pData;
  93. const float L = 0.0f;
  94. const float R = ImGui::GetIO().DisplaySize.x;
  95. const float B = ImGui::GetIO().DisplaySize.y;
  96. const float T = 0.0f;
  97. const float mvp[4][4] =
  98. {
  99. { 2.0f/(R-L), 0.0f, 0.0f, 0.0f},
  100. { 0.0f, 2.0f/(T-B), 0.0f, 0.0f,},
  101. { 0.0f, 0.0f, 0.5f, 0.0f },
  102. { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
  103. };
  104. memcpy(&pConstantBuffer->mvp, mvp, sizeof(mvp));
  105. g_pd3dDeviceContext->Unmap(g_pVertexConstantBuffer, 0);
  106. }
  107. // Setup viewport
  108. {
  109. D3D11_VIEWPORT vp;
  110. memset(&vp, 0, sizeof(D3D11_VIEWPORT));
  111. vp.Width = ImGui::GetIO().DisplaySize.x;
  112. vp.Height = ImGui::GetIO().DisplaySize.y;
  113. vp.MinDepth = 0.0f;
  114. vp.MaxDepth = 1.0f;
  115. vp.TopLeftX = 0;
  116. vp.TopLeftY = 0;
  117. g_pd3dDeviceContext->RSSetViewports(1, &vp);
  118. }
  119. // Bind shader and vertex buffers
  120. unsigned int stride = sizeof(ImDrawVert);
  121. unsigned int offset = 0;
  122. g_pd3dDeviceContext->IASetInputLayout(g_pInputLayout);
  123. g_pd3dDeviceContext->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset);
  124. g_pd3dDeviceContext->IASetIndexBuffer(g_pIB, sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);
  125. g_pd3dDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  126. g_pd3dDeviceContext->VSSetShader(g_pVertexShader, NULL, 0);
  127. g_pd3dDeviceContext->VSSetConstantBuffers(0, 1, &g_pVertexConstantBuffer);
  128. g_pd3dDeviceContext->PSSetShader(g_pPixelShader, NULL, 0);
  129. g_pd3dDeviceContext->PSSetSamplers(0, 1, &g_pFontSampler);
  130. // Setup render state
  131. const float blendFactor[4] = { 0.f, 0.f, 0.f, 0.f };
  132. g_pd3dDeviceContext->OMSetBlendState(g_pBlendState, blendFactor, 0xffffffff);
  133. g_pd3dDeviceContext->RSSetState(g_pRasterizerState);
  134. // Render command lists
  135. int vtx_offset = 0;
  136. int idx_offset = 0;
  137. for (int n = 0; n < draw_data->CmdListsCount; n++)
  138. {
  139. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  140. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++)
  141. {
  142. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  143. if (pcmd->UserCallback)
  144. {
  145. pcmd->UserCallback(cmd_list, pcmd);
  146. }
  147. else
  148. {
  149. const D3D11_RECT r = { (LONG)pcmd->ClipRect.x, (LONG)pcmd->ClipRect.y, (LONG)pcmd->ClipRect.z, (LONG)pcmd->ClipRect.w };
  150. g_pd3dDeviceContext->PSSetShaderResources(0, 1, (ID3D11ShaderResourceView**)&pcmd->TextureId);
  151. g_pd3dDeviceContext->RSSetScissorRects(1, &r);
  152. g_pd3dDeviceContext->DrawIndexed(pcmd->ElemCount, idx_offset, vtx_offset);
  153. }
  154. idx_offset += pcmd->ElemCount;
  155. }
  156. vtx_offset += cmd_list->VtxBuffer.size();
  157. }
  158. // Restore modified state
  159. g_pd3dDeviceContext->IASetInputLayout(NULL);
  160. g_pd3dDeviceContext->PSSetShader(NULL, NULL, 0);
  161. g_pd3dDeviceContext->VSSetShader(NULL, NULL, 0);
  162. }
  163. IMGUI_API LRESULT ImGui_ImplDX11_WndProcHandler(HWND, UINT msg, WPARAM wParam, LPARAM lParam)
  164. {
  165. ImGuiIO& io = ImGui::GetIO();
  166. switch (msg)
  167. {
  168. case WM_LBUTTONDOWN:
  169. io.MouseDown[0] = true;
  170. return true;
  171. case WM_LBUTTONUP:
  172. io.MouseDown[0] = false;
  173. return true;
  174. case WM_RBUTTONDOWN:
  175. io.MouseDown[1] = true;
  176. return true;
  177. case WM_RBUTTONUP:
  178. io.MouseDown[1] = false;
  179. return true;
  180. case WM_MBUTTONDOWN:
  181. io.MouseDown[2] = true;
  182. return true;
  183. case WM_MBUTTONUP:
  184. io.MouseDown[2] = false;
  185. return true;
  186. case WM_MOUSEWHEEL:
  187. io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  188. return true;
  189. case WM_MOUSEMOVE:
  190. io.MousePos.x = (signed short)(lParam);
  191. io.MousePos.y = (signed short)(lParam >> 16);
  192. return true;
  193. case WM_KEYDOWN:
  194. if (wParam < 256)
  195. io.KeysDown[wParam] = 1;
  196. return true;
  197. case WM_KEYUP:
  198. if (wParam < 256)
  199. io.KeysDown[wParam] = 0;
  200. return true;
  201. case WM_CHAR:
  202. // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
  203. if (wParam > 0 && wParam < 0x10000)
  204. io.AddInputCharacter((unsigned short)wParam);
  205. return true;
  206. }
  207. return 0;
  208. }
  209. static void ImGui_ImplDX11_CreateFontsTexture()
  210. {
  211. ImGuiIO& io = ImGui::GetIO();
  212. // Build
  213. unsigned char* pixels;
  214. int width, height;
  215. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  216. // Create DX11 texture
  217. {
  218. D3D11_TEXTURE2D_DESC texDesc;
  219. ZeroMemory(&texDesc, sizeof(texDesc));
  220. texDesc.Width = width;
  221. texDesc.Height = height;
  222. texDesc.MipLevels = 1;
  223. texDesc.ArraySize = 1;
  224. texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  225. texDesc.SampleDesc.Count = 1;
  226. texDesc.Usage = D3D11_USAGE_DEFAULT;
  227. texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  228. texDesc.CPUAccessFlags = 0;
  229. ID3D11Texture2D *pTexture = NULL;
  230. D3D11_SUBRESOURCE_DATA subResource;
  231. subResource.pSysMem = pixels;
  232. subResource.SysMemPitch = texDesc.Width * 4;
  233. subResource.SysMemSlicePitch = 0;
  234. g_pd3dDevice->CreateTexture2D(&texDesc, &subResource, &pTexture);
  235. // Create texture view
  236. D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
  237. ZeroMemory(&srvDesc, sizeof(srvDesc));
  238. srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  239. srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  240. srvDesc.Texture2D.MipLevels = texDesc.MipLevels;
  241. srvDesc.Texture2D.MostDetailedMip = 0;
  242. g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &g_pFontTextureView);
  243. pTexture->Release();
  244. }
  245. // Store our identifier
  246. io.Fonts->TexID = (void *)g_pFontTextureView;
  247. // Create texture sampler
  248. {
  249. D3D11_SAMPLER_DESC samplerDesc;
  250. ZeroMemory(&samplerDesc, sizeof(samplerDesc));
  251. samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  252. samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  253. samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
  254. samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  255. samplerDesc.MipLODBias = 0.f;
  256. samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
  257. samplerDesc.MinLOD = 0.f;
  258. samplerDesc.MaxLOD = 0.f;
  259. g_pd3dDevice->CreateSamplerState(&samplerDesc, &g_pFontSampler);
  260. }
  261. // Cleanup (don't clear the input data if you want to append new fonts later)
  262. io.Fonts->ClearInputData();
  263. io.Fonts->ClearTexData();
  264. }
  265. bool ImGui_ImplDX11_CreateDeviceObjects()
  266. {
  267. if (!g_pd3dDevice)
  268. return false;
  269. if (g_pFontSampler)
  270. ImGui_ImplDX11_InvalidateDeviceObjects();
  271. // Create the vertex shader
  272. {
  273. static const char* vertexShader =
  274. "cbuffer vertexBuffer : register(b0) \
  275. {\
  276. float4x4 ProjectionMatrix; \
  277. };\
  278. struct VS_INPUT\
  279. {\
  280. float2 pos : POSITION;\
  281. float4 col : COLOR0;\
  282. float2 uv : TEXCOORD0;\
  283. };\
  284. \
  285. struct PS_INPUT\
  286. {\
  287. float4 pos : SV_POSITION;\
  288. float4 col : COLOR0;\
  289. float2 uv : TEXCOORD0;\
  290. };\
  291. \
  292. PS_INPUT main(VS_INPUT input)\
  293. {\
  294. PS_INPUT output;\
  295. output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
  296. output.col = input.col;\
  297. output.uv = input.uv;\
  298. return output;\
  299. }";
  300. D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_4_0", 0, 0, &g_pVertexShaderBlob, NULL);
  301. 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!
  302. return false;
  303. if (g_pd3dDevice->CreateVertexShader((DWORD*)g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), NULL, &g_pVertexShader) != S_OK)
  304. return false;
  305. // Create the input layout
  306. D3D11_INPUT_ELEMENT_DESC localLayout[] = {
  307. { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->pos), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  308. { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->uv), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  309. { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((ImDrawVert*)0)->col), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  310. };
  311. if (g_pd3dDevice->CreateInputLayout(localLayout, 3, g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pInputLayout) != S_OK)
  312. return false;
  313. // Create the constant buffer
  314. {
  315. D3D11_BUFFER_DESC cbDesc;
  316. cbDesc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER);
  317. cbDesc.Usage = D3D11_USAGE_DYNAMIC;
  318. cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  319. cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  320. cbDesc.MiscFlags = 0;
  321. g_pd3dDevice->CreateBuffer(&cbDesc, NULL, &g_pVertexConstantBuffer);
  322. }
  323. }
  324. // Create the pixel shader
  325. {
  326. static const char* pixelShader =
  327. "struct PS_INPUT\
  328. {\
  329. float4 pos : SV_POSITION;\
  330. float4 col : COLOR0;\
  331. float2 uv : TEXCOORD0;\
  332. };\
  333. sampler sampler0;\
  334. Texture2D texture0;\
  335. \
  336. float4 main(PS_INPUT input) : SV_Target\
  337. {\
  338. float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
  339. return out_col; \
  340. }";
  341. D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_4_0", 0, 0, &g_pPixelShaderBlob, NULL);
  342. 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!
  343. return false;
  344. if (g_pd3dDevice->CreatePixelShader((DWORD*)g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize(), NULL, &g_pPixelShader) != S_OK)
  345. return false;
  346. }
  347. // Create the blending setup
  348. {
  349. D3D11_BLEND_DESC desc;
  350. ZeroMemory(&desc, sizeof(desc));
  351. desc.AlphaToCoverageEnable = false;
  352. desc.RenderTarget[0].BlendEnable = true;
  353. desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
  354. desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
  355. desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
  356. desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
  357. desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
  358. desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
  359. desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
  360. g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState);
  361. }
  362. // Create the rasterizer state
  363. {
  364. D3D11_RASTERIZER_DESC desc;
  365. ZeroMemory(&desc, sizeof(desc));
  366. desc.FillMode = D3D11_FILL_SOLID;
  367. desc.CullMode = D3D11_CULL_NONE;
  368. desc.ScissorEnable = true;
  369. desc.DepthClipEnable = true;
  370. g_pd3dDevice->CreateRasterizerState(&desc, &g_pRasterizerState);
  371. }
  372. ImGui_ImplDX11_CreateFontsTexture();
  373. return true;
  374. }
  375. void ImGui_ImplDX11_InvalidateDeviceObjects()
  376. {
  377. if (!g_pd3dDevice)
  378. return;
  379. if (g_pFontSampler) { g_pFontSampler->Release(); g_pFontSampler = NULL; }
  380. if (g_pFontTextureView) { g_pFontTextureView->Release(); g_pFontTextureView = NULL; ImGui::GetIO().Fonts->TexID = 0; }
  381. if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
  382. if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
  383. if (g_pBlendState) { g_pBlendState->Release(); g_pBlendState = NULL; }
  384. if (g_pRasterizerState) { g_pRasterizerState->Release(); g_pRasterizerState = NULL; }
  385. if (g_pPixelShader) { g_pPixelShader->Release(); g_pPixelShader = NULL; }
  386. if (g_pPixelShaderBlob) { g_pPixelShaderBlob->Release(); g_pPixelShaderBlob = NULL; }
  387. if (g_pVertexConstantBuffer) { g_pVertexConstantBuffer->Release(); g_pVertexConstantBuffer = NULL; }
  388. if (g_pInputLayout) { g_pInputLayout->Release(); g_pInputLayout = NULL; }
  389. if (g_pVertexShader) { g_pVertexShader->Release(); g_pVertexShader = NULL; }
  390. if (g_pVertexShaderBlob) { g_pVertexShaderBlob->Release(); g_pVertexShaderBlob = NULL; }
  391. }
  392. bool ImGui_ImplDX11_Init(void* hwnd, ID3D11Device* device, ID3D11DeviceContext* device_context)
  393. {
  394. g_hWnd = (HWND)hwnd;
  395. g_pd3dDevice = device;
  396. g_pd3dDeviceContext = device_context;
  397. if (!QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
  398. return false;
  399. if (!QueryPerformanceCounter((LARGE_INTEGER *)&g_Time))
  400. return false;
  401. ImGuiIO& io = ImGui::GetIO();
  402. io.KeyMap[ImGuiKey_Tab] = VK_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array that we will update during the application lifetime.
  403. io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
  404. io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
  405. io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
  406. io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
  407. io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
  408. io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
  409. io.KeyMap[ImGuiKey_Home] = VK_HOME;
  410. io.KeyMap[ImGuiKey_End] = VK_END;
  411. io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
  412. io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
  413. io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
  414. io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
  415. io.KeyMap[ImGuiKey_A] = 'A';
  416. io.KeyMap[ImGuiKey_C] = 'C';
  417. io.KeyMap[ImGuiKey_V] = 'V';
  418. io.KeyMap[ImGuiKey_X] = 'X';
  419. io.KeyMap[ImGuiKey_Y] = 'Y';
  420. io.KeyMap[ImGuiKey_Z] = 'Z';
  421. io.RenderDrawListsFn = ImGui_ImplDX11_RenderDrawLists; // Alternatively you can set this to NULL and call ImGui::GetDrawData() after ImGui::Render() to get the same ImDrawData pointer.
  422. io.ImeWindowHandle = g_hWnd;
  423. return true;
  424. }
  425. void ImGui_ImplDX11_Shutdown()
  426. {
  427. ImGui_ImplDX11_InvalidateDeviceObjects();
  428. ImGui::Shutdown();
  429. g_pd3dDevice = NULL;
  430. g_pd3dDeviceContext = NULL;
  431. g_hWnd = (HWND)0;
  432. }
  433. void ImGui_ImplDX11_NewFrame()
  434. {
  435. if (!g_pVB)
  436. ImGui_ImplDX11_CreateDeviceObjects();
  437. ImGuiIO& io = ImGui::GetIO();
  438. // Setup display size (every frame to accommodate for window resizing)
  439. RECT rect;
  440. GetClientRect(g_hWnd, &rect);
  441. io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
  442. // Setup time step
  443. INT64 current_time;
  444. QueryPerformanceCounter((LARGE_INTEGER *)&current_time);
  445. io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
  446. g_Time = current_time;
  447. // Read keyboard modifiers inputs
  448. io.KeyCtrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
  449. io.KeyShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
  450. io.KeyAlt = (GetKeyState(VK_MENU) & 0x8000) != 0;
  451. // io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events
  452. // io.MousePos : filled by WM_MOUSEMOVE events
  453. // io.MouseDown : filled by WM_*BUTTON* events
  454. // io.MouseWheel : filled by WM_MOUSEWHEEL events
  455. // Hide OS mouse cursor if ImGui is drawing it
  456. SetCursor(io.MouseDrawCursor ? NULL : LoadCursor(NULL, IDC_ARROW));
  457. // Start the frame
  458. ImGui::NewFrame();
  459. }