Skip to content

Commit

Permalink
Convert DX12 example to use explicit context API
Browse files Browse the repository at this point in the history
--
This commit is part of the conversion of Dear ImGui toward explicit context API.
This commit is supposed to be applied *after* make_explicit_imgui.py has been run.
--
  • Loading branch information
Dragnalith committed Nov 23, 2023
1 parent 55946ad commit 61d0a54
Showing 1 changed file with 40 additions and 31 deletions.
71 changes: 40 additions & 31 deletions examples/example_win32_directx12/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,20 @@ int main(int, char**)

// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGuiContext* ctx = ImGui::CreateContext();
::SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(ctx));

ImGuiIO& io = ImGui::GetIO(ctx); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls

// Setup Dear ImGui style
ImGui::StyleColorsDark();
ImGui::StyleColorsDark(ctx);
//ImGui::StyleColorsLight();

// Setup Platform/Renderer backends
ImGui_ImplWin32_Init(hwnd);
ImGui_ImplDX12_Init(g_pd3dDevice, NUM_FRAMES_IN_FLIGHT,
ImGui_ImplWin32_Init(ctx, hwnd);
ImGui_ImplDX12_Init(ctx, g_pd3dDevice, NUM_FRAMES_IN_FLIGHT,
DXGI_FORMAT_R8G8B8A8_UNORM, g_pd3dSrvDescHeap,
g_pd3dSrvDescHeap->GetCPUDescriptorHandleForHeapStart(),
g_pd3dSrvDescHeap->GetGPUDescriptorHandleForHeapStart());
Expand Down Expand Up @@ -138,49 +140,49 @@ int main(int, char**)
break;

// Start the Dear ImGui frame
ImGui_ImplDX12_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
ImGui_ImplDX12_NewFrame(ctx);
ImGui_ImplWin32_NewFrame(ctx);
ImGui::NewFrame(ctx);

// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
if (show_demo_window)
ImGui::ShowDemoWindow(&show_demo_window);
ImGui::ShowDemoWindow(ctx, &show_demo_window);

// 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
{
static float f = 0.0f;
static int counter = 0;

ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
ImGui::Begin(ctx, "Hello, world!"); // Create a window called "Hello, world!" and append into it.

ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
ImGui::Checkbox("Another Window", &show_another_window);
ImGui::Text(ctx, "This is some useful text."); // Display some text (you can use a format strings too)
ImGui::Checkbox(ctx, "Demo Window", &show_demo_window); // Edit bools storing our window open/close state
ImGui::Checkbox(ctx, "Another Window", &show_another_window);

ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
ImGui::SliderFloat(ctx, "float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
ImGui::ColorEdit3(ctx, "clear color", (float*)&clear_color); // Edit 3 floats representing a color

if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
if (ImGui::Button(ctx, "Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::SameLine(ctx);
ImGui::Text(ctx, "counter = %d", counter);

ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::End();
ImGui::Text(ctx, "Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::End(ctx);
}

// 3. Show another simple window.
if (show_another_window)
{
ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
ImGui::Text("Hello from another window!");
if (ImGui::Button("Close Me"))
ImGui::Begin(ctx, "Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
ImGui::Text(ctx, "Hello from another window!");
if (ImGui::Button(ctx, "Close Me"))
show_another_window = false;
ImGui::End();
ImGui::End(ctx);
}

// Rendering
ImGui::Render();
ImGui::Render(ctx);

FrameContext* frameCtx = WaitForNextFrameResources();
UINT backBufferIdx = g_pSwapChain->GetCurrentBackBufferIndex();
Expand All @@ -201,7 +203,7 @@ int main(int, char**)
g_pd3dCommandList->ClearRenderTargetView(g_mainRenderTargetDescriptor[backBufferIdx], clear_color_with_alpha, 0, nullptr);
g_pd3dCommandList->OMSetRenderTargets(1, &g_mainRenderTargetDescriptor[backBufferIdx], FALSE, nullptr);
g_pd3dCommandList->SetDescriptorHeaps(1, &g_pd3dSrvDescHeap);
ImGui_ImplDX12_RenderDrawData(ImGui::GetDrawData(), g_pd3dCommandList);
ImGui_ImplDX12_RenderDrawData(ctx, ImGui::GetDrawData(ctx), g_pd3dCommandList);
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
g_pd3dCommandList->ResourceBarrier(1, &barrier);
Expand All @@ -221,9 +223,9 @@ int main(int, char**)
WaitForLastSubmittedFrame();

// Cleanup
ImGui_ImplDX12_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
ImGui_ImplDX12_Shutdown(ctx);
ImGui_ImplWin32_Shutdown(ctx);
ImGui::DestroyContext(ctx);

CleanupDeviceD3D();
::DestroyWindow(hwnd);
Expand Down Expand Up @@ -434,7 +436,7 @@ FrameContext* WaitForNextFrameResources()
}

// Forward declare message handler from imgui_impl_win32.cpp
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(ImGuiContext* ctx, HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

// Win32 message handler
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
Expand All @@ -443,7 +445,14 @@ extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
LONG_PTR pLong = ::GetWindowLongPtrA(hWnd, GWLP_USERDATA);
ImGuiContext* ctx = reinterpret_cast<ImGuiContext*>(pLong);

if (ctx == NULL) {
return ::DefWindowProcW(hWnd, msg, wParam, lParam);
}

if (ImGui_ImplWin32_WndProcHandler(ctx, hWnd, msg, wParam, lParam))
return true;

switch (msg)
Expand Down

0 comments on commit 61d0a54

Please sign in to comment.