| 1 |
#pragma once |
| 2 |
|
| 3 |
#ifndef DOXYGEN_GENERATING_OUTPUT |
| 4 |
#define NCINE_INCLUDE_OPENGL |
| 5 |
#include "../../../CommonHeaders.h" |
| 6 |
#endif |
| 7 |
|
| 8 |
#include <memory> |
| 9 |
|
| 10 |
#include <Containers/SmallVector.h> |
| 11 |
|
| 12 |
using namespace Death::Containers; |
| 13 |
|
| 14 |
namespace nCine::RHI::GL |
| 15 |
{ |
| 16 |
class GLRenderbuffer; |
| 17 |
class GLTexture; |
| 18 |
|
| 19 |
/** |
| 20 |
@brief Wraps an OpenGL framebuffer object |
| 21 |
|
| 22 |
Manages the lifetime of a single OpenGL framebuffer object (FBO) and its attachments. Renderbuffers |
| 23 |
are owned by the framebuffer and attached or detached as color, depth or stencil targets; textures |
| 24 |
can be attached as well. Binding is cached separately for the read and draw targets so that redundant |
| 25 |
`glBindFramebuffer()` calls are skipped. Also configures the draw buffers and checks completeness. |
| 26 |
*/ |
| 27 |
class GLFramebuffer |
| 28 |
{ |
| 29 |
public: |
| 30 |
/** @brief Maximum number of color draw buffers */ |
| 31 |
static const std::uint32_t MaxDrawbuffers = 8; |
| 32 |
/** @brief Maximum number of attached renderbuffers */ |
| 33 |
static const std::uint32_t MaxRenderbuffers = 4; |
| 34 |
|
| 35 |
explicit GLFramebuffer(); |
| 36 |
~GLFramebuffer(); |
| 37 |
|
| 38 |
GLFramebuffer(const GLFramebuffer&) = delete; |
| 39 |
GLFramebuffer& operator=(const GLFramebuffer&) = delete; |
| 40 |
|
| 41 |
/** @brief Returns the OpenGL handle of the framebuffer object */ |
| 42 |
inline GLuint GetGLHandle() const { |
| 43 |
return _glHandle; |
| 44 |
} |
| 45 |
|
| 46 |
/** @brief Binds the framebuffer to both the read and draw targets */ |
| 47 |
bool Bind() const; |
| 48 |
/** @brief Unbinds any framebuffer from both the read and draw targets */ |
| 49 |
static bool Unbind(); |
| 50 |
|
| 51 |
/** @brief Binds the framebuffer to the specified target (read, draw or both) */ |
| 52 |
bool Bind(GLenum target) const; |
| 53 |
/** @brief Unbinds any framebuffer from the specified target (read, draw or both) */ |
| 54 |
static bool Unbind(GLenum target); |
| 55 |
|
| 56 |
/** @brief Sets the handle @ref Unbind() binds as the "default" framebuffer — non-zero when the |
| 57 |
screen is a framebuffer owned by a host (e.g. the libretro frontend's render FBO) */ |
| 58 |
static void SetDefaultHandle(GLuint handle) { |
| 59 |
_defaultHandle = handle; |
| 60 |
} |
| 61 |
/** @brief Drops the cached read/draw bindings so the next bind re-applies (external code changed them) */ |
| 62 |
static void InvalidateCachedBindings() { |
| 63 |
_readBoundBuffer = ~0u; |
| 64 |
_drawBoundBuffer = ~0u; |
| 65 |
} |
| 66 |
|
| 67 |
/** @brief Returns the number of active color draw buffers */ |
| 68 |
inline std::uint32_t GetDrawbufferCount() const { return _numDrawBuffers; } |
| 69 |
/** @brief Sets the number of color attachments enabled as draw buffers */ |
| 70 |
bool DrawBuffers(std::uint32_t numDrawBuffers); |
| 71 |
|
| 72 |
/** @brief Returns the number of attached renderbuffers */ |
| 73 |
inline std::uint32_t GetRenderbufferCount() const { return std::uint32_t(_attachedRenderbuffers.size()); } |
| 74 |
/** @brief Creates a renderbuffer with a debug label and attaches it to the given attachment point */ |
| 75 |
bool AttachRenderbuffer(const char *label, GLenum internalFormat, GLsizei width, GLsizei height, GLenum attachment); |
| 76 |
/** @brief Creates a renderbuffer and attaches it to the given attachment point */ |
| 77 |
bool AttachRenderbuffer(GLenum internalFormat, GLsizei width, GLsizei height, GLenum attachment); |
| 78 |
/** @brief Detaches and destroys the renderbuffer at the given attachment point */ |
| 79 |
bool DetachRenderbuffer(GLenum internalFormat); |
| 80 |
|
| 81 |
/** @brief Attaches a texture to the given attachment point */ |
| 82 |
void AttachTexture(GLTexture& texture, GLenum attachment); |
| 83 |
/** @brief Detaches any texture from the given attachment point */ |
| 84 |
void DetachTexture(GLenum attachment); |
| 85 |
#if !(defined(DEATH_TARGET_APPLE) && defined(DEATH_TARGET_ARM)) |
| 86 |
/** @brief Hints that the contents of the listed attachments are no longer needed */ |
| 87 |
void Invalidate(GLsizei numAttachments, const GLenum* attachments); |
| 88 |
#endif |
| 89 |
|
| 90 |
/** @brief Returns `true` if the framebuffer is complete and ready for rendering */ |
| 91 |
bool IsStatusComplete(); |
| 92 |
|
| 93 |
/** @brief Sets an OpenGL object label for the framebuffer, for debugging */ |
| 94 |
void SetObjectLabel(StringView label); |
| 95 |
|
| 96 |
private: |
| 97 |
static std::uint32_t _readBoundBuffer; |
| 98 |
static std::uint32_t _drawBoundBuffer; |
| 99 |
static GLuint _defaultHandle; |
| 100 |
|
| 101 |
std::uint32_t _numDrawBuffers; |
| 102 |
SmallVector<std::unique_ptr<GLRenderbuffer>, MaxRenderbuffers> _attachedRenderbuffers; |
| 103 |
GLuint _glHandle; |
| 104 |
|
| 105 |
static bool BindHandle(GLenum target, GLuint glHandle); |
| 106 |
static GLuint GetBoundHandle(GLenum target); |
| 107 |
static void SetBoundHandle(GLenum target, GLuint glHandle); |
| 108 |
}; |
| 109 |
} |