Merge pull request 'Some VSync fixes and improvements' (#80) from vsync_fixes into master

Reviewed-on: https://git.madvoxel.net/OpenVulkano/OpenVulkano/pulls/80
Reviewed-by: Georg Hagen <georg.hagen@madvoxel.com>
This commit is contained in:
Oleksii_Hyzha
2024-07-24 01:15:39 +02:00
9 changed files with 65 additions and 36 deletions

View File

@@ -21,6 +21,9 @@ namespace OpenVulkano
const char* FRAMEBUFFER_CLEAR_COLOR_STR = "FramebufferClearColor";
const char* PREFER_FRAMEBUFFER_FORMAT_SRGB_STR = "PreferFramebufferFormatSRGB";
const char* LAZY_RENDERING_STR = "LazyRendering";
const char* VSYNC_STR = "VSync";
const char* FPSCAP_STR = "FpsCap";
const char* PREFERRED_IMAGE_COUNT_STR = "PreferredImageCount";
}
EngineConfiguration::EngineConfiguration()
@@ -56,6 +59,16 @@ namespace OpenVulkano
{
root[LAZY_RENDERING_STR] >> m_lazyRendering;
}
if (root.has_child(VSYNC_STR)) { root[VSYNC_STR] >> m_vSync; }
if (root.has_child(FPSCAP_STR) && root[FPSCAP_STR].val().is_integer())
{
root[FPSCAP_STR] >> m_fpsCap;
}
if (root.has_child(PREFERRED_IMAGE_COUNT_STR) && root[PREFERRED_IMAGE_COUNT_STR].val().is_integer())
{
root[PREFERRED_IMAGE_COUNT_STR] >> m_preferredImageCount;
}
}
EngineConfiguration* EngineConfiguration::GetEngineConfiguration()

View File

@@ -15,16 +15,7 @@ namespace OpenVulkano
{
class EngineConfiguration
{
private:
EngineConfiguration();
~EngineConfiguration() = default;
uint32_t m_numThreads = 1;
std::array<float, 4> m_frameBufferClearColor = { 0.39f, 0.58f, 0.93f, 1.0f };
bool m_preferFramebufferFormatSRGB = true;
bool m_lazyRendering = false;
public:
public:
[[nodiscard]] static EngineConfiguration* GetEngineConfiguration();
void SetNumThreads(uint32_t numThreads) { m_numThreads = numThreads; }
@@ -38,5 +29,31 @@ namespace OpenVulkano
[[nodiscard]] bool GetLazyRendering() const { return m_lazyRendering; }
void SetLazyRendering(bool lazyRender) { m_lazyRendering = lazyRender; }
[[nodiscard]] bool GetVSync() const { return m_vSync; }
void SetVSync(bool vSync) { m_vSync = vSync; }
[[nodiscard]] int32_t GetFpsCap() const { return m_fpsCap; }
void SetFpsCap(int32_t fpsCap) { m_fpsCap = fpsCap; }
[[nodiscard]] uint32_t GetPrefferedSwapChainImageCount() const { return m_preferredImageCount; }
void SetPrefferedSwapChainImageCount(uint32_t preferredImageCount) { m_preferredImageCount = preferredImageCount; }
private:
EngineConfiguration();
~EngineConfiguration() = default;
uint32_t m_numThreads = 1;
std::array<float, 4> m_frameBufferClearColor = { 0.39f, 0.58f, 0.93f, 1.0f };
bool m_preferFramebufferFormatSRGB = true;
bool m_lazyRendering = false;
bool m_vSync = false;
int32_t m_fpsCap = -1; // -1 = no fps cap. 0 = fps cap if vsync, no cap otherwise. > 0 = set fps cap
#ifdef __APPLE__
uint32_t m_preferredImageCount = 3;
#else
uint32_t m_preferredImageCount = 2;
#endif
};
}

View File

@@ -152,8 +152,9 @@ namespace OpenVulkano::Vulkan
{
ManagedBuffer::Ptr vertexBuffer =
CreateDeviceOnlyBufferWithData(sizeof(Vertex) * geometry->GetVertexCount(), vk::BufferUsageFlagBits::eVertexBuffer, geometry->GetVertices());
ManagedBuffer::Ptr indexBuffer =
CreateDeviceOnlyBufferWithData(Utils::EnumAsInt(geometry->indexType) * geometry->GetIndexCount(), vk::BufferUsageFlagBits::eIndexBuffer, geometry->GetIndices());
ManagedBuffer::Ptr indexBuffer;
if (geometry->GetIndexCount())
indexBuffer = CreateDeviceOnlyBufferWithData(Utils::EnumAsInt(geometry->indexType) * geometry->GetIndexCount(), vk::BufferUsageFlagBits::eIndexBuffer, geometry->GetIndices());
VulkanGeometry* vkGeo = new VulkanGeometry(geometry, vertexBuffer, indexBuffer);
geometries.emplace_back(vkGeo);
geometry->renderGeo = vkGeo;

View File

@@ -47,7 +47,7 @@ namespace OpenVulkano::Vulkan
void RecordBind(vk::CommandBuffer& cmdBuffer)
{
cmdBuffer.bindVertexBuffers(0, 1, &m_vertexBuffer->buffer, &m_offsets);
cmdBuffer.bindIndexBuffer(m_indexBuffer->buffer, 0, m_indexType);
if (m_indexBuffer) { cmdBuffer.bindIndexBuffer(m_indexBuffer->buffer, 0, m_indexType); }
}
void RecordDraw(vk::CommandBuffer& cmdBuffer)

View File

@@ -81,10 +81,13 @@ namespace OpenVulkano::Vulkan
{ preTransform = vk::SurfaceTransformFlagBitsKHR::eIdentity; }
else { preTransform = surfaceCapabilities.currentTransform; }
uint32_t usingImages = std::max(preferredImageCount, surfaceCapabilities.minImageCount);
EngineConfiguration* config = EngineConfiguration::GetEngineConfiguration();
uint32_t usingImages = std::max(config->GetPrefferedSwapChainImageCount(), surfaceCapabilities.minImageCount);
if (surfaceCapabilities.maxImageCount > 0) //GPU has limit of swap chain images
usingImages = std::min(usingImages, surfaceCapabilities.maxImageCount);
Logger::RENDER->debug("GPU supports {0} to {1} swap chain images. Preferred: {2}, Using: {3}", surfaceCapabilities.minImageCount, surfaceCapabilities.maxImageCount, preferredImageCount, usingImages);
Logger::RENDER->debug("GPU supports {0} to {1} swap chain images. Preferred: {2}, Using: {3}",
surfaceCapabilities.minImageCount, surfaceCapabilities.maxImageCount,
config->GetPrefferedSwapChainImageCount(), usingImages);
const vk::SwapchainCreateInfoKHR createInfo({}, surface, usingImages, surfaceFormat.format,
surfaceFormat.colorSpace, size, 1,
@@ -151,6 +154,9 @@ namespace OpenVulkano::Vulkan
vk::PresentModeKHR SwapChain::ChosePresentMode()
{
std::vector<vk::PresentModeKHR> presentModes = device->physicalDevice.getSurfacePresentModesKHR(surface);
EngineConfiguration* config = EngineConfiguration::GetEngineConfiguration();
const bool useVSync = config->GetVSync();
const int32_t fpsCap = config->GetFpsCap();
#ifdef DEBUG
std::string availableModes = "";
for (const auto& presentMode : presentModes)
@@ -158,12 +164,13 @@ namespace OpenVulkano::Vulkan
if (availableModes.length() > 0) availableModes += ", ";
availableModes += vk::to_string(presentMode);
}
Logger::RENDER->debug("Available swap chain present modes {0}. Searching best mode for: vsync={1}", availableModes, useVsync);
Logger::RENDER->debug("Available swap chain present modes {0}. Searching best mode for: vsync={1}", availableModes, useVSync);
#endif
vk::PresentModeKHR mode = vk::PresentModeKHR::eFifo;
if (useVsync)
if (useVSync)
{
if (Utils::Contains(presentModes, vk::PresentModeKHR::eMailbox)) mode = vk::PresentModeKHR::eMailbox;
if (fpsCap != 0 && Utils::Contains(presentModes, vk::PresentModeKHR::eMailbox))
mode = vk::PresentModeKHR::eMailbox;
}
else
{

View File

@@ -10,6 +10,7 @@
#include "Image.hpp"
#include "FrameBuffer.hpp"
#include "Base/UI/IWindow.hpp"
#include "Base/EngineConfiguration.hpp"
#include "Base/Logger.hpp"
#include <vulkan/vulkan.hpp>
@@ -46,13 +47,6 @@ namespace OpenVulkano
vk::Rect2D fullscreenScissor;
std::vector<vk::Semaphore> imageAvailableSemaphores;
uint32_t currentSemaphoreId = 0;
bool useVsync = false;
#ifdef __APPLE__
uint32_t preferredImageCount = 3; //TODO add option
#else
uint32_t preferredImageCount = 2; //TODO add option
#endif
vk::Extent2D size{0,0};
public:
@@ -99,13 +93,6 @@ namespace OpenVulkano
}
}
[[nodiscard]] bool UseVsync() const { return useVsync; }
void SetVsync(bool vsync)
{ //TODO change swap chain
this->useVsync = vsync;
}
[[nodiscard]] uint32_t GetImageCount() const
{
return images.size();