imgui_impl_dx10.cpp 26 KB

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