| 1 | // SPDX-License-Identifier: LGPL-2.1-only |
| 2 | // Copyright (C) 2021 James Hogan <[email protected]> |
| 3 | |
| 4 | #include "XRFramebuffer.h" |
| 5 | |
| 6 | #include <osg/FrameBufferObject> |
| 7 | #include <osg/Image> |
| 8 | #include <osg/State> |
| 9 | #include <osg/Version> |
| 10 | |
| 11 | using namespace osgXR; |
| 12 | |
| 13 | #if(OSG_VERSION_GREATER_OR_EQUAL(3, 4, 0)) |
| 14 | typedef osg::GLExtensions OSG_GLExtensions; |
| 15 | #else |
| 16 | typedef osg::FBOExtensions OSG_GLExtensions; |
| 17 | #endif |
| 18 | |
| 19 | static const OSG_GLExtensions* getGLExtensions(const osg::State& state) |
| 20 | { |
| 21 | #if(OSG_VERSION_GREATER_OR_EQUAL(3, 4, 0)) |
| 22 | return state.get<osg::GLExtensions>(); |
| 23 | #else |
| 24 | return osg::FBOExtensions::instance(state.getContextID(), true); |
| 25 | #endif |
| 26 | } |
| 27 | |
| 28 | XRFramebuffer::XRFramebuffer(uint32_t width, uint32_t height, |
| 29 | GLuint texture, GLuint depthTexture) : |
| 30 | _width(width), |
| 31 | _height(height), |
| 32 | _depthFormat(GL_DEPTH_COMPONENT16), |
| 33 | _fbo(0), |
| 34 | _texture(texture), |
| 35 | _depthTexture(depthTexture), |
| 36 | _generated(false), |
| 37 | _boundTexture(false), |
| 38 | _boundDepthTexture(false), |
| 39 | _deleteDepthTexture(false) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | XRFramebuffer::~XRFramebuffer() |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | bool XRFramebuffer::valid(osg::State &state) const |
| 48 | { |
| 49 | if (!_fbo) |
| 50 | return false; |
| 51 | |
| 52 | const OSG_GLExtensions *fbo_ext = getGLExtensions(state); |
| 53 | GLenum complete = fbo_ext->glCheckFramebufferStatus(GL_FRAMEBUFFER_EXT); |
| 54 | switch (complete) |
| 55 | { |
| 56 | case GL_FRAMEBUFFER_COMPLETE_EXT: |
| 57 | return true; |
| 58 | case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: |
| 59 | OSG_WARN << "osgXR: FBO Incomplete attachment" << std::endl; |
| 60 | break; |
| 61 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: |
| 62 | OSG_WARN << "osgXR: FBO Incomplete missing attachment" << std::endl; |
| 63 | break; |
| 64 | case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: |
| 65 | OSG_WARN << "osgXR: FBO Incomplete draw buffer" << std::endl; |
| 66 | break; |
| 67 | case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: |
| 68 | OSG_WARN << "osgXR: FBO Incomplete read buffer" << std::endl; |
| 69 | break; |
| 70 | case GL_FRAMEBUFFER_UNSUPPORTED_EXT: |
| 71 | OSG_WARN << "osgXR: FBO Incomplete unsupported" << std::endl; |
| 72 | break; |
| 73 | case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT: |
| 74 | OSG_WARN << "osgXR: FBO Incomplete multisample" << std::endl; |
| 75 | break; |
| 76 | default: |
| 77 | OSG_WARN << "osgXR: FBO Incomplete ??? (0x" << std::hex << complete << std::dec << ")" << std::endl; |
| 78 | break; |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | void XRFramebuffer::bind(osg::State &state) |
| 84 | { |
| 85 | const OSG_GLExtensions *fbo_ext = getGLExtensions(state); |
| 86 | |
| 87 | if (!_fbo && !_generated) |
| 88 | { |
| 89 | fbo_ext->glGenFramebuffers(1, &_fbo); |
| 90 | _generated = true; |
| 91 | } |
| 92 | |
| 93 | if (_fbo) |
| 94 | { |
| 95 | fbo_ext->glBindFramebuffer(GL_FRAMEBUFFER_EXT, _fbo); |
| 96 | if (!_boundTexture && _texture) |
| 97 | { |
| 98 | fbo_ext->glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, _texture, 0); |
| 99 | _boundTexture = true; |
| 100 | } |
| 101 | if (!_boundDepthTexture) |
| 102 | { |
| 103 | if (!_depthTexture) |
| 104 | { |
| 105 | glGenTextures(1, &_depthTexture); |
| 106 | glBindTexture(GL_TEXTURE_2D, _depthTexture); |
| 107 | glTexImage2D(GL_TEXTURE_2D, 0, _depthFormat, _width, _height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, nullptr); |
| 108 | glBindTexture(GL_TEXTURE_2D, 0); |
| 109 | |
| 110 | _deleteDepthTexture = true; |
| 111 | } |
| 112 | |
| 113 | fbo_ext->glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, _depthTexture, 0); |
| 114 | _boundDepthTexture = true; |
| 115 | |
| 116 | valid(state); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void XRFramebuffer::unbind(osg::State &state) |
| 122 | { |
| 123 | const OSG_GLExtensions *fbo_ext = getGLExtensions(state); |
| 124 | |
| 125 | if (_fbo && _generated) |
| 126 | fbo_ext->glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0); |
| 127 | } |
| 128 | |
| 129 | void XRFramebuffer::releaseGLObjects(osg::State &state) |
| 130 | { |
| 131 | // FIXME can we do it like RenderBuffer::releaseGLObjects? |
| 132 | // FIXME better yet, switch to use FrameBufferObject, dynamically bound |
| 133 | |
| 134 | // GL context must be current |
| 135 | if (_fbo) |
| 136 | { |
| 137 | /* |
| 138 | unsigned int contextID = state->getContextID(); |
| 139 | osg::get<GLFrameBufferObjectManager>(contextID)->scheduleGLObjectForDeletion(_fbo); |
| 140 | */ |
| 141 | const OSG_GLExtensions *fbo_ext = getGLExtensions(state); |
| 142 | fbo_ext->glDeleteFramebuffers(1, &_fbo); |
| 143 | _fbo = 0; |
| 144 | } |
| 145 | if (_deleteDepthTexture) |
| 146 | { |
| 147 | glDeleteTextures(1, &_depthTexture); |
| 148 | _depthTexture = 0; |
| 149 | _deleteDepthTexture = false; |
| 150 | } |
| 151 | } |