Skip to content
Snippets Groups Projects
Commit a10fd28d authored by Sebastian Gaida's avatar Sebastian Gaida
Browse files

[#34] add recreation for swapchain and imageViews

parent df58bf24
No related branches found
No related tags found
1 merge request!43Resolve "Resizing-Event"
Pipeline #25348 failed
...@@ -88,7 +88,9 @@ namespace vkcv ...@@ -88,7 +88,9 @@ namespace vkcv
* @param[in] width new window width * @param[in] width new window width
* @param[in] height new window hight * @param[in] height new window hight
*/ */
static void recreateSwapchain( const vk::Device& device ,int width, int height); void recreateSwapchain(int width, int height);
static std::vector<vk::ImageView> createImageViews( Context &context, SwapChain& swapChain);
public: public:
/** /**
......
...@@ -10,6 +10,7 @@ namespace vkcv { ...@@ -10,6 +10,7 @@ namespace vkcv {
vk::SurfaceKHR m_surface; vk::SurfaceKHR m_surface;
vk::SwapchainKHR m_swapchain; vk::SwapchainKHR m_swapchain;
vk::SurfaceFormatKHR m_format; vk::SurfaceFormatKHR m_format;
vk::PresentModeKHR m_presentMode;
uint32_t m_ImageCount; uint32_t m_ImageCount;
...@@ -21,7 +22,7 @@ namespace vkcv { ...@@ -21,7 +22,7 @@ namespace vkcv {
* @param swapchain to show images in the window * @param swapchain to show images in the window
* @param format * @param format
*/ */
SwapChain(vk::SurfaceKHR surface, vk::SwapchainKHR swapchain, vk::SurfaceFormatKHR format, uint32_t imageCount); SwapChain(vk::SurfaceKHR surface, vk::SwapchainKHR swapchain, vk::SurfaceFormatKHR format, uint32_t imageCount, vk::PresentModeKHR presentMode);
public: public:
SwapChain(const SwapChain &other) = default; SwapChain(const SwapChain &other) = default;
...@@ -64,6 +65,13 @@ namespace vkcv { ...@@ -64,6 +65,13 @@ namespace vkcv {
* @return number of images in swapchain * @return number of images in swapchain
*/ */
uint32_t getImageCount(); uint32_t getImageCount();
/**
*
* @param width
* @param height
*/
vk::SwapchainKHR recreateSwapchain( const Context &context, const Window &window, int width, int height);
}; };
} }
...@@ -142,8 +142,8 @@ int main(int argc, const char** argv) { ...@@ -142,8 +142,8 @@ int main(int argc, const char** argv) {
auto start = std::chrono::system_clock::now(); auto start = std::chrono::system_clock::now();
while (window.isWindowOpen()) { while (window.isWindowOpen()) {
window.pollEvents();
core.beginFrame(); core.beginFrame();
window.pollEvents();
auto end = std::chrono::system_clock::now(); auto end = std::chrono::system_clock::now();
auto deltatime = end - start; auto deltatime = end - start;
start = end; start = end;
......
...@@ -41,31 +41,8 @@ namespace vkcv ...@@ -41,31 +41,8 @@ namespace vkcv
SwapChain swapChain = SwapChain::create(window, context, surface); SwapChain swapChain = SwapChain::create(window, context, surface);
std::vector<vk::Image> swapChainImages = context.getDevice().getSwapchainImagesKHR(swapChain.getSwapchain());
std::vector<vk::ImageView> imageViews; std::vector<vk::ImageView> imageViews;
imageViews.reserve( swapChainImages.size() ); imageViews = createImageViews( context, swapChain);
//here can be swizzled with vk::ComponentSwizzle if needed
vk::ComponentMapping componentMapping(
vk::ComponentSwizzle::eR,
vk::ComponentSwizzle::eG,
vk::ComponentSwizzle::eB,
vk::ComponentSwizzle::eA );
vk::ImageSubresourceRange subResourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 );
for ( auto image : swapChainImages )
{
vk::ImageViewCreateInfo imageViewCreateInfo(
vk::ImageViewCreateFlags(),
image,
vk::ImageViewType::e2D,
swapChain.getSurfaceFormat().format,
componentMapping,
subResourceRange
);
imageViews.push_back(context.getDevice().createImageView(imageViewCreateInfo));
}
const auto& queueManager = context.getQueueManager(); const auto& queueManager = context.getQueueManager();
...@@ -102,7 +79,7 @@ namespace vkcv ...@@ -102,7 +79,7 @@ namespace vkcv
m_ImageManager->m_core = this; m_ImageManager->m_core = this;
m_resizeHandle = window.e_resize.add( [&](int width, int height) { recreateSwapchain( this->getContext().getDevice(),width ,height ); }); m_resizeHandle = window.e_resize.add( [&](int width, int height) { recreateSwapchain( width ,height ); });
} }
Core::~Core() noexcept { Core::~Core() noexcept {
...@@ -306,8 +283,17 @@ namespace vkcv ...@@ -306,8 +283,17 @@ namespace vkcv
return m_swapchain.getSurfaceFormat().format; return m_swapchain.getSurfaceFormat().format;
} }
void Core::recreateSwapchain( const vk::Device& device, int width, int height) { void Core::recreateSwapchain( int width, int height) {
device.waitIdle(); m_Context.getDevice().waitIdle();
destroyTemporaryFramebuffers();
for (auto image : m_swapchainImageViews) {
m_Context.m_Device.destroyImageView(image);
}
m_swapchain.recreateSwapchain(m_Context, m_window, width, height);
m_swapchainImageViews = createImageViews(m_Context, m_swapchain);
} }
void Core::submitCommands(const SubmitInfo &submitInfo, const RecordCommandFunction& record, const FinishCommandFunction& finish) void Core::submitCommands(const SubmitInfo &submitInfo, const RecordCommandFunction& record, const FinishCommandFunction& finish)
...@@ -362,4 +348,33 @@ namespace vkcv ...@@ -362,4 +348,33 @@ namespace vkcv
vk::DescriptorSetLayout Core::getDescritorSetLayout(ResourcesHandle handle, size_t setIndex) { vk::DescriptorSetLayout Core::getDescritorSetLayout(ResourcesHandle handle, size_t setIndex) {
return m_DescriptorManager->getDescriptorSetLayout(handle, setIndex); return m_DescriptorManager->getDescriptorSetLayout(handle, setIndex);
} }
std::vector<vk::ImageView> Core::createImageViews( Context &context, SwapChain& swapChain){
std::vector<vk::ImageView> imageViews;
std::vector<vk::Image> swapChainImages = context.getDevice().getSwapchainImagesKHR(swapChain.getSwapchain());
imageViews.reserve( swapChainImages.size() );
//here can be swizzled with vk::ComponentSwizzle if needed
vk::ComponentMapping componentMapping(
vk::ComponentSwizzle::eR,
vk::ComponentSwizzle::eG,
vk::ComponentSwizzle::eB,
vk::ComponentSwizzle::eA );
vk::ImageSubresourceRange subResourceRange( vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1 );
for ( auto image : swapChainImages )
{
vk::ImageViewCreateInfo imageViewCreateInfo(
vk::ImageViewCreateFlags(),
image,
vk::ImageViewType::e2D,
swapChain.getSurfaceFormat().format,
componentMapping,
subResourceRange
);
imageViews.push_back(context.getDevice().createImageView(imageViewCreateInfo));
}
return imageViews;
}
} }
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
namespace vkcv { namespace vkcv {
SwapChain::SwapChain(vk::SurfaceKHR surface, vk::SwapchainKHR swapchain, vk::SurfaceFormatKHR format, uint32_t imageCount) SwapChain::SwapChain(vk::SurfaceKHR surface, vk::SwapchainKHR swapchain, vk::SurfaceFormatKHR format, uint32_t imageCount, vk::PresentModeKHR presentMode)
: m_surface(surface), m_swapchain(swapchain), m_format( format), m_ImageCount(imageCount) : m_surface(surface), m_swapchain(swapchain), m_format( format), m_ImageCount(imageCount), m_presentMode(presentMode)
{} {}
const vk::SwapchainKHR& SwapChain::getSwapchain() const { const vk::SwapchainKHR& SwapChain::getSwapchain() const {
...@@ -157,7 +157,32 @@ namespace vkcv { ...@@ -157,7 +157,32 @@ namespace vkcv {
vk::SwapchainKHR swapchain = device.createSwapchainKHR(swapchainCreateInfo); vk::SwapchainKHR swapchain = device.createSwapchainKHR(swapchainCreateInfo);
return SwapChain(surface, swapchain, surfaceFormat, imageCount); return SwapChain(surface, swapchain, surfaceFormat, imageCount, presentMode);
}
vk::SwapchainKHR SwapChain::recreateSwapchain( const Context &context, const Window &window, int width, int height){
vk::SwapchainKHR oldSwapchain = m_swapchain;
vk::Extent2D extent2D = chooseSwapExtent(context.getPhysicalDevice(), m_surface, window);
vk::SwapchainCreateInfoKHR swapchainCreateInfo(
vk::SwapchainCreateFlagsKHR(),
m_surface,
m_ImageCount,
m_format.format,
m_format.colorSpace,
extent2D,
1,
vk::ImageUsageFlagBits::eColorAttachment,
vk::SharingMode::eExclusive,
0,
nullptr,
vk::SurfaceTransformFlagBitsKHR::eIdentity,
vk::CompositeAlphaFlagBitsKHR::eOpaque,
m_presentMode,
true,
oldSwapchain
);
m_swapchain = context.getDevice().createSwapchainKHR(swapchainCreateInfo);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment