2
0

imgui_impl_dx11.cpp 23 KB

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