From ccf77ac4596ac7db0df978f0c5b1fbf71eb9001b Mon Sep 17 00:00:00 2001 From: SirLynix Date: Tue, 6 Dec 2022 07:49:41 +0100 Subject: [PATCH] OpenGLRenderer: Setup draw buffers only once --- .../Nazara/OpenGLRenderer/Wrapper/Framebuffer.hpp | 1 + .../Nazara/OpenGLRenderer/Wrapper/Framebuffer.inl | 9 +++++++++ src/Nazara/OpenGLRenderer/OpenGLCommandBuffer.cpp | 8 +------- .../OpenGLRenderer/OpenGLFboFramebuffer.cpp | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/include/Nazara/OpenGLRenderer/Wrapper/Framebuffer.hpp b/include/Nazara/OpenGLRenderer/Wrapper/Framebuffer.hpp index cb6f6e2b9..c59e639e8 100644 --- a/include/Nazara/OpenGLRenderer/Wrapper/Framebuffer.hpp +++ b/include/Nazara/OpenGLRenderer/Wrapper/Framebuffer.hpp @@ -24,6 +24,7 @@ namespace Nz::GL inline GLenum Check() const; + inline void DrawBuffers(GLsizei n, const GLenum* bufs); inline void Renderbuffer(GLenum attachment, GLenum renderbuffer); inline void Texture2D(GLenum attachment, GLenum textarget, GLuint texture, GLint level = 0); diff --git a/include/Nazara/OpenGLRenderer/Wrapper/Framebuffer.inl b/include/Nazara/OpenGLRenderer/Wrapper/Framebuffer.inl index be740fe01..171f3eab8 100644 --- a/include/Nazara/OpenGLRenderer/Wrapper/Framebuffer.inl +++ b/include/Nazara/OpenGLRenderer/Wrapper/Framebuffer.inl @@ -17,6 +17,15 @@ namespace Nz::GL return context.glCheckFramebufferStatus(target); } + inline void Framebuffer::DrawBuffers(GLsizei n, const GLenum* bufs) + { + assert(m_objectId); + + const Context& context = EnsureContext(); + context.BindFramebuffer(FramebufferTarget::Draw, m_objectId); + context.glDrawBuffers(n, bufs); + } + inline void Framebuffer::Renderbuffer(GLenum attachment, GLenum renderbuffer) { assert(m_objectId); diff --git a/src/Nazara/OpenGLRenderer/OpenGLCommandBuffer.cpp b/src/Nazara/OpenGLRenderer/OpenGLCommandBuffer.cpp index 8ae6db26d..16a88089c 100644 --- a/src/Nazara/OpenGLRenderer/OpenGLCommandBuffer.cpp +++ b/src/Nazara/OpenGLRenderer/OpenGLCommandBuffer.cpp @@ -61,10 +61,6 @@ namespace Nz { const GL::Context* context = GL::Context::GetCurrentContext(); - StackArray fboDrawBuffers = NazaraStackArrayNoInit(GLenum, m_maxColorBufferCount); - for (std::size_t i = 0; i < m_maxColorBufferCount; ++i) - fboDrawBuffers[i] = GLenum(GL_COLOR_ATTACHMENT0 + i); - StackArray colorIndexes = NazaraStackArrayNoInit(std::size_t, m_maxColorBufferCount); for (const auto& commandVariant : m_commands) @@ -129,7 +125,7 @@ namespace Nz context = GL::Context::GetCurrentContext(); std::size_t colorBufferCount = command.framebuffer->GetColorBufferCount(); - assert(colorBufferCount <= fboDrawBuffers.size()); + assert(colorBufferCount <= colorIndexes.size()); colorIndexes.fill(0); std::size_t colorIndex = 0; @@ -175,8 +171,6 @@ namespace Nz { const OpenGLFboFramebuffer& fboFramebuffer = static_cast(*command.framebuffer); - context->glDrawBuffers(GLsizei(colorBufferCount), fboDrawBuffers.data()); - invalidateAttachments = NazaraStackVector(GLenum, colorBufferCount + 1); for (std::size_t i = 0; i < colorBufferCount; ++i) diff --git a/src/Nazara/OpenGLRenderer/OpenGLFboFramebuffer.cpp b/src/Nazara/OpenGLRenderer/OpenGLFboFramebuffer.cpp index 7a39bd502..c99e5250e 100644 --- a/src/Nazara/OpenGLRenderer/OpenGLFboFramebuffer.cpp +++ b/src/Nazara/OpenGLRenderer/OpenGLFboFramebuffer.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -85,6 +86,20 @@ namespace Nz throw std::runtime_error("invalid framebuffer: 0x" + NumberToString(status, 16)); m_colorAttachmentCount = colorAttachmentCount; + + if (m_colorAttachmentCount > 0) + { + StackArray fboDrawBuffers = NazaraStackArrayNoInit(GLenum, m_colorAttachmentCount); + for (std::size_t i = 0; i < m_colorAttachmentCount; ++i) + fboDrawBuffers[i] = GLenum(GL_COLOR_ATTACHMENT0 + i); + + m_framebuffer.DrawBuffers(SafeCast(m_colorAttachmentCount), fboDrawBuffers.data()); + } + else + { + GLenum buffer = GL_NONE; + m_framebuffer.DrawBuffers(1, &buffer); + } } void OpenGLFboFramebuffer::Activate() const