Fix some stuff

This commit is contained in:
2026-06-21 18:00:22 +02:00
parent b1dad89214
commit d534e57ad2
14 changed files with 518 additions and 238 deletions
+32 -8
View File
@@ -1,4 +1,5 @@
#include <chrono>
#include <SDL_vulkan.h>
#include <thread>
#include <destrum/App.h>
@@ -66,6 +67,8 @@ void App::run() {
InputManager::GetInstance().BeginFrame();
camera.Update(dt);
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
@@ -76,8 +79,11 @@ void App::run() {
switch (event.window.event) {
case SDL_WINDOWEVENT_SIZE_CHANGED:
case SDL_WINDOWEVENT_RESIZED:
m_params.windowSize = { event.window.data1, event.window.data2 };
{
resizePending = true;
lastResizeTime = std::chrono::steady_clock::now();
break;
}
}
}
if (InputManager::GetInstance().ProcessEvent(event)) {
@@ -104,15 +110,33 @@ void App::run() {
const float alpha = accumulator / fixedDt;
if (!gfxDevice.needsSwapchainRecreate()) {
customDraw();
if (gfxDevice.needsSwapchainRecreate() || resizePending) {
auto now = std::chrono::steady_clock::now();
if (resizePending &&
now - lastResizeTime < std::chrono::milliseconds(100)) {
continue;
}
int w = 0;
int h = 0;
SDL_Vulkan_GetDrawableSize(window, &w, &h);
if (w == 0 || h == 0) {
continue;
}
spdlog::info("Recreating swapchain to size: {}x{}", w, h);
gfxDevice.recreateSwapchain(w, h);
onWindowResize(w, h);
resizePending = false;
continue;
}
if (gfxDevice.needsSwapchainRecreate()) {
spdlog::info("Recreating swapchain to size: {}x{}", m_params.windowSize.x, m_params.windowSize.y);
gfxDevice.recreateSwapchain(m_params.windowSize.x, m_params.windowSize.y);
onWindowResize(m_params.windowSize.x, m_params.windowSize.y);
}
customDraw();
if (frameLimit) {
auto sleepTime = Time::GetInstance().SleepDuration();
+4 -1
View File
@@ -45,7 +45,8 @@ void GfxDevice::init(SDL_Window* window, const std::string& appName, bool vSync)
.imageCubeArray = VK_TRUE,
.geometryShader = VK_TRUE, // for im3d
.depthClamp = VK_TRUE,
.samplerAnisotropy = VK_TRUE,
.fillModeNonSolid = VK_TRUE,
.samplerAnisotropy = VK_TRUE
};
constexpr auto features12 = VkPhysicalDeviceVulkan12Features{
@@ -63,11 +64,13 @@ void GfxDevice::init(SDL_Window* window, const std::string& appName, bool vSync)
.dynamicRendering = true,
};
physicalDevice = vkb::PhysicalDeviceSelector{instance}
.set_minimum_version(1, 3)
.set_required_features(deviceFeatures)
.set_required_features_12(features12)
.set_required_features_13(features13)
.add_required_extension(VK_EXT_EXTENDED_DYNAMIC_STATE_3_EXTENSION_NAME)
.set_surface(surface)
.prefer_gpu_device_type(vkb::PreferredDeviceType::discrete)
.select()
+1 -1
View File
@@ -89,7 +89,7 @@ void Pipeline::DefaultPipelineConfigInfo(PipelineConfigInfo& configInfo) {
configInfo.depthStencilInfo.front = {}; // Optional
configInfo.depthStencilInfo.back = {}; // Optional
configInfo.dynamicStateEnables = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR};
configInfo.dynamicStateEnables = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR, VK_DYNAMIC_STATE_POLYGON_MODE_EXT};
configInfo.dynamicStateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
configInfo.dynamicStateInfo.pDynamicStates = configInfo.dynamicStateEnables.data();
configInfo.dynamicStateInfo.dynamicStateCount = static_cast<uint32_t>(configInfo.dynamicStateEnables.size());
@@ -89,6 +89,8 @@ void MeshPipeline::draw(VkCommandBuffer cmd,
};
vkCmdSetScissor(cmd, 0, 1, &scissor);
vkCmdSetPolygonModeEXT(cmd, m_renderWireframe ? VK_POLYGON_MODE_LINE : VK_POLYGON_MODE_FILL);
auto prevMeshId = NULL_MESH_ID;
const auto frustum = edge::createFrustumFromCamera(camera);
@@ -81,6 +81,8 @@ void SkyboxPipeline::draw(VkCommandBuffer cmd, GfxDevice& gfxDevice, const Camer
skyboxRotation = glm::rotate(skyboxRotation, rotationSpeed * static_cast<float>(Time::GetInstance().DeltaTime()), glm::vec3(0.f, 1.f, 0.f));
pipeline->bind(cmd);
vkCmdSetPolygonModeEXT(cmd, VK_POLYGON_MODE_FILL);
gfxDevice.bindBindlessDescSet(cmd, pipelineLayout);
const glm::mat3 r = glm::mat3(skyboxRotation);
+56 -18
View File
@@ -65,40 +65,72 @@ void Swapchain::createSwapchain(GfxDevice* gfxDevice, VkFormat format, std::uint
extent = m_swapchain.extent;
}
void Swapchain::recreateSwapchain(const GfxDevice& gfxDevice, VkFormat format, std::uint32_t width, std::uint32_t height, bool vSync) {
assert(m_swapchain);
void Swapchain::recreateSwapchain(
const GfxDevice& gfxDevice,
VkFormat format,
std::uint32_t width,
std::uint32_t height,
bool vSync)
{
if (width == 0 || height == 0) {
dirty = true;
return;
}
VkDevice device = gfxDevice.getDevice();
vkDeviceWaitIdle(device);
auto oldSwapchain = m_swapchain;
auto res = vkb::SwapchainBuilder{gfxDevice.getVkbDevice()}
.set_old_swapchain(oldSwapchain)
.set_desired_format(VkSurfaceFormatKHR{
.format = format,
.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR,
})
.add_image_usage_flags(VK_IMAGE_USAGE_TRANSFER_DST_BIT)
.set_desired_present_mode(
vSync ? VK_PRESENT_MODE_FIFO_KHR : VK_PRESENT_MODE_IMMEDIATE_KHR)
.set_desired_extent(width, height)
.build();
assert(format == VK_FORMAT_B8G8R8A8_SRGB && "TODO: test other formats");
auto res = vkb::SwapchainBuilder{gfxDevice.getDevice()}
.set_old_swapchain(m_swapchain)
.set_desired_format(VkSurfaceFormatKHR{
.format = format,
.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR,
})
.add_image_usage_flags(VK_IMAGE_USAGE_TRANSFER_DST_BIT)
.set_desired_present_mode(
vSync ? VK_PRESENT_MODE_FIFO_KHR : VK_PRESENT_MODE_IMMEDIATE_KHR)
.set_desired_extent(width, height)
.build();
if (!res.has_value()) {
throw std::runtime_error(std::format(
"failed to create swapchain: error = {}, vk result = {}",
res.full_error().type.message(),
string_VkResult(res.full_error().vk_result)));
}
vkb::destroy_swapchain(m_swapchain);
for (auto imageView: imageViews) {
vkDestroyImageView(m_gfxDevice->getDevice(), imageView, nullptr);
for (auto sem : imageRenderSemaphores) {
vkDestroySemaphore(device, sem, nullptr);
}
imageRenderSemaphores.clear();
for (auto imageView : imageViews) {
vkDestroyImageView(device, imageView, nullptr);
}
imageViews.clear();
vkb::destroy_swapchain(oldSwapchain);
m_swapchain = res.value();
images = m_swapchain.get_images().value();
imageViews = m_swapchain.get_image_views().value();
dirty = false;
VkSemaphoreCreateInfo sci{
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
};
imageRenderSemaphores.resize(images.size());
for (auto& sem : imageRenderSemaphores) {
VK_CHECK(vkCreateSemaphore(device, &sci, nullptr, &sem));
}
extent = m_swapchain.extent;
dirty = false;
}
void Swapchain::cleanup() {
@@ -126,6 +158,12 @@ void Swapchain::resetFences(int index) const {
VK_CHECK(vkResetFences(m_gfxDevice->getDevice(), 1, &frame.renderFence));
}
struct SwapchainAcquireResult {
VkResult result = VK_SUCCESS;
VkImage image = VK_NULL_HANDLE;
uint32_t imageIndex = 0;
};
std::pair<VkImage, int> Swapchain::acquireNextImage(int index) {
std::uint32_t swapchainImageIndex{};
const auto result = vkAcquireNextImageKHR(