| 1 |
#include "GLRenderTarget.h" |
| 2 |
|
| 3 |
#include <Containers/StringView.h> |
| 4 |
|
| 5 |
namespace nCine::RHI::GL |
| 6 |
{ |
| 7 |
namespace |
| 8 |
{ |
| 9 |
GLenum DepthStencilFormatToGLFormat(DepthStencilFormat format) |
| 10 |
{ |
| 11 |
#if defined(RHI_GL_PROFILE_ES2) |
| 12 |
// The only depth renderbuffer format guaranteed by ES 2.0 core is DEPTH_COMPONENT16 (24-bit depth |
| 13 |
// needs OES_depth24, packed depth-stencil needs OES_packed_depth_stencil - neither is assumed). |
| 14 |
// The game's texture render targets all use DepthStencilFormat::None, so this only guards spec |
| 15 |
// correctness for potential future users |
| 16 |
static_cast<void>(format); |
| 17 |
return GL_DEPTH_COMPONENT16; |
| 18 |
#else |
| 19 |
switch (format) { |
| 20 |
case DepthStencilFormat::Depth16: |
| 21 |
return GL_DEPTH_COMPONENT16; |
| 22 |
case DepthStencilFormat::Depth24: |
| 23 |
return GL_DEPTH_COMPONENT24; |
| 24 |
default: |
| 25 |
case DepthStencilFormat::Depth24_Stencil8: |
| 26 |
return GL_DEPTH24_STENCIL8; |
| 27 |
} |
| 28 |
#endif |
| 29 |
} |
| 30 |
|
| 31 |
GLenum DepthStencilFormatToGLAttachment(DepthStencilFormat format) |
| 32 |
{ |
| 33 |
#if defined(RHI_GL_PROFILE_ES2) |
| 34 |
// ES2 has no GL_DEPTH_STENCIL_ATTACHMENT; everything attaches as a plain depth buffer here |
| 35 |
// (matches DepthStencilFormatToGLFormat() above, which always yields DEPTH_COMPONENT16) |
| 36 |
static_cast<void>(format); |
| 37 |
return GL_DEPTH_ATTACHMENT; |
| 38 |
#else |
| 39 |
switch (format) { |
| 40 |
case DepthStencilFormat::Depth16: |
| 41 |
case DepthStencilFormat::Depth24: |
| 42 |
return GL_DEPTH_ATTACHMENT; |
| 43 |
default: |
| 44 |
case DepthStencilFormat::Depth24_Stencil8: |
| 45 |
return GL_DEPTH_STENCIL_ATTACHMENT; |
| 46 |
} |
| 47 |
#endif |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
void GLRenderTarget::AttachColorTexture(GLTexture& texture, std::uint32_t index) |
| 52 |
{ |
| 53 |
_fbo.AttachTexture(texture, GL_COLOR_ATTACHMENT0 + index); |
| 54 |
} |
| 55 |
|
| 56 |
void GLRenderTarget::DetachColorTexture(std::uint32_t index) |
| 57 |
{ |
| 58 |
_fbo.DetachTexture(GL_COLOR_ATTACHMENT0 + index); |
| 59 |
} |
| 60 |
|
| 61 |
void GLRenderTarget::AttachDepthStencil(DepthStencilFormat format, std::int32_t width, std::int32_t height) |
| 62 |
{ |
| 63 |
_fbo.AttachRenderbuffer(DepthStencilFormatToGLFormat(format), width, height, DepthStencilFormatToGLAttachment(format)); |
| 64 |
} |
| 65 |
|
| 66 |
void GLRenderTarget::DetachDepthStencil(DepthStencilFormat format) |
| 67 |
{ |
| 68 |
_fbo.DetachRenderbuffer(DepthStencilFormatToGLAttachment(format)); |
| 69 |
} |
| 70 |
|
| 71 |
void GLRenderTarget::BindDraw() |
| 72 |
{ |
| 73 |
_fbo.Bind(GL_DRAW_FRAMEBUFFER); |
| 74 |
} |
| 75 |
|
| 76 |
void GLRenderTarget::UnbindDraw() |
| 77 |
{ |
| 78 |
GLFramebuffer::Unbind(GL_DRAW_FRAMEBUFFER); |
| 79 |
} |
| 80 |
|
| 81 |
bool GLRenderTarget::SetDrawBuffers(std::uint32_t numColorAttachments) |
| 82 |
{ |
| 83 |
return _fbo.DrawBuffers(numColorAttachments); |
| 84 |
} |
| 85 |
|
| 86 |
bool GLRenderTarget::IsStatusComplete() |
| 87 |
{ |
| 88 |
return _fbo.IsStatusComplete(); |
| 89 |
} |
| 90 |
|
| 91 |
void GLRenderTarget::InvalidateDepthStencil(DepthStencilFormat format) |
| 92 |
{ |
| 93 |
#if !(defined(DEATH_TARGET_APPLE) && defined(DEATH_TARGET_ARM)) |
| 94 |
const GLenum invalidAttachment = DepthStencilFormatToGLAttachment(format); |
| 95 |
_fbo.Invalidate(1, &invalidAttachment); |
| 96 |
#else |
| 97 |
static_cast<void>(format); |
| 98 |
#endif |
| 99 |
} |
| 100 |
|
| 101 |
void GLRenderTarget::SetObjectLabel(StringView label) |
| 102 |
{ |
| 103 |
_fbo.SetObjectLabel(label); |
| 104 |
} |
| 105 |
} |