Fix issue with resizing viewport

This commit is contained in:
2020-10-15 23:55:11 +02:00
parent 079b55f0d0
commit 3c129e4653
4 changed files with 34 additions and 12 deletions

View File

@@ -1,4 +1,11 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "vulkan/vulkan.hpp"
#include "../Device.hpp"
#include "../../Base/ICloseable.hpp"
@@ -93,9 +100,7 @@ namespace openVulkanoCpp
{
for (auto shader : shaders)
{
Scene::Shader* s = shader->shader;
shader->Close();
shader->Init(context, s, this);
shader->Resize();
}
}

View File

@@ -32,8 +32,14 @@ namespace openVulkanoCpp::Vulkan
this->device = context->device->device;
this->shader = shader;
this->owner = owner;
this->context = context;
shaderModuleVertex = context->device->CreateShaderModule(shader->vertexShaderName + ".vert.spv");
shaderModuleFragment = context->device->CreateShaderModule(shader->fragmentShaderName + ".frag.spv");
BuildPipeline();
}
void VulkanShader::BuildPipeline()
{
std::vector<vk::PipelineShaderStageCreateInfo> shaderStageCreateInfos(2);
shaderStageCreateInfos[0] = { {}, vk::ShaderStageFlagBits::eVertex, shaderModuleVertex, "main" };
shaderStageCreateInfos[1] = { {}, vk::ShaderStageFlagBits::eFragment, shaderModuleFragment, "main" };
@@ -47,9 +53,7 @@ namespace openVulkanoCpp::Vulkan
attributeDescriptions.emplace_back(4, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, textureCoordinates));
attributeDescriptions.emplace_back(5, 0, vk::Format::eR32G32B32A32Sfloat, offsetof(Vertex, color));
auto viewport = context->swapChain.GetFullscreenViewport();
auto scissor = context->swapChain.GetFullscreenScissor();
vk::PipelineViewportStateCreateInfo viewportStateCreateInfo = { {}, 1, &viewport, 1, &scissor };
vk::PipelineViewportStateCreateInfo viewportStateCreateInfo = { {}, 1, &context->swapChain.GetFullscreenViewport(), 1, &context->swapChain.GetFullscreenScissor() };
vk::PipelineVertexInputStateCreateInfo pipelineVertexInputStateCreateInfo = { {}, 1, &vertexBindDesc,
static_cast<uint32_t>(attributeDescriptions.size()), attributeDescriptions.data() };
vk::PipelineInputAssemblyStateCreateInfo inputAssembly = { {}, ToVkTopology(shader->topology), 0 };
@@ -69,7 +73,12 @@ namespace openVulkanoCpp::Vulkan
vk::GraphicsPipelineCreateInfo pipelineCreateInfo = { {}, static_cast<uint32_t>(shaderStageCreateInfos.size()), shaderStageCreateInfos.data(), &pipelineVertexInputStateCreateInfo, &inputAssembly,
nullptr, &viewportStateCreateInfo, &rasterizer, &msaa, &depth, &colorInfo, nullptr, context->pipeline.pipelineLayout, context->swapChainRenderPass.renderPass };
pipeline = this->device.createGraphicsPipeline({}, pipelineCreateInfo).value;
}
void VulkanShader::Resize()
{
device.destroyPipeline(pipeline);
BuildPipeline();
}
void VulkanShader::Record(vk::CommandBuffer& cmdBuffer, uint32_t bufferId)

View File

@@ -23,15 +23,21 @@ namespace openVulkanoCpp::Vulkan
vk::ShaderModule shaderModuleVertex, shaderModuleFragment;
vk::Pipeline pipeline;
IShaderOwner* owner;
Context* context;
VulkanShader() = default;
~VulkanShader() override { if (shader) VulkanShader::Close(); }
void Init(Context* context, Scene::Shader* shader, IShaderOwner* owner);
void Resize();
void Record(vk::CommandBuffer& cmdBuffer, uint32_t bufferId) override;
void Close() override;
private:
void BuildPipeline();
};
}

View File

@@ -45,6 +45,7 @@ namespace openVulkanoCpp
vk::SurfaceFormatKHR surfaceFormat;
vk::PresentModeKHR presentMode;
vk::Viewport fullscreenViewport;
vk::Rect2D fullscreenScissor;
bool useVsync = false;
uint32_t preferredImageCount = 2; //TODO add option
@@ -87,19 +88,19 @@ namespace openVulkanoCpp
FrameBuffer::Resize(vk::Extent3D(size, 1));
}
vk::Extent2D GetSize() const
[[nodiscard]] vk::Extent2D GetSize() const
{
return size;
}
vk::Viewport GetFullscreenViewport() const
[[nodiscard]] const vk::Viewport& GetFullscreenViewport() const
{
return fullscreenViewport;
}
vk::Rect2D GetFullscreenScissor() const
[[nodiscard]] const vk::Rect2D& GetFullscreenScissor() const
{
return { {0,0}, GetSize() };
return fullscreenScissor;
}
uint32_t AcquireNextImage(const vk::Fence fence = vk::Fence())
@@ -127,14 +128,14 @@ namespace openVulkanoCpp
1, &swapChain, &currentFrameBufferId));
}
bool UseVsync() { return useVsync; }
bool UseVsync() const { return useVsync; }
void SetVsync(bool useVsync)
{ //TODO change swap chain
this->useVsync = useVsync;
}
uint32_t GetImageCount() const
[[nodiscard]] uint32_t GetImageCount() const
{
return images.size();
}
@@ -175,6 +176,7 @@ namespace openVulkanoCpp
CreateSwapChainImages();
fullscreenViewport = vk::Viewport{ 0, 0, (float)size.width, (float)size.height, 0, 1 };
fullscreenScissor = vk::Rect2D{ {0,0}, size };
Logger::RENDER->debug("Swap chain for window {0} created", window->GetWindowId());
}