Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
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
116
117
118
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "config.h"

#include "Platform/Video/OpenGL/Headers/glResources.h"
#include "Platform/Video/OpenGL/Headers/glLockManager.h"

#include "Headers/glTexture.h"
#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/Video/OpenGL/Headers/GLWrapper.h"

#include "Core/Headers/ParamHandler.h"

#include "Core/Headers/Console.h"
#include "Utility/Headers/Localization.h"

namespace Divide {

glTexture::glTexture(GFXDevice& context,
                     const stringImpl& name,
                     const stringImpl& resourceLocation,
                     TextureType type,
                     bool asyncLoad)
    : Texture(context, name, resourceLocation, type, asyncLoad),
     _lockManager(MemoryManager_NEW glLockManager())
{
    _allocatedStorage = false;

    _type = GLUtil::glTextureTypeTable[to_uint(type)];

    U32 tempHandle = 0;
    glCreateTextures(_type, 1, &tempHandle);
    
    DIVIDE_ASSERT(tempHandle != 0,
                  "glTexture error: failed to generate new texture handle!");
    _textureData.setHandleHigh(tempHandle);
}

glTexture::~glTexture()
{
    MemoryManager::DELETE(_lockManager);
}

bool glTexture::unload() {
    U32 textureID = _textureData.getHandleHigh();
    if (textureID > 0) {
        if (_lockManager) {
            _lockManager->Wait(false);
        }
        glDeleteTextures(1, &textureID);
        _textureData.setHandleHigh(0U);
    }

    return true;
}

void glTexture::threadedLoad() {
    updateSampler();
    Texture::threadedLoad();
    _lockManager->Lock(_asyncLoad);
}

void glTexture::setMipMapRange(GLushort base, GLushort max) {
    if (_mipMinLevel == base && _mipMaxLevel == max) {
        return;
    }

    Texture::setMipMapRange(base, max);

    glTextureParameteri(_textureData.getHandleHigh(), GL_TEXTURE_BASE_LEVEL, base);
    glTextureParameteri(_textureData.getHandleHigh(), GL_TEXTURE_MAX_LEVEL, max);
}

void glTexture::resize(const bufferPtr ptr,
                       const vec2<U16>& dimensions,
                       const vec2<U16>& mipLevels) {
    // Immutable storage requires us to create a new texture object 
    U32 tempHandle = 0;
    glCreateTextures(_type, 1, &tempHandle);
    
    DIVIDE_ASSERT(tempHandle != 0,
        "glTexture error: failed to generate new texture handle!");

    U32 textureID = _textureData.getHandleHigh();
    if (textureID > 0) {
        glDeleteTextures(1, &textureID);
    }
    _textureData.setHandleHigh(tempHandle);
    _allocatedStorage = false;
    TextureLoadInfo info;
    loadData(info, _descriptor, ptr, dimensions, mipLevels);
}

void glTexture::updateMipMaps() {
    if (_mipMapsDirty && _descriptor._samplerDescriptor.generateMipMaps()) {
        glGenerateTextureMipmap(_textureData.getHandleHigh());
    }
    _mipMapsDirty = false;
}

void glTexture::updateSampler() {
    if (_samplerDirty) {
        _textureData._samplerHash = GL_API::getOrCreateSamplerObject(_descriptor._samplerDescriptor);
        _samplerDirty = false;
    }
}

void glTexture::reserveStorage(const TextureLoadInfo& info) {
    DIVIDE_ASSERT(
        !(_textureData._textureType == TextureType::TEXTURE_CUBE_MAP &&
          _width != _height),
        "glTexture::reserverStorage error: width and height for "
        "cube map texture do not match!");

    ParamHandler& par = ParamHandler::instance();

    GLenum glInternalFormat = _descriptor._internalFormat == GFXImageFormat::DEPTH_COMPONENT
                            ? GL_DEPTH_COMPONENT32
                            : GLUtil::glImageFormatTable[to_uint(_descriptor._internalFormat)];

    switch (_textureData._textureType) {
        case TextureType::TEXTURE_1D: {
            glTextureStorage1D(
                _textureData.getHandleHigh(),
                _mipMaxLevel,
                glInternalFormat,
                _width);

        } break;
        case TextureType::TEXTURE_2D: {
            glTextureStorage2D(
                _textureData.getHandleHigh(),
                _mipMaxLevel,
                glInternalFormat,
                _width,
                _height);
        } break;
        case TextureType::TEXTURE_2D_MS: {
            glTextureStorage2DMultisample(
                _textureData.getHandleHigh(), 
                par.getParam<I32>(_ID("rendering.MSAAsampless"), 0),
                glInternalFormat,
                _width,
                _height,
                GL_TRUE);
        } break;
        case TextureType::TEXTURE_2D_ARRAY_MS: {
            glTextureStorage3DMultisample(
                _textureData.getHandleHigh(),
                par.getParam<I32>(_ID("rendering.MSAAsampless"), 0),
                glInternalFormat,
                _width,
                _height,
                _numLayers,
                GL_TRUE);
        } break;
        case TextureType::TEXTURE_3D:
        case TextureType::TEXTURE_2D_ARRAY:
        case TextureType::TEXTURE_CUBE_MAP:
        case TextureType::TEXTURE_CUBE_ARRAY: {
            U32 numFaces = 1;
            if (_textureData._textureType == TextureType::TEXTURE_CUBE_MAP ||
                _textureData._textureType == TextureType::TEXTURE_CUBE_ARRAY) {
                numFaces = 6;
            }
            glTextureStorage3D(
                _textureData.getHandleHigh(),
                _mipMaxLevel,
                glInternalFormat,
                _width,
                _height,
                _numLayers * numFaces);
        } break;
        default:
            return;
    };
    
    _allocatedStorage = true;
}

void glTexture::loadData(const TextureLoadInfo& info,
                         const TextureDescriptor& descriptor,
                         const bufferPtr data,
                         const vec2<U16>& dimensions,
                         const vec2<U16>& mipLevels) {
    // This should never be called for compressed textures                            
    assert(!descriptor._compressed);
    _descriptor = descriptor;

    if (info._layerIndex == 0) {

        setMipMapRange(mipLevels.x, mipLevels.y);

        if (Config::Profile::USE_2x2_TEXTURES) {
            _width = _height = 2;
        }
        else {
            _width = dimensions.width;
            _height = dimensions.height;
        }

        assert(_width > 0 && _height > 0);
    } else {
        DIVIDE_ASSERT(
            _width == dimensions.width &&
            _height == dimensions.height,
            "glTexture error: Invalid dimensions for texture array layer");
    }

    if (!_allocatedStorage) {
        reserveStorage(info);
    }
    assert(_allocatedStorage);

    loadDataUncompressed(info, data);

    DIVIDE_ASSERT(_width > 0 && _height > 0,
        "glTexture error: Invalid texture dimensions!");

}

void glTexture::loadData(const TextureLoadInfo& info,
                         const TextureDescriptor& descriptor,
                         const vectorImpl<ImageTools::ImageLayer>& imageLayers,
                         const vec2<GLushort>& mipLevels) {
    _descriptor = descriptor;

    if (info._layerIndex == 0) {
            
        setMipMapRange(mipLevels.x, mipLevels.y);

        if (Config::Profile::USE_2x2_TEXTURES) {
            _width = _height = 2;
        } else {
            _width = imageLayers[0]._dimensions.width;
            _height = imageLayers[0]._dimensions.height;
        }

        assert(_width > 0 && _height > 0);
    } else {
        DIVIDE_ASSERT(
            _width == imageLayers[0]._dimensions.width && 
            _height == imageLayers[0]._dimensions.height,
            "glTexture error: Invalid dimensions for texture array layer");
    }

    if (!_allocatedStorage) {
        reserveStorage(info);
    }
    assert(_allocatedStorage);

    if (_descriptor._compressed) {
        loadDataCompressed(info, imageLayers);
    } else {
        loadDataUncompressed(info, (bufferPtr)imageLayers[0]._data.data());
    }

    DIVIDE_ASSERT(_width > 0 && _height > 0,
                  "glTexture error: Invalid texture dimensions!");
}

void glTexture::loadDataCompressed(const TextureLoadInfo& info,
                                   const vectorImpl<ImageTools::ImageLayer>& imageLayers) {
    GLenum glFormat = GLUtil::glImageFormatTable[to_uint(_descriptor.baseFormat())];
    GLint numMips = static_cast<GLint>(imageLayers.size());

    GL_API::setPixelPackUnpackAlignment();
    for (GLint i = 0; i < numMips; ++i) {
        const ImageTools::ImageLayer& layer = imageLayers[i];
        switch (_textureData._textureType) {
            case TextureType::TEXTURE_1D: {
                glCompressedTextureSubImage1D(
                    _textureData.getHandleHigh(),
                    i,
                    0,
                    layer._dimensions.width,
                    glFormat,
                    static_cast<GLsizei>(layer._size),
                    layer._data.data());
            } break;
            case TextureType::TEXTURE_2D: {
                glCompressedTextureSubImage2D(
                    _textureData.getHandleHigh(),
                    i,
                    0,
                    0,
                    layer._dimensions.width,
                    layer._dimensions.height,
                    glFormat,
                    static_cast<GLsizei>(layer._size),
                    layer._data.data());
            } break;

            case TextureType::TEXTURE_3D:
            case TextureType::TEXTURE_CUBE_MAP:
            case TextureType::TEXTURE_CUBE_ARRAY: {
                glCompressedTextureSubImage3D(
                    _textureData.getHandleHigh(),
                    i,
                    0,
                    0,
                    (info._cubeMapCount * 6) + info._layerIndex,
                    layer._dimensions.width,
                    layer._dimensions.height,
                    layer._dimensions.depth,
                    glFormat,
                    static_cast<GLsizei>(layer._size),
                    layer._data.data());
            } break;
            default:
                DIVIDE_UNEXPECTED_CALL("Unsupported texture format!");
                break;
        }
    };
    _mipMapsDirty = true;

    if (numMips == 1) {
        updateMipMaps();
    }
}

void glTexture::loadDataUncompressed(const TextureLoadInfo& info, bufferPtr data) {
    if (data) {
        GL_API::setPixelPackUnpackAlignment();
        switch (_textureData._textureType) {
            case TextureType::TEXTURE_1D: {
                glTextureSubImage1D(
                    _textureData.getHandleHigh(),
                    0,
                    0,
                    _width,
                    GLUtil::glImageFormatTable[to_uint(_descriptor.baseFormat())],
                    GLUtil::glDataFormat[to_uint(_descriptor.dataType())],
                    data);
                _mipMapsDirty = true;
            } break;
            case TextureType::TEXTURE_2D:
            case TextureType::TEXTURE_2D_MS: {
                glTextureSubImage2D(
                    _textureData.getHandleHigh(),
                    0,
                    0,
                    0,
                    _width,
                    _height,
                    GLUtil::glImageFormatTable[to_uint(_descriptor.baseFormat())],
                    GLUtil::glDataFormat[to_uint(_descriptor.dataType())],
                    data);
                _mipMapsDirty = true;
            } break;

            case TextureType::TEXTURE_3D:
            case TextureType::TEXTURE_2D_ARRAY:
            case TextureType::TEXTURE_2D_ARRAY_MS:
            case TextureType::TEXTURE_CUBE_MAP:
            case TextureType::TEXTURE_CUBE_ARRAY: {
                glTextureSubImage3D(
                    _textureData.getHandleHigh(),
                    0,
                    0,
                    0,
                    (info._cubeMapCount * 6) + info._layerIndex,
                    _width,
                    _height,
                    1,
                    GLUtil::glImageFormatTable[to_uint(_descriptor.baseFormat())],
                    GLUtil::glDataFormat[to_uint(_descriptor.dataType())],
                    data);
                _mipMapsDirty = info._layerIndex == _numLayers;
            } break;
        }
    }

    updateMipMaps();
}

bool glTexture::flushTextureState() {
    if (getState() == ResourceState::RES_LOADED) {
        if (_lockManager) {
            _lockManager->Wait(true);
            MemoryManager::DELETE(_lockManager);
        }
        updateSampler();
        updateMipMaps();
    }

    return true;
}

void glTexture::bind(U8 unit, bool flushStateOnRequest) {
    if (flushStateOnRequest) {
        flushTextureState();
    }

    GL_API::bindTexture(unit, _textureData.getHandleHigh(), _type, _textureData._samplerHash);
}

void glTexture::bindLayer(U8 slot, U8 level, U8 layer, bool layered, bool read, bool write, bool flushStateOnRequest) {
    if (flushStateOnRequest) {
        flushTextureState();
    }

    GLenum access = read ? (write ? GL_READ_WRITE : GL_READ_ONLY)
                            : (write ? GL_WRITE_ONLY : GL_NONE);
    GL_API::bindTextureImage(slot, _textureData.getHandleHigh(), level, layered, layer, access, 
                                GLUtil::glImageFormatTable[to_uint(_descriptor._internalFormat)]);
}

};

