create swapchain image views

This commit is contained in:
IlyaShurupov 2024-11-08 11:37:26 +03:00
parent 8a443a0a25
commit ee8f06292d

View file

@ -51,6 +51,7 @@ private:
createLogicalDevice();
getQueues();
createSwapChain();
createSwapChainImageViews();
}
void mainLoop() {
@ -60,6 +61,7 @@ private:
}
void cleanup() {
destroySwapChainImageViews();
vkDestroySwapchainKHR(mDevice, mSwapChain, nullptr);
vkDestroyDevice(mDevice, nullptr);
vkDestroySurfaceKHR(mInstance, mSurface, nullptr);
@ -374,6 +376,46 @@ private:
return out;
}
void createSwapChainImageViews() {
mSwapChainImageViews.resize(mSwapChainImages.size());
int i = 0;
for (const auto& image : mSwapChainImages) {
VkImageViewCreateInfo createInfo {
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.image = image,
.viewType = VK_IMAGE_VIEW_TYPE_2D,
.format = mSwapChainFormat,
.components = {
.r = VK_COMPONENT_SWIZZLE_R,
.g = VK_COMPONENT_SWIZZLE_G,
.b = VK_COMPONENT_SWIZZLE_B,
.a = VK_COMPONENT_SWIZZLE_A,
},
.subresourceRange = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.baseMipLevel = 0,
.levelCount = 1,
.baseArrayLayer = 0,
.layerCount = 1,
}
};
if (vkCreateImageView(mDevice, &createInfo, nullptr, &mSwapChainImageViews[i]) != VK_SUCCESS) {
throw std::runtime_error("cannot create image view for the swap chain");
}
i++;
}
}
void destroySwapChainImageViews() {
for (const auto& imageView : mSwapChainImageViews) {
vkDestroyImageView(mDevice, imageView, nullptr);
}
}
private:
GLFWwindow* mWindow = nullptr;
@ -390,9 +432,12 @@ private:
VkSurfaceKHR mSurface = VK_NULL_HANDLE;
VkSwapchainKHR mSwapChain = VK_NULL_HANDLE;
std::vector<VkImage> mSwapChainImages;
VkFormat mSwapChainFormat {};
VkExtent2D mSwapChainExtent {};
std::vector<VkImage> mSwapChainImages;
std::vector<VkImageView> mSwapChainImageViews;
};
int main() {