2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

texturemanager.cpp 3.6 KB
Newer Older
Jakob Weiss's avatar
Jakob Weiss 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
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "texturemanager.h"
#include "texture.h"
#include "singleton.h"


cgt::TextureManager::TextureManager()
{

}

cgt::TextureManager::~TextureManager()
{
    std::lock_guard<std::mutex> scope_lock(accessMutex_);
    std::cout << "[TextureManager] releasing texture pool with " << texturePool_.size() << " textures..." << std::endl;
    for (auto& it : texturePool_) {
        std::cout << "Deleting texture of size [" << std::get<1>(it.first) << "," << std::get<2>(it.first) << "," << std::get<3>(it.first) 
            << "] of type " << std::get<0>(it.first) << " " << std::get<3>(it.first) << std::endl;
        glDeleteTextures(1, &it.second);
    }
}

GLuint cgt::TextureManager::generateId(GLenum type, const cgt::svec3 & dimensions, GLenum internalFormat)
{
#ifdef ENABLE_TEXTURE_POOL
    std::lock_guard<std::mutex> scope_lock(accessMutex_);

    GLuint id;
    auto it = texturePool_.find(std::make_tuple(type, dimensions.x, dimensions.y, dimensions.z, internalFormat));
    if (it != texturePool_.end()) {
        id = it->second;
        texturePool_.erase(it);
    }
    else {
        glGenTextures(1, &id);

        glBindTexture(type, id);

        switch (type) {
        case GL_TEXTURE_1D:
Jakob Weiss's avatar
Jakob Weiss committed
40
41
            glTexImage1D(type, 0, internalFormat, static_cast<GLsizei>(dimensions.x), 0, 
                Texture::calcMatchingPixelFormat(internalFormat), Texture::calcMatchingPixelDataType(internalFormat), nullptr);
Jakob Weiss's avatar
Jakob Weiss committed
42
43
44
45
            break;

        case GL_TEXTURE_1D_ARRAY: // fallthrough
        case GL_TEXTURE_2D:
Jakob Weiss's avatar
Jakob Weiss committed
46
47
            glTexImage2D(type, 0, internalFormat, static_cast<GLsizei>(dimensions.x), static_cast<GLsizei>(dimensions.y), 0, 
                Texture::calcMatchingPixelFormat(internalFormat), Texture::calcMatchingPixelDataType(internalFormat), nullptr);
Jakob Weiss's avatar
Jakob Weiss committed
48
49
50
51
            break;

        case GL_TEXTURE_2D_ARRAY: // fallthrough
        case GL_TEXTURE_3D:
Jakob Weiss's avatar
Jakob Weiss committed
52
53
            glTexImage3D(type, 0, internalFormat, static_cast<GLsizei>(dimensions.x), static_cast<GLsizei>(dimensions.y), static_cast<GLsizei>(dimensions.z), 0, 
                Texture::calcMatchingPixelFormat(internalFormat), Texture::calcMatchingPixelDataType(internalFormat), nullptr);
Jakob Weiss's avatar
Jakob Weiss committed
54
55
56
57
58
59
60
61
62
63
64
65
            break;
        }
        LGL_ERROR;
    }
#else
    GLuint id;
    glGenTextures(1, &id);

    glBindTexture(type, id);

    switch (type) {
    case GL_TEXTURE_1D:
Jakob Weiss's avatar
Jakob Weiss committed
66
67
        glTexImage1D(type, 0, internalFormat, static_cast<GLsizei>(dimensions.x), 0, 
            Texture::calcMatchingPixelFormat(internalFormat), Texture::calcMatchingPixelDataType(internalFormat), nullptr);
Jakob Weiss's avatar
Jakob Weiss committed
68
69
70
71
        break;

    case GL_TEXTURE_1D_ARRAY: // fallthrough
    case GL_TEXTURE_2D:
Jakob Weiss's avatar
Jakob Weiss committed
72
73
        glTexImage2D(type, 0, internalFormat, static_cast<GLsizei>(dimensions.x), static_cast<GLsizei>(dimensions.y), 0, 
            Texture::calcMatchingPixelFormat(internalFormat), Texture::calcMatchingPixelDataType(internalFormat), nullptr);
Jakob Weiss's avatar
Jakob Weiss committed
74
75
76
77
        break;

    case GL_TEXTURE_2D_ARRAY: // fallthrough
    case GL_TEXTURE_3D:
Jakob Weiss's avatar
Jakob Weiss committed
78
79
        glTexImage3D(type, 0, internalFormat, static_cast<GLsizei>(dimensions.x), static_cast<GLsizei>(dimensions.y), static_cast<GLsizei>(dimensions.z), 0, 
            Texture::calcMatchingPixelFormat(internalFormat), Texture::calcMatchingPixelDataType(internalFormat), nullptr);
Jakob Weiss's avatar
Jakob Weiss committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        break;
    }
    LGL_ERROR;
#endif
    return id;
}

void cgt::TextureManager::releaseId(GLuint id, GLenum type, const cgt::svec3 & dimensions, GLenum internalFormat)
{
#ifdef ENABLE_TEXTURE_POOL
    std::lock_guard<std::mutex> scope_lock(accessMutex_);
    texturePool_.insert(std::make_pair(std::make_tuple(type, dimensions.x, dimensions.y, dimensions.z, internalFormat), id));
#else
    glDeleteTextures(1, &id);
#endif
}