imgui_impl_dx10.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. // ImGui Renderer for: DirectX10
  2. // This needs to be used along with a Platform Binding (e.g. Win32)
  3. // Implemented features:
  4. // [X] User texture binding. Use 'ID3D10ShaderResourceView*' as ImTextureID. Read the FAQ about ImTextureID in imgui.cpp.
  5. // [X] Multi-viewport rendering (when ImGuiConfigFlags_ViewportsEnable is enabled).
  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 use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  8. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  9. // https://github.com/ocornut/imgui
  10. // CHANGELOG
  11. // (minor and older changes stripped away, please see git history for details)
  12. // 2018-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
  13. // 2018-06-08: Misc: Extracted imgui_impl_dx10.cpp/.h away from the old combined DX10+Win32 example.
  14. // 2018-06-08: DirectX10: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
  15. // 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.
  16. // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX10_RenderDrawData() in the .h file so you can call it yourself.
  17. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  18. // 2016-05-07: DirectX10: Disabling depth-write.
  19. #include "imgui.h"
  20. #include "imgui_impl_dx10.h"
  21. // DirectX
  22. #include <stdio.h>
  23. #include <d3d10_1.h>
  24. #include <d3d10.h>
  25. #include <d3dcompiler.h>
  26. // DirectX data
  27. static ID3D10Device* g_pd3dDevice = NULL;
  28. static IDXGIFactory* g_pFactory = NULL;
  29. static ID3D10Buffer* g_pVB = NULL;
  30. static ID3D10Buffer* g_pIB = NULL;
  31. static ID3D10Blob * g_pVertexShaderBlob = NULL;
  32. static ID3D10VertexShader* g_pVertexShader = NULL;
  33. static ID3D10InputLayout* g_pInputLayout = NULL;
  34. static ID3D10Buffer* g_pVertexConstantBuffer = NULL;
  35. static ID3D10Blob * g_pPixelShaderBlob = NULL;
  36. static ID3D10PixelShader* g_pPixelShader = NULL;
  37. static ID3D10SamplerState* g_pFontSampler = NULL;
  38. static ID3D10ShaderResourceView*g_pFontTextureView = NULL;
  39. static ID3D10RasterizerState* g_pRasterizerState = NULL;
  40. static ID3D10BlendState* g_pBlendState = NULL;
  41. static ID3D10DepthStencilState* g_pDepthStencilState = NULL;
  42. static int g_VertexBufferSize = 5000, g_IndexBufferSize = 10000;
  43. struct VERTEX_CONSTANT_BUFFER
  44. {
  45. float mvp[4][4];
  46. };
  47. // Forward Declarations
  48. static void ImGui_ImplDX10_InitPlatformInterface();
  49. static void ImGui_ImplDX10_ShutdownPlatformInterface();
  50. // Render function
  51. // (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
  52. void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data)
  53. {
  54. ID3D10Device* ctx = g_pd3dDevice;
  55. // Create and grow vertex/index buffers if needed
  56. if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
  57. {
  58. if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
  59. g_VertexBufferSize = draw_data->TotalVtxCount + 5000;
  60. D3D10_BUFFER_DESC desc;
  61. memset(&desc, 0, sizeof(D3D10_BUFFER_DESC));
  62. desc.Usage = D3D10_USAGE_DYNAMIC;
  63. desc.ByteWidth = g_VertexBufferSize * sizeof(ImDrawVert);
  64. desc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
  65. desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
  66. desc.MiscFlags = 0;
  67. if (ctx->CreateBuffer(&desc, NULL, &g_pVB) < 0)
  68. return;
  69. }
  70. if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
  71. {
  72. if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
  73. g_IndexBufferSize = draw_data->TotalIdxCount + 10000;
  74. D3D10_BUFFER_DESC desc;
  75. memset(&desc, 0, sizeof(D3D10_BUFFER_DESC));
  76. desc.Usage = D3D10_USAGE_DYNAMIC;
  77. desc.ByteWidth = g_IndexBufferSize * sizeof(ImDrawIdx);
  78. desc.BindFlags = D3D10_BIND_INDEX_BUFFER;
  79. desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
  80. if (ctx->CreateBuffer(&desc, NULL, &g_pIB) < 0)
  81. return;
  82. }
  83. // Copy and convert all vertices into a single contiguous buffer
  84. ImDrawVert* vtx_dst = NULL;
  85. ImDrawIdx* idx_dst = NULL;
  86. g_pVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&vtx_dst);
  87. g_pIB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&idx_dst);
  88. for (int n = 0; n < draw_data->CmdListsCount; n++)
  89. {
  90. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  91. memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert));
  92. memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
  93. vtx_dst += cmd_list->VtxBuffer.Size;
  94. idx_dst += cmd_list->IdxBuffer.Size;
  95. }
  96. g_pVB->Unmap();
  97. g_pIB->Unmap();
  98. // Setup orthographic projection matrix into our constant buffer
  99. // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin is (0,0) for single viewport apps.
  100. {
  101. void* mapped_resource;
  102. if (g_pVertexConstantBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &mapped_resource) != S_OK)
  103. return;
  104. VERTEX_CONSTANT_BUFFER* constant_buffer = (VERTEX_CONSTANT_BUFFER*)mapped_resource;
  105. float L = draw_data->DisplayPos.x;
  106. float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
  107. float T = draw_data->DisplayPos.y;
  108. float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
  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. g_pVertexConstantBuffer->Unmap();
  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_DX10_STATE
  121. {
  122. UINT ScissorRectsCount, ViewportsCount;
  123. D3D10_RECT ScissorRects[D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
  124. D3D10_VIEWPORT Viewports[D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
  125. ID3D10RasterizerState* RS;
  126. ID3D10BlendState* BlendState;
  127. FLOAT BlendFactor[4];
  128. UINT SampleMask;
  129. UINT StencilRef;
  130. ID3D10DepthStencilState* DepthStencilState;
  131. ID3D10ShaderResourceView* PSShaderResource;
  132. ID3D10SamplerState* PSSampler;
  133. ID3D10PixelShader* PS;
  134. ID3D10VertexShader* VS;
  135. D3D10_PRIMITIVE_TOPOLOGY PrimitiveTopology;
  136. ID3D10Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer;
  137. UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset;
  138. DXGI_FORMAT IndexBufferFormat;
  139. ID3D10InputLayout* InputLayout;
  140. };
  141. BACKUP_DX10_STATE old;
  142. old.ScissorRectsCount = old.ViewportsCount = D3D10_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. ctx->PSGetShader(&old.PS);
  151. ctx->VSGetShader(&old.VS);
  152. ctx->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer);
  153. ctx->IAGetPrimitiveTopology(&old.PrimitiveTopology);
  154. ctx->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset);
  155. ctx->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset);
  156. ctx->IAGetInputLayout(&old.InputLayout);
  157. // Setup viewport
  158. D3D10_VIEWPORT vp;
  159. memset(&vp, 0, sizeof(D3D10_VIEWPORT));
  160. vp.Width = (UINT)draw_data->DisplaySize.x;
  161. vp.Height = (UINT)draw_data->DisplaySize.y;
  162. vp.MinDepth = 0.0f;
  163. vp.MaxDepth = 1.0f;
  164. vp.TopLeftX = vp.TopLeftY = 0;
  165. ctx->RSSetViewports(1, &vp);
  166. // Bind shader and vertex buffers
  167. unsigned int stride = sizeof(ImDrawVert);
  168. unsigned int offset = 0;
  169. ctx->IASetInputLayout(g_pInputLayout);
  170. ctx->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset);
  171. ctx->IASetIndexBuffer(g_pIB, sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);
  172. ctx->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  173. ctx->VSSetShader(g_pVertexShader);
  174. ctx->VSSetConstantBuffers(0, 1, &g_pVertexConstantBuffer);
  175. ctx->PSSetShader(g_pPixelShader);
  176. ctx->PSSetSamplers(0, 1, &g_pFontSampler);
  177. // Setup render state
  178. const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
  179. ctx->OMSetBlendState(g_pBlendState, blend_factor, 0xffffffff);
  180. ctx->OMSetDepthStencilState(g_pDepthStencilState, 0);
  181. ctx->RSSetState(g_pRasterizerState);
  182. // Render command lists
  183. int vtx_offset = 0;
  184. int idx_offset = 0;
  185. ImVec2 pos = draw_data->DisplayPos;
  186. for (int n = 0; n < draw_data->CmdListsCount; n++)
  187. {
  188. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  189. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
  190. {
  191. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  192. if (pcmd->UserCallback)
  193. {
  194. // User callback (registered via ImDrawList::AddCallback)
  195. pcmd->UserCallback(cmd_list, pcmd);
  196. }
  197. else
  198. {
  199. // Apply scissor/clipping rectangle
  200. const D3D10_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)};
  201. ctx->RSSetScissorRects(1, &r);
  202. // Bind texture, Draw
  203. ctx->PSSetShaderResources(0, 1, (ID3D10ShaderResourceView**)&pcmd->TextureId);
  204. ctx->DrawIndexed(pcmd->ElemCount, idx_offset, vtx_offset);
  205. }
  206. idx_offset += pcmd->ElemCount;
  207. }
  208. vtx_offset += cmd_list->VtxBuffer.Size;
  209. }
  210. // Restore modified DX state
  211. ctx->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects);
  212. ctx->RSSetViewports(old.ViewportsCount, old.Viewports);
  213. ctx->RSSetState(old.RS); if (old.RS) old.RS->Release();
  214. ctx->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask); if (old.BlendState) old.BlendState->Release();
  215. ctx->OMSetDepthStencilState(old.DepthStencilState, old.StencilRef); if (old.DepthStencilState) old.DepthStencilState->Release();
  216. ctx->PSSetShaderResources(0, 1, &old.PSShaderResource); if (old.PSShaderResource) old.PSShaderResource->Release();
  217. ctx->PSSetSamplers(0, 1, &old.PSSampler); if (old.PSSampler) old.PSSampler->Release();
  218. ctx->PSSetShader(old.PS); if (old.PS) old.PS->Release();
  219. ctx->VSSetShader(old.VS); if (old.VS) old.VS->Release();
  220. ctx->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer); if (old.VSConstantBuffer) old.VSConstantBuffer->Release();
  221. ctx->IASetPrimitiveTopology(old.PrimitiveTopology);
  222. ctx->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset); if (old.IndexBuffer) old.IndexBuffer->Release();
  223. ctx->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); if (old.VertexBuffer) old.VertexBuffer->Release();
  224. ctx->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
  225. }
  226. static void ImGui_ImplDX10_CreateFontsTexture()
  227. {
  228. // Build texture atlas
  229. ImGuiIO& io = ImGui::GetIO();
  230. unsigned char* pixels;
  231. int width, height;
  232. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  233. // Upload texture to graphics system
  234. {
  235. D3D10_TEXTURE2D_DESC desc;
  236. ZeroMemory(&desc, sizeof(desc));
  237. desc.Width = width;
  238. desc.Height = height;
  239. desc.MipLevels = 1;
  240. desc.ArraySize = 1;
  241. desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  242. desc.SampleDesc.Count = 1;
  243. desc.Usage = D3D10_USAGE_DEFAULT;
  244. desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
  245. desc.CPUAccessFlags = 0;
  246. ID3D10Texture2D *pTexture = NULL;
  247. D3D10_SUBRESOURCE_DATA subResource;
  248. subResource.pSysMem = pixels;
  249. subResource.SysMemPitch = desc.Width * 4;
  250. subResource.SysMemSlicePitch = 0;
  251. g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture);
  252. // Create texture view
  253. D3D10_SHADER_RESOURCE_VIEW_DESC srv_desc;
  254. ZeroMemory(&srv_desc, sizeof(srv_desc));
  255. srv_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  256. srv_desc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
  257. srv_desc.Texture2D.MipLevels = desc.MipLevels;
  258. srv_desc.Texture2D.MostDetailedMip = 0;
  259. g_pd3dDevice->CreateShaderResourceView(pTexture, &srv_desc, &g_pFontTextureView);
  260. pTexture->Release();
  261. }
  262. // Store our identifier
  263. io.Fonts->TexID = (void *)g_pFontTextureView;
  264. // Create texture sampler
  265. {
  266. D3D10_SAMPLER_DESC desc;
  267. ZeroMemory(&desc, sizeof(desc));
  268. desc.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR;
  269. desc.AddressU = D3D10_TEXTURE_ADDRESS_WRAP;
  270. desc.AddressV = D3D10_TEXTURE_ADDRESS_WRAP;
  271. desc.AddressW = D3D10_TEXTURE_ADDRESS_WRAP;
  272. desc.MipLODBias = 0.f;
  273. desc.ComparisonFunc = D3D10_COMPARISON_ALWAYS;
  274. desc.MinLOD = 0.f;
  275. desc.MaxLOD = 0.f;
  276. g_pd3dDevice->CreateSamplerState(&desc, &g_pFontSampler);
  277. }
  278. }
  279. bool ImGui_ImplDX10_CreateDeviceObjects()
  280. {
  281. if (!g_pd3dDevice)
  282. return false;
  283. if (g_pFontSampler)
  284. ImGui_ImplDX10_InvalidateDeviceObjects();
  285. // By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A)
  286. // If you would like to use this DX10 sample code but remove this dependency you can:
  287. // 1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [preferred solution]
  288. // 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL.
  289. // See https://github.com/ocornut/imgui/pull/638 for sources and details.
  290. // Create the vertex shader
  291. {
  292. static const char* vertexShader =
  293. "cbuffer vertexBuffer : register(b0) \
  294. {\
  295. float4x4 ProjectionMatrix; \
  296. };\
  297. struct VS_INPUT\
  298. {\
  299. float2 pos : POSITION;\
  300. float4 col : COLOR0;\
  301. float2 uv : TEXCOORD0;\
  302. };\
  303. \
  304. struct PS_INPUT\
  305. {\
  306. float4 pos : SV_POSITION;\
  307. float4 col : COLOR0;\
  308. float2 uv : TEXCOORD0;\
  309. };\
  310. \
  311. PS_INPUT main(VS_INPUT input)\
  312. {\
  313. PS_INPUT output;\
  314. output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
  315. output.col = input.col;\
  316. output.uv = input.uv;\
  317. return output;\
  318. }";
  319. D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_4_0", 0, 0, &g_pVertexShaderBlob, NULL);
  320. 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!
  321. return false;
  322. if (g_pd3dDevice->CreateVertexShader((DWORD*)g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pVertexShader) != S_OK)
  323. return false;
  324. // Create the input layout
  325. D3D10_INPUT_ELEMENT_DESC local_layout[] =
  326. {
  327. { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->pos), D3D10_INPUT_PER_VERTEX_DATA, 0 },
  328. { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->uv), D3D10_INPUT_PER_VERTEX_DATA, 0 },
  329. { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((ImDrawVert*)0)->col), D3D10_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. D3D10_BUFFER_DESC desc;
  336. desc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER);
  337. desc.Usage = D3D10_USAGE_DYNAMIC;
  338. desc.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
  339. desc.CPUAccessFlags = D3D10_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(), &g_pPixelShader) != S_OK)
  365. return false;
  366. }
  367. // Create the blending setup
  368. {
  369. D3D10_BLEND_DESC desc;
  370. ZeroMemory(&desc, sizeof(desc));
  371. desc.AlphaToCoverageEnable = false;
  372. desc.BlendEnable[0] = true;
  373. desc.SrcBlend = D3D10_BLEND_SRC_ALPHA;
  374. desc.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
  375. desc.BlendOp = D3D10_BLEND_OP_ADD;
  376. desc.SrcBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA;
  377. desc.DestBlendAlpha = D3D10_BLEND_ZERO;
  378. desc.BlendOpAlpha = D3D10_BLEND_OP_ADD;
  379. desc.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;
  380. g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState);
  381. }
  382. // Create the rasterizer state
  383. {
  384. D3D10_RASTERIZER_DESC desc;
  385. ZeroMemory(&desc, sizeof(desc));
  386. desc.FillMode = D3D10_FILL_SOLID;
  387. desc.CullMode = D3D10_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. D3D10_DEPTH_STENCIL_DESC desc;
  395. ZeroMemory(&desc, sizeof(desc));
  396. desc.DepthEnable = false;
  397. desc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ALL;
  398. desc.DepthFunc = D3D10_COMPARISON_ALWAYS;
  399. desc.StencilEnable = false;
  400. desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
  401. desc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS;
  402. desc.BackFace = desc.FrontFace;
  403. g_pd3dDevice->CreateDepthStencilState(&desc, &g_pDepthStencilState);
  404. }
  405. ImGui_ImplDX10_CreateFontsTexture();
  406. return true;
  407. }
  408. void ImGui_ImplDX10_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_ImplDX10_Init(ID3D10Device* device)
  427. {
  428. // Get factory from device
  429. IDXGIDevice* pDXGIDevice = NULL;
  430. IDXGIAdapter* pDXGIAdapter = NULL;
  431. IDXGIFactory* pFactory = NULL;
  432. if (device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)) != S_OK)
  433. return false;
  434. if (pDXGIDevice->GetParent(IID_PPV_ARGS(&pDXGIAdapter)) != S_OK)
  435. return false;
  436. if (pDXGIAdapter->GetParent(IID_PPV_ARGS(&pFactory)) != S_OK)
  437. return false;
  438. g_pd3dDevice = device;
  439. g_pFactory = pFactory;
  440. // Setup back-end capabilities flags
  441. ImGuiIO& io = ImGui::GetIO();
  442. io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
  443. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  444. ImGui_ImplDX10_InitPlatformInterface();
  445. return true;
  446. }
  447. void ImGui_ImplDX10_Shutdown()
  448. {
  449. ImGui_ImplDX10_ShutdownPlatformInterface();
  450. ImGui_ImplDX10_InvalidateDeviceObjects();
  451. g_pd3dDevice = NULL;
  452. }
  453. void ImGui_ImplDX10_NewFrame()
  454. {
  455. if (!g_pFontSampler)
  456. ImGui_ImplDX10_CreateDeviceObjects();
  457. }
  458. //--------------------------------------------------------------------------------------------------------
  459. // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
  460. // This is an _advanced_ and _optional_ feature, allowing the back-end to create and handle multiple viewports simultaneously.
  461. // If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
  462. //--------------------------------------------------------------------------------------------------------
  463. struct ImGuiViewportDataDx10
  464. {
  465. IDXGISwapChain* SwapChain;
  466. ID3D10RenderTargetView* RTView;
  467. ImGuiViewportDataDx10() { SwapChain = NULL; RTView = NULL; }
  468. ~ImGuiViewportDataDx10() { IM_ASSERT(SwapChain == NULL && RTView == NULL); }
  469. };
  470. static void ImGui_ImplDX10_CreateWindow(ImGuiViewport* viewport)
  471. {
  472. ImGuiViewportDataDx10* data = IM_NEW(ImGuiViewportDataDx10)();
  473. viewport->RendererUserData = data;
  474. HWND hwnd = (HWND)viewport->PlatformHandle;
  475. IM_ASSERT(hwnd != 0);
  476. // Create swap chain
  477. DXGI_SWAP_CHAIN_DESC sd;
  478. ZeroMemory(&sd, sizeof(sd));
  479. sd.BufferDesc.Width = (UINT)viewport->Size.x;
  480. sd.BufferDesc.Height = (UINT)viewport->Size.y;
  481. sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  482. sd.SampleDesc.Count = 1;
  483. sd.SampleDesc.Quality = 0;
  484. sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  485. sd.BufferCount = 1;
  486. sd.OutputWindow = hwnd;
  487. sd.Windowed = TRUE;
  488. sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  489. sd.Flags = 0;
  490. IM_ASSERT(data->SwapChain == NULL && data->RTView == NULL);
  491. g_pFactory->CreateSwapChain(g_pd3dDevice, &sd, &data->SwapChain);
  492. // Create the render target
  493. if (data->SwapChain)
  494. {
  495. ID3D10Texture2D* pBackBuffer;
  496. data->SwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
  497. g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &data->RTView);
  498. pBackBuffer->Release();
  499. }
  500. }
  501. static void ImGui_ImplDX10_DestroyWindow(ImGuiViewport* viewport)
  502. {
  503. // The main viewport (owned by the application) will always have RendererUserData == NULL here since we didn't create the data for it.
  504. if (ImGuiViewportDataDx10* data = (ImGuiViewportDataDx10*)viewport->RendererUserData)
  505. {
  506. if (data->SwapChain)
  507. data->SwapChain->Release();
  508. data->SwapChain = NULL;
  509. if (data->RTView)
  510. data->RTView->Release();
  511. data->RTView = NULL;
  512. IM_DELETE(data);
  513. }
  514. viewport->RendererUserData = NULL;
  515. }
  516. static void ImGui_ImplDX10_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  517. {
  518. ImGuiViewportDataDx10* data = (ImGuiViewportDataDx10*)viewport->RendererUserData;
  519. if (data->RTView)
  520. {
  521. data->RTView->Release();
  522. data->RTView = NULL;
  523. }
  524. if (data->SwapChain)
  525. {
  526. ID3D10Texture2D* pBackBuffer = NULL;
  527. data->SwapChain->ResizeBuffers(0, (UINT)size.x, (UINT)size.y, DXGI_FORMAT_UNKNOWN, 0);
  528. data->SwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
  529. if (pBackBuffer == NULL) { fprintf(stderr, "ImGui_ImplDX10_SetWindowSize() failed creating buffers.\n"); return; }
  530. g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &data->RTView);
  531. pBackBuffer->Release();
  532. }
  533. }
  534. static void ImGui_ImplDX10_RenderViewport(ImGuiViewport* viewport, void*)
  535. {
  536. ImGuiViewportDataDx10* data = (ImGuiViewportDataDx10*)viewport->RendererUserData;
  537. ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);
  538. g_pd3dDevice->OMSetRenderTargets(1, &data->RTView, NULL);
  539. if (!(viewport->Flags & ImGuiViewportFlags_NoRendererClear))
  540. g_pd3dDevice->ClearRenderTargetView(data->RTView, (float*)&clear_color);
  541. ImGui_ImplDX10_RenderDrawData(viewport->DrawData);
  542. }
  543. static void ImGui_ImplDX10_SwapBuffers(ImGuiViewport* viewport, void*)
  544. {
  545. ImGuiViewportDataDx10* data = (ImGuiViewportDataDx10*)viewport->RendererUserData;
  546. data->SwapChain->Present(0, 0); // Present without vsync
  547. }
  548. void ImGui_ImplDX10_InitPlatformInterface()
  549. {
  550. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  551. platform_io.Renderer_CreateWindow = ImGui_ImplDX10_CreateWindow;
  552. platform_io.Renderer_DestroyWindow = ImGui_ImplDX10_DestroyWindow;
  553. platform_io.Renderer_SetWindowSize = ImGui_ImplDX10_SetWindowSize;
  554. platform_io.Renderer_RenderWindow = ImGui_ImplDX10_RenderViewport;
  555. platform_io.Renderer_SwapBuffers = ImGui_ImplDX10_SwapBuffers;
  556. }
  557. void ImGui_ImplDX10_ShutdownPlatformInterface()
  558. {
  559. ImGui::DestroyPlatformWindows();
  560. }