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
/*
   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 <iostream>
#include <cassert>
#include "Utility/Headers/Vector.h"
#include "Utility/Headers/GUIDWrapper.h"
#include "Core/Math/Headers/MathClasses.h"
#include "Hardware/Video/Headers/RenderAPIEnums.h"

enum PrimitiveType;
enum GFXDataFormat;
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 GUIDWrapper {
protected:
    struct IndirectDrawCommand {
        IndirectDrawCommand() : count(0), instanceCount(0), firstIndex(0), baseVertex(0), baseInstance(0) {}
        U32  count;
        U32  instanceCount;
        U32  firstIndex;
        U32  baseVertex;
        U32  baseInstance;
    };

public:
    struct DeferredDrawCommand {
        DeferredDrawCommand() : _signedData(0), _unsignedData(0), _floatData(0.0f) {}

        IndirectDrawCommand _cmd;
        I32                 _signedData;
        U32                 _unsignedData;
        F32                 _floatData;
    };

    VertexBuffer(const PrimitiveType& type) : GUIDWrapper(),
                            _type(type),
                            _computeTriangles(true),
                            _largeIndices(false),
                            _format(UNSIGNED_SHORT),
                            _currentShader(nullptr),
                            _primitiveRestartEnabled(false),
                            _firstElement(0),
                            _instanceCount(1),
                            _indexDelimiter(0)
    {
        _depthPass = _forceOptimizeForDepth = false;
        _LODcount = _rangeCount = 0;
        _optimizeForDepth = true;
        Reset();
    }

    virtual ~VertexBuffer()
    {
        _LODcount = 1;
        _currentShader = nullptr;
        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 computeTriangleList() = 0;
    virtual bool SetActive() = 0;

    virtual void Draw(bool skipBind = false, const U8 LODindex = 0) = 0;
    virtual void DrawRange(bool skipBind = false) = 0;
    virtual void DrawCommands(const vectorImpl<DeferredDrawCommand>& commands, bool skipBind = false) = 0;

    virtual void setShaderProgram(ShaderProgram* const shaderProgram) = 0;
    inline ShaderProgram* const currentShader()  {return _currentShader;}

    inline void setLODCount(const U8 LODcount)               {_LODcount = LODcount;}
    inline void setInstanceCount(const U32 instanceCount)    {_instanceCount = instanceCount;}
    inline void setRangeCount(const U32 rangeCount)          {_rangeCount = rangeCount;}
    inline void setFirstElement(U32 firstElement)            {_firstElement = firstElement;}
    inline void setDepthPass(bool state = false)             {if(_optimizeForDepth) _depthPass = state;}
    inline void useLargeIndices(bool state = true)           {assert(!_created); _largeIndices = state; _format = _largeIndices ? UNSIGNED_INT : UNSIGNED_SHORT;}
    inline void computeTriangles(bool state = true)          {_computeTriangles = state;}
    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 reserveTriangleCount(U32 size)  {_dataTriangles.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()    { _texcoordDirty  = true; return _dataTexcoord;}
    inline vectorImpl<vec4<U8>  >&  getBoneIndices() { _indicesDirty   = true; return _boneIndices;}
    inline vectorImpl<vec4<F32> >&  getBoneWeights() { _weightsDirty   = true; return _boneWeights;}

    inline vectorImpl<vec3<U32> >&  getTriangles()   {return _dataTriangles;}
    inline const vec3<F32>&         getMinPosition() {return _minPosition;}
    inline const vec3<F32>&         getMaxPosition() {return _maxPosition;}

    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];}
    inline const PrimitiveType&           getPrimitiveType()      const {return _type;}
    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 void addIndex(U32 index){
        if(_largeIndices){
            _hardwareIndicesL.push_back(index);
        }else{
            _hardwareIndicesS.push_back(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;
        if (_largeIndices){
            _hardwareIndicesL.push_back(Config::PRIMITIVE_RESTART_INDEX_L);
        }else{
            _hardwareIndicesS.push_back(Config::PRIMITIVE_RESTART_INDEX_S);
        }
    }
    inline void addPosition(const vec3<F32>& pos){
        if(pos.x > _maxPosition.x)	_maxPosition.x = pos.x;
        if(pos.x < _minPosition.x)	_minPosition.x = pos.x;
        if(pos.y > _maxPosition.y)	_maxPosition.y = pos.y;
        if(pos.y < _minPosition.y)	_minPosition.y = pos.y;
        if(pos.z > _maxPosition.z)	_maxPosition.z = pos.z;
        if(pos.z < _minPosition.z)	_minPosition.z = pos.z;
        _dataPosition.push_back(pos);
        _positionDirty  = true;
    }

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

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

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

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

    inline void setIndiceLimits(const vec2<U32>& indiceLimits, U8 LODindex = 0) {
        assert(LODindex < _indiceLimits.size());
       _indiceLimits[LODindex] = indiceLimits;
    }

    inline void modifyPositionValue(U32 index, const vec3<F32>& newValue){
        assert(index < _dataPosition.size());
        _dataPosition[index] = newValue;
        _positionDirty = true;
    }

    inline void modifyColorValue(U32 index, const vec3<U8>& newValue) {
        assert(index < _dataColor.size());
        _dataColor[index] = newValue;
        _colorDirty = true;
    }

    inline void modifyNormalValue(U32 index, const vec3<F32>& newValue)  {
        assert(index < _dataNormal.size());
        _dataNormal[index] = newValue;
        _normalDirty = true;
    }

    inline void modifyTangentValue(U32 index, const vec3<F32>& newValue)  {
        assert(index < _dataTangent.size());
        _dataTangent[index] = newValue;
        _tangentDirty = true;
    }

    inline void modifyBiTangentValue(U32 index, const vec3<F32>& newValue)  {
        assert(index < _dataBiTangent.size());
        _dataBiTangent[index] = newValue;
        _bitangentDirty = true;
    }

    inline void optimizeForDepth(bool state = true,bool force = false) {
        _optimizeForDepth = state;
        _forceOptimizeForDepth = force;
    }

    inline void Reset() {
        _created = false;
        _primitiveRestartEnabled = false;
        _VBoffsetPosition = _VBoffsetColor = _VBoffsetNormal = _VBoffsetTexcoord = _VBoffsetTangent = 0;
        _VBoffsetBiTangent = _VBoffsetBoneIndices = _VBoffsetBoneWeights = 0;
        _VBoffsetBoneIndicesDEPTH = _VBoffsetBoneWeightsDEPTH = 0;
        _dataPosition.clear();
        _dataColor.clear();
        _dataNormal.clear();
        _dataTexcoord.clear();
        _dataTangent.clear();
        _dataBiTangent.clear();
        _boneIndices.clear();
        _boneWeights.clear();
        _hardwareIndicesL.clear();
        _hardwareIndicesS.clear();
        _dataTriangles.clear();
        _indiceLimits.resize(Config::SCENE_NODE_LOD, vec2<U32>(0,0));
        _positionDirty = _colorDirty = _normalDirty = _texcoordDirty = true;
        _tangentDirty = _bitangentDirty = _indicesDirty = _weightsDirty = true;
        _minPosition = vec3<F32>(10000.0f);
        _maxPosition = vec3<F32>(-10000.0f);
    }

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

protected:

    ///Number of LOD nodes in this buffer
    U8          _LODcount; 
    ///How many elements should we actually render when using "DrawRange"
    U32         _rangeCount;
    ///The format of the buffer data
    GFXDataFormat _format;
    ///Number of instances to draw
    U32         _instanceCount;
    ///An index value that separates ojects (OGL: primitive restart index)
    U32         _indexDelimiter;
    ptrdiff_t	_VBoffsetPosition;
    ptrdiff_t   _VBoffsetColor;
    ptrdiff_t	_VBoffsetNormal;
    ptrdiff_t	_VBoffsetTexcoord;
    ptrdiff_t	_VBoffsetTangent, _VBoffsetBiTangent;
    ptrdiff_t   _VBoffsetBoneIndices, _VBoffsetBoneWeights;
    ptrdiff_t   _VBoffsetBoneIndicesDEPTH, _VBoffsetBoneWeightsDEPTH;

    vectorImpl<vec2<U32> > _indiceLimits;
    ///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<U32> > _dataTriangles;	//< 3 indices, pointing to position values, that form a triangle in the mesh.
    vec3<F32> _minPosition,  _maxPosition;
    ///Use ither U32 or U16 indices. Always prefer the later
    bool _largeIndices;
    ///Some objects need triangle data in order for other parts of the engine to take advantage of direct data (physics, navmeshes, etc)
    bool _computeTriangles;
    /// Cache system to update only required data
    bool _positionDirty, _colorDirty, _normalDirty, _texcoordDirty;
    bool _tangentDirty, _bitangentDirty, _indicesDirty, _weightsDirty;
    ///Store position and animation data in different VB so rendering to depth is faster (it skips tex coords, tangent and bitangent data upload to GPU mem)
    ///Set this to FALSE if you need bump/normal/parallax mapping in depth pass (normal mapped object casts correct shadows)
    bool _optimizeForDepth;
    bool _forceOptimizeForDepth; //<Override vb requirements for optimization
    bool _primitiveRestartEnabled;
    ///If it's true, use depth only VB/VAO else use the regular buffers
    bool _depthPass;
    ///Was the data submited to the GPU?
    bool _created;
    ///Used for VertexAttribPointer data.
    ShaderProgram* _currentShader;
    ///Offset to the first element in the buffer
    U32 _firstElement;
    ///The format the data is in (TRIANGLES, TRIANGLE_STRIP,QUADS,etc)
    PrimitiveType  _type;
};

#endif

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

Diff revisions: vs.
Revision Author Commited Message
268 Diff Diff 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]]

264 Diff Diff k1ngp1n picture k1ngp1n Tue 27 May, 2014 13:04:38 +0000

[Ionut] [[BR]]
- New camera system: [[BR]]
— Stack based (push/pop active camera) [[BR]]
— Per camera frustum [[BR]]
— Per camera projection [[BR]]
— Shadow mapping now uses the new camera system (each light has a shadow camera) [[BR]]
[[BR]]
- New terrain, water and vegetation system: [[BR]]
— Direct import of terrain from L3DT both as 8-bit grayscale and 16-bit raw formats [[BR]]
— Multiple alpha maps for texture splatting support [[BR]]
— Per texture detail map support [[BR]]
— Transform feedback based visibility culling for vegetation [[BR]]
— Alpha-map based vegetation placing on terrain [[BR]]
— Water now uses a refraction map instead of alpha blending for better refraction effects [[BR]]
[[BR]]
- SceneGraph improvements: [[BR]]
— Better bounding box updates from child to parent [[BR]]
— Better onDraw / postDraw implementation [[BR]]
[[BR]]
- Performance improvements: [[BR]]
— Batch 2D rendering tasks together as much as possible[[BR]]
— Improve renderStateBlock hash generation for better sorting [[BR]]
— Move most state related gfx changes from API implementation to the GFXDevice class [[BR]]
— Move viewport stack to the GFXDevice class [[BR]]
— Added various performance counters (frame duration with round-robin query system, draw call count, etc) [[BR]]
— Fixed and improved clip plane management [[BR]]
— Delete client-side texture data after GPU upload [[BR]]
[[BR]]
- New features: [[BR]]
— Added callback support to the Task class so that a specific task may call a function in the main thread upon completion [[BR]]
— Added mouse-wheel zoom support to Orbit/ThirdPerson cameras [[BR]]
— Added 2D Texture Array support (e.g. used for terrain textures) [[BR]]

259 k1ngp1n picture k1ngp1n Sun 27 Apr, 2014 18:24:52 +0000

[Ionut][[BR]]
- Implemented Exponential Stable Cascaded Shadow Maps for directional lights (still buggy, but basically working) [[BR]]
— CSM settings (resolution, number of splits, etc) are part of the scene render state [[BR]]
- SGN can skip rendering for the current frame if they are not ready (e.g. OnDraw can now return false or materials can fail to bind) [[BR]]
- Frustum class is now using the Plane class [[BR]]
- Removed Math SIMD implementation as the compiler generated code will always be faster and safer [[BR]]
- Added a “STUBBED” macro for outputing to the error stream of important messages (instead of using #pragma message) [[BR]]
- Added “freeze time” logic to pause/resume updates with or without freezing the GUI too [[BR]]
- Added “processGUI” method the every Scene class to separate time-sensitive GUI updates from game logic loop [[BR]]
- GLSL <-> Vertex Buffer bind locations are automatically set for every vertex shader [[BR]]
- Added basic support for compute shaders [[BR]]
- Removed most OS define checks to make porting to a new platform easier [[BR]]
- Texture transparency check per-pixel is now using OpenMP [[BR]]
- Texture samplers can now set a border color for CLAMP_TO_BORDER wrap mode [[BR]]
- Removed “Object” suffix from GFX Classes (pixel buffer, vertex buffer, frame buffer, etc) [[BR]]
- Stop processing scene input when application looses focus [[BR]]
- SceneGraphNode class is now responsible for updating SGNComponents on draw and calling its _node’s OnDraw method [[BR]]
- UBOs can now list all of the active uniforms inside a block with info such as type and offset [[BR]]
- Removed deprecated ‘GLCheck’ macro [[BR]]
- A lot of bug fixes and performance improvements in various places [[BR]]