Examples: Vulkan: Coding style tweaks.

This commit is contained in:
ocornut 2016-04-03 17:32:53 +02:00
parent 9c513d4443
commit 0e7b9b8284
5 changed files with 188 additions and 234 deletions

View File

@ -80,3 +80,7 @@ allegro5_example/
marmalade_example/ marmalade_example/
Marmalade example using IwGx Marmalade example using IwGx
vulkan_example/
Vulkan example.
This is quite long and tedious, because: Vulkan.

View File

@ -0,0 +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

View File

@ -59,7 +59,8 @@ static VkBuffer g_IndexBuffer[IMGUI_VK_QUEUED_FRAMES] = {};
static VkDeviceMemory g_UploadBufferMemory = VK_NULL_HANDLE; static VkDeviceMemory g_UploadBufferMemory = VK_NULL_HANDLE;
static VkBuffer g_UploadBuffer = VK_NULL_HANDLE; static VkBuffer g_UploadBuffer = VK_NULL_HANDLE;
static unsigned char __glsl_shader_vert_spv[] = { static unsigned char __glsl_shader_vert_spv[] =
{
0x03, 0x02, 0x23, 0x07, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x08, 0x00, 0x03, 0x02, 0x23, 0x07, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x08, 0x00,
0x6c, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, 0x6c, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00,
0x01, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, 0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, 0x20, 0x00, 0x00, 0x00,
@ -161,7 +162,8 @@ static unsigned char __glsl_shader_vert_spv[] = {
}; };
static unsigned int __glsl_shader_vert_spv_len = 1172; static unsigned int __glsl_shader_vert_spv_len = 1172;
static unsigned char __glsl_shader_frag_spv[] = { static unsigned char __glsl_shader_frag_spv[] =
{
0x03, 0x02, 0x23, 0x07, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x08, 0x00, 0x03, 0x02, 0x23, 0x07, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x08, 0x00,
0x6c, 0x5d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, 0x6c, 0x5d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00,
0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00,
@ -224,15 +226,15 @@ static uint32_t ImGui_ImplGlfwVulkan_MemoryType(VkMemoryPropertyFlags properties
{ {
VkPhysicalDeviceMemoryProperties prop; VkPhysicalDeviceMemoryProperties prop;
vkGetPhysicalDeviceMemoryProperties(g_Gpu, &prop); vkGetPhysicalDeviceMemoryProperties(g_Gpu, &prop);
for(uint32_t i=0; i < prop.memoryTypeCount; ++i) for (uint32_t i = 0; i < prop.memoryTypeCount; i++)
if((prop.memoryTypes[i].propertyFlags & properties) == properties && if ((prop.memoryTypes[i].propertyFlags & properties) == properties && type_bits & (1<<i))
type_bits & (1<<i))
return i; return i;
return 0xffffffff; // Unable to find memoryType return 0xffffffff; // Unable to find memoryType
} }
static void ImGui_ImplGlfwVulkan_VkResult(VkResult err) static void ImGui_ImplGlfwVulkan_VkResult(VkResult err)
{ {
if(g_CheckVkResult) if (g_CheckVkResult)
g_CheckVkResult(err); g_CheckVkResult(err);
} }
@ -244,22 +246,19 @@ void ImGui_ImplGlfwVulkan_RenderDrawLists(ImDrawData* draw_data)
// Create the Vertex Buffer: // Create the Vertex Buffer:
size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert); size_t vertex_size = draw_data->TotalVtxCount * sizeof(ImDrawVert);
if(!g_VertexBuffer[g_FrameIndex] || if (!g_VertexBuffer[g_FrameIndex] || g_VertexBufferSize[g_FrameIndex] < vertex_size)
g_VertexBufferSize[g_FrameIndex] < vertex_size){ {
if(g_VertexBuffer[g_FrameIndex]) if (g_VertexBuffer[g_FrameIndex])
vkDestroyBuffer(g_Device, g_VertexBuffer[g_FrameIndex], g_Allocator); vkDestroyBuffer(g_Device, g_VertexBuffer[g_FrameIndex], g_Allocator);
if(g_VertexBufferMemory[g_FrameIndex]) if (g_VertexBufferMemory[g_FrameIndex])
vkFreeMemory(g_Device, g_VertexBufferMemory[g_FrameIndex], g_Allocator); vkFreeMemory(g_Device, g_VertexBufferMemory[g_FrameIndex], g_Allocator);
size_t vertex_buffer_size = ((vertex_size-1)/g_BufferMemoryAlignment+1)*g_BufferMemoryAlignment; size_t vertex_buffer_size = ((vertex_size-1) / g_BufferMemoryAlignment+1) * g_BufferMemoryAlignment;
VkBufferCreateInfo buffer_info = {}; VkBufferCreateInfo buffer_info = {};
buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
buffer_info.size = vertex_buffer_size; buffer_info.size = vertex_buffer_size;
buffer_info.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; buffer_info.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
err = vkCreateBuffer(g_Device, err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_VertexBuffer[g_FrameIndex]);
&buffer_info,
g_Allocator,
&g_VertexBuffer[g_FrameIndex]);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
VkMemoryRequirements req; VkMemoryRequirements req;
vkGetBufferMemoryRequirements(g_Device, g_VertexBuffer[g_FrameIndex], &req); vkGetBufferMemoryRequirements(g_Device, g_VertexBuffer[g_FrameIndex], &req);
@ -267,38 +266,29 @@ void ImGui_ImplGlfwVulkan_RenderDrawLists(ImDrawData* draw_data)
VkMemoryAllocateInfo alloc_info = {}; VkMemoryAllocateInfo alloc_info = {};
alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
alloc_info.allocationSize = req.size; alloc_info.allocationSize = req.size;
alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType( alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_VertexBufferMemory[g_FrameIndex]);
req.memoryTypeBits);
err = vkAllocateMemory(g_Device,
&alloc_info,
g_Allocator,
&g_VertexBufferMemory[g_FrameIndex]);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
err = vkBindBufferMemory(g_Device, err = vkBindBufferMemory(g_Device, g_VertexBuffer[g_FrameIndex], g_VertexBufferMemory[g_FrameIndex], 0);
g_VertexBuffer[g_FrameIndex],
g_VertexBufferMemory[g_FrameIndex], 0);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
g_VertexBufferSize[g_FrameIndex] = vertex_buffer_size; g_VertexBufferSize[g_FrameIndex] = vertex_buffer_size;
} }
// Create the Index Buffer: // Create the Index Buffer:
size_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx); size_t index_size = draw_data->TotalIdxCount * sizeof(ImDrawIdx);
if(!g_IndexBuffer[g_FrameIndex] || if (!g_IndexBuffer[g_FrameIndex] || g_IndexBufferSize[g_FrameIndex] < index_size)
g_IndexBufferSize[g_FrameIndex] < index_size){ {
if(g_IndexBuffer[g_FrameIndex]) if (g_IndexBuffer[g_FrameIndex])
vkDestroyBuffer(g_Device, g_IndexBuffer[g_FrameIndex], g_Allocator); vkDestroyBuffer(g_Device, g_IndexBuffer[g_FrameIndex], g_Allocator);
if(g_IndexBufferMemory[g_FrameIndex]) if (g_IndexBufferMemory[g_FrameIndex])
vkFreeMemory(g_Device, g_IndexBufferMemory[g_FrameIndex], g_Allocator); vkFreeMemory(g_Device, g_IndexBufferMemory[g_FrameIndex], g_Allocator);
size_t index_buffer_size = ((index_size-1)/g_BufferMemoryAlignment+1)*g_BufferMemoryAlignment; size_t index_buffer_size = ((index_size-1) / g_BufferMemoryAlignment+1) * g_BufferMemoryAlignment;
VkBufferCreateInfo buffer_info = {}; VkBufferCreateInfo buffer_info = {};
buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
buffer_info.size = index_buffer_size; buffer_info.size = index_buffer_size;
buffer_info.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT; buffer_info.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
err = vkCreateBuffer(g_Device, err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_IndexBuffer[g_FrameIndex]);
&buffer_info,
g_Allocator,
&g_IndexBuffer[g_FrameIndex]);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
VkMemoryRequirements req; VkMemoryRequirements req;
vkGetBufferMemoryRequirements(g_Device, g_IndexBuffer[g_FrameIndex], &req); vkGetBufferMemoryRequirements(g_Device, g_IndexBuffer[g_FrameIndex], &req);
@ -306,38 +296,27 @@ void ImGui_ImplGlfwVulkan_RenderDrawLists(ImDrawData* draw_data)
VkMemoryAllocateInfo alloc_info = {}; VkMemoryAllocateInfo alloc_info = {};
alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
alloc_info.allocationSize = req.size; alloc_info.allocationSize = req.size;
alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType( alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_IndexBufferMemory[g_FrameIndex]);
req.memoryTypeBits);
err = vkAllocateMemory(g_Device,
&alloc_info,
g_Allocator,
&g_IndexBufferMemory[g_FrameIndex]);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
err = vkBindBufferMemory(g_Device, err = vkBindBufferMemory(g_Device, g_IndexBuffer[g_FrameIndex], g_IndexBufferMemory[g_FrameIndex], 0);
g_IndexBuffer[g_FrameIndex],
g_IndexBufferMemory[g_FrameIndex], 0);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
g_IndexBufferSize[g_FrameIndex] = index_buffer_size; g_IndexBufferSize[g_FrameIndex] = index_buffer_size;
} }
// Upload Vertex and index Data: // Upload Vertex and index Data:
{ {
ImDrawVert* vtx_dst; ImDrawVert* vtx_dst;
ImDrawIdx* idx_dst; ImDrawIdx* idx_dst;
err = vkMapMemory(g_Device, g_VertexBufferMemory[g_FrameIndex], err = vkMapMemory(g_Device, g_VertexBufferMemory[g_FrameIndex], 0, vertex_size, 0, (void**)(&vtx_dst));
0, vertex_size, 0,
reinterpret_cast<void**>(&vtx_dst));
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
err = vkMapMemory(g_Device, g_IndexBufferMemory[g_FrameIndex], err = vkMapMemory(g_Device, g_IndexBufferMemory[g_FrameIndex], 0, index_size, 0, (void**)(&idx_dst));
0, index_size, 0,
reinterpret_cast<void**>(&idx_dst));
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
for(int n = 0; n < draw_data->CmdListsCount; n++){ for (int n = 0; n < draw_data->CmdListsCount; n++)
{
const ImDrawList* cmd_list = draw_data->CmdLists[n]; const ImDrawList* cmd_list = draw_data->CmdLists[n];
memcpy(vtx_dst, &cmd_list->VtxBuffer[0], memcpy(vtx_dst, &cmd_list->VtxBuffer[0], cmd_list->VtxBuffer.size() * sizeof(ImDrawVert));
cmd_list->VtxBuffer.size() * sizeof(ImDrawVert)); memcpy(idx_dst, &cmd_list->IdxBuffer[0], cmd_list->IdxBuffer.size() * sizeof(ImDrawIdx));
memcpy(idx_dst, &cmd_list->IdxBuffer[0],
cmd_list->IdxBuffer.size() * sizeof(ImDrawIdx));
vtx_dst += cmd_list->VtxBuffer.size(); vtx_dst += cmd_list->VtxBuffer.size();
idx_dst += cmd_list->IdxBuffer.size(); idx_dst += cmd_list->IdxBuffer.size();
} }
@ -353,29 +332,22 @@ void ImGui_ImplGlfwVulkan_RenderDrawLists(ImDrawData* draw_data)
vkUnmapMemory(g_Device, g_VertexBufferMemory[g_FrameIndex]); vkUnmapMemory(g_Device, g_VertexBufferMemory[g_FrameIndex]);
vkUnmapMemory(g_Device, g_IndexBufferMemory[g_FrameIndex]); vkUnmapMemory(g_Device, g_IndexBufferMemory[g_FrameIndex]);
} }
// Bind pipeline and descriptor sets: // Bind pipeline and descriptor sets:
{ {
vkCmdBindPipeline(g_CommandBuffer, vkCmdBindPipeline(g_CommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_Pipeline);
VK_PIPELINE_BIND_POINT_GRAPHICS,
g_Pipeline);
VkDescriptorSet desc_set[1] = {g_DescriptorSet}; VkDescriptorSet desc_set[1] = {g_DescriptorSet};
vkCmdBindDescriptorSets(g_CommandBuffer, vkCmdBindDescriptorSets(g_CommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_PipelineLayout, 0, 1, desc_set, 0, NULL);
VK_PIPELINE_BIND_POINT_GRAPHICS,
g_PipelineLayout,
0, 1, desc_set,
0, NULL);
} }
// Bind Vertex And Index Buffer: // Bind Vertex And Index Buffer:
{ {
VkBuffer vertex_buffers[1] = {g_VertexBuffer[g_FrameIndex]}; VkBuffer vertex_buffers[1] = {g_VertexBuffer[g_FrameIndex]};
VkDeviceSize vertex_offset[1] = {0}; VkDeviceSize vertex_offset[1] = {0};
vkCmdBindVertexBuffers(g_CommandBuffer, vkCmdBindVertexBuffers(g_CommandBuffer, 0, 1, vertex_buffers, vertex_offset);
0, 1, vkCmdBindIndexBuffer(g_CommandBuffer, g_IndexBuffer[g_FrameIndex], 0, VK_INDEX_TYPE_UINT16);
vertex_buffers, vertex_offset);
vkCmdBindIndexBuffer(g_CommandBuffer,
g_IndexBuffer[g_FrameIndex],
0, VK_INDEX_TYPE_UINT16);
} }
// Setup viewport: // Setup viewport:
{ {
VkViewport viewport; VkViewport viewport;
@ -387,6 +359,7 @@ void ImGui_ImplGlfwVulkan_RenderDrawLists(ImDrawData* draw_data)
viewport.maxDepth = 1.0f; viewport.maxDepth = 1.0f;
vkCmdSetViewport(g_CommandBuffer, 0, 1, &viewport); vkCmdSetViewport(g_CommandBuffer, 0, 1, &viewport);
} }
// Setup scale and translation: // Setup scale and translation:
{ {
float scale[2]; float scale[2];
@ -395,39 +368,32 @@ void ImGui_ImplGlfwVulkan_RenderDrawLists(ImDrawData* draw_data)
float translate[2]; float translate[2];
translate[0] = -1.0f; translate[0] = -1.0f;
translate[1] = -1.0f; translate[1] = -1.0f;
vkCmdPushConstants(g_CommandBuffer, vkCmdPushConstants(g_CommandBuffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 0, sizeof(float) * 2, scale);
g_PipelineLayout, vkCmdPushConstants(g_CommandBuffer, g_PipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, sizeof(float) * 2, sizeof(float) * 2, translate);
VK_SHADER_STAGE_VERTEX_BIT,
sizeof(float) * 0,
sizeof(float) * 2,
scale);
vkCmdPushConstants(g_CommandBuffer,
g_PipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT,
sizeof(float) * 2,
sizeof(float) * 2,
translate);
} }
// Render the command lists: // Render the command lists:
int vtx_offset = 0; int vtx_offset = 0;
int idx_offset = 0; int idx_offset = 0;
for(int n = 0; n < draw_data->CmdListsCount; n++){ for (int n = 0; n < draw_data->CmdListsCount; n++)
{
const ImDrawList* cmd_list = draw_data->CmdLists[n]; const ImDrawList* cmd_list = draw_data->CmdLists[n];
for(int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++){ for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++)
{
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
if(pcmd->UserCallback){ if (pcmd->UserCallback)
{
pcmd->UserCallback(cmd_list, pcmd); pcmd->UserCallback(cmd_list, pcmd);
} }
else{ else
{
VkRect2D scissor; VkRect2D scissor;
scissor.offset.x = static_cast<int32_t>(pcmd->ClipRect.x); scissor.offset.x = static_cast<int32_t>(pcmd->ClipRect.x);
scissor.offset.y = static_cast<int32_t>(pcmd->ClipRect.y); scissor.offset.y = static_cast<int32_t>(pcmd->ClipRect.y);
scissor.extent.width = static_cast<uint32_t>(pcmd->ClipRect.z - pcmd->ClipRect.x); scissor.extent.width = static_cast<uint32_t>(pcmd->ClipRect.z - pcmd->ClipRect.x);
scissor.extent.height = static_cast<uint32_t>(pcmd->ClipRect.w - pcmd->ClipRect.y + 1); // TODO: + 1?????? scissor.extent.height = static_cast<uint32_t>(pcmd->ClipRect.w - pcmd->ClipRect.y + 1); // TODO: + 1??????
vkCmdSetScissor(g_CommandBuffer, 0, 1, &scissor); vkCmdSetScissor(g_CommandBuffer, 0, 1, &scissor);
vkCmdDrawIndexed(g_CommandBuffer, vkCmdDrawIndexed(g_CommandBuffer, pcmd->ElemCount, 1, idx_offset, vtx_offset, 0);
pcmd->ElemCount, 1,
idx_offset, vtx_offset, 0);
} }
idx_offset += pcmd->ElemCount; idx_offset += pcmd->ElemCount;
} }
@ -468,6 +434,7 @@ void ImGui_ImplGlfwVulkan_KeyCallback(GLFWwindow*, int key, int, int action, int
io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL]; io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL];
io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT]; io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT];
io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT]; io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT];
io.KeySuper = io.KeysDown[GLFW_KEY_LEFT_SUPER] || io.KeysDown[GLFW_KEY_RIGHT_SUPER];
} }
void ImGui_ImplGlfwVulkan_CharCallback(GLFWwindow*, unsigned int c) void ImGui_ImplGlfwVulkan_CharCallback(GLFWwindow*, unsigned int c)
@ -487,6 +454,7 @@ bool ImGui_ImplGlfwVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
size_t upload_size = width*height*4*sizeof(char); size_t upload_size = width*height*4*sizeof(char);
VkResult err; VkResult err;
// Create the Image: // Create the Image:
{ {
VkImageCreateInfo info = {}; VkImageCreateInfo info = {};
@ -500,32 +468,23 @@ bool ImGui_ImplGlfwVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
info.arrayLayers = 1; info.arrayLayers = 1;
info.samples = VK_SAMPLE_COUNT_1_BIT; info.samples = VK_SAMPLE_COUNT_1_BIT;
info.tiling = VK_IMAGE_TILING_OPTIMAL; info.tiling = VK_IMAGE_TILING_OPTIMAL;
info.usage = info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
VK_IMAGE_USAGE_SAMPLED_BIT |
VK_IMAGE_USAGE_TRANSFER_DST_BIT;
info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
err = vkCreateImage(g_Device, err = vkCreateImage(g_Device, &info, g_Allocator, &g_FontImage);
&info,
g_Allocator,
&g_FontImage);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
VkMemoryRequirements req; VkMemoryRequirements req;
vkGetImageMemoryRequirements(g_Device, g_FontImage, &req); vkGetImageMemoryRequirements(g_Device, g_FontImage, &req);
VkMemoryAllocateInfo alloc_info = {}; VkMemoryAllocateInfo alloc_info = {};
alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
alloc_info.allocationSize = req.size; alloc_info.allocationSize = req.size;
alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType( alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, req.memoryTypeBits);
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_FontMemory);
req.memoryTypeBits);
err = vkAllocateMemory(g_Device,
&alloc_info,
g_Allocator,
&g_FontMemory);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
err = vkBindImageMemory(g_Device, g_FontImage, g_FontMemory, 0); err = vkBindImageMemory(g_Device, g_FontImage, g_FontMemory, 0);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
} }
// Create the Image View: // Create the Image View:
{ {
VkResult err; VkResult err;
@ -537,12 +496,10 @@ bool ImGui_ImplGlfwVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
info.subresourceRange.levelCount = 1; info.subresourceRange.levelCount = 1;
info.subresourceRange.layerCount = 1; info.subresourceRange.layerCount = 1;
err = vkCreateImageView(g_Device, err = vkCreateImageView(g_Device, &info, g_Allocator, &g_FontView);
&info,
g_Allocator,
&g_FontView);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
} }
// Update the Descriptor Set: // Update the Descriptor Set:
{ {
VkDescriptorImageInfo desc_image[1] = {}; VkDescriptorImageInfo desc_image[1] = {};
@ -555,10 +512,9 @@ bool ImGui_ImplGlfwVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
write_desc[0].descriptorCount = 1; write_desc[0].descriptorCount = 1;
write_desc[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; write_desc[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
write_desc[0].pImageInfo = desc_image; write_desc[0].pImageInfo = desc_image;
vkUpdateDescriptorSets(g_Device, vkUpdateDescriptorSets(g_Device, 1, write_desc, 0, NULL);
1, write_desc,
0, NULL);
} }
// Create the Upload Buffer: // Create the Upload Buffer:
{ {
VkBufferCreateInfo buffer_info = {}; VkBufferCreateInfo buffer_info = {};
@ -566,10 +522,7 @@ bool ImGui_ImplGlfwVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
buffer_info.size = upload_size; buffer_info.size = upload_size;
buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
err = vkCreateBuffer(g_Device, err = vkCreateBuffer(g_Device, &buffer_info, g_Allocator, &g_UploadBuffer);
&buffer_info,
g_Allocator,
&g_UploadBuffer);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
VkMemoryRequirements req; VkMemoryRequirements req;
vkGetBufferMemoryRequirements(g_Device, g_UploadBuffer, &req); vkGetBufferMemoryRequirements(g_Device, g_UploadBuffer, &req);
@ -577,22 +530,17 @@ bool ImGui_ImplGlfwVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
VkMemoryAllocateInfo alloc_info = {}; VkMemoryAllocateInfo alloc_info = {};
alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
alloc_info.allocationSize = req.size; alloc_info.allocationSize = req.size;
alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType( alloc_info.memoryTypeIndex = ImGui_ImplGlfwVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, err = vkAllocateMemory(g_Device, &alloc_info, g_Allocator, &g_UploadBufferMemory);
req.memoryTypeBits);
err = vkAllocateMemory(g_Device,
&alloc_info,
g_Allocator,
&g_UploadBufferMemory);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
err = vkBindBufferMemory(g_Device, g_UploadBuffer, g_UploadBufferMemory, 0); err = vkBindBufferMemory(g_Device, g_UploadBuffer, g_UploadBufferMemory, 0);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
} }
// Upload to Buffer: // Upload to Buffer:
{ {
char *map; char* map = NULL;
err = vkMapMemory(g_Device, g_UploadBufferMemory, 0, upload_size, 0, err = vkMapMemory(g_Device, g_UploadBufferMemory, 0, upload_size, 0, (void**)(&map));
reinterpret_cast<void**>(&map));
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
memcpy(map, pixels, upload_size); memcpy(map, pixels, upload_size);
VkMappedMemoryRange range[1] = {}; VkMappedMemoryRange range[1] = {};
@ -616,21 +564,15 @@ bool ImGui_ImplGlfwVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
copy_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; copy_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
copy_barrier[0].subresourceRange.levelCount = 1; copy_barrier[0].subresourceRange.levelCount = 1;
copy_barrier[0].subresourceRange.layerCount = 1; copy_barrier[0].subresourceRange.layerCount = 1;
vkCmdPipelineBarrier(command_buffer, vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, copy_barrier);
VK_PIPELINE_STAGE_HOST_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
0,
0, NULL, 0, NULL, 1, copy_barrier);
VkBufferImageCopy region = {}; VkBufferImageCopy region = {};
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
region.imageSubresource.layerCount = 1; region.imageSubresource.layerCount = 1;
region.imageExtent.width = width; region.imageExtent.width = width;
region.imageExtent.height = height; region.imageExtent.height = height;
vkCmdCopyBufferToImage(command_buffer, vkCmdCopyBufferToImage(command_buffer, g_UploadBuffer, g_FontImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
g_UploadBuffer,
g_FontImage,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1, &region);
VkImageMemoryBarrier use_barrier[1] = {}; VkImageMemoryBarrier use_barrier[1] = {};
use_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; use_barrier[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
use_barrier[0].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; use_barrier[0].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
@ -643,16 +585,11 @@ bool ImGui_ImplGlfwVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
use_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; use_barrier[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
use_barrier[0].subresourceRange.levelCount = 1; use_barrier[0].subresourceRange.levelCount = 1;
use_barrier[0].subresourceRange.layerCount = 1; use_barrier[0].subresourceRange.layerCount = 1;
vkCmdPipelineBarrier(command_buffer, vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, use_barrier);
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
0,
0, NULL, 0, NULL, 1, use_barrier);
} }
io.Fonts->TexID = (void *)(intptr_t)g_FontImage;
io.Fonts->ClearInputData(); // Store our identifier
io.Fonts->ClearTexData(); io.Fonts->TexID = (void *)(intptr_t)g_FontImage;
return true; return true;
} }
@ -660,7 +597,6 @@ bool ImGui_ImplGlfwVulkan_CreateFontsTexture(VkCommandBuffer command_buffer)
bool ImGui_ImplGlfwVulkan_CreateDeviceObjects() bool ImGui_ImplGlfwVulkan_CreateDeviceObjects()
{ {
VkResult err; VkResult err;
VkShaderModule vert_module; VkShaderModule vert_module;
VkShaderModule frag_module; VkShaderModule frag_module;
@ -679,7 +615,9 @@ bool ImGui_ImplGlfwVulkan_CreateDeviceObjects()
err = vkCreateShaderModule(g_Device, &frag_info, g_Allocator, &frag_module); err = vkCreateShaderModule(g_Device, &frag_info, g_Allocator, &frag_module);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
} }
if(!g_FontSampler){
if (!g_FontSampler)
{
VkSamplerCreateInfo info = {}; VkSamplerCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
info.magFilter = VK_FILTER_LINEAR; info.magFilter = VK_FILTER_LINEAR;
@ -690,13 +628,12 @@ bool ImGui_ImplGlfwVulkan_CreateDeviceObjects()
info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT; info.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
info.minLod = -1000; info.minLod = -1000;
info.maxLod = 1000; info.maxLod = 1000;
err = vkCreateSampler(g_Device, err = vkCreateSampler(g_Device, &info, g_Allocator, &g_FontSampler);
&info,
g_Allocator,
&g_FontSampler);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
} }
if(!g_DescriptorSetLayout){
if (!g_DescriptorSetLayout)
{
VkSampler sampler[1] = {g_FontSampler}; VkSampler sampler[1] = {g_FontSampler};
VkDescriptorSetLayoutBinding binding[1] = {}; VkDescriptorSetLayoutBinding binding[1] = {};
binding[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; binding[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
@ -707,12 +644,10 @@ bool ImGui_ImplGlfwVulkan_CreateDeviceObjects()
info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
info.bindingCount = 1; info.bindingCount = 1;
info.pBindings = binding; info.pBindings = binding;
err = vkCreateDescriptorSetLayout(g_Device, err = vkCreateDescriptorSetLayout(g_Device, &info, g_Allocator, &g_DescriptorSetLayout);
&info,
g_Allocator,
&g_DescriptorSetLayout);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
} }
// Create Descriptor Set: // Create Descriptor Set:
{ {
VkDescriptorSetAllocateInfo alloc_info = {}; VkDescriptorSetAllocateInfo alloc_info = {};
@ -723,7 +658,9 @@ bool ImGui_ImplGlfwVulkan_CreateDeviceObjects()
err = vkAllocateDescriptorSets(g_Device, &alloc_info, &g_DescriptorSet); err = vkAllocateDescriptorSets(g_Device, &alloc_info, &g_DescriptorSet);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
} }
if(!g_PipelineLayout){
if (!g_PipelineLayout)
{
VkPushConstantRange push_constants[2] = {}; VkPushConstantRange push_constants[2] = {};
push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; push_constants[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
push_constants[0].offset = sizeof(float) * 0; push_constants[0].offset = sizeof(float) * 0;
@ -738,8 +675,7 @@ bool ImGui_ImplGlfwVulkan_CreateDeviceObjects()
layout_info.pSetLayouts = set_layout; layout_info.pSetLayouts = set_layout;
layout_info.pushConstantRangeCount = 2; layout_info.pushConstantRangeCount = 2;
layout_info.pPushConstantRanges = push_constants; layout_info.pPushConstantRanges = push_constants;
err = vkCreatePipelineLayout(g_Device, &layout_info, err = vkCreatePipelineLayout(g_Device, &layout_info, g_Allocator, &g_PipelineLayout);
g_Allocator, &g_PipelineLayout);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
} }
@ -805,17 +741,14 @@ bool ImGui_ImplGlfwVulkan_CreateDeviceObjects()
color_attachment[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; color_attachment[0].srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
color_attachment[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; color_attachment[0].dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
color_attachment[0].alphaBlendOp = VK_BLEND_OP_ADD; color_attachment[0].alphaBlendOp = VK_BLEND_OP_ADD;
color_attachment[0].colorWriteMask = color_attachment[0].colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
VkPipelineColorBlendStateCreateInfo blend_info = {}; VkPipelineColorBlendStateCreateInfo blend_info = {};
blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; blend_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
blend_info.attachmentCount = 1; blend_info.attachmentCount = 1;
blend_info.pAttachments = color_attachment; blend_info.pAttachments = color_attachment;
VkDynamicState dynamic_states[2] = {VK_DYNAMIC_STATE_VIEWPORT, VkDynamicState dynamic_states[2] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
VK_DYNAMIC_STATE_SCISSOR};
VkPipelineDynamicStateCreateInfo dynamic_state = {}; VkPipelineDynamicStateCreateInfo dynamic_state = {};
dynamic_state.dynamicStateCount = 2; dynamic_state.dynamicStateCount = 2;
dynamic_state.pDynamicStates = dynamic_states; dynamic_state.pDynamicStates = dynamic_states;
@ -834,11 +767,7 @@ bool ImGui_ImplGlfwVulkan_CreateDeviceObjects()
info.pDynamicState = &dynamic_state; info.pDynamicState = &dynamic_state;
info.layout = g_PipelineLayout; info.layout = g_PipelineLayout;
info.renderPass = g_RenderPass; info.renderPass = g_RenderPass;
err = vkCreateGraphicsPipelines(g_Device, err = vkCreateGraphicsPipelines(g_Device, g_PipelineCache, 1, &info, g_Allocator, &g_Pipeline);
g_PipelineCache,
1, &info,
g_Allocator,
&g_Pipeline);
ImGui_ImplGlfwVulkan_VkResult(err); ImGui_ImplGlfwVulkan_VkResult(err);
vkDestroyShaderModule(g_Device, vert_module, g_Allocator); vkDestroyShaderModule(g_Device, vert_module, g_Allocator);
@ -849,43 +778,47 @@ bool ImGui_ImplGlfwVulkan_CreateDeviceObjects()
void ImGui_ImplGlfwVulkan_InvalidateFontUploadObjects() void ImGui_ImplGlfwVulkan_InvalidateFontUploadObjects()
{ {
if(g_UploadBuffer){ if (g_UploadBuffer)
{
vkDestroyBuffer(g_Device, g_UploadBuffer, g_Allocator); vkDestroyBuffer(g_Device, g_UploadBuffer, g_Allocator);
g_UploadBuffer = VK_NULL_HANDLE; g_UploadBuffer = VK_NULL_HANDLE;
} }
if(g_UploadBufferMemory){ if (g_UploadBufferMemory)
{
vkFreeMemory(g_Device, g_UploadBufferMemory, g_Allocator); vkFreeMemory(g_Device, g_UploadBufferMemory, g_Allocator);
g_UploadBufferMemory = VK_NULL_HANDLE; g_UploadBufferMemory = VK_NULL_HANDLE;
} }
} }
void ImGui_ImplGlfwVulkan_InvalidateDeviceObjects() void ImGui_ImplGlfwVulkan_InvalidateDeviceObjects()
{ {
ImGui_ImplGlfwVulkan_InvalidateFontUploadObjects(); ImGui_ImplGlfwVulkan_InvalidateFontUploadObjects();
for(int i=0; i<IMGUI_VK_QUEUED_FRAMES; i++){ for (int i=0; i<IMGUI_VK_QUEUED_FRAMES; i++)
if(g_VertexBuffer[i]) {
if (g_VertexBuffer[i])
vkDestroyBuffer(g_Device, g_VertexBuffer[i], g_Allocator); vkDestroyBuffer(g_Device, g_VertexBuffer[i], g_Allocator);
if(g_VertexBufferMemory[i]) if (g_VertexBufferMemory[i])
vkFreeMemory(g_Device, g_VertexBufferMemory[i], g_Allocator); vkFreeMemory(g_Device, g_VertexBufferMemory[i], g_Allocator);
if(g_IndexBuffer[i]) if (g_IndexBuffer[i])
vkDestroyBuffer(g_Device, g_IndexBuffer[i], g_Allocator); vkDestroyBuffer(g_Device, g_IndexBuffer[i], g_Allocator);
if(g_IndexBufferMemory[i]) if (g_IndexBufferMemory[i])
vkFreeMemory(g_Device, g_IndexBufferMemory[i], g_Allocator); vkFreeMemory(g_Device, g_IndexBufferMemory[i], g_Allocator);
} }
if(g_FontView) if (g_FontView)
vkDestroyImageView(g_Device, g_FontView, g_Allocator); vkDestroyImageView(g_Device, g_FontView, g_Allocator);
if(g_FontImage) if (g_FontImage)
vkDestroyImage(g_Device, g_FontImage, g_Allocator); vkDestroyImage(g_Device, g_FontImage, g_Allocator);
if(g_FontMemory) if (g_FontMemory)
vkFreeMemory(g_Device, g_FontMemory, g_Allocator); vkFreeMemory(g_Device, g_FontMemory, g_Allocator);
if(g_FontSampler) if (g_FontSampler)
vkDestroySampler(g_Device, g_FontSampler, g_Allocator); vkDestroySampler(g_Device, g_FontSampler, g_Allocator);
if(g_DescriptorSetLayout) if (g_DescriptorSetLayout)
vkDestroyDescriptorSetLayout(g_Device, g_DescriptorSetLayout, g_Allocator); vkDestroyDescriptorSetLayout(g_Device, g_DescriptorSetLayout, g_Allocator);
if(g_PipelineLayout) if (g_PipelineLayout)
vkDestroyPipelineLayout(g_Device, g_PipelineLayout, g_Allocator); vkDestroyPipelineLayout(g_Device, g_PipelineLayout, g_Allocator);
if(g_Pipeline) if (g_Pipeline)
vkDestroyPipeline(g_Device, g_Pipeline, g_Allocator); vkDestroyPipeline(g_Device, g_Pipeline, g_Allocator);
} }
@ -998,5 +931,5 @@ void ImGui_ImplGlfwVulkan_Render(VkCommandBuffer command_buffer)
g_CommandBuffer = command_buffer; g_CommandBuffer = command_buffer;
ImGui::Render(); ImGui::Render();
g_CommandBuffer = VK_NULL_HANDLE; g_CommandBuffer = VK_NULL_HANDLE;
g_FrameIndex = (g_FrameIndex+1)%IMGUI_VK_QUEUED_FRAMES; g_FrameIndex = (g_FrameIndex + 1) % IMGUI_VK_QUEUED_FRAMES;
} }

View File

@ -4,11 +4,12 @@
// If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
// https://github.com/ocornut/imgui // https://github.com/ocornut/imgui
#define IMGUI_VK_QUEUED_FRAMES 2
struct GLFWwindow; struct GLFWwindow;
struct ImGui_ImplGlfwVulkan_Init_Data{ #define IMGUI_VK_QUEUED_FRAMES 2
struct ImGui_ImplGlfwVulkan_Init_Data
{
VkAllocationCallbacks* allocator; VkAllocationCallbacks* allocator;
VkPhysicalDevice gpu; VkPhysicalDevice gpu;
VkDevice device; VkDevice device;
@ -29,7 +30,6 @@ IMGUI_API void ImGui_ImplGlfwVulkan_InvalidateDeviceObjects();
IMGUI_API bool ImGui_ImplGlfwVulkan_CreateFontsTexture(VkCommandBuffer command_buffer); IMGUI_API bool ImGui_ImplGlfwVulkan_CreateFontsTexture(VkCommandBuffer command_buffer);
IMGUI_API bool ImGui_ImplGlfwVulkan_CreateDeviceObjects(); IMGUI_API bool ImGui_ImplGlfwVulkan_CreateDeviceObjects();
// GLFW callbacks (installed by default if you enable 'install_callbacks' during initialization) // GLFW callbacks (installed by default if you enable 'install_callbacks' during initialization)
// Provided here if you want to chain callbacks. // Provided here if you want to chain callbacks.
// You can also handle inputs yourself and use those as a reference. // You can also handle inputs yourself and use those as a reference.
@ -38,4 +38,3 @@ IMGUI_API void ImGui_ImplGlfwVulkan_ScrollCallback(GLFWwindow* window, do
IMGUI_API void ImGui_ImplGlfwVulkan_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); IMGUI_API void ImGui_ImplGlfwVulkan_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
IMGUI_API void ImGui_ImplGlfwVulkan_CharCallback(GLFWwindow* window, unsigned int c); IMGUI_API void ImGui_ImplGlfwVulkan_CharCallback(GLFWwindow* window, unsigned int c);

View File

@ -3,8 +3,8 @@
#include <imgui.h> #include <imgui.h>
#include <stdio.h> #include <stdio.h> // printf, fprintf
#include <stdlib.h> #include <stdlib.h> // abort
#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_NONE
#define GLFW_INCLUDE_VULKAN #define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
@ -47,9 +47,10 @@ static VkClearValue g_ClearValue = {};
static void check_vk_result(VkResult err) static void check_vk_result(VkResult err)
{ {
if(err == 0) return; if (err == 0) return;
printf("VkResult %d\n", err); printf("VkResult %d\n", err);
if(err < 0) abort(); if (err < 0)
abort();
} }
static void resize_vulkan(GLFWwindow* /*window*/, int w, int h) static void resize_vulkan(GLFWwindow* /*window*/, int w, int h)
@ -58,15 +59,17 @@ static void resize_vulkan(GLFWwindow* /*window*/, int w, int h)
VkSwapchainKHR old_swapchain = g_Swapchain; VkSwapchainKHR old_swapchain = g_Swapchain;
err = vkDeviceWaitIdle(g_Device); err = vkDeviceWaitIdle(g_Device);
check_vk_result(err); check_vk_result(err);
// Destroy old Framebuffer: // Destroy old Framebuffer:
for(uint32_t i=0; i<g_BackBufferCount; i++) for (uint32_t i=0; i<g_BackBufferCount; i++)
if(g_BackBufferView[i]) if (g_BackBufferView[i])
vkDestroyImageView(g_Device, g_BackBufferView[i], g_Allocator); vkDestroyImageView(g_Device, g_BackBufferView[i], g_Allocator);
for(uint32_t i=0; i<g_BackBufferCount; i++) for(uint32_t i=0; i<g_BackBufferCount; i++)
if(g_Framebuffer[i]) if (g_Framebuffer[i])
vkDestroyFramebuffer(g_Device, g_Framebuffer[i], g_Allocator); vkDestroyFramebuffer(g_Device, g_Framebuffer[i], g_Allocator);
if(g_RenderPass) if (g_RenderPass)
vkDestroyRenderPass(g_Device, g_RenderPass, g_Allocator); vkDestroyRenderPass(g_Device, g_RenderPass, g_Allocator);
// Create Swapchain: // Create Swapchain:
{ {
VkSwapchainCreateInfoKHR info = {}; VkSwapchainCreateInfoKHR info = {};
@ -86,13 +89,15 @@ static void resize_vulkan(GLFWwindow* /*window*/, int w, int h)
err = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(g_Gpu, g_Surface, &cap); err = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(g_Gpu, g_Surface, &cap);
check_vk_result(err); check_vk_result(err);
info.minImageCount = (cap.minImageCount + 2 < cap.maxImageCount) ? (cap.minImageCount + 2) : cap.maxImageCount; info.minImageCount = (cap.minImageCount + 2 < cap.maxImageCount) ? (cap.minImageCount + 2) : cap.maxImageCount;
if(cap.currentExtent.width == 0xffffffff){ if (cap.currentExtent.width == 0xffffffff)
{
fb_width = w; fb_width = w;
fb_height = h; fb_height = h;
info.imageExtent.width = fb_width; info.imageExtent.width = fb_width;
info.imageExtent.height = fb_height; info.imageExtent.height = fb_height;
} }
else{ else
{
fb_width = cap.currentExtent.width; fb_width = cap.currentExtent.width;
fb_height = cap.currentExtent.height; fb_height = cap.currentExtent.height;
info.imageExtent.width = fb_width; info.imageExtent.width = fb_width;
@ -105,8 +110,9 @@ static void resize_vulkan(GLFWwindow* /*window*/, int w, int h)
err = vkGetSwapchainImagesKHR(g_Device, g_Swapchain, &g_BackBufferCount, g_BackBuffer); err = vkGetSwapchainImagesKHR(g_Device, g_Swapchain, &g_BackBufferCount, g_BackBuffer);
check_vk_result(err); check_vk_result(err);
} }
if(old_swapchain) if (old_swapchain)
vkDestroySwapchainKHR(g_Device, old_swapchain, g_Allocator); vkDestroySwapchainKHR(g_Device, old_swapchain, g_Allocator);
// Create the Render Pass: // Create the Render Pass:
{ {
VkAttachmentDescription attachment = {}; VkAttachmentDescription attachment = {};
@ -134,6 +140,7 @@ static void resize_vulkan(GLFWwindow* /*window*/, int w, int h)
err = vkCreateRenderPass(g_Device, &info, g_Allocator, &g_RenderPass); err = vkCreateRenderPass(g_Device, &info, g_Allocator, &g_RenderPass);
check_vk_result(err); check_vk_result(err);
} }
// Create The Image Views // Create The Image Views
{ {
VkImageViewCreateInfo info = {}; VkImageViewCreateInfo info = {};
@ -145,12 +152,14 @@ static void resize_vulkan(GLFWwindow* /*window*/, int w, int h)
info.components.b = VK_COMPONENT_SWIZZLE_B; info.components.b = VK_COMPONENT_SWIZZLE_B;
info.components.a = VK_COMPONENT_SWIZZLE_A; info.components.a = VK_COMPONENT_SWIZZLE_A;
info.subresourceRange = g_ImageRange; info.subresourceRange = g_ImageRange;
for(uint32_t i=0; i<g_BackBufferCount; i++){ for (uint32_t i = 0; i<g_BackBufferCount; i++)
{
info.image = g_BackBuffer[i]; info.image = g_BackBuffer[i];
err = vkCreateImageView(g_Device, &info, g_Allocator, &g_BackBufferView[i]); err = vkCreateImageView(g_Device, &info, g_Allocator, &g_BackBufferView[i]);
check_vk_result(err); check_vk_result(err);
} }
} }
// Create Framebuffer: // Create Framebuffer:
{ {
VkImageView attachment[1]; VkImageView attachment[1];
@ -162,7 +171,8 @@ static void resize_vulkan(GLFWwindow* /*window*/, int w, int h)
info.width = fb_width; info.width = fb_width;
info.height = fb_height; info.height = fb_height;
info.layers = 1; info.layers = 1;
for(uint32_t i=0; i<g_BackBufferCount; i++){ for (uint32_t i = 0; i<g_BackBufferCount; i++)
{
attachment[0] = g_BackBufferView[i]; attachment[0] = g_BackBufferView[i];
err = vkCreateFramebuffer(g_Device, &info, g_Allocator, &g_Framebuffer[i]); err = vkCreateFramebuffer(g_Device, &info, g_Allocator, &g_Framebuffer[i]);
check_vk_result(err); check_vk_result(err);
@ -173,9 +183,10 @@ static void resize_vulkan(GLFWwindow* /*window*/, int w, int h)
static void setup_vulkan(GLFWwindow* window) static void setup_vulkan(GLFWwindow* window)
{ {
VkResult err; VkResult err;
// Create Vulkan Instance // Create Vulkan Instance
{ {
int glfw_extensions_count; uint32_t glfw_extensions_count;
const char** glfw_extensions = glfwGetRequiredInstanceExtensions(&glfw_extensions_count); const char** glfw_extensions = glfwGetRequiredInstanceExtensions(&glfw_extensions_count);
VkInstanceCreateInfo create_info = {}; VkInstanceCreateInfo create_info = {};
create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
@ -184,17 +195,20 @@ static void setup_vulkan(GLFWwindow* window)
err = vkCreateInstance(&create_info, g_Allocator, &g_Instance); err = vkCreateInstance(&create_info, g_Allocator, &g_Instance);
check_vk_result(err); check_vk_result(err);
} }
// Create Window Surface // Create Window Surface
{ {
err = glfwCreateWindowSurface(g_Instance, window, g_Allocator, &g_Surface); err = glfwCreateWindowSurface(g_Instance, window, g_Allocator, &g_Surface);
check_vk_result(err); check_vk_result(err);
} }
// Get Gpu
// Get GPU
{ {
uint32_t count = 1; uint32_t count = 1;
err = vkEnumeratePhysicalDevices(g_Instance, &count, &g_Gpu); err = vkEnumeratePhysicalDevices(g_Instance, &count, &g_Gpu);
check_vk_result(err); check_vk_result(err);
} }
// Create Logical Device // Create Logical Device
{ {
int device_extension_count = 1; int device_extension_count = 1;
@ -217,6 +231,7 @@ static void setup_vulkan(GLFWwindow* window)
check_vk_result(err); check_vk_result(err);
vkGetDeviceQueue(g_Device, g_QueueFamily, queue_index, &g_Queue); vkGetDeviceQueue(g_Device, g_QueueFamily, queue_index, &g_Queue);
} }
// Create Framebuffers // Create Framebuffers
{ {
int w, h; int w, h;
@ -224,8 +239,10 @@ static void setup_vulkan(GLFWwindow* window)
resize_vulkan(window, w, h); resize_vulkan(window, w, h);
glfwSetFramebufferSizeCallback(window, resize_vulkan); glfwSetFramebufferSizeCallback(window, resize_vulkan);
} }
// Create Command Buffers // Create Command Buffers
for(int i=0; i<IMGUI_VK_QUEUED_FRAMES; i++){ for (int i=0; i<IMGUI_VK_QUEUED_FRAMES; i++)
{
{ {
VkCommandPoolCreateInfo info = {}; VkCommandPoolCreateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
@ -257,20 +274,23 @@ static void setup_vulkan(GLFWwindow* window)
check_vk_result(err); check_vk_result(err);
} }
} }
// Create Descriptor Pool // Create Descriptor Pool
{ {
VkDescriptorPoolSize pool_size[11] = { VkDescriptorPoolSize pool_size[11] =
{VK_DESCRIPTOR_TYPE_SAMPLER, 1000}, {
{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000}, { VK_DESCRIPTOR_TYPE_SAMPLER, 1000 },
{VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000}, { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000 },
{VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000}, { VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1000 },
{VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000}, { VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1000 },
{VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000}, { VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 1000 },
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000}, { VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER, 1000 },
{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000}, { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000 },
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000}, { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1000 },
{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000}, { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1000 },
{VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000}}; { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, 1000 },
{ VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 }
};
VkDescriptorPoolCreateInfo pool_info = {}; VkDescriptorPoolCreateInfo pool_info = {};
pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
@ -285,13 +305,15 @@ static void setup_vulkan(GLFWwindow* window)
static void cleanup_vulkan() static void cleanup_vulkan()
{ {
vkDestroyDescriptorPool(g_Device, g_DescriptorPool, g_Allocator); vkDestroyDescriptorPool(g_Device, g_DescriptorPool, g_Allocator);
for(int i=0; i<IMGUI_VK_QUEUED_FRAMES; i++){ for (int i = 0; i < IMGUI_VK_QUEUED_FRAMES; i++)
{
vkDestroyFence(g_Device, g_Fence[i], g_Allocator); vkDestroyFence(g_Device, g_Fence[i], g_Allocator);
vkFreeCommandBuffers(g_Device, g_CommandPool[i], 1, &g_CommandBuffer[i]); vkFreeCommandBuffers(g_Device, g_CommandPool[i], 1, &g_CommandBuffer[i]);
vkDestroyCommandPool(g_Device, g_CommandPool[i], g_Allocator); vkDestroyCommandPool(g_Device, g_CommandPool[i], g_Allocator);
vkDestroySemaphore(g_Device, g_Semaphore[i], g_Allocator); vkDestroySemaphore(g_Device, g_Semaphore[i], g_Allocator);
} }
for(uint32_t i=0; i<g_BackBufferCount; i++){ for (uint32_t i = 0; i < g_BackBufferCount; i++)
{
vkDestroyImageView(g_Device, g_BackBufferView[i], g_Allocator); vkDestroyImageView(g_Device, g_BackBufferView[i], g_Allocator);
vkDestroyFramebuffer(g_Device, g_Framebuffer[i], g_Allocator); vkDestroyFramebuffer(g_Device, g_Framebuffer[i], g_Allocator);
} }
@ -305,18 +327,15 @@ static void cleanup_vulkan()
static void frame_begin() static void frame_begin()
{ {
VkResult err; VkResult err;
while(true){ while (true)
{
err = vkWaitForFences(g_Device, 1, &g_Fence[g_FrameIndex], VK_TRUE, 100); err = vkWaitForFences(g_Device, 1, &g_Fence[g_FrameIndex], VK_TRUE, 100);
if(err == VK_SUCCESS) break; if (err == VK_SUCCESS) break;
if(err == VK_TIMEOUT) continue; if (err == VK_TIMEOUT) continue;
check_vk_result(err); check_vk_result(err);
} }
{ {
err = vkAcquireNextImageKHR( err = vkAcquireNextImageKHR(g_Device, g_Swapchain, UINT64_MAX, g_Semaphore[g_FrameIndex], VK_NULL_HANDLE, &g_BackBufferIndex);
g_Device, g_Swapchain,
UINT64_MAX,
g_Semaphore[g_FrameIndex], VK_NULL_HANDLE,
&g_BackBufferIndex);
check_vk_result(err); check_vk_result(err);
} }
{ {
@ -356,11 +375,7 @@ static void frame_end()
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = g_BackBuffer[g_BackBufferIndex]; barrier.image = g_BackBuffer[g_BackBufferIndex];
barrier.subresourceRange = g_ImageRange; barrier.subresourceRange = g_ImageRange;
vkCmdPipelineBarrier(g_CommandBuffer[g_FrameIndex], 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);
VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
0,
0, NULL, 0, NULL, 1, &barrier);
} }
{ {
VkSubmitInfo info = {}; VkSubmitInfo info = {};
@ -391,7 +406,7 @@ static void frame_end()
check_vk_result(err); check_vk_result(err);
check_vk_result(res); check_vk_result(res);
} }
g_FrameIndex = (g_FrameIndex)%IMGUI_VK_QUEUED_FRAMES; g_FrameIndex = (g_FrameIndex) % IMGUI_VK_QUEUED_FRAMES;
} }
static void error_callback(int error, const char* description) static void error_callback(int error, const char* description)
@ -410,7 +425,8 @@ int main(int, char**)
GLFWwindow* window = glfwCreateWindow(1280, 720, "ImGui Vulkan example", NULL, NULL); GLFWwindow* window = glfwCreateWindow(1280, 720, "ImGui Vulkan example", NULL, NULL);
// Setup Vulkan // Setup Vulkan
if(!glfwVulkanSupported()){ if (!glfwVulkanSupported())
{
printf("GLFW: Vulkan Not Supported\n"); printf("GLFW: Vulkan Not Supported\n");
return 1; return 1;
} }
@ -508,9 +524,7 @@ int main(int, char**)
g_ClearValue.color.float32[3] = clear_color.w; g_ClearValue.color.float32[3] = clear_color.w;
frame_begin(); frame_begin();
ImGui_ImplGlfwVulkan_Render(g_CommandBuffer[g_FrameIndex]); ImGui_ImplGlfwVulkan_Render(g_CommandBuffer[g_FrameIndex]);
frame_end(); frame_end();
} }