renderdata.cpp 6.97 KB
Newer Older
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
11
12
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
13
14
15
16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18
19
20
21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
22
23
24
25
26
// 
// ================================================================================================

#include "renderdata.h"

27
#include "tgt/framebufferobject.h"
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "tgt/textureunit.h"
#include "tgt/shadermanager.h"

#include "core/datastructures/imagedata.h"
#include "core/datastructures/imagerepresentationgl.h"

namespace campvis {

    const std::string RenderData::loggerCat_ = "CAMPVis.core.datastructures.RenderData";

    RenderData::RenderData()
        : AbstractData()
        , _depthTexture(0)
    {

    }

45
46
47
48
    RenderData::RenderData(const tgt::FramebufferObject* fbo)
        : AbstractData()
        , _depthTexture(0)
    {
49
        tgt::Texture* const * attachments = fbo->getAttachments();
50
51
52
        for (size_t i = 0; i < TGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS; ++i) {
            if (attachments[i] != 0) {
                ImageData* img = new ImageData(2, attachments[i]->getDimensions(), attachments[i]->getNumChannels());
53
                ImageRepresentationGL::create(img, attachments[i]);
54
55
56
57
58
                _colorTextures.push_back(DataHandle(img));
            }
        }
        if (attachments[TGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS] != 0) {
            ImageData* img = new ImageData(2, attachments[TGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS]->getDimensions(), attachments[TGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS]->getNumChannels());
59
            ImageRepresentationGL::create(img, attachments[TGT_FRAMEBUFFEROBJECT_MAX_SUPPORTED_COLOR_ATTACHMENTS]);
60
61
62
63
            _depthTexture = DataHandle(img);
        }
    }

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    RenderData::~RenderData() {

    }

    RenderData* RenderData::clone() const {
        RenderData* toReturn = new RenderData();
        toReturn->_colorTextures = _colorTextures;
        toReturn->_depthTexture = _depthTexture;
        return toReturn;
    }

    size_t RenderData::getLocalMemoryFootprint() const {
        size_t sum = sizeof(RenderData);

        for (std::vector<DataHandle>::const_iterator it = _colorTextures.begin(); it != _colorTextures.end(); ++it)
            if (it->getData() != 0)
                sum += it->getData()->getLocalMemoryFootprint() + sizeof(DataHandle);

        if (_depthTexture.getData() != 0)
            sum += _depthTexture.getData()->getLocalMemoryFootprint() + sizeof(DataHandle);

        return sum;
    }

    size_t RenderData::getVideoMemoryFootprint() const {
        size_t sum = 0;
        for (std::vector<DataHandle>::const_iterator it = _colorTextures.begin(); it != _colorTextures.end(); ++it)
            if (it->getData() != 0)
                sum += it->getData()->getVideoMemoryFootprint();

        if (_depthTexture.getData() != 0)
            sum += _depthTexture.getData()->getVideoMemoryFootprint();

        return sum;
    }

    size_t RenderData::getNumColorTextures() const {
        return _colorTextures.size();
    }

    const ImageData* RenderData::getColorTexture(size_t index /*= 0*/) const {
        tgtAssert(index < _colorTextures.size(), "Index out of bounds.");
106
        if (index >= _colorTextures.size())
107
108
109
110
111
            return 0;

        return static_cast<const ImageData*>(_colorTextures[index].getData());
    }

112
113
114
115
116
117
118
119
    campvis::DataHandle RenderData::getColorDataHandle(size_t index /*= 0*/) const {
        tgtAssert(index < _colorTextures.size(), "Index out of bounds.");
        if (index >= _colorTextures.size())
            return DataHandle(0);

        return _colorTextures[index];
    }

120
121
122
123
124
125
126
127
128
129
130
131
    bool RenderData::hasDepthTexture() const {
        return _depthTexture.getData() != 0;
    }

    const ImageData* RenderData::getDepthTexture() const {
        const AbstractData* d = _depthTexture.getData();
        if (d == 0)
            return 0;

        return static_cast<const ImageData*>(d);
    }

132
133
134
135
    campvis::DataHandle RenderData::getDepthDataHandle() const {
        return _depthTexture;
    }

136
137
138
139
140
141
142
143
144
145
    void RenderData::addColorTexture(ImageData* texture) {
        _colorTextures.push_back(DataHandle(texture));
    }

    void RenderData::setDepthTexture(ImageData* texture) {
        _depthTexture = DataHandle(texture);
    }

    void RenderData::bindColorTexture(tgt::Shader* shader, const tgt::TextureUnit& colorTexUnit, const std::string& colorTexUniform /*= "_colorTexture"*/, const std::string& texParamsUniform /*= "_texParams"*/, size_t index /*= 0*/) const {
        tgtAssert(index < _colorTextures.size(), "Index out of bounds.");
146
        if (index >= _colorTextures.size())
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
            return;

        const ImageData* id = static_cast<const ImageData*>(_colorTextures[index].getData());
        tgtAssert(id != 0, "WTF, color texture with 0 pointer?!");

        const ImageRepresentationGL* rep = id->getRepresentation<ImageRepresentationGL>(true);
        rep->bind(shader, colorTexUnit, colorTexUniform, texParamsUniform);
    }

    void RenderData::bindDepthTexture(tgt::Shader* shader, const tgt::TextureUnit& depthTexUnit, const std::string& depthTexUniform /*= "_depthTexture"*/, const std::string& texParamsUniform /*= "_texParams"*/) const {
        tgtAssert(_depthTexture.getData() != 0, "Empty Depth Texture!");
        if (_depthTexture.getData() == 0)
            return;

        const ImageData* id = static_cast<const ImageData*>(_depthTexture.getData());
        const ImageRepresentationGL* rep = id->getRepresentation<ImageRepresentationGL>(true);
        rep->bind(shader, depthTexUnit, depthTexUniform, texParamsUniform);
    }

    void RenderData::bind(tgt::Shader* shader, const tgt::TextureUnit& colorTexUnit, const tgt::TextureUnit& depthTexUnit, const std::string& colorTexUniform /*= "_colorTexture"*/, const std::string& depthTexUniform /*= "_depthTexture"*/, const std::string& texParamsUniform /*= "_texParams"*/, size_t index /*= 0*/) const {
167
168
        if (hasDepthTexture())
            bindDepthTexture(shader, depthTexUnit, depthTexUniform, texParamsUniform);
169
170
171
172
        bindColorTexture(shader, colorTexUnit, colorTexUniform, texParamsUniform, index);
    }

}