Skip to content

Commit

Permalink
Draft Dx11 glfw
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Jan 1, 2024
1 parent 9315d94 commit 0dd0089
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ option(HELLOIMGUI_USE_SDL_METAL "Build HelloImGui for SDL2+Metal" OFF)
option(HELLOIMGUI_USE_GLFW_VULKAN "Build HelloImGui for Glfw3+Vulkan" OFF)
# Use SDL2 + Vulkan
option(HELLOIMGUI_USE_SDL_VULKAN "Build HelloImGui for SDL2+Vulkan" OFF)
# Use SDL + DirectX 11
# Use SDL2 + DirectX 11
option(HELLOIMGUI_USE_SDL_DIRECTX11 "Build HelloImGui for SDL2+DirectX11" OFF)
# Use Glfw3 + DirectX 11
option(HELLOIMGUI_USE_GLFW_DIRECTX11 "Build HelloImGui for Glfw3+DirectX11" OFF)
# Experimental! DirectX12 was not tested, although it does compile!
option(HELLOIMGUI_USE_SDL_DIRECTX12 "Build HelloImGui for SDL2+DirectX12" OFF)

Expand Down
7 changes: 7 additions & 0 deletions hello_imgui_cmake/hello_imgui_build_lib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ function(him_get_available_backends out_var)
HELLOIMGUI_USE_GLFW_VULKAN
HELLOIMGUI_USE_SDL_VULKAN
HELLOIMGUI_USE_SDL_DIRECTX11
HELLOIMGUI_USE_GLFW_DIRECTX11
HELLOIMGUI_USE_SDL_DIRECTX12

PARENT_SCOPE
Expand Down Expand Up @@ -664,6 +665,12 @@ function(him_main_add_hello_imgui_library)
target_compile_definitions(${HELLOIMGUI_TARGET} PUBLIC HELLOIMGUI_USE_SDL_DIRECTX11)
endif ()

if(HELLOIMGUI_USE_GLFW_DIRECTX11)
him_has_directx11(${HELLOIMGUI_TARGET})
him_use_glfw_backend(${HELLOIMGUI_TARGET})
target_compile_definitions(${HELLOIMGUI_TARGET} PUBLIC HELLOIMGUI_USE_GLFW_DIRECTX11)
endif ()

if(HELLOIMGUI_USE_SDL_DIRECTX12)
him_has_directx12(${HELLOIMGUI_TARGET})
him_use_sdl2_backend(${HELLOIMGUI_TARGET})
Expand Down
89 changes: 89 additions & 0 deletions src/hello_imgui/internal/backend_impls/rendering_dx11_glfw.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#if defined(HELLOIMGUI_HAS_DIRECTX11) && defined(HELLOIMGUI_USE_GLFW3)
#include "rendering_dx11.h"

#include <backends/imgui_impl_dx11.h>
#include <backends/imgui_impl_glfw.h>

#define GLFW_INCLUDE_NONE
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>

#include "hello_imgui/hello_imgui_logger.h"
#include "hello_imgui/hello_imgui.h"


namespace HelloImGui
{
// Below is implementation of RenderingCallbacks_LinkWindowingToRenderingBackend
void PrepareGlfwForVulkan(GLFWwindow* window)
{
auto& gVkGlobals = HelloImGui::GetVulkanGlobals();

{
if (!glfwVulkanSupported())
{
IM_ASSERT(0 && "GLFW: Vulkan Not Supported");
exit(1);
}

ImVector<const char*> extensions;
uint32_t extensions_count = 0;
const char** glfw_extensions = glfwGetRequiredInstanceExtensions(&extensions_count);
for (uint32_t i = 0; i < extensions_count; i++)
extensions.push_back(glfw_extensions[i]);
HelloImGui::VulkanSetup::SetupVulkan(extensions);

// Create Window Surface
VkSurfaceKHR surface;
VkResult err = glfwCreateWindowSurface(gVkGlobals.Instance, window, gVkGlobals.Allocator, &surface);
HelloImGui::VulkanSetup::check_vk_result(err);

// Create Framebuffers
int w, h;
glfwGetFramebufferSize(window, &w, &h);
ImGui_ImplVulkanH_Window* wd = &gVkGlobals.ImGuiMainWindowData;
HelloImGui::VulkanSetup::SetupVulkanWindow(wd, surface, w, h);
}

{
ImGui_ImplVulkanH_Window* wd = &gVkGlobals.ImGuiMainWindowData;

// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForVulkan(window, true);
ImGui_ImplVulkan_InitInfo init_info = {};
init_info.Instance = gVkGlobals.Instance;
init_info.PhysicalDevice = gVkGlobals.PhysicalDevice;
init_info.Device = gVkGlobals.Device;
init_info.QueueFamily = gVkGlobals.QueueFamily;
init_info.Queue = gVkGlobals.Queue;
init_info.PipelineCache = gVkGlobals.PipelineCache;
init_info.DescriptorPool = gVkGlobals.DescriptorPool;
init_info.Subpass = 0;
init_info.MinImageCount = gVkGlobals.MinImageCount;
init_info.ImageCount = wd->ImageCount;
init_info.MSAASamples = VK_SAMPLE_COUNT_1_BIT;
init_info.Allocator = gVkGlobals.Allocator;
init_info.CheckVkResultFn = HelloImGui::VulkanSetup::check_vk_result;
ImGui_ImplVulkan_Init(&init_info, wd->RenderPass);
}
}


RenderingCallbacksPtr CreateBackendCallbacks_GlfwVulkan()
{
auto callbacks = PrepareBackendCallbacksCommonVulkan();

callbacks->Impl_GetFrameBufferSize = []
{
auto window = (GLFWwindow *) HelloImGui::GetRunnerParams()->backendPointers.glfwWindow;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
return ScreenSize{width, height};
};

return callbacks;
}

}

#endif // #if defined(HELLOIMGUI_HAS_VULKAN) && defined(HELLOIMGUI_USE_GLFW3)

0 comments on commit 0dd0089

Please sign in to comment.