| 1 |
// dear imgui: Renderer Backend for DirectX12 |
| 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 'D3D12_GPU_DESCRIPTOR_HANDLE' as ImTextureID. Read the FAQ about ImTextureID! |
| 6 |
// [X] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset). |
| 7 |
// [X] Renderer: Expose selected render state for draw callbacks to use. Access in '(ImGui_ImplXXXX_RenderState*)GetPlatformIO().Renderer_RenderState'. |
| 8 |
|
| 9 |
// The aim of imgui_impl_dx12.h/.cpp is to be usable in your engine without any modification. |
| 10 |
// IF YOU FEEL YOU NEED TO MAKE ANY CHANGE TO THIS CODE, please share them and your feedback at https://github.com/ocornut/imgui/ |
| 11 |
|
| 12 |
// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
| 13 |
// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
| 14 |
// Learn about Dear ImGui: |
| 15 |
// - FAQ https://dearimgui.com/faq |
| 16 |
// - Getting Started https://dearimgui.com/getting-started |
| 17 |
// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
| 18 |
// - Introduction, links and more at the top of imgui.cpp |
| 19 |
|
| 20 |
// CHANGELOG |
| 21 |
// (minor and older changes stripped away, please see git history for details) |
| 22 |
// 2024-12-09: DirectX12: Let user specifies the DepthStencilView format by setting ImGui_ImplDX12_InitInfo::DSVFormat. |
| 23 |
// 2024-11-15: DirectX12: *BREAKING CHANGE* Changed ImGui_ImplDX12_Init() signature to take a ImGui_ImplDX12_InitInfo struct. Legacy ImGui_ImplDX12_Init() signature is still supported (will obsolete). |
| 24 |
// 2024-11-15: DirectX12: *BREAKING CHANGE* User is now required to pass function pointers to allocate/free SRV Descriptors. We provide convenience legacy fields to pass a single descriptor, matching the old API, but upcoming features will want multiple. |
| 25 |
// 2024-10-23: DirectX12: Unmap() call specify written range. The range is informational and may be used by debug tools. |
| 26 |
// 2024-10-07: DirectX12: Changed default texture sampler to Clamp instead of Repeat/Wrap. |
| 27 |
// 2024-10-07: DirectX12: Expose selected render state in ImGui_ImplDX12_RenderState, which you can access in 'void* platform_io.Renderer_RenderState' during draw callbacks. |
| 28 |
// 2024-10-07: DirectX12: Compiling with '#define ImTextureID=ImU64' is unnecessary now that dear imgui defaults ImTextureID to u64 instead of void*. |
| 29 |
// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. |
| 30 |
// 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). |
| 31 |
// 2021-05-19: DirectX12: Replaced direct access to ImDrawCmd::TextureId with a call to ImDrawCmd::GetTexID(). (will become a requirement) |
| 32 |
// 2021-02-18: DirectX12: Change blending equation to preserve alpha in output buffer. |
| 33 |
// 2021-01-11: DirectX12: Improve Windows 7 compatibility (for D3D12On7) by loading d3d12.dll dynamically. |
| 34 |
// 2020-09-16: DirectX12: Avoid rendering calls with zero-sized scissor rectangle since it generates a validation layer warning. |
| 35 |
// 2020-09-08: DirectX12: Clarified support for building on 32-bit systems by redefining ImTextureID. |
| 36 |
// 2019-10-18: DirectX12: *BREAKING CHANGE* Added extra ID3D12DescriptorHeap parameter to ImGui_ImplDX12_Init() function. |
| 37 |
// 2019-05-29: DirectX12: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag. |
| 38 |
// 2019-04-30: DirectX12: Added support for special ImDrawCallback_ResetRenderState callback to reset render state. |
| 39 |
// 2019-03-29: Misc: Various minor tidying up. |
| 40 |
// 2018-12-03: Misc: Added #pragma comment statement to automatically link with d3dcompiler.lib when using D3DCompile(). |
| 41 |
// 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window. |
| 42 |
// 2018-06-12: DirectX12: Moved the ID3D12GraphicsCommandList* parameter from NewFrame() to RenderDrawData(). |
| 43 |
// 2018-06-08: Misc: Extracted imgui_impl_dx12.cpp/.h away from the old combined DX12+Win32 example. |
| 44 |
// 2018-06-08: DirectX12: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle (to ease support for future multi-viewport). |
| 45 |
// 2018-02-22: Merged into master with all Win32 code synchronized to other examples. |
| 46 |
|
| 47 |
#include "imgui.h" |
| 48 |
#ifndef IMGUI_DISABLE |
| 49 |
#include "imgui_impl_dx12.h" |
| 50 |
|
| 51 |
// DirectX |
| 52 |
#include <d3d12.h> |
| 53 |
#include <dxgi1_4.h> |
| 54 |
#include <d3dcompiler.h> |
| 55 |
#ifdef _MSC_VER |
| 56 |
#pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below. |
| 57 |
#endif |
| 58 |
|
| 59 |
// DirectX12 data |
| 60 |
struct ImGui_ImplDX12_RenderBuffers; |
| 61 |
|
| 62 |
struct ImGui_ImplDX12_Texture |
| 63 |
{ |
| 64 |
ID3D12Resource* pTextureResource; |
| 65 |
D3D12_CPU_DESCRIPTOR_HANDLE hFontSrvCpuDescHandle; |
| 66 |
D3D12_GPU_DESCRIPTOR_HANDLE hFontSrvGpuDescHandle; |
| 67 |
}; |
| 68 |
|
| 69 |
struct ImGui_ImplDX12_Data |
| 70 |
{ |
| 71 |
ImGui_ImplDX12_InitInfo InitInfo; |
| 72 |
ID3D12Device* pd3dDevice; |
| 73 |
ID3D12RootSignature* pRootSignature; |
| 74 |
ID3D12PipelineState* pPipelineState; |
| 75 |
DXGI_FORMAT RTVFormat; |
| 76 |
DXGI_FORMAT DSVFormat; |
| 77 |
ID3D12DescriptorHeap* pd3dSrvDescHeap; |
| 78 |
UINT numFramesInFlight; |
| 79 |
|
| 80 |
ImGui_ImplDX12_RenderBuffers* pFrameResources; |
| 81 |
UINT frameIndex; |
| 82 |
|
| 83 |
ImGui_ImplDX12_Texture FontTexture; |
| 84 |
bool LegacySingleDescriptorUsed; |
| 85 |
|
| 86 |
ImGui_ImplDX12_Data() { memset((void*)this, 0, sizeof(*this)); frameIndex = UINT_MAX; } |
| 87 |
}; |
| 88 |
|
| 89 |
// Backend data stored in io.BackendRendererUserData to allow support for multiple Dear ImGui contexts |
| 90 |
// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. |
| 91 |
static ImGui_ImplDX12_Data* ImGui_ImplDX12_GetBackendData() |
| 92 |
{ |
| 93 |
return ImGui::GetCurrentContext() ? (ImGui_ImplDX12_Data*)ImGui::GetIO().BackendRendererUserData : nullptr; |
| 94 |
} |
| 95 |
|
| 96 |
// Buffers used during the rendering of a frame |
| 97 |
struct ImGui_ImplDX12_RenderBuffers |
| 98 |
{ |
| 99 |
ID3D12Resource* IndexBuffer; |
| 100 |
ID3D12Resource* VertexBuffer; |
| 101 |
int IndexBufferSize; |
| 102 |
int VertexBufferSize; |
| 103 |
}; |
| 104 |
|
| 105 |
struct VERTEX_CONSTANT_BUFFER_DX12 |
| 106 |
{ |
| 107 |
float mvp[4][4]; |
| 108 |
}; |
| 109 |
|
| 110 |
// Functions |
| 111 |
static void ImGui_ImplDX12_SetupRenderState(ImDrawData* draw_data, ID3D12GraphicsCommandList* command_list, ImGui_ImplDX12_RenderBuffers* fr) |
| 112 |
{ |
| 113 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 114 |
|
| 115 |
// Setup orthographic projection matrix into our constant buffer |
| 116 |
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). |
| 117 |
VERTEX_CONSTANT_BUFFER_DX12 vertex_constant_buffer; |
| 118 |
{ |
| 119 |
float L = draw_data->DisplayPos.x; |
| 120 |
float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; |
| 121 |
float T = draw_data->DisplayPos.y; |
| 122 |
float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y; |
| 123 |
float mvp[4][4] = |
| 124 |
{ |
| 125 |
{ 2.0f/(R-L), 0.0f, 0.0f, 0.0f }, |
| 126 |
{ 0.0f, 2.0f/(T-B), 0.0f, 0.0f }, |
| 127 |
{ 0.0f, 0.0f, 0.5f, 0.0f }, |
| 128 |
{ (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f }, |
| 129 |
}; |
| 130 |
memcpy(&vertex_constant_buffer.mvp, mvp, sizeof(mvp)); |
| 131 |
} |
| 132 |
|
| 133 |
// Setup viewport |
| 134 |
D3D12_VIEWPORT vp; |
| 135 |
memset(&vp, 0, sizeof(D3D12_VIEWPORT)); |
| 136 |
vp.Width = draw_data->DisplaySize.x; |
| 137 |
vp.Height = draw_data->DisplaySize.y; |
| 138 |
vp.MinDepth = 0.0f; |
| 139 |
vp.MaxDepth = 1.0f; |
| 140 |
vp.TopLeftX = vp.TopLeftY = 0.0f; |
| 141 |
command_list->RSSetViewports(1, &vp); |
| 142 |
|
| 143 |
// Bind shader and vertex buffers |
| 144 |
unsigned int stride = sizeof(ImDrawVert); |
| 145 |
unsigned int offset = 0; |
| 146 |
D3D12_VERTEX_BUFFER_VIEW vbv; |
| 147 |
memset(&vbv, 0, sizeof(D3D12_VERTEX_BUFFER_VIEW)); |
| 148 |
vbv.BufferLocation = fr->VertexBuffer->GetGPUVirtualAddress() + offset; |
| 149 |
vbv.SizeInBytes = fr->VertexBufferSize * stride; |
| 150 |
vbv.StrideInBytes = stride; |
| 151 |
command_list->IASetVertexBuffers(0, 1, &vbv); |
| 152 |
D3D12_INDEX_BUFFER_VIEW ibv; |
| 153 |
memset(&ibv, 0, sizeof(D3D12_INDEX_BUFFER_VIEW)); |
| 154 |
ibv.BufferLocation = fr->IndexBuffer->GetGPUVirtualAddress(); |
| 155 |
ibv.SizeInBytes = fr->IndexBufferSize * sizeof(ImDrawIdx); |
| 156 |
ibv.Format = sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT; |
| 157 |
command_list->IASetIndexBuffer(&ibv); |
| 158 |
command_list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); |
| 159 |
command_list->SetPipelineState(bd->pPipelineState); |
| 160 |
command_list->SetGraphicsRootSignature(bd->pRootSignature); |
| 161 |
command_list->SetGraphicsRoot32BitConstants(0, 16, &vertex_constant_buffer, 0); |
| 162 |
|
| 163 |
// Setup blend factor |
| 164 |
const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f }; |
| 165 |
command_list->OMSetBlendFactor(blend_factor); |
| 166 |
} |
| 167 |
|
| 168 |
template<typename T> |
| 169 |
static inline void SafeRelease(T*& res) |
| 170 |
{ |
| 171 |
if (res) |
| 172 |
res->Release(); |
| 173 |
res = nullptr; |
| 174 |
} |
| 175 |
|
| 176 |
// Render function |
| 177 |
void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data, ID3D12GraphicsCommandList* command_list) |
| 178 |
{ |
| 179 |
// Avoid rendering when minimized |
| 180 |
if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f) |
| 181 |
return; |
| 182 |
|
| 183 |
// FIXME: I'm assuming that this only gets called once per frame! |
| 184 |
// If not, we can't just re-allocate the IB or VB, we'll have to do a proper allocator. |
| 185 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 186 |
bd->frameIndex = bd->frameIndex + 1; |
| 187 |
ImGui_ImplDX12_RenderBuffers* fr = &bd->pFrameResources[bd->frameIndex % bd->numFramesInFlight]; |
| 188 |
|
| 189 |
// Create and grow vertex/index buffers if needed |
| 190 |
if (fr->VertexBuffer == nullptr || fr->VertexBufferSize < draw_data->TotalVtxCount) |
| 191 |
{ |
| 192 |
SafeRelease(fr->VertexBuffer); |
| 193 |
fr->VertexBufferSize = draw_data->TotalVtxCount + 5000; |
| 194 |
D3D12_HEAP_PROPERTIES props; |
| 195 |
memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES)); |
| 196 |
props.Type = D3D12_HEAP_TYPE_UPLOAD; |
| 197 |
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; |
| 198 |
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; |
| 199 |
D3D12_RESOURCE_DESC desc; |
| 200 |
memset(&desc, 0, sizeof(D3D12_RESOURCE_DESC)); |
| 201 |
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; |
| 202 |
desc.Width = fr->VertexBufferSize * sizeof(ImDrawVert); |
| 203 |
desc.Height = 1; |
| 204 |
desc.DepthOrArraySize = 1; |
| 205 |
desc.MipLevels = 1; |
| 206 |
desc.Format = DXGI_FORMAT_UNKNOWN; |
| 207 |
desc.SampleDesc.Count = 1; |
| 208 |
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; |
| 209 |
desc.Flags = D3D12_RESOURCE_FLAG_NONE; |
| 210 |
if (bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&fr->VertexBuffer)) < 0) |
| 211 |
return; |
| 212 |
} |
| 213 |
if (fr->IndexBuffer == nullptr || fr->IndexBufferSize < draw_data->TotalIdxCount) |
| 214 |
{ |
| 215 |
SafeRelease(fr->IndexBuffer); |
| 216 |
fr->IndexBufferSize = draw_data->TotalIdxCount + 10000; |
| 217 |
D3D12_HEAP_PROPERTIES props; |
| 218 |
memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES)); |
| 219 |
props.Type = D3D12_HEAP_TYPE_UPLOAD; |
| 220 |
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; |
| 221 |
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; |
| 222 |
D3D12_RESOURCE_DESC desc; |
| 223 |
memset(&desc, 0, sizeof(D3D12_RESOURCE_DESC)); |
| 224 |
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; |
| 225 |
desc.Width = fr->IndexBufferSize * sizeof(ImDrawIdx); |
| 226 |
desc.Height = 1; |
| 227 |
desc.DepthOrArraySize = 1; |
| 228 |
desc.MipLevels = 1; |
| 229 |
desc.Format = DXGI_FORMAT_UNKNOWN; |
| 230 |
desc.SampleDesc.Count = 1; |
| 231 |
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; |
| 232 |
desc.Flags = D3D12_RESOURCE_FLAG_NONE; |
| 233 |
if (bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&fr->IndexBuffer)) < 0) |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
// Upload vertex/index data into a single contiguous GPU buffer |
| 238 |
// During Map() we specify a null read range (as per DX12 API, this is informational and for tooling only) |
| 239 |
void* vtx_resource, *idx_resource; |
| 240 |
D3D12_RANGE range = { 0, 0 }; |
| 241 |
if (fr->VertexBuffer->Map(0, &range, &vtx_resource) != S_OK) |
| 242 |
return; |
| 243 |
if (fr->IndexBuffer->Map(0, &range, &idx_resource) != S_OK) |
| 244 |
return; |
| 245 |
ImDrawVert* vtx_dst = (ImDrawVert*)vtx_resource; |
| 246 |
ImDrawIdx* idx_dst = (ImDrawIdx*)idx_resource; |
| 247 |
for (int n = 0; n < draw_data->CmdListsCount; n++) |
| 248 |
{ |
| 249 |
const ImDrawList* draw_list = draw_data->CmdLists[n]; |
| 250 |
memcpy(vtx_dst, draw_list->VtxBuffer.Data, draw_list->VtxBuffer.Size * sizeof(ImDrawVert)); |
| 251 |
memcpy(idx_dst, draw_list->IdxBuffer.Data, draw_list->IdxBuffer.Size * sizeof(ImDrawIdx)); |
| 252 |
vtx_dst += draw_list->VtxBuffer.Size; |
| 253 |
idx_dst += draw_list->IdxBuffer.Size; |
| 254 |
} |
| 255 |
|
| 256 |
// During Unmap() we specify the written range (as per DX12 API, this is informational and for tooling only) |
| 257 |
range.End = (SIZE_T)((intptr_t)vtx_dst - (intptr_t)vtx_resource); |
| 258 |
IM_ASSERT(range.End == draw_data->TotalVtxCount * sizeof(ImDrawVert)); |
| 259 |
fr->VertexBuffer->Unmap(0, &range); |
| 260 |
range.End = (SIZE_T)((intptr_t)idx_dst - (intptr_t)idx_resource); |
| 261 |
IM_ASSERT(range.End == draw_data->TotalIdxCount * sizeof(ImDrawIdx)); |
| 262 |
fr->IndexBuffer->Unmap(0, &range); |
| 263 |
|
| 264 |
// Setup desired DX state |
| 265 |
ImGui_ImplDX12_SetupRenderState(draw_data, command_list, fr); |
| 266 |
|
| 267 |
// Setup render state structure (for callbacks and custom texture bindings) |
| 268 |
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 269 |
ImGui_ImplDX12_RenderState render_state; |
| 270 |
render_state.Device = bd->pd3dDevice; |
| 271 |
render_state.CommandList = command_list; |
| 272 |
platform_io.Renderer_RenderState = &render_state; |
| 273 |
|
| 274 |
// Render command lists |
| 275 |
// (Because we merged all buffers into a single one, we maintain our own offset into them) |
| 276 |
int global_vtx_offset = 0; |
| 277 |
int global_idx_offset = 0; |
| 278 |
ImVec2 clip_off = draw_data->DisplayPos; |
| 279 |
for (int n = 0; n < draw_data->CmdListsCount; n++) |
| 280 |
{ |
| 281 |
const ImDrawList* draw_list = draw_data->CmdLists[n]; |
| 282 |
for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++) |
| 283 |
{ |
| 284 |
const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i]; |
| 285 |
if (pcmd->UserCallback != nullptr) |
| 286 |
{ |
| 287 |
// User callback, registered via ImDrawList::AddCallback() |
| 288 |
// (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.) |
| 289 |
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) |
| 290 |
ImGui_ImplDX12_SetupRenderState(draw_data, command_list, fr); |
| 291 |
else |
| 292 |
pcmd->UserCallback(draw_list, pcmd); |
| 293 |
} |
| 294 |
else |
| 295 |
{ |
| 296 |
// Project scissor/clipping rectangles into framebuffer space |
| 297 |
ImVec2 clip_min(pcmd->ClipRect.x - clip_off.x, pcmd->ClipRect.y - clip_off.y); |
| 298 |
ImVec2 clip_max(pcmd->ClipRect.z - clip_off.x, pcmd->ClipRect.w - clip_off.y); |
| 299 |
if (clip_max.x <= clip_min.x || clip_max.y <= clip_min.y) |
| 300 |
continue; |
| 301 |
|
| 302 |
// Apply scissor/clipping rectangle |
| 303 |
const D3D12_RECT r = { (LONG)clip_min.x, (LONG)clip_min.y, (LONG)clip_max.x, (LONG)clip_max.y }; |
| 304 |
command_list->RSSetScissorRects(1, &r); |
| 305 |
|
| 306 |
// Bind texture, Draw |
| 307 |
D3D12_GPU_DESCRIPTOR_HANDLE texture_handle = {}; |
| 308 |
texture_handle.ptr = (UINT64)pcmd->GetTexID(); |
| 309 |
command_list->SetGraphicsRootDescriptorTable(1, texture_handle); |
| 310 |
command_list->DrawIndexedInstanced(pcmd->ElemCount, 1, pcmd->IdxOffset + global_idx_offset, pcmd->VtxOffset + global_vtx_offset, 0); |
| 311 |
} |
| 312 |
} |
| 313 |
global_idx_offset += draw_list->IdxBuffer.Size; |
| 314 |
global_vtx_offset += draw_list->VtxBuffer.Size; |
| 315 |
} |
| 316 |
platform_io.Renderer_RenderState = nullptr; |
| 317 |
} |
| 318 |
|
| 319 |
static void ImGui_ImplDX12_CreateFontsTexture() |
| 320 |
{ |
| 321 |
// Build texture atlas |
| 322 |
ImGuiIO& io = ImGui::GetIO(); |
| 323 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 324 |
unsigned char* pixels; |
| 325 |
int width, height; |
| 326 |
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); |
| 327 |
|
| 328 |
// Upload texture to graphics system |
| 329 |
ImGui_ImplDX12_Texture* font_tex = &bd->FontTexture; |
| 330 |
{ |
| 331 |
D3D12_HEAP_PROPERTIES props; |
| 332 |
memset(&props, 0, sizeof(D3D12_HEAP_PROPERTIES)); |
| 333 |
props.Type = D3D12_HEAP_TYPE_DEFAULT; |
| 334 |
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; |
| 335 |
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; |
| 336 |
|
| 337 |
D3D12_RESOURCE_DESC desc; |
| 338 |
ZeroMemory(&desc, sizeof(desc)); |
| 339 |
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; |
| 340 |
desc.Alignment = 0; |
| 341 |
desc.Width = width; |
| 342 |
desc.Height = height; |
| 343 |
desc.DepthOrArraySize = 1; |
| 344 |
desc.MipLevels = 1; |
| 345 |
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| 346 |
desc.SampleDesc.Count = 1; |
| 347 |
desc.SampleDesc.Quality = 0; |
| 348 |
desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; |
| 349 |
desc.Flags = D3D12_RESOURCE_FLAG_NONE; |
| 350 |
|
| 351 |
ID3D12Resource* pTexture = nullptr; |
| 352 |
bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, |
| 353 |
D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&pTexture)); |
| 354 |
|
| 355 |
UINT uploadPitch = (width * 4 + D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u) & ~(D3D12_TEXTURE_DATA_PITCH_ALIGNMENT - 1u); |
| 356 |
UINT uploadSize = height * uploadPitch; |
| 357 |
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; |
| 358 |
desc.Alignment = 0; |
| 359 |
desc.Width = uploadSize; |
| 360 |
desc.Height = 1; |
| 361 |
desc.DepthOrArraySize = 1; |
| 362 |
desc.MipLevels = 1; |
| 363 |
desc.Format = DXGI_FORMAT_UNKNOWN; |
| 364 |
desc.SampleDesc.Count = 1; |
| 365 |
desc.SampleDesc.Quality = 0; |
| 366 |
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; |
| 367 |
desc.Flags = D3D12_RESOURCE_FLAG_NONE; |
| 368 |
|
| 369 |
props.Type = D3D12_HEAP_TYPE_UPLOAD; |
| 370 |
props.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; |
| 371 |
props.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; |
| 372 |
|
| 373 |
ID3D12Resource* uploadBuffer = nullptr; |
| 374 |
HRESULT hr = bd->pd3dDevice->CreateCommittedResource(&props, D3D12_HEAP_FLAG_NONE, &desc, |
| 375 |
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&uploadBuffer)); |
| 376 |
IM_ASSERT(SUCCEEDED(hr)); |
| 377 |
|
| 378 |
void* mapped = nullptr; |
| 379 |
D3D12_RANGE range = { 0, uploadSize }; |
| 380 |
hr = uploadBuffer->Map(0, &range, &mapped); |
| 381 |
IM_ASSERT(SUCCEEDED(hr)); |
| 382 |
for (int y = 0; y < height; y++) |
| 383 |
memcpy((void*) ((uintptr_t) mapped + y * uploadPitch), pixels + y * width * 4, width * 4); |
| 384 |
uploadBuffer->Unmap(0, &range); |
| 385 |
|
| 386 |
D3D12_TEXTURE_COPY_LOCATION srcLocation = {}; |
| 387 |
srcLocation.pResource = uploadBuffer; |
| 388 |
srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; |
| 389 |
srcLocation.PlacedFootprint.Footprint.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| 390 |
srcLocation.PlacedFootprint.Footprint.Width = width; |
| 391 |
srcLocation.PlacedFootprint.Footprint.Height = height; |
| 392 |
srcLocation.PlacedFootprint.Footprint.Depth = 1; |
| 393 |
srcLocation.PlacedFootprint.Footprint.RowPitch = uploadPitch; |
| 394 |
|
| 395 |
D3D12_TEXTURE_COPY_LOCATION dstLocation = {}; |
| 396 |
dstLocation.pResource = pTexture; |
| 397 |
dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; |
| 398 |
dstLocation.SubresourceIndex = 0; |
| 399 |
|
| 400 |
D3D12_RESOURCE_BARRIER barrier = {}; |
| 401 |
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; |
| 402 |
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE; |
| 403 |
barrier.Transition.pResource = pTexture; |
| 404 |
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; |
| 405 |
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST; |
| 406 |
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE; |
| 407 |
|
| 408 |
ID3D12Fence* fence = nullptr; |
| 409 |
hr = bd->pd3dDevice->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)); |
| 410 |
IM_ASSERT(SUCCEEDED(hr)); |
| 411 |
|
| 412 |
HANDLE event = CreateEvent(0, 0, 0, 0); |
| 413 |
IM_ASSERT(event != nullptr); |
| 414 |
|
| 415 |
D3D12_COMMAND_QUEUE_DESC queueDesc = {}; |
| 416 |
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; |
| 417 |
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE; |
| 418 |
queueDesc.NodeMask = 1; |
| 419 |
|
| 420 |
ID3D12CommandQueue* cmdQueue = nullptr; |
| 421 |
hr = bd->pd3dDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&cmdQueue)); |
| 422 |
IM_ASSERT(SUCCEEDED(hr)); |
| 423 |
|
| 424 |
ID3D12CommandAllocator* cmdAlloc = nullptr; |
| 425 |
hr = bd->pd3dDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&cmdAlloc)); |
| 426 |
IM_ASSERT(SUCCEEDED(hr)); |
| 427 |
|
| 428 |
ID3D12GraphicsCommandList* cmdList = nullptr; |
| 429 |
hr = bd->pd3dDevice->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, cmdAlloc, nullptr, IID_PPV_ARGS(&cmdList)); |
| 430 |
IM_ASSERT(SUCCEEDED(hr)); |
| 431 |
|
| 432 |
cmdList->CopyTextureRegion(&dstLocation, 0, 0, 0, &srcLocation, nullptr); |
| 433 |
cmdList->ResourceBarrier(1, &barrier); |
| 434 |
|
| 435 |
hr = cmdList->Close(); |
| 436 |
IM_ASSERT(SUCCEEDED(hr)); |
| 437 |
|
| 438 |
cmdQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*)&cmdList); |
| 439 |
hr = cmdQueue->Signal(fence, 1); |
| 440 |
IM_ASSERT(SUCCEEDED(hr)); |
| 441 |
|
| 442 |
fence->SetEventOnCompletion(1, event); |
| 443 |
WaitForSingleObject(event, INFINITE); |
| 444 |
|
| 445 |
cmdList->Release(); |
| 446 |
cmdAlloc->Release(); |
| 447 |
cmdQueue->Release(); |
| 448 |
CloseHandle(event); |
| 449 |
fence->Release(); |
| 450 |
uploadBuffer->Release(); |
| 451 |
|
| 452 |
// Create texture view |
| 453 |
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc; |
| 454 |
ZeroMemory(&srvDesc, sizeof(srvDesc)); |
| 455 |
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| 456 |
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; |
| 457 |
srvDesc.Texture2D.MipLevels = desc.MipLevels; |
| 458 |
srvDesc.Texture2D.MostDetailedMip = 0; |
| 459 |
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; |
| 460 |
bd->pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, font_tex->hFontSrvCpuDescHandle); |
| 461 |
SafeRelease(font_tex->pTextureResource); |
| 462 |
font_tex->pTextureResource = pTexture; |
| 463 |
} |
| 464 |
|
| 465 |
// Store our identifier |
| 466 |
io.Fonts->SetTexID((ImTextureID)font_tex->hFontSrvGpuDescHandle.ptr); |
| 467 |
} |
| 468 |
|
| 469 |
bool ImGui_ImplDX12_CreateDeviceObjects() |
| 470 |
{ |
| 471 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 472 |
if (!bd || !bd->pd3dDevice) |
| 473 |
return false; |
| 474 |
if (bd->pPipelineState) |
| 475 |
ImGui_ImplDX12_InvalidateDeviceObjects(); |
| 476 |
|
| 477 |
// Create the root signature |
| 478 |
{ |
| 479 |
D3D12_DESCRIPTOR_RANGE descRange = {}; |
| 480 |
descRange.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV; |
| 481 |
descRange.NumDescriptors = 1; |
| 482 |
descRange.BaseShaderRegister = 0; |
| 483 |
descRange.RegisterSpace = 0; |
| 484 |
descRange.OffsetInDescriptorsFromTableStart = 0; |
| 485 |
|
| 486 |
D3D12_ROOT_PARAMETER param[2] = {}; |
| 487 |
|
| 488 |
param[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS; |
| 489 |
param[0].Constants.ShaderRegister = 0; |
| 490 |
param[0].Constants.RegisterSpace = 0; |
| 491 |
param[0].Constants.Num32BitValues = 16; |
| 492 |
param[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX; |
| 493 |
|
| 494 |
param[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; |
| 495 |
param[1].DescriptorTable.NumDescriptorRanges = 1; |
| 496 |
param[1].DescriptorTable.pDescriptorRanges = &descRange; |
| 497 |
param[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; |
| 498 |
|
| 499 |
// Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling. |
| 500 |
D3D12_STATIC_SAMPLER_DESC staticSampler = {}; |
| 501 |
staticSampler.Filter = D3D12_FILTER_MIN_MAG_MIP_LINEAR; |
| 502 |
staticSampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; |
| 503 |
staticSampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; |
| 504 |
staticSampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_CLAMP; |
| 505 |
staticSampler.MipLODBias = 0.f; |
| 506 |
staticSampler.MaxAnisotropy = 0; |
| 507 |
staticSampler.ComparisonFunc = D3D12_COMPARISON_FUNC_ALWAYS; |
| 508 |
staticSampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK; |
| 509 |
staticSampler.MinLOD = 0.f; |
| 510 |
staticSampler.MaxLOD = 0.f; |
| 511 |
staticSampler.ShaderRegister = 0; |
| 512 |
staticSampler.RegisterSpace = 0; |
| 513 |
staticSampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL; |
| 514 |
|
| 515 |
D3D12_ROOT_SIGNATURE_DESC desc = {}; |
| 516 |
desc.NumParameters = _countof(param); |
| 517 |
desc.pParameters = param; |
| 518 |
desc.NumStaticSamplers = 1; |
| 519 |
desc.pStaticSamplers = &staticSampler; |
| 520 |
desc.Flags = |
| 521 |
D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT | |
| 522 |
D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS | |
| 523 |
D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS | |
| 524 |
D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS; |
| 525 |
|
| 526 |
// Load d3d12.dll and D3D12SerializeRootSignature() function address dynamically to facilitate using with D3D12On7. |
| 527 |
// See if any version of d3d12.dll is already loaded in the process. If so, give preference to that. |
| 528 |
static HINSTANCE d3d12_dll = ::GetModuleHandleA("d3d12.dll"); |
| 529 |
if (d3d12_dll == nullptr) |
| 530 |
{ |
| 531 |
// Attempt to load d3d12.dll from local directories. This will only succeed if |
| 532 |
// (1) the current OS is Windows 7, and |
| 533 |
// (2) there exists a version of d3d12.dll for Windows 7 (D3D12On7) in one of the following directories. |
| 534 |
// See https://github.com/ocornut/imgui/pull/3696 for details. |
| 535 |
const char* localD3d12Paths[] = { ".\\d3d12.dll", ".\\d3d12on7\\d3d12.dll", ".\\12on7\\d3d12.dll" }; // A. current directory, B. used by some games, C. used in Microsoft D3D12On7 sample |
| 536 |
for (int i = 0; i < IM_ARRAYSIZE(localD3d12Paths); i++) |
| 537 |
if ((d3d12_dll = ::LoadLibraryA(localD3d12Paths[i])) != nullptr) |
| 538 |
break; |
| 539 |
|
| 540 |
// If failed, we are on Windows >= 10. |
| 541 |
if (d3d12_dll == nullptr) |
| 542 |
d3d12_dll = ::LoadLibraryA("d3d12.dll"); |
| 543 |
|
| 544 |
if (d3d12_dll == nullptr) |
| 545 |
return false; |
| 546 |
} |
| 547 |
|
| 548 |
PFN_D3D12_SERIALIZE_ROOT_SIGNATURE D3D12SerializeRootSignatureFn = (PFN_D3D12_SERIALIZE_ROOT_SIGNATURE)::GetProcAddress(d3d12_dll, "D3D12SerializeRootSignature"); |
| 549 |
if (D3D12SerializeRootSignatureFn == nullptr) |
| 550 |
return false; |
| 551 |
|
| 552 |
ID3DBlob* blob = nullptr; |
| 553 |
if (D3D12SerializeRootSignatureFn(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, nullptr) != S_OK) |
| 554 |
return false; |
| 555 |
|
| 556 |
bd->pd3dDevice->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&bd->pRootSignature)); |
| 557 |
blob->Release(); |
| 558 |
} |
| 559 |
|
| 560 |
// By using D3DCompile() from <d3dcompiler.h> / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A) |
| 561 |
// If you would like to use this DX12 sample code but remove this dependency you can: |
| 562 |
// 1) compile once, save the compiled shader blobs into a file or source code and assign them to psoDesc.VS/PS [preferred solution] |
| 563 |
// 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL. |
| 564 |
// See https://github.com/ocornut/imgui/pull/638 for sources and details. |
| 565 |
|
| 566 |
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc; |
| 567 |
memset(&psoDesc, 0, sizeof(D3D12_GRAPHICS_PIPELINE_STATE_DESC)); |
| 568 |
psoDesc.NodeMask = 1; |
| 569 |
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; |
| 570 |
psoDesc.pRootSignature = bd->pRootSignature; |
| 571 |
psoDesc.SampleMask = UINT_MAX; |
| 572 |
psoDesc.NumRenderTargets = 1; |
| 573 |
psoDesc.RTVFormats[0] = bd->RTVFormat; |
| 574 |
psoDesc.DSVFormat = bd->DSVFormat; |
| 575 |
psoDesc.SampleDesc.Count = 1; |
| 576 |
psoDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE; |
| 577 |
|
| 578 |
ID3DBlob* vertexShaderBlob; |
| 579 |
ID3DBlob* pixelShaderBlob; |
| 580 |
|
| 581 |
// Create the vertex shader |
| 582 |
{ |
| 583 |
static const char* vertexShader = |
| 584 |
"cbuffer vertexBuffer : register(b0) \ |
| 585 |
{\ |
| 586 |
float4x4 ProjectionMatrix; \ |
| 587 |
};\ |
| 588 |
struct VS_INPUT\ |
| 589 |
{\ |
| 590 |
float2 pos : POSITION;\ |
| 591 |
float4 col : COLOR0;\ |
| 592 |
float2 uv : TEXCOORD0;\ |
| 593 |
};\ |
| 594 |
\ |
| 595 |
struct PS_INPUT\ |
| 596 |
{\ |
| 597 |
float4 pos : SV_POSITION;\ |
| 598 |
float4 col : COLOR0;\ |
| 599 |
float2 uv : TEXCOORD0;\ |
| 600 |
};\ |
| 601 |
\ |
| 602 |
PS_INPUT main(VS_INPUT input)\ |
| 603 |
{\ |
| 604 |
PS_INPUT output;\ |
| 605 |
output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\ |
| 606 |
output.col = input.col;\ |
| 607 |
output.uv = input.uv;\ |
| 608 |
return output;\ |
| 609 |
}"; |
| 610 |
|
| 611 |
if (FAILED(D3DCompile(vertexShader, strlen(vertexShader), nullptr, nullptr, nullptr, "main", "vs_5_0", 0, 0, &vertexShaderBlob, nullptr))) |
| 612 |
return false; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob! |
| 613 |
psoDesc.VS = { vertexShaderBlob->GetBufferPointer(), vertexShaderBlob->GetBufferSize() }; |
| 614 |
|
| 615 |
// Create the input layout |
| 616 |
static D3D12_INPUT_ELEMENT_DESC local_layout[] = |
| 617 |
{ |
| 618 |
{ "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)offsetof(ImDrawVert, pos), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, |
| 619 |
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (UINT)offsetof(ImDrawVert, uv), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, |
| 620 |
{ "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (UINT)offsetof(ImDrawVert, col), D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 }, |
| 621 |
}; |
| 622 |
psoDesc.InputLayout = { local_layout, 3 }; |
| 623 |
} |
| 624 |
|
| 625 |
// Create the pixel shader |
| 626 |
{ |
| 627 |
static const char* pixelShader = |
| 628 |
"struct PS_INPUT\ |
| 629 |
{\ |
| 630 |
float4 pos : SV_POSITION;\ |
| 631 |
float4 col : COLOR0;\ |
| 632 |
float2 uv : TEXCOORD0;\ |
| 633 |
};\ |
| 634 |
SamplerState sampler0 : register(s0);\ |
| 635 |
Texture2D texture0 : register(t0);\ |
| 636 |
\ |
| 637 |
float4 main(PS_INPUT input) : SV_Target\ |
| 638 |
{\ |
| 639 |
float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \ |
| 640 |
return out_col; \ |
| 641 |
}"; |
| 642 |
|
| 643 |
if (FAILED(D3DCompile(pixelShader, strlen(pixelShader), nullptr, nullptr, nullptr, "main", "ps_5_0", 0, 0, &pixelShaderBlob, nullptr))) |
| 644 |
{ |
| 645 |
vertexShaderBlob->Release(); |
| 646 |
return false; // NB: Pass ID3DBlob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob! |
| 647 |
} |
| 648 |
psoDesc.PS = { pixelShaderBlob->GetBufferPointer(), pixelShaderBlob->GetBufferSize() }; |
| 649 |
} |
| 650 |
|
| 651 |
// Create the blending setup |
| 652 |
{ |
| 653 |
D3D12_BLEND_DESC& desc = psoDesc.BlendState; |
| 654 |
desc.AlphaToCoverageEnable = false; |
| 655 |
desc.RenderTarget[0].BlendEnable = true; |
| 656 |
desc.RenderTarget[0].SrcBlend = D3D12_BLEND_SRC_ALPHA; |
| 657 |
desc.RenderTarget[0].DestBlend = D3D12_BLEND_INV_SRC_ALPHA; |
| 658 |
desc.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD; |
| 659 |
desc.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_ONE; |
| 660 |
desc.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_INV_SRC_ALPHA; |
| 661 |
desc.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD; |
| 662 |
desc.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL; |
| 663 |
} |
| 664 |
|
| 665 |
// Create the rasterizer state |
| 666 |
{ |
| 667 |
D3D12_RASTERIZER_DESC& desc = psoDesc.RasterizerState; |
| 668 |
desc.FillMode = D3D12_FILL_MODE_SOLID; |
| 669 |
desc.CullMode = D3D12_CULL_MODE_NONE; |
| 670 |
desc.FrontCounterClockwise = FALSE; |
| 671 |
desc.DepthBias = D3D12_DEFAULT_DEPTH_BIAS; |
| 672 |
desc.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP; |
| 673 |
desc.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS; |
| 674 |
desc.DepthClipEnable = true; |
| 675 |
desc.MultisampleEnable = FALSE; |
| 676 |
desc.AntialiasedLineEnable = FALSE; |
| 677 |
desc.ForcedSampleCount = 0; |
| 678 |
desc.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF; |
| 679 |
} |
| 680 |
|
| 681 |
// Create depth-stencil State |
| 682 |
{ |
| 683 |
D3D12_DEPTH_STENCIL_DESC& desc = psoDesc.DepthStencilState; |
| 684 |
desc.DepthEnable = false; |
| 685 |
desc.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL; |
| 686 |
desc.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; |
| 687 |
desc.StencilEnable = false; |
| 688 |
desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D12_STENCIL_OP_KEEP; |
| 689 |
desc.FrontFace.StencilFunc = D3D12_COMPARISON_FUNC_ALWAYS; |
| 690 |
desc.BackFace = desc.FrontFace; |
| 691 |
} |
| 692 |
|
| 693 |
HRESULT result_pipeline_state = bd->pd3dDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&bd->pPipelineState)); |
| 694 |
vertexShaderBlob->Release(); |
| 695 |
pixelShaderBlob->Release(); |
| 696 |
if (result_pipeline_state != S_OK) |
| 697 |
return false; |
| 698 |
|
| 699 |
ImGui_ImplDX12_CreateFontsTexture(); |
| 700 |
|
| 701 |
return true; |
| 702 |
} |
| 703 |
|
| 704 |
void ImGui_ImplDX12_InvalidateDeviceObjects() |
| 705 |
{ |
| 706 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 707 |
if (!bd || !bd->pd3dDevice) |
| 708 |
return; |
| 709 |
|
| 710 |
ImGuiIO& io = ImGui::GetIO(); |
| 711 |
SafeRelease(bd->pRootSignature); |
| 712 |
SafeRelease(bd->pPipelineState); |
| 713 |
|
| 714 |
// Free SRV descriptor used by texture |
| 715 |
ImGui_ImplDX12_Texture* font_tex = &bd->FontTexture; |
| 716 |
bd->InitInfo.SrvDescriptorFreeFn(&bd->InitInfo, font_tex->hFontSrvCpuDescHandle, font_tex->hFontSrvGpuDescHandle); |
| 717 |
SafeRelease(font_tex->pTextureResource); |
| 718 |
io.Fonts->SetTexID(0); // We copied bd->hFontSrvGpuDescHandle to io.Fonts->TexID so let's clear that as well. |
| 719 |
|
| 720 |
for (UINT i = 0; i < bd->numFramesInFlight; i++) |
| 721 |
{ |
| 722 |
ImGui_ImplDX12_RenderBuffers* fr = &bd->pFrameResources[i]; |
| 723 |
SafeRelease(fr->IndexBuffer); |
| 724 |
SafeRelease(fr->VertexBuffer); |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
bool ImGui_ImplDX12_Init(ImGui_ImplDX12_InitInfo* init_info) |
| 729 |
{ |
| 730 |
ImGuiIO& io = ImGui::GetIO(); |
| 731 |
IMGUI_CHECKVERSION(); |
| 732 |
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); |
| 733 |
|
| 734 |
// Setup backend capabilities flags |
| 735 |
ImGui_ImplDX12_Data* bd = IM_NEW(ImGui_ImplDX12_Data)(); |
| 736 |
bd->InitInfo = *init_info; // Deep copy |
| 737 |
init_info = &bd->InitInfo; |
| 738 |
|
| 739 |
bd->pd3dDevice = init_info->Device; |
| 740 |
bd->RTVFormat = init_info->RTVFormat; |
| 741 |
bd->DSVFormat = init_info->DSVFormat; |
| 742 |
bd->numFramesInFlight = init_info->NumFramesInFlight; |
| 743 |
bd->pd3dSrvDescHeap = init_info->SrvDescriptorHeap; |
| 744 |
|
| 745 |
io.BackendRendererUserData = (void*)bd; |
| 746 |
io.BackendRendererName = "imgui_impl_dx12"; |
| 747 |
io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. |
| 748 |
|
| 749 |
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS |
| 750 |
if (init_info->SrvDescriptorAllocFn == nullptr) |
| 751 |
{ |
| 752 |
// Wrap legacy behavior of passing space for a single descriptor |
| 753 |
IM_ASSERT(init_info->LegacySingleSrvCpuDescriptor.ptr != 0 && init_info->LegacySingleSrvGpuDescriptor.ptr != 0); |
| 754 |
init_info->SrvDescriptorAllocFn = [](ImGui_ImplDX12_InitInfo*, D3D12_CPU_DESCRIPTOR_HANDLE* out_cpu_handle, D3D12_GPU_DESCRIPTOR_HANDLE* out_gpu_handle) |
| 755 |
{ |
| 756 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 757 |
IM_ASSERT(bd->LegacySingleDescriptorUsed == false); |
| 758 |
*out_cpu_handle = bd->InitInfo.LegacySingleSrvCpuDescriptor; |
| 759 |
*out_gpu_handle = bd->InitInfo.LegacySingleSrvGpuDescriptor; |
| 760 |
bd->LegacySingleDescriptorUsed = true; |
| 761 |
}; |
| 762 |
init_info->SrvDescriptorFreeFn = [](ImGui_ImplDX12_InitInfo*, D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_GPU_DESCRIPTOR_HANDLE) |
| 763 |
{ |
| 764 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 765 |
IM_ASSERT(bd->LegacySingleDescriptorUsed == true); |
| 766 |
bd->LegacySingleDescriptorUsed = false; |
| 767 |
}; |
| 768 |
} |
| 769 |
#endif |
| 770 |
|
| 771 |
// Allocate 1 SRV descriptor for the font texture |
| 772 |
IM_ASSERT(init_info->SrvDescriptorAllocFn != nullptr && init_info->SrvDescriptorFreeFn != nullptr); |
| 773 |
init_info->SrvDescriptorAllocFn(&bd->InitInfo, &bd->FontTexture.hFontSrvCpuDescHandle, &bd->FontTexture.hFontSrvGpuDescHandle); |
| 774 |
|
| 775 |
// Create buffers with a default size (they will later be grown as needed) |
| 776 |
bd->frameIndex = UINT_MAX; |
| 777 |
bd->pFrameResources = new ImGui_ImplDX12_RenderBuffers[bd->numFramesInFlight]; |
| 778 |
for (int i = 0; i < (int)bd->numFramesInFlight; i++) |
| 779 |
{ |
| 780 |
ImGui_ImplDX12_RenderBuffers* fr = &bd->pFrameResources[i]; |
| 781 |
fr->IndexBuffer = nullptr; |
| 782 |
fr->VertexBuffer = nullptr; |
| 783 |
fr->IndexBufferSize = 10000; |
| 784 |
fr->VertexBufferSize = 5000; |
| 785 |
} |
| 786 |
|
| 787 |
return true; |
| 788 |
} |
| 789 |
|
| 790 |
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS |
| 791 |
// Legacy initialization API Obsoleted in 1.91.5 |
| 792 |
// font_srv_cpu_desc_handle and font_srv_gpu_desc_handle are handles to a single SRV descriptor to use for the internal font texture, they must be in 'srv_descriptor_heap' |
| 793 |
bool ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames_in_flight, DXGI_FORMAT rtv_format, ID3D12DescriptorHeap* srv_descriptor_heap, D3D12_CPU_DESCRIPTOR_HANDLE font_srv_cpu_desc_handle, D3D12_GPU_DESCRIPTOR_HANDLE font_srv_gpu_desc_handle) |
| 794 |
{ |
| 795 |
ImGui_ImplDX12_InitInfo init_info; |
| 796 |
init_info.Device = device; |
| 797 |
init_info.NumFramesInFlight = num_frames_in_flight; |
| 798 |
init_info.RTVFormat = rtv_format; |
| 799 |
init_info.SrvDescriptorHeap = srv_descriptor_heap; |
| 800 |
init_info.LegacySingleSrvCpuDescriptor = font_srv_cpu_desc_handle; |
| 801 |
init_info.LegacySingleSrvGpuDescriptor = font_srv_gpu_desc_handle;; |
| 802 |
return ImGui_ImplDX12_Init(&init_info); |
| 803 |
} |
| 804 |
#endif |
| 805 |
|
| 806 |
void ImGui_ImplDX12_Shutdown() |
| 807 |
{ |
| 808 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 809 |
IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?"); |
| 810 |
ImGuiIO& io = ImGui::GetIO(); |
| 811 |
|
| 812 |
// Clean up windows and device objects |
| 813 |
ImGui_ImplDX12_InvalidateDeviceObjects(); |
| 814 |
delete[] bd->pFrameResources; |
| 815 |
|
| 816 |
io.BackendRendererName = nullptr; |
| 817 |
io.BackendRendererUserData = nullptr; |
| 818 |
io.BackendFlags &= ~ImGuiBackendFlags_RendererHasVtxOffset; |
| 819 |
IM_DELETE(bd); |
| 820 |
} |
| 821 |
|
| 822 |
void ImGui_ImplDX12_NewFrame() |
| 823 |
{ |
| 824 |
ImGui_ImplDX12_Data* bd = ImGui_ImplDX12_GetBackendData(); |
| 825 |
IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplDX12_Init()?"); |
| 826 |
|
| 827 |
if (!bd->pPipelineState) |
| 828 |
ImGui_ImplDX12_CreateDeviceObjects(); |
| 829 |
} |
| 830 |
|
| 831 |
//----------------------------------------------------------------------------- |
| 832 |
|
| 833 |
#endif // #ifndef IMGUI_DISABLE |