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
/*
   Copyright (c) 2017 DIVIDE-Studio
   Copyright (c) 2009 Ionut Cava

   This file is part of DIVIDE Framework.

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software
   and associated documentation files (the "Software"), to deal in the Software
   without restriction,
   including without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
   PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
   IN CONNECTION WITH THE SOFTWARE
   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 */

#ifndef _VERTEX_BUFFER_OBJECT_H
#define _VERTEX_BUFFER_OBJECT_H

#include "config.h"

#include "VertexDataInterface.h"
#include "Core/Headers/ByteBuffer.h"
#include "Platform/Video/Headers/RenderStagePass.h"
#include "Platform/Video/Headers/RenderAPIWrapper.h"

namespace Divide {

/// Vertex Buffer interface class to allow API-independent implementation of data
/// This class does NOT represent an API-level VB, such as: GL_ARRAY_BUFFER / D3DVERTEXBUFFER
/// It is only a "buffer" for "vertex info" abstract of implementation. (e.g.:
/// OGL uses a vertex array object for this)

class NOINITVTABLE VertexBuffer : public VertexDataInterface {
   public:
    struct Vertex {
        vec3<F32> _position;
        F32       _normal;
        F32       _tangent;
        UColour   _colour;
        vec2<F32> _texcoord;
        P32       _weights;
        P32       _indices;
    };

    VertexBuffer(GFXDevice& context);
    virtual ~VertexBuffer();

    virtual bool create(bool staticDraw = true);

    virtual void draw(const GenericDrawCommand& command) = 0;

    inline void useLargeIndices(bool state = true) {
        assert(_hardwareIndicesL.empty() && _hardwareIndicesS.empty() &&
               "VertexBuffer error: Index format type specified before "
               "buffer creation!");
        _format = state ? GFXDataFormat::UNSIGNED_INT
                        : GFXDataFormat::UNSIGNED_SHORT;
    }

    inline void setVertexCount(U32 size) { 
        _data.resize(size);
    }

    inline size_t getVertexCount() const {
        return _data.size();
    }

    inline const vectorImpl<Vertex>& getVertices() const {
        return _data;
    }

    inline void reserveIndexCount(U32 size) {
        usesLargeIndices() ? _hardwareIndicesL.reserve(size)
                           : _hardwareIndicesS.reserve(size);
    }

    inline void resizeVertexCount(U32 size, const Vertex& defaultValue = Vertex()) {
        _data.resize(size, defaultValue);
    }

    inline const vec3<F32>& getPosition(U32 index) const {
        return _data[index]._position;
    }

    inline const vec2<F32>& getTexCoord(U32 index) const {
        return _data[index]._texcoord;
    }

    inline F32 getNormal(U32 index) const {
        return _data[index]._normal;
    }

    inline F32 getNormal(U32 index, vec3<F32>& normalOut) const {
        F32 normal = getNormal(index);
        Util::UNPACK_VEC3(normal, normalOut.x, normalOut.y, normalOut.z);
        return normal;
    }

    inline F32 getTangent(U32 index) const {
        return _data[index]._tangent;
    }

    inline F32 getTangent(U32 index, vec3<F32>& tangentOut) const {
        F32 tangent = getTangent(index);
        Util::UNPACK_VEC3(tangent, tangentOut.x, tangentOut.y, tangentOut.z);
        return tangent;
    }

    inline P32 getBoneIndices(U32 index) const {
        return _data[index]._indices;
    }

    inline P32 getBoneWeightsPacked(U32 index) const {
        return _data[index]._weights;
    }

    inline FColour getBoneWeights(U32 index) const {
        const P32& weight = _data[index]._weights;
        return FColour(CHAR_TO_FLOAT_SNORM(weight.b[0]),
                       CHAR_TO_FLOAT_SNORM(weight.b[1]),
                       CHAR_TO_FLOAT_SNORM(weight.b[2]),
                       CHAR_TO_FLOAT_SNORM(weight.b[3]));
    }

    virtual bool queueRefresh() = 0;

    inline bool usesLargeIndices() const { 
        return _format == GFXDataFormat::UNSIGNED_INT;
    }

    inline U32 getIndexCount() const {
        return to_U32(usesLargeIndices() ? _hardwareIndicesL.size()
                                          : _hardwareIndicesS.size());
    }

    inline U32 getIndex(U32 index) const {
        assert(index < getIndexCount());

        return usesLargeIndices() ? _hardwareIndicesL[index]
                                  : _hardwareIndicesS[index];
    }

    template<typename T>
    const vectorImplBest<T>& getIndices() const {
        static_assert(false, "VertexBuffer::getIndices error: Need valid index data type!");
    }

    template <typename T>
    inline void addIndex(T index) {
        if (usesLargeIndices()) {
            _hardwareIndicesL.push_back(to_U32(index));
        } else {
            _hardwareIndicesS.push_back(to_U16(index));
        }
    }

