From af3045190ff503657cfe8e53a3bc03965ceeadc4 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Thu, 1 Mar 2018 14:36:41 +0000 Subject: various code cleanup --- validation.c | 11 ++++++++--- vulkan.c | 57 ++++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/validation.c b/validation.c index c9d2f9f..cfe258a 100644 --- a/validation.c +++ b/validation.c @@ -1,12 +1,15 @@ +#ifndef WITH_VALIDATION +#define WITH_VALIDATION +#endif + +#include #include #include #include #include - #include "eprintf.h" #include "nelem.h" -#define WITH_VALIDATION #include "validation.h" static const char *vlayers[] = { @@ -43,7 +46,7 @@ static bool havelayers(void) instlrp = emalloc(ninstlrp * sizeof *instlrp); vkEnumerateInstanceLayerProperties(&ninstlrp, instlrp); - for (int vl = 0; vl < NELEM(vlayers); vl++) { + for (size_t vl = 0; vl < NELEM(vlayers); vl++) { for (uint32_t il = 0; il < ninstlrp; il++) if (strcmp(vlayers[vl], instlrp[il].layerName) == 0) goto next; @@ -95,6 +98,8 @@ void validation_destroycb(VkInstance inst, void *_cb) PFN_vkDestroyDebugReportCallbackEXT func; VkDebugReportCallbackEXT *cb = _cb; + assert(cb != NULL); + func = (PFN_vkDestroyDebugReportCallbackEXT) vkGetInstanceProcAddr(inst, "vkDestroyDebugReportCallbackEXT"); if (func == NULL) diff --git a/vulkan.c b/vulkan.c index c20a436..03925c4 100644 --- a/vulkan.c +++ b/vulkan.c @@ -74,6 +74,8 @@ struct swpdtl { static GLFWwindow *createwin(int width, int height, const char *title) { + assert(title != NULL); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); @@ -115,8 +117,6 @@ static VkInstance createinst(void) .enabledLayerCount = layers.count, .ppEnabledLayerNames = layers.list, }; - - res = vkCreateInstance(&info, NULL, &inst); if (res != VK_SUCCESS) eprintf("Could not create instance"); @@ -138,7 +138,7 @@ static bool hasdevexts(VkPhysicalDevice phy) vkEnumerateDeviceExtensionProperties(phy, NULL, &nextprops, extprops); for (uint32_t pi = 0; pi < nextprops; pi++) { - for (int xi = 0; xi < NELEM(devexts); xi++) { + for (size_t xi = 0; xi < NELEM(devexts); xi++) { if (strcmp(devexts[xi], extprops[pi].extensionName) == 0) { hasext[xi] = true; break; @@ -148,7 +148,7 @@ static bool hasdevexts(VkPhysicalDevice phy) free(extprops); - for (int i = 0; i < NELEM(hasext); i++) + for (size_t i = 0; i < NELEM(hasext); i++) if (!hasext[i]) return false; @@ -158,6 +158,7 @@ static bool hasdevexts(VkPhysicalDevice phy) static bool isdevsuitable(VkPhysicalDevice phy, const int *qf, const struct swpdtl *sdtl) { assert(qf != NULL); + assert(sdtl != NULL); return qf[QF_GRAPHICS] >= 0 && qf[QF_PRESENT] >= 0 && hasdevexts(phy) && sdtl->fmts.count > 0 && @@ -219,6 +220,7 @@ static VkPhysicalDevice pickphy(int *qf, struct swpdtl *sdtl, VkInstance inst, V uint32_t nphys; assert(qf != NULL); + assert(sdtl != NULL); vkEnumeratePhysicalDevices(inst, &nphys, NULL); phys = emalloc(nphys * sizeof *phys); @@ -247,9 +249,13 @@ static int loaddqcinfs(VkDeviceQueueCreateInfo *dqcinfs, const int *qf, const fl { int ndqcinfs = 0; + assert(dqcinfs != NULL); + assert(qf != NULL); + assert(prio != NULL); + for (int fi = 0; fi < QF_MAX; fi++) { for (int cii = 0; cii < fi; cii++) - if (dqcinfs[cii].queueFamilyIndex == qf[fi]) + if (dqcinfs[cii].queueFamilyIndex == (uint32_t)qf[fi]) goto next; dqcinfs[ndqcinfs++] = (VkDeviceQueueCreateInfo){ .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, @@ -272,6 +278,8 @@ static VkDevice createdev(VkPhysicalDevice phy, const int *qf) VkDevice dev; VkResult res; + assert(qf != NULL); + ndqcinfs = loaddqcinfs(dqcinfs, qf, &(float){ 1 }); validation_info(&layers, NULL); info = (VkDeviceCreateInfo){ @@ -328,14 +336,20 @@ static VkExtent2D getswpextent(const VkSurfaceCapabilitiesKHR *caps) { VkExtent2D extent = { WINWIDTH, WINHEIGHT }; + assert(caps != NULL); + if (caps->currentExtent.width != UINT32_MAX) return caps->currentExtent; - if (extent.width > caps->maxImageExtent.width) extent.width = caps->maxImageExtent.width; - if (extent.width < caps->minImageExtent.width) extent.width = caps->minImageExtent.width; + if (extent.width > caps->maxImageExtent.width) + extent.width = caps->maxImageExtent.width; + if (extent.width < caps->minImageExtent.width) + extent.width = caps->minImageExtent.width; - if (extent.height > caps->maxImageExtent.height) extent.height = caps->maxImageExtent.height; - if (extent.height < caps->minImageExtent.height) extent.height = caps->minImageExtent.height; + if (extent.height > caps->maxImageExtent.height) + extent.height = caps->maxImageExtent.height; + if (extent.height < caps->minImageExtent.height) + extent.height = caps->minImageExtent.height; return extent; } @@ -382,7 +396,6 @@ static void createswp(struct swp *swp, VkSurfaceKHR surf, VkDevice dev, const in .clipped = VK_TRUE, .oldSwapchain = VK_NULL_HANDLE, }; - res = vkCreateSwapchainKHR(dev, &info, NULL, &swp->swp); if (res != VK_SUCCESS) eprintf("Could not create swapchain"); @@ -438,6 +451,8 @@ static void createviews(struct views *views, VkDevice dev, const struct swpimgs static void destroyviews(struct views *views, VkDevice dev) { + assert(views != NULL); + LIST_FOREACH(views, i) vkDestroyImageView(dev, views->list[i], NULL); free(views->list); } @@ -455,7 +470,6 @@ static VkShaderModule createsmod(VkDevice dev, const void *data, size_t size) .codeSize = size, .pCode = data, }; - res = vkCreateShaderModule(dev, &smcinf, NULL, &smod); if (res != VK_SUCCESS) eprintf("Could not create shader module"); @@ -501,7 +515,6 @@ static VkRenderPass createpass(VkDevice dev, VkFormat fmt) .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, }, }; - res = vkCreateRenderPass(dev, &info, NULL, &pass); if (res != VK_SUCCESS) eprintf("Could not create render pass"); @@ -527,7 +540,6 @@ static void createpipeline(struct ppln *ppln, VkDevice dev, /* .pushConstantRangeCount = 0, */ /* .pPushConstantRanges = 0, */ }; - res = vkCreatePipelineLayout(dev, &plcinf, NULL, &ppln->layout); if (res != VK_SUCCESS) eprintf("Could not create pipeline layout"); @@ -629,7 +641,6 @@ static void createpipeline(struct ppln *ppln, VkDevice dev, .subpass = 0, .basePipelineHandle = VK_NULL_HANDLE, }; - res = vkCreateGraphicsPipelines(dev, VK_NULL_HANDLE, 1, &gpcinf, NULL, &ppln->ppln); if (res != VK_SUCCESS) eprintf("Could not create graphics pipeline"); @@ -640,6 +651,8 @@ static void createpipeline(struct ppln *ppln, VkDevice dev, static void destroyppln(struct ppln *ppln, VkDevice dev) { + assert(ppln != NULL); + vkDestroyPipeline(dev, ppln->ppln, NULL); vkDestroyPipelineLayout(dev, ppln->layout, NULL); } @@ -651,6 +664,7 @@ static void createfbs(struct fbs *fbs, VkDevice dev, const struct views *views, assert(fbs != NULL); assert(views != NULL); + assert(extent != NULL); *fbs = (struct fbs){ .count = views->count }; LIST_REALLOC(fbs); @@ -673,13 +687,15 @@ static void createfbs(struct fbs *fbs, VkDevice dev, const struct views *views, static void destroyfbs(struct fbs *fbs, VkDevice dev) { + assert(fbs != NULL); + LIST_FOREACH(fbs, i) vkDestroyFramebuffer(dev, fbs->list[i], NULL); free(fbs->list); } static VkCommandPool createcpool(struct cmdbufs *cmdbufs, VkDevice dev, const int *qf, const struct fbs *fbs, VkRenderPass pass, - VkExtent2D *extent, const struct ppln *ppln) + const VkExtent2D *extent, const struct ppln *ppln) { VkCommandBufferAllocateInfo cbainf; VkCommandPoolCreateInfo cpcinf; @@ -688,6 +704,9 @@ static VkCommandPool createcpool(struct cmdbufs *cmdbufs, VkDevice dev, const in assert(cmdbufs != NULL); assert(qf != NULL); + assert(fbs != NULL); + assert(extent != NULL); + assert(ppln != NULL); cpcinf = (VkCommandPoolCreateInfo){ .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, @@ -716,6 +735,7 @@ static VkCommandPool createcpool(struct cmdbufs *cmdbufs, VkDevice dev, const in .flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT, /* .pInheritanceInfo = NULL, */ }); + vkCmdBeginRenderPass(cmdbufs->list[i], &(VkRenderPassBeginInfo){ .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO, .renderPass = pass, @@ -729,7 +749,9 @@ static VkCommandPool createcpool(struct cmdbufs *cmdbufs, VkDevice dev, const in } }, }, VK_SUBPASS_CONTENTS_INLINE); - vkCmdBindPipeline(cmdbufs->list[i], VK_PIPELINE_BIND_POINT_GRAPHICS, ppln->ppln); + + vkCmdBindPipeline(cmdbufs->list[i], + VK_PIPELINE_BIND_POINT_GRAPHICS, ppln->ppln); vkCmdDraw(cmdbufs->list[i], 3, 1, 0, 0); vkCmdEndRenderPass(cmdbufs->list[i]); res = vkEndCommandBuffer(cmdbufs->list[i]); @@ -762,6 +784,8 @@ void drawframe(VkDevice dev, VkSwapchainKHR swp, VkQueue graphq, VkSubmitInfo sinf; uint32_t i; + assert(cmdbufs != NULL); + vkAcquireNextImageKHR(dev, swp, UINT64_MAX, semavail, VK_NULL_HANDLE, &i); sinf = (VkSubmitInfo){ .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, @@ -786,7 +810,6 @@ void drawframe(VkDevice dev, VkSwapchainKHR swp, VkQueue graphq, .pImageIndices = &i, /* .pResults = NULL, */ }; - vkQueuePresentKHR(presq, &pinf); } -- cgit v1.2.3-54-g00ecf