Skip to content

Commit

Permalink
Examples: DX11 + Win32: Initial attempt at implementing the viewport/…
Browse files Browse the repository at this point in the history
…platform api. (WIP/test API) (#1542)
  • Loading branch information
ocornut committed Feb 27, 2018
1 parent 735267d commit 25349b3
Show file tree
Hide file tree
Showing 3 changed files with 400 additions and 3 deletions.
9 changes: 7 additions & 2 deletions examples/directx11_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,12 @@ int main(int, char**)

// Setup ImGui binding
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGuiIO& io = ImGui::GetIO();
io.NavFlags |= ImGuiNavFlags_EnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_MultiViewports;

ImGui_ImplWin32_Init(hwnd);
ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);
//io.NavFlags |= ImGuiNavFlags_EnableKeyboard; // Enable Keyboard Controls

// Setup style
ImGui::StyleColorsDark();
Expand Down Expand Up @@ -206,6 +208,9 @@ int main(int, char**)
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());

ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindows();

g_pSwapChain->Present(1, 0); // Present with vsync
//g_pSwapChain->Present(0, 0); // Present without vsync
}
Expand Down
140 changes: 140 additions & 0 deletions examples/imgui_impl_dx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2018-XX-XX: Platform: Added support for multiple windows via the ImGuiRendererInterface
// 2018-XX-XX: DirectX11: Offset projection matrix and clipping rectangle by io.DisplayPos (which will be non-zero for multi-viewport applications).
// 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX11_RenderDrawData() in the .h file so you can call it yourself.
// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
Expand All @@ -26,6 +27,7 @@
// DirectX data
static ID3D11Device* g_pd3dDevice = NULL;
static ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
static IDXGIFactory1* g_pFactory = NULL;
static ID3D11Buffer* g_pVB = NULL;
static ID3D11Buffer* g_pIB = NULL;
static ID3D10Blob * g_pVertexShaderBlob = NULL;
Expand All @@ -46,6 +48,10 @@ struct VERTEX_CONSTANT_BUFFER
float mvp[4][4];
};

// Forward Declarations
static void ImGui_ImplDX11_InitPlatformInterface();
static void ImGui_ImplDX11_ShutdownPlatformInterface();

// Render function
// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop)
void ImGui_ImplDX11_RenderDrawData(ImDrawData* draw_data)
Expand Down Expand Up @@ -466,13 +472,29 @@ void ImGui_ImplDX11_InvalidateDeviceObjects()

bool ImGui_ImplDX11_Init(ID3D11Device* device, ID3D11DeviceContext* device_context)
{
// Get factory from device
IDXGIDevice* pDXGIDevice = NULL;
IDXGIAdapter* pDXGIAdapter = NULL;
IDXGIFactory1* pFactory = NULL;
if (device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)) != S_OK)
return false;
if (pDXGIDevice->GetParent(IID_PPV_ARGS(&pDXGIAdapter)) != S_OK)
return false;
if (pDXGIAdapter->GetParent(IID_PPV_ARGS(&pFactory)) != S_OK)
return false;

ImGuiIO& io = ImGui::GetIO();
g_pd3dDevice = device;
g_pd3dDeviceContext = device_context;
g_pFactory = pFactory;
if (io.ConfigFlags & ImGuiConfigFlags_MultiViewports)
ImGui_ImplDX11_InitPlatformInterface();
return true;
}

void ImGui_ImplDX11_Shutdown()
{
ImGui_ImplDX11_ShutdownPlatformInterface();
ImGui_ImplDX11_InvalidateDeviceObjects();
g_pd3dDevice = NULL;
g_pd3dDeviceContext = NULL;
Expand All @@ -483,3 +505,121 @@ void ImGui_ImplDX11_NewFrame()
if (!g_pFontSampler)
ImGui_ImplDX11_CreateDeviceObjects();
}

// --------------------------------------------------------------------------------------------------------
// Platform Windows
// --------------------------------------------------------------------------------------------------------

#include "imgui_internal.h" // ImGuiViewport

