| 1 |
#include "GLFramebuffer.h" |
| 2 |
#include "GLRenderbuffer.h" |
| 3 |
#include "GLTexture.h" |
| 4 |
#include "GLDebug.h" |
| 5 |
#include "../../../../Main.h" |
| 6 |
|
| 7 |
namespace nCine::RHI::GL |
| 8 |
{ |
| 9 |
std::uint32_t GLFramebuffer::_readBoundBuffer = 0; |
| 10 |
std::uint32_t GLFramebuffer::_drawBoundBuffer = 0; |
| 11 |
GLuint GLFramebuffer::_defaultHandle = 0; |
| 12 |
|
| 13 |
GLFramebuffer::GLFramebuffer() |
| 14 |
: _glHandle(0), _numDrawBuffers(0) |
| 15 |
{ |
| 16 |
glGenFramebuffers(1, &_glHandle); |
| 17 |
GL_LOG_ERRORS(); |
| 18 |
} |
| 19 |
|
| 20 |
GLFramebuffer::~GLFramebuffer() |
| 21 |
{ |
| 22 |
if (_readBoundBuffer == _glHandle) { |
| 23 |
Unbind(GL_READ_FRAMEBUFFER); |
| 24 |
} |
| 25 |
if (_drawBoundBuffer == _glHandle) { |
| 26 |
Unbind(GL_DRAW_FRAMEBUFFER); |
| 27 |
} |
| 28 |
glDeleteFramebuffers(1, &_glHandle); |
| 29 |
GL_LOG_ERRORS(); |
| 30 |
} |
| 31 |
|
| 32 |
bool GLFramebuffer::Bind() const |
| 33 |
{ |
| 34 |
return Bind(GL_FRAMEBUFFER); |
| 35 |
} |
| 36 |
|
| 37 |
bool GLFramebuffer::Unbind() |
| 38 |
{ |
| 39 |
return Unbind(GL_FRAMEBUFFER); |
| 40 |
} |
| 41 |
|
| 42 |
bool GLFramebuffer::Bind(GLenum target) const |
| 43 |
{ |
| 44 |
return BindHandle(target, _glHandle); |
| 45 |
} |
| 46 |
|
| 47 |
bool GLFramebuffer::Unbind(GLenum target) |
| 48 |
{ |
| 49 |
return BindHandle(target, _defaultHandle); |
| 50 |
} |
| 51 |
|
| 52 |
bool GLFramebuffer::DrawBuffers(std::uint32_t numDrawBuffers) |
| 53 |
{ |
| 54 |
#if defined(RHI_GL_PROFILE_ES2) |
| 55 |
// glDrawBuffers() is ES 3.0 and MRT does not exist on ES2 - a framebuffer always renders to its single |
| 56 |
// GL_COLOR_ATTACHMENT0, which is exactly what a draw-buffer count of 0/1 selects, so those are no-ops |
| 57 |
DEATH_ASSERT(numDrawBuffers <= 1, "Multiple render targets are not supported on OpenGL|ES 2.0", false); |
| 58 |
_numDrawBuffers = numDrawBuffers; |
| 59 |
return false; |
| 60 |
#else |
| 61 |
static const GLenum drawBuffers[MaxDrawbuffers] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3, |
| 62 |
GL_COLOR_ATTACHMENT4, GL_COLOR_ATTACHMENT5, GL_COLOR_ATTACHMENT6, GL_COLOR_ATTACHMENT7 }; |
| 63 |
if (numDrawBuffers < MaxDrawbuffers && _numDrawBuffers != numDrawBuffers) { |
| 64 |
glDrawBuffers(numDrawBuffers, drawBuffers); |
| 65 |
GL_LOG_ERRORS(); |
| 66 |
_numDrawBuffers = numDrawBuffers; |
| 67 |
return true; |
| 68 |
} |
| 69 |
return false; |
| 70 |
#endif |
| 71 |
} |
| 72 |
|
| 73 |
bool GLFramebuffer::AttachRenderbuffer(const char* label, GLenum internalFormat, GLsizei width, GLsizei height, GLenum attachment) |
| 74 |
{ |
| 75 |
if (_attachedRenderbuffers.size() >= MaxRenderbuffers - 1) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
for (std::uint32_t i = 0; i < _attachedRenderbuffers.size(); i++) { |
| 79 |
if (_attachedRenderbuffers[i]->GetAttachment() == attachment) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
std::unique_ptr<GLRenderbuffer>& buffer = _attachedRenderbuffers.emplace_back(std::make_unique<GLRenderbuffer>(internalFormat, width, height)); |
| 85 |
buffer->SetObjectLabel(label); |
| 86 |
buffer->SetAttachment(attachment); |
| 87 |
|
| 88 |
Bind(GL_FRAMEBUFFER); |
| 89 |
glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, buffer->_glHandle); |
| 90 |
GL_LOG_ERRORS(); |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
bool GLFramebuffer::AttachRenderbuffer(GLenum internalFormat, GLsizei width, GLsizei height, GLenum attachment) |
| 95 |
{ |
| 96 |
return AttachRenderbuffer(nullptr, internalFormat, width, height, attachment); |
| 97 |
} |
| 98 |
|
| 99 |
bool GLFramebuffer::DetachRenderbuffer(GLenum attachment) |
| 100 |
{ |
| 101 |
for (std::uint32_t i = 0; i < _attachedRenderbuffers.size(); i++) { |
| 102 |
if (_attachedRenderbuffers[i]->GetAttachment() == attachment) { |
| 103 |
Bind(GL_FRAMEBUFFER); |
| 104 |
glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, 0); |
| 105 |
GL_LOG_ERRORS(); |
| 106 |
_attachedRenderbuffers.eraseUnordered(i); |
| 107 |
return true; |
| 108 |
} |
| 109 |
} |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
void GLFramebuffer::AttachTexture(GLTexture& texture, GLenum attachment) |
| 114 |
{ |
| 115 |
Bind(GL_FRAMEBUFFER); |
| 116 |
glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, texture._target, texture._glHandle, 0); |
| 117 |
GL_LOG_ERRORS(); |
| 118 |
} |
| 119 |
|
| 120 |
void GLFramebuffer::DetachTexture(GLenum attachment) |
| 121 |
{ |
| 122 |
Bind(GL_FRAMEBUFFER); |
| 123 |
glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, 0, 0); |
| 124 |
GL_LOG_ERRORS(); |
| 125 |
} |
| 126 |
|
| 127 |
#if !(defined(DEATH_TARGET_APPLE) && defined(DEATH_TARGET_ARM)) |
| 128 |
void GLFramebuffer::Invalidate(GLsizei numAttachments, const GLenum* attachments) |
| 129 |
{ |
| 130 |
#if defined(RHI_GL_PROFILE_ES2) |
| 131 |
// glInvalidateFramebuffer() is ES 3.0 (ES2 would need EXT_discard_framebuffer, not assumed) and |
| 132 |
// invalidation is purely a bandwidth optimization - skipping it is always correct |
| 133 |
static_cast<void>(numAttachments); |
| 134 |
static_cast<void>(attachments); |
| 135 |
#else |
| 136 |
Bind(GL_FRAMEBUFFER); |
| 137 |
glInvalidateFramebuffer(GL_FRAMEBUFFER, numAttachments, attachments); |
| 138 |
GL_LOG_ERRORS(); |
| 139 |
#endif |
| 140 |
} |
| 141 |
#endif |
| 142 |
|
| 143 |
bool GLFramebuffer::IsStatusComplete() |
| 144 |
{ |
| 145 |
Bind(GL_FRAMEBUFFER); |
| 146 |
const GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 147 |
Unbind(GL_FRAMEBUFFER); |
| 148 |
|
| 149 |
return (status == GL_FRAMEBUFFER_COMPLETE); |
| 150 |
} |
| 151 |
|
| 152 |
void GLFramebuffer::SetObjectLabel(StringView label) |
| 153 |
{ |
| 154 |
GLDebug::SetObjectLabel(GLDebug::LabelTypes::FrameBuffer, _glHandle, label); |
| 155 |
} |
| 156 |
|
| 157 |
bool GLFramebuffer::BindHandle(GLenum target, GLuint glHandle) |
| 158 |
{ |
| 159 |
FATAL_ASSERT(target == GL_FRAMEBUFFER || target == GL_READ_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER); |
| 160 |
|
| 161 |
#if defined(RHI_GL_PROFILE_ES2) |
| 162 |
// ES2 only has the combined GL_FRAMEBUFFER target (separate READ/DRAW bind points are ES 3.0); |
| 163 |
// remap every request so callers like GLRenderTarget::BindDraw() and the destructor stay portable |
| 164 |
target = GL_FRAMEBUFFER; |
| 165 |
#endif |
| 166 |
if (target == GL_FRAMEBUFFER && (_readBoundBuffer != glHandle || _drawBoundBuffer != glHandle)) { |
| 167 |
glBindFramebuffer(target, glHandle); |
| 168 |
GL_LOG_ERRORS(); |
| 169 |
_readBoundBuffer = glHandle; |
| 170 |
_drawBoundBuffer = glHandle; |
| 171 |
return true; |
| 172 |
} else if (target == GL_READ_FRAMEBUFFER && _readBoundBuffer != glHandle) { |
| 173 |
glBindFramebuffer(target, glHandle); |
| 174 |
GL_LOG_ERRORS(); |
| 175 |
_readBoundBuffer = glHandle; |
| 176 |
return true; |
| 177 |
} else if (target == GL_DRAW_FRAMEBUFFER && _drawBoundBuffer != glHandle) { |
| 178 |
glBindFramebuffer(target, glHandle); |
| 179 |
GL_LOG_ERRORS(); |
| 180 |
_drawBoundBuffer = glHandle; |
| 181 |
return true; |
| 182 |
} |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
GLuint GLFramebuffer::GetBoundHandle(GLenum target) |
| 187 |
{ |
| 188 |
FATAL_ASSERT(target == GL_FRAMEBUFFER || target == GL_READ_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER); |
| 189 |
|
| 190 |
if (target == GL_FRAMEBUFFER || target == GL_READ_FRAMEBUFFER) |
| 191 |
return _readBoundBuffer; |
| 192 |
else |
| 193 |
return _drawBoundBuffer; |
| 194 |
} |
| 195 |
|
| 196 |
void GLFramebuffer::SetBoundHandle(GLenum target, GLuint glHandle) |
| 197 |
{ |
| 198 |
FATAL_ASSERT(target == GL_FRAMEBUFFER || target == GL_READ_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER); |
| 199 |
|
| 200 |
if (target == GL_FRAMEBUFFER || target == GL_READ_FRAMEBUFFER) |
| 201 |
_readBoundBuffer = glHandle; |
| 202 |
|
| 203 |
if (target == GL_FRAMEBUFFER || target == GL_DRAW_FRAMEBUFFER) |
| 204 |
_drawBoundBuffer = glHandle; |
| 205 |
} |
| 206 |
} |