imgui_impl_dx11.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. // ImGui Win32 + DirectX11 binding
  2. // Implemented features:
  3. // [X] User texture binding. Use 'ID3D11ShaderResourceView*' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
  4. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  5. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  6. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  7. // https://github.com/ocornut/imgui
  8. // CHANGELOG
  9. // (minor and older changes stripped away, please see git history for details)
  10. // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX11_RenderDrawData() in the .h file so you can call it yourself.
  11. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  12. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  13. // 2018-02-06: Inputs: Honoring the io.WantMoveMouse by repositioning the mouse by using navigation and ImGuiNavFlags_MoveMouse is set.
  14. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  15. // 2018-01-08: Inputs: Added mapping for ImGuiKey_Insert.
  16. // 2018-01-05: Inputs: Added WM_LBUTTONDBLCLK double-click handlers for window classes with the CS_DBLCLKS flag.
  17. // 2017-10-23: Inputs: Added WM_SYSKEYDOWN / WM_SYSKEYUP handlers so e.g. the VK_MENU key can be read.
  18. // 2017-10-23: Inputs: Using Win32 ::SetCapture/::GetCapture() to retrieve mouse positions outside the client area when dragging.
  19. // 2016-11-12: Inputs: Only call Win32 ::SetCursor(NULL) when io.MouseDrawCursor is set.
  20. // 2016-05-07: DirectX11: Disabling depth-write.
  21. #include "imgui.h"
  22. #include "imgui_impl_dx11.h"
  23. // DirectX
  24. #include <d3d11.h>
  25. #include <d3dcompiler.h>
  26. #define DIRECTINPUT_VERSION 0x0800
  27. #include <dinput.h>
  28. static ID3D11Device* g_pd3dDevice = NULL;
  29. static ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
  30. static ID3D11Buffer* g_pVB = NULL;
  31. static ID3D11Buffer* g_pIB = NULL;
  32. static ID3D10Blob * g_pVertexShaderBlob = NULL;
  33. static ID3D11VertexShader* g_pVertexShader = NULL;
  34. static ID3D11InputLayout* g_pInputLayout = NULL;
  35. static ID3D11Buffer* g_pVertexConstantBuffer = NULL;
  36. static ID3D10Blob * g_pPixelShaderBlob = NULL;
  37. static ID3D11PixelShader* g_pPixelShader = NULL;
  38. static ID3D11SamplerState* g_pFontSampler = NULL;
  39. static ID3D11ShaderResourceView*g_pFontTextureView = NULL;
  40. static ID3D11RasterizerState* g_pRasterizerState = NULL;
  41. static ID3D11BlendState* g_pBlendState = NULL;
  42. static ID3D11DepthStencilState* g_pDepthStencilState = NULL;
  43. static int g_VertexBufferSize = 5000, g_IndexBufferSize = 10000;
  44. struct VERTEX_CONSTANT_BUFFER
  45. {
  46. float mvp[4][4];
  47. };
  48. // Render function
  49. // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
  50. void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
  51. {
  52. ID3D11DeviceContext* ctx = g_pd3dDeviceContext;
  53. // Create and grow vertex/index buffers if needed
  54. if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
  55. {
  56. if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
  57. g_VertexBufferSize = draw_data->TotalVtxCount + 5000;
  58. D3D11_BUFFER_DESC desc;
  59. memset(&desc, 0, sizeof(D3D11_BUFFER_DESC));
  60. desc.Usage = D3D11_USAGE_DYNAMIC;
  61. desc.ByteWidth = g_VertexBufferSize * sizeof(ImDrawVert);
  62. desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  63. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  64. desc.MiscFlags = 0;
  65. if (g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVB) < 0)
  66. return;
  67. }
  68. if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
  69. {
  70. if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
  71. g_IndexBufferSize = draw_data->TotalIdxCount + 10000;
  72. D3D11_BUFFER_DESC desc;
  73. memset(&desc, 0, sizeof(D3D11_BUFFER_DESC));
  74. desc.Usage = D3D11_USAGE_DYNAMIC;
  75. desc.ByteWidth = g_IndexBufferSize * sizeof(ImDrawIdx);
  76. desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  77. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  78. if (g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pIB) < 0)
  79. return;
  80. }
  81. // Copy and convert all vertices into a single contiguous buffer
  82. D3D11_MAPPED_SUBRESOURCE vtx_resource, idx_resource;
  83. if (ctx->Map(g_pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &vtx_resource) != S_OK)
  84. return;
  85. if (ctx->Map(g_pIB, 0, D3D11_MAP_WRITE_DISCARD, 0, &idx_resource) != S_OK)
  86. return;
  87. ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource.pData;
  88. ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource.pData;
  89. for (int n = 0; n < draw_data->CmdListsCount; n++)
  90. {
  91. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  92. memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
  93. memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
  94. vtx_dst += cmd_list->VtxBuffer.Size;
  95. idx_dst += cmd_list->IdxBuffer.Size;
  96. }
  97. ctx->Unmap(g_pVB, 0);
  98. ctx->Unmap(g_pIB, 0);
  99. // Setup orthographic projection matrix into our constant buffer
  100. {
  101. D3D11_MAPPED_SUBRESOURCE mapped_resource;
  102. if (ctx->Map(g_pVertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_resource) != S_OK)
  103. return;
  104. VERTEX_CONSTANT_BUFFER* constant_buffer = (VERTEX_CONSTANT_BUFFER*)mapped_resource.pData;
  105. float L = 0.0f;
  106. float R = ImGui::GetIO().DisplaySize.x;
  107. float B = ImGui::GetIO().DisplaySize.y;
  108. float T = 0.0f;
  109. float mvp[4][4] =
  110. {
  111. { 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
  112. { 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
  113. { 0.0f, 0.0f, 0.5f, 0.0f },
  114. { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
  115. };
  116. memcpy(&constant_buffer->mvp, mvp, sizeof(mvp));
  117. ctx->Unmap(g_pVertexConstantBuffer, 0);
  118. }
  119. // Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!)
  120. struct BACKUP_DX11_STATE
  121. {
  122. UINT ScissorRectsCount, ViewportsCount;
  123. D3D11_RECT ScissorRects[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
  124. D3D11_VIEWPORT Viewports[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
  125. ID3D11RasterizerState* RS;
  126. ID3D11BlendState* BlendState;
  127. FLOAT BlendFactor[4];
  128. UINT SampleMask;
  129. UINT StencilRef;
  130. ID3D11DepthStencilState* DepthStencilState;
  131. ID3D11ShaderResourceView* PSShaderResource;
  132. ID3D11SamplerState* PSSampler;
  133. ID3D11PixelShader* PS;
  134. ID3D11VertexShader* VS;
  135. UINT PSInstancesCount, VSInstancesCount;
  136. ID3D11ClassInstance* PSInstances[256], *VSInstances[256]; // 256 is max according to PSSetShader documentation
  137. D3D11_PRIMITIVE_TOPOLOGY PrimitiveTopology;
  138. ID3D11Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer;
  139. UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset;
  140. DXGI_FORMAT IndexBufferFormat;
  141. ID3D11InputLayout* InputLayout;
  142. };
  143. BACKUP_DX11_STATE old;
  144. old.ScissorRectsCount = old.ViewportsCount = D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
  145. ctx->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects);
  146. ctx->RSGetViewports(&old.ViewportsCount, old.Viewports);
  147. ctx->RSGetState(&old.RS);
  148. ctx->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask);
  149. ctx->OMGetDepthStencilState(&old.DepthStencilState, &old.StencilRef);
  150. ctx->PSGetShaderResources(0, 1, &old.PSShaderResource);
  151. ctx->PSGetSamplers(0, 1, &old.PSSampler);
  152. old.PSInstancesCount = old.VSInstancesCount = 256;
  153. ctx->PSGetShader(&old.PS, old.PSInstances, &old.PSInstancesCount);
  154. ctx->VSGetShader(&old.VS, old.VSInstances, &old.VSInstancesCount);
  155. ctx->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer);
  156. ctx->IAGetPrimitiveTopology(&old.PrimitiveTopology);
  157. ctx->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset);
  158. ctx->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset);
  159. ctx->IAGetInputLayout(&old.InputLayout);
  160. // Setup viewport
  161. D3D11_VIEWPORT vp;
  162. memset(&vp, 0, sizeof(D3D11_VIEWPORT));
  163. vp.Width = ImGui::GetIO().DisplaySize.x;
  164. vp.Height = ImGui::GetIO().DisplaySize.y;
  165. vp.MinDepth = 0.0f;
  166. vp.MaxDepth = 1.0f;
  167. vp.TopLeftX = vp.TopLeftY = 0.0f;
  168. ctx->RSSetViewports(1, &vp);
  169. // Bind shader and vertex buffers
  170. unsigned int stride = sizeof(ImDrawVert);
  171. unsigned int offset = 0;
  172. ctx->IASetInputLayout(g_pInputLayout);
  173. ctx->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset);
  174. ctx->IASetIndexBuffer(g_pIB, sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);
  175. ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  176. ctx->VSSetShader(g_pVertexShader, NULL, 0);
  177. ctx->VSSetConstantBuffers(0, 1, &g_pVertexConstantBuffer);
  178. ctx->PSSetShader(g_pPixelShader, NULL, 0);
  179. ctx->PSSetSamplers(0, 1, &g_pFontSampler);
  180. // Setup render state
  181. const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
  182. ctx->OMSetBlendState(g_pBlendState, blend_factor, 0xffffffff);
  183. ctx->OMSetDepthStencilState(g_pDepthStencilState, 0);
  184. ctx->RSSetState(g_pRasterizerState);
  185. // Render command lists
  186. int vtx_offset = 0;
  187. int idx_offset = 0;
  188. for (int n = 0; n < draw_data->CmdListsCount; n++)
  189. {
  190. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  191. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  192. {
  193. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  194. if (pcmd->UserCallback)
  195. {
  196. pcmd->UserCallback(cmd_list, pcmd);
  197. }
  198. else
  199. {
  200. const D3D11_RECT r = { (LONG)pcmd->ClipRect.x, (LONG)pcmd->ClipRect.y, (LONG)pcmd->ClipRect.z, (LONG)pcmd->ClipRect.w };
  201. ctx->PSSetShaderResources(0, 1, (ID3D11ShaderResourceView**)&pcmd->TextureId);
  202. ctx->RSSetScissorRects(1, &r);
  203. ctx->DrawIndexed(pcmd->ElemCount, idx_offset, vtx_offset);
  204. }
  205. idx_offset += pcmd->ElemCount;
  206. }
  207. vtx_offset += cmd_list->VtxBuffer.Size;
  208. }
  209. // Restore modified DX state
  210. ctx->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects);
  211. ctx->RSSetViewports(old.ViewportsCount, old.Viewports);
  212. ctx->RSSetState(old.RS); if (old.RS) old.RS->Release();
  213. ctx->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask); if (old.BlendState) old.BlendState->Release();
  214. ctx->OMSetDepthStencilState(old.DepthStencilState, old.StencilRef); if (old.DepthStencilState) old.DepthStencilState->Release();
  215. ctx->PSSetShaderResources(0, 1, &old.PSShaderResource); if (old.PSShaderResource) old.PSShaderResource->Release();
  216. ctx->PSSetSamplers(0, 1, &old.PSSampler); if (old.PSSampler) old.PSSampler->Release();
  217. ctx->PSSetShader(old.PS, old.PSInstances, old.PSInstancesCount); if (old.PS) old.PS->Release();
  218. for (UINT i = 0; i < old.PSInstancesCount; i++) if (old.PSInstances[i]) old.PSInstances[i]->Release();
  219. ctx->VSSetShader(old.VS, old.VSInstances, old.VSInstancesCount); if (old.VS) old.VS->Release();
  220. ctx->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer); if (old.VSConstantBuffer) old.VSConstantBuffer->Release();
  221. for (UINT i = 0; i < old.VSInstancesCount; i++) if (old.VSInstances[i]) old.VSInstances[i]->Release();
  222. ctx->IASetPrimitiveTopology(old.PrimitiveTopology);
  223. ctx->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset); if (old.IndexBuffer) old.IndexBuffer->Release();
  224. ctx->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); if (old.VertexBuffer) old.VertexBuffer->Release();
  225. ctx->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
  226. }
  227. static void ImGui_ImplDX11_CreateFontsTexture()
  228. {
  229. // Build texture atlas
  230. ImGuiIO& io = ImGui::GetIO();
  231. unsigned char* pixels;
  232. int width, height;
  233. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  234. // Upload texture to graphics system
  235. {
  236. D3D11_TEXTURE2D_DESC desc;
  237. ZeroMemory(&desc, sizeof(desc));
  238. desc.Width = width;
  239. desc.Height = height;
  240. desc.MipLevels = 1;
  241. desc.ArraySize = 1;
  242. desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  243. desc.SampleDesc.Count = 1;
  244. desc.Usage = D3D11_USAGE_DEFAULT;
  245. desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  246. desc.CPUAccessFlags = 0;
  247. ID3D11Texture2D *pTexture = NULL;
  248. D3D11_SUBRESOURCE_DATA subResource;
  249. subResource.pSysMem = pixels;
  250. subResource.SysMemPitch = desc.Width * 4;
  251. subResource.SysMemSlicePitch = 0;
  252. g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);
  253. // Create texture view
  254. D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
  255. ZeroMemory(&srvDesc, sizeof(srvDesc));
  256. srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  257. srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  258. srvDesc.Texture2D.MipLevels = desc.MipLevels;
  259. srvDesc.Texture2D.MostDetailedMip = 0;
  260. g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &g_pFontTextureView);
  261. pTexture->Release();
  262. }
  263. // Store our identifier
  264. io.Fonts->TexID = (void *)g_pFontTextureView;
  265. // Create texture sampler
  266. {
  267. D3D11_SAMPLER_DESC desc;
  268. ZeroMemory(&desc, sizeof(desc));
  269. desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  270. desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  271. desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
  272. desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  273. desc.MipLODBias = 0.f;
  274. desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
  275. desc.MinLOD = 0.f;
  276. desc.MaxLOD = 0.f;
  277. g_pd3dDevice->CreateSamplerState(&desc, &g_pFontSampler);
  278. }
  279. }
  280. bool ImGui_ImplDX11_CreateDeviceObjects()
  281. {
  282. if (!g_pd3dDevice)
  283. return false;
  284. if (g_pFontSampler)
  285. ImGui_ImplDX11_InvalidateDeviceObjects();
  286. // By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
  287. // If you would like to use this DX11 sample code but remove this dependency you can:
  288. // 1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [preferred solution]
  289. // 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.
  290. // See https://github.com/ocornut/imgui/pull/638 for sources and details.
  291. // Create the vertex shader
  292. {
  293. static const char* vertexShader =
  294. "cbuffer vertexBuffer : register(b0) \
  295. {\
  296. float4x4 ProjectionMatrix; \
  297. };\
  298. struct VS_INPUT\
  299. {\
  300. float2 pos : POSITION;\
  301. float4 col : COLOR0;\
  302. float2 uv : TEXCOORD0;\
  303. };\
  304. \
  305. struct PS_INPUT\
  306. {\
  307. float4 pos : SV_POSITION;\
  308. float4 col : COLOR0;\
  309. float2 uv : TEXCOORD0;\
  310. };\
  311. \
  312. PS_INPUT main(VS_INPUT input)\
  313. {\
  314. PS_INPUT output;\
  315. output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
  316. output.col = input.col;\
  317. output.uv = input.uv;\
  318. return output;\
  319. }";
  320. D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_4_0", 0, 0, &g_pVertexShaderBlob, NULL);
  321. 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!
  322. return false;
  323. if (g_pd3dDevice->CreateVertexShader((DWORD*)g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), NULL, &g_pVertexShader) != S_OK)
  324. return false;
  325. // Create the input layout
  326. D3D11_INPUT_ELEMENT_DESC local_layout[] = {
  327. { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->pos), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  328. { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->uv), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  329. { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((ImDrawVert*)0)->col), D3D11_INPUT_PER_VERTEX_DATA, 0 },
  330. };
  331. if (g_pd3dDevice->CreateInputLayout(local_layout, 3, g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pInputLayout) != S_OK)
  332. return false;
  333. // Create the constant buffer
  334. {
  335. D3D11_BUFFER_DESC desc;
  336. desc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER);
  337. desc.Usage = D3D11_USAGE_DYNAMIC;
  338. desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  339. desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  340. desc.MiscFlags = 0;
  341. g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVertexConstantBuffer);
  342. }
  343. }
  344. // Create the pixel shader
  345. {
  346. static const char* pixelShader =
  347. "struct PS_INPUT\
  348. {\
  349. float4 pos : SV_POSITION;\
  350. float4 col : COLOR0;\
  351. float2 uv : TEXCOORD0;\
  352. };\
  353. sampler sampler0;\
  354. Texture2D texture0;\
  355. \
  356. float4 main(PS_INPUT input) : SV_Target\
  357. {\
  358. float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
  359. return out_col; \
  360. }";
  361. D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_4_0", 0, 0, &g_pPixelShaderBlob, NULL);
  362. 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!
  363. return false;
  364. if (g_pd3dDevice->CreatePixelShader((DWORD*)g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize(), NULL, &g_pPixelShader) != S_OK)
  365. return false;
  366. }
  367. // Create the blending setup
  368. {
  369. D3D11_BLEND_DESC desc;
  370. ZeroMemory(&desc, sizeof(desc));
  371. desc.AlphaToCoverageEnable = false;
  372. desc.RenderTarget[0].BlendEnable = true;
  373. desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
  374. desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
  375. desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
  376. desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
  377. desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
  378. desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
  379. desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
  380. g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState);
  381. }
  382. // Create the rasterizer state
  383. {
  384. D3D11_RASTERIZER_DESC desc;
  385. ZeroMemory(&desc, sizeof(desc));
  386. desc.FillMode = D3D11_FILL_SOLID;
  387. desc.CullMode = D3D11_CULL_NONE;
  388. desc.ScissorEnable = true;
  389. desc.DepthClipEnable = true;
  390. g_pd3dDevice->CreateRasterizerState(&desc, &g_pRasterizerState);
  391. }
  392. // Create depth-stencil State
  393. {
  394. D3D11_DEPTH_STENCIL_DESC desc;
  395. ZeroMemory(&desc, sizeof(desc));
  396. desc.DepthEnable = false;
  397. desc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  398. desc.DepthFunc = D3D11_COMPARISON_ALWAYS;
  399. desc.StencilEnable = false;
  400. desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  401. desc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  402. desc.BackFace = desc.FrontFace;
  403. g_pd3dDevice->CreateDepthStencilState(&desc, &g_pDepthStencilState);
  404. }
  405. ImGui_ImplDX11_CreateFontsTexture();
  406. return true;
  407. }
  408. void ImGui_ImplDX11_InvalidateDeviceObjects()
  409. {
  410. if (!g_pd3dDevice)
  411. return;
  412. if (g_pFontSampler) { g_pFontSampler->Release(); g_pFontSampler = NULL; }
  413. 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.
  414. if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
  415. if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
  416. if (g_pBlendState) { g_pBlendState->Release(); g_pBlendState = NULL; }
  417. if (g_pDepthStencilState) { g_pDepthStencilState->Release(); g_pDepthStencilState = NULL; }
  418. if (g_pRasterizerState) { g_pRasterizerState->Release(); g_pRasterizerState = NULL; }
  419. if (g_pPixelShader) { g_pPixelShader->Release(); g_pPixelShader = NULL; }
  420. if (g_pPixelShaderBlob) { g_pPixelShaderBlob->Release(); g_pPixelShaderBlob = NULL; }
  421. if (g_pVertexConstantBuffer) { g_pVertexConstantBuffer->Release(); g_pVertexConstantBuffer = NULL; }
  422. if (g_pInputLayout) { g_pInputLayout->Release(); g_pInputLayout = NULL; }
  423. if (g_pVertexShader) { g_pVertexShader->Release(); g_pVertexShader = NULL; }
  424. if (g_pVertexShaderBlob) { g_pVertexShaderBlob->Release(); g_pVertexShaderBlob = NULL; }
  425. }
  426. bool ImGui_ImplDX11_Init(ID3D11Device* device, ID3D11DeviceContext* device_context)
  427. {
  428. g_pd3dDevice = device;
  429. g_pd3dDeviceContext = device_context;
  430. return true;
  431. }
  432. void ImGui_ImplDX11_Shutdown()
  433. {
  434. ImGui_ImplDX11_InvalidateDeviceObjects();
  435. g_pd3dDevice = NULL;
  436. g_pd3dDeviceContext = NULL;
  437. }
  438. void ImGui_ImplDX11_NewFrame()
  439. {
  440. if (!g_pFontSampler)
  441. ImGui_ImplDX11_CreateDeviceObjects();
  442. }