Currently job artifacts in CI/CD pipelines on LRZ GitLab never expire. Starting from Wed 26.1.2022 the default expiration time will be 30 days (GitLab default). Currently existing artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

voxelhierarchymapper.cpp 8.11 KB
Newer Older
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
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2013, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, Germany
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// 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
// 
// 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.
// 
// ================================================================================================

#include "voxelhierarchymapper.h"

27
28
29
30
31
#include "cgt/assert.h"
#include "cgt/framebufferobject.h"
#include "cgt/shadermanager.h"
#include "cgt/texture.h"
#include "cgt/textureunit.h"
32
#include "cgt/cgt_gl.h"
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

#include "core/classification/abstracttransferfunction.h"
#include "core/datastructures/imagerepresentationgl.h"
#include "core/datastructures/geometrydatafactory.h"

#define VOXEL_DEPTH_MIPMAPPING 32
#define DIV_CEIL(x,y) ((x) > 0) ? (1 + ((x) - 1)/(y)) : ((x) / (y))

namespace campvis {

    std::string VoxelHierarchyMapper::loggerCat_ = "CAMPVis.modules.vis.VoxelHierarchyMapper";

    VoxelHierarchyMapper::VoxelHierarchyMapper()
        : _hierarchyRendererShader(nullptr)
        , _mimapRendererShader(nullptr)
        , _fbo(nullptr)
        , _hierarchyTexture(nullptr)
        , _quad(nullptr)
    {  
        _hierarchyRendererShader = ShdrMgr.loadWithCustomGlslVersion("core/glsl/passthrough.vert", "", "modules/vis/glsl/hierarchyrenderer.frag", "", "400");
        _mimapRendererShader = ShdrMgr.loadWithCustomGlslVersion("core/glsl/passthrough.vert", "", "modules/vis/glsl/mipmaprenderer.frag", "", "400");
54
        _fbo = new cgt::FramebufferObject();
55

56
        _quad = GeometryDataFactory::createQuad(cgt::vec3(0.f), cgt::vec3(1.f), cgt::vec3(1.f, 1.f, 0.f), cgt::vec3(0.f, 0.f, 0.f));
57
58
59
60
61
62
63
64
65
    }

    VoxelHierarchyMapper::~VoxelHierarchyMapper() {
        ShdrMgr.dispose(_hierarchyRendererShader);
        ShdrMgr.dispose(_mimapRendererShader);
        delete _quad;
    }

    void VoxelHierarchyMapper::createHierarchy(const ImageRepresentationGL* image, AbstractTransferFunction* transferFunction) {
66
67
        cgtAssert(image != nullptr, "Image must not be 0.");
        cgtAssert(transferFunction != nullptr, "Transfer function must not be 0.");
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        
        // perform ceiling integer division:
        // z is not considered.
        _brickSize = 2;
        _dimBricks = image->getSize();
        for (int i = 0; i < 2; ++i)
            _dimBricks.elem[i] = DIV_CEIL(_dimBricks.elem[i], _brickSize);

        // set the depth of the bricks
        _brickDepth = _dimBricks.z / 32;

        // since the texture is a 2D texture and the elements store the depth  will pack VOXEL_DEPTH number of values along the z axis into one block, the _dimBricks.z is 
        _dimBricks.z = 32;

        _dimPackedBricks = _dimBricks;
        _dimPackedBricks.z = _dimPackedBricks.z / 32;

        _maxMipmapLevel = computeMaxLevel(_dimPackedBricks.x, _dimPackedBricks.y);

87
        cgtAssert(_dimPackedBricks.z == 1, "This should not happen!");
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115


        // create initial texture:
        createEmptyTextureWithMipMaps();
        renderInitialHierarchy(image, transferFunction);

        // generate mipmap levels
        renderMipmaps();
    }



    GLuint VoxelHierarchyMapper::computeMaxLevel(size_t resolutionX, size_t resolutionY) {
        GLuint count = 0;
        GLuint resolution = static_cast<GLuint>(std::max(resolutionX, resolutionY));

        while (resolution) {
            resolution /= 2;
            count++;
        }
        return count-1;
    }