Commits for Divide-Framework/trunk/Source Code/Platform/Video/OpenGL/Textures/glTexture.cpp

Diff revisions: vs.
Revision Author Commited Message
727 Diff Diff IonutCava picture IonutCava Tue 14 Jun, 2016 16:01:38 +0000

[IonutCava]
- Remove node and cmd buffers from GFXDevice and add them as a new BufferData struct to RenderPass class.
— Each render pass holds its own buffers
- Improvements / fixes to the CSM code
- Added a global toggle to enable/disable custom memory allocators

722 Diff Diff IonutCava picture IonutCava Thu 09 Jun, 2016 16:15:33 +0000

[IonutCava]
- Restructure RenderTarget system:
— Separate attachments, use an attachment pool, use draw descriptors, require explicit information for RT calls, etc

720 Diff Diff IonutCava picture IonutCava Mon 06 Jun, 2016 15:51:06 +0000

[IonutCava]
- PVS-Studio guided static analysis fixes and optimizations
- Added a flag to SceneState to bypass the Save/Load system not needed for certain types of scenes (menus, cinematics, etc)

713 Diff Diff IonutCava picture IonutCava Wed 25 May, 2016 15:43:38 +0000

[IonutCava]
- Removed all unique_ptr’s from the code with classic new/delete pairs. They were seriously not needed.
- Added the concept of SceneComponent to scene specific classes: aiManager, lightPool, sceneGraph, physicsInterface etc
— This allowed the removal of the global GET_ACTIVE_SCENEGRAPH call;

