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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include "stdafx.h"

#include "Headers/glVertexArray.h"

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

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

namespace Divide {

namespace {
// Once vertex buffers reach a certain size, the for loop grows really really fast up to millions of iterations.
// Multiple if-checks per loop are not an option, so do some template hacks to speed this function up
template<bool normals, bool tangents, bool colour, bool texcoords, bool bones>
void fillSmallData5(const vectorImpl<VertexBuffer::Vertex>& dataIn, ByteBuffer& dataOut)
{
    for (const VertexBuffer::Vertex& data : dataIn) {
        dataOut << data._position;

        if (normals) {
            dataOut << data._normal;
        }

        if (tangents) {
            dataOut << data._tangent;
        }

        if (colour) {
            dataOut << data._colour.r;
            dataOut << data._colour.g;
            dataOut << data._colour.b;
            dataOut << data._colour.a;
        }

        if (texcoords) {
            dataOut << data._texcoord.s;
            dataOut << data._texcoord.t;
        }

        if (bones) {
            dataOut << data._weights.i;
            dataOut << data._indices.i;
        }
    }
}

template <bool normals, bool tangents, bool colour, bool texCoords>
void fillSmallData4(const vectorImpl<VertexBuffer::Vertex>& dataIn, ByteBuffer& dataOut, bool bones)
{
    if (bones) {
        fillSmallData5<normals, tangents, colour, texCoords, true>(dataIn, dataOut);
    } else {
        fillSmallData5<normals, tangents, colour, texCoords, false>(dataIn, dataOut);
    }
}

template <bool normals, bool tangents, bool colour>
void fillSmallData3(const vectorImpl<VertexBuffer::Vertex>& dataIn, ByteBuffer& dataOut, bool texCoords, bool bones)
{
    if (texCoords) {
        fillSmallData4<normals, tangents, colour, true>(dataIn, dataOut, bones);
    } else {
        fillSmallData4<normals, tangents, colour, false>(dataIn, dataOut, bones);
    }
}

template <bool normals, bool tangents>
void fillSmallData2(const vectorImpl<VertexBuffer::Vertex>& dataIn, ByteBuffer& dataOut, bool colour, bool texCoords, bool bones)
{
    if (colour) {
        fillSmallData3<normals, tangents, true>(dataIn, dataOut, texCoords, bones);
    } else {
        fillSmallData3<normals, tangents, false>(dataIn, dataOut, texCoords, bones);
    }
}

template <bool normals>
void fillSmallData1(const vectorImpl<VertexBuffer::Vertex>& dataIn, ByteBuffer& dataOut, bool tangents, bool colour, bool texCoords, bool bones)
{
    if (tangents) {
        fillSmallData2<normals, true>(dataIn, dataOut, colour, texCoords, bones);
    } else {
        fillSmallData2<normals, false>(dataIn, dataOut, colour, texCoords, bones);
    }
}

void fillSmallData(const vectorImpl<VertexBuffer::Vertex>& dataIn, ByteBuffer& dataOut, bool normals, bool tangents, bool colour, bool texCoords, bool bones)
{
    if (normals) {
        fillSmallData1<true>(dataIn, dataOut, tangents, colour, texCoords, bones);
    } else {
        fillSmallData1<false>(dataIn, dataOut, tangents, colour, texCoords, bones);
    }
}
};

GLUtil::glVAOCache glVertexArray::_VAOMap;

IMPLEMENT_CUSTOM_ALLOCATOR(glVertexArray, 0, 0)

void glVertexArray::cleanup() {
    _VAOMap.clear();
}

/// Default destructor
glVertexArray::glVertexArray(GFXDevice& context)
    : VertexBuffer(context),
      _refreshQueued(false),
      _formatInternal(GL_NONE)
{
    // We assume everything is static draw
    _usage = GL_STATIC_DRAW;
    _prevSize = -1;
    _prevSizeIndices = -1;
    _effectiveEntrySize = -1;
    _IBid = 0;

    _vaoCaches.fill(0);

    _useAttribute.fill(false);
    _attributeOffset.fill(0);
}

glVertexArray::~glVertexArray()
{
    GLUtil::releaseVBO(_VBHandle._id, _VBHandle._offset);
}

void glVertexArray::reset() {
    _usage = GL_STATIC_DRAW;
    _prevSize = -1;
    _prevSizeIndices = -1;

    _useAttribute.fill(false);
    _attributeOffset.fill(0);
    VertexBuffer::reset();
}

/// Trim down the Vertex vector to only upload the minimal ammount of data to the GPU
std::pair<bufferPtr, size_t> glVertexArray::getMinimalData() {
    bool useColour    = _useAttribute[to_base(VertexAttribute::ATTRIB_COLOR)];
    bool useNormals   = _useAttribute[to_base(VertexAttribute::ATTRIB_NORMAL)];
    bool useTangents  = _useAttribute[to_base(VertexAttribute::ATTRIB_TANGENT)];
    bool useTexcoords = _useAttribute[to_base(VertexAttribute::ATTRIB_TEXCOORD)];
    bool useBoneData  = _useAttribute[to_base(VertexAttribute::ATTRIB_BONE_INDICE)];

    size_t prevOffset = sizeof(vec3<F32>);
    if (useNormals) {
        _attributeOffset[to_base(VertexAttribute::ATTRIB_NORMAL)] = to_U32(prevOffset);
        prevOffset += sizeof(F32);
    }

    if (useTangents) {
        _attributeOffset[to_base(VertexAttribute::ATTRIB_TANGENT)] = to_U32(prevOffset);
        prevOffset += sizeof(F32);
    }

    if (useColour) {
        _attributeOffset[to_base(VertexAttribute::ATTRIB_COLOR)] = to_U32(prevOffset);
        prevOffset += sizeof(vec4<U8>);
    }

    if (useTexcoords) {
        _attributeOffset[to_base(VertexAttribute::ATTRIB_TEXCOORD)] = to_U32(prevOffset);
        prevOffset += sizeof(vec2<F32>);
    }

    if (useBoneData) {
        _attributeOffset[to_base(VertexAttribute::ATTRIB_BONE_WEIGHT)] = to_U32(prevOffset);
        prevOffset += sizeof(U32);
        _attributeOffset[to_base(VertexAttribute::ATTRIB_BONE_INDICE)] = to_U32(prevOffset);
        prevOffset += sizeof(U32);
    }

    _effectiveEntrySize = static_cast<GLsizei>(prevOffset);

    _smallData.reserve(_data.size() * _effectiveEntrySize);

    fillSmallData(_data, _smallData, useNormals, useTangents, useColour, useTexcoords, useBoneData);

    if (_staticBuffer && !keepData()) {
        _data.clear();
    } else {
        vectorAlg::shrinkToFit(_data);
    }

    return std::make_pair((bufferPtr)_smallData.contents(), _smallData.size());
}

/// Create a dynamic or static VB
bool glVertexArray::create(bool staticDraw) {
    // If we want a dynamic buffer, then we are doing something outdated, such
    // as software skinning, or software water rendering
    if (!staticDraw) {
        // OpenGLES support isn't added, but this check doesn't break anything,
        // so I'll just leave it here -Ionut
        GLenum usage = (_context.getAPI() == RenderAPI::OpenGLES)
                           ? GL_STREAM_DRAW
                           : GL_DYNAMIC_DRAW;
        if (usage != _usage) {
            _usage = usage;
            _refreshQueued = true;
        }
    }

    return VertexBuffer::create(staticDraw);
}

/// Update internal data based on construction method (dynamic/static)
bool glVertexArray::refresh() {
    // Dynamic LOD elements (such as terrain) need dynamic indices
    // We can manually override index usage (again, used by the Terrain
    // rendering system)
    assert(!_hardwareIndicesL.empty() || !_hardwareIndicesS.empty() &&
           "glVertexArray::refresh error: Invalid index data on Refresh()!");

    GLsizei nSizeIndices =
        (GLsizei)(usesLargeIndices() ? _hardwareIndicesL.size() * sizeof(U32)
                                     : _hardwareIndicesS.size() * sizeof(U16));

    bool indicesChanged = (nSizeIndices != _prevSizeIndices);
    _prevSizeIndices = nSizeIndices;
    _refreshQueued = indicesChanged;

    /// Can only add attributes for now. No removal support. -Ionut
    for (U8 i = 0; i < to_base(VertexAttribute::COUNT); ++i) {
        _useAttribute[i] = _useAttribute[i] || _attribDirty[i];
        if (!_refreshQueued) {
           if (_attribDirty[i]) {
               _refreshQueued = true;
           }
        }
    }
    _attribDirty.fill(false);


    Console::printfn("VAO HASHES: ");
    std::array<bool, to_base(RenderStagePass::count())> vaoCachesDirty;
    std::array<AttribFlags, to_base(RenderStagePass::count())> attributesPerStage;

    vaoCachesDirty.fill(false);
    for (RenderStagePass::PassIndex i = 0; i < RenderStagePass::count(); ++i) {
        const AttribFlags& stageMask = _attribMaskPerStage[i];

        AttribFlags& stageUsage = attributesPerStage[i];
        for (U8 j = 0; j < to_base(VertexAttribute::COUNT); ++j) {
            stageUsage[j] = _useAttribute[j] && stageMask[j];
        }

        size_t crtHash = 0;
        // Dirty on a VAO map cache miss
        vaoCachesDirty[i] = !_VAOMap.getVAO(stageUsage, _vaoCaches[i], crtHash);
        Console::printfn("      %d : %d (pass: %s)", to_base(RenderStagePass::stage(i)), to_U32(crtHash), (to_base(RenderStagePass::pass(i)) == 0 ? "PrePass" : "FwdPass"));    }

    std::pair<bufferPtr, size_t> bufferData = getMinimalData();
    // If any of the VBO's components changed size, we need to recreate the
    // entire buffer.

    GLsizei size = static_cast<GLsizei>(bufferData.second);
    bool sizeChanged = size != _prevSize;
    bool needsReallocation = false;
    U32 countRequirement = GLUtil::VBO::getChunkCountForSize(size);
    if (sizeChanged) {
        needsReallocation = countRequirement > GLUtil::VBO::getChunkCountForSize(_prevSize);
        _prevSize = size;
    }

    if (!_refreshQueued && !sizeChanged) {
        return false;
    }

    if (_VBHandle._id == 0 || needsReallocation) {
        if (_VBHandle._id != 0) {
            GLUtil::releaseVBO(_VBHandle._id, _VBHandle._offset);
        }
        // Generate a new Vertex Buffer Object
        GLUtil::commitVBO(countRequirement,_usage, _VBHandle._id, _VBHandle._offset);
        assert(_VBHandle._id != 0 && Locale::get(_ID("ERROR_VB_INIT")));
    }
    // Refresh buffer data (if this is the first call to refresh, this will be true)
    if (sizeChanged) {
        Console::d_printfn(Locale::get(_ID("DISPLAY_VB_METRICS")),
                           _VBHandle._id,
                           size,
                           countRequirement * GLUtil::VBO::MAX_VBO_CHUNK_SIZE_BYTES,
                           GLUtil::getVBOMemUsage(_VBHandle._id),
                           GLUtil::getVBOCount());
    }

    // Allocate sufficient space in our buffer
    glNamedBufferSubData(_VBHandle._id, _VBHandle._offset * GLUtil::VBO::MAX_VBO_CHUNK_SIZE_BYTES, size, bufferData.first);

    _smallData.clear();

    // Check if we need to update the IBO (will be true for the first Refresh() call)
    if (indicesChanged) {
        bufferPtr data = usesLargeIndices()
                             ? static_cast<bufferPtr>(_hardwareIndicesL.data())
                             : static_cast<bufferPtr>(_hardwareIndicesS.data());
        // Update our IB
        glNamedBufferData(_IBid, nSizeIndices, data, GL_STATIC_DRAW);
    }

    vectorImpl<GLuint> vaos;
    for (RenderStagePass::PassIndex pass = 0; pass < RenderStagePass::count(); ++pass) {
        if (vaoCachesDirty[pass]) {
            GLuint crtVao = _vaoCaches[pass];
            if (std::find_if(std::cbegin(vaos),
                                std::cend(vaos),
                                    [crtVao](GLuint vao) {
                                        return crtVao == vao;
                                    }) == std::cend(vaos))
            {
                vaos.push_back(crtVao);
            }
        }
    }

    for (U8 i = 0; i < to_U8(vaos.size()); ++i) {
        // Set vertex attribute pointers
        uploadVBAttributes(vaos[i]);
    }

    vaoCachesDirty.fill(false);

    // Possibly clear client-side buffer for all non-required attributes?
    // foreach attribute if !required then delete else skip ?
    _refreshQueued = false;

    return true;
}

/// This method creates the initial VAO and VB OpenGL objects and queues a
/// Refresh call
bool glVertexArray::createInternal() {
    // Avoid double create calls
    assert(_VBHandle._id == 0 && "glVertexArray error: Attempted to double create a VB");
    // Position data is a minim requirement
    if (_data.empty()) {
        Console::errorfn(Locale::get(_ID("ERROR_VB_POSITION")));
        return false;
    }
    VertexBuffer::createInternal();

    _formatInternal = GLUtil::glDataFormat[to_U32(_format)];
    // Generate an "Index Buffer Object"
    glCreateBuffers(1, &_IBid);
    if (Config::ENABLE_GPU_VALIDATION) {
        glObjectLabel(GL_BUFFER,
                      _IBid,
                      -1,
                      Util::StringFormat("DVD_VAO_INDEX_BUFFER_%d", _IBid).c_str());
    }
    // Validate buffer creation
    // Assert if the IB creation failed
    assert(_IBid != 0 && Locale::get(_ID("ERROR_IB_INIT")));
    // Calling refresh updates all stored information and sends it to the GPU
    return queueRefresh();
}

/// Render the current buffer data using the specified command
void glVertexArray::draw(const GenericDrawCommand& command) {
    bool useCmdBuffer = command.isEnabledOption(GenericDrawCommand::RenderOptions::RENDER_INDIRECT);

    // Make sure the buffer is current
    // Make sure we have valid data (buffer creation is deferred to the first activate call)
    if (_IBid == 0) {
        if (!createInternal()) {
            return;
        }
    }

    // Check if we have a refresh request queued up
    if (_refreshQueued) {
        if (!refresh()) {
            return;
        }
    }

    // Bind the vertex array object that in turn activates all of the bindings and pointers set on creation
    const RenderStagePass& stagePass = _context.getRenderStage();
    GLuint vao = _vaoCaches[to_U32(stagePass.index())];
    if (GL_API::setActiveVAO(vao)) {
        // If this is the first time the VAO is bound in the current loop, check
        // for primitive restart requests
        GL_API::togglePrimitiveRestart(_primitiveRestartEnabled);
    }
    // Bind the the vertex buffer and index buffer
    GL_API::bindActiveBuffer(vao,
                             0,
                             _VBHandle._id,
                             _VBHandle._offset * GLUtil::VBO::MAX_VBO_CHUNK_SIZE_BYTES,
                             _effectiveEntrySize);

    if (command.isEnabledOption(GenericDrawCommand::RenderOptions::RENDER_TESSELLATED)) {
        GL_API::setPatchVertexCount(command.patchVertexCount());
    }

    GLUtil::submitRenderCommand(command, useCmdBuffer, _formatInternal, _IBid);
}

/// Activate and set all of the required vertex attributes.
void glVertexArray::uploadVBAttributes(GLuint VAO) {
    // Bind the current VAO to save our attributes
    GL_API::setActiveVAO(VAO);
    static const U32 positionLoc = to_base(AttribLocation::VERTEX_POSITION);
    static const U32 colourLoc = to_base(AttribLocation::VERTEX_COLOR);
    static const U32 normalLoc = to_base(AttribLocation::VERTEX_NORMAL);
    static const U32 texCoordLoc = to_base(AttribLocation::VERTEX_TEXCOORD);
    static const U32 tangentLoc = to_base(AttribLocation::VERTEX_TANGENT);
    static const U32 boneWeightLoc = to_base(AttribLocation::VERTEX_BONE_WEIGHT);
    static const U32 boneIndiceLoc = to_base(AttribLocation::VERTEX_BONE_INDICE);
    
    glEnableVertexAttribArray(positionLoc);
    glVertexAttribFormat(positionLoc, 3, GL_FLOAT, GL_FALSE, _attributeOffset[positionLoc]);
    glVertexAttribBinding(positionLoc, 0);

    if (_useAttribute[colourLoc]) {
        glEnableVertexAttribArray(colourLoc);
        glVertexAttribFormat(colourLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, _attributeOffset[colourLoc]);
        glVertexAttribBinding(colourLoc, 0);
    } else {
        glDisableVertexAttribArray(colourLoc);
    }

    if (_useAttribute[normalLoc]) {
        glEnableVertexAttribArray(normalLoc);
        glVertexAttribFormat(normalLoc, 1, GL_FLOAT, GL_FALSE, _attributeOffset[normalLoc]);
        glVertexAttribBinding(normalLoc, 0);
    } else {
        glDisableVertexAttribArray(normalLoc);
    }

    if (_useAttribute[texCoordLoc]) {
        glEnableVertexAttribArray(texCoordLoc);
        glVertexAttribFormat(texCoordLoc, 2, GL_FLOAT, GL_FALSE, _attributeOffset[texCoordLoc]);
        glVertexAttribBinding(texCoordLoc, 0);
    } else {
        glDisableVertexAttribArray(texCoordLoc);
    }

    if (_useAttribute[tangentLoc]) {
        glEnableVertexAttribArray(tangentLoc);
        glVertexAttribFormat(tangentLoc, 1, GL_FLOAT, GL_FALSE, _attributeOffset[tangentLoc]);
        glVertexAttribBinding(tangentLoc, 0);
    } else {
        glDisableVertexAttribArray(tangentLoc);
    }

    if (_useAttribute[boneWeightLoc]) {
        assert(_useAttribute[boneIndiceLoc]);

        glEnableVertexAttribArray(boneWeightLoc);
        glEnableVertexAttribArray(boneIndiceLoc);
        glVertexAttribFormat(boneWeightLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, _attributeOffset[boneWeightLoc]);
        glVertexAttribIFormat(boneIndiceLoc, 4, GL_UNSIGNED_BYTE, _attributeOffset[boneIndiceLoc]);
        glVertexAttribBinding(boneWeightLoc, 0);
        glVertexAttribBinding(boneIndiceLoc, 0);
    } else {
        glDisableVertexAttribArray(boneWeightLoc);
        glDisableVertexAttribArray(boneIndiceLoc);
    }
}

};

