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.2 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
#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:
            glTexImage1D(type, 0, internalFormat, dimensions.x, 0, Texture::calcMatchingFormat(internalFormat), Texture::calcMatchingDataType(internalFormat), nullptr);
            break;

        case GL_TEXTURE_1D_ARRAY: // fallthrough
        case GL_TEXTURE_2D:
            glTexImage2D(type, 0, internalFormat, dimensions.x, dimensions.y, 0, Texture::calcMatchingFormat(internalFormat), Texture::calcMatchingDataType(internalFormat), nullptr);
            break;

        case GL_TEXTURE_2D_ARRAY: // fallthrough
        case GL_TEXTURE_3D:
            glTexImage3D(type, 0, internalFormat, dimensions.x, dimensions.y, dimensions.z, 0, Texture::calcMatchingFormat(internalFormat), Texture::calcMatchingDataType(internalFormat), nullptr);
            break;
        }
        LGL_ERROR;
    }
#else
    GLuint id;
    glGenTextures(1, &id);

    glBindTexture(type, id);

    switch (type) {
    case GL_TEXTURE_1D:
        glTexImage1D(type, 0, internalFormat, dimensions.x, 0, Texture::calcMatchingFormat(internalFormat), Texture::calcMatchingDataType(internalFormat), nullptr);
        break;

    case GL_TEXTURE_1D_ARRAY: // fallthrough
    case GL_TEXTURE_2D:
        glTexImage2D(type, 0, internalFormat, dimensions.x, dimensions.y, 0, Texture::calcMatchingFormat(internalFormat), Texture::calcMatchingDataType(internalFormat), nullptr);
        break;

    case GL_TEXTURE_2D_ARRAY: // fallthrough
    case GL_TEXTURE_3D:
        glTexImage3D(type, 0, internalFormat, dimensions.x, dimensions.y, dimensions.z, 0, Texture::calcMatchingFormat(internalFormat), Texture::calcMatchingDataType(internalFormat), nullptr);
        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
}