From 9d304789cd0e8cbbfa9e9803479b13eb8e9a5720 Mon Sep 17 00:00:00 2001 From: Jeremy Hayes Date: Sun, 9 Oct 2016 11:48:12 -0600 Subject: [PATCH] demos: clang-format cube.cpp Change-Id: I5f55446eb6f3c312b6cc012733b7f4e454ac73ba --- demos/cube.cpp | 2218 +++++++++++++++++++++++++++----------------------------- 1 file changed, 1087 insertions(+), 1131 deletions(-) diff --git a/demos/cube.cpp b/demos/cube.cpp index 60cde8a..080d9df 100644 --- a/demos/cube.cpp +++ b/demos/cube.cpp @@ -67,8 +67,7 @@ } while (0) #endif -struct texture_object -{ +struct texture_object { vk::Sampler sampler; vk::Image image; @@ -82,7 +81,7 @@ struct texture_object int32_t tex_height; }; -static char const*const tex_files[] = {"lunarg.ppm"}; +static char const *const tex_files[] = {"lunarg.ppm"}; static int validation_error = 0; @@ -191,9 +190,9 @@ static const float g_uv_buffer_data[] = { 1.0f, 1.0f, 1.0f, 0.0f, }; +// clang-format on -typedef struct -{ +typedef struct { vk::Image image; vk::CommandBuffer cmd; vk::CommandBuffer graphics_to_present_cmd; @@ -205,104 +204,82 @@ typedef struct LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); #endif -struct Demo -{ - Demo() : +struct Demo { + Demo() + : #if defined(VK_USE_PLATFORM_WIN32_KHR) - connection{nullptr}, - window{nullptr}, - minsize(POINT{0, 0}), // Use explicit construction to avoid MSVC error C2797. + connection{nullptr}, + window{nullptr}, + minsize(POINT{ + 0, 0}), // Use explicit construction to avoid MSVC error C2797. #elif defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR) - display{nullptr}, - xlib_window{0}, - xlib_wm_delete_window{0}, - connection{nullptr}, - screen{nullptr}, - xcb_window{0}, - atom_wm_delete_window{nullptr}, + display{nullptr}, + xlib_window{0}, xlib_wm_delete_window{0}, connection{nullptr}, + screen{nullptr}, xcb_window{0}, atom_wm_delete_window{nullptr}, #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) - display{nullptr}, - registry{nullptr}, - compositor{nullptr}, - window{nullptr}, - shell{nullptr}, - shell_surface{nullptr}, + display{nullptr}, + registry{nullptr}, compositor{nullptr}, window{nullptr}, + shell{nullptr}, shell_surface{nullptr}, #endif - prepared{false}, - use_staging_buffer{false}, - use_xlib{false}, - graphics_queue_family_index{0}, - present_queue_family_index{0}, - enabled_extension_count{0}, - enabled_layer_count{0}, - width{0}, - height{0}, - swapchainImageCount{0}, - frame_index{0}, - spin_angle{0.0f}, - spin_increment{0.0f}, - pause{false}, - quit{false}, - curFrame{0}, - frameCount{0}, - validate{false}, - use_break{false}, - suppress_popups{false}, - current_buffer{0}, - queue_family_count{0} - { + prepared{false}, + use_staging_buffer{false}, use_xlib{false}, + graphics_queue_family_index{0}, present_queue_family_index{0}, + enabled_extension_count{0}, enabled_layer_count{0}, width{0}, + height{0}, swapchainImageCount{0}, frame_index{0}, spin_angle{0.0f}, + spin_increment{0.0f}, pause{false}, quit{false}, curFrame{0}, + frameCount{0}, validate{false}, use_break{false}, + suppress_popups{false}, current_buffer{0}, queue_family_count{0} { #if defined(VK_USE_PLATFORM_WIN32_KHR) memset(name, '\0', APP_NAME_STR_LEN); #elif defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR) #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) #endif - memset(fencesInited, 0, sizeof(bool)*FRAME_LAG); + memset(fencesInited, 0, sizeof(bool) * FRAME_LAG); memset(projection_matrix, 0, sizeof(projection_matrix)); memset(view_matrix, 0, sizeof(view_matrix)); memset(model_matrix, 0, sizeof(model_matrix)); } - void build_image_ownership_cmd(uint32_t const& i) - { - auto const cmd_buf_info = vk::CommandBufferBeginInfo() - .setFlags(vk::CommandBufferUsageFlagBits::eSimultaneousUse); + void build_image_ownership_cmd(uint32_t const &i) { + auto const cmd_buf_info = vk::CommandBufferBeginInfo().setFlags( + vk::CommandBufferUsageFlagBits::eSimultaneousUse); auto result = buffers[i].graphics_to_present_cmd.begin(&cmd_buf_info); VERIFY(result == vk::Result::eSuccess); - auto const image_ownership_barrier = vk::ImageMemoryBarrier() - .setSrcAccessMask(vk::AccessFlags()) - .setDstAccessMask(vk::AccessFlagBits::eColorAttachmentWrite) - .setOldLayout(vk::ImageLayout::ePresentSrcKHR) - .setNewLayout(vk::ImageLayout::ePresentSrcKHR) - .setSrcQueueFamilyIndex(graphics_queue_family_index) - .setDstQueueFamilyIndex(present_queue_family_index) - .setImage(buffers[i].image) - .setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1)); + auto const image_ownership_barrier = + vk::ImageMemoryBarrier() + .setSrcAccessMask(vk::AccessFlags()) + .setDstAccessMask(vk::AccessFlagBits::eColorAttachmentWrite) + .setOldLayout(vk::ImageLayout::ePresentSrcKHR) + .setNewLayout(vk::ImageLayout::ePresentSrcKHR) + .setSrcQueueFamilyIndex(graphics_queue_family_index) + .setDstQueueFamilyIndex(present_queue_family_index) + .setImage(buffers[i].image) + .setSubresourceRange(vk::ImageSubresourceRange( + vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1)); - buffers[i].graphics_to_present_cmd.pipelineBarrier(vk::PipelineStageFlagBits::eColorAttachmentOutput, - vk::PipelineStageFlagBits::eColorAttachmentOutput, vk::DependencyFlagBits(), 0, nullptr, 0, nullptr, 1, - &image_ownership_barrier); + buffers[i].graphics_to_present_cmd.pipelineBarrier( + vk::PipelineStageFlagBits::eColorAttachmentOutput, + vk::PipelineStageFlagBits::eColorAttachmentOutput, + vk::DependencyFlagBits(), 0, nullptr, 0, nullptr, 1, + &image_ownership_barrier); result = buffers[i].graphics_to_present_cmd.end(); VERIFY(result == vk::Result::eSuccess); } - vk::Bool32 check_layers(uint32_t check_count, char const*const*const check_names, uint32_t layer_count, - vk::LayerProperties *layers) - { - for(uint32_t i = 0; i < check_count; i++) - { + vk::Bool32 check_layers(uint32_t check_count, + char const *const *const check_names, + uint32_t layer_count, vk::LayerProperties *layers) { + for (uint32_t i = 0; i < check_count; i++) { vk::Bool32 found = VK_FALSE; - for(uint32_t j = 0; j < layer_count; j++) - { - if(!strcmp(check_names[i], layers[j].layerName)) - { + for (uint32_t j = 0; j < layer_count; j++) { + if (!strcmp(check_names[i], layers[j].layerName)) { found = VK_TRUE; break; } } - if(!found) - { + if (!found) { fprintf(stderr, "Cannot find layer: %s\n", check_names[i]); return 0; } @@ -310,29 +287,24 @@ struct Demo return VK_TRUE; } - void cleanup() - { + void cleanup() { prepared = false; device.waitIdle(); // Wait for fences from present operations - for(uint32_t i = 0; i < FRAME_LAG; i++) - { - if(fencesInited[i]) - { + for (uint32_t i = 0; i < FRAME_LAG; i++) { + if (fencesInited[i]) { device.waitForFences(1, &fences[i], VK_TRUE, UINT64_MAX); } device.destroyFence(fences[i], nullptr); device.destroySemaphore(image_acquired_semaphores[i], nullptr); device.destroySemaphore(draw_complete_semaphores[i], nullptr); - if(separate_present_queue) - { + if (separate_present_queue) { device.destroySemaphore(image_ownership_semaphores[i], nullptr); } } - for(uint32_t i = 0; i < swapchainImageCount; i++) - { + for (uint32_t i = 0; i < swapchainImageCount; i++) { device.destroyFramebuffer(framebuffers[i], nullptr); } device.destroyDescriptorPool(desc_pool, nullptr); @@ -343,8 +315,7 @@ struct Demo device.destroyPipelineLayout(pipeline_layout, nullptr); device.destroyDescriptorSetLayout(desc_layout, nullptr); - for(uint32_t i = 0; i < texture_count; i++) - { + for (uint32_t i = 0; i < texture_count; i++) { device.destroyImageView(textures[i].view, nullptr); device.destroyImage(textures[i].image, nullptr); device.freeMemory(textures[i].mem, nullptr); @@ -359,16 +330,14 @@ struct Demo device.destroyBuffer(uniform_data.buf, nullptr); device.freeMemory(uniform_data.mem, nullptr); - for(uint32_t i = 0; i < swapchainImageCount; i++) - { + for (uint32_t i = 0; i < swapchainImageCount; i++) { device.destroyImageView(buffers[i].view, nullptr); device.freeCommandBuffers(cmd_pool, 1, &buffers[i].cmd); } device.destroyCommandPool(cmd_pool, nullptr); - if(separate_present_queue) - { + if (separate_present_queue) { device.destroyCommandPool(present_cmd_pool, nullptr); } @@ -377,13 +346,10 @@ struct Demo inst.destroy(nullptr); #if defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR) - if(use_xlib) - { + if (use_xlib) { XDestroyWindow(display, xlib_window); XCloseDisplay(display); - } - else - { + } else { xcb_destroy_window(connection, xcb_window); xcb_disconnect(connection); } @@ -398,8 +364,7 @@ struct Demo #endif } - void create_device() - { + void create_device() { float const priorities[1] = {0.0}; vk::DeviceQueueCreateInfo queues[2]; @@ -408,16 +373,16 @@ struct Demo queues[0].setPQueuePriorities(priorities); auto deviceInfo = vk::DeviceCreateInfo() - .setQueueCreateInfoCount(1) - .setPQueueCreateInfos(queues) - .setEnabledLayerCount(0) - .setPpEnabledLayerNames(nullptr) - .setEnabledExtensionCount(enabled_extension_count) - .setPpEnabledExtensionNames((const char *const *)extension_names) - .setPEnabledFeatures(nullptr); - - if(separate_present_queue) - { + .setQueueCreateInfoCount(1) + .setPQueueCreateInfos(queues) + .setEnabledLayerCount(0) + .setPpEnabledLayerNames(nullptr) + .setEnabledExtensionCount(enabled_extension_count) + .setPpEnabledExtensionNames( + (const char *const *)extension_names) + .setPEnabledFeatures(nullptr); + + if (separate_present_queue) { queues[1].setQueueFamilyIndex(present_queue_family_index); queues[1].setQueueCount(1); queues[1].setPQueuePriorities(priorities); @@ -428,28 +393,25 @@ struct Demo VERIFY(result == vk::Result::eSuccess); } - void destroy_texture_image(texture_object *tex_objs) - { + void destroy_texture_image(texture_object *tex_objs) { // clean up staging resources device.freeMemory(tex_objs->mem, nullptr); device.destroyImage(tex_objs->image, nullptr); } - void draw() - { - if(fencesInited[frame_index]) - { + void draw() { + if (fencesInited[frame_index]) { // Ensure no more than FRAME_LAG presentations are outstanding device.waitForFences(1, &fences[frame_index], VK_TRUE, UINT64_MAX); device.resetFences(1, &fences[frame_index]); } // Get the index of the next available swapchain image: - auto result = device.acquireNextImageKHR(swapchain, UINT64_MAX, image_acquired_semaphores[frame_index], fences[frame_index], - ¤t_buffer); + auto result = device.acquireNextImageKHR( + swapchain, UINT64_MAX, image_acquired_semaphores[frame_index], + fences[frame_index], ¤t_buffer); fencesInited[frame_index] = true; - if(result == vk::Result::eErrorOutOfDateKHR) - { + if (result == vk::Result::eErrorOutOfDateKHR) { // swapchain is out of date (e.g. the window was resized) and // must be recreated: frame_index += 1; @@ -458,14 +420,10 @@ struct Demo resize(); draw(); return; - } - else if(result == vk::Result::eSuboptimalKHR) - { + } else if (result == vk::Result::eSuboptimalKHR) { // swapchain is not as optimal as it could be, but the platform's // presentation engine will still present the image correctly. - } - else - { + } else { VERIFY(result == vk::Result::eSuccess); } @@ -473,142 +431,157 @@ struct Demo // that the image won't be rendered to until the presentation // engine has fully released ownership to the application, and it is // okay to render to the image. - vk::PipelineStageFlags const pipe_stage_flags = vk::PipelineStageFlagBits::eColorAttachmentOutput; - auto const submit_info = vk::SubmitInfo() - .setPWaitDstStageMask(&pipe_stage_flags) - .setWaitSemaphoreCount(1) - .setPWaitSemaphores(&image_acquired_semaphores[frame_index]) - .setCommandBufferCount(1) - .setPCommandBuffers(&buffers[current_buffer].cmd) - .setSignalSemaphoreCount(1) - .setPSignalSemaphores(&draw_complete_semaphores[frame_index]); + vk::PipelineStageFlags const pipe_stage_flags = + vk::PipelineStageFlagBits::eColorAttachmentOutput; + auto const submit_info = + vk::SubmitInfo() + .setPWaitDstStageMask(&pipe_stage_flags) + .setWaitSemaphoreCount(1) + .setPWaitSemaphores(&image_acquired_semaphores[frame_index]) + .setCommandBufferCount(1) + .setPCommandBuffers(&buffers[current_buffer].cmd) + .setSignalSemaphoreCount(1) + .setPSignalSemaphores(&draw_complete_semaphores[frame_index]); result = graphics_queue.submit(1, &submit_info, vk::Fence()); VERIFY(result == vk::Result::eSuccess); - if(separate_present_queue) - { + if (separate_present_queue) { // If we are using separate queues, change image ownership to the // present queue before presenting, waiting for the draw complete - // semaphore and signalling the ownership released semaphore when finished - auto const submit_info = vk::SubmitInfo() - .setPWaitDstStageMask(&pipe_stage_flags) - .setWaitSemaphoreCount(1) - .setPWaitSemaphores(&draw_complete_semaphores[frame_index]) - .setCommandBufferCount(1) - .setPCommandBuffers(&buffers[current_buffer].graphics_to_present_cmd) - .setSignalSemaphoreCount(1) - .setPSignalSemaphores(&image_ownership_semaphores[frame_index]); - - result = present_queue.submit(1, &submit_info, vk::Fence()); + // semaphore and signalling the ownership released semaphore when + // finished + auto const submit_info = + vk::SubmitInfo() + .setPWaitDstStageMask(&pipe_stage_flags) + .setWaitSemaphoreCount(1) + .setPWaitSemaphores(&draw_complete_semaphores[frame_index]) + .setCommandBufferCount(1) + .setPCommandBuffers( + &buffers[current_buffer].graphics_to_present_cmd) + .setSignalSemaphoreCount(1) + .setPSignalSemaphores( + &image_ownership_semaphores[frame_index]); + + result = present_queue.submit(1, &submit_info, vk::Fence()); VERIFY(result == vk::Result::eSuccess); } // If we are using separate queues we have to wait for image ownership, // otherwise wait for draw complete - auto const presentInfo = vk::PresentInfoKHR() - .setWaitSemaphoreCount(1) - .setPWaitSemaphores(separate_present_queue ? - &image_ownership_semaphores[frame_index] : &draw_complete_semaphores[frame_index]) - .setSwapchainCount(1) - .setPSwapchains(&swapchain) - .setPImageIndices(¤t_buffer); + auto const presentInfo = + vk::PresentInfoKHR() + .setWaitSemaphoreCount(1) + .setPWaitSemaphores( + separate_present_queue + ? &image_ownership_semaphores[frame_index] + : &draw_complete_semaphores[frame_index]) + .setSwapchainCount(1) + .setPSwapchains(&swapchain) + .setPImageIndices(¤t_buffer); result = present_queue.presentKHR(&presentInfo); frame_index += 1; frame_index %= FRAME_LAG; - if(result == vk::Result::eErrorOutOfDateKHR) - { + if (result == vk::Result::eErrorOutOfDateKHR) { // swapchain is out of date (e.g. the window was resized) and // must be recreated: resize(); - } - else if(result == vk::Result::eSuboptimalKHR) - { + } else if (result == vk::Result::eSuboptimalKHR) { // swapchain is not as optimal as it could be, but the platform's // presentation engine will still present the image correctly. - } - else - { + } else { VERIFY(result == vk::Result::eSuccess); } } - void draw_build_cmd(vk::CommandBuffer commandBuffer) - { - auto const commandInfo = vk::CommandBufferBeginInfo() - .setFlags(vk::CommandBufferUsageFlagBits::eSimultaneousUse); + void draw_build_cmd(vk::CommandBuffer commandBuffer) { + auto const commandInfo = vk::CommandBufferBeginInfo().setFlags( + vk::CommandBufferUsageFlagBits::eSimultaneousUse); - vk::ClearValue const clearValues[2] = - { - vk::ClearColorValue(std::array({ 0.2f, 0.2f, 0.2f, 0.2f })), - vk::ClearDepthStencilValue(1.0f, 0u) - }; + vk::ClearValue const clearValues[2] = { + vk::ClearColorValue(std::array({0.2f, 0.2f, 0.2f, 0.2f})), + vk::ClearDepthStencilValue(1.0f, 0u)}; - auto const passInfo = vk::RenderPassBeginInfo() - .setRenderPass(render_pass) - .setFramebuffer(framebuffers[current_buffer]) - .setRenderArea(vk::Rect2D(vk::Offset2D(0, 0), vk::Extent2D((uint32_t)width, (uint32_t)height))) - .setClearValueCount(2) - .setPClearValues(clearValues); + auto const passInfo = + vk::RenderPassBeginInfo() + .setRenderPass(render_pass) + .setFramebuffer(framebuffers[current_buffer]) + .setRenderArea( + vk::Rect2D(vk::Offset2D(0, 0), + vk::Extent2D((uint32_t)width, (uint32_t)height))) + .setClearValueCount(2) + .setPClearValues(clearValues); auto result = commandBuffer.begin(&commandInfo); VERIFY(result == vk::Result::eSuccess); - auto const image_memory_barrier = vk::ImageMemoryBarrier() - .setSrcAccessMask(vk::AccessFlagBits::eMemoryRead) - .setDstAccessMask(vk::AccessFlagBits::eColorAttachmentWrite) - .setOldLayout(vk::ImageLayout::ePresentSrcKHR) - .setNewLayout(vk::ImageLayout::eColorAttachmentOptimal) - .setSrcQueueFamilyIndex(VK_QUEUE_FAMILY_IGNORED) - .setDstQueueFamilyIndex(VK_QUEUE_FAMILY_IGNORED) - .setImage(buffers[current_buffer].image) - .setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1)); - - commandBuffer.pipelineBarrier(vk::PipelineStageFlagBits::eColorAttachmentOutput, - vk::PipelineStageFlagBits::eColorAttachmentOutput, vk::DependencyFlagBits(), 0, nullptr, 0, nullptr, 1, + auto const image_memory_barrier = + vk::ImageMemoryBarrier() + .setSrcAccessMask(vk::AccessFlagBits::eMemoryRead) + .setDstAccessMask(vk::AccessFlagBits::eColorAttachmentWrite) + .setOldLayout(vk::ImageLayout::ePresentSrcKHR) + .setNewLayout(vk::ImageLayout::eColorAttachmentOptimal) + .setSrcQueueFamilyIndex(VK_QUEUE_FAMILY_IGNORED) + .setDstQueueFamilyIndex(VK_QUEUE_FAMILY_IGNORED) + .setImage(buffers[current_buffer].image) + .setSubresourceRange(vk::ImageSubresourceRange( + vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1)); + + commandBuffer.pipelineBarrier( + vk::PipelineStageFlagBits::eColorAttachmentOutput, + vk::PipelineStageFlagBits::eColorAttachmentOutput, + vk::DependencyFlagBits(), 0, nullptr, 0, nullptr, 1, &image_memory_barrier); commandBuffer.beginRenderPass(&passInfo, vk::SubpassContents::eInline); commandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline); - commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, pipeline_layout, - 0, 1, &desc_set, - 0, nullptr); + commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eGraphics, + pipeline_layout, 0, 1, &desc_set, 0, + nullptr); auto const viewport = vk::Viewport() - .setWidth((float)width) - .setHeight((float)height) - .setMinDepth((float)0.0f) - .setMaxDepth((float)1.0f); + .setWidth((float)width) + .setHeight((float)height) + .setMinDepth((float)0.0f) + .setMaxDepth((float)1.0f); commandBuffer.setViewport(0, 1, &viewport); - vk::Rect2D const scissor(vk::Offset2D(0, 0), vk::Extent2D(width, height)); + vk::Rect2D const scissor(vk::Offset2D(0, 0), + vk::Extent2D(width, height)); commandBuffer.setScissor(0, 1, &scissor); commandBuffer.draw(12 * 3, 1, 0, 0); // Note that ending the renderpass changes the image's layout from // COLOR_ATTACHMENT_OPTIMAL to PRESENT_SRC_KHR commandBuffer.endRenderPass(); - if(separate_present_queue) - { - // We have to transfer ownership from the graphics queue family to the - // present queue family to be able to present. Note that we don't have - // to transfer from present queue family back to graphics queue family at - // the start of the next frame because we don't care about the image's + if (separate_present_queue) { + // We have to transfer ownership from the graphics queue family to + // the + // present queue family to be able to present. Note that we don't + // have + // to transfer from present queue family back to graphics queue + // family at + // the start of the next frame because we don't care about the + // image's // contents at that point. - auto const image_ownership_barrier = vk::ImageMemoryBarrier() - .setSrcAccessMask(vk::AccessFlags()) - .setDstAccessMask(vk::AccessFlagBits::eColorAttachmentWrite) - .setOldLayout(vk::ImageLayout::ePresentSrcKHR) - .setNewLayout(vk::ImageLayout::ePresentSrcKHR) - .setSrcQueueFamilyIndex(graphics_queue_family_index) - .setDstQueueFamilyIndex(present_queue_family_index) - .setImage(buffers[current_buffer].image) - .setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1)); - - commandBuffer.pipelineBarrier(vk::PipelineStageFlagBits::eColorAttachmentOutput, - vk::PipelineStageFlagBits::eColorAttachmentOutput, vk::DependencyFlagBits(), 0, nullptr, 0, nullptr, 1, + auto const image_ownership_barrier = + vk::ImageMemoryBarrier() + .setSrcAccessMask(vk::AccessFlags()) + .setDstAccessMask(vk::AccessFlagBits::eColorAttachmentWrite) + .setOldLayout(vk::ImageLayout::ePresentSrcKHR) + .setNewLayout(vk::ImageLayout::ePresentSrcKHR) + .setSrcQueueFamilyIndex(graphics_queue_family_index) + .setDstQueueFamilyIndex(present_queue_family_index) + .setImage(buffers[current_buffer].image) + .setSubresourceRange(vk::ImageSubresourceRange( + vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1)); + + commandBuffer.pipelineBarrier( + vk::PipelineStageFlagBits::eColorAttachmentOutput, + vk::PipelineStageFlagBits::eColorAttachmentOutput, + vk::DependencyFlagBits(), 0, nullptr, 0, nullptr, 1, &image_ownership_barrier); } @@ -616,31 +589,27 @@ struct Demo VERIFY(result == vk::Result::eSuccess); } - void flush_init_cmd() - { + void flush_init_cmd() { // TODO: hmm. - // This function could get called twice if the texture uses a staging buffer + // This function could get called twice if the texture uses a staging + // buffer // In that case the second call should be ignored - if(!cmd) - { + if (!cmd) { return; } auto result = cmd.end(); VERIFY(result == vk::Result::eSuccess); - auto const fenceInfo = vk::FenceCreateInfo() - .setFlags(vk::FenceCreateFlagBits(0)); + auto const fenceInfo = + vk::FenceCreateInfo().setFlags(vk::FenceCreateFlagBits(0)); vk::Fence fence; device.createFence(&fenceInfo, nullptr, &fence); - vk::CommandBuffer const commandBuffers[] = - { - cmd - }; - auto const submitInfo = vk::SubmitInfo() - .setCommandBufferCount(1) - .setPCommandBuffers(commandBuffers); + vk::CommandBuffer const commandBuffers[] = {cmd}; + auto const submitInfo = + vk::SubmitInfo().setCommandBufferCount(1).setPCommandBuffers( + commandBuffers); result = graphics_queue.submit(1, &submitInfo, fence); VERIFY(result == vk::Result::eSuccess); @@ -654,8 +623,7 @@ struct Demo cmd = vk::CommandBuffer(); } - void init(int argc, char **argv) - { + void init(int argc, char **argv) { vec3 eye = {0.0f, 3.0f, 5.0f}; vec3 origin = {0, 0, 0}; vec3 up = {0.0f, 1.0f, 0.0}; @@ -664,52 +632,46 @@ struct Demo use_xlib = false; for (int i = 1; i < argc; i++) { - if(strcmp(argv[i], "--use_staging") == 0) - { + if (strcmp(argv[i], "--use_staging") == 0) { use_staging_buffer = true; continue; } - if(strcmp(argv[i], "--break") == 0) - { + if (strcmp(argv[i], "--break") == 0) { use_break = true; continue; } - if(strcmp(argv[i], "--validate") == 0) - { + if (strcmp(argv[i], "--validate") == 0) { validate = true; continue; } #if defined(VK_USE_PLATFORM_XLIB_KHR) - if(strcmp(argv[i], "--xlib") == 0) - { + if (strcmp(argv[i], "--xlib") == 0) { use_xlib = true; continue; } #endif - if(strcmp(argv[i], "--c") == 0 && frameCount == UINT32_MAX && - i < argc - 1 && sscanf(argv[i + 1], "%d", &frameCount) == 1) - { + if (strcmp(argv[i], "--c") == 0 && frameCount == UINT32_MAX && + i < argc - 1 && sscanf(argv[i + 1], "%d", &frameCount) == 1) { i++; continue; } - if(strcmp(argv[i], "--suppress_popups") == 0) - { + if (strcmp(argv[i], "--suppress_popups") == 0) { suppress_popups = true; continue; } - fprintf(stderr, "Usage:\n %s [--use_staging] [--validate] [--break] " + fprintf(stderr, + "Usage:\n %s [--use_staging] [--validate] [--break] " #if defined(VK_USE_PLATFORM_XLIB_KHR) - "[--xlib] " + "[--xlib] " #endif - "[--c ] [--suppress_popups]\n", + "[--c ] [--suppress_popups]\n", APP_SHORT_NAME); fflush(stderr); exit(1); } - if(!use_xlib) - { + if (!use_xlib) { init_connection(); } @@ -727,19 +689,18 @@ struct Demo mat4x4_look_at(view_matrix, eye, origin, up); mat4x4_identity(model_matrix); - projection_matrix[1][1]*=-1; //Flip projection matrix from GL to Vulkan orientation. + projection_matrix[1][1] *= + -1; // Flip projection matrix from GL to Vulkan orientation. } - void init_connection() - { + void init_connection() { #if defined(VK_USE_PLATFORM_XCB_KHR) const xcb_setup_t *setup; xcb_screen_iterator_t iter; int scr; connection = xcb_connect(nullptr, &scr); - if(xcb_connection_has_error(connection) > 0) - { + if (xcb_connection_has_error(connection) > 0) { printf("Cannot find a compatible Vulkan installable client driver " "(ICD).\nExiting ...\n"); fflush(stdout); @@ -748,15 +709,14 @@ struct Demo setup = xcb_get_setup(connection); iter = xcb_setup_roots_iterator(setup); - while(scr-- > 0) + while (scr-- > 0) xcb_screen_next(&iter); screen = iter.data; #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) display = wl_display_connect(nullptr); - if(display == nullptr) - { + if (display == nullptr) { printf("Cannot find a compatible Vulkan installable client driver " "(ICD).\nExiting ...\n"); fflush(stdout); @@ -769,73 +729,74 @@ struct Demo #endif } - void init_vk() - { + void init_vk() { uint32_t instance_extension_count = 0; uint32_t instance_layer_count = 0; uint32_t validation_layer_count = 0; - char const*const* instance_validation_layers = nullptr; + char const *const *instance_validation_layers = nullptr; enabled_extension_count = 0; enabled_layer_count = 0; - char const*const instance_validation_layers_alt1[] = - { - "VK_LAYER_LUNARG_standard_validation" - }; + char const *const instance_validation_layers_alt1[] = { + "VK_LAYER_LUNARG_standard_validation"}; - char const*const instance_validation_layers_alt2[] = - { + char const *const instance_validation_layers_alt2[] = { "VK_LAYER_GOOGLE_threading", "VK_LAYER_LUNARG_parameter_validation", "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_image", "VK_LAYER_LUNARG_core_validation", "VK_LAYER_LUNARG_swapchain", - "VK_LAYER_GOOGLE_unique_objects" - }; + "VK_LAYER_GOOGLE_unique_objects"}; // Look for validation layers vk::Bool32 validation_found = VK_FALSE; - if(validate) - { - auto result = vk::enumerateInstanceLayerProperties(&instance_layer_count, nullptr); + if (validate) { + auto result = vk::enumerateInstanceLayerProperties( + &instance_layer_count, nullptr); VERIFY(result == vk::Result::eSuccess); instance_validation_layers = instance_validation_layers_alt1; - if(instance_layer_count > 0) - { - std::unique_ptr instance_layers(new vk::LayerProperties[instance_layer_count]); - result = vk::enumerateInstanceLayerProperties(&instance_layer_count, instance_layers.get()); + if (instance_layer_count > 0) { + std::unique_ptr instance_layers( + new vk::LayerProperties[instance_layer_count]); + result = vk::enumerateInstanceLayerProperties( + &instance_layer_count, instance_layers.get()); VERIFY(result == vk::Result::eSuccess); - validation_found = check_layers(ARRAY_SIZE(instance_validation_layers_alt1), instance_validation_layers, - instance_layer_count, instance_layers.get()); - if(validation_found) - { - enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt1); + validation_found = + check_layers(ARRAY_SIZE(instance_validation_layers_alt1), + instance_validation_layers, + instance_layer_count, instance_layers.get()); + if (validation_found) { + enabled_layer_count = + ARRAY_SIZE(instance_validation_layers_alt1); enabled_layers[0] = "VK_LAYER_LUNARG_standard_validation"; validation_layer_count = 1; - } - else - { + } else { // use alternative set of validation layers - instance_validation_layers = instance_validation_layers_alt2; - enabled_layer_count = ARRAY_SIZE(instance_validation_layers_alt2); - validation_found = check_layers(ARRAY_SIZE(instance_validation_layers_alt2), instance_validation_layers, - instance_layer_count, instance_layers.get()); - validation_layer_count = ARRAY_SIZE(instance_validation_layers_alt2); - for (uint32_t i = 0; i < validation_layer_count; i++) - { + instance_validation_layers = + instance_validation_layers_alt2; + enabled_layer_count = + ARRAY_SIZE(instance_validation_layers_alt2); + validation_found = check_layers( + ARRAY_SIZE(instance_validation_layers_alt2), + instance_validation_layers, instance_layer_count, + instance_layers.get()); + validation_layer_count = + ARRAY_SIZE(instance_validation_layers_alt2); + for (uint32_t i = 0; i < validation_layer_count; i++) { enabled_layers[i] = instance_validation_layers[i]; } } } - if(!validation_found) - { - ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find required validation layer.\n\n" - "Please look at the Getting Started guide for additional information.\n", - "vkCreateInstance Failure"); + if (!validation_found) { + ERR_EXIT("vkEnumerateInstanceLayerProperties failed to find " + "required validation layer.\n\n" + "Please look at the Getting Started guide for " + "additional information.\n", + "vkCreateInstance Failure"); } } @@ -847,125 +808,145 @@ struct Demo #endif memset(extension_names, 0, sizeof(extension_names)); - auto result = vk::enumerateInstanceExtensionProperties(nullptr, &instance_extension_count, nullptr); + auto result = vk::enumerateInstanceExtensionProperties( + nullptr, &instance_extension_count, nullptr); VERIFY(result == vk::Result::eSuccess); - if(instance_extension_count > 0) - { - std::unique_ptr instance_extensions(new vk::ExtensionProperties[instance_extension_count]); - result = vk::enumerateInstanceExtensionProperties(nullptr, &instance_extension_count, instance_extensions.get()); + if (instance_extension_count > 0) { + std::unique_ptr instance_extensions( + new vk::ExtensionProperties[instance_extension_count]); + result = vk::enumerateInstanceExtensionProperties( + nullptr, &instance_extension_count, instance_extensions.get()); VERIFY(result == vk::Result::eSuccess); - for(uint32_t i = 0; i < instance_extension_count; i++) - { - if(!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) - { + for (uint32_t i = 0; i < instance_extension_count; i++) { + if (!strcmp(VK_KHR_SURFACE_EXTENSION_NAME, + instance_extensions[i].extensionName)) { surfaceExtFound = 1; - extension_names[enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME; + extension_names[enabled_extension_count++] = + VK_KHR_SURFACE_EXTENSION_NAME; } #if defined(VK_USE_PLATFORM_WIN32_KHR) - if(!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) - { + if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, + instance_extensions[i].extensionName)) { platformSurfaceExtFound = 1; - extension_names[enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME; + extension_names[enabled_extension_count++] = + VK_KHR_WIN32_SURFACE_EXTENSION_NAME; } #endif #if defined(VK_USE_PLATFORM_XLIB_KHR) - if(!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) - { + if (!strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, + instance_extensions[i].extensionName)) { platformSurfaceExtFound = 1; xlibSurfaceExtFound = 1; - extension_names[enabled_extension_count++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME; + extension_names[enabled_extension_count++] = + VK_KHR_XLIB_SURFACE_EXTENSION_NAME; } #endif #if defined(VK_USE_PLATFORM_XCB_KHR) - if(!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) - { + if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, + instance_extensions[i].extensionName)) { platformSurfaceExtFound = 1; - extension_names[enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME; + extension_names[enabled_extension_count++] = + VK_KHR_XCB_SURFACE_EXTENSION_NAME; } #endif #if defined(VK_USE_PLATFORM_WAYLAND_KHR) - if(!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) - { + if (!strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, + instance_extensions[i].extensionName)) { platformSurfaceExtFound = 1; - extension_names[enabled_extension_count++] = VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME; + extension_names[enabled_extension_count++] = + VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME; } #endif assert(enabled_extension_count < 64); } } - if(!surfaceExtFound) - { - ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_SURFACE_EXTENSION_NAME " extension.\n\n" - "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" - "Please look at the Getting Started guide for additional information.\n", - "vkCreateInstance Failure"); + if (!surfaceExtFound) { + ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find " + "the " VK_KHR_SURFACE_EXTENSION_NAME " extension.\n\n" + "Do you have a compatible Vulkan installable client " + "driver (ICD) installed?\n" + "Please look at the Getting Started guide for additional " + "information.\n", + "vkCreateInstance Failure"); } - if(!platformSurfaceExtFound) - { + if (!platformSurfaceExtFound) { #if defined(VK_USE_PLATFORM_WIN32_KHR) - ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME " extension.\n\n" - "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" - "Please look at the Getting Started guide for additional information.\n", - "vkCreateInstance Failure"); + ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find " + "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME + " extension.\n\n" + "Do you have a compatible Vulkan installable client " + "driver (ICD) installed?\n" + "Please look at the Getting Started guide for additional " + "information.\n", + "vkCreateInstance Failure"); #elif defined(VK_USE_PLATFORM_XCB_KHR) - ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XCB_SURFACE_EXTENSION_NAME " extension.\n\n" - "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" - "Please look at the Getting Started guide for additional information.\n", - "vkCreateInstance Failure"); + ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find " + "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME " extension.\n\n" + "Do you have a compatible Vulkan installable client " + "driver (ICD) installed?\n" + "Please look at the Getting Started guide for additional " + "information.\n", + "vkCreateInstance Failure"); #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) - ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME " extension.\n\n" - "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" - "Please look at the Getting Started guide for additional information.\n", - "vkCreateInstance Failure"); + ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find " + "the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME + " extension.\n\n" + "Do you have a compatible Vulkan installable client " + "driver (ICD) installed?\n" + "Please look at the Getting Started guide for additional " + "information.\n", + "vkCreateInstance Failure"); #endif } #if defined(VK_USE_PLATFORM_XLIB_KHR) - if(use_xlib && !xlibSurfaceExtFound) - { - ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME " extension.\n\n" - "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" - "Please look at the Getting Started guide for additional information.\n", - "vkCreateInstance Failure"); + if (use_xlib && !xlibSurfaceExtFound) { + ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find " + "the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME " extension.\n\n" + "Do you have a compatible Vulkan installable client " + "driver (ICD) installed?\n" + "Please look at the Getting Started guide for additional " + "information.\n", + "vkCreateInstance Failure"); } #endif auto const app = vk::ApplicationInfo() - .setPApplicationName(APP_SHORT_NAME) - .setApplicationVersion(0) - .setPEngineName(APP_SHORT_NAME) - .setEngineVersion(0) - .setApiVersion(VK_API_VERSION_1_0); - auto const inst_info = vk::InstanceCreateInfo() - .setPApplicationInfo(&app) - .setEnabledLayerCount(enabled_layer_count) - .setPpEnabledLayerNames(instance_validation_layers) - .setEnabledExtensionCount(enabled_extension_count) - .setPpEnabledExtensionNames(extension_names); + .setPApplicationName(APP_SHORT_NAME) + .setApplicationVersion(0) + .setPEngineName(APP_SHORT_NAME) + .setEngineVersion(0) + .setApiVersion(VK_API_VERSION_1_0); + auto const inst_info = + vk::InstanceCreateInfo() + .setPApplicationInfo(&app) + .setEnabledLayerCount(enabled_layer_count) + .setPpEnabledLayerNames(instance_validation_layers) + .setEnabledExtensionCount(enabled_extension_count) + .setPpEnabledExtensionNames(extension_names); result = vk::createInstance(&inst_info, nullptr, &inst); - if(result == vk::Result::eErrorIncompatibleDriver) - { - ERR_EXIT("Cannot find a compatible Vulkan installable client driver (ICD).\n\n" - "Please look at the Getting Started guide for additional information.\n", - "vkCreateInstance Failure"); - } - else if(result == vk::Result::eErrorExtensionNotPresent) - { + if (result == vk::Result::eErrorIncompatibleDriver) { + ERR_EXIT("Cannot find a compatible Vulkan installable client " + "driver (ICD).\n\n" + "Please look at the Getting Started guide for additional " + "information.\n", + "vkCreateInstance Failure"); + } else if (result == vk::Result::eErrorExtensionNotPresent) { ERR_EXIT("Cannot find a specified extension library.\n" - "Make sure your layers path is set appropriately.\n", - "vkCreateInstance Failure"); - } - else if(result != vk::Result::eSuccess) - { + "Make sure your layers path is set appropriately.\n", + "vkCreateInstance Failure"); + } else if (result != vk::Result::eSuccess) { ERR_EXIT("vkCreateInstance failed.\n\n" - "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" - "Please look at the Getting Started guide for additional information.\n", - "vkCreateInstance Failure"); + "Do you have a compatible Vulkan installable client " + "driver (ICD) installed?\n" + "Please look at the Getting Started guide for additional " + "information.\n", + "vkCreateInstance Failure"); } /* Make initial call to query gpu_count, then second call for gpu info*/ @@ -974,20 +955,22 @@ struct Demo VERIFY(result == vk::Result::eSuccess); assert(gpu_count > 0); - if(gpu_count > 0) - { - std::unique_ptr physical_devices(new vk::PhysicalDevice[gpu_count]); - result = inst.enumeratePhysicalDevices(&gpu_count, physical_devices.get()); + if (gpu_count > 0) { + std::unique_ptr physical_devices( + new vk::PhysicalDevice[gpu_count]); + result = inst.enumeratePhysicalDevices(&gpu_count, + physical_devices.get()); VERIFY(result == vk::Result::eSuccess); /* For cube demo we just grab the first physical device */ gpu = physical_devices[0]; - } - else - { - ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible devices.\n\n" - "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" - "Please look at the Getting Started guide for additional information.\n", - "vkEnumeratePhysicalDevices Failure"); + } else { + ERR_EXIT("vkEnumeratePhysicalDevices reported zero accessible " + "devices.\n\n" + "Do you have a compatible Vulkan installable client " + "driver (ICD) installed?\n" + "Please look at the Getting Started guide for additional " + "information.\n", + "vkEnumeratePhysicalDevices Failure"); } /* Look for device extensions */ @@ -996,32 +979,36 @@ struct Demo enabled_extension_count = 0; memset(extension_names, 0, sizeof(extension_names)); - result = gpu.enumerateDeviceExtensionProperties(nullptr, &device_extension_count, nullptr); + result = gpu.enumerateDeviceExtensionProperties( + nullptr, &device_extension_count, nullptr); VERIFY(result == vk::Result::eSuccess); - if(device_extension_count > 0) - { - std::unique_ptr device_extensions(new vk::ExtensionProperties[device_extension_count]); - result = gpu.enumerateDeviceExtensionProperties(nullptr, &device_extension_count, device_extensions.get()); + if (device_extension_count > 0) { + std::unique_ptr device_extensions( + new vk::ExtensionProperties[device_extension_count]); + result = gpu.enumerateDeviceExtensionProperties( + nullptr, &device_extension_count, device_extensions.get()); VERIFY(result == vk::Result::eSuccess); - for(uint32_t i = 0; i < device_extension_count; i++) - { - if(!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, device_extensions[i].extensionName)) - { + for (uint32_t i = 0; i < device_extension_count; i++) { + if (!strcmp(VK_KHR_SWAPCHAIN_EXTENSION_NAME, + device_extensions[i].extensionName)) { swapchainExtFound = 1; - extension_names[enabled_extension_count++] = VK_KHR_SWAPCHAIN_EXTENSION_NAME; + extension_names[enabled_extension_count++] = + VK_KHR_SWAPCHAIN_EXTENSION_NAME; } assert(enabled_extension_count < 64); } } - if(!swapchainExtFound) - { - ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find the " VK_KHR_SWAPCHAIN_EXTENSION_NAME " extension.\n\n" - "Do you have a compatible Vulkan installable client driver (ICD) installed?\n" - "Please look at the Getting Started guide for additional information.\n", - "vkCreateInstance Failure"); + if (!swapchainExtFound) { + ERR_EXIT("vkEnumerateDeviceExtensionProperties failed to find " + "the " VK_KHR_SWAPCHAIN_EXTENSION_NAME " extension.\n\n" + "Do you have a compatible Vulkan installable client " + "driver (ICD) installed?\n" + "Please look at the Getting Started guide for additional " + "information.\n", + "vkCreateInstance Failure"); } gpu.getProperties(&gpu_props); @@ -1040,71 +1027,67 @@ struct Demo gpu.getFeatures(&physDevFeatures); } - void init_vk_swapchain() - { - // Create a WSI surface for the window: + void init_vk_swapchain() { +// Create a WSI surface for the window: #if defined(VK_USE_PLATFORM_WIN32_KHR) { auto const createInfo = vk::Win32SurfaceCreateInfoKHR() - .setHinstance(connection) - .setHwnd(window); + .setHinstance(connection) + .setHwnd(window); - auto result = inst.createWin32SurfaceKHR(&createInfo, nullptr, &surface); + auto result = + inst.createWin32SurfaceKHR(&createInfo, nullptr, &surface); VERIFY(result == vk::Result::eSuccess); } #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) && !defined(VK_USE_PLATFORM_XCB_KHR) { auto const createInfo = vk::WaylandSurfaceCreateInfoKHR() - .setDisplay(display) - .setSurface(window); + .setDisplay(display) + .setSurface(window); - auto result = inst.createWaylandSurfaceKHR(&createInfo, nullptr, &surface); + auto result = + inst.createWaylandSurfaceKHR(&createInfo, nullptr, &surface); VERIFY(result == vk::Result::eSuccess); } #endif - if(use_xlib) - { + if (use_xlib) { #if defined(VK_USE_PLATFORM_XLIB_KHR) - auto const createInfo = vk::XlibSurfaceCreateInfoKHR() - .setDpy(display) - .setWindow(xlib_window); + auto const createInfo = + vk::XlibSurfaceCreateInfoKHR().setDpy(display).setWindow( + xlib_window); - auto result = inst.createXlibSurfaceKHR(&createInfo, nullptr, &surface); + auto result = + inst.createXlibSurfaceKHR(&createInfo, nullptr, &surface); VERIFY(result == vk::Result::eSuccess); #endif - } - else - { + } else { #if defined(VK_USE_PLATFORM_XCB_KHR) auto const createInfo = vk::XcbSurfaceCreateInfoKHR() - .setConnection(connection) - .setWindow(xcb_window); + .setConnection(connection) + .setWindow(xcb_window); - auto result = inst.createXcbSurfaceKHR(&createInfo, nullptr, &surface); + auto result = + inst.createXcbSurfaceKHR(&createInfo, nullptr, &surface); VERIFY(result == vk::Result::eSuccess); #endif } // Iterate over each queue to learn whether it supports presenting: - std::unique_ptr supportsPresent(new vk::Bool32[queue_family_count]); - for(uint32_t i = 0; i < queue_family_count; i++) - { + std::unique_ptr supportsPresent( + new vk::Bool32[queue_family_count]); + for (uint32_t i = 0; i < queue_family_count; i++) { gpu.getSurfaceSupportKHR(i, surface, &supportsPresent[i]); } uint32_t graphicsQueueFamilyIndex = UINT32_MAX; uint32_t presentQueueFamilyIndex = UINT32_MAX; - for(uint32_t i = 0; i < queue_family_count; i++) - { - if(queue_props[i].queueFlags & vk::QueueFlagBits::eGraphics) - { - if(graphicsQueueFamilyIndex == UINT32_MAX) - { + for (uint32_t i = 0; i < queue_family_count; i++) { + if (queue_props[i].queueFlags & vk::QueueFlagBits::eGraphics) { + if (graphicsQueueFamilyIndex == UINT32_MAX) { graphicsQueueFamilyIndex = i; } - if (supportsPresent[i] == VK_TRUE) - { + if (supportsPresent[i] == VK_TRUE) { graphicsQueueFamilyIndex = i; presentQueueFamilyIndex = i; break; @@ -1112,14 +1095,12 @@ struct Demo } } - if(presentQueueFamilyIndex == UINT32_MAX) - { - // If didn't find a queue that supports both graphics and present, then + if (presentQueueFamilyIndex == UINT32_MAX) { + // If didn't find a queue that supports both graphics and present, + // then // find a separate present queue. - for(uint32_t i = 0; i < queue_family_count; ++i) - { - if(supportsPresent[i] == VK_TRUE) - { + for (uint32_t i = 0; i < queue_family_count; ++i) { + if (supportsPresent[i] == VK_TRUE) { presentQueueFamilyIndex = i; break; } @@ -1127,24 +1108,23 @@ struct Demo } // Generate error if could not find both a graphics and a present queue - if(graphicsQueueFamilyIndex == UINT32_MAX || presentQueueFamilyIndex == UINT32_MAX) - { - ERR_EXIT("Could not find both graphics and present queues\n", "Swapchain Initialization Failure"); + if (graphicsQueueFamilyIndex == UINT32_MAX || + presentQueueFamilyIndex == UINT32_MAX) { + ERR_EXIT("Could not find both graphics and present queues\n", + "Swapchain Initialization Failure"); } graphics_queue_family_index = graphicsQueueFamilyIndex; present_queue_family_index = presentQueueFamilyIndex; - separate_present_queue = (graphics_queue_family_index != present_queue_family_index); + separate_present_queue = + (graphics_queue_family_index != present_queue_family_index); create_device(); device.getQueue(graphics_queue_family_index, 0, &graphics_queue); - if(!separate_present_queue) - { + if (!separate_present_queue) { present_queue = graphics_queue; - } - else - { + } else { device.getQueue(present_queue_family_index, 0, &present_queue); } @@ -1153,19 +1133,19 @@ struct Demo auto result = gpu.getSurfaceFormatsKHR(surface, &formatCount, nullptr); VERIFY(result == vk::Result::eSuccess); - std::unique_ptr surfFormats(new vk::SurfaceFormatKHR[formatCount]); - result = gpu.getSurfaceFormatsKHR(surface, &formatCount, surfFormats.get()); + std::unique_ptr surfFormats( + new vk::SurfaceFormatKHR[formatCount]); + result = + gpu.getSurfaceFormatsKHR(surface, &formatCount, surfFormats.get()); VERIFY(result == vk::Result::eSuccess); // If the format list includes just one entry of VK_FORMAT_UNDEFINED, // the surface has no preferred format. Otherwise, at least one // supported format will be returned. - if(formatCount == 1 && surfFormats[0].format == vk::Format::eUndefined) - { + if (formatCount == 1 && + surfFormats[0].format == vk::Format::eUndefined) { format = vk::Format::eB8G8R8A8Unorm; - } - else - { + } else { assert(formatCount >= 1); format = surfFormats[0].format; } @@ -1181,19 +1161,20 @@ struct Demo // Create fences that we can use to throttle if we get too far // ahead of the image presents vk::FenceCreateInfo const fence_ci; - for(uint32_t i = 0; i < FRAME_LAG; i++) - { + for (uint32_t i = 0; i < FRAME_LAG; i++) { device.createFence(&fence_ci, nullptr, &fences[i]); fencesInited[i] = false; - result = device.createSemaphore(&semaphoreCreateInfo, nullptr, &image_acquired_semaphores[i]); + result = device.createSemaphore(&semaphoreCreateInfo, nullptr, + &image_acquired_semaphores[i]); VERIFY(result == vk::Result::eSuccess); - result = device.createSemaphore(&semaphoreCreateInfo, nullptr, &draw_complete_semaphores[i]); + result = device.createSemaphore(&semaphoreCreateInfo, nullptr, + &draw_complete_semaphores[i]); VERIFY(result == vk::Result::eSuccess); - if(separate_present_queue) - { - result = device.createSemaphore(&semaphoreCreateInfo, nullptr, &image_ownership_semaphores[i]); + if (separate_present_queue) { + result = device.createSemaphore(&semaphoreCreateInfo, nullptr, + &image_ownership_semaphores[i]); VERIFY(result == vk::Result::eSuccess); } } @@ -1203,17 +1184,18 @@ struct Demo gpu.getMemoryProperties(&memory_properties); } - void prepare() - { - auto const cmd_pool_info = vk::CommandPoolCreateInfo() - .setQueueFamilyIndex(graphics_queue_family_index); - auto result = device.createCommandPool(&cmd_pool_info, nullptr, &cmd_pool); + void prepare() { + auto const cmd_pool_info = + vk::CommandPoolCreateInfo().setQueueFamilyIndex( + graphics_queue_family_index); + auto result = + device.createCommandPool(&cmd_pool_info, nullptr, &cmd_pool); VERIFY(result == vk::Result::eSuccess); auto const cmd = vk::CommandBufferAllocateInfo() - .setCommandPool(cmd_pool) - .setLevel(vk::CommandBufferLevel::ePrimary) - .setCommandBufferCount(1); + .setCommandPool(cmd_pool) + .setLevel(vk::CommandBufferLevel::ePrimary) + .setCommandBufferCount(1); prepare_buffers(); prepare_depth(); @@ -1224,28 +1206,28 @@ struct Demo prepare_render_pass(); prepare_pipeline(); - for(uint32_t i = 0; i < swapchainImageCount; ++i) - { + for (uint32_t i = 0; i < swapchainImageCount; ++i) { result = device.allocateCommandBuffers(&cmd, &buffers[i].cmd); VERIFY(result == vk::Result::eSuccess); } - if(separate_present_queue) - { - auto const cmd_pool_info = vk::CommandPoolCreateInfo() - .setQueueFamilyIndex(present_queue_family_index); + if (separate_present_queue) { + auto const cmd_pool_info = + vk::CommandPoolCreateInfo().setQueueFamilyIndex( + present_queue_family_index); - result = device.createCommandPool(&cmd_pool_info, nullptr, &present_cmd_pool); + result = device.createCommandPool(&cmd_pool_info, nullptr, + &present_cmd_pool); VERIFY(result == vk::Result::eSuccess); auto const cmd = vk::CommandBufferAllocateInfo() - .setCommandPool(present_cmd_pool) - .setLevel(vk::CommandBufferLevel::ePrimary) - .setCommandBufferCount(1); + .setCommandPool(present_cmd_pool) + .setLevel(vk::CommandBufferLevel::ePrimary) + .setCommandBufferCount(1); - for(uint32_t i = 0; i < swapchainImageCount; i++) - { - result = device.allocateCommandBuffers(&cmd, &buffers[i].graphics_to_present_cmd); + for (uint32_t i = 0; i < swapchainImageCount; i++) { + result = device.allocateCommandBuffers( + &cmd, &buffers[i].graphics_to_present_cmd); VERIFY(result == vk::Result::eSuccess); build_image_ownership_cmd(i); @@ -1257,8 +1239,7 @@ struct Demo prepare_framebuffers(); - for(uint32_t i = 0; i < swapchainImageCount; ++i) - { + for (uint32_t i = 0; i < swapchainImageCount; ++i) { current_buffer = i; draw_build_cmd(buffers[i].cmd); } @@ -1273,8 +1254,7 @@ struct Demo prepared = true; } - void prepare_buffers() - { + void prepare_buffers() { vk::SwapchainKHR oldSwapchain = swapchain; // Check the surface capabilities and formats @@ -1283,24 +1263,24 @@ struct Demo VERIFY(result == vk::Result::eSuccess); uint32_t presentModeCount; - result = gpu.getSurfacePresentModesKHR(surface, &presentModeCount, nullptr); + result = + gpu.getSurfacePresentModesKHR(surface, &presentModeCount, nullptr); VERIFY(result == vk::Result::eSuccess); - std::unique_ptr presentModes(new vk::PresentModeKHR[presentModeCount]); - result = gpu.getSurfacePresentModesKHR(surface, &presentModeCount, presentModes.get()); + std::unique_ptr presentModes( + new vk::PresentModeKHR[presentModeCount]); + result = gpu.getSurfacePresentModesKHR(surface, &presentModeCount, + presentModes.get()); VERIFY(result == vk::Result::eSuccess); vk::Extent2D swapchainExtent; // width and height are either both -1, or both not -1. - if(surfCapabilities.currentExtent.width == (uint32_t)-1) - { + if (surfCapabilities.currentExtent.width == (uint32_t)-1) { // If the surface size is undefined, the size is set to // the size of the images requested. swapchainExtent.width = width; swapchainExtent.height = height; - } - else - { + } else { // If the surface size is defined, the swap chain size must match swapchainExtent = surfCapabilities.currentExtent; width = surfCapabilities.currentExtent.width; @@ -1311,29 +1291,28 @@ struct Demo // and to have no tearing. It's a great default present mode to use. vk::PresentModeKHR swapchainPresentMode = vk::PresentModeKHR::eFifo; - // There are times when you may wish to use another present mode. The - // following code shows how to select them, and the comments provide some - // reasons you may wish to use them. - // - // It should be noted that Vulkan 1.0 doesn't provide a method for - // synchronizing rendering with the presentation engine's display. There - // is a method provided for throttling rendering with the display, but - // there are some presentation engines for which this method will not work. - // If an application doesn't throttle its rendering, and if it renders much - // faster than the refresh rate of the display, this can waste power on - // mobile devices. That is because power is being spent rendering images - // that may never be seen. +// There are times when you may wish to use another present mode. The +// following code shows how to select them, and the comments provide some +// reasons you may wish to use them. +// +// It should be noted that Vulkan 1.0 doesn't provide a method for +// synchronizing rendering with the presentation engine's display. There +// is a method provided for throttling rendering with the display, but +// there are some presentation engines for which this method will not work. +// If an application doesn't throttle its rendering, and if it renders much +// faster than the refresh rate of the display, this can waste power on +// mobile devices. That is because power is being spent rendering images +// that may never be seen. //#define DESIRE_VK_PRESENT_MODE_IMMEDIATE_KHR //#define DESIRE_VK_PRESENT_MODE_MAILBOX_KHR //#define DESIRE_VK_PRESENT_MODE_FIFO_RELAXED_KHR #if defined(DESIRE_VK_PRESENT_MODE_IMMEDIATE_KHR) - // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care about + // VK_PRESENT_MODE_IMMEDIATE_KHR is for applications that don't care + // about // tearing, or have some way of synchronizing their rendering with the // display. - for(size_t i = 0; i < presentModeCount; ++i) - { - if(presentModes[i] == vk::PresentModeKHR::eImmediate) - { + for (size_t i = 0; i < presentModeCount; ++i) { + if (presentModes[i] == vk::PresentModeKHR::eImmediate) { swapchainPresentMode = vk::PresentModeKHR::eImmediate; break; } @@ -1341,115 +1320,124 @@ struct Demo #elif defined(DESIRE_VK_PRESENT_MODE_MAILBOX_KHR) // VK_PRESENT_MODE_MAILBOX_KHR may be useful for applications that // generally render a new presentable image every refresh cycle, but are - // occasionally early. In this case, the application wants the new image - // to be displayed instead of the previously-queued-for-presentation image + // occasionally early. In this case, the application wants the new + // image + // to be displayed instead of the previously-queued-for-presentation + // image // that has not yet been displayed. - for(size_t i = 0; i < presentModeCount; ++i) - { - if (presentModes[i] == vk::PresentModeKHR::eMailbox) - { + for (size_t i = 0; i < presentModeCount; ++i) { + if (presentModes[i] == vk::PresentModeKHR::eMailbox) { swapchainPresentMode = vk::PresentModeKHR::eMailbox; break; } } #elif defined(DESIRE_VK_PRESENT_MODE_FIFO_RELAXED_KHR) // VK_PRESENT_MODE_FIFO_RELAXED_KHR is for applications that generally - // render a new presentable image every refresh cycle, but are occasionally + // render a new presentable image every refresh cycle, but are + // occasionally // late. In this case (perhaps because of stuttering/latency concerns), - // the application wants the late image to be immediately displayed, even + // the application wants the late image to be immediately displayed, + // even // though that may mean some tearing. - for(size_t i = 0; i < presentModeCount; ++i) - { - if(presentModes[i] == vk::PresentModeKHR::eFifoRelaxed) - { + for (size_t i = 0; i < presentModeCount; ++i) { + if (presentModes[i] == vk::PresentModeKHR::eFifoRelaxed) { swapchainPresentMode = vk::PresentModeKHR::eFifoRelaxed; break; } } #endif - // Determine the number of VkImage's to use in the swap chain (we desire to + // Determine the number of VkImage's to use in the swap chain (we desire + // to // own only 1 image at a time, besides the images being displayed and // queued for display): - uint32_t desiredNumberOfSwapchainImages = surfCapabilities.minImageCount + 1; - // If maxImageCount is 0, we can ask for as many images as we want, otherwise + uint32_t desiredNumberOfSwapchainImages = + surfCapabilities.minImageCount + 1; + // If maxImageCount is 0, we can ask for as many images as we want, + // otherwise // we're limited to maxImageCount - if((surfCapabilities.maxImageCount > 0) && (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) - { + if ((surfCapabilities.maxImageCount > 0) && + (desiredNumberOfSwapchainImages > surfCapabilities.maxImageCount)) { // Application must settle for fewer images than desired: desiredNumberOfSwapchainImages = surfCapabilities.maxImageCount; } vk::SurfaceTransformFlagBitsKHR preTransform; - if(surfCapabilities.supportedTransforms & vk::SurfaceTransformFlagBitsKHR::eIdentity) - { + if (surfCapabilities.supportedTransforms & + vk::SurfaceTransformFlagBitsKHR::eIdentity) { preTransform = vk::SurfaceTransformFlagBitsKHR::eIdentity; - } - else - { + } else { preTransform = surfCapabilities.currentTransform; } - auto const swapchain_ci = vk::SwapchainCreateInfoKHR() - .setSurface(surface) - .setMinImageCount(desiredNumberOfSwapchainImages) - .setImageFormat(format) - .setImageColorSpace(color_space) - .setImageExtent({ swapchainExtent.width, swapchainExtent.height }) - .setImageArrayLayers(1) - .setImageUsage(vk::ImageUsageFlagBits::eColorAttachment) - .setImageSharingMode(vk::SharingMode::eExclusive) - .setQueueFamilyIndexCount(0) - .setPQueueFamilyIndices(nullptr) - .setPreTransform(preTransform) - .setCompositeAlpha(vk::CompositeAlphaFlagBitsKHR::eOpaque) - .setPresentMode(swapchainPresentMode) - .setClipped(true) - .setOldSwapchain(oldSwapchain); + auto const swapchain_ci = + vk::SwapchainCreateInfoKHR() + .setSurface(surface) + .setMinImageCount(desiredNumberOfSwapchainImages) + .setImageFormat(format) + .setImageColorSpace(color_space) + .setImageExtent({swapchainExtent.width, swapchainExtent.height}) + .setImageArrayLayers(1) + .setImageUsage(vk::ImageUsageFlagBits::eColorAttachment) + .setImageSharingMode(vk::SharingMode::eExclusive) + .setQueueFamilyIndexCount(0) + .setPQueueFamilyIndices(nullptr) + .setPreTransform(preTransform) + .setCompositeAlpha(vk::CompositeAlphaFlagBitsKHR::eOpaque) + .setPresentMode(swapchainPresentMode) + .setClipped(true) + .setOldSwapchain(oldSwapchain); result = device.createSwapchainKHR(&swapchain_ci, nullptr, &swapchain); VERIFY(result == vk::Result::eSuccess); - // If we just re-created an existing swapchain, we should destroy the old + // If we just re-created an existing swapchain, we should destroy the + // old // swapchain at this point. // Note: destroying the swapchain also cleans up all its associated // presentable images once the platform is done with them. - if(oldSwapchain) - { + if (oldSwapchain) { device.destroySwapchainKHR(oldSwapchain, nullptr); } - result = device.getSwapchainImagesKHR(swapchain, &swapchainImageCount, nullptr); + result = device.getSwapchainImagesKHR(swapchain, &swapchainImageCount, + nullptr); VERIFY(result == vk::Result::eSuccess); - std::unique_ptr swapchainImages(new vk::Image[swapchainImageCount]); - result = device.getSwapchainImagesKHR(swapchain, &swapchainImageCount, swapchainImages.get()); + std::unique_ptr swapchainImages( + new vk::Image[swapchainImageCount]); + result = device.getSwapchainImagesKHR(swapchain, &swapchainImageCount, + swapchainImages.get()); VERIFY(result == vk::Result::eSuccess); buffers.reset(new SwapchainBuffers[swapchainImageCount]); - for(uint32_t i = 0; i < swapchainImageCount; ++i) - { - auto const color_image_view = vk::ImageViewCreateInfo() - .setImage(swapchainImages[i]) - .setViewType(vk::ImageViewType::e2D) - .setFormat(format) - .setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1)); + for (uint32_t i = 0; i < swapchainImageCount; ++i) { + auto const color_image_view = + vk::ImageViewCreateInfo() + .setImage(swapchainImages[i]) + .setViewType(vk::ImageViewType::e2D) + .setFormat(format) + .setSubresourceRange(vk::ImageSubresourceRange( + vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1)); buffers[i].image = swapchainImages[i]; - result = device.createImageView(&color_image_view, nullptr, &buffers[i].view); + result = device.createImageView(&color_image_view, nullptr, + &buffers[i].view); VERIFY(result == vk::Result::eSuccess); // The draw loop will be expecting the presentable images to be in - // LAYOUT_PRESENT_SRC_KHR since that's how they're left at the end of every frame. - set_image_layout(buffers[i].image, vk::ImageAspectFlagBits::eColor, vk::ImageLayout::eUndefined, - vk::ImageLayout::ePresentSrcKHR, vk::AccessFlagBits()); + // LAYOUT_PRESENT_SRC_KHR since that's how they're left at the end + // of every frame. + set_image_layout(buffers[i].image, vk::ImageAspectFlagBits::eColor, + vk::ImageLayout::eUndefined, + vk::ImageLayout::ePresentSrcKHR, + vk::AccessFlagBits()); } } - void prepare_cube_data_buffer() - { + void prepare_cube_data_buffer() { mat4x4 VP; mat4x4_mul(VP, projection_matrix, view_matrix); @@ -1459,8 +1447,7 @@ struct Demo vktexcube_vs_uniform data; memcpy(data.mvp, MVP, sizeof(MVP)); // dumpMatrix("MVP", MVP) - for(int32_t i = 0; i < 12 * 3; i++) - { + for (int32_t i = 0; i < 12 * 3; i++) { data.position[i][0] = g_vertex_buffer_data[i * 3]; data.position[i][1] = g_vertex_buffer_data[i * 3 + 1]; data.position[i][2] = g_vertex_buffer_data[i * 3 + 2]; @@ -1471,10 +1458,12 @@ struct Demo data.attr[i][3] = 0; } - auto const buf_info = vk::BufferCreateInfo() - .setSize(sizeof(data)) - .setUsage(vk::BufferUsageFlagBits::eUniformBuffer); - auto result = device.createBuffer(&buf_info, nullptr, &uniform_data.buf); + auto const buf_info = + vk::BufferCreateInfo() + .setSize(sizeof(data)) + .setUsage(vk::BufferUsageFlagBits::eUniformBuffer); + auto result = + device.createBuffer(&buf_info, nullptr, &uniform_data.buf); VERIFY(result == vk::Result::eSuccess); vk::MemoryRequirements mem_reqs; @@ -1483,15 +1472,20 @@ struct Demo uniform_data.mem_alloc.setAllocationSize(mem_reqs.size); uniform_data.mem_alloc.setMemoryTypeIndex(0); - bool const pass = memory_type_from_properties(mem_reqs.memoryTypeBits, - vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, + bool const pass = memory_type_from_properties( + mem_reqs.memoryTypeBits, + vk::MemoryPropertyFlagBits::eHostVisible | + vk::MemoryPropertyFlagBits::eHostCoherent, &uniform_data.mem_alloc.memoryTypeIndex); VERIFY(pass); - result = device.allocateMemory(&uniform_data.mem_alloc, nullptr, &(uniform_data.mem)); + result = device.allocateMemory(&uniform_data.mem_alloc, nullptr, + &(uniform_data.mem)); VERIFY(result == vk::Result::eSuccess); - auto pData = device.mapMemory(uniform_data.mem, 0, uniform_data.mem_alloc.allocationSize, vk::MemoryMapFlags()); + auto pData = device.mapMemory(uniform_data.mem, 0, + uniform_data.mem_alloc.allocationSize, + vk::MemoryMapFlags()); VERIFY(pData.result == vk::Result::eSuccess); memcpy(pData.value, &data, sizeof data); @@ -1506,23 +1500,23 @@ struct Demo uniform_data.buffer_info.range = sizeof(data); } - void prepare_depth() - { + void prepare_depth() { depth.format = vk::Format::eD16Unorm; - auto const image = vk::ImageCreateInfo() - .setImageType(vk::ImageType::e2D) - .setFormat(depth.format) - .setExtent({(uint32_t)width, (uint32_t)height, 1}) - .setMipLevels(1) - .setArrayLayers(1) - .setSamples(vk::SampleCountFlagBits::e1) - .setTiling(vk::ImageTiling::eOptimal) - .setUsage(vk::ImageUsageFlagBits::eDepthStencilAttachment) - .setSharingMode(vk::SharingMode::eExclusive) - .setQueueFamilyIndexCount(0) - .setPQueueFamilyIndices(nullptr) - .setInitialLayout(vk::ImageLayout::eUndefined); + auto const image = + vk::ImageCreateInfo() + .setImageType(vk::ImageType::e2D) + .setFormat(depth.format) + .setExtent({(uint32_t)width, (uint32_t)height, 1}) + .setMipLevels(1) + .setArrayLayers(1) + .setSamples(vk::SampleCountFlagBits::e1) + .setTiling(vk::ImageTiling::eOptimal) + .setUsage(vk::ImageUsageFlagBits::eDepthStencilAttachment) + .setSharingMode(vk::SharingMode::eExclusive) + .setQueueFamilyIndexCount(0) + .setPQueueFamilyIndices(nullptr) + .setInitialLayout(vk::ImageLayout::eUndefined); auto result = device.createImage(&image, nullptr, &depth.image); VERIFY(result == vk::Result::eSuccess); @@ -1533,7 +1527,8 @@ struct Demo depth.mem_alloc.setAllocationSize(mem_reqs.size); depth.mem_alloc.setMemoryTypeIndex(0); - auto const pass = memory_type_from_properties(mem_reqs.memoryTypeBits, vk::MemoryPropertyFlagBits(0), + auto const pass = memory_type_from_properties( + mem_reqs.memoryTypeBits, vk::MemoryPropertyFlagBits(0), &depth.mem_alloc.memoryTypeIndex); VERIFY(pass); @@ -1543,22 +1538,23 @@ struct Demo result = device.bindImageMemory(depth.image, depth.mem, 0); VERIFY(result == vk::Result::eSuccess); - set_image_layout(depth.image, vk::ImageAspectFlagBits::eDepth, vk::ImageLayout::eUndefined, - vk::ImageLayout::eDepthStencilAttachmentOptimal, vk::AccessFlagBits()); + set_image_layout(depth.image, vk::ImageAspectFlagBits::eDepth, + vk::ImageLayout::eUndefined, + vk::ImageLayout::eDepthStencilAttachmentOptimal, + vk::AccessFlagBits()); auto const view = vk::ImageViewCreateInfo() - .setImage(depth.image) - .setViewType(vk::ImageViewType::e2D) - .setFormat(depth.format) - .setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eDepth, 0, 1, 0, 1)); + .setImage(depth.image) + .setViewType(vk::ImageViewType::e2D) + .setFormat(depth.format) + .setSubresourceRange(vk::ImageSubresourceRange( + vk::ImageAspectFlagBits::eDepth, 0, 1, 0, 1)); result = device.createImageView(&view, nullptr, &depth.view); VERIFY(result == vk::Result::eSuccess); } - void prepare_descriptor_layout() - { - vk::DescriptorSetLayoutBinding const layout_bindings[2] = - { + void prepare_descriptor_layout() { + vk::DescriptorSetLayoutBinding const layout_bindings[2] = { vk::DescriptorSetLayoutBinding() .setBinding(0) .setDescriptorType(vk::DescriptorType::eUniformBuffer) @@ -1570,57 +1566,54 @@ struct Demo .setDescriptorType(vk::DescriptorType::eCombinedImageSampler) .setDescriptorCount(texture_count) .setStageFlags(vk::ShaderStageFlagBits::eFragment) - .setPImmutableSamplers(nullptr) - }; + .setPImmutableSamplers(nullptr)}; - auto const descriptor_layout = vk::DescriptorSetLayoutCreateInfo() - .setBindingCount(2) - .setPBindings(layout_bindings); + auto const descriptor_layout = + vk::DescriptorSetLayoutCreateInfo().setBindingCount(2).setPBindings( + layout_bindings); - auto result = device.createDescriptorSetLayout(&descriptor_layout, nullptr, &desc_layout); + auto result = device.createDescriptorSetLayout(&descriptor_layout, + nullptr, &desc_layout); VERIFY(result == vk::Result::eSuccess); - auto const pPipelineLayoutCreateInfo = vk::PipelineLayoutCreateInfo() - .setSetLayoutCount(1) - .setPSetLayouts(&desc_layout); + auto const pPipelineLayoutCreateInfo = + vk::PipelineLayoutCreateInfo().setSetLayoutCount(1).setPSetLayouts( + &desc_layout); - result = device.createPipelineLayout(&pPipelineLayoutCreateInfo, nullptr, &pipeline_layout); + result = device.createPipelineLayout(&pPipelineLayoutCreateInfo, + nullptr, &pipeline_layout); VERIFY(result == vk::Result::eSuccess); } - void prepare_descriptor_pool() - { - vk::DescriptorPoolSize const poolSizes[2] = - { + void prepare_descriptor_pool() { + vk::DescriptorPoolSize const poolSizes[2] = { vk::DescriptorPoolSize() .setType(vk::DescriptorType::eUniformBuffer) .setDescriptorCount(1), vk::DescriptorPoolSize() .setType(vk::DescriptorType::eCombinedImageSampler) - .setDescriptorCount(texture_count) - }; + .setDescriptorCount(texture_count)}; auto const descriptor_pool = vk::DescriptorPoolCreateInfo() - .setMaxSets(1) - .setPoolSizeCount(2) - .setPPoolSizes(poolSizes); + .setMaxSets(1) + .setPoolSizeCount(2) + .setPPoolSizes(poolSizes); - auto result = device.createDescriptorPool(&descriptor_pool, nullptr, &desc_pool); + auto result = + device.createDescriptorPool(&descriptor_pool, nullptr, &desc_pool); VERIFY(result == vk::Result::eSuccess); } - void prepare_descriptor_set() - { + void prepare_descriptor_set() { auto const alloc_info = vk::DescriptorSetAllocateInfo() - .setDescriptorPool(desc_pool) - .setDescriptorSetCount(1) - .setPSetLayouts(&desc_layout); + .setDescriptorPool(desc_pool) + .setDescriptorSetCount(1) + .setPSetLayouts(&desc_layout); auto result = device.allocateDescriptorSets(&alloc_info, &desc_set); VERIFY(result == vk::Result::eSuccess); vk::DescriptorImageInfo tex_descs[texture_count]; - for(uint32_t i = 0; i < texture_count; i++) - { + for (uint32_t i = 0; i < texture_count; i++) { tex_descs[i].setSampler(textures[i].sampler); tex_descs[i].setImageView(textures[i].view); tex_descs[i].setImageLayout(vk::ImageLayout::eGeneral); @@ -1642,31 +1635,29 @@ struct Demo device.updateDescriptorSets(2, writes, 0, nullptr); } - void prepare_framebuffers() - { + void prepare_framebuffers() { vk::ImageView attachments[2]; attachments[1] = depth.view; auto const fb_info = vk::FramebufferCreateInfo() - .setRenderPass(render_pass) - .setAttachmentCount(2) - .setPAttachments(attachments) - .setWidth((uint32_t)width) - .setHeight((uint32_t)height) - .setLayers(1); + .setRenderPass(render_pass) + .setAttachmentCount(2) + .setPAttachments(attachments) + .setWidth((uint32_t)width) + .setHeight((uint32_t)height) + .setLayers(1); framebuffers.reset(new vk::Framebuffer[swapchainImageCount]); - for(uint32_t i = 0; i < swapchainImageCount; i++) - { + for (uint32_t i = 0; i < swapchainImageCount; i++) { attachments[0] = buffers[i].view; - auto const result = device.createFramebuffer(&fb_info, nullptr, &framebuffers[i]); + auto const result = + device.createFramebuffer(&fb_info, nullptr, &framebuffers[i]); VERIFY(result == vk::Result::eSuccess); } } - vk::ShaderModule prepare_fs() - { + vk::ShaderModule prepare_fs() { size_t size; void *fragShaderCode = read_spv("cube-frag.spv", &size); @@ -1677,14 +1668,13 @@ struct Demo return frag_shader_module; } - void prepare_pipeline() - { + void prepare_pipeline() { vk::PipelineCacheCreateInfo const pipelineCacheInfo; - auto result = device.createPipelineCache(&pipelineCacheInfo, nullptr, &pipelineCache); + auto result = device.createPipelineCache(&pipelineCacheInfo, nullptr, + &pipelineCache); VERIFY(result == vk::Result::eSuccess); - vk::PipelineShaderStageCreateInfo const shaderStageInfo[2] = - { + vk::PipelineShaderStageCreateInfo const shaderStageInfo[2] = { vk::PipelineShaderStageCreateInfo() .setStage(vk::ShaderStageFlagBits::eVertex) .setModule(prepare_vs()) @@ -1692,90 +1682,88 @@ struct Demo vk::PipelineShaderStageCreateInfo() .setStage(vk::ShaderStageFlagBits::eFragment) .setModule(prepare_fs()) - .setPName("main") - }; + .setPName("main")}; vk::PipelineVertexInputStateCreateInfo const vertexInputInfo; - auto const inputAssemblyInfo = vk::PipelineInputAssemblyStateCreateInfo() - .setTopology(vk::PrimitiveTopology::eTriangleList); + auto const inputAssemblyInfo = + vk::PipelineInputAssemblyStateCreateInfo().setTopology( + vk::PrimitiveTopology::eTriangleList); // TODO: Where are pViewports and pScissors set? auto const viewportInfo = vk::PipelineViewportStateCreateInfo() - .setViewportCount(1) - .setScissorCount(1); - - auto const rasterizationInfo = vk::PipelineRasterizationStateCreateInfo() - .setDepthClampEnable(VK_FALSE) - .setRasterizerDiscardEnable(VK_FALSE) - .setPolygonMode(vk::PolygonMode::eFill) - .setCullMode(vk::CullModeFlagBits::eBack) - .setFrontFace(vk::FrontFace::eCounterClockwise) - .setDepthBiasEnable(VK_FALSE) - .setLineWidth(1.0f); + .setViewportCount(1) + .setScissorCount(1); + + auto const rasterizationInfo = + vk::PipelineRasterizationStateCreateInfo() + .setDepthClampEnable(VK_FALSE) + .setRasterizerDiscardEnable(VK_FALSE) + .setPolygonMode(vk::PolygonMode::eFill) + .setCullMode(vk::CullModeFlagBits::eBack) + .setFrontFace(vk::FrontFace::eCounterClockwise) + .setDepthBiasEnable(VK_FALSE) + .setLineWidth(1.0f); auto const multisampleInfo = vk::PipelineMultisampleStateCreateInfo(); auto const stencilOp = vk::StencilOpState() - .setFailOp(vk::StencilOp::eKeep) - .setPassOp(vk::StencilOp::eKeep) - .setCompareOp(vk::CompareOp::eAlways); - - auto const depthStencilInfo = vk::PipelineDepthStencilStateCreateInfo() - .setDepthTestEnable(VK_TRUE) - .setDepthWriteEnable(VK_TRUE) - .setDepthCompareOp(vk::CompareOp::eLessOrEqual) - .setDepthBoundsTestEnable(VK_FALSE) - .setStencilTestEnable(VK_FALSE) - .setFront(stencilOp) - .setBack(stencilOp); - - vk::PipelineColorBlendAttachmentState const colorBlendAttachments[1] = - { - vk::PipelineColorBlendAttachmentState() - .setColorWriteMask(vk::ColorComponentFlagBits::eR| vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB - | vk::ColorComponentFlagBits::eA) - }; + .setFailOp(vk::StencilOp::eKeep) + .setPassOp(vk::StencilOp::eKeep) + .setCompareOp(vk::CompareOp::eAlways); + + auto const depthStencilInfo = + vk::PipelineDepthStencilStateCreateInfo() + .setDepthTestEnable(VK_TRUE) + .setDepthWriteEnable(VK_TRUE) + .setDepthCompareOp(vk::CompareOp::eLessOrEqual) + .setDepthBoundsTestEnable(VK_FALSE) + .setStencilTestEnable(VK_FALSE) + .setFront(stencilOp) + .setBack(stencilOp); + + vk::PipelineColorBlendAttachmentState const colorBlendAttachments[1] = { + vk::PipelineColorBlendAttachmentState().setColorWriteMask( + vk::ColorComponentFlagBits::eR | + vk::ColorComponentFlagBits::eG | + vk::ColorComponentFlagBits::eB | + vk::ColorComponentFlagBits::eA)}; auto const colorBlendInfo = vk::PipelineColorBlendStateCreateInfo() - .setAttachmentCount(1) - .setPAttachments(colorBlendAttachments); + .setAttachmentCount(1) + .setPAttachments(colorBlendAttachments); - vk::DynamicState const dynamicStates[2] = - { - vk::DynamicState::eViewport, - vk::DynamicState::eScissor - }; + vk::DynamicState const dynamicStates[2] = {vk::DynamicState::eViewport, + vk::DynamicState::eScissor}; auto const dynamicStateInfo = vk::PipelineDynamicStateCreateInfo() - .setPDynamicStates(dynamicStates) - .setDynamicStateCount(2); + .setPDynamicStates(dynamicStates) + .setDynamicStateCount(2); auto const pipeline = vk::GraphicsPipelineCreateInfo() - .setStageCount(2) - .setPStages(shaderStageInfo) - .setPVertexInputState(&vertexInputInfo) - .setPInputAssemblyState(&inputAssemblyInfo) - .setPViewportState(&viewportInfo) - .setPRasterizationState(&rasterizationInfo) - .setPMultisampleState(&multisampleInfo) - .setPDepthStencilState(&depthStencilInfo) - .setPColorBlendState(&colorBlendInfo) - .setPDynamicState(&dynamicStateInfo) - .setLayout(pipeline_layout) - .setRenderPass(render_pass); - - result = device.createGraphicsPipelines(pipelineCache, 1, &pipeline, nullptr, &this->pipeline); + .setStageCount(2) + .setPStages(shaderStageInfo) + .setPVertexInputState(&vertexInputInfo) + .setPInputAssemblyState(&inputAssemblyInfo) + .setPViewportState(&viewportInfo) + .setPRasterizationState(&rasterizationInfo) + .setPMultisampleState(&multisampleInfo) + .setPDepthStencilState(&depthStencilInfo) + .setPColorBlendState(&colorBlendInfo) + .setPDynamicState(&dynamicStateInfo) + .setLayout(pipeline_layout) + .setRenderPass(render_pass); + + result = device.createGraphicsPipelines(pipelineCache, 1, &pipeline, + nullptr, &this->pipeline); VERIFY(result == vk::Result::eSuccess); device.destroyShaderModule(frag_shader_module, nullptr); device.destroyShaderModule(vert_shader_module, nullptr); } - void prepare_render_pass() - { - const vk::AttachmentDescription attachments[2] = - { + void prepare_render_pass() { + const vk::AttachmentDescription attachments[2] = { vk::AttachmentDescription() .setFlags(vk::AttachmentDescriptionFlagBits::eMayAlias) .setFormat(format) @@ -1794,82 +1782,86 @@ struct Demo .setStoreOp(vk::AttachmentStoreOp::eDontCare) .setStencilLoadOp(vk::AttachmentLoadOp::eDontCare) .setStencilStoreOp(vk::AttachmentStoreOp::eDontCare) - .setInitialLayout(vk::ImageLayout::eDepthStencilAttachmentOptimal) - .setFinalLayout(vk::ImageLayout::eDepthStencilAttachmentOptimal) - }; - - auto const color_reference = vk::AttachmentReference() - .setAttachment(0) - .setLayout(vk::ImageLayout::eColorAttachmentOptimal); - - auto const depth_reference = vk::AttachmentReference() - .setAttachment(1) - .setLayout(vk::ImageLayout::eDepthStencilAttachmentOptimal); - - auto const subpass = vk::SubpassDescription() - .setPipelineBindPoint(vk::PipelineBindPoint::eGraphics) - .setInputAttachmentCount(0) - .setPInputAttachments(nullptr) - .setColorAttachmentCount(1) - .setPColorAttachments(&color_reference) - .setPResolveAttachments(nullptr) - .setPDepthStencilAttachment(&depth_reference) - .setPreserveAttachmentCount(0) - .setPPreserveAttachments(nullptr); + .setInitialLayout( + vk::ImageLayout::eDepthStencilAttachmentOptimal) + .setFinalLayout( + vk::ImageLayout::eDepthStencilAttachmentOptimal)}; + + auto const color_reference = + vk::AttachmentReference().setAttachment(0).setLayout( + vk::ImageLayout::eColorAttachmentOptimal); + + auto const depth_reference = + vk::AttachmentReference().setAttachment(1).setLayout( + vk::ImageLayout::eDepthStencilAttachmentOptimal); + + auto const subpass = + vk::SubpassDescription() + .setPipelineBindPoint(vk::PipelineBindPoint::eGraphics) + .setInputAttachmentCount(0) + .setPInputAttachments(nullptr) + .setColorAttachmentCount(1) + .setPColorAttachments(&color_reference) + .setPResolveAttachments(nullptr) + .setPDepthStencilAttachment(&depth_reference) + .setPreserveAttachmentCount(0) + .setPPreserveAttachments(nullptr); auto const rp_info = vk::RenderPassCreateInfo() - .setAttachmentCount(2) - .setPAttachments(attachments) - .setSubpassCount(1) - .setPSubpasses(&subpass) - .setDependencyCount(0) - .setPDependencies(nullptr); + .setAttachmentCount(2) + .setPAttachments(attachments) + .setSubpassCount(1) + .setPSubpasses(&subpass) + .setDependencyCount(0) + .setPDependencies(nullptr); auto result = device.createRenderPass(&rp_info, nullptr, &render_pass); VERIFY(result == vk::Result::eSuccess); } - vk::ShaderModule prepare_shader_module(const void *code, size_t size) - { - auto const moduleCreateInfo = vk::ShaderModuleCreateInfo() - .setCodeSize(size) - .setPCode((uint32_t const*)code); + vk::ShaderModule prepare_shader_module(const void *code, size_t size) { + auto const moduleCreateInfo = + vk::ShaderModuleCreateInfo().setCodeSize(size).setPCode( + (uint32_t const *)code); vk::ShaderModule module; - auto result = device.createShaderModule(&moduleCreateInfo, nullptr, &module); + auto result = + device.createShaderModule(&moduleCreateInfo, nullptr, &module); VERIFY(result == vk::Result::eSuccess); return module; } - void prepare_texture_image(const char *filename, texture_object *tex_obj, vk::ImageTiling tiling, - vk::ImageUsageFlags usage, vk::MemoryPropertyFlags required_props) - { + void prepare_texture_image(const char *filename, texture_object *tex_obj, + vk::ImageTiling tiling, + vk::ImageUsageFlags usage, + vk::MemoryPropertyFlags required_props) { int32_t tex_width; int32_t tex_height; - if(!loadTexture(filename, nullptr, nullptr, &tex_width, &tex_height)) - { + if (!loadTexture(filename, nullptr, nullptr, &tex_width, &tex_height)) { ERR_EXIT("Failed to load textures", "Load Texture Failure"); } tex_obj->tex_width = tex_width; tex_obj->tex_height = tex_height; - auto const image_create_info = vk::ImageCreateInfo() - .setImageType(vk::ImageType::e2D) - .setFormat(vk::Format::eR8G8B8A8Unorm) - .setExtent({(uint32_t)tex_width, (uint32_t)tex_height, 1}) - .setMipLevels(1) - .setArrayLayers(1) - .setSamples(vk::SampleCountFlagBits::e1) - .setTiling(tiling) - .setUsage(usage) - .setSharingMode(vk::SharingMode::eExclusive) - .setQueueFamilyIndexCount(0) - .setPQueueFamilyIndices(nullptr) - .setInitialLayout(vk::ImageLayout::ePreinitialized); - - auto result = device.createImage(&image_create_info, nullptr, &tex_obj->image); + auto const image_create_info = + vk::ImageCreateInfo() + .setImageType(vk::ImageType::e2D) + .setFormat(vk::Format::eR8G8B8A8Unorm) + .setExtent({(uint32_t)tex_width, (uint32_t)tex_height, 1}) + .setMipLevels(1) + .setArrayLayers(1) + .setSamples(vk::SampleCountFlagBits::e1) + .setTiling(tiling) + .setUsage(usage) + .setSharingMode(vk::SharingMode::eExclusive) + .setQueueFamilyIndexCount(0) + .setPQueueFamilyIndices(nullptr) + .setInitialLayout(vk::ImageLayout::ePreinitialized); + + auto result = + device.createImage(&image_create_info, nullptr, &tex_obj->image); VERIFY(result == vk::Result::eSuccess); vk::MemoryRequirements mem_reqs; @@ -1878,29 +1870,33 @@ struct Demo tex_obj->mem_alloc.setAllocationSize(mem_reqs.size); tex_obj->mem_alloc.setMemoryTypeIndex(0); - auto pass = memory_type_from_properties(mem_reqs.memoryTypeBits, required_props, &tex_obj->mem_alloc.memoryTypeIndex); + auto pass = + memory_type_from_properties(mem_reqs.memoryTypeBits, required_props, + &tex_obj->mem_alloc.memoryTypeIndex); VERIFY(pass == true); - result = device.allocateMemory(&tex_obj->mem_alloc, nullptr, &(tex_obj->mem)); + result = device.allocateMemory(&tex_obj->mem_alloc, nullptr, + &(tex_obj->mem)); VERIFY(result == vk::Result::eSuccess); result = device.bindImageMemory(tex_obj->image, tex_obj->mem, 0); VERIFY(result == vk::Result::eSuccess); - if(required_props & vk::MemoryPropertyFlagBits::eHostVisible) - { - auto const subres = vk::ImageSubresource() - .setAspectMask(vk::ImageAspectFlagBits::eColor) - .setMipLevel(0) - .setArrayLayer(0); + if (required_props & vk::MemoryPropertyFlagBits::eHostVisible) { + auto const subres = + vk::ImageSubresource() + .setAspectMask(vk::ImageAspectFlagBits::eColor) + .setMipLevel(0) + .setArrayLayer(0); vk::SubresourceLayout layout; device.getImageSubresourceLayout(tex_obj->image, &subres, &layout); - auto data = device.mapMemory(tex_obj->mem, 0, tex_obj->mem_alloc.allocationSize); + auto data = device.mapMemory(tex_obj->mem, 0, + tex_obj->mem_alloc.allocationSize); VERIFY(data.result == vk::Result::eSuccess); - if(!loadTexture(filename, (uint8_t*)data.value, &layout, &tex_width, &tex_height)) - { + if (!loadTexture(filename, (uint8_t *)data.value, &layout, + &tex_width, &tex_height)) { fprintf(stderr, "Error loading texture: %s\n", filename); } @@ -1908,107 +1904,127 @@ struct Demo } tex_obj->imageLayout = vk::ImageLayout::eShaderReadOnlyOptimal; - set_image_layout(tex_obj->image, vk::ImageAspectFlagBits::eColor, vk::ImageLayout::ePreinitialized, tex_obj->imageLayout, - vk::AccessFlagBits::eHostWrite); + set_image_layout(tex_obj->image, vk::ImageAspectFlagBits::eColor, + vk::ImageLayout::ePreinitialized, tex_obj->imageLayout, + vk::AccessFlagBits::eHostWrite); } - void prepare_textures() - { + void prepare_textures() { vk::Format const tex_format = vk::Format::eR8G8B8A8Unorm; vk::FormatProperties props; gpu.getFormatProperties(tex_format, &props); - for(uint32_t i = 0; i < texture_count; i++) - { - if((props.linearTilingFeatures & vk::FormatFeatureFlagBits::eSampledImage) && !use_staging_buffer) - { + for (uint32_t i = 0; i < texture_count; i++) { + if ((props.linearTilingFeatures & + vk::FormatFeatureFlagBits::eSampledImage) && + !use_staging_buffer) { /* Device can texture using linear textures */ - prepare_texture_image(tex_files[i], &textures[i], vk::ImageTiling::eLinear, + prepare_texture_image( + tex_files[i], &textures[i], vk::ImageTiling::eLinear, vk::ImageUsageFlagBits::eSampled, - vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent); - } - else if(props.optimalTilingFeatures & vk::FormatFeatureFlagBits::eSampledImage) - { - /* Must use staging buffer to copy linear texture to optimized */ + vk::MemoryPropertyFlagBits::eHostVisible | + vk::MemoryPropertyFlagBits::eHostCoherent); + } else if (props.optimalTilingFeatures & + vk::FormatFeatureFlagBits::eSampledImage) { + /* Must use staging buffer to copy linear texture to optimized + */ texture_object staging_texture; - prepare_texture_image(tex_files[i], &staging_texture, vk::ImageTiling::eLinear, + prepare_texture_image( + tex_files[i], &staging_texture, vk::ImageTiling::eLinear, vk::ImageUsageFlagBits::eTransferSrc, - vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent); - - prepare_texture_image(tex_files[i], &textures[i], vk::ImageTiling::eOptimal, - vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled, - vk::MemoryPropertyFlagBits::eDeviceLocal); - - set_image_layout(staging_texture.image, vk::ImageAspectFlagBits::eColor, staging_texture.imageLayout, + vk::MemoryPropertyFlagBits::eHostVisible | + vk::MemoryPropertyFlagBits::eHostCoherent); + + prepare_texture_image(tex_files[i], &textures[i], + vk::ImageTiling::eOptimal, + vk::ImageUsageFlagBits::eTransferDst | + vk::ImageUsageFlagBits::eSampled, + vk::MemoryPropertyFlagBits::eDeviceLocal); + + set_image_layout( + staging_texture.image, vk::ImageAspectFlagBits::eColor, + staging_texture.imageLayout, vk::ImageLayout::eTransferSrcOptimal, vk::AccessFlags()); - set_image_layout(textures[i].image, vk::ImageAspectFlagBits::eColor, textures[i].imageLayout, + set_image_layout( + textures[i].image, vk::ImageAspectFlagBits::eColor, + textures[i].imageLayout, vk::ImageLayout::eTransferDstOptimal, vk::AccessFlags()); - auto const subresource = vk::ImageSubresourceLayers() - .setAspectMask(vk::ImageAspectFlagBits::eColor) - .setMipLevel(0) - .setBaseArrayLayer(0) - .setLayerCount(1); - - auto const copy_region = vk::ImageCopy() - .setSrcSubresource(subresource) - .setSrcOffset({0, 0, 0}) - .setDstSubresource(subresource) - .setDstOffset({0, 0, 0}) - .setExtent({(uint32_t)staging_texture.tex_width, (uint32_t)staging_texture.tex_height, 1}); - - cmd.copyImage(staging_texture.image, vk::ImageLayout::eTransferSrcOptimal, textures[i].image, - vk::ImageLayout::eTransferDstOptimal, 1, ©_region); - - set_image_layout(textures[i].image, vk::ImageAspectFlagBits::eColor, vk::ImageLayout::eTransferDstOptimal, - textures[i].imageLayout, vk::AccessFlags()); + auto const subresource = + vk::ImageSubresourceLayers() + .setAspectMask(vk::ImageAspectFlagBits::eColor) + .setMipLevel(0) + .setBaseArrayLayer(0) + .setLayerCount(1); + + auto const copy_region = + vk::ImageCopy() + .setSrcSubresource(subresource) + .setSrcOffset({0, 0, 0}) + .setDstSubresource(subresource) + .setDstOffset({0, 0, 0}) + .setExtent({(uint32_t)staging_texture.tex_width, + (uint32_t)staging_texture.tex_height, 1}); + + cmd.copyImage( + staging_texture.image, vk::ImageLayout::eTransferSrcOptimal, + textures[i].image, vk::ImageLayout::eTransferDstOptimal, 1, + ©_region); + + set_image_layout(textures[i].image, + vk::ImageAspectFlagBits::eColor, + vk::ImageLayout::eTransferDstOptimal, + textures[i].imageLayout, vk::AccessFlags()); flush_init_cmd(); destroy_texture_image(&staging_texture); - } - else - { - assert(!"No support for R8G8B8A8_UNORM as texture image format"); + } else { + assert( + !"No support for R8G8B8A8_UNORM as texture image format"); } - auto const samplerInfo = vk::SamplerCreateInfo() - .setMagFilter(vk::Filter::eNearest) - .setMinFilter(vk::Filter::eNearest) - .setMipmapMode(vk::SamplerMipmapMode::eNearest) - .setAddressModeU(vk::SamplerAddressMode::eClampToEdge) - .setAddressModeV(vk::SamplerAddressMode::eClampToEdge) - .setAddressModeW(vk::SamplerAddressMode::eClampToEdge) - .setMipLodBias(0.0f) - .setAnisotropyEnable(VK_FALSE) - .setMaxAnisotropy(1) - .setCompareEnable(VK_FALSE) - .setCompareOp(vk::CompareOp::eNever) - .setMinLod(0.0f) - .setMaxLod(0.0f) - .setBorderColor(vk::BorderColor::eFloatOpaqueWhite) - .setUnnormalizedCoordinates(VK_FALSE); - - auto result = device.createSampler(&samplerInfo, nullptr, &textures[i].sampler); + auto const samplerInfo = + vk::SamplerCreateInfo() + .setMagFilter(vk::Filter::eNearest) + .setMinFilter(vk::Filter::eNearest) + .setMipmapMode(vk::SamplerMipmapMode::eNearest) + .setAddressModeU(vk::SamplerAddressMode::eClampToEdge) + .setAddressModeV(vk::SamplerAddressMode::eClampToEdge) + .setAddressModeW(vk::SamplerAddressMode::eClampToEdge) + .setMipLodBias(0.0f) + .setAnisotropyEnable(VK_FALSE) + .setMaxAnisotropy(1) + .setCompareEnable(VK_FALSE) + .setCompareOp(vk::CompareOp::eNever) + .setMinLod(0.0f) + .setMaxLod(0.0f) + .setBorderColor(vk::BorderColor::eFloatOpaqueWhite) + .setUnnormalizedCoordinates(VK_FALSE); + + auto result = device.createSampler(&samplerInfo, nullptr, + &textures[i].sampler); VERIFY(result == vk::Result::eSuccess); - auto const viewInfo = vk::ImageViewCreateInfo() - .setImage(textures[i].image) - .setViewType(vk::ImageViewType::e2D) - .setFormat(tex_format) - .setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1)); + auto const viewInfo = + vk::ImageViewCreateInfo() + .setImage(textures[i].image) + .setViewType(vk::ImageViewType::e2D) + .setFormat(tex_format) + .setSubresourceRange(vk::ImageSubresourceRange( + vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1)); - result = device.createImageView(&viewInfo, nullptr, &textures[i].view); + result = + device.createImageView(&viewInfo, nullptr, &textures[i].view); VERIFY(result == vk::Result::eSuccess); } } - vk::ShaderModule prepare_vs() - { + vk::ShaderModule prepare_vs() { size_t size; - void* vertShaderCode = read_spv("cube-vert.spv", &size); + void *vertShaderCode = read_spv("cube-vert.spv", &size); vert_shader_module = prepare_shader_module(vertShaderCode, size); @@ -2017,11 +2033,9 @@ struct Demo return vert_shader_module; } - char *read_spv(const char *filename, size_t *psize) - { + char *read_spv(const char *filename, size_t *psize) { FILE *fp = fopen(filename, "rb"); - if(!fp) - { + if (!fp) { return nullptr; } @@ -2038,20 +2052,19 @@ struct Demo fclose(fp); - return (char*)shader_code; + return (char *)shader_code; } - void resize() - { + void resize() { uint32_t i; // Don't react to resize until after first initialization. - if(!prepared) - { + if (!prepared) { return; } - // In order to properly resize the window, we must re-create the swapchain + // In order to properly resize the window, we must re-create the + // swapchain // AND redo the command buffers, etc. // // First, perform part of the cleanup() function: @@ -2059,8 +2072,7 @@ struct Demo auto result = device.waitIdle(); VERIFY(result == vk::Result::eSuccess); - for(i = 0; i < swapchainImageCount; i++) - { + for (i = 0; i < swapchainImageCount; i++) { device.destroyFramebuffer(framebuffers[i], nullptr); } @@ -2072,8 +2084,7 @@ struct Demo device.destroyPipelineLayout(pipeline_layout, nullptr); device.destroyDescriptorSetLayout(desc_layout, nullptr); - for(i = 0; i < texture_count; i++) - { + for (i = 0; i < texture_count; i++) { device.destroyImageView(textures[i].view, nullptr); device.destroyImage(textures[i].image, nullptr); device.freeMemory(textures[i].mem, nullptr); @@ -2087,100 +2098,103 @@ struct Demo device.destroyBuffer(uniform_data.buf, nullptr); device.freeMemory(uniform_data.mem, nullptr); - for(i = 0; i < swapchainImageCount; i++) - { + for (i = 0; i < swapchainImageCount; i++) { device.destroyImageView(buffers[i].view, nullptr); device.freeCommandBuffers(cmd_pool, 1, &buffers[i].cmd); } device.destroyCommandPool(cmd_pool, nullptr); - if(separate_present_queue) - { + if (separate_present_queue) { device.destroyCommandPool(present_cmd_pool, nullptr); } - // Second, re-perform the prepare() function, which will re-create the swapchain. + // Second, re-perform the prepare() function, which will re-create the + // swapchain. prepare(); } - void set_image_layout(vk::Image image, vk::ImageAspectFlags aspectMask, vk::ImageLayout oldLayout, - vk::ImageLayout newLayout, vk::AccessFlags srcAccessMask) - { - if(!cmd) - { + void set_image_layout(vk::Image image, vk::ImageAspectFlags aspectMask, + vk::ImageLayout oldLayout, vk::ImageLayout newLayout, + vk::AccessFlags srcAccessMask) { + if (!cmd) { auto const cmd = vk::CommandBufferAllocateInfo() - .setCommandPool(cmd_pool) - .setLevel(vk::CommandBufferLevel::ePrimary) - .setCommandBufferCount(1); + .setCommandPool(cmd_pool) + .setLevel(vk::CommandBufferLevel::ePrimary) + .setCommandBufferCount(1); auto result = device.allocateCommandBuffers(&cmd, &this->cmd); VERIFY(result == vk::Result::eSuccess); - auto const cmd_buf_info = vk::CommandBufferBeginInfo() - .setPInheritanceInfo(nullptr); + auto const cmd_buf_info = + vk::CommandBufferBeginInfo().setPInheritanceInfo(nullptr); result = this->cmd.begin(&cmd_buf_info); VERIFY(result == vk::Result::eSuccess); } - auto DstAccessMask = [](vk::ImageLayout const& layout) - { + auto DstAccessMask = [](vk::ImageLayout const &layout) { vk::AccessFlags flags; - switch(layout) - { - case vk::ImageLayout::eTransferDstOptimal: - // Make sure anything that was copying from this image has completed - flags = vk::AccessFlagBits::eTransferRead; - break; - case vk::ImageLayout::eColorAttachmentOptimal: - flags = vk::AccessFlagBits::eColorAttachmentWrite; - break; - case vk::ImageLayout::eDepthStencilAttachmentOptimal: - flags = vk::AccessFlagBits::eDepthStencilAttachmentWrite; - break; - case vk::ImageLayout::eShaderReadOnlyOptimal: - // Make sure any Copy or CPU writes to image are flushed - flags = vk::AccessFlagBits::eShaderRead | vk::AccessFlagBits::eInputAttachmentRead; - break; - case vk::ImageLayout::ePresentSrcKHR: - flags = vk::AccessFlagBits::eMemoryRead; - break; - default: - break; + switch (layout) { + case vk::ImageLayout::eTransferDstOptimal: + // Make sure anything that was copying from this image has + // completed + flags = vk::AccessFlagBits::eTransferRead; + break; + case vk::ImageLayout::eColorAttachmentOptimal: + flags = vk::AccessFlagBits::eColorAttachmentWrite; + break; + case vk::ImageLayout::eDepthStencilAttachmentOptimal: + flags = vk::AccessFlagBits::eDepthStencilAttachmentWrite; + break; + case vk::ImageLayout::eShaderReadOnlyOptimal: + // Make sure any Copy or CPU writes to image are flushed + flags = vk::AccessFlagBits::eShaderRead | + vk::AccessFlagBits::eInputAttachmentRead; + break; + case vk::ImageLayout::ePresentSrcKHR: + flags = vk::AccessFlagBits::eMemoryRead; + break; + default: + break; } return flags; }; auto const barrier = vk::ImageMemoryBarrier() - .setSrcAccessMask(srcAccessMask) - .setDstAccessMask(DstAccessMask(newLayout)) - .setOldLayout(oldLayout) - .setNewLayout(newLayout) - .setSrcQueueFamilyIndex(0) - .setDstQueueFamilyIndex(0) - .setImage(image) - .setSubresourceRange(vk::ImageSubresourceRange(aspectMask, 0, 1, 0, 1)); - - cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eTopOfPipe, vk::DependencyFlagBits(), - 0, nullptr, 0, nullptr, 1, &barrier); + .setSrcAccessMask(srcAccessMask) + .setDstAccessMask(DstAccessMask(newLayout)) + .setOldLayout(oldLayout) + .setNewLayout(newLayout) + .setSrcQueueFamilyIndex(0) + .setDstQueueFamilyIndex(0) + .setImage(image) + .setSubresourceRange(vk::ImageSubresourceRange( + aspectMask, 0, 1, 0, 1)); + + cmd.pipelineBarrier(vk::PipelineStageFlagBits::eTopOfPipe, + vk::PipelineStageFlagBits::eTopOfPipe, + vk::DependencyFlagBits(), 0, nullptr, 0, nullptr, 1, + &barrier); } - void update_data_buffer() - { + void update_data_buffer() { mat4x4 VP; mat4x4_mul(VP, projection_matrix, view_matrix); // Rotate 22.5 degrees around the Y axis mat4x4 Model; mat4x4_dup(Model, model_matrix); - mat4x4_rotate(model_matrix, Model, 0.0f, 1.0f, 0.0f, (float)degreesToRadians(spin_angle)); + mat4x4_rotate(model_matrix, Model, 0.0f, 1.0f, 0.0f, + (float)degreesToRadians(spin_angle)); mat4x4 MVP; mat4x4_mul(MVP, VP, model_matrix); - auto data = device.mapMemory(uniform_data.mem, 0, uniform_data.mem_alloc.allocationSize, vk::MemoryMapFlags()); + auto data = device.mapMemory(uniform_data.mem, 0, + uniform_data.mem_alloc.allocationSize, + vk::MemoryMapFlags()); VERIFY(data.result == vk::Result::eSuccess); memcpy(data.value, (const void *)&MVP[0][0], sizeof(MVP)); @@ -2188,54 +2202,46 @@ struct Demo device.unmapMemory(uniform_data.mem); } - bool loadTexture(const char *filename, uint8_t *rgba_data, vk::SubresourceLayout *layout, int32_t *width, int32_t *height) - { + bool loadTexture(const char *filename, uint8_t *rgba_data, + vk::SubresourceLayout *layout, int32_t *width, + int32_t *height) { FILE *fPtr = fopen(filename, "rb"); - if(!fPtr) - { + if (!fPtr) { return false; } char header[256]; - char* cPtr = fgets(header, 256, fPtr); // P6 - if(cPtr == nullptr || strncmp(header, "P6\n", 3)) - { + char *cPtr = fgets(header, 256, fPtr); // P6 + if (cPtr == nullptr || strncmp(header, "P6\n", 3)) { fclose(fPtr); return false; } - do - { + do { cPtr = fgets(header, 256, fPtr); - if(cPtr == nullptr) - { + if (cPtr == nullptr) { fclose(fPtr); return false; } - } - while(!strncmp(header, "#", 1)); + } while (!strncmp(header, "#", 1)); sscanf(header, "%u %u", width, height); - if(rgba_data == nullptr) - { + if (rgba_data == nullptr) { fclose(fPtr); return true; } - char* result = fgets(header, 256, fPtr); // Format + char *result = fgets(header, 256, fPtr); // Format VERIFY(result != nullptr); - if(cPtr == nullptr || strncmp(header, "255\n", 3)) - { + if (cPtr == nullptr || strncmp(header, "255\n", 3)) { fclose(fPtr); return false; } - for(int y = 0; y < *height; y++) - { + for (int y = 0; y < *height; y++) { uint8_t *rowPtr = rgba_data; - for(int x = 0; x < *width; x++) - { + for (int x = 0; x < *width; x++) { size_t s = fread(rowPtr, 3, 1, fPtr); (void)s; rowPtr[3] = 255; /* Alpha of 1 */ @@ -2249,16 +2255,15 @@ struct Demo return true; } - bool memory_type_from_properties(uint32_t typeBits, vk::MemoryPropertyFlags requirements_mask, uint32_t *typeIndex) - { + bool memory_type_from_properties(uint32_t typeBits, + vk::MemoryPropertyFlags requirements_mask, + uint32_t *typeIndex) { // Search memtypes to find first index with those properties - for(uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) - { - if((typeBits & 1) == 1) - { + for (uint32_t i = 0; i < VK_MAX_MEMORY_TYPES; i++) { + if ((typeBits & 1) == 1) { // Type is available, does it match user properties? - if((memory_properties.memoryTypes[i].propertyFlags & requirements_mask) == requirements_mask) - { + if ((memory_properties.memoryTypes[i].propertyFlags & + requirements_mask) == requirements_mask) { *typeIndex = i; return true; } @@ -2271,10 +2276,8 @@ struct Demo } #if defined(VK_USE_PLATFORM_WIN32_KHR) - void run() - { - if(!prepared) - { + void run() { + if (!prepared) { return; } @@ -2282,14 +2285,12 @@ struct Demo draw(); curFrame++; - if(frameCount != INT_MAX && curFrame == frameCount) - { + if (frameCount != INT_MAX && curFrame == frameCount) { PostQuitMessage(validation_error); } } - void create_window() - { + void create_window() { WNDCLASSEX win_class; // Initialize the window class structure: @@ -2307,8 +2308,7 @@ struct Demo win_class.hIconSm = LoadIcon(nullptr, IDI_WINLOGO); // Register window class: - if(!RegisterClassEx(&win_class)) - { + if (!RegisterClassEx(&win_class)) { // It didn't work, so try to give a useful error: printf("Unexpected error trying to start the application!\n"); fflush(stdout); @@ -2319,53 +2319,58 @@ struct Demo RECT wr = {0, 0, width, height}; AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE); window = CreateWindowEx(0, - name, // class name - name, // app name - WS_OVERLAPPEDWINDOW | // window style - WS_VISIBLE | WS_SYSMENU, - 100, 100, // x/y coords - wr.right - wr.left, // width - wr.bottom - wr.top, // height - nullptr, // handle to parent - nullptr, // handle to menu - connection, // hInstance - nullptr); // no extra parameters - - if(!window) - { + name, // class name + name, // app name + WS_OVERLAPPEDWINDOW | // window style + WS_VISIBLE | WS_SYSMENU, + 100, 100, // x/y coords + wr.right - wr.left, // width + wr.bottom - wr.top, // height + nullptr, // handle to parent + nullptr, // handle to menu + connection, // hInstance + nullptr); // no extra parameters + + if (!window) { // It didn't work, so try to give a useful error: printf("Cannot create a window in which to draw!\n"); fflush(stdout); exit(1); } - // Window client area size must be at least 1 pixel high, to prevent crash. + // Window client area size must be at least 1 pixel high, to prevent + // crash. minsize.x = GetSystemMetrics(SM_CXMINTRACK); - minsize.y = GetSystemMetrics(SM_CYMINTRACK)+1; + minsize.y = GetSystemMetrics(SM_CYMINTRACK) + 1; } #elif defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR) #if defined(VK_USE_PLATFORM_XLIB_KHR) - void create_xlib_window() - { + void create_xlib_window() { display = XOpenDisplay(nullptr); long visualMask = VisualScreenMask; int numberOfVisuals; - XVisualInfo vInfoTemplate={}; + XVisualInfo vInfoTemplate = {}; vInfoTemplate.screen = DefaultScreen(display); - XVisualInfo *visualInfo = XGetVisualInfo(display, visualMask, &vInfoTemplate, &numberOfVisuals); + XVisualInfo *visualInfo = XGetVisualInfo( + display, visualMask, &vInfoTemplate, &numberOfVisuals); - Colormap colormap = XCreateColormap(display, RootWindow(display, vInfoTemplate.screen), visualInfo->visual, AllocNone); + Colormap colormap = + XCreateColormap(display, RootWindow(display, vInfoTemplate.screen), + visualInfo->visual, AllocNone); - XSetWindowAttributes windowAttributes={}; + XSetWindowAttributes windowAttributes = {}; windowAttributes.colormap = colormap; windowAttributes.background_pixel = 0xFFFFFFFF; windowAttributes.border_pixel = 0; - windowAttributes.event_mask = KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask; + windowAttributes.event_mask = + KeyPressMask | KeyReleaseMask | StructureNotifyMask | ExposureMask; - xlib_window = XCreateWindow(display, RootWindow(display, vInfoTemplate.screen), 0, 0, width, height, - 0, visualInfo->depth, InputOutput, visualInfo->visual, CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, + xlib_window = XCreateWindow( + display, RootWindow(display, vInfoTemplate.screen), 0, 0, width, + height, 0, visualInfo->depth, InputOutput, visualInfo->visual, + CWBackPixel | CWBorderPixel | CWEventMask | CWColormap, &windowAttributes); XSelectInput(display, xlib_window, ExposureMask | KeyPressMask); @@ -2374,19 +2379,15 @@ struct Demo xlib_wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False); } - void handle_xlib_event(const XEvent *event) - { - switch(event->type) - { + void handle_xlib_event(const XEvent *event) { + switch (event->type) { case ClientMessage: - if((Atom)event->xclient.data.l[0] == xlib_wm_delete_window) - { + if ((Atom)event->xclient.data.l[0] == xlib_wm_delete_window) { quit = true; } break; case KeyPress: - switch (event->xkey.keycode) - { + switch (event->xkey.keycode) { case 0x9: // Escape quit = true; break; @@ -2402,8 +2403,8 @@ struct Demo } break; case ConfigureNotify: - if(((int32_t)width != event->xconfigure.width) || ((int32_t)height != event->xconfigure.height)) - { + if (((int32_t)width != event->xconfigure.width) || + ((int32_t)height != event->xconfigure.height)) { width = event->xconfigure.width; height = event->xconfigure.height; resize(); @@ -2414,21 +2415,15 @@ struct Demo } } - void run_xlib() - { - while(!quit) - { + void run_xlib() { + while (!quit) { XEvent event; - if(pause) - { + if (pause) { XNextEvent(display, &event); handle_xlib_event(&event); - } - else - { - while(XPending(display) > 0) - { + } else { + while (XPending(display) > 0) { XNextEvent(display, &event); handle_xlib_event(&event); } @@ -2438,8 +2433,7 @@ struct Demo draw(); curFrame++; - if(frameCount != UINT32_MAX && curFrame == frameCount) - { + if (frameCount != UINT32_MAX && curFrame == frameCount) { quit = true; } } @@ -2448,26 +2442,23 @@ struct Demo #endif #if defined(VK_USE_PLATFORM_XCB_KHR) - void handle_xcb_event(const xcb_generic_event_t *event) - { + void handle_xcb_event(const xcb_generic_event_t *event) { uint8_t event_code = event->response_type & 0x7f; - switch(event_code) - { + switch (event_code) { case XCB_EXPOSE: // TODO: Resize window break; case XCB_CLIENT_MESSAGE: - if((*(xcb_client_message_event_t *)event).data.data32[0] == (*atom_wm_delete_window).atom) - { + if ((*(xcb_client_message_event_t *)event).data.data32[0] == + (*atom_wm_delete_window).atom) { quit = true; } break; - case XCB_KEY_RELEASE: - { - const xcb_key_release_event_t *key = (const xcb_key_release_event_t *)event; + case XCB_KEY_RELEASE: { + const xcb_key_release_event_t *key = + (const xcb_key_release_event_t *)event; - switch(key->detail) - { + switch (key->detail) { case 0x9: // Escape quit = true; break; @@ -2482,11 +2473,10 @@ struct Demo break; } } break; - case XCB_CONFIGURE_NOTIFY: - { - const xcb_configure_notify_event_t *cfg = (const xcb_configure_notify_event_t *)event; - if((width != cfg->width) || (height != cfg->height)) - { + case XCB_CONFIGURE_NOTIFY: { + const xcb_configure_notify_event_t *cfg = + (const xcb_configure_notify_event_t *)event; + if ((width != cfg->width) || (height != cfg->height)) { width = cfg->width; height = cfg->height; resize(); @@ -2497,23 +2487,17 @@ struct Demo } } - void run_xcb() - { + void run_xcb() { xcb_flush(connection); - while(!quit) - { + while (!quit) { xcb_generic_event_t *event; - if(pause) - { + if (pause) { event = xcb_wait_for_event(connection); - } - else - { + } else { event = xcb_poll_for_event(connection); - while(event) - { + while (event) { handle_xcb_event(event); free(event); event = xcb_poll_for_event(connection); @@ -2523,82 +2507,84 @@ struct Demo update_data_buffer(); draw(); curFrame++; - if(frameCount != UINT32_MAX && curFrame == frameCount) - { + if (frameCount != UINT32_MAX && curFrame == frameCount) { quit = true; } } } - void create_xcb_window() - { + void create_xcb_window() { uint32_t value_mask, value_list[32]; xcb_window = xcb_generate_id(connection); value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; value_list[0] = screen->black_pixel; - value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY; + value_list[1] = XCB_EVENT_MASK_KEY_RELEASE | XCB_EVENT_MASK_EXPOSURE | + XCB_EVENT_MASK_STRUCTURE_NOTIFY; - xcb_create_window(connection, XCB_COPY_FROM_PARENT, xcb_window, screen->root, 0, 0, width, height, 0, - XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, value_mask, value_list); + xcb_create_window(connection, XCB_COPY_FROM_PARENT, xcb_window, + screen->root, 0, 0, width, height, 0, + XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, + value_mask, value_list); /* Magic code that will send notification when window is destroyed */ - xcb_intern_atom_cookie_t cookie = xcb_intern_atom(connection, 1, 12, "WM_PROTOCOLS"); - xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(connection, cookie, 0); + xcb_intern_atom_cookie_t cookie = + xcb_intern_atom(connection, 1, 12, "WM_PROTOCOLS"); + xcb_intern_atom_reply_t *reply = + xcb_intern_atom_reply(connection, cookie, 0); - xcb_intern_atom_cookie_t cookie2 = xcb_intern_atom(connection, 0, 16, "WM_DELETE_WINDOW"); + xcb_intern_atom_cookie_t cookie2 = + xcb_intern_atom(connection, 0, 16, "WM_DELETE_WINDOW"); atom_wm_delete_window = xcb_intern_atom_reply(connection, cookie2, 0); - xcb_change_property(connection, XCB_PROP_MODE_REPLACE, xcb_window, (*reply).atom, 4, 32, 1, - &(*atom_wm_delete_window).atom); + xcb_change_property(connection, XCB_PROP_MODE_REPLACE, xcb_window, + (*reply).atom, 4, 32, 1, + &(*atom_wm_delete_window).atom); free(reply); xcb_map_window(connection, xcb_window); - // Force the x/y coordinates to 100,100 results are identical in consecutive + // Force the x/y coordinates to 100,100 results are identical in + // consecutive // runs const uint32_t coords[] = {100, 100}; - xcb_configure_window(connection, xcb_window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords); + xcb_configure_window(connection, xcb_window, + XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, coords); } #endif #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) - void run() - { - while (!quit) - { + void run() { + while (!quit) { update_data_buffer(); draw(); curFrame++; - if(frameCount != UINT32_MAX && curFrame == frameCount) - { + if (frameCount != UINT32_MAX && curFrame == frameCount) { quit = true; } } } - void create_window() - { + void create_window() { window = wl_compositor_create_surface(compositor); - if(!window) - { + if (!window) { printf("Can not create wayland_surface from compositor!\n"); fflush(stdout); exit(1); } shell_surface = wl_shell_get_shell_surface(shell, window); - if(!shell_surface) - { + if (!shell_surface) { printf("Can not get shell_surface from wayland_surface!\n"); fflush(stdout); exit(1); } - wl_shell_surface_add_listener(shell_surface, &shell_surface_listener, this); + wl_shell_surface_add_listener(shell_surface, &shell_surface_listener, + this); wl_shell_surface_set_toplevel(shell_surface); wl_shell_surface_set_title(shell_surface, APP_SHORT_NAME); } @@ -2611,7 +2597,7 @@ struct Demo POINT minsize; // minimum window size char name[APP_NAME_STR_LEN]; // Name to put on the window/icon #elif defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_XCB_KHR) - Display* display; + Display *display; Window xlib_window; Atom xlib_wm_delete_window; @@ -2650,8 +2636,8 @@ struct Demo uint32_t enabled_extension_count; uint32_t enabled_layer_count; - char const* extension_names[64]; - char const* enabled_layers[64]; + char const *extension_names[64]; + char const *enabled_layers[64]; uint32_t width; uint32_t height; @@ -2668,8 +2654,7 @@ struct Demo vk::CommandPool cmd_pool; vk::CommandPool present_cmd_pool; - struct - { + struct { vk::Format format; vk::Image image; vk::MemoryAllocateInfo mem_alloc; @@ -2680,8 +2665,7 @@ struct Demo static int32_t const texture_count = 1; texture_object textures[texture_count]; - struct - { + struct { vk::Buffer buf; vk::MemoryAllocateInfo mem_alloc; vk::DeviceMemory mem; @@ -2729,24 +2713,22 @@ struct Demo Demo demo; // MS-Windows event handling function: -LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) -{ - switch (uMsg) - { +LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { + switch (uMsg) { case WM_CLOSE: PostQuitMessage(validation_error); break; case WM_PAINT: demo.run(); break; - case WM_GETMINMAXINFO: // set window's minimum size - ((MINMAXINFO*)lParam)->ptMinTrackSize = demo.minsize; + case WM_GETMINMAXINFO: // set window's minimum size + ((MINMAXINFO *)lParam)->ptMinTrackSize = demo.minsize; return 0; case WM_SIZE: // Resize the application to the new window size, except when // it was minimized. Vulkan doesn't support images or swapchains // with width=0 and height=0. - if(wParam != SIZE_MINIMIZED) { + if (wParam != SIZE_MINIMIZED) { demo.width = lParam & 0xffff; demo.height = (lParam & 0xffff0000) >> 16; demo.resize(); @@ -2759,8 +2741,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) return (DefWindowProc(hWnd, uMsg, wParam, lParam)); } -int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) -{ +int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, + int nCmdShow) { // TODO: Gah.. refactor. This isn't 1989. MSG msg; // message bool done; // flag saying when app is complete @@ -2774,47 +2756,36 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, // with the non-Windows side. So, we have to convert the information to // Ascii character strings. LPWSTR *commandLineArgs = CommandLineToArgvW(GetCommandLineW(), &argc); - if(nullptr == commandLineArgs) - { + if (nullptr == commandLineArgs) { argc = 0; } - if(argc > 0) - { + if (argc > 0) { argv = (char **)malloc(sizeof(char *) * argc); - if(argv == nullptr) - { + if (argv == nullptr) { argc = 0; - } - else - { - for(int iii = 0; iii < argc; iii++) - { + } else { + for (int iii = 0; iii < argc; iii++) { size_t wideCharLen = wcslen(commandLineArgs[iii]); size_t numConverted = 0; argv[iii] = (char *)malloc(sizeof(char) * (wideCharLen + 1)); - if(argv[iii] != nullptr) - { - wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, commandLineArgs[iii], wideCharLen + 1); + if (argv[iii] != nullptr) { + wcstombs_s(&numConverted, argv[iii], wideCharLen + 1, + commandLineArgs[iii], wideCharLen + 1); } } } - } - else - { + } else { argv = nullptr; } demo.init(argc, argv); // Free up the items we had to allocate for the command line arguments. - if(argc > 0 && argv != nullptr) - { - for(int iii = 0; iii < argc; iii++) - { - if(argv[iii] != nullptr) - { + if (argc > 0 && argv != nullptr) { + for (int iii = 0; iii < argc; iii++) { + if (argv[iii] != nullptr) { free(argv[iii]); } } @@ -2831,15 +2802,12 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, done = false; // initialize loop condition variable // main message loop - while(!done) - { + while (!done) { PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE); - if(msg.message == WM_QUIT) // check for a quit message + if (msg.message == WM_QUIT) // check for a quit message { done = true; // if found, quit app - } - else - { + } else { /* Translate and dispatch to event queue*/ TranslateMessage(&msg); DispatchMessage(&msg); @@ -2855,48 +2823,39 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, #elif __linux__ #if defined(VK_USE_PLATFORM_WAYLAND_KHR) -static void handle_ping(void *data UNUSED, wl_shell_surface *shell_surface, uint32_t serial) -{ +static void handle_ping(void *data UNUSED, wl_shell_surface *shell_surface, + uint32_t serial) { wl_shell_surface_pong(shell_surface, serial); } -static void handle_configure(void *data UNUSED, wl_shell_surface *shell_surface UNUSED, uint32_t edges UNUSED, - int32_t width UNUSED, int32_t height UNUSED) -{ -} +static void handle_configure(void *data UNUSED, + wl_shell_surface *shell_surface UNUSED, + uint32_t edges UNUSED, int32_t width UNUSED, + int32_t height UNUSED) {} -static void handle_popup_done(void *data UNUSED, wl_shell_surface *shell_surface UNUSED) -{ -} +static void handle_popup_done(void *data UNUSED, + wl_shell_surface *shell_surface UNUSED) {} -static const wl_shell_surface_listener shell_surface_listener = -{ - handle_ping, - handle_configure, - handle_popup_done -}; +static const wl_shell_surface_listener shell_surface_listener = { + handle_ping, handle_configure, handle_popup_done}; #endif -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { Demo demo; demo.init(argc, argv); #if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR) - if(demo.use_xlib) - { + if (demo.use_xlib) { demo.create_xlib_window(); - } - else - { + } else { demo.create_xcb_window(); #elif defined(VK_USE_PLATFORM_XCB_KHR) - demo.create_xcb_window(); + demo.create_xcb_window(); #elif defined(VK_USE_PLATFORM_XLIB_KHR) - demo.create_xlib_window(); + demo.create_xlib_window(); #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) - demo.create_window(); + demo.create_window(); #endif } @@ -2905,19 +2864,16 @@ int main(int argc, char **argv) demo.prepare(); #if defined(VK_USE_PLATFORM_XLIB_KHR) && defined(VK_USE_PLATFORM_XCB_KHR) - if(demo.use_xlib) - { + if (demo.use_xlib) { demo.run_xlib(); - } - else - { + } else { demo.run_xcb(); #elif defined(VK_USE_PLATFORM_XCB_KHR) - demo.run_xcb(); +demo.run_xcb(); #elif defined(VK_USE_PLATFORM_XLIB_KHR) - demo.run_xlib(); +demo.run_xlib(); #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) - demo.run(); +demo.run(); #endif } -- 2.7.4