X-Git-Url: https://eleni.mutantstargoat.com/git/?p=demo;a=blobdiff_plain;f=src%2Fvulkan%2Fvkutil.cc;fp=src%2Fvulkan%2Fvkutil.cc;h=526ec382c16661a99021401249d9393fa486a1a9;hp=65467c563a014ecb24a56ca8028fd71db3c7efaa;hb=9d97500df375665ce6757c1341373cdd34fd9235;hpb=8fb0cca684e078cd2537070c53ad970ebbc2e9a7 diff --git a/src/vulkan/vkutil.cc b/src/vulkan/vkutil.cc index 65467c5..526ec38 100644 --- a/src/vulkan/vkutil.cc +++ b/src/vulkan/vkutil.cc @@ -248,10 +248,10 @@ bool vku_create_device() return false; } -/* if(!(vkcmdbuf = vku_alloc_cmdbuf(vk_pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY))) { - fprintf(stderr, "failed to create primary command buffer\n"); - return false; - } */ + /* if(!(vkcmdbuf = vku_alloc_cmdbuf(vk_pool, VK_COMMAND_BUFFER_LEVEL_PRIMARY))) { + fprintf(stderr, "failed to create primary command buffer\n"); + return false; + } */ return true; } @@ -310,6 +310,24 @@ VkCommandBuffer vku_alloc_cmdbuf(VkCommandPool pool, VkCommandBufferLevel level) return cmdbuf; } +bool vku_alloc_cmdbufs(VkCommandPool pool, VkCommandBufferLevel level, unsigned int count, VkCommandBuffer *buf_array) +{ + VkCommandBufferAllocateInfo cinf; + memset(&cinf, 0, sizeof cinf); + + cinf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; + cinf.commandPool = vk_pool; + cinf.level = level; + cinf.commandBufferCount = count; + + if(vkAllocateCommandBuffers(vk_device, &cinf, buf_array) != VK_SUCCESS) { + fprintf(stderr, "Failed to allocate command buffer\n"); + return false; + } + + return true; +} + void vku_free_cmdbuf(VkCommandPool pool, VkCommandBuffer buf) { vkFreeCommandBuffers(vk_device, pool, 1, &buf); @@ -349,101 +367,39 @@ void vku_submit_cmdbuf(VkQueue q, VkCommandBuffer buf, VkFence done_fence) vkQueueSubmit(q, 1, &info, done_fence); } -VkSwapchainKHR vku_create_swapchain(VkSurfaceKHR surf, int xsz, int ysz, int n, - VkPresentModeKHR pmode, VkSwapchainKHR prev) +bool vku_get_surface_format(VkPhysicalDevice gpu, VkSurfaceKHR surface, VkSurfaceFormatKHR *sformat) { - VkSurfaceCapabilitiesKHR scap; - vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vk_physical, surf, &scap); - - VkPhysicalDeviceMemoryProperties mprops; - vkGetPhysicalDeviceMemoryProperties(vk_physical, &mprops); - - uint32_t pmcnt; - vkGetPhysicalDeviceSurfacePresentModesKHR(vk_physical, surf, &pmcnt, 0); - VkPresentModeKHR scpm = VK_PRESENT_MODE_IMMEDIATE_KHR; - VkExtent2D scext; - scext.width = xsz; - scext.height = ysz; - -// VkSurfaceTransformFlagBitsKHR pretransf; -// if(scap.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR) { -// pretransf = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; -// } else { -// pretransf = scap.currentTransform; -// } - - VkSwapchainKHR sc; - VkSwapchainCreateInfoKHR inf; + uint32_t fcount; - VkSurfaceFormatKHR sformat; - sformat.format = VK_FORMAT_B8G8R8A8_UNORM; - - memset(&inf, 0, sizeof inf); - inf.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; - inf.surface = surf; - inf.minImageCount = n; - inf.imageFormat = sformat.format; /* TODO enumerate and choose */ - inf.imageColorSpace = sformat.colorSpace; - inf.imageExtent = scext; - inf.imageArrayLayers = 1; - inf.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; - inf.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; /* XXX make this an option? */ - inf.preTransform = VK_SURFACE_TRANSFORM_INHERIT_BIT_KHR; - inf.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; - inf.presentMode = pmode; - inf.oldSwapchain = VK_NULL_HANDLE; - inf.clipped = VK_TRUE; - - if(vkCreateSwapchainKHR(vk_device, &inf, 0, &sc) != 0) { - return 0; + if(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical, + vk_surface, &fcount, 0) != VK_SUCCESS) { + fprintf(stderr, "Failed to get format count for physical device.\n"); + return false; + } + if(fcount == 0) { + fprintf(stderr, "No color formats were found.\n"); + return false; } - return sc; -} - -VkImage *vku_get_swapchain_images(VkSwapchainKHR sc, int *count) -{ - uint32_t nimg; - VkImage *images; - if(vkGetSwapchainImagesKHR(vk_device, sc, &nimg, 0) != 0) { - return 0; + VkSurfaceFormatKHR *formats = new VkSurfaceFormatKHR[fcount]; + if(vkGetPhysicalDeviceSurfaceFormatsKHR(vk_physical, vk_surface, + &fcount, formats) != VK_SUCCESS) { + delete [] formats; + fprintf(stderr, "Failed to get surface formats.\n"); + return false; } - images = new VkImage[nimg]; - vkGetSwapchainImagesKHR(vk_device, sc, &nimg, images); - if(count) *count = (int)nimg; - return images; -} + *sformat = formats[0]; -VkImageView *vku_create_image_views(VkImage *images, int count) -{ - VkImageView *iviews; - - iviews = new VkImageView[count]; - for(int i=0; iformat = VK_FORMAT_B8G8R8_UNORM; } - return iviews; + delete [] formats; + return true; } + int vku_get_next_image(VkSwapchainKHR sc) { uint32_t next; @@ -470,89 +426,6 @@ void vku_present(VkSwapchainKHR sc, int img_idx) vkQueuePresentKHR(vk_queue, &inf); } -bool vku_create_renderpass() -{ - VkAttachmentDescription attachments[2]; - memset(&attachments, 0, 2 * sizeof *attachments); - - /* color */ - attachments[0].format = VK_FORMAT_B8G8R8A8_UNORM; - attachments[0].samples = VK_SAMPLE_COUNT_1_BIT; - attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE; - attachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; - attachments[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; - attachments[0].initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; - attachments[0].finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; - - /* depth */ - attachments[1].format = VK_FORMAT_D32_SFLOAT_S8_UINT; //TODO - attachments[1].samples = VK_SAMPLE_COUNT_1_BIT; - attachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; - attachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; - attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; - attachments[1].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; - attachments[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; - - VkAttachmentReference color_ref; - color_ref.attachment = 0; - color_ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; - - VkAttachmentReference depth_ref; - depth_ref.attachment = 1; - depth_ref.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; - - VkSubpassDescription subpass_desc; - memset(&subpass_desc, 0, sizeof subpass_desc); - - subpass_desc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; - subpass_desc.colorAttachmentCount = 1; - subpass_desc.pColorAttachments = &color_ref; - subpass_desc.pDepthStencilAttachment = &depth_ref; - - VkRenderPassCreateInfo inf; - memset(&inf, 0, sizeof inf); - - inf.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; - inf.attachmentCount = 2; - inf.pAttachments = attachments; - inf.subpassCount = 1; - inf.pSubpasses = &subpass_desc; - - if(vkCreateRenderPass(vk_device, &inf, 0, &vkrpass) != VK_SUCCESS) { - return false; - } - - return true; -} - -bool vku_create_framebuffers(VkImageView *image_views, int count, int w, int h) -{ - delete vkfbufs; - vkfbufs = new VkFramebuffer[count]; - - VkFramebufferCreateInfo inf; - memset(&inf, 0, sizeof inf); - - inf.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; - inf.renderPass = vkrpass; - inf.attachmentCount = count; - inf.pAttachments = image_views; - inf.width = w; - inf.height = h; - inf.layers = 1; - - for(int i=0; i ds_enabled; -// ds_enabled.push_back(VK_DYNAMIC_STATE_VIEWPORT); - //ds_enabled.push_back(VK_DYNAMIC_STATE_SCISSOR); -// VkPipelineDynamicStateCreateInfo ds; -// ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; -// ds.pDynamicStates = ds_enabled.data(); -// ds.dynamicStateCount = static_cast(ds_enabled.size()); - - /* depth tests */ - VkPipelineDepthStencilStateCreateInfo dsi; - memset(&dsi, 0, sizeof dsi); - dsi.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; - dsi.depthTestEnable = VK_TRUE; - dsi.depthWriteEnable = VK_TRUE; - dsi.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL; - dsi.back.failOp = VK_STENCIL_OP_KEEP; - dsi.back.passOp = VK_STENCIL_OP_KEEP; - dsi.back.compareOp = VK_COMPARE_OP_ALWAYS; - dsi.front = dsi.back; - - /* multisampling - must be set even if not used */ - VkPipelineMultisampleStateCreateInfo msi; - memset(&msi, 0, sizeof msi); - msi.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; - msi.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; - - /* vertex input descriptions */ - VkVertexInputBindingDescription vib; - memset(&vib, 0, sizeof vib); - vib.stride = sizeof(Vec3); - vib.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; - - /* input attr bindings */ - VkVertexInputAttributeDescription via[2]; - memset(&via, 0, sizeof via); - via[0].format = VK_FORMAT_R32G32B32A32_SFLOAT; - - return true; -} static const char *get_device_name_str(VkPhysicalDeviceType type) {