Commits for Divide-Framework/trunk/Source Code/Platform/Video/OpenGL/Buffers/VertexBuffer/glVertexArray.cpp

Diff revisions: vs.
Revision Author Commited Message
912 Diff Diff IonutCava picture IonutCava Sun 02 Jul, 2017 23:42:39 +0000

[Ionut]
- Add experimental Weighted Blended Order Independed Transparency (ref: http://casual-effects.blogspot.co.uk/2015/03/implemented-weighted-blended-order.html)
— Add per drawbuffer blend
— All translucent renderbin items go via the new OIT 2-step rendering: accumulation and composition
- Make sure we have proper blend enabled for text rendering
- Add a primitive form of PushConstants (unused yet. Emulated via Uniform calls)
- Fix bug with XMLParser not using case insensitive key lookups

904 Diff Diff IonutCava picture IonutCava Sun 25 Jun, 2017 21:38:57 +0000

[Ionut]
- Rework the way GPU queries are handled via GenericDrawCommand rendering options.

895 Diff Diff IonutCava picture IonutCava Wed 21 Jun, 2017 21:10:26 +0000

[IonutCava]
- Reorder and cleanup OpenGL backend a bit.
- Small code cleanup
- Some small profile-guided optimizations

890 Diff Diff IonutCava picture IonutCava Sun 14 May, 2017 20:54:59 +0000

[Ionut]
- Add pre-compiled header support
- Initial code for per-stage tessellation computation

882 Diff Diff IonutCava picture IonutCava Thu 23 Mar, 2017 16:49:48 +0000

[IonutCava]
- More work on tessellated terrain rendering: got the mesh to render properly and match the collision mesh’s layout.
- Fixed a bug with GenericDrawCommand RenderOptions
- Added a “query number of generated primitives” option to GenericDrawCommand

877 Diff Diff IonutCava picture IonutCava Thu 16 Mar, 2017 21:55:59 +0000

[IonutCava]
- Reflection updates
- Sky parallax fixes
- Initial support for Tessellation patches (used by the new terrain system – W.I.P.)

872 Diff Diff IonutCava picture IonutCava Mon 06 Mar, 2017 23:23:20 +0000

[IonutCava]
- Optimise static casts

867 Diff Diff IonutCava picture IonutCava Mon 20 Feb, 2017 17:01:12 +0000

[IonutCava]
- Rename to_XNN and to_const_XNN functions to reflect the target data type they are converting to.
- Cleanup Angle namespace and remove all “inDegrees” parameters from rotation functions. Use aliases to differentiate between Degree and Radian parameters.
- Profile guided optimizations.

858 Diff Diff IonutCava picture IonutCava Sun 12 Feb, 2017 20:55:05 +0000

[IonutCava]
- More resource loading updates

852 IonutCava picture IonutCava Tue 07 Feb, 2017 17:17:27 +0000

[IonutCava]
- Separate Pass Type from Render Stage:
— Render Stage: Display, Shadow, Reflection, Refraction, etc
— Pass Type: Depth pass, Colour Pass, etc
- ToDo: Bug fixes for issues introduced with this change