712 Diff Diff IonutCava picture IonutCava Tue 24 May, 2016 16:18:37 +0000

[IonutCava]
- Added the concept of “buffer” to be used by GenericVertexData and ShaderBuffer without caring if it’s persistent or not.
— Persistent buffers handle their own locking instead of relying on the parent class
- HiZ occlusion culling fixes and optimizations

692 Diff Diff IonutCava picture IonutCava Wed 27 Apr, 2016 16:24:26 +0000

[IonutCava]
- Resource system cleanup:
— Resource name and resource file location (if any) is now passed only via explicit constructors! (ResourceDescriptors already contain this data, so why use setters?)
- Fix a bug with ortho projection matrix calculation (set m[3][3] to 1. this was skipped when the identity() call was changed to zero() in the ortho() function)
— This also fixed text rendering

670 Diff Diff IonutCava picture IonutCava Wed 06 Apr, 2016 16:21:35 +0000

[IonutCava]
- getInstance() renamed to instance() for simplicity (should really go away altogether, but that’s a different issue)
- some particle emitter threading updates

667 Diff Diff IonutCava picture IonutCava Fri 01 Apr, 2016 16:12:16 +0000

[IonutCava]
- replaced more strings with _ID equivalent
- streamlined shader include preprocessing
- improved intersection test performance for the Octree system at the cost of some memory

653 Diff Diff IonutCava picture IonutCava Fri 26 Feb, 2016 13:37:24 +0000

[IonutCava]
- Finished DXT texture loading system using DevIL
— NV_DDS implementation not finished yet

652 IonutCava picture IonutCava Thu 25 Feb, 2016 17:13:10 +0000

[IonutCava]
- More work on compressed texture loading (DXT1, DXT3, DXT5)