    template <typename T>
    inline void addIndices(const vectorImplBest<T>& indices, bool containsRestartIndex) {
        if (usesLargeIndices()) {
            std::transform(std::cbegin(indices),
                           std::cend(indices),
                           std::back_inserter(_hardwareIndicesL),
                           static_caster<T, U32>());
                           
        } else {
            std::transform(std::cbegin(indices),
                           std::cend(indices),
                           std::back_inserter(_hardwareIndicesS),
                           static_caster<T, U16>());
        }

        if (containsRestartIndex) {
            hasRestartIndex(true);
        }
    }

    inline void hasRestartIndex(bool state) {
        _primitiveRestartEnabled = state;
    }

    inline void addRestartIndex() {
        hasRestartIndex(true);
        if (usesLargeIndices()) {
            addIndex(Config::PRIMITIVE_RESTART_INDEX_L);
        } else {
            addIndex(Config::PRIMITIVE_RESTART_INDEX_S);
        }
    }

    inline void modifyPositionValues(U32 indexOffset, const vectorImpl<vec3<F32>>& newValues) {
       assert(indexOffset + newValues.size() - 1 < _data.size());
       DIVIDE_ASSERT(_staticBuffer == false ||
           (_staticBuffer == true && !_data.empty()),
           "VertexBuffer error: Modifying static buffers after creation is not allowed!");

       vectorImpl<Vertex>::iterator it = _data.begin() + indexOffset;
       for (const vec3<F32>& value : newValues) {
            (it++)->_position.set(value);
       }

       _attribDirty[to_base(VertexAttribute::ATTRIB_POSITION)] = true;
    }

    inline void modifyPositionValue(U32 index, const vec3<F32>& newValue) {
        modifyPositionValue(index, newValue.x, newValue.y, newValue.z);
    }

    inline void modifyPositionValue(U32 index, F32 x, F32 y, F32 z) {
        assert(index < _data.size());

        DIVIDE_ASSERT(_staticBuffer == false ||
                      (_staticBuffer == true && !_data.empty()),
                      "VertexBuffer error: Modifying static buffers after creation is not allowed!");

        _data[index]._position.set(x, y, z);
        _attribDirty[to_base(VertexAttribute::ATTRIB_POSITION)] = true;
    }

    inline void modifyColourValue(U32 index, const UColour& newValue) {
        modifyColourValue(index, newValue.r, newValue.g, newValue.b, newValue.a);
    }

    inline void modifyColourValue(U32 index, U8 r, U8 g, U8 b, U8 a) {
        assert(index < _data.size());

        DIVIDE_ASSERT(_staticBuffer == false ||
                      (_staticBuffer == true && !_data.empty()),
                      "VertexBuffer error: Modifying static buffers after creation is not allowed!");

        _data[index]._colour.set(r, g, b, a);
        _attribDirty[to_base(VertexAttribute::ATTRIB_COLOR)] = true;
    }

    inline void modifyNormalValue(U32 index, const vec3<F32>& newValue) {
        modifyNormalValue(index, newValue.x, newValue.y, newValue.z);
    }

    inline void modifyNormalValue(U32 index, F32 x, F32 y, F32 z) {
        assert(index < _data.size());

        DIVIDE_ASSERT(_staticBuffer == false ||
                      (_staticBuffer == true && !_data.empty()),
                      "VertexBuffer error: Modifying static buffers after creation is not allowed!");

        _data[index]._normal = Util::PACK_VEC3(x, y, z);
        _attribDirty[to_base(VertexAttribute::ATTRIB_NORMAL)] = true;
    }

    inline void modifyTangentValue(U32 index, const vec3<F32>& newValue) {
        modifyTangentValue(index, newValue.x, newValue.y, newValue.z);
    }

    inline void modifyTangentValue(U32 index, F32 x, F32 y, F32 z) {
        assert(index < _data.size());

        DIVIDE_ASSERT(_staticBuffer == false ||
                     (_staticBuffer == true && !_data.empty()),
                      "VertexBuffer error: Modifying static buffers after creation is not allowed!");

        _data[index]._tangent = Util::PACK_VEC3(x, y, z);
        _attribDirty[to_base(VertexAttribute::ATTRIB_TANGENT)] = true;
    }

    inline void modifyTexCoordValue(U32 index, const vec2<F32>& newValue) {
        modifyTexCoordValue(index, newValue.s, newValue.t);
    }

    inline void modifyTexCoordValue(U32 index, F32 s, F32 t) {
        assert(index < _data.size());

        DIVIDE_ASSERT(_staticBuffer == false ||
                      (_staticBuffer == true && !_data.empty()),
                      "VertexBuffer error: Modifying static buffers after creation is not allowed!");

        _data[index]._texcoord.set(s, t);
        _attribDirty[to_base(VertexAttribute::ATTRIB_TEXCOORD)] = true;
    }

    inline void modifyBoneIndices(U32 index, P32 indices) {
        assert(index < _data.size());

        DIVIDE_ASSERT(_staticBuffer == false ||
                      (_staticBuffer == true && !_data.empty()),
                      "VertexBuffer error: Modifying static buffers after creation is not allowed!");

        _data[index]._indices = indices;
        _attribDirty[to_base(VertexAttribute::ATTRIB_BONE_INDICE)] = true;
    }

