Starting from 2021-07-01, all LRZ GitLab users will be required to explicitly accept the GitLab Terms of Service. Please see the detailed information at https://doku.lrz.de/display/PUBLIC/GitLab and make sure that your projects conform to the requirements.

framebufferobject.cpp 7.06 KB
Newer Older
schultezub's avatar
schultezub committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/**********************************************************************
 *                                                                    *
 * tgt - Tiny Graphics Toolbox                                        *
 *                                                                    *
 * Copyright (C) 2006-2011 Visualization and Computer Graphics Group, *
 * Department of Computer Science, University of Muenster, Germany.   *
 * <http://viscg.uni-muenster.de>                                     *
 *                                                                    *
 * This file is part of the tgt library. This library is free         *
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library is distributed in the hope that it will be useful,    *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the       *
 * GNU Lesser General Public License for more details.                *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

#include "tgt/framebufferobject.h"
#include "tgt/logmanager.h"
27
#include "tgt/openglgarbagecollector.h"
schultezub's avatar
schultezub committed
28 29 30 31 32 33 34

namespace tgt {

const std::string FramebufferObject::loggerCat_("tgt.FramebufferObject");

FramebufferObject::FramebufferObject()
  : id_(0)
35
  , numColorAttachments_(0)
schultezub's avatar
schultezub committed
36
{
37
    memset(attachments_, 0, sizeof(Texture*) * (TGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS+2));
schultezub's avatar
schultezub committed
38 39 40 41 42
    generateId();
}

FramebufferObject::~FramebufferObject()
{
43
    GLGC.addGarbageFramebufferObject(id_);
schultezub's avatar
schultezub committed
44 45 46 47
}

void FramebufferObject::activate()
{
48 49
    glBindFramebufferEXT(GL_FRAMEBUFFER, id_);
    LGL_ERROR;
schultezub's avatar
schultezub committed
50 51 52 53
}

void FramebufferObject::deactivate()
{
54 55
    glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
    LGL_ERROR;
schultezub's avatar
schultezub committed
56 57 58 59 60 61
}

void FramebufferObject::attachTexture(Texture* texture, GLenum attachment, int mipLevel, int zSlice)
{
    switch(texture->getType()) {
        case GL_TEXTURE_1D:
62
            glFramebufferTexture1D( GL_FRAMEBUFFER, attachment, GL_TEXTURE_1D, texture->getId(), mipLevel );
schultezub's avatar
schultezub committed
63 64
            break;
        case GL_TEXTURE_3D:
65
            glFramebufferTexture3D( GL_FRAMEBUFFER, attachment, GL_TEXTURE_3D, texture->getId(), mipLevel, zSlice );
schultezub's avatar
schultezub committed
66
            break;
67 68
        case GL_TEXTURE_2D_ARRAY:
            glFramebufferTextureLayer( GL_FRAMEBUFFER, attachment, texture->getId(), mipLevel, zSlice );
schultezub's avatar
schultezub committed
69 70
            break;
        default: //GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE
71
            glFramebufferTexture2D( GL_FRAMEBUFFER, attachment, texture->getType(), texture->getId(), mipLevel );
schultezub's avatar
schultezub committed
72 73
            break;
    }
74
    LGL_ERROR;
75 76 77 78 79

    size_t index = decodeAttachment(attachment);
    attachments_[index] = texture;
    if (index < TGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS)
        ++numColorAttachments_;
schultezub's avatar
schultezub committed
80 81 82
}

Texture* FramebufferObject::getTextureAtAttachment(GLenum attachment) {
83
    return attachments_[decodeAttachment(attachment)];
schultezub's avatar
schultezub committed
84 85 86
}

void FramebufferObject::detachTexture(GLenum attachment) {
87 88
    size_t index = decodeAttachment(attachment);
    if (attachments_[index] != 0) {
89 90 91 92 93 94 95 96 97 98 99 100 101 102
        switch (attachments_[index]->getType()) {
            case GL_TEXTURE_1D:
                glFramebufferTexture1D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_1D, 0, 0);
                break;
            case GL_TEXTURE_2D_ARRAY:
                glFramebufferTextureLayer(GL_FRAMEBUFFER, attachment, 0, 0, 0);
                break;
            case GL_TEXTURE_3D:
                glFramebufferTexture3D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_3D, 0, 0, 0);
                break;
            default: // GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE
                glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, 0, 0);
                break;
        }
103
        attachments_[index] = 0;
104
        LGL_ERROR;
105 106 107

        if (index < TGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS)
            --numColorAttachments_;
schultezub's avatar
schultezub committed
108 109 110 111 112 113 114
    }
    else {
        LWARNING("Trying to detach unknown texture!");
    }
}

void FramebufferObject::detachAll() {
115 116 117
    for (GLenum i = 0; i < TGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS; ++i) {
        if (colorAttachments_[i] != 0)
            detachTexture(GL_COLOR_ATTACHMENT0 + i);
schultezub's avatar
schultezub committed
118
    }
119 120 121 122 123 124
    if (depthAttachment_ != 0)
        detachTexture(GL_DEPTH_ATTACHMENT);
    if (stencilAttachment_ != 0)
        detachTexture(GL_STENCIL_ATTACHMENT);

    numColorAttachments_ = 0;
schultezub's avatar
schultezub committed
125 126 127 128 129 130
}

bool FramebufferObject::isComplete() const
{
  bool complete = false;

131
  GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
schultezub's avatar
schultezub committed
132
  switch(status) {
133
      case GL_FRAMEBUFFER_COMPLETE:
schultezub's avatar
schultezub committed
134 135
          complete = true;
          break;
136 137
      case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
          LERROR("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT");
schultezub's avatar
schultezub committed
138
          break;
139 140
      case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
          LERROR("GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT");
schultezub's avatar
schultezub committed
141 142
          break;
      case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
143
          LERROR("GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS");
schultezub's avatar
schultezub committed
144 145
          break;
      case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
146
          LERROR("GL_FRAMEBUFFER_INCOMPLETE_FORMATS");
schultezub's avatar
schultezub committed
147
          break;
148 149
      case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
          LERROR("GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER");
schultezub's avatar
schultezub committed
150
          break;
151 152
      case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
          LERROR("GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER");
schultezub's avatar
schultezub committed
153
          break;
154 155
      case GL_FRAMEBUFFER_UNSUPPORTED:
          LERROR("GL_FRAMEBUFFER_UNSUPPORTED");
schultezub's avatar
schultezub committed
156 157 158 159 160 161 162 163 164 165 166 167 168
          break;
      default:
          LERROR("Unknown error!");
  }
  return complete;
}

bool FramebufferObject::isActive() const {
    return ((getActiveObject() == id_) && (id_ != 0));
}

GLuint FramebufferObject::getActiveObject() {
    GLint fbo;
169
    glGetIntegerv (GL_FRAMEBUFFER_BINDING, &fbo);
schultezub's avatar
schultezub committed
170 171 172 173 174
    return static_cast<GLuint>(fbo);
}

GLuint FramebufferObject::generateId() {
    id_ = 0;
175 176
    glGenFramebuffersEXT(1, &id_);
    LGL_ERROR;
schultezub's avatar
schultezub committed
177 178 179
    return id_;
}

180
Texture* const * FramebufferObject::getAttachments() const {
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    return attachments_;
}

const Texture* FramebufferObject::getColorAttachment(size_t index /*= 0*/) const {
    tgtAssert(index < TGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS, "Index out of bounds!");
    return attachments_[index];
}

const Texture* FramebufferObject::getDepthAttachment() const {
    return attachments_[TGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS];
}

const Texture* FramebufferObject::getStencilAttachment() const {
    return attachments_[TGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS+1];
}

size_t FramebufferObject::getNumColorAttachments() const {
    return numColorAttachments_;
}

schultezub's avatar
schultezub committed
201
} // namespace