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
/*
   Copyright (c) 2014 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 "VertexDataInterface.h"

namespace Divide {

class ShaderProgram;
/// 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 VertexBuffer : public VertexDataInterface {
protected:
    enum VertexAttribute{
        ATTRIB_POSITION = 0,
        ATTRIB_COLOR = 1,
        ATTRIB_NORMAL = 2,
        ATTRIB_TEXCOORD = 3,
        ATTRIB_TANGENT = 4,
        ATTRIB_BITANGENT = 5,
        ATTRIB_BONE_WEIGHT = 6,
        ATTRIB_BONE_INDICE = 7,
        VertexAttribute_PLACEHOLDER = 8
    };
public:

    VertexBuffer() : VertexDataInterface(),
                    _largeIndices(false),
                    _format(UNSIGNED_SHORT),
                    _primitiveRestartEnabled(false),
                    _indexDelimiter(0),
                    _currentPartitionIndex(0)
    {
        _LODcount = 0;
        Reset();
    }

    virtual ~VertexBuffer()
    {
        _LODcount = 1;
        Reset();
    }

    virtual bool Create(bool staticDraw = true) = 0;
    virtual void Destroy() = 0;
    /// Some engine elements, like physics or some geometry shading techniques require a triangle list.
    virtual bool SetActive() = 0;

    virtual void Draw(const GenericDrawCommand& command, bool skipBind = false) = 0;
    virtual void Draw(const vectorImpl<GenericDrawCommand>& commands, bool skipBind = false) = 0;

    inline void setLODCount(const U8 LODcount)               {_LODcount = LODcount;}

    inline void useLargeIndices(bool state = true)           {
        DIVIDE_ASSERT(!_created, "VertexBuffer error: Index format type specified before buffer creation!");
        _largeIndices = state; _format = _largeIndices ? UNSIGNED_INT : UNSIGNED_SHORT;
    }

    inline void reservePositionCount(U32 size)  {_dataPosition.reserve(size);}
    inline void reserveColourCount(U32 size)    {_dataColor.reserve(size);}
    inline void reserveNormalCount(U32 size)    {_dataNormal.reserve(size);}
    inline void reserveTangentCount(U32 size)   {_dataTangent.reserve(size);}
    inline void reserveBiTangentCount(U32 size) {_dataBiTangent.reserve(size);}

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

    inline void resizePositionCount(U32 size, const vec3<F32>& defaultValue = VECTOR3_ZERO)  {
        _dataPosition.resize(size,defaultValue);
    }

    inline void resizeColoCount(U32 size, const vec3<U8>& defaultValue = vec3<U8>())           {
        _dataColor.resize(size,defaultValue);
    }

    inline void resizeNormalCount(U32 size, const vec3<F32>& defaultValue = VECTOR3_ZERO)    {
        _dataNormal.resize(size,defaultValue);
    }

    inline void resizeTangentCount(U32 size, const vec3<F32>& defaultValue = VECTOR3_ZERO)   {
        _dataTangent.resize(size,defaultValue);
    }

    inline void resizeBiTangentCount(U32 size, const vec3<F32>& defaultValue = VECTOR3_ZERO) {
        _dataBiTangent.resize(size,defaultValue);
    }

    inline vectorImpl<vec2<F32> >&  getTexcoord()    { _attribDirty[ATTRIB_TEXCOORD] = true; return _dataTexcoord; }
    inline vectorImpl<vec4<U8>  >&  getBoneIndices() { _attribDirty[ATTRIB_BONE_INDICE] = true; return _boneIndices; }
    inline vectorImpl<vec4<F32> >&  getBoneWeights() { _attribDirty[ATTRIB_BONE_WEIGHT] = true; return _boneWeights; }

    inline const vectorImpl<vec3<F32> >&  getPosition()	   const {return _dataPosition;}
    inline const vectorImpl<vec3<U8>  >&  getColor()       const {return _dataColor;}
    inline const vectorImpl<vec3<F32> >&  getNormal()	   const {return _dataNormal;}
    inline const vectorImpl<vec3<F32> >&  getTangent()	   const {return _dataTangent;}
    inline const vectorImpl<vec3<F32> >&  getBiTangent()   const {return _dataBiTangent;}
    inline const vec3<F32>&               getPosition(U32 index)  const {return _dataPosition[index];}
    inline const vec3<F32>&               getNormal(U32 index)    const {return _dataNormal[index];}
    inline const vec3<F32>&               getTangent(U32 index)   const {return _dataTangent[index];}
    inline const vec3<F32>&               getBiTangent(U32 index) const {return _dataBiTangent[index];}
    virtual bool queueRefresh() = 0;

    inline bool usesLargeIndices()  const { return _largeIndices;}
    inline U32  getIndexCount()     const { return (U32)(_largeIndices ? _hardwareIndicesL.size() : _hardwareIndicesS.size());}
    inline U32  getIndex(U32 index) const { return _largeIndices ? _hardwareIndicesL[index] : _hardwareIndicesS[index];}
    
    inline const vectorImpl<U32>&  getIndicesL() const { return _hardwareIndicesL; }
    inline const vectorImpl<U16>&  getIndicesS() const { return _hardwareIndicesS; }

    inline void addIndex(U32 index){
        _largeIndices ? addIndexL(index) : addIndexS(static_cast<U16>(index));
    }

    inline void addIndexL(U32 index){
        _hardwareIndicesL.push_back(index);
    }

    inline void addIndexS(U16 index){
        _hardwareIndicesS.push_back(index);
    }

    inline void addRestartIndex(){
        _primitiveRestartEnabled = true;
        addIndex(_largeIndices ? Config::PRIMITIVE_RESTART_INDEX_L : Config::PRIMITIVE_RESTART_INDEX_S);
    }

    inline void addPosition(const vec3<F32>& pos){
        setMinMaxPosition(pos);
        _dataPosition.push_back(pos);
        _attribDirty[ATTRIB_POSITION] = true;
    }

    inline void addColor(const vec3<U8>& col){
        _dataColor.push_back(col);
        _attribDirty[ATTRIB_COLOR] = true;
    }

    inline void addTexCoord(const vec2<F32>& texCoord){
        _dataTexcoord.push_back(texCoord);
        _attribDirty[ATTRIB_TEXCOORD] = true;
    }

    inline void addNormal(const vec3<F32>& norm){
        _dataNormal.push_back(norm);
        _attribDirty[ATTRIB_NORMAL] = true;
    }

    inline void addTangent(const vec3<F32>& tangent){
        _dataTangent.push_back(tangent);
        _attribDirty[ATTRIB_TANGENT] = true;
    }

    inline void addBiTangent(const vec3<F32>& bitangent){
        _dataBiTangent.push_back(bitangent);
        _attribDirty[ATTRIB_BITANGENT] = true;
    }

    inline void addBoneIndex(const vec4<U8>& idx){
        _boneIndices.push_back(idx);
        _attribDirty[ATTRIB_BONE_INDICE] = true;
    }

    inline void addBoneWeight(const vec4<F32>& idx){
        _boneWeights.push_back(idx);
        _attribDirty[ATTRIB_BONE_WEIGHT] = true;
    }

    inline void modifyPositionValue(U32 index, const vec3<F32>& newValue){
        DIVIDE_ASSERT(index < _dataPosition.size(), "VertexBuffer error: Invalid position offset!");
        _dataPosition[index] = newValue;
        _attribDirty[ATTRIB_POSITION] = true;
    }

    inline void modifyColorValue(U32 index, const vec3<U8>& newValue) {
        DIVIDE_ASSERT(index < _dataColor.size(), "VertexBuffer error: Invalid color offset!");
        _dataColor[index] = newValue;
        _attribDirty[ATTRIB_COLOR] = true;
    }

    inline void modifyNormalValue(U32 index, const vec3<F32>& newValue)  {
        DIVIDE_ASSERT(index < _dataNormal.size(), "VertexBuffer error: Invalid normal offset!");
        _dataNormal[index] = newValue;
        _attribDirty[ATTRIB_NORMAL] = true;
    }

    inline void modifyTangentValue(U32 index, const vec3<F32>& newValue)  {
        DIVIDE_ASSERT(index < _dataTangent.size(), "VertexBuffer error: Invalid tangent offset!");
        _dataTangent[index] = newValue;
        _attribDirty[ATTRIB_TANGENT] = true;
    }

    inline void modifyBiTangentValue(U32 index, const vec3<F32>& newValue)  {
        DIVIDE_ASSERT(index < _dataBiTangent.size(), "VertexBuffer error: Invalid bitangent offset!");
        _dataBiTangent[index] = newValue;
        _attribDirty[ATTRIB_BITANGENT] = true;
    }

    inline void shrinkAllDataToFit() {
        shrinkToFit(_dataPosition);
        shrinkToFit(_dataColor);
        shrinkToFit(_dataNormal);
        shrinkToFit(_dataTexcoord);
        shrinkToFit(_dataTangent);
        shrinkToFit(_dataBiTangent);
        shrinkToFit(_boneWeights);
        shrinkToFit(_boneIndices);
    }

    inline size_t partitionBuffer(U32 currentIndexCount){
		_partitions.push_back(vectorAlg::makePair(getIndexCount() - currentIndexCount, currentIndexCount));
        _currentPartitionIndex = (U32)_partitions.size();
        _minPosition.push_back(std::numeric_limits<F32>::max());
        _maxPosition.push_back(std::numeric_limits<F32>::min());
        return _currentPartitionIndex - 1;
    }

    inline U32 getPartitionCount(U16 partitionIdx){
        if(_partitions.empty())
            return getIndexCount();

        DIVIDE_ASSERT(partitionIdx < _partitions.size(), "VertexBuffer error: Invalid partition offset!");
        return _partitions[partitionIdx].second;
    }

    inline U32 getPartitionOffset(U16 partitionIdx){
        if(_partitions.empty())
            return 0;

        DIVIDE_ASSERT(partitionIdx < _partitions.size(), "VertexBuffer error: Invalid partition offset!");
        return _partitions[partitionIdx].first;
    }

    inline U32 getLastPartitionOffset(){
        if(_partitions.empty())
            return 0;

        if (_partitions.empty()) return 0;
        return getPartitionOffset((U16)(_partitions.size() - 1));
    }

    inline void Reset() {
        _created = false;
        _primitiveRestartEnabled = false;
        _partitions.clear();
        _dataPosition.clear();
        _dataColor.clear();
        _dataNormal.clear();
        _dataTexcoord.clear();
        _dataTangent.clear();
        _dataBiTangent.clear();
        _boneIndices.clear();
        _boneWeights.clear();
        _hardwareIndicesL.clear();
        _hardwareIndicesS.clear();
        memset(_attribDirty, true, VertexAttribute_PLACEHOLDER * sizeof(bool));
        memset(_VBoffset, 0, VertexAttribute_PLACEHOLDER * sizeof(ptrdiff_t));
        _minPosition.resize(1, vec3<F32>(std::numeric_limits<F32>::max()));
        _maxPosition.resize(1, vec3<F32>(std::numeric_limits<F32>::min()));
        
    }

protected:
    /// Just before we render the frame
    virtual bool frameStarted(const FrameEvent& evt) { return VertexDataInterface::frameStarted(evt); }

    virtual void checkStatus() = 0;
    virtual bool Refresh() = 0;
    virtual bool CreateInternal() = 0;

    inline void setMinMaxPosition(const vec3<F32>& pos){
        vec3<F32>& min = _minPosition[_currentPartitionIndex];
        vec3<F32>& max = _maxPosition[_currentPartitionIndex];

        if (pos.x > max.x)	max.x = pos.x;
        if (pos.x < min.x)	min.x = pos.x;
        if (pos.y > max.y)	max.y = pos.y;
        if (pos.y < min.y)	min.y = pos.y;
        if (pos.z > max.z)	max.z = pos.z;
        if (pos.z < min.z)	min.z = pos.z;
    }

protected:

    ///Number of LOD nodes in this buffer
    U8          _LODcount; 
    ///The format of the buffer data
    GFXDataFormat _format;
    ///An index value that separates objects (OGL: primitive restart index)
    U32         _indexDelimiter;
    ptrdiff_t	_VBoffset[VertexAttribute_PLACEHOLDER];
        
    // first: offset, second: count
    vectorImpl<vectorAlg::pair<U32, U32> >   _partitions;
    ///Used for creating an "IB". If it's empty, then an outside source should provide the indices
    vectorImpl<U32>        _hardwareIndicesL;
    vectorImpl<U16>        _hardwareIndicesS;
    vectorImpl<vec3<F32> > _dataPosition;
    vectorImpl<vec3<U8>  > _dataColor;
    vectorImpl<vec3<F32> > _dataNormal;
    vectorImpl<vec2<F32> > _dataTexcoord;
    vectorImpl<vec3<F32> > _dataTangent;
    vectorImpl<vec3<F32> > _dataBiTangent;
    vectorImpl<vec4<U8>  > _boneIndices;
    vectorImpl<vec4<F32> > _boneWeights;
    vectorImpl<vec3<F32> > _minPosition;
    vectorImpl<vec3<F32> > _maxPosition;
    ///Use either U32 or U16 indices. Always prefer the later
    bool _largeIndices;
    /// Cache system to update only required data
    bool _attribDirty[VertexAttribute_PLACEHOLDER];
    bool _primitiveRestartEnabled;
    ///Was the data submitted to the GPU?
    bool _created;

private:
    U32 _currentPartitionIndex;
};

}; //namespace Divide
#endif

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

Diff revisions: vs.
Revision Author Commited Message
331 Diff Diff IonutCava picture IonutCava Sat 06 Dec, 2014 20:53:45 +0000

[Ionut]
- Limited line length to 132 characters to improve readability and diff-comparisons
- Refactored memory allocation/deallocation functions
- Fixed a few compatibility issues with HashMap.h
- Fixed a bug in GPU Skinning shaders (cast a float to int)

321 Diff Diff IonutCava picture IonutCava Wed 03 Sep, 2014 22:05:15 +0000

[Ionut]
- Added support (and enabled by default) for the EASTL library (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2271.html / https://github.com/paulhodge/EASTL)
— Strings, vectors and hash_maps can be combined from EASTL, STL and Boost (not all combinations work. e.g. EASTL strings with STL/Boost containers due to the lack of a proper hash function)

318 Diff Diff IonutCava picture IonutCava Sat 30 Aug, 2014 17:35:53 +0000

[Ionut]
- Wrapped the entire code in a “Divide” namespace
- VertexBuffers now call “shrink_to_fit” on all internal data storage
- Improved some vector performance by preferring “emplace_back” instead of “push_back” + proepr usage of reserve / resize
- Wrapped OIS specific types and classes in Divide::Input namespace
- Added the messageBox.layout file (forgot it in the previous few commits)

288 Diff Diff IonutCava picture IonutCava Wed 23 Jul, 2014 20:34:25 +0000

[Ionut]
- Added a common interface to GenericVertexData and VertexBuffer
- Removed deferred deletion of FrameListeners from FrameListenerManager. It complicated the code with no added benefit
- Added proper FrameListener unregister calls

286 Diff Diff IonutCava picture IonutCava Fri 18 Jul, 2014 16:32:03 +0000

[Ionut] [[BR]]

    • very buggy commit ** [[BR]]

- New rendering pipeline: [[BR]]
— Bumped OpenGL version to 4.4 [[BR]]
— Command based rendering. GFXDevice dispatches rendering commands to their respective buffers [[BR]]
— State changing is exclusively controlled by the GFXDevice class [[BR]]
— Material properties and transform matrices are stored in shader storage buffers that are updated after culling and uploaded once per RenderPass. Objects use drawIDs to index data in shaders [[BR]]
— Removed all custom matrices and other, per shader, bind data [[BR]]
— Removed per node light data. Light culling and shader buffers will replace the old light management system [[BR]]
— Framebuffers now use the Texture class for attachments [[BR]]
— Sampler objects now work like RenderStateBlocks. GL_API handles them and stores them in a hash indexed map [[BR]]
— Removed “prepareMaterial” calls. Pre-draw material setup is now only limited to texture binding [[BR]]
— Moved immediate mode emulation and primitive rendering from API level to GFXDevice level [[BR]]
— Framebuffer rendering must be completed with a call to end() to ensure proper MSAA resolve (asserts, otherwise) [[BR]]
- Fixed hash code generation for RenderStateBlock (and now, glSamplerObject) using boost::hash_combine [[BR]]
- Temporarily disabled XML caching for materials [[BR]]

279 Diff Diff IonutCava picture IonutCava Sat 28 Jun, 2014 16:20:46 +0000

[Ionut][[BR]]
- Unified draw command system into GenericDrawCommand [[BR]]
- Fixed double VB creation in Object3D [[BR]]
- Reworked triangle data computation (now Object3D’s responsibility) [[BR]]
- VertexArrays can now render using the primitive type specified in the draw command instead of the fixed type specified at creation [[BR]]
- Optimized terrain shader [[BR]]

274 Diff Diff IonutCava picture IonutCava Fri 13 Jun, 2014 12:56:03 +0000

[Ionut] [[BR]]
- Simplified buffer based rendering (VertexBuffer & GenericVertexData are now exclusively command based) [[BR]]
- Update bone buffer to persistent mapped-SSBO system with double-buffering [[BR]]
- Simplified sky rendering and (finally) reduced the sun’s radius [[BR]]

273 Diff Diff IonutCava picture IonutCava Wed 11 Jun, 2014 20:34:00 +0000

[Ionut] [[BR]]
- Improved batching part2: [[BR]]
— Moved all bone transformation matrices upload from uniform functions in SceneNode to Shader Storage Buffers in AnimationController class with updates controlled by SceneGraphNode’s AnimationComponent [[BR]]
-— No double-buffering or efficient mapping of data yet. Just raw buffer updates [[BR]]
— Replaced all RenderStateBlock* pointers with I64 hash values [[BR]]
-— Will be used later in draw commands as per-shader state hash [[BR]]
— Moved getShaderProgram from Material to Material::ShaderInfo [[BR]]
-— Improved redundant uniform checks [[BR]]
— Improved texture unit changing in case of redundant texture bind case (i.e. skip changing the texture unit) [[BR]]
[[BR]]
- Improved float and double comparison functions using algorithms described in http://randomascii.wordpress.com/2012/02/13/dont-store-that-in-a-float/ [[BR]]
- Added a new utility function, DIVIDE_ASSERT, to more easily bind an assert check with a specific message [[BR]]
- Added missing files from Forward+ renderer’s initial code [[BR]]

270 Diff Diff IonutCava picture IonutCava Thu 05 Jun, 2014 19:08:07 +0000

[Ionut][[BR]]
- SubMeshes now use a single vertex buffer (owned by the parent mesh) with proper offsetting and querying [[BR]]
— Implementation not optimal yet [[BR]]
- Cleaned up VertexBuffer related classes [[BR]]

268 IonutCava picture IonutCava Wed 04 Jun, 2014 10:20:27 +0000

[IonutCava] [[BR]]
- Terrain and vegetation updates: [[BR]]
— Use standard phong lighting model [[BR]]
— Separate grass patches per chunk [[BR]]
— Reduce number of required texture bind points for terrain by adding both textures and normal maps in a single texture array [[BR]]
— Draw terrain with 2 drawcalls (using glMultiDrawElements twice, once for LoD == 0 and once for LoD > 0) [[BR]]
[[BR]]
- Buffer management updates: [[BR]]
— Unified buffer binding system [[BR]]
— Made Uniform buffer objects visible to all classes and moved lightUBO to LightManager, matricesUBO to GFXDevice etc [[BR]]
— Added initial support for Shader Buffer Objects [[BR]]
— Moved viewport management to GFXDevice class [[BR]]
[[BR]]
- Rendering improvements: [[BR]]
— Added Hi-Z occlusion culling to instanceCull shader (using Hi-Z depth buffer constructed in GFXDevice class) [[BR]]
— Resolved issues with shadow maps overriding texture bind locations [[[BR]]
— Improved shader and shader program loading [[BR]]
— Improved material transparency support [[BR]]
— Added buffer lock manager class from “Approaching Zero Driver Overhead in OpenGL (Presented by NVIDIA)” speech at GDC14 [[BR]]