    inline void modifyBoneWeights(U32 index, const FColour& weights) {
        P32 boneWeights;
        boneWeights.b[0] = FLOAT_TO_CHAR_SNORM(weights.x);
        boneWeights.b[1] = FLOAT_TO_CHAR_SNORM(weights.y);
        boneWeights.b[2] = FLOAT_TO_CHAR_SNORM(weights.z);
        boneWeights.b[3] = FLOAT_TO_CHAR_SNORM(weights.w);
        modifyBoneWeights(index, boneWeights);
    }

    inline void modifyBoneWeights(U32 index, P32 packedWeights) {
        assert(index < _data.size());

        DIVIDE_ASSERT(_staticBuffer == false ||
                      (_staticBuffer == true && !_data.empty()),
                      "VertexBuffer error: Modifying static buffers after creation is not allowed!");

        _data[index]._weights = packedWeights;
        _attribDirty[to_base(VertexAttribute::ATTRIB_BONE_WEIGHT)] = true;
    }

    inline size_t partitionBuffer() {
        U32 previousIndexCount = _partitions.empty() ? 0 : _partitions.back().second;
        U32 previousOffset = _partitions.empty() ? 0 : _partitions.back().first;
        U32 partitionedIndexCount = previousIndexCount + previousOffset;
        _partitions.push_back(std::make_pair(partitionedIndexCount, getIndexCount() - partitionedIndexCount));
        return _partitions.size() - 1;
    }

    inline U32 getPartitionIndexCount(U16 partitionID) {
        if (_partitions.empty()) {
            return getIndexCount();
        }
        assert(partitionID < _partitions.size() && "VertexBuffer error: Invalid partition offset!");
        return _partitions[partitionID].second;
    }

    inline U32 getPartitionOffset(U16 partitionID) {
        if (_partitions.empty()) {
            return 0;
        }
        assert(partitionID < _partitions.size() && "VertexBuffer error: Invalid partition offset!");
        return _partitions[partitionID].first;
    }

    virtual void reset() {
        _staticBuffer = false;
        _primitiveRestartEnabled = false;
        _partitions.clear();
        _data.clear();
        _hardwareIndicesL.clear();
        _hardwareIndicesS.clear();
        _attribDirty.fill(false);
    }

    void fromBuffer(VertexBuffer& other);
    bool deserialize(ByteBuffer& dataIn);
    bool serialize(ByteBuffer& dataOut) const;

    static void setAttribMasks(const AttribFlags& flagMask);
    static void setAttribMask(const RenderStagePass& stagePass, const AttribFlags& flagMask);

    inline void keepData(const bool state) {
        _keepDataInMemory = state;
    }

    virtual void computeNormals();
    virtual void computeTangents();

   protected:
    static std::array<AttribFlags, to_base(RenderStagePass::count())> _attribMaskPerStage;

    virtual bool refresh() = 0;
    virtual bool createInternal();

    inline bool keepData() const {
        return _keepDataInMemory;
    }

   protected:
    void notifyListeners() const;

   protected:
    /// Flag used to prevent clearing of the _data vector for static buffers
    bool _keepDataInMemory;
    /// If this flag is true, no further modification are allowed on the buffer (static geometry)
    bool _staticBuffer;
    /// The format of the buffer data
    GFXDataFormat _format;
    // first: offset, second: count
    vectorImpl<std::pair<U32, U32> > _partitions;
    /// Used for creating an "IB". If it's empty, then an outside source should
    /// provide the indices
    vectorImplBest<U32> _hardwareIndicesL;
    vectorImplBest<U16> _hardwareIndicesS;
    vectorImpl<Vertex> _data;
    /// Cache system to update only required data
    std::array<bool, to_base(VertexAttribute::COUNT)> _attribDirty;
    bool _primitiveRestartEnabled;
};

template<>
inline const vectorImplBest<U32>& VertexBuffer::getIndices<U32>() const {
    return _hardwareIndicesL;
}

template<>
inline const vectorImplBest<U16>& VertexBuffer::getIndices<U16>() const {
    return _hardwareIndicesS;
}
};  // namespace Divide
#endif

Commits for Divide-Framework/trunk/Source Code/Platform/Video/Buffers/VertexBuffer/Headers/VertexBuffer.h

Diff revisions: vs.
Revision Author Commited Message
1004 Diff Diff IonutCava picture IonutCava Wed 20 Dec, 2017 17:16:32 +0000

[Ionut]
- More GUI cleanup (added multiple GUIButton events)
- Typedef vec4<U8> and vec4<F32> to UColour and FColour respectively for easier to read code.

950 Diff Diff IonutCava picture IonutCava Sun 22 Oct, 2017 22:25:05 +0000

[Ionut]
- Revamp camera management
- More work on improving the command buffer system:
— Performance tweaks
— Validation
— Debug features
- Some code cleanup for Object3D stuff (preRender/onRender changes)

Still haven’t fixed random flashing issue

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

869 IonutCava picture IonutCava Sun 26 Feb, 2017 23:25:54 +0000

[IonutCava]
- Reduce loading time using profile guided optimizations
- Camera/Quaternion work in an attempt to fix a weird Yaw-rotation bug.