Skip to content

Commit baef794

Browse files
committed
Examples: DX12: Empty skeleton toward implementing the multi-viewport api for DirectX12. (WIP/test API) (#1542, #301)
1 parent cb601d7 commit baef794

File tree

2 files changed

+149
-2
lines changed

2 files changed

+149
-2
lines changed

examples/directx12_example/main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void ResizeSwapChain(HWND hWnd, int width, int height)
9393
sd.Width = width;
9494
sd.Height = height;
9595

96-
IDXGIFactory4* dxgiFactory = nullptr;
96+
IDXGIFactory4* dxgiFactory = NULL;
9797
g_pSwapChain->GetParent(IID_PPV_ARGS(&dxgiFactory));
9898

9999
g_pSwapChain->Release();
@@ -289,7 +289,9 @@ int main(int, char**)
289289
// Setup ImGui binding
290290
ImGui::CreateContext();
291291
ImGuiIO& io = ImGui::GetIO(); (void)io;
292+
io.ConfigFlags |= ImGuiConfigFlags_MultiViewports;
292293
//io.NavFlags |= ImGuiNavFlags_EnableKeyboard; // Enable Keyboard Controls
294+
293295
ImGui_ImplWin32_Init(hwnd);
294296
ImGui_ImplDX12_Init(g_pd3dDevice, NUM_FRAMES_IN_FLIGHT, DXGI_FORMAT_R8G8B8A8_UNORM,
295297
g_pd3dSrvDescHeap->GetCPUDescriptorHandleForHeapStart(), g_pd3dSrvDescHeap->GetGPUDescriptorHandleForHeapStart());
@@ -399,6 +401,9 @@ int main(int, char**)
399401

400402
g_pd3dCommandQueue->ExecuteCommandLists(1, (ID3D12CommandList* const*)&g_pd3dCommandList);
401403

404+
ImGui::UpdatePlatformWindows();
405+
ImGui::RenderPlatformWindows();
406+
402407
g_pSwapChain->Present(1, 0); // Present with vsync
403408
//g_pSwapChain->Present(0, 0); // Present without vsync
404409

examples/imgui_impl_dx12.cpp

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
// DirectX
2020
#include <d3d12.h>
21+
#include <dxgi1_4.h>
2122
#include <d3dcompiler.h>
2223

2324
// DirectX data
@@ -48,6 +49,10 @@ struct VERTEX_CONSTANT_BUFFER
4849
float mvp[4][4];
4950
};
5051

52+
// Forward Declarations
53+
static void ImGui_ImplDX12_InitPlatformInterface();
54+
static void ImGui_ImplDX12_ShutdownPlatformInterface();
55+
5156
// Render function
5257
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
5358
void ImGui_ImplDX12_RenderDrawData(ImDrawData* draw_data)
@@ -587,7 +592,7 @@ bool ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames_in_flight, DXGI_FO
587592
g_RTVFormat = rtv_format;
588593
g_hFontSrvCpuDescHandle = font_srv_cpu_desc_handle;
589594
g_hFontSrvGpuDescHandle = font_srv_gpu_desc_handle;
590-
g_pFrameResources = new FrameResources [num_frames_in_flight];
595+
g_pFrameResources = new FrameResources[num_frames_in_flight];
591596
g_numFramesInFlight = num_frames_in_flight;
592597
g_frameIndex = UINT_MAX;
593598

@@ -599,11 +604,16 @@ bool ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames_in_flight, DXGI_FO
599604
g_pFrameResources[i].IndexBufferSize = 10000;
600605
}
601606

607+
ImGuiIO& io = ImGui::GetIO();
608+
if (io.ConfigFlags & ImGuiConfigFlags_MultiViewports)
609+
ImGui_ImplDX12_InitPlatformInterface();
610+
602611
return true;
603612
}
604613

605614
void ImGui_ImplDX12_Shutdown()
606615
{
616+
ImGui_ImplDX12_ShutdownPlatformInterface();
607617
ImGui_ImplDX12_InvalidateDeviceObjects();
608618
delete[] g_pFrameResources;
609619
g_pd3dDevice = NULL;
@@ -622,3 +632,135 @@ void ImGui_ImplDX12_NewFrame(ID3D12GraphicsCommandList* command_list)
622632

623633
g_pd3dCommandList = command_list;
624634
}
635+
636+
// --------------------------------------------------------------------------------------------------------
637+
// Platform Windows
638+
// --------------------------------------------------------------------------------------------------------
639+
640+
#include "imgui_internal.h" // ImGuiViewport
641+
642+
struct ImGuiPlatformDataDx12
643+
{
644+
IDXGISwapChain3* SwapChain;
645+
646+
ImGuiPlatformDataDx12() { SwapChain = NULL; }
647+
~ImGuiPlatformDataDx12() { IM_ASSERT(SwapChain == NULL); }
648+
};
649+
650+
static void ImGui_ImplDX12_CreateViewport(ImGuiViewport* viewport)
651+
{
652+
ImGuiPlatformDataDx12* data = IM_NEW(ImGuiPlatformDataDx12)();
653+
viewport->RendererUserData = data;
654+
IM_ASSERT(0);
655+
656+
/*
657+
// FIXME-PLATFORM
658+
HWND hwnd = (HWND)viewport->PlatformHandle;
659+
IM_ASSERT(hwnd != 0);
660+
661+
// Create swap chain
662+
DXGI_SWAP_CHAIN_DESC sd;
663+
ZeroMemory(&sd, sizeof(sd));
664+
sd.BufferDesc.Width = (UINT)viewport->Size.x;
665+
sd.BufferDesc.Height = (UINT)viewport->Size.y;
666+
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
667+
sd.SampleDesc.Count = 1;
668+
sd.SampleDesc.Quality = 0;
669+
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
670+
sd.BufferCount = 1;
671+
sd.OutputWindow = hwnd;
672+
sd.Windowed = TRUE;
673+
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
674+
sd.Flags = 0;
675+
676+
IM_ASSERT(data->SwapChain == NULL && data->RTView == NULL);
677+
g_pFactory->CreateSwapChain(g_pd3dDevice, &sd, &data->SwapChain);
678+
679+
// Create the render target
680+
if (data->SwapChain)
681+
{
682+
ID3D11Texture2D* pBackBuffer;
683+
data->SwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
684+
g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &data->RTView);
685+
pBackBuffer->Release();
686+
}
687+
*/
688+
}
689+
690+
static void ImGui_ImplDX12_DestroyViewport(ImGuiViewport* viewport)
691+
{
692+
if (ImGuiPlatformDataDx12* data = (ImGuiPlatformDataDx12*)viewport->RendererUserData)
693+
{
694+
IM_ASSERT(0);
695+
/*
696+
if (data->SwapChain)
697+
data->SwapChain->Release();
698+
data->SwapChain = NULL;
699+
if (data->RTView)
700+
data->RTView->Release();
701+
data->RTView = NULL;
702+
IM_DELETE(data);
703+
*/
704+
}
705+
viewport->RendererUserData = NULL;
706+
}
707+
708+
static void ImGui_ImplDX12_ResizeViewport(ImGuiViewport* viewport, int w, int h)
709+
{
710+
ImGuiPlatformDataDx12* data = (ImGuiPlatformDataDx12*)viewport->RendererUserData;
711+
IM_ASSERT(0);
712+
/*
713+
if (data->RTView)
714+
{
715+
data->RTView->Release();
716+
data->RTView = NULL;
717+
}
718+
if (data->SwapChain)
719+
{
720+
ID3D11Texture2D* pBackBuffer = NULL;
721+
data->SwapChain->ResizeBuffers(0, w, h, DXGI_FORMAT_UNKNOWN, 0);
722+
data->SwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
723+
g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &data->RTView);
724+
pBackBuffer->Release();
725+
}
726+
*/
727+
}
728+
729+
static void ImGui_ImplDX12_RenderViewport(ImGuiViewport* viewport)
730+
{
731+
ImGuiPlatformDataDx12* data = (ImGuiPlatformDataDx12*)viewport->RendererUserData;
732+
IM_ASSERT(0);
733+
/*
734+
ImVec4 clear_color = ImGui::GetStyle().Colors[ImGuiCol_WindowBg]; // FIXME-PLATFORM
735+
clear_color.w = 1.0f;
736+
g_pd3dDeviceContext->OMSetRenderTargets(1, &data->RTView, NULL);
737+
g_pd3dDeviceContext->ClearRenderTargetView(data->RTView, (float*)&clear_color);
738+
*/
739+
ImGui_ImplDX12_RenderDrawData(&viewport->DrawData);
740+
}
741+
742+
static void ImGui_ImplDX12_SwapBuffers(ImGuiViewport* viewport)
743+
{
744+
ImGuiPlatformDataDx12* data = (ImGuiPlatformDataDx12*)viewport->RendererUserData;
745+
IM_ASSERT(0);
746+
/*
747+
data->SwapChain->Present(0, 0); // Present without vsync
748+
*/
749+
}
750+
751+
void ImGui_ImplDX12_InitPlatformInterface()
752+
{
753+
ImGuiIO& io = ImGui::GetIO();
754+
io.RendererInterface.CreateViewport = ImGui_ImplDX12_CreateViewport;
755+
io.RendererInterface.DestroyViewport = ImGui_ImplDX12_DestroyViewport;
756+
io.RendererInterface.ResizeViewport = ImGui_ImplDX12_ResizeViewport;
757+
io.RendererInterface.RenderViewport = ImGui_ImplDX12_RenderViewport;
758+
io.RendererInterface.SwapBuffers = ImGui_ImplDX12_SwapBuffers;
759+
}
760+
761+
void ImGui_ImplDX12_ShutdownPlatformInterface()
762+
{
763+
ImGuiIO& io = ImGui::GetIO();
764+
memset(&io.RendererInterface, 0, sizeof(io.RendererInterface));
765+
}
766+

0 commit comments

Comments
 (0)