stories/engine/swapchain.cpp

52 lines
1.4 KiB
C++

#include "swapchain.h"
#include "logicaldevice.h"
Engine::SwapChain::SwapChain(LogicalDevice* logical):
logical(logical),
vk(),
images(),
extent(logical->chooseSwapExtent()),
imageViews(),
frameBuffers()
{
VkResult result = logical->createVkSwapChain(extent, vk);
if (result != VK_SUCCESS)
throw std::runtime_error("failed to create swap chain!");
logical->getVkSwapChainImages(vk, images);
imageViews.resize(images.size());
frameBuffers.resize(images.size());
for (size_t i = 0; i < images.size(); i++) {
if (logical->createVkImageView(images[i], imageViews[i]) != VK_SUCCESS)
throw std::runtime_error("failed to create image views!");
}
for (size_t i = 0; i < images.size(); i++) {
if (logical->createVkFrameBuffer(imageViews[i], extent, frameBuffers[i]) != VK_SUCCESS)
throw std::runtime_error("failed to create framebuffer!");
}
}
Engine::SwapChain::~SwapChain() {
for (VkFramebuffer& buffer : frameBuffers)
logical->destroyVkFrameBuffer(buffer);
for (VkImageView& imageView : imageViews)
logical->destroyVkImageView(imageView);
//images are also indirectly freed by this operation
logical->destroyVkSwapChain(vk);
}
VkExtent2D Engine::SwapChain::getExtent() const {
return extent;
}
VkFramebuffer Engine::SwapChain::getFrameBuffer(uint32_t index) {
return frameBuffers[index];
}