    void VoxelHierarchyMapper::renderMipmaps() {
        LDEBUG("Start computing the levels of the voxel object.");

        /// Activate the shader for geometry Rendering.
        _mimapRendererShader->activate();
116
        _mimapRendererShader->setUniform("_projectionMatrix", cgt::mat4::createOrtho(0, 1, 0, 1, -1, 1));
117

118
        cgt::TextureUnit bbvUnit;
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
        bbvUnit.activate();
        _hierarchyTexture->bind();
        _mimapRendererShader->setUniform("_voxelTexture", bbvUnit.getUnitNumber());
        
        _fbo->activate();

        for (GLuint level = 0; level < _maxMipmapLevel; ++level) {
            double resX = _hierarchyTexture->getWidth() / pow(2.0, static_cast<double>(level));
            double resY = _hierarchyTexture->getHeight() / pow(2.0, static_cast<double>(level));

            _mimapRendererShader->setUniform("_level", static_cast<int>(level));
            _mimapRendererShader->setUniform("_inverseTexSizeX", 1.f / static_cast<float>(resX));
            _mimapRendererShader->setUniform("_inverseTexSizeY", 1.f / static_cast<float>(resY));

            _fbo->attachTexture(_hierarchyTexture, GL_COLOR_ATTACHMENT0, level+1, 0);
            _fbo->isComplete();

            glViewport(0, 0, static_cast<GLsizei>(resX / 2.0), static_cast<GLsizei>(resY / 2.0));
            _quad->render(GL_POLYGON);
        }

        _fbo->deactivate();
        _mimapRendererShader->deactivate();    
        LGL_ERROR;

        LDEBUG("...finished computing voxel visibilities mip maps.");
    }


    void VoxelHierarchyMapper::createEmptyTextureWithMipMaps() {
        // delete old stuff
        delete _hierarchyTexture;

152
        cgt::TextureUnit tempUnit;
153
154
155
        tempUnit.activate();

        // create new texture
156
        _hierarchyTexture = new cgt::Texture(GL_TEXTURE_2D, _dimPackedBricks, GL_R32UI, cgt::Texture::NEAREST);
157
        _hierarchyTexture->setWrapping(cgt::Texture::CLAMP);
158
159
160
161
162
163
164
165
166
        LGL_ERROR;

        // attach mipmaps manually (as we want special ones)
        glBindTexture(GL_TEXTURE_2D, _hierarchyTexture->getId());

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, _maxMipmapLevel);  
167
        LGL_ERROR;
168
169
170
171
172

        int div = 2;
        for (GLuint level = 1; level <= _maxMipmapLevel; ++level) {
            glTexImage2D(GL_TEXTURE_2D, level, GL_R32UI, _hierarchyTexture->getWidth()/div, _hierarchyTexture->getHeight()/div, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, 0);
            div = div << 1;
173
            LGL_ERROR;
174
175
176
177
178
179
180
181
        }
        LGL_ERROR;
    }

    void VoxelHierarchyMapper::renderInitialHierarchy(const ImageRepresentationGL* image, AbstractTransferFunction* transferFunction) {
        LDEBUG("Start computing voxel visibilities...");

        _hierarchyRendererShader->activate();
182
        _hierarchyRendererShader->setUniform("_projectionMatrix", cgt::mat4::createOrtho(0, 1, 0, 1, -1, 1));
183
184
185
        _hierarchyRendererShader->setUniform("_voxelDepth", static_cast<GLuint>(_brickDepth));
        _hierarchyRendererShader->setUniform("_voxelSize", static_cast<GLuint>(_brickSize));

186
        cgt::TextureUnit volumeUnit, tfUnit;
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
        image->bind(_hierarchyRendererShader, volumeUnit, "_volume", "_volumeTextureParams");
        transferFunction->bind(_hierarchyRendererShader, tfUnit, "_transferFunction", "_transferFunctionParams");

        _fbo->activate();
        _fbo->attachTexture(_hierarchyTexture, GL_COLOR_ATTACHMENT0, 0, 0);
        _fbo->isComplete();
        glViewport(0, 0, static_cast<GLsizei>(_hierarchyTexture->getWidth()), static_cast<GLsizei>(_hierarchyTexture->getHeight()));

        _quad->render(GL_POLYGON);

        _fbo->deactivate();
        _hierarchyRendererShader->deactivate();    

        LGL_ERROR;

        LDEBUG("...finished computing voxel visibilities.");
    }

}