Debian Codegraph @

subprojects/imgui-1.91.6/backends/imgui_impl_dx10.cpp

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