OpenGLRenderer: Fix FboFramebuffer making context switches

When a window was created and rendered onto, FBO rendering was made on the device reference context which required a context switch.

This has been fixed with OpenGLFboFramebuffer managing a per-context framebuffer and creating one when needed
This commit is contained in:
SirLynix
2023-02-26 13:43:21 +01:00
parent 2e8ea0e887
commit 421e684344
5 changed files with 115 additions and 50 deletions

View File

@@ -9,9 +9,11 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/OpenGLRenderer/OpenGLFramebuffer.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Framebuffer.hpp>
#include <memory>
#include <vector>
#include <unordered_map>
namespace Nz
{
@@ -22,14 +24,14 @@ namespace Nz
class NAZARA_OPENGLRENDERER_API OpenGLFboFramebuffer final : public OpenGLFramebuffer
{
public:
OpenGLFboFramebuffer(OpenGLDevice& device, const std::vector<std::shared_ptr<Texture>>& attachments);
OpenGLFboFramebuffer(OpenGLDevice& device, std::vector<std::shared_ptr<Texture>> attachments);
OpenGLFboFramebuffer(const OpenGLFboFramebuffer&) = delete;
OpenGLFboFramebuffer(OpenGLFboFramebuffer&&) noexcept = default;
OpenGLFboFramebuffer(OpenGLFboFramebuffer&&) = delete;
~OpenGLFboFramebuffer() = default;
void Activate() const override;
inline const Vector2ui& GetAttachmentSize(std::size_t i) const;
inline Vector2ui GetAttachmentSize(std::size_t i) const;
std::size_t GetColorBufferCount() const override;
@@ -41,9 +43,20 @@ namespace Nz
OpenGLFboFramebuffer& operator=(OpenGLFboFramebuffer&&) = delete;
private:
GL::Framebuffer m_framebuffer;
GL::Framebuffer& CreateFramebuffer(const GL::Context& context) const;
struct ContextFramebuffer
{
GL::Framebuffer framebuffer;
NazaraSlot(GL::Context, OnContextDestruction, onContextDestruction);
};
std::size_t m_colorAttachmentCount;
std::vector<Vector2ui> m_attachmentSizes;
std::string m_debugName;
std::vector<std::shared_ptr<Texture>> m_attachments;
mutable std::unordered_map<const GL::Context*, ContextFramebuffer> m_framebuffers;
OpenGLDevice& m_device;
Vector2ui m_size;
};
}

View File

@@ -7,9 +7,9 @@
namespace Nz
{
inline const Vector2ui& OpenGLFboFramebuffer::GetAttachmentSize(std::size_t i) const
inline Vector2ui OpenGLFboFramebuffer::GetAttachmentSize(std::size_t i) const
{
return m_attachmentSizes[i];
return Vector2ui(m_attachments[i]->GetSize());
}
}

View File

@@ -199,6 +199,8 @@ namespace Nz::GL
static const Context* GetCurrentContext();
static bool SetCurrentContext(const Context* context);
NazaraSignal(OnContextDestruction, Context* /*context*/);
protected:
virtual bool Activate() const = 0;
virtual void Desactivate() const = 0;