1 // dear imgui: standalone example application for SDL2 + Vulkan
2 // If you are new to dear imgui, see examples/README.txt and documentation at the top of imgui.cpp.
3
4 #include "imgui.h"
5 #include "imgui_impl_sdl.h"
6 #include "imgui_impl_vulkan.h"
7 #include <stdio.h> // printf, fprintf
8 #include <stdlib.h> // abort
9 #include <SDL.h>
10 #include <SDL_vulkan.h>
11 #include <vulkan/vulkan.h>
12
13 //#define IMGUI_UNLIMITED_FRAME_RATE
14 #ifdef _DEBUG
15 #define IMGUI_VULKAN_DEBUG_REPORT
16 #endif
17
18 static VkAllocationCallbacks* g_Allocator = NULL;
19 static VkInstance g_Instance = VK_NULL_HANDLE;
20 static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE;
21 static VkDevice g_Device = VK_NULL_HANDLE;
22 static uint32_t g_QueueFamily = (uint32_t)-1;
23 static VkQueue g_Queue = VK_NULL_HANDLE;
24 static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE;
25 static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE;
26 static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
27
28 static ImGui_ImplVulkanH_WindowData g_WindowData;
29
check_vk_result(VkResult err)30 static void check_vk_result(VkResult err)
31 {
32 if (err == 0) return;
33 printf("VkResult %d\n", err);
34 if (err < 0)
35 abort();
36 }
37
38 #ifdef IMGUI_VULKAN_DEBUG_REPORT
debug_report(VkDebugReportFlagsEXT flags,VkDebugReportObjectTypeEXT objectType,uint64_t object,size_t location,int32_t messageCode,const char * pLayerPrefix,const char * pMessage,void * pUserData)39 static VKAPI_ATTR VkBool32 VKAPI_CALL debug_report(VkDebugReportFlagsEXT flags, VkDebugReportObjectTypeEXT objectType, uint64_t object, size_t location, int32_t messageCode, const char* pLayerPrefix, const char* pMessage, void* pUserData)
40 {
41 (void)flags; (void)object; (void)location; (void)messageCode; (void)pUserData; (void)pLayerPrefix; // Unused arguments
42 fprintf(stderr, "[vulkan] ObjectType: %i\nMessage: %s\n\n", objectType, pMessage);
43 return VK_FALSE;
44 }
45 #endif // IMGUI_VULKAN_DEBUG_REPORT
46
SetupVulkan(const char ** extensions,uint32_t extensions_count)47 static void SetupVulkan(const char** extensions, uint32_t extensions_count)
48 {
49 VkResult err;
50
51 // Create Vulkan Instance
52 {
53 VkInstanceCreateInfo create_info = {};
54 create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
55 create_info.enabledExtensionCount = extensions_count;
56 create_info.ppEnabledExtensionNames = extensions;
57
58 #ifdef IMGUI_VULKAN_DEBUG_REPORT
59 // Enabling multiple validation layers grouped as LunarG standard validation
60 const char* layers[] = { "VK_LAYER_LUNARG_standard_validation" };
61 create_info.enabledLayerCount = 1;
62 create_info.ppEnabledLayerNames = layers;
63
64 // Enable debug report extension (we need additional storage, so we duplicate the user array to add our new extension to it)
65 const char** extensions_ext = (const char**)malloc(sizeof(const char*) * (extensions_count + 1));
66 memcpy(extensions_ext, extensions, extensions_count * sizeof(const char*));
67 extensions_ext[extensions_count] = "VK_EXT_debug_report";
68 create_info.enabledExtensionCount = extensions_count + 1;
69 create_info.ppEnabledExtensionNames = extensions_ext;
70
71 // Create Vulkan Instance
72 err = vkCreateInstance(&create_info, g_Allocator, &g_Instance);
73 check_vk_result(err);
74 free(extensions_ext);
75
76 // Get the function pointer (required for any extensions)
77 auto vkCreateDebugReportCallbackEXT = (PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(g_Instance, "vkCreateDebugReportCallbackEXT");
78 IM_ASSERT(vkCreateDebugReportCallbackEXT != NULL);
79
80 // Setup the debug report callback
81 VkDebugReportCallbackCreateInfoEXT debug_report_ci = {};
82 debug_report_ci.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
83 debug_report_ci.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
84 debug_report_ci.pfnCallback = debug_report;
85 debug_report_ci.pUserData = NULL;
86 err = vkCreateDebugReportCallbackEXT(g_Instance, &debug_report_ci, g_Allocator, &g_DebugReport);
87 check_vk_result(err);
88 #else
89 // Create Vulkan Instance without any debug feature
90 err = vkCreateInstance(&create_info, g_Allocator, &g_Instance);
91 check_vk_result(err);
92 #endif
93 }
94
95 // Select GPU
96 {
97 uint32_t gpu_count;
98 err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, NULL);
99 check_vk_result(err);
100
101 VkPhysicalDevice* gpus = (VkPhysicalDevice*)malloc(sizeof(VkPhysicalDevice) * gpu_count);
102 err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, gpus);
103 check_vk_result(err);
104
105 // If a number >1 of GPUs got reported, you should find the best fit GPU for your purpose
106 // e.g. VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU if available, or with the greatest memory available, etc.
107 // for sake of simplicity we'll just take the first one, assuming it has a graphics queue family.
108 g_PhysicalDevice = gpus[0];
109 free(gpus);
110 }
111
112 // Select graphics queue family
113 {
114 uint32_t count;
115 vkGetPhysicalDeviceQueueFamilyProperties(g_PhysicalDevice, &count, NULL);
116 VkQueueFamilyProperties* queues = (VkQueueFamilyProperties*)malloc(sizeof(VkQueueFamilyProperties) * count);
117 vkGetPhysicalDeviceQueueFamilyProperties(g_PhysicalDevice, &count, queues);
118 for (uint32_t i = 0; i < count; i++)
119 if (queues[i].queueFlags & VK_QUEUE_GRAPHICS_BIT)
120 {
121 g_QueueFamily = i;
122 break;
123 }
124 free(queues);
125 IM_ASSERT(g_QueueFamily != -1);
126 }
127
128 // Create Logical Device (with 1 queue)
129 {
130 int device_extension_count = 1;
131 const char* device_extensions[] = { "VK_KHR_swapchain" };
132 const float queue_priority[] = { 1.0f };
133 VkDeviceQueueCreateInfo queue_info[1] = {};
134 queue_info[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
135 queue_info[0].queueFamilyIndex = g_QueueFamily;
136 queue_info[0].queueCount = 1;
137 queue_info[0].pQueuePriorities = queue_priority;
138 VkDeviceCreateInfo create_info = {};
139 create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
140 create_info.queueCreateInfoCount = sizeof(queue_info) / sizeof(queue_info[0]);
141 create_info.pQueueCreateInfos = queue_info;
142 create_info.enabledExtensionCount = device_extension_count;
143 create_info.ppEnabledExtensionNames = device_extensions;
144 err = vkCreateDevice(g_PhysicalDevice, &create_info, g_Allocator, &g_Device);
145 check_vk_result(err);
146 vkGetDeviceQueue(g_Device, g_QueueFamily, 0, &g_Queue);
147 }
148
149 // Create Descriptor Pool
150 {
151 VkDescriptorPoolSize pool_sizes[] =
152 {
153 { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 },
154 { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 },
155 { VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 },
156 { VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 },
157 { VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 },
158 { VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 },
159 { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 },
160 { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 },
161 { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 },
162 { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 },
163 { VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 }
164 };
165 VkDescriptorPoolCreateInfo pool_info = {};
166 pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
167 pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
168 pool_info.maxSets = 1000 * IM_ARRAYSIZE(pool_sizes);
169 pool_info.poolSizeCount = (uint32_t)IM_ARRAYSIZE(pool_sizes);
170 pool_info.pPoolSizes = pool_sizes;
171 err = vkCreateDescriptorPool(g_Device, &pool_info, g_Allocator, &g_DescriptorPool);
172 check_vk_result(err);
173 }
174 }
175
SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData * wd,VkSurfaceKHR surface,int width,int height)176 static void SetupVulkanWindowData(ImGui_ImplVulkanH_WindowData* wd, VkSurfaceKHR surface, int width, int height)
177 {
178 wd->Surface = surface;
179
180 // Check for WSI support
181 VkBool32 res;
182 vkGetPhysicalDeviceSurfaceSupportKHR(g_PhysicalDevice, g_QueueFamily, wd->Surface, &res);
183 if (res != VK_TRUE)
184 {
185 fprintf(stderr, "Error no WSI support on physical device 0\n");
186 exit(-1);
187 }
188
189 // Select Surface Format
190 const VkFormat requestSurfaceImageFormat[] = { VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_B8G8R8_UNORM, VK_FORMAT_R8G8B8_UNORM };
191 const VkColorSpaceKHR requestSurfaceColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
192 wd->SurfaceFormat = ImGui_ImplVulkanH_SelectSurfaceFormat(g_PhysicalDevice, wd->Surface, requestSurfaceImageFormat, (size_t)IM_ARRAYSIZE(requestSurfaceImageFormat), requestSurfaceColorSpace);
193
194 // Select Present Mode
195 #ifdef IMGUI_UNLIMITED_FRAME_RATE
196 VkPresentModeKHR present_modes[] = { VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_FIFO_KHR };
197 #else
198 VkPresentModeKHR present_modes[] = { VK_PRESENT_MODE_FIFO_KHR };
199 #endif
200 wd->PresentMode = ImGui_ImplVulkanH_SelectPresentMode(g_PhysicalDevice, wd->Surface, &present_modes[0], IM_ARRAYSIZE(present_modes));
201 //printf("[vulkan] Selected PresentMode = %d\n", wd->PresentMode);
202
203 // Create SwapChain, RenderPass, Framebuffer, etc.
204 ImGui_ImplVulkanH_CreateWindowDataCommandBuffers(g_PhysicalDevice, g_Device, g_QueueFamily, wd, g_Allocator);
205 ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, wd, g_Allocator, width, height);
206 }
207
CleanupVulkan()208 static void CleanupVulkan()
209 {
210 ImGui_ImplVulkanH_WindowData* wd = &g_WindowData;
211 ImGui_ImplVulkanH_DestroyWindowData(g_Instance, g_Device, wd, g_Allocator);
212 vkDestroyDescriptorPool(g_Device, g_DescriptorPool, g_Allocator);
213
214 #ifdef IMGUI_VULKAN_DEBUG_REPORT
215 // Remove the debug report callback
216 auto vkDestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(g_Instance, "vkDestroyDebugReportCallbackEXT");
217 vkDestroyDebugReportCallbackEXT(g_Instance, g_DebugReport, g_Allocator);
218 #endif // IMGUI_VULKAN_DEBUG_REPORT
219
220 vkDestroyDevice(g_Device, g_Allocator);
221 vkDestroyInstance(g_Instance, g_Allocator);
222 }
223
FrameRender(ImGui_ImplVulkanH_WindowData * wd)224 static void FrameRender(ImGui_ImplVulkanH_WindowData* wd)
225 {
226 VkResult err;
227
228 VkSemaphore& image_acquired_semaphore = wd->Frames[wd->FrameIndex].ImageAcquiredSemaphore;
229 err = vkAcquireNextImageKHR(g_Device, wd->Swapchain, UINT64_MAX, image_acquired_semaphore, VK_NULL_HANDLE, &wd->FrameIndex);
230 check_vk_result(err);
231
232 ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[wd->FrameIndex];
233 {
234 err = vkWaitForFences(g_Device, 1, &fd->Fence, VK_TRUE, UINT64_MAX); // wait indefinitely instead of periodically checking
235 check_vk_result(err);
236
237 err = vkResetFences(g_Device, 1, &fd->Fence);
238 check_vk_result(err);
239 }
240 {
241 err = vkResetCommandPool(g_Device, fd->CommandPool, 0);
242 check_vk_result(err);
243 VkCommandBufferBeginInfo info = {};
244 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
245 info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
246 err = vkBeginCommandBuffer(fd->CommandBuffer, &info);
247 check_vk_result(err);
248 }
249 {
250 VkRenderPassBeginInfo info = {};
251 info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
252 info.renderPass = wd->RenderPass;
253 info.framebuffer = wd->Framebuffer[wd->FrameIndex];
254 info.renderArea.extent.width = wd->Width;
255 info.renderArea.extent.height = wd->Height;
256 info.clearValueCount = 1;
257 info.pClearValues = &wd->ClearValue;
258 vkCmdBeginRenderPass(fd->CommandBuffer, &info, VK_SUBPASS_CONTENTS_INLINE);
259 }
260
261 // Record Imgui Draw Data and draw funcs into command buffer
262 ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), fd->CommandBuffer);
263
264 // Submit command buffer
265 vkCmdEndRenderPass(fd->CommandBuffer);
266 {
267 VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
268 VkSubmitInfo info = {};
269 info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
270 info.waitSemaphoreCount = 1;
271 info.pWaitSemaphores = &image_acquired_semaphore;
272 info.pWaitDstStageMask = &wait_stage;
273 info.commandBufferCount = 1;
274 info.pCommandBuffers = &fd->CommandBuffer;
275 info.signalSemaphoreCount = 1;
276 info.pSignalSemaphores = &fd->RenderCompleteSemaphore;
277
278 err = vkEndCommandBuffer(fd->CommandBuffer);
279 check_vk_result(err);
280 err = vkQueueSubmit(g_Queue, 1, &info, fd->Fence);
281 check_vk_result(err);
282 }
283 }
284
FramePresent(ImGui_ImplVulkanH_WindowData * wd)285 static void FramePresent(ImGui_ImplVulkanH_WindowData* wd)
286 {
287 ImGui_ImplVulkanH_FrameData* fd = &wd->Frames[wd->FrameIndex];
288 VkPresentInfoKHR info = {};
289 info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
290 info.waitSemaphoreCount = 1;
291 info.pWaitSemaphores = &fd->RenderCompleteSemaphore;
292 info.swapchainCount = 1;
293 info.pSwapchains = &wd->Swapchain;
294 info.pImageIndices = &wd->FrameIndex;
295 VkResult err = vkQueuePresentKHR(g_Queue, &info);
296 check_vk_result(err);
297 }
298
main(int,char **)299 int main(int, char**)
300 {
301 // Setup SDL
302 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0)
303 {
304 printf("Error: %s\n", SDL_GetError());
305 return 1;
306 }
307
308 // Setup window
309 SDL_DisplayMode current;
310 SDL_GetCurrentDisplayMode(0, ¤t);
311 SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+Vulkan example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_VULKAN|SDL_WINDOW_RESIZABLE);
312
313 // Setup Vulkan
314 uint32_t extensions_count = 0;
315 SDL_Vulkan_GetInstanceExtensions(window, &extensions_count, NULL);
316 const char** extensions = new const char*[extensions_count];
317 SDL_Vulkan_GetInstanceExtensions(window, &extensions_count, extensions);
318 SetupVulkan(extensions, extensions_count);
319 delete[] extensions;
320
321 // Create Window Surface
322 VkSurfaceKHR surface;
323 VkResult err;
324 if (SDL_Vulkan_CreateSurface(window, g_Instance, &surface) == 0)
325 {
326 printf("Failed to create Vulkan surface.\n");
327 return 1;
328 }
329
330 // Create Framebuffers
331 int w, h;
332 SDL_GetWindowSize(window, &w, &h);
333 ImGui_ImplVulkanH_WindowData* wd = &g_WindowData;
334 SetupVulkanWindowData(wd, surface, w, h);
335
336 // Setup Dear ImGui context
337 ImGui::CreateContext();
338 ImGuiIO& io = ImGui::GetIO(); (void)io;
339 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
340
341 // Setup Dear ImGui style
342 ImGui::StyleColorsDark();
343 //ImGui::StyleColorsClassic();
344
345 // Setup Platform/Renderer bindings
346 ImGui_ImplSDL2_InitForVulkan(window);
347 ImGui_ImplVulkan_InitInfo init_info = {};
348 init_info.Instance = g_Instance;
349 init_info.PhysicalDevice = g_PhysicalDevice;
350 init_info.Device = g_Device;
351 init_info.QueueFamily = g_QueueFamily;
352 init_info.Queue = g_Queue;
353 init_info.PipelineCache = g_PipelineCache;
354 init_info.DescriptorPool = g_DescriptorPool;
355 init_info.Allocator = g_Allocator;
356 init_info.CheckVkResultFn = check_vk_result;
357 ImGui_ImplVulkan_Init(&init_info, wd->RenderPass);
358
359 // Load Fonts
360 // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
361 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
362 // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
363 // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
364 // - Read 'misc/fonts/README.txt' for more instructions and details.
365 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
366 //io.Fonts->AddFontDefault();
367 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
368 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
369 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
370 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
371 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
372 //IM_ASSERT(font != NULL);
373
374 // Upload Fonts
375 {
376 // Use any command queue
377 VkCommandPool command_pool = wd->Frames[wd->FrameIndex].CommandPool;
378 VkCommandBuffer command_buffer = wd->Frames[wd->FrameIndex].CommandBuffer;
379
380 err = vkResetCommandPool(g_Device, command_pool, 0);
381 check_vk_result(err);
382 VkCommandBufferBeginInfo begin_info = {};
383 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
384 begin_info.flags |= VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
385 err = vkBeginCommandBuffer(command_buffer, &begin_info);
386 check_vk_result(err);
387
388 ImGui_ImplVulkan_CreateFontsTexture(command_buffer);
389
390 VkSubmitInfo end_info = {};
391 end_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
392 end_info.commandBufferCount = 1;
393 end_info.pCommandBuffers = &command_buffer;
394 err = vkEndCommandBuffer(command_buffer);
395 check_vk_result(err);
396 err = vkQueueSubmit(g_Queue, 1, &end_info, VK_NULL_HANDLE);
397 check_vk_result(err);
398
399 err = vkDeviceWaitIdle(g_Device);
400 check_vk_result(err);
401 ImGui_ImplVulkan_InvalidateFontUploadObjects();
402 }
403
404 bool show_demo_window = true;
405 bool show_another_window = false;
406 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
407
408 // Main loop
409 bool done = false;
410 while (!done)
411 {
412 // Poll and handle events (inputs, window resize, etc.)
413 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
414 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
415 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
416 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
417 SDL_Event event;
418 while (SDL_PollEvent(&event))
419 {
420 ImGui_ImplSDL2_ProcessEvent(&event);
421 if (event.type == SDL_QUIT)
422 done = true;
423 if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED && event.window.windowID == SDL_GetWindowID(window))
424 ImGui_ImplVulkanH_CreateWindowDataSwapChainAndFramebuffer(g_PhysicalDevice, g_Device, &g_WindowData, g_Allocator, (int)event.window.data1, (int)event.window.data2);
425 }
426
427 // Start the Dear ImGui frame
428 ImGui_ImplVulkan_NewFrame();
429 ImGui_ImplSDL2_NewFrame(window);
430 ImGui::NewFrame();
431
432 // 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!).
433 if (show_demo_window)
434 ImGui::ShowDemoWindow(&show_demo_window);
435
436 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
437 {
438 static float f = 0.0f;
439 static int counter = 0;
440
441 ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
442
443 ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
444 ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
445 ImGui::Checkbox("Another Window", &show_another_window);
446
447 ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
448 ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
449
450 if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
451 counter++;
452 ImGui::SameLine();
453 ImGui::Text("counter = %d", counter);
454
455 ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
456 ImGui::End();
457 }
458
459 // 3. Show another simple window.
460 if (show_another_window)
461 {
462 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)
463 ImGui::Text("Hello from another window!");
464 if (ImGui::Button("Close Me"))
465 show_another_window = false;
466 ImGui::End();
467 }
468
469 // Rendering
470 ImGui::Render();
471 memcpy(&wd->ClearValue.color.float32[0], &clear_color, 4 * sizeof(float));
472 FrameRender(wd);
473
474 FramePresent(wd);
475 }
476
477 // Cleanup
478 err = vkDeviceWaitIdle(g_Device);
479 check_vk_result(err);
480 ImGui_ImplVulkan_Shutdown();
481 ImGui_ImplSDL2_Shutdown();
482 ImGui::DestroyContext();
483 CleanupVulkan();
484
485 SDL_DestroyWindow(window);
486 SDL_Quit();
487
488 return 0;
489 }
490