Skip to content

Commit

Permalink
Examples: Add Win32+Vulkan example, amends. (#8180)
Browse files Browse the repository at this point in the history
  • Loading branch information
ocornut committed Jan 6, 2025
1 parent 38e606a commit 93a9307
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 86 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Other changes:
platforms not supporting VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR. (#8222) [@Zer0xFF]
- Backends: Vulkan: Added a few more ImGui_ImplVulkanH_XXX helper functions
primarily for the purpose of making our examples simpler.
- Examples: Added Win32+Vulkan example for completeness. (#8180) [@jristic]


-----------------------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion docs/EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,13 @@ DirectX12 example, Windows only. <BR>
This is quite long and tedious, because: DirectX12.

[example_win32_opengl3/](https://github.com/ocornut/imgui/blob/master/examples/example_win32_opengl3/) <BR>
Raw Windows + OpenGL3 + example (modern, programmable pipeline) <BR>
Raw Windows + OpenGL3 example (modern, programmable pipeline) <BR>
= main.cpp + imgui_impl_win32.cpp + imgui_impl_opengl3.cpp <BR>

[example_win32_vulkan/](https://github.com/ocornut/imgui/blob/master/examples/example_win32_vulkan/) <BR>
Raw Windows + Vulkan example <BR>
= main.cpp + imgui_impl_win32.cpp + imgui_impl_vulkan.cpp <BR>


### Miscellaneous

Expand Down
2 changes: 1 addition & 1 deletion examples/example_glfw_vulkan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE;
static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;

static ImGui_ImplVulkanH_Window g_MainWindowData;
static int g_MinImageCount = 2;
static uint32_t g_MinImageCount = 2;
static bool g_SwapChainRebuild = false;

static void glfw_error_callback(int error, const char* description)
Expand Down
18 changes: 8 additions & 10 deletions examples/example_win32_vulkan/example_win32_vulkan.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,35 @@
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{4CB8C22A-96F8-4D31-B747-FDAA6B742E47}</ProjectGuid>
<RootNamespace>example_win32_vulkan</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<ProjectGuid>{0A1E32DF-E0F4-4CCE-B3DC-9644C503BD88}</ProjectGuid>
<RootNamespace>example_win32_directx11</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
<PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
<PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
<PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v143</PlatformToolset>
<PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down Expand Up @@ -120,6 +119,7 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<AdditionalIncludeDirectories>..\..;..\..\backends;%VULKAN_SDK%\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BufferSecurityCheck>false</BufferSecurityCheck>
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
Expand Down Expand Up @@ -169,11 +169,9 @@
</ItemGroup>
<ItemGroup>
<None Include="..\..\misc\debuggers\imgui.natstepfilter" />
<None Include="..\..\misc\debuggers\imgui.natvis" />
<None Include="..\README.txt" />
</ItemGroup>
<ItemGroup>
<Natvis Include="..\..\misc\debuggers\imgui.natvis" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
26 changes: 12 additions & 14 deletions examples/example_win32_vulkan/example_win32_vulkan.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="imgui">
<UniqueIdentifier>{fb3d294f-51ec-478e-a627-25831c80fefd}</UniqueIdentifier>
<UniqueIdentifier>{0587d7a3-f2ce-4d56-b84f-a0005d3bfce6}</UniqueIdentifier>
</Filter>
<Filter Include="sources">
<UniqueIdentifier>{4f33ddea-9910-456d-b868-4267eb3c2b19}</UniqueIdentifier>
<UniqueIdentifier>{08e36723-ce4f-4cff-9662-c40801cf1acf}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
Expand All @@ -18,10 +18,10 @@
<ClInclude Include="..\..\imgui_internal.h">
<Filter>imgui</Filter>
</ClInclude>
<ClInclude Include="..\..\backends\imgui_impl_vulkan.h">
<ClInclude Include="..\..\backends\imgui_impl_win32.h">
<Filter>sources</Filter>
</ClInclude>
<ClInclude Include="..\..\backends\imgui_impl_win32.h">
<ClInclude Include="..\..\backends\imgui_impl_vulkan.h">
<Filter>sources</Filter>
</ClInclude>
</ItemGroup>
Expand All @@ -38,28 +38,26 @@
<ClCompile Include="..\..\imgui_draw.cpp">
<Filter>imgui</Filter>
</ClCompile>
<ClCompile Include="..\..\backends\imgui_impl_vulkan.cpp">
<Filter>sources</Filter>
<ClCompile Include="..\..\imgui_widgets.cpp">
<Filter>imgui</Filter>
</ClCompile>
<ClCompile Include="..\..\backends\imgui_impl_win32.cpp">
<Filter>sources</Filter>
</ClCompile>
<ClCompile Include="..\..\imgui_tables.cpp">
<Filter>imgui</Filter>
<ClCompile Include="..\..\backends\imgui_impl_vulkan.cpp">
<Filter>sources</Filter>
</ClCompile>
<ClCompile Include="..\..\imgui_widgets.cpp">
<ClCompile Include="..\..\imgui_tables.cpp">
<Filter>imgui</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\README.txt" />
<None Include="..\..\misc\debuggers\imgui.natstepfilter">
<None Include="..\..\misc\debuggers\imgui.natvis">
<Filter>imgui</Filter>
</None>
</ItemGroup>
<ItemGroup>
<Natvis Include="..\..\misc\debuggers\imgui.natvis">
<None Include="..\..\misc\debuggers\imgui.natstepfilter">
<Filter>imgui</Filter>
</Natvis>
</None>
</ItemGroup>
</Project>
61 changes: 10 additions & 51 deletions examples/example_win32_vulkan/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Dear ImGui: standalone example application for Vulkan
// Dear ImGui: standalone example application for Win32 + Vulkan

// Learn about Dear ImGui:
// - FAQ https://dearimgui.com/faq
Expand Down Expand Up @@ -74,35 +74,6 @@ static bool IsExtensionAvailable(const ImVector<VkExtensionProperties>& properti
return false;
}

static VkPhysicalDevice SetupVulkan_SelectPhysicalDevice()
{
uint32_t gpu_count;
VkResult err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, nullptr);
check_vk_result(err);
IM_ASSERT(gpu_count > 0);

ImVector<VkPhysicalDevice> gpus;
gpus.resize(gpu_count);
err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, gpus.Data);
check_vk_result(err);

// If a number >1 of GPUs got reported, find discrete GPU if present, or use first one available. This covers
// most common cases (multi-gpu/integrated+dedicated graphics). Handling more complicated setups (multiple
// dedicated GPUs) is out of scope of this sample.
for (VkPhysicalDevice& device : gpus)
{
VkPhysicalDeviceProperties properties;
vkGetPhysicalDeviceProperties(device, &properties);
if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
return device;
}

// Use first GPU (Integrated) is a Discrete one is not available.
if (gpu_count > 0)
return gpus[0];
return VK_NULL_HANDLE;
}

static void SetupVulkan(ImVector<const char*> instance_extensions)
{
VkResult err;
Expand Down Expand Up @@ -166,23 +137,12 @@ static void SetupVulkan(ImVector<const char*> instance_extensions)
}

// Select Physical Device (GPU)
g_PhysicalDevice = SetupVulkan_SelectPhysicalDevice();
g_PhysicalDevice = ImGui_ImplVulkanH_SelectPhysicalDevice(g_Instance);
IM_ASSERT(g_PhysicalDevice != VK_NULL_HANDLE);

// Select graphics queue family
{
uint32_t count;
vkGetPhysicalDeviceQueueFamilyProperties(g_PhysicalDevice, &count, nullptr);
VkQueueFamilyProperties* queues = (VkQueueFamilyProperties*)malloc(sizeof(VkQueueFamilyProperties) * count);
vkGetPhysicalDeviceQueueFamilyProperties(g_PhysicalDevice, &count, queues);
for (uint32_t i = 0; i < count; i++)
if (queues[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
{
g_QueueFamily = i;
break;
}
free(queues);
IM_ASSERT(g_QueueFamily != (uint32_t)-1);
}
g_QueueFamily = ImGui_ImplVulkanH_SelectQueueFamilyIndex(g_PhysicalDevice);
IM_ASSERT(g_QueueFamily != (uint32_t)-1);

// Create Logical Device (with 1 queue)
{
Expand Down Expand Up @@ -257,7 +217,7 @@ static void SetupVulkanWindow(ImGui_ImplVulkanH_Window* wd, VkSurfaceKHR surface
wd->SurfaceFormat = ImGui_ImplVulkanH_SelectSurfaceFormat(g_PhysicalDevice, wd->Surface, requestSurfaceImageFormat, (size_t)IM_ARRAYSIZE(requestSurfaceImageFormat), requestSurfaceColorSpace);

// Select Present Mode
#ifdef APP_UNLIMITED_FRAME_RATE
#ifdef APP_USE_UNLIMITED_FRAME_RATE
VkPresentModeKHR present_modes[] = { VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_FIFO_KHR };
#else
VkPresentModeKHR present_modes[] = { VK_PRESENT_MODE_FIFO_KHR };
Expand Down Expand Up @@ -387,7 +347,7 @@ int main(int, char**)
//ImGui_ImplWin32_EnableDpiAwareness();
WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"ImGui Example", nullptr };
::RegisterClassExW(&wc);
HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui Vulkan Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr);
HWND hwnd = ::CreateWindowW(wc.lpszClassName, L"Dear ImGui Win32+Vulkan Example", WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr);

ImVector<const char*> extensions;
extensions.push_back("VK_KHR_surface");
Expand All @@ -400,14 +360,15 @@ int main(int, char**)
VkWin32SurfaceCreateInfoKHR createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
createInfo.hwnd = hwnd;
createInfo.hinstance = GetModuleHandle(nullptr);
createInfo.hinstance = ::GetModuleHandle(nullptr);
if (vkCreateWin32SurfaceKHR(g_Instance, &createInfo, nullptr, &surface) != VK_SUCCESS)
{
printf("Failed to create Vulkan surface.\n");
return 1;
}

// Show the window
// FIXME: Retrieve client size from window itself.
ImGui_ImplVulkanH_Window* wd = &g_MainWindowData;
SetupVulkanWindow(wd, surface, 1280, 800);
::ShowWindow(hwnd, SW_SHOWDEFAULT);
Expand Down Expand Up @@ -525,7 +486,6 @@ int main(int, char**)

// Rendering
ImGui::Render();

ImDrawData* draw_data = ImGui::GetDrawData();
const bool is_minimized = (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f);
if (!is_minimized)
Expand All @@ -539,10 +499,9 @@ int main(int, char**)
}
}

// Cleanup
err = vkDeviceWaitIdle(g_Device);
check_vk_result(err);

// Cleanup
ImGui_ImplVulkan_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
Expand Down
18 changes: 9 additions & 9 deletions examples/imgui_examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example_sdl3_sdlrenderer3",
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example_sdl3_vulkan", "example_sdl3_vulkan\example_sdl3_vulkan.vcxproj", "{663A7E89-1E42-4222-921C-177F5B5910DF}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example_win32_vulkan", "example_win32_vulkan\example_win32_vulkan.vcxproj", "{4CB8C22A-96F8-4D31-B747-FDAA6B742E47}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example_win32_vulkan", "example_win32_vulkan\example_win32_vulkan.vcxproj", "{0A1E32DF-E0F4-4CCE-B3DC-9644C503BD88}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -173,14 +173,14 @@ Global
{663A7E89-1E42-4222-921C-177F5B5910DF}.Release|Win32.Build.0 = Release|Win32
{663A7E89-1E42-4222-921C-177F5B5910DF}.Release|x64.ActiveCfg = Release|x64
{663A7E89-1E42-4222-921C-177F5B5910DF}.Release|x64.Build.0 = Release|x64
{4CB8C22A-96F8-4D31-B747-FDAA6B742E47}.Debug|Win32.ActiveCfg = Debug|Win32
{4CB8C22A-96F8-4D31-B747-FDAA6B742E47}.Debug|Win32.Build.0 = Debug|Win32
{4CB8C22A-96F8-4D31-B747-FDAA6B742E47}.Debug|x64.ActiveCfg = Debug|x64
{4CB8C22A-96F8-4D31-B747-FDAA6B742E47}.Debug|x64.Build.0 = Debug|x64
{4CB8C22A-96F8-4D31-B747-FDAA6B742E47}.Release|Win32.ActiveCfg = Release|Win32
{4CB8C22A-96F8-4D31-B747-FDAA6B742E47}.Release|Win32.Build.0 = Release|Win32
{4CB8C22A-96F8-4D31-B747-FDAA6B742E47}.Release|x64.ActiveCfg = Release|x64
{4CB8C22A-96F8-4D31-B747-FDAA6B742E47}.Release|x64.Build.0 = Release|x64
{0A1E32DF-E0F4-4CCE-B3DC-9644C503BD88}.Debug|Win32.ActiveCfg = Debug|Win32
{0A1E32DF-E0F4-4CCE-B3DC-9644C503BD88}.Debug|Win32.Build.0 = Debug|Win32
{0A1E32DF-E0F4-4CCE-B3DC-9644C503BD88}.Debug|x64.ActiveCfg = Debug|x64
{0A1E32DF-E0F4-4CCE-B3DC-9644C503BD88}.Debug|x64.Build.0 = Debug|x64
{0A1E32DF-E0F4-4CCE-B3DC-9644C503BD88}.Release|Win32.ActiveCfg = Release|Win32
{0A1E32DF-E0F4-4CCE-B3DC-9644C503BD88}.Release|Win32.Build.0 = Release|Win32
{0A1E32DF-E0F4-4CCE-B3DC-9644C503BD88}.Release|x64.ActiveCfg = Release|x64
{0A1E32DF-E0F4-4CCE-B3DC-9644C503BD88}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 93a9307

Please sign in to comment.