imgui_impl_dx10.cpp 24 KB

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