mirror of
https://github.com/Drezil/imgui.git
synced 2025-07-04 03:58:47 +02:00
Merge branch 'master' into 2016-07-navigation
This commit is contained in:
8
examples/.gitignore
vendored
8
examples/.gitignore
vendored
@ -25,6 +25,14 @@ opengl3_example/Release/*
|
||||
opengl3_example/ipch/*
|
||||
opengl3_example/x64/*
|
||||
opengl3_example/opengl3_example
|
||||
sdl_opengl2_example/Debug/*
|
||||
sdl_opengl2_example/Release/*
|
||||
sdl_opengl2_example/ipch/*
|
||||
sdl_opengl2_example/x64/*
|
||||
sdl_opengl3_example/Debug/*
|
||||
sdl_opengl3_example/Release/*
|
||||
sdl_opengl3_example/ipch/*
|
||||
sdl_opengl3_example/x64/*
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.suo
|
||||
|
3
examples/sdl_opengl2_example/build_win32.bat
Normal file
3
examples/sdl_opengl2_example/build_win32.bat
Normal file
@ -0,0 +1,3 @@
|
||||
@REM Build for Visual Studio compiler. Run your copy of vcvars32.bat or vcvarsall.bat to setup command-line compiler.
|
||||
mkdir Debug
|
||||
cl /nologo /Zi /MD /I ..\.. /I ..\libs\gl3w /I %SDL_DIR%\include main.cpp imgui_impl_sdl.cpp ..\..\imgui*.cpp ..\libs\gl3w\GL\gl3w.c /FeDebug/sdl_opengl2_example.exe /FoDebug/ /link /libpath:%SDL_DIR%\lib\x86 SDL2.lib SDL2main.lib opengl32.lib /subsystem:console
|
3
examples/sdl_opengl3_example/build_win32.bat
Normal file
3
examples/sdl_opengl3_example/build_win32.bat
Normal file
@ -0,0 +1,3 @@
|
||||
@REM Build for Visual Studio compiler. Run your copy of vcvars32.bat or vcvarsall.bat to setup command-line compiler.
|
||||
mkdir Debug
|
||||
cl /nologo /Zi /MD /I ..\.. /I ..\libs\gl3w /I %SDL_DIR%\include main.cpp imgui_impl_sdl_gl3.cpp ..\..\imgui*.cpp ..\libs\gl3w\GL\gl3w.c /FeDebug/sdl_opengl3_example.exe /FoDebug/ /link /libpath:%SDL_DIR%\lib\x86 SDL2.lib SDL2main.lib opengl32.lib /subsystem:console
|
@ -1,4 +1,4 @@
|
||||
@REM Build for Visual Studio compiler. Run your copy of vcvars32.bat or vcvarsall.bat to setup command-line compiler.
|
||||
mkdir Debug
|
||||
cl /nologo /Zi /MD /I ..\.. /I ..\libs\glfw\include /I %VULKAN_SDK%\include *.cpp ..\..\*.cpp /FeDebug/vulkan_example.exe /FoDebug/ /link /LIBPATH:..\libs\glfw\lib-vc2010-32 /libpath:%VULKAN_SDK%\bin32 glfw3.lib opengl32.lib gdi32.lib shell32.lib vulkan-1.lib
|
||||
cl /nologo /Zi /MD /I ..\.. /I ..\libs\glfw\include /I %VULKAN_SDK%\include *.cpp ..\..\*.cpp /FeDebug/vulkan_example.exe /FoDebug/ /link /LIBPATH:..\libs\glfw\lib-vc2010-32 /libpath:%VULKAN_SDK%\lib32 glfw3.lib opengl32.lib gdi32.lib shell32.lib vulkan-1.lib
|
||||
|
||||
|
4
examples/vulkan_example/build_win64.bat
Normal file
4
examples/vulkan_example/build_win64.bat
Normal file
@ -0,0 +1,4 @@
|
||||
@REM Build for Visual Studio compiler. Run your copy of amd64/vcvars32.bat to setup 64-bit command-line compiler.
|
||||
mkdir Debug
|
||||
cl /nologo /Zi /MD /I ..\.. /I ..\libs\glfw\include /I %VULKAN_SDK%\include *.cpp ..\..\*.cpp /FeDebug/vulkan_example.exe /FoDebug/ /link /LIBPATH:..\libs\glfw\lib-vc2010-64 /libpath:%VULKAN_SDK%\lib glfw3.lib opengl32.lib gdi32.lib shell32.lib vulkan-1.lib
|
||||
|
@ -236,10 +236,10 @@ void ImGui_ImplGlfwVulkan_RenderDrawLists(ImDrawData* draw_data)
|
||||
VkMappedMemoryRange range[2] = {};
|
||||
range[0].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
|
||||
range[0].memory = g_VertexBufferMemory[g_FrameIndex];
|
||||
range[0].size = vertex_size;
|
||||
range[0].size = VK_WHOLE_SIZE;
|
||||
range[1].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
|
||||
range[1].memory = g_IndexBufferMemory[g_FrameIndex];
|
||||
range[1].size = index_size;
|
||||
range[1].size = VK_WHOLE_SIZE;
|
||||
err = vkFlushMappedMemoryRanges(g_Device, 2, range);
|
||||
ImGui_ImplGlfwVulkan_VkResult(err);
|
||||
vkUnmapMemory(g_Device, g_VertexBufferMemory[g_FrameIndex]);
|
||||
@ -301,10 +301,10 @@ void ImGui_ImplGlfwVulkan_RenderDrawLists(ImDrawData* draw_data)
|
||||
else
|
||||
{
|
||||
VkRect2D scissor;
|
||||
scissor.offset.x = (int32_t)(pcmd->ClipRect.x);
|
||||
scissor.offset.y = (int32_t)(pcmd->ClipRect.y);
|
||||
scissor.offset.x = (int32_t)(pcmd->ClipRect.x) > 0 ? (int32_t)(pcmd->ClipRect.x) : 0;
|
||||
scissor.offset.y = (int32_t)(pcmd->ClipRect.y) > 0 ? (int32_t)(pcmd->ClipRect.y) : 0;
|
||||
scissor.extent.width = (uint32_t)(pcmd->ClipRect.z - pcmd->ClipRect.x);
|
||||
scissor.extent.height = (uint32_t)(pcmd->ClipRect.w - pcmd->ClipRect.y + 1); // TODO: + 1??????
|
||||
scissor.extent.height = (uint32_t)(pcmd->ClipRect.w - pcmd->ClipRect.y + 1); // FIXME: Why +1 here?
|
||||
vkCmdSetScissor(g_CommandBuffer, 0, 1, &scissor);
|
||||
vkCmdDrawIndexed(g_CommandBuffer, pcmd->ElemCount, 1, idx_offset, vtx_offset, 0);
|
||||
}
|
||||
@ -483,6 +483,7 @@ bool ImGui_ImplGlfwVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
|
||||
region.imageSubresource.layerCount = 1;
|
||||
region.imageExtent.width = width;
|
||||
region.imageExtent.height = height;
|
||||
region.imageExtent.depth = 1;
|
||||
vkCmdCopyBufferToImage(command_buffer, g_UploadBuffer, g_FontImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
||||
|
||||
VkImageMemoryBarrier use_barrier[1] = {};
|
||||
@ -540,6 +541,7 @@ bool ImGui_ImplGlfwVulkan_CreateDeviceObjects()
|
||||
info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
||||
info.minLod = -1000;
|
||||
info.maxLod = 1000;
|
||||
info.maxAnisotropy = 1.0f;
|
||||
err = vkCreateSampler(g_Device, &info, g_Allocator, &g_FontSampler);
|
||||
ImGui_ImplGlfwVulkan_VkResult(err);
|
||||
}
|
||||
|
@ -12,6 +12,10 @@
|
||||
#include "imgui_impl_glfw_vulkan.h"
|
||||
|
||||
#define IMGUI_MAX_POSSIBLE_BACK_BUFFERS 16
|
||||
#define IMGUI_UNLIMITED_FRAME_RATE
|
||||
//#ifdef _DEBUG
|
||||
//#define IMGUI_VULKAN_DEBUG_REPORT
|
||||
//#endif
|
||||
|
||||
static VkAllocationCallbacks* g_Allocator = NULL;
|
||||
static VkInstance g_Instance = VK_NULL_HANDLE;
|
||||
@ -22,17 +26,17 @@ static VkSwapchainKHR g_Swapchain = VK_NULL_HANDLE;
|
||||
static VkRenderPass g_RenderPass = VK_NULL_HANDLE;
|
||||
static uint32_t g_QueueFamily = 0;
|
||||
static VkQueue g_Queue = VK_NULL_HANDLE;
|
||||
static VkDebugReportCallbackEXT g_Debug_Report = VK_NULL_HANDLE;
|
||||
|
||||
static VkFormat g_ImageFormat = VK_FORMAT_B8G8R8A8_UNORM;
|
||||
static VkFormat g_ViewFormat = VK_FORMAT_B8G8R8A8_UNORM;
|
||||
static VkColorSpaceKHR g_ColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
|
||||
static VkSurfaceFormatKHR g_SurfaceFormat;
|
||||
static VkImageSubresourceRange g_ImageRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
|
||||
static VkPresentModeKHR g_PresentMode;
|
||||
|
||||
static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE;
|
||||
static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
|
||||
|
||||
static int fb_width, fb_height;
|
||||
static uint32_t g_BackBufferIndex = 0;
|
||||
static uint32_t g_BackbufferIndices[IMGUI_VK_QUEUED_FRAMES]; // keep track of recently rendered swapchain frame indices
|
||||
static uint32_t g_BackBufferCount = 0;
|
||||
static VkImage g_BackBuffer[IMGUI_MAX_POSSIBLE_BACK_BUFFERS] = {};
|
||||
static VkImageView g_BackBufferView[IMGUI_MAX_POSSIBLE_BACK_BUFFERS] = {};
|
||||
@ -42,7 +46,8 @@ static uint32_t g_FrameIndex = 0;
|
||||
static VkCommandPool g_CommandPool[IMGUI_VK_QUEUED_FRAMES];
|
||||
static VkCommandBuffer g_CommandBuffer[IMGUI_VK_QUEUED_FRAMES];
|
||||
static VkFence g_Fence[IMGUI_VK_QUEUED_FRAMES];
|
||||
static VkSemaphore g_Semaphore[IMGUI_VK_QUEUED_FRAMES];
|
||||
static VkSemaphore g_PresentCompleteSemaphore[IMGUI_VK_QUEUED_FRAMES];
|
||||
static VkSemaphore g_RenderCompleteSemaphore[IMGUI_VK_QUEUED_FRAMES];
|
||||
|
||||
static VkClearValue g_ClearValue = {};
|
||||
|
||||
@ -76,14 +81,14 @@ static void resize_vulkan(GLFWwindow* /*window*/, int w, int h)
|
||||
VkSwapchainCreateInfoKHR info = {};
|
||||
info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
||||
info.surface = g_Surface;
|
||||
info.imageFormat = g_ImageFormat;
|
||||
info.imageColorSpace = g_ColorSpace;
|
||||
info.imageFormat = g_SurfaceFormat.format;
|
||||
info.imageColorSpace = g_SurfaceFormat.colorSpace;
|
||||
info.imageArrayLayers = 1;
|
||||
info.imageUsage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
||||
info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
||||
info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
||||
info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
||||
info.presentMode = VK_PRESENT_MODE_FIFO_KHR;
|
||||
info.presentMode = g_PresentMode;
|
||||
info.clipped = VK_TRUE;
|
||||
info.oldSwapchain = old_swapchain;
|
||||
VkSurfaceCapabilitiesKHR cap;
|
||||
@ -93,6 +98,7 @@ static void resize_vulkan(GLFWwindow* /*window*/, int w, int h)
|
||||
info.minImageCount = (cap.minImageCount + 2 < cap.maxImageCount) ? (cap.minImageCount + 2) : cap.maxImageCount;
|
||||
else
|
||||
info.minImageCount = cap.minImageCount + 2;
|
||||
|
||||
if (cap.currentExtent.width == 0xffffffff)
|
||||
{
|
||||
fb_width = w;
|
||||
@ -120,14 +126,14 @@ static void resize_vulkan(GLFWwindow* /*window*/, int w, int h)
|
||||
// Create the Render Pass:
|
||||
{
|
||||
VkAttachmentDescription attachment = {};
|
||||
attachment.format = g_ViewFormat;
|
||||
attachment.format = g_SurfaceFormat.format;
|
||||
attachment.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
||||
attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
||||
attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
||||
attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
||||
attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
attachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||
attachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
VkAttachmentReference color_attachment = {};
|
||||
color_attachment.attachment = 0;
|
||||
color_attachment.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||
@ -150,7 +156,7 @@ static void resize_vulkan(GLFWwindow* /*window*/, int w, int h)
|
||||
VkImageViewCreateInfo info = {};
|
||||
info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||
info.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||
info.format = g_ViewFormat;
|
||||
info.format = g_SurfaceFormat.format;
|
||||
info.components.r = VK_COMPONENT_SWIZZLE_R;
|
||||
info.components.g = VK_COMPONENT_SWIZZLE_G;
|
||||
info.components.b = VK_COMPONENT_SWIZZLE_B;
|
||||
@ -184,20 +190,64 @@ static void resize_vulkan(GLFWwindow* /*window*/, int w, int h)
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef IMGUI_VULKAN_DEBUG_REPORT
|
||||
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)
|
||||
{
|
||||
printf("[vulkan] ObjectType: %i\nMessage: %s\n\n", objectType, pMessage );
|
||||
return VK_FALSE;
|
||||
}
|
||||
#endif // IMGUI_VULKAN_DEBUG_REPORT
|
||||
|
||||
static void setup_vulkan(GLFWwindow* window)
|
||||
{
|
||||
VkResult err;
|
||||
|
||||
// Create Vulkan Instance
|
||||
{
|
||||
uint32_t glfw_extensions_count;
|
||||
const char** glfw_extensions = glfwGetRequiredInstanceExtensions(&glfw_extensions_count);
|
||||
uint32_t extensions_count;
|
||||
const char** glfw_extensions = glfwGetRequiredInstanceExtensions(&extensions_count);
|
||||
|
||||
VkInstanceCreateInfo create_info = {};
|
||||
create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
||||
create_info.enabledExtensionCount = glfw_extensions_count;
|
||||
create_info.enabledExtensionCount = extensions_count;
|
||||
create_info.ppEnabledExtensionNames = glfw_extensions;
|
||||
|
||||
#ifdef IMGUI_VULKAN_DEBUG_REPORT
|
||||
// enabling multiple validation layers grouped as lunarg standard validation
|
||||
const char* layers[] = {"VK_LAYER_LUNARG_standard_validation"};
|
||||
create_info.enabledLayerCount = 1;
|
||||
create_info.ppEnabledLayerNames = layers;
|
||||
|
||||
// need additional storage for char pointer to debug report extension
|
||||
const char** extensions = (const char**)malloc(sizeof(const char*) * (extensions_count + 1));
|
||||
for (size_t i = 0; i < extensions_count; i++)
|
||||
extensions[i] = glfw_extensions[i];
|
||||
extensions[ extensions_count ] = "VK_EXT_debug_report";
|
||||
create_info.enabledExtensionCount = extensions_count+1;
|
||||
create_info.ppEnabledExtensionNames = extensions;
|
||||
#endif // IMGUI_VULKAN_DEBUG_REPORT
|
||||
|
||||
err = vkCreateInstance(&create_info, g_Allocator, &g_Instance);
|
||||
check_vk_result(err);
|
||||
|
||||
#ifdef IMGUI_VULKAN_DEBUG_REPORT
|
||||
free(extensions);
|
||||
|
||||
// create the debug report callback
|
||||
VkDebugReportCallbackCreateInfoEXT debug_report_ci ={};
|
||||
debug_report_ci.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT;
|
||||
debug_report_ci.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT;
|
||||
debug_report_ci.pfnCallback = debug_report;
|
||||
debug_report_ci.pUserData = NULL;
|
||||
|
||||
// get the proc address of the function pointer, required for used extensions
|
||||
PFN_vkCreateDebugReportCallbackEXT vkCreateDebugReportCallbackEXT =
|
||||
(PFN_vkCreateDebugReportCallbackEXT)vkGetInstanceProcAddr(g_Instance, "vkCreateDebugReportCallbackEXT");
|
||||
|
||||
err = vkCreateDebugReportCallbackEXT( g_Instance, &debug_report_ci, g_Allocator, &g_Debug_Report );
|
||||
check_vk_result(err);
|
||||
#endif // IMGUI_VULKAN_DEBUG_REPORT
|
||||
}
|
||||
|
||||
// Create Window Surface
|
||||
@ -206,11 +256,21 @@ static void setup_vulkan(GLFWwindow* window)
|
||||
check_vk_result(err);
|
||||
}
|
||||
|
||||
// Get GPU (WARNING here we assume the first gpu is one we can use)
|
||||
// Get GPU
|
||||
{
|
||||
uint32_t count = 1;
|
||||
err = vkEnumeratePhysicalDevices(g_Instance, &count, &g_Gpu);
|
||||
uint32_t gpu_count;
|
||||
err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, NULL);
|
||||
check_vk_result(err);
|
||||
|
||||
VkPhysicalDevice* gpus = (VkPhysicalDevice*)malloc(sizeof(VkPhysicalDevice) * gpu_count);
|
||||
err = vkEnumeratePhysicalDevices(g_Instance, &gpu_count, gpus);
|
||||
check_vk_result(err);
|
||||
|
||||
// If a number >1 of GPUs got reported, you should find the best fit GPU for your purpose
|
||||
// e.g. VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU if available, or with the greatest memory available, etc.
|
||||
// for sake of simplicity we'll just take the first one, assuming it has a graphics queue family.
|
||||
g_Gpu = gpus[0];
|
||||
free(gpus);
|
||||
}
|
||||
|
||||
// Get queue
|
||||
@ -243,26 +303,83 @@ static void setup_vulkan(GLFWwindow* window)
|
||||
|
||||
// Get Surface Format
|
||||
{
|
||||
VkFormat image_view_format[][2] = {{VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_B8G8R8A8_UNORM}, {VK_FORMAT_B8G8R8A8_SRGB, VK_FORMAT_B8G8R8A8_UNORM}};
|
||||
// Per Spec Format and View Format are expected to be the same unless VK_IMAGE_CREATE_MUTABLE_BIT was set at image creation
|
||||
// Assuming that the default behavior is without setting this bit, there is no need for separate Spawchain image and image view format
|
||||
// additionally several new color spaces were introduced with Vulkan Spec v1.0.40
|
||||
// hence we must make sure that a format with the mostly available color space, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR, is found and used
|
||||
uint32_t count;
|
||||
vkGetPhysicalDeviceSurfaceFormatsKHR(g_Gpu, g_Surface, &count, NULL);
|
||||
VkSurfaceFormatKHR *formats = (VkSurfaceFormatKHR*)malloc(sizeof(VkSurfaceFormatKHR) * count);
|
||||
vkGetPhysicalDeviceSurfaceFormatsKHR(g_Gpu, g_Surface, &count, formats);
|
||||
for (size_t i = 0; i < sizeof(image_view_format) / sizeof(image_view_format[0]); i++)
|
||||
|
||||
// first check if only one format, VK_FORMAT_UNDEFINED, is available, which would imply that any format is available
|
||||
if (count == 1)
|
||||
{
|
||||
for (uint32_t j = 0; j < count; j++)
|
||||
if( formats[0].format == VK_FORMAT_UNDEFINED )
|
||||
{
|
||||
if (formats[j].format == image_view_format[i][0])
|
||||
g_SurfaceFormat.format = VK_FORMAT_B8G8R8A8_UNORM;
|
||||
g_SurfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
|
||||
}
|
||||
else
|
||||
{ // no point in searching another format
|
||||
g_SurfaceFormat = formats[0];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// request several formats, the first found will be used
|
||||
VkFormat requestSurfaceImageFormat[] = {VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_B8G8R8_UNORM, VK_FORMAT_R8G8B8_UNORM};
|
||||
VkColorSpaceKHR requestSurfaceColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
|
||||
bool requestedFound = false;
|
||||
for (size_t i = 0; i < sizeof(requestSurfaceImageFormat) / sizeof(requestSurfaceImageFormat[0]); i++)
|
||||
{
|
||||
if( requestedFound ) {
|
||||
break;
|
||||
}
|
||||
for (uint32_t j = 0; j < count; j++)
|
||||
{
|
||||
g_ImageFormat = image_view_format[i][0];
|
||||
g_ViewFormat = image_view_format[i][1];
|
||||
g_ColorSpace = formats[j].colorSpace;
|
||||
if (formats[j].format == requestSurfaceImageFormat[i] && formats[j].colorSpace == requestSurfaceColorSpace)
|
||||
{
|
||||
g_SurfaceFormat = formats[j];
|
||||
requestedFound = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if none of the requested image formats could be found, use the first available
|
||||
if (!requestedFound)
|
||||
g_SurfaceFormat = formats[0];
|
||||
}
|
||||
free(formats);
|
||||
}
|
||||
|
||||
|
||||
// Get Present Mode
|
||||
{
|
||||
// Requst a certain mode and confirm that it is available. If not use VK_PRESENT_MODE_FIFO_KHR which is mandatory
|
||||
#ifdef IMGUI_UNLIMITED_FRAME_RATE
|
||||
g_PresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
|
||||
#else
|
||||
g_PresentMode = VK_PRESENT_MODE_FIFO_KHR;
|
||||
#endif
|
||||
uint32_t count = 0;
|
||||
vkGetPhysicalDeviceSurfacePresentModesKHR(g_Gpu, g_Surface, &count, nullptr);
|
||||
VkPresentModeKHR* presentModes = (VkPresentModeKHR*)malloc(sizeof(VkQueueFamilyProperties) * count);
|
||||
vkGetPhysicalDeviceSurfacePresentModesKHR(g_Gpu, g_Surface, &count, presentModes);
|
||||
bool presentModeAvailable = false;
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
if (presentModes[i] == g_PresentMode)
|
||||
{
|
||||
presentModeAvailable = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( !presentModeAvailable )
|
||||
g_PresentMode = VK_PRESENT_MODE_FIFO_KHR; // always available
|
||||
}
|
||||
|
||||
|
||||
// Create Logical Device
|
||||
{
|
||||
int device_extension_count = 1;
|
||||
@ -324,7 +441,9 @@ static void setup_vulkan(GLFWwindow* window)
|
||||
{
|
||||
VkSemaphoreCreateInfo info = {};
|
||||
info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
||||
err = vkCreateSemaphore(g_Device, &info, g_Allocator, &g_Semaphore[i]);
|
||||
err = vkCreateSemaphore(g_Device, &info, g_Allocator, &g_PresentCompleteSemaphore[i]);
|
||||
check_vk_result(err);
|
||||
err = vkCreateSemaphore(g_Device, &info, g_Allocator, &g_RenderCompleteSemaphore[i]);
|
||||
check_vk_result(err);
|
||||
}
|
||||
}
|
||||
@ -364,7 +483,8 @@ static void cleanup_vulkan()
|
||||
vkDestroyFence(g_Device, g_Fence[i], g_Allocator);
|
||||
vkFreeCommandBuffers(g_Device, g_CommandPool[i], 1, &g_CommandBuffer[i]);
|
||||
vkDestroyCommandPool(g_Device, g_CommandPool[i], g_Allocator);
|
||||
vkDestroySemaphore(g_Device, g_Semaphore[i], g_Allocator);
|
||||
vkDestroySemaphore(g_Device, g_PresentCompleteSemaphore[i], g_Allocator);
|
||||
vkDestroySemaphore(g_Device, g_RenderCompleteSemaphore[i], g_Allocator);
|
||||
}
|
||||
for (uint32_t i = 0; i < g_BackBufferCount; i++)
|
||||
{
|
||||
@ -374,6 +494,13 @@ static void cleanup_vulkan()
|
||||
vkDestroyRenderPass(g_Device, g_RenderPass, g_Allocator);
|
||||
vkDestroySwapchainKHR(g_Device, g_Swapchain, g_Allocator);
|
||||
vkDestroySurfaceKHR(g_Instance, g_Surface, g_Allocator);
|
||||
|
||||
#ifdef IMGUI_VULKAN_DEBUG_REPORT
|
||||
// get the proc address of the function pointer, required for used extensions
|
||||
auto vkDestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(g_Instance, "vkDestroyDebugReportCallbackEXT");
|
||||
vkDestroyDebugReportCallbackEXT(g_Instance, g_Debug_Report, g_Allocator);
|
||||
#endif // IMGUI_VULKAN_DEBUG_REPORT
|
||||
|
||||
vkDestroyDevice(g_Device, g_Allocator);
|
||||
vkDestroyInstance(g_Instance, g_Allocator);
|
||||
}
|
||||
@ -389,7 +516,7 @@ static void frame_begin()
|
||||
check_vk_result(err);
|
||||
}
|
||||
{
|
||||
err = vkAcquireNextImageKHR(g_Device, g_Swapchain, UINT64_MAX, g_Semaphore[g_FrameIndex], VK_NULL_HANDLE, &g_BackBufferIndex);
|
||||
err = vkAcquireNextImageKHR(g_Device, g_Swapchain, UINT64_MAX, g_PresentCompleteSemaphore[g_FrameIndex], VK_NULL_HANDLE, &g_BackbufferIndices[g_FrameIndex]);
|
||||
check_vk_result(err);
|
||||
}
|
||||
{
|
||||
@ -405,7 +532,7 @@ static void frame_begin()
|
||||
VkRenderPassBeginInfo info = {};
|
||||
info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
|
||||
info.renderPass = g_RenderPass;
|
||||
info.framebuffer = g_Framebuffer[g_BackBufferIndex];
|
||||
info.framebuffer = g_Framebuffer[g_BackbufferIndices[g_FrameIndex]];
|
||||
info.renderArea.extent.width = fb_width;
|
||||
info.renderArea.extent.height = fb_height;
|
||||
info.clearValueCount = 1;
|
||||
@ -418,28 +545,17 @@ static void frame_end()
|
||||
{
|
||||
VkResult err;
|
||||
vkCmdEndRenderPass(g_CommandBuffer[g_FrameIndex]);
|
||||
{
|
||||
VkImageMemoryBarrier barrier = {};
|
||||
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
||||
barrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
||||
barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
||||
barrier.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||
barrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
||||
barrier.image = g_BackBuffer[g_BackBufferIndex];
|
||||
barrier.subresourceRange = g_ImageRange;
|
||||
vkCmdPipelineBarrier(g_CommandBuffer[g_FrameIndex], VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, NULL, 0, NULL, 1, &barrier);
|
||||
}
|
||||
{
|
||||
VkPipelineStageFlags wait_stage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||
VkSubmitInfo info = {};
|
||||
info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||
info.waitSemaphoreCount = 1;
|
||||
info.pWaitSemaphores = &g_Semaphore[g_FrameIndex];
|
||||
info.pWaitSemaphores = &g_PresentCompleteSemaphore[g_FrameIndex];
|
||||
info.pWaitDstStageMask = &wait_stage;
|
||||
info.commandBufferCount = 1;
|
||||
info.pCommandBuffers = &g_CommandBuffer[g_FrameIndex];
|
||||
info.signalSemaphoreCount = 1;
|
||||
info.pSignalSemaphores = &g_RenderCompleteSemaphore[g_FrameIndex];
|
||||
|
||||
err = vkEndCommandBuffer(g_CommandBuffer[g_FrameIndex]);
|
||||
check_vk_result(err);
|
||||
@ -448,21 +564,31 @@ static void frame_end()
|
||||
err = vkQueueSubmit(g_Queue, 1, &info, g_Fence[g_FrameIndex]);
|
||||
check_vk_result(err);
|
||||
}
|
||||
{
|
||||
VkResult res;
|
||||
VkSwapchainKHR swapchains[1] = {g_Swapchain};
|
||||
uint32_t indices[1] = {g_BackBufferIndex};
|
||||
VkPresentInfoKHR info = {};
|
||||
info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
||||
info.swapchainCount = 1;
|
||||
info.pSwapchains = swapchains;
|
||||
info.pImageIndices = indices;
|
||||
info.pResults = &res;
|
||||
err = vkQueuePresentKHR(g_Queue, &info);
|
||||
check_vk_result(err);
|
||||
check_vk_result(res);
|
||||
}
|
||||
g_FrameIndex = (g_FrameIndex) % IMGUI_VK_QUEUED_FRAMES;
|
||||
}
|
||||
|
||||
static void frame_present()
|
||||
{
|
||||
VkResult err;
|
||||
// If IMGUI_UNLIMITED_FRAME_RATE is defined we present the latest but one frame. Otherwise we present the latest rendered frame
|
||||
#ifdef IMGUI_UNLIMITED_FRAME_RATE
|
||||
uint32_t PresentIndex = (g_FrameIndex + IMGUI_VK_QUEUED_FRAMES - 1) % IMGUI_VK_QUEUED_FRAMES;
|
||||
#else
|
||||
uint32_t PresentIndex = g_FrameIndex;
|
||||
#endif // IMGUI_UNLIMITED_FRAME_RATE
|
||||
|
||||
VkSwapchainKHR swapchains[1] = {g_Swapchain};
|
||||
uint32_t indices[1] = {g_BackbufferIndices[PresentIndex]};
|
||||
VkPresentInfoKHR info = {};
|
||||
info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
||||
info.waitSemaphoreCount = 1;
|
||||
info.pWaitSemaphores = &g_RenderCompleteSemaphore[PresentIndex];
|
||||
info.swapchainCount = 1;
|
||||
info.pSwapchains = swapchains;
|
||||
info.pImageIndices = indices;
|
||||
err = vkQueuePresentKHR(g_Queue, &info);
|
||||
check_vk_result(err);
|
||||
|
||||
g_FrameIndex = (g_FrameIndex + 1) % IMGUI_VK_QUEUED_FRAMES;
|
||||
}
|
||||
|
||||
static void error_callback(int error, const char* description)
|
||||
@ -540,6 +666,17 @@ int main(int, char**)
|
||||
bool show_another_window = false;
|
||||
ImVec4 clear_color = ImColor(114, 144, 154);
|
||||
|
||||
// When IMGUI_UNLIMITED_FRAME_RATE is defined we render into latest image acquired from the swapchain but we display the image which was rendered before.
|
||||
// Hence we must render once and increase the g_FrameIndex without presenting, which we do before entering the render loop.
|
||||
// This is also the reason why frame_end() is split into frame_end() and frame_present(), the later one not being called here.
|
||||
#ifdef IMGUI_UNLIMITED_FRAME_RATE
|
||||
ImGui_ImplGlfwVulkan_NewFrame();
|
||||
frame_begin();
|
||||
ImGui_ImplGlfwVulkan_Render(g_CommandBuffer[g_FrameIndex]);
|
||||
frame_end();
|
||||
g_FrameIndex = (g_FrameIndex + 1) % IMGUI_VK_QUEUED_FRAMES;
|
||||
#endif // IMGUI_UNLIMITED_FRAME_RATE
|
||||
|
||||
// Main loop
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
@ -582,6 +719,7 @@ int main(int, char**)
|
||||
frame_begin();
|
||||
ImGui_ImplGlfwVulkan_Render(g_CommandBuffer[g_FrameIndex]);
|
||||
frame_end();
|
||||
frame_present();
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
|
Reference in New Issue
Block a user