imgui_impl_dx10.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // ImGui Win32 + DirectX10 binding
  2. // In this binding, ImTextureID is used to store a 'ID3D10ShaderResourceView*' texture identifier. Read the FAQ about ImTextureID in imgui.cpp.
  3. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  4. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  5. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  6. // https://github.com/ocornut/imgui
  7. #include "imgui.h"
  8. #include "imgui_impl_dx10.h"
  9. // DirectX
  10. #include <d3d10_1.h>
  11. #include <d3d10.h>
  12. #include <d3dcompiler.h>
  13. #define DIRECTINPUT_VERSION 0x0800
  14. #include <dinput.h>
  15. // Data
  16. static INT64 g_Time = 0;
  17. static INT64 g_TicksPerSecond = 0;
  18. static HWND g_hWnd = 0;
  19. static ID3D10Device* g_pd3dDevice = NULL;
  20. static ID3D10Buffer* g_pVB = NULL;
  21. static ID3D10Buffer* g_pIB = NULL;
  22. static ID3D10Blob * g_pVertexShaderBlob = NULL;
  23. static ID3D10VertexShader* g_pVertexShader = NULL;
  24. static ID3D10InputLayout* g_pInputLayout = NULL;
  25. static ID3D10Buffer* g_pVertexConstantBuffer = NULL;
  26. static ID3D10Blob * g_pPixelShaderBlob = NULL;
  27. static ID3D10PixelShader* g_pPixelShader = NULL;
  28. static ID3D10SamplerState* g_pFontSampler = NULL;
  29. static ID3D10ShaderResourceView*g_pFontTextureView = NULL;
  30. static ID3D10RasterizerState* g_pRasterizerState = NULL;
  31. static ID3D10BlendState* g_pBlendState = NULL;
  32. static int g_VertexBufferSize = 5000, g_IndexBufferSize = 10000;
  33. struct VERTEX_CONSTANT_BUFFER
  34. {
  35. float mvp[4][4];
  36. };
  37. // This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
  38. // If text or lines are blurry when integrating ImGui in your engine:
  39. // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f)
  40. void ImGui_ImplDX10_RenderDrawLists(ImDrawData* draw_data)
  41. {
  42. ID3D10Device* ctx = g_pd3dDevice;
  43. // Create and grow vertex/index buffers if needed
  44. if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
  45. {
  46. if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
  47. g_VertexBufferSize = draw_data->TotalVtxCount + 5000;
  48. D3D10_BUFFER_DESC desc;
  49. memset(&desc, 0, sizeof(D3D10_BUFFER_DESC));
  50. desc.Usage = D3D10_USAGE_DYNAMIC;
  51. desc.ByteWidth = g_VertexBufferSize * sizeof(ImDrawVert);
  52. desc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
  53. desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
  54. desc.MiscFlags = 0;
  55. if (ctx->CreateBuffer(&desc, NULL, &g_pVB) < 0)
  56. return;
  57. }
  58. if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
  59. {
  60. if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
  61. g_IndexBufferSize = draw_data->TotalIdxCount + 10000;
  62. D3D10_BUFFER_DESC desc;
  63. memset(&desc, 0, sizeof(D3D10_BUFFER_DESC));
  64. desc.Usage = D3D10_USAGE_DYNAMIC;
  65. desc.ByteWidth = g_IndexBufferSize * sizeof(ImDrawIdx);
  66. desc.BindFlags = D3D10_BIND_INDEX_BUFFER;
  67. desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
  68. if (ctx->CreateBuffer(&desc, NULL, &g_pIB) < 0)
  69. return;
  70. }
  71. // Copy and convert all vertices into a single contiguous buffer
  72. ImDrawVert* vtx_dst = NULL;
  73. ImDrawIdx* idx_dst = NULL;
  74. g_pVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&vtx_dst);
  75. g_pIB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&idx_dst);
  76. for (int n = 0; n < draw_data->CmdListsCount; n++)
  77. {
  78. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  79. memcpy(vtx_dst, &cmd_list->VtxBuffer[0], cmd_list->VtxBuffer.size() * sizeof(ImDrawVert));
  80. memcpy(idx_dst, &cmd_list->IdxBuffer[0], cmd_list->IdxBuffer.size() * sizeof(ImDrawIdx));
  81. vtx_dst += cmd_list->VtxBuffer.size();
  82. idx_dst += cmd_list->IdxBuffer.size();
  83. }
  84. g_pVB->Unmap();
  85. g_pIB->Unmap();
  86. // Setup orthographic projection matrix into our constant buffer
  87. {
  88. void* mapped_resource;
  89. if (g_pVertexConstantBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &mapped_resource) != S_OK)
  90. return;
  91. VERTEX_CONSTANT_BUFFER* constant_buffer = (VERTEX_CONSTANT_BUFFER*)mapped_resource;
  92. const float L = 0.0f;
  93. const float R = ImGui::GetIO().DisplaySize.x;
  94. const float B = ImGui::GetIO().DisplaySize.y;
  95. const float T = 0.0f;
  96. const float mvp[4][4] =
  97. {
  98. { 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
  99. { 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
  100. { 0.0f, 0.0f, 0.5f, 0.0f },
  101. { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f },
  102. };
  103. memcpy(&constant_buffer->mvp, mvp, sizeof(mvp));
  104. g_pVertexConstantBuffer->Unmap();
  105. }
  106. // Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!)
  107. struct BACKUP_DX10_STATE
  108. {
  109. UINT ScissorRectsCount, ViewportsCount;
  110. D3D10_RECT ScissorRects[D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
  111. D3D10_VIEWPORT Viewports[D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE];
  112. ID3D10RasterizerState* RS;
  113. ID3D10BlendState* BlendState;
  114. FLOAT BlendFactor[4];
  115. UINT SampleMask;
  116. ID3D10ShaderResourceView* PSShaderResource;
  117. ID3D10SamplerState* PSSampler;
  118. ID3D10PixelShader* PS;
  119. ID3D10VertexShader* VS;
  120. D3D10_PRIMITIVE_TOPOLOGY PrimitiveTopology;
  121. ID3D10Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer;
  122. UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset;
  123. DXGI_FORMAT IndexBufferFormat;
  124. ID3D10InputLayout* InputLayout;
  125. };
  126. BACKUP_DX10_STATE old;
  127. old.ScissorRectsCount = old.ViewportsCount = D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE;
  128. ctx->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects);
  129. ctx->RSGetViewports(&old.ViewportsCount, old.Viewports);
  130. ctx->RSGetState(&old.RS);
  131. ctx->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask);
  132. ctx->PSGetShaderResources(0, 1, &old.PSShaderResource);
  133. ctx->PSGetSamplers(0, 1, &old.PSSampler);
  134. ctx->PSGetShader(&old.PS);
  135. ctx->VSGetShader(&old.VS);
  136. ctx->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer);
  137. ctx->IAGetPrimitiveTopology(&old.PrimitiveTopology);
  138. ctx->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset);
  139. ctx->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset);
  140. ctx->IAGetInputLayout(&old.InputLayout);
  141. // Setup viewport
  142. D3D10_VIEWPORT vp;
  143. memset(&vp, 0, sizeof(D3D10_VIEWPORT));
  144. vp.Width = (UINT)ImGui::GetIO().DisplaySize.x;
  145. vp.Height = (UINT)ImGui::GetIO().DisplaySize.y;
  146. vp.MinDepth = 0.0f;
  147. vp.MaxDepth = 1.0f;
  148. vp.TopLeftX = vp.TopLeftY = 0;
  149. ctx->RSSetViewports(1, &vp);
  150. // Bind shader and vertex buffers
  151. unsigned int stride = sizeof(ImDrawVert);
  152. unsigned int offset = 0;
  153. ctx->IASetInputLayout(g_pInputLayout);
  154. ctx->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset);
  155. ctx->IASetIndexBuffer(g_pIB, sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0);
  156. ctx->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  157. ctx->VSSetShader(g_pVertexShader);
  158. ctx->VSSetConstantBuffers(0, 1, &g_pVertexConstantBuffer);
  159. ctx->PSSetShader(g_pPixelShader);
  160. ctx->PSSetSamplers(0, 1, &g_pFontSampler);
  161. // Setup render state
  162. const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f };
  163. ctx->OMSetBlendState(g_pBlendState, blend_factor, 0xffffffff);
  164. ctx->RSSetState(g_pRasterizerState);
  165. // Render command lists
  166. int vtx_offset = 0;
  167. int idx_offset = 0;
  168. for (int n = 0; n < draw_data->CmdListsCount; n++)
  169. {
  170. const ImDrawList* cmd_list = draw_data->CmdLists[n];
  171. for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++)
  172. {
  173. const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
  174. if (pcmd->UserCallback)
  175. {
  176. pcmd->UserCallback(cmd_list, pcmd);
  177. }
  178. else
  179. {
  180. const D3D10_RECT r = { (LONG)pcmd->ClipRect.x, (LONG)pcmd->ClipRect.y, (LONG)pcmd->ClipRect.z, (LONG)pcmd->ClipRect.w };
  181. ctx->PSSetShaderResources(0, 1, (ID3D10ShaderResourceView**)&pcmd->TextureId);
  182. ctx->RSSetScissorRects(1, &r);
  183. ctx->DrawIndexed(pcmd->ElemCount, idx_offset, vtx_offset);
  184. }
  185. idx_offset += pcmd->ElemCount;
  186. }
  187. vtx_offset += cmd_list->VtxBuffer.size();
  188. }
  189. // Restore modified DX state
  190. ctx->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects);
  191. ctx->RSSetViewports(old.ViewportsCount, old.Viewports);
  192. ctx->RSSetState(old.RS); if (old.RS) old.RS->Release();
  193. ctx->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask); if (old.BlendState) old.BlendState->Release();
  194. ctx->PSSetShaderResources(0, 1, &old.PSShaderResource); if (old.PSShaderResource) old.PSShaderResource->Release();
  195. ctx->PSSetSamplers(0, 1, &old.PSSampler); if (old.PSSampler) old.PSSampler->Release();
  196. ctx->PSSetShader(old.PS); if (old.PS) old.PS->Release();
  197. ctx->VSSetShader(old.VS); if (old.VS) old.VS->Release();
  198. ctx->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer); if (old.VSConstantBuffer) old.VSConstantBuffer->Release();
  199. ctx->IASetPrimitiveTopology(old.PrimitiveTopology);
  200. ctx->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset); if (old.IndexBuffer) old.IndexBuffer->Release();
  201. ctx->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); if (old.VertexBuffer) old.VertexBuffer->Release();
  202. ctx->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release();
  203. }
  204. IMGUI_API LRESULT ImGui_ImplDX10_WndProcHandler(HWND, UINT msg, WPARAM wParam, LPARAM lParam)
  205. {
  206. ImGuiIO& io = ImGui::GetIO();
  207. switch (msg)
  208. {
  209. case WM_LBUTTONDOWN:
  210. io.MouseDown[0] = true;
  211. return true;
  212. case WM_LBUTTONUP:
  213. io.MouseDown[0] = false;
  214. return true;
  215. case WM_RBUTTONDOWN:
  216. io.MouseDown[1] = true;
  217. return true;
  218. case WM_RBUTTONUP:
  219. io.MouseDown[1] = false;
  220. return true;
  221. case WM_MBUTTONDOWN:
  222. io.MouseDown[2] = true;
  223. return true;
  224. case WM_MBUTTONUP:
  225. io.MouseDown[2] = false;
  226. return true;
  227. case WM_MOUSEWHEEL:
  228. io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f;
  229. return true;
  230. case WM_MOUSEMOVE:
  231. io.MousePos.x = (signed short)(lParam);
  232. io.MousePos.y = (signed short)(lParam >> 16);
  233. return true;
  234. case WM_KEYDOWN:
  235. if (wParam < 256)
  236. io.KeysDown[wParam] = 1;
  237. return true;
  238. case WM_KEYUP:
  239. if (wParam < 256)
  240. io.KeysDown[wParam] = 0;
  241. return true;
  242. case WM_CHAR:
  243. // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
  244. if (wParam > 0 && wParam < 0x10000)
  245. io.AddInputCharacter((unsigned short)wParam);
  246. return true;
  247. }
  248. return 0;
  249. }
  250. static void ImGui_ImplDX10_CreateFontsTexture()
  251. {
  252. ImGuiIO& io = ImGui::GetIO();
  253. // Build
  254. unsigned char* pixels;
  255. int width, height;
  256. io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
  257. // Create DX10 texture
  258. {
  259. D3D10_TEXTURE2D_DESC texDesc;
  260. ZeroMemory(&texDesc, sizeof(texDesc));
  261. texDesc.Width = width;
  262. texDesc.Height = height;
  263. texDesc.MipLevels = 1;
  264. texDesc.ArraySize = 1;
  265. texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  266. texDesc.SampleDesc.Count = 1;
  267. texDesc.Usage = D3D10_USAGE_DEFAULT;
  268. texDesc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
  269. texDesc.CPUAccessFlags = 0;
  270. ID3D10Texture2D *pTexture = NULL;
  271. D3D10_SUBRESOURCE_DATA subResource;
  272. subResource.pSysMem = pixels;
  273. subResource.SysMemPitch = texDesc.Width * 4;
  274. subResource.SysMemSlicePitch = 0;
  275. g_pd3dDevice->CreateTexture2D(&texDesc, &subResource, &pTexture);
  276. // Create texture view
  277. D3D10_SHADER_RESOURCE_VIEW_DESC srvDesc;
  278. ZeroMemory(&srvDesc, sizeof(srvDesc));
  279. srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  280. srvDesc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
  281. srvDesc.Texture2D.MipLevels = texDesc.MipLevels;
  282. srvDesc.Texture2D.MostDetailedMip = 0;
  283. g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &g_pFontTextureView);
  284. pTexture->Release();
  285. }
  286. // Store our identifier
  287. io.Fonts->TexID = (void *)g_pFontTextureView;
  288. // Create texture sampler
  289. {
  290. D3D10_SAMPLER_DESC samplerDesc;
  291. ZeroMemory(&samplerDesc, sizeof(samplerDesc));
  292. samplerDesc.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR;
  293. samplerDesc.AddressU = D3D10_TEXTURE_ADDRESS_WRAP;
  294. samplerDesc.AddressV = D3D10_TEXTURE_ADDRESS_WRAP;
  295. samplerDesc.AddressW = D3D10_TEXTURE_ADDRESS_WRAP;
  296. samplerDesc.MipLODBias = 0.f;
  297. samplerDesc.ComparisonFunc = D3D10_COMPARISON_ALWAYS;
  298. samplerDesc.MinLOD = 0.f;
  299. samplerDesc.MaxLOD = 0.f;
  300. g_pd3dDevice->CreateSamplerState(&samplerDesc, &g_pFontSampler);
  301. }
  302. // Cleanup (don't clear the input data if you want to append new fonts later)
  303. io.Fonts->ClearInputData();
  304. io.Fonts->ClearTexData();
  305. }
  306. bool ImGui_ImplDX10_CreateDeviceObjects()
  307. {
  308. if (!g_pd3dDevice)
  309. return false;
  310. if (g_pFontSampler)
  311. ImGui_ImplDX10_InvalidateDeviceObjects();
  312. // Create the vertex shader
  313. {
  314. static const char* vertexShader =
  315. "cbuffer vertexBuffer : register(b0) \
  316. {\
  317. float4x4 ProjectionMatrix; \
  318. };\
  319. struct VS_INPUT\
  320. {\
  321. float2 pos : POSITION;\
  322. float4 col : COLOR0;\
  323. float2 uv : TEXCOORD0;\
  324. };\
  325. \
  326. struct PS_INPUT\
  327. {\
  328. float4 pos : SV_POSITION;\
  329. float4 col : COLOR0;\
  330. float2 uv : TEXCOORD0;\
  331. };\
  332. \
  333. PS_INPUT main(VS_INPUT input)\
  334. {\
  335. PS_INPUT output;\
  336. output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
  337. output.col = input.col;\
  338. output.uv = input.uv;\
  339. return output;\
  340. }";
  341. D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_4_0", 0, 0, &g_pVertexShaderBlob, NULL);
  342. 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!
  343. return false;
  344. if (g_pd3dDevice->CreateVertexShader((DWORD*)g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pVertexShader) != S_OK)
  345. return false;
  346. // Create the input layout
  347. D3D10_INPUT_ELEMENT_DESC localLayout[] = {
  348. { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->pos), D3D10_INPUT_PER_VERTEX_DATA, 0 },
  349. { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->uv), D3D10_INPUT_PER_VERTEX_DATA, 0 },
  350. { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((ImDrawVert*)0)->col), D3D10_INPUT_PER_VERTEX_DATA, 0 },
  351. };
  352. if (g_pd3dDevice->CreateInputLayout(localLayout, 3, g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pInputLayout) != S_OK)
  353. return false;
  354. // Create the constant buffer
  355. {
  356. D3D10_BUFFER_DESC cbDesc;
  357. cbDesc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER);
  358. cbDesc.Usage = D3D10_USAGE_DYNAMIC;
  359. cbDesc.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
  360. cbDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
  361. cbDesc.MiscFlags = 0;
  362. g_pd3dDevice->CreateBuffer(&cbDesc, NULL, &g_pVertexConstantBuffer);
  363. }
  364. }
  365. // Create the pixel shader
  366. {
  367. static const char* pixelShader =
  368. "struct PS_INPUT\
  369. {\
  370. float4 pos : SV_POSITION;\
  371. float4 col : COLOR0;\
  372. float2 uv : TEXCOORD0;\
  373. };\
  374. sampler sampler0;\
  375. Texture2D texture0;\
  376. \
  377. float4 main(PS_INPUT input) : SV_Target\
  378. {\
  379. float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
  380. return out_col; \
  381. }";
  382. D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_4_0", 0, 0, &g_pPixelShaderBlob, NULL);
  383. 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!
  384. return false;
  385. if (g_pd3dDevice->CreatePixelShader((DWORD*)g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize(), &g_pPixelShader) != S_OK)
  386. return false;
  387. }
  388. // Create the blending setup
  389. {
  390. D3D10_BLEND_DESC desc;
  391. ZeroMemory(&desc, sizeof(desc));
  392. desc.AlphaToCoverageEnable = false;
  393. desc.BlendEnable[0] = true;
  394. desc.SrcBlend = D3D10_BLEND_SRC_ALPHA;
  395. desc.DestBlend = D3D10_BLEND_INV_SRC_ALPHA;
  396. desc.BlendOp = D3D10_BLEND_OP_ADD;
  397. desc.SrcBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA;
  398. desc.DestBlendAlpha = D3D10_BLEND_ZERO;
  399. desc.BlendOpAlpha = D3D10_BLEND_OP_ADD;
  400. desc.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL;
  401. g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState);
  402. }
  403. // Create the rasterizer state
  404. {
  405. D3D10_RASTERIZER_DESC desc;
  406. ZeroMemory(&desc, sizeof(desc));
  407. desc.FillMode = D3D10_FILL_SOLID;
  408. desc.CullMode = D3D10_CULL_NONE;
  409. desc.ScissorEnable = true;
  410. desc.DepthClipEnable = true;
  411. g_pd3dDevice->CreateRasterizerState(&desc, &g_pRasterizerState);
  412. }
  413. ImGui_ImplDX10_CreateFontsTexture();
  414. return true;
  415. }
  416. void ImGui_ImplDX10_InvalidateDeviceObjects()
  417. {
  418. if (!g_pd3dDevice)
  419. return;
  420. if (g_pFontSampler) { g_pFontSampler->Release(); g_pFontSampler = NULL; }
  421. if (g_pFontTextureView) { g_pFontTextureView->Release(); g_pFontTextureView = NULL; ImGui::GetIO().Fonts->TexID = 0; }
  422. if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
  423. if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
  424. if (g_pBlendState) { g_pBlendState->Release(); g_pBlendState = NULL; }
  425. if (g_pRasterizerState) { g_pRasterizerState->Release(); g_pRasterizerState = NULL; }
  426. if (g_pPixelShader) { g_pPixelShader->Release(); g_pPixelShader = NULL; }
  427. if (g_pPixelShaderBlob) { g_pPixelShaderBlob->Release(); g_pPixelShaderBlob = NULL; }
  428. if (g_pVertexConstantBuffer) { g_pVertexConstantBuffer->Release(); g_pVertexConstantBuffer = NULL; }
  429. if (g_pInputLayout) { g_pInputLayout->Release(); g_pInputLayout = NULL; }
  430. if (g_pVertexShader) { g_pVertexShader->Release(); g_pVertexShader = NULL; }
  431. if (g_pVertexShaderBlob) { g_pVertexShaderBlob->Release(); g_pVertexShaderBlob = NULL; }
  432. }
  433. bool ImGui_ImplDX10_Init(void* hwnd, ID3D10Device* device)
  434. {
  435. g_hWnd = (HWND)hwnd;
  436. g_pd3dDevice = device;
  437. if (!QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
  438. return false;
  439. if (!QueryPerformanceCounter((LARGE_INTEGER *)&g_Time))
  440. return false;
  441. ImGuiIO& io = ImGui::GetIO();
  442. io.KeyMap[ImGuiKey_Tab] = VK_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array that we will update during the application lifetime.
  443. io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
  444. io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
  445. io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
  446. io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
  447. io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
  448. io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
  449. io.KeyMap[ImGuiKey_Home] = VK_HOME;
  450. io.KeyMap[ImGuiKey_End] = VK_END;
  451. io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
  452. io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
  453. io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
  454. io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
  455. io.KeyMap[ImGuiKey_A] = 'A';
  456. io.KeyMap[ImGuiKey_C] = 'C';
  457. io.KeyMap[ImGuiKey_V] = 'V';
  458. io.KeyMap[ImGuiKey_X] = 'X';
  459. io.KeyMap[ImGuiKey_Y] = 'Y';
  460. io.KeyMap[ImGuiKey_Z] = 'Z';
  461. io.RenderDrawListsFn = ImGui_ImplDX10_RenderDrawLists; // Alternatively you can set this to NULL and call ImGui::GetDrawData() after ImGui::Render() to get the same ImDrawData pointer.
  462. io.ImeWindowHandle = g_hWnd;
  463. return true;
  464. }
  465. void ImGui_ImplDX10_Shutdown()
  466. {
  467. ImGui_ImplDX10_InvalidateDeviceObjects();
  468. ImGui::Shutdown();
  469. g_pd3dDevice = NULL;
  470. g_hWnd = (HWND)0;
  471. }
  472. void ImGui_ImplDX10_NewFrame()
  473. {
  474. if (!g_pFontSampler)
  475. ImGui_ImplDX10_CreateDeviceObjects();
  476. ImGuiIO& io = ImGui::GetIO();
  477. // Setup display size (every frame to accommodate for window resizing)
  478. RECT rect;
  479. GetClientRect(g_hWnd, &rect);
  480. io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
  481. // Setup time step
  482. INT64 current_time;
  483. QueryPerformanceCounter((LARGE_INTEGER *)&current_time);
  484. io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
  485. g_Time = current_time;
  486. // Read keyboard modifiers inputs
  487. io.KeyCtrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0;
  488. io.KeyShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0;
  489. io.KeyAlt = (GetKeyState(VK_MENU) & 0x8000) != 0;
  490. io.KeySuper = false;
  491. // io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events
  492. // io.MousePos : filled by WM_MOUSEMOVE events
  493. // io.MouseDown : filled by WM_*BUTTON* events
  494. // io.MouseWheel : filled by WM_MOUSEWHEEL events
  495. // Hide OS mouse cursor if ImGui is drawing it
  496. SetCursor(io.MouseDrawCursor ? NULL : LoadCursor(NULL, IDC_ARROW));
  497. // Start the frame
  498. ImGui::NewFrame();
  499. }