struct ImGuiPlatformDataDx11
{
IDXGISwapChain* SwapChain;
ID3D11RenderTargetView* RTView;

ImGuiPlatformDataDx11() { SwapChain = NULL; RTView = NULL; }
~ImGuiPlatformDataDx11() { IM_ASSERT(SwapChain == NULL && RTView == NULL); }
};

static void ImGui_ImplDX11_CreateViewport(ImGuiViewport* viewport)
{
ImGuiPlatformDataDx11* data = IM_NEW(ImGuiPlatformDataDx11)();
viewport->RendererUserData = data;

// FIXME-PLATFORM
HWND hwnd = (HWND)viewport->PlatformHandle;
IM_ASSERT(hwnd != 0);

// Create swap chain
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferDesc.Width = (UINT)viewport->Size.x;
sd.BufferDesc.Height = (UINT)viewport->Size.y;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.BufferCount = 1;
sd.OutputWindow = hwnd;
sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
sd.Flags = 0;

IM_ASSERT(data->SwapChain == NULL && data->RTView == NULL);
g_pFactory->CreateSwapChain(g_pd3dDevice, &sd, &data->SwapChain);

// Create the render target
if (data->SwapChain)
{
ID3D11Texture2D* pBackBuffer;
data->SwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &data->RTView);
pBackBuffer->Release();
}
}

static void ImGui_ImplDX11_DestroyViewport(ImGuiViewport* viewport)
{
if (ImGuiPlatformDataDx11* data = (ImGuiPlatformDataDx11*)viewport->RendererUserData)
{
if (data->SwapChain)
data->SwapChain->Release();
data->SwapChain = NULL;
if (data->RTView)
data->RTView->Release();
data->RTView = NULL;
IM_DELETE(data);
}
viewport->RendererUserData = NULL;
}

static void ImGui_ImplDX11_ResizeViewport(ImGuiViewport* viewport, int w, int h)
{
ImGuiPlatformDataDx11* data = (ImGuiPlatformDataDx11*)viewport->RendererUserData;
if (data->RTView)
{
data->RTView->Release();
data->RTView = NULL;
}
if (data->SwapChain)
{
ID3D11Texture2D* pBackBuffer = NULL;
data->SwapChain->ResizeBuffers(0, w, h, DXGI_FORMAT_UNKNOWN, 0);
data->SwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &data->RTView);
pBackBuffer->Release();
}
}

static void ImGui_ImplDX11_RenderViewport(ImGuiViewport* viewport)
{
ImGuiPlatformDataDx11* data = (ImGuiPlatformDataDx11*)viewport->RendererUserData;
ImVec4 clear_color = ImGui::GetStyle().Colors[ImGuiCol_WindowBg]; // FIXME-PLATFORM
clear_color.w = 1.0f;
g_pd3dDeviceContext->OMSetRenderTargets(1, &data->RTView, NULL);
g_pd3dDeviceContext->ClearRenderTargetView(data->RTView, (float*)&clear_color);
ImGui_ImplDX11_RenderDrawData(&viewport->DrawData);
}

static void ImGui_ImplDX11_SwapBuffers(ImGuiViewport* viewport)
{
ImGuiPlatformDataDx11* data = (ImGuiPlatformDataDx11*)viewport->RendererUserData;
data->SwapChain->Present(0, 0); // Present without vsync
}

void ImGui_ImplDX11_InitPlatformInterface()
{
ImGuiIO& io = ImGui::GetIO();
io.RendererInterface.CreateViewport = ImGui_ImplDX11_CreateViewport;
io.RendererInterface.DestroyViewport = ImGui_ImplDX11_DestroyViewport;
io.RendererInterface.ResizeViewport = ImGui_ImplDX11_ResizeViewport;
io.RendererInterface.RenderViewport = ImGui_ImplDX11_RenderViewport;
io.RendererInterface.SwapBuffers = ImGui_ImplDX11_SwapBuffers;
}

void ImGui_ImplDX11_ShutdownPlatformInterface()
{
ImGuiIO& io = ImGui::GetIO();
memset(&io.RendererInterface, 0, sizeof(io.RendererInterface));
}

Loading

0 comments on commit 25349b3

Please sign in to comment.