VkResult createImageViews(VkDevice device, uint32_t imageCount, const VkImage pImages,…
- User
VkResult createImageViews(VkDevice device, uint32_t imageCount, const VkImage *pImages, VkFormat format, VkImageView *ppImageViews) { VkImageViewCreateInfo imageViewCreateInfo = {0}; / Why size_t? */ size_t i; VkResult createImageViewResult;
codeimageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; imageViewCreateInfo.format = format; imageViewCreateInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY; imageViewCreateInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; imageViewCreateInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; imageViewCreateInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY; imageViewCreateInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; imageViewCreateInfo.subresourceRange.baseMipLevel = 0; imageViewCreateInfo.subresourceRange.levelCount = 1; imageViewCreateInfo.subresourceRange.baseArrayLayer = 0; imageViewCreateInfo.subresourceRange.layerCount = 1; *ppImageViews = malloc(sizeof(VkImageView) * imageCount); for (i = 0; i < imageCount; ++i) { imageViewCreateInfo.image = pImages[i]; createImageViewResult = vkCreateImageView(device, &imageViewCreateInfo, NULL, ppImageViews[i]); if (createImageViewResult != VK_SUCCESS) { return createImageViewResult; } } return VK_SUCCESS;} This Vulkan code crashes on vkCreateImageView, why?
- Assistant
The crash on vkCreateImageView is likely due to a memory access violation. The issue lies in the line
createImageViewResult = vkCreateImageView(device, &imageViewCreateInfo, NULL, ppImageViews[i]);.The fourth argument of vkCreateImageView expects a pointer to a VkImageView object, but
ppImageViews[i]returns the value of the pointer stored at indexiinppImageViews. To fix this, you need to pass the address ofppImageViews[i]instead of its value. The corrected line of code should be:ccreateImageViewResult = vkCreateImageView(device, &imageViewCreateInfo, NULL, &ppImageViews[i]);By using the address operator
&, you pass the correct pointer to the function, allowing it to correctly populate theVkImageViewobject pointed to byppImageViews[i]. - User
I get: error: passing argument 4 of ‘vkCreateImageView’ from incompatible pointer type Remember, ppImageViews is a pointer to an array of image views (an output variable).
- Assistant
Apologies for the confusion. Since
ppImageViewsis already a pointer to an array ofVkImageView, you don't need to take the address ofppImageViews[i]when callingvkCreateImageView.The corrected line should be:
ccreateImageViewResult = vkCreateImageView(device, &imageViewCreateInfo, NULL, &(*ppImageViews)[i]);Here, the
(*ppImageViews)is used to dereference the pointer to the array, and then[i]is used to access theith element of the array.With this modification,
vkCreateImageViewshould be called properly, and the crash should be resolved.