Renderer: Expose present mode and allow to query/set it

This commit is contained in:
SirLynix
2023-08-13 18:16:03 +02:00
parent 28d69ab552
commit e8620894f7
21 changed files with 420 additions and 43 deletions

View File

@@ -34,13 +34,17 @@ namespace Nz
inline GL::Context& GetContext();
const OpenGLFramebuffer& GetFramebuffer(std::size_t i) const override;
std::size_t GetFramebufferCount() const override;
PresentMode GetPresentMode() const override;
const OpenGLRenderPass& GetRenderPass() const override;
const Vector2ui& GetSize() const override;
PresentModeFlags GetSupportedPresentModes() const override;
void NotifyResize(const Vector2ui& newSize) override;
void Present();
void SetPresentMode(PresentMode presentMode) override;
TransientResources& Transient() override;
private:
@@ -49,6 +53,8 @@ namespace Nz
std::vector<std::unique_ptr<OpenGLRenderImage>> m_renderImage;
std::shared_ptr<GL::Context> m_context;
OpenGLWindowFramebuffer m_framebuffer;
PresentMode m_presentMode;
PresentModeFlags m_supportedPresentModes;
Vector2ui m_size;
bool m_sizeInvalidated;
};

View File

@@ -148,8 +148,6 @@ namespace Nz::GL
inline bool DidLastCallSucceed() const;
virtual void EnableVerticalSync(bool enabled) = 0;
inline bool GetBoolean(GLenum name) const;
inline bool GetBoolean(GLenum name, GLuint index) const;
inline const OpenGLDevice* GetDevice() const;
@@ -159,6 +157,7 @@ namespace Nz::GL
template<typename T> T GetInteger(GLenum name) const;
template<typename T> T GetInteger(GLenum name, GLuint index) const;
inline const ContextParams& GetParams() const;
virtual PresentModeFlags GetSupportedPresentModes() const = 0;
inline const OpenGLVaoCache& GetVaoCache() const;
inline bool IsExtensionSupported(Extension extension) const;
@@ -183,6 +182,7 @@ namespace Nz::GL
inline void ResetStencilWriteMasks() const;
void SetCurrentTextureUnit(UInt32 textureUnit) const;
virtual void SetPresentMode(PresentMode presentMode) = 0;
void SetScissorBox(GLint x, GLint y, GLsizei width, GLsizei height) const;
void SetViewport(GLint x, GLint y, GLsizei width, GLsizei height) const;

View File

@@ -35,10 +35,12 @@ namespace Nz::GL
virtual bool Create(const ContextParams& params, WindowHandle window, const EGLContextBase* shareContext = nullptr);
virtual void Destroy();
void EnableVerticalSync(bool enabled) override;
PresentModeFlags GetSupportedPresentModes() const override;
inline bool HasPlatformExtension(const std::string& str) const;
void SetPresentMode(PresentMode presentMode) override;
void SwapBuffers() override;
EGLContextBase& operator=(const EGLContextBase&) = delete;
@@ -72,6 +74,8 @@ namespace Nz::GL
std::unordered_set<std::string> m_supportedPlatformExtensions;
EGLContext m_handle;
EGLint m_maxSwapInterval;
EGLint m_minSwapInterval;
bool m_ownsDisplay;
};
}

View File

@@ -17,6 +17,7 @@ NAZARA_OPENGLRENDERER_EGL_FUNC(eglCreatePbufferSurface, PFNEGLCREATEPBUFFERSURFA
NAZARA_OPENGLRENDERER_EGL_FUNC(eglCreateWindowSurface, PFNEGLCREATEWINDOWSURFACEPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglDestroyContext, PFNEGLDESTROYCONTEXTPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglDestroySurface, PFNEGLDESTROYSURFACEPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglGetConfigAttrib, PFNEGLGETCONFIGATTRIBPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglGetDisplay, PFNEGLGETDISPLAYPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglGetError, PFNEGLGETERRORPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglGetProcAddress, PFNEGLGETPROCADDRESSPROC)
@@ -24,6 +25,7 @@ NAZARA_OPENGLRENDERER_EGL_FUNC(eglInitialize, PFNEGLINITIALIZEPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglMakeCurrent, PFNEGLMAKECURRENTPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglQueryString, PFNEGLQUERYSTRINGPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglSwapBuffers, PFNEGLSWAPBUFFERSPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglSwapInterval, PFNEGLSWAPINTERVALPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglTerminate, PFNEGLTERMINATEPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC_OPT(eglCreatePlatformWindowSurface, PFNEGLCREATEPLATFORMWINDOWSURFACEPROC)

View File

@@ -34,10 +34,12 @@ namespace Nz::GL
bool Create(const WGLContext* baseContext, const ContextParams& params, WindowHandle window, const WGLContext* shareContext = nullptr);
void Destroy();
void EnableVerticalSync(bool enabled) override;
PresentModeFlags GetSupportedPresentModes() const override;
inline bool HasPlatformExtension(const std::string& str) const;
void SetPresentMode(PresentMode presentMode) override;
void SwapBuffers() override;
WGLContext& operator=(const WGLContext&) = delete;

View File

@@ -6,7 +6,7 @@
namespace Nz::GL
{
inline GL::WGLContext::WGLContext(const OpenGLDevice* device, const WGLLoader& loader) :
inline WGLContext::WGLContext(const OpenGLDevice* device, const WGLLoader& loader) :
Context(device),
m_loader(loader),
m_handle(nullptr)

View File

@@ -33,10 +33,12 @@ namespace Nz::GL
virtual bool Create(const ContextParams& params, WindowHandle window, const WebContext* shareContext = nullptr);
virtual void Destroy();
void EnableVerticalSync(bool enabled) override;
PresentModeFlags GetSupportedPresentModes() const override;
inline bool HasPlatformExtension(const std::string& str) const;
void SetPresentMode(PresentMode presentMode) override;
void SwapBuffers() override;
WebContext& operator=(const WebContext&) = delete;

View File

@@ -109,6 +109,24 @@ namespace Nz
using PipelineStageFlags = Flags<PipelineStage>;
enum class PresentMode
{
Immediate,
Mailbox,
RelaxedVerticalSync,
VerticalSync,
Max = VerticalSync
};
template<>
struct EnumAsFlags<PresentMode>
{
static constexpr PresentMode max = PresentMode::Max;
};
using PresentModeFlags = Flags<PresentMode>;
enum class QueueType
{
Compute,

View File

@@ -32,8 +32,13 @@ namespace Nz
virtual std::shared_ptr<CommandPool> CreateCommandPool(QueueType queueType) = 0;
virtual PresentMode GetPresentMode() const = 0;
virtual PresentModeFlags GetSupportedPresentModes() const = 0;
virtual void NotifyResize(const Vector2ui& newSize) = 0;
virtual void SetPresentMode(PresentMode presentMode) = 0;
virtual TransientResources& Transient() = 0;
protected:

View File

@@ -8,6 +8,7 @@
#define NAZARA_RENDERER_SWAPCHAINPARAMETERS_HPP
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/Renderer/Enums.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
#include <vector>
@@ -15,8 +16,8 @@ namespace Nz
{
struct SwapchainParameters
{
std::vector<PixelFormat> depthFormats = {Nz::PixelFormat::Depth24Stencil8, Nz::PixelFormat::Depth32FStencil8, Nz::PixelFormat::Depth16Stencil8, Nz::PixelFormat::Depth32F, Nz::PixelFormat::Depth24}; //< By order of preference
bool verticalSync = false;
std::vector<PixelFormat> depthFormats = { Nz::PixelFormat::Depth24Stencil8, Nz::PixelFormat::Depth32FStencil8, Nz::PixelFormat::Depth16Stencil8, Nz::PixelFormat::Depth32F, Nz::PixelFormat::Depth24 }; //< By order of preference
std::vector<PresentMode> presentMode = { PresentMode::Mailbox, PresentMode::Immediate, PresentMode::RelaxedVerticalSync, PresentMode::VerticalSync }; //< By order of preference
};
}

View File

@@ -18,6 +18,7 @@
namespace Nz
{
inline std::optional<PixelFormat> FromVulkan(VkFormat format);
inline std::optional<PresentMode> FromVulkan(VkPresentModeKHR presentMode);
inline VkAttachmentLoadOp ToVulkan(AttachmentLoadOp loadOp);
inline VkAttachmentStoreOp ToVulkan(AttachmentStoreOp storeOp);
@@ -33,6 +34,7 @@ namespace Nz
inline VkPipelineStageFlags ToVulkan(PipelineStageFlags pipelineStages);
inline VkFormat ToVulkan(PixelFormat pixelFormat);
inline VkImageAspectFlags ToVulkan(PixelFormatContent pixelFormatContent);
inline VkPresentModeKHR ToVulkan(PresentMode presentMode);
inline VkPrimitiveTopology ToVulkan(PrimitiveMode primitiveMode);
inline VkCompareOp ToVulkan(RendererComparison comparison);
inline VkFilter ToVulkan(SamplerFilter samplerFilter);

View File

@@ -32,6 +32,20 @@ namespace Nz
return std::nullopt;
}
std::optional<PresentMode> FromVulkan(VkPresentModeKHR presentMode)
{
switch (presentMode)
{
case VK_PRESENT_MODE_IMMEDIATE_KHR: return PresentMode::Immediate;
case VK_PRESENT_MODE_MAILBOX_KHR: return PresentMode::Mailbox;
case VK_PRESENT_MODE_FIFO_KHR: return PresentMode::VerticalSync;
case VK_PRESENT_MODE_FIFO_RELAXED_KHR: return PresentMode::RelaxedVerticalSync;
default: break;
}
return std::nullopt;
}
inline VkAttachmentLoadOp ToVulkan(AttachmentLoadOp loadOp)
{
switch (loadOp)
@@ -306,6 +320,20 @@ namespace Nz
return VK_IMAGE_ASPECT_COLOR_BIT;
}
VkPresentModeKHR ToVulkan(PresentMode presentMode)
{
switch (presentMode)
{
case PresentMode::Immediate: return VK_PRESENT_MODE_IMMEDIATE_KHR;
case PresentMode::Mailbox: return VK_PRESENT_MODE_MAILBOX_KHR;
case PresentMode::RelaxedVerticalSync: return VK_PRESENT_MODE_FIFO_RELAXED_KHR;
case PresentMode::VerticalSync: return VK_PRESENT_MODE_FIFO_KHR;
}
NazaraError("Unhandled PresentMode 0x" + NumberToString(UnderlyingCast(presentMode), 16));
return VK_PRESENT_MODE_FIFO_KHR;
}
inline VkPrimitiveTopology ToVulkan(PrimitiveMode primitiveMode)
{
switch (primitiveMode)
@@ -318,7 +346,7 @@ namespace Nz
case PrimitiveMode::TriangleFan: return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN;
}
NazaraError("Unhandled FaceFilling 0x" + NumberToString(UnderlyingCast(primitiveMode), 16));
NazaraError("Unhandled PrimitiveMode 0x" + NumberToString(UnderlyingCast(primitiveMode), 16));
return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
}
@@ -502,3 +530,4 @@ namespace Nz
}
#include <Nazara/VulkanRenderer/DebugOff.hpp>
#include "Utils.hpp"

View File

@@ -53,12 +53,16 @@ namespace Nz
inline Vk::QueueHandle& GetGraphicsQueue();
const VulkanRenderPass& GetRenderPass() const override;
const Vector2ui& GetSize() const override;
PresentMode GetPresentMode() const override;
PresentModeFlags GetSupportedPresentModes() const override;
inline const Vk::Swapchain& GetSwapchain() const;
void NotifyResize(const Vector2ui& newSize) override;
void Present(UInt32 imageIndex, VkSemaphore waitSemaphore = VK_NULL_HANDLE);
void SetPresentMode(PresentMode presentMode) override;
TransientResources& Transient() override;
VulkanSwapchain& operator=(const VulkanSwapchain&) = delete;
@@ -85,6 +89,8 @@ namespace Nz
Vk::QueueHandle m_transferQueue;
Vk::Surface m_surface;
Vk::Swapchain m_swapchain;
PresentMode m_presentMode;
PresentModeFlags m_supportedPresentModes;
Vector2ui m_swapchainSize;
VkFormat m_depthStencilFormat;
VkSurfaceFormatKHR m_surfaceFormat;