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
#include "Headers/glGenericVertexData.h"
#include "Hardware/Video/Headers/GFXDevice.h"

glGenericVertexData::glGenericVertexData(bool persistentMapped) : GenericVertexData(persistentMapped), _prevResult(nullptr),
                                                                                                       _bufferSet(nullptr),
                                                                                                       _bufferPersistent(nullptr),
                                                                                                       _bufferPersistentData(nullptr),
                                                                                                       _elementCount(nullptr),
                                                                                                       _elementSize(nullptr),
                                                                                                       _startDestOffset(nullptr),
                                                                                                       _readOffset(nullptr)
{
    _vertexArray[GVD_USAGE_DRAW] = _vertexArray[GVD_USAGE_FDBCK] = 0;
    _transformFeedback = 0;
    _numQueries  = 0;
    _currentWriteQuery = 0;
    _currentReadQuery = 0;
    _indirectDrawBuffer = 0;
    for (U8 i = 0; i < 2; ++i){
        _feedbackQueries[i] = nullptr;
        _resultAvailable[i] = nullptr;
    }

    _lockManager = persistentMapped ? New glBufferLockManager(true) : nullptr;
}

glGenericVertexData::~glGenericVertexData()
{
    for (U8 i = 0; i < _bufferObjects.size(); ++i){
        if (_bufferPersistent[i]){
            glBindBuffer(GL_ARRAY_BUFFER, _bufferObjects[i]);
            glUnmapBuffer(GL_ARRAY_BUFFER);
        }
    }

    glDeleteBuffers(1, &_indirectDrawBuffer);
    _indirectDrawBuffer = 0;

    if (!_bufferObjects.empty()){
        glDeleteVertexArrays(GVD_USAGE_PLACEHOLDER, &_vertexArray[0]);
        glDeleteBuffers((GLsizei)_bufferObjects.size(), &_bufferObjects[0]);
        _vertexArray[GVD_USAGE_DRAW] = _vertexArray[GVD_USAGE_FDBCK] = 0;
    }
    glDeleteTransformFeedbacks(1, &_transformFeedback);

    for (U8 i = 0; i < 2; ++i)
        glDeleteQueries(_numQueries, _feedbackQueries[i]);

    _transformFeedback = 0;
    _bufferObjects.clear();
    _feedbackBuffers.clear();

    for (U8 i = 0; i < 2; ++i){
        SAFE_DELETE_ARRAY(_feedbackQueries[i]);
        SAFE_DELETE_ARRAY(_resultAvailable[i]);
    }
    SAFE_DELETE_ARRAY(_prevResult);
    SAFE_DELETE_ARRAY(_bufferSet);
    SAFE_DELETE_ARRAY(_bufferPersistent);
    SAFE_DELETE_ARRAY(_elementCount);
    SAFE_DELETE_ARRAY(_elementSize);
    SAFE_DELETE_ARRAY(_startDestOffset);
    SAFE_DELETE_ARRAY(_readOffset);
    free(_bufferPersistentData);
    
    SAFE_DELETE(_lockManager);
}

void glGenericVertexData::Create(U8 numBuffers, U8 numQueries){
    // double create is not implemented yet as I don't quite need it. -Ionut
    DIVIDE_ASSERT(_bufferObjects.empty(), "glGenericVertexData error: create called with no buffers specified!");

    glGenVertexArrays(GVD_USAGE_PLACEHOLDER, &_vertexArray[0]);
    glGenTransformFeedbacks(1, &_transformFeedback);

    _bufferObjects.resize(numBuffers, 0);
    glGenBuffers(numBuffers, &_bufferObjects[0]);
    glGenBuffers(1, &_indirectDrawBuffer);

    _numQueries = numQueries;
    for (U8 i = 0; i < 2; ++i){
        _feedbackQueries[i] = New GLuint[_numQueries];
        _resultAvailable[i] = New bool[_numQueries];
    }

    for (U8 i = 0; i < 2; ++i){
        memset(_feedbackQueries[i], 0, sizeof(GLuint) * _numQueries);
        memset(_resultAvailable[i], false, sizeof(bool)   * _numQueries);
        glGenQueries(_numQueries, _feedbackQueries[i]);
    }

    _prevResult = New GLuint[_numQueries];
    memset(_prevResult, 0, sizeof(GLuint) * _numQueries);

    _bufferSet = new bool[numBuffers];
    memset(_bufferSet, false, numBuffers * sizeof(bool));

    _elementCount = new GLuint[numBuffers];
    memset(_elementCount, 0, numBuffers * sizeof(GLuint));

    _elementSize = new size_t[numBuffers];
    memset(_elementSize, 0, numBuffers * sizeof(size_t));
    
    _startDestOffset = new size_t[numBuffers];
    memset(_startDestOffset, 0, numBuffers * sizeof(size_t));

    _readOffset = new size_t[numBuffers];
    memset(_readOffset, 0, numBuffers * sizeof(size_t));

    _bufferPersistent = new bool[numBuffers];
    memset(_bufferPersistent, false, numBuffers * sizeof(bool));

    _bufferPersistentData = (GLvoid**)malloc(sizeof(GLvoid*) * numBuffers);

}

bool glGenericVertexData::frameStarted(const FrameEvent& evt) {
    if (!_bufferObjects.empty() && _doubleBufferedQuery){
        _currentWriteQuery = (_currentWriteQuery + 1) % 2;
        _currentReadQuery  = (_currentWriteQuery + 1) % 2;
    }

    return GenericVertexData::frameStarted(evt);
}

void glGenericVertexData::BindFeedbackBufferRange(U32 buffer, U32 elementCountOffset, size_t elementCount){
    DIVIDE_ASSERT(isFeedbackBuffer(buffer), "glGenericVertexData error: called bind buffer range for non-feedback buffer!");

    GL_API::setActiveTransformFeedback(_transformFeedback);
    glBindBufferRange(GL_TRANSFORM_FEEDBACK_BUFFER, getBindPoint(_bufferObjects[buffer]), _bufferObjects[buffer], elementCountOffset * _elementSize[buffer], elementCount * _elementSize[buffer]);
}

void glGenericVertexData::Draw(const GenericDrawCommand& command) {
    const IndirectDrawCommand& cmd = command._cmd;

    if (cmd.instanceCount == 0) return;

    bool feedbackActive = (command._drawToBuffer && !_feedbackBuffers.empty());

    GL_API::setActiveVAO(_vertexArray[feedbackActive ? GVD_USAGE_FDBCK : GVD_USAGE_DRAW]);

    SetAttributes(feedbackActive);

    // Write query result to the primCount field of the indirect draw command
    //glBindBuffer(GL_QUERY_BUFFER_AMD, _indirectDrawBuffer);
    //glGetQueryObjectuiv(queryId, GL_QUERY_RESULT_AVAILABLE, BUFFER_OFFSET(offsetof(DrawArraysIndirectCommand, primCount)));
    //GL_API::setActiveBuffer(GL_DRAW_INDIRECT_BUFFER, _indirectDrawBuffer);
    //glBufferData(GL_DRAW_INDIRECT_BUFFER, sizeof(IndirectDrawCommand), &cmd, GL_DYNAMIC_COPY);

    if (feedbackActive){
       GL_API::setActiveTransformFeedback(_transformFeedback);
       glBeginTransformFeedback(glPrimitiveTypeTable[command._type]);
       glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, _feedbackQueries[_currentWriteQuery][command._queryID]);

    }
    
    if(!Config::Profile::DISABLE_DRAWS)
        glDrawArraysIndirect(glPrimitiveTypeTable[command._type], &cmd);

    if (feedbackActive){
        glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
        glEndTransformFeedback();
        _resultAvailable[_currentWriteQuery][command._queryID] = true;
    }
   
    GFX_DEVICE.registerDrawCall();    
}

void glGenericVertexData::SetBuffer(U32 buffer, U32 elementCount, size_t elementSize, void* data, bool dynamic, bool stream, bool persistentMapped) {
    DIVIDE_ASSERT(buffer >= 0 && buffer < _bufferObjects.size(), "glGenericVertexData error: set buffer called for invalid buffer index!");
    // glBufferData on persistentMapped buffers is not allowed
    DIVIDE_ASSERT(!_bufferSet[buffer], "glGenericVertexData error: set buffer called for an already created buffer!");
    DIVIDE_ASSERT((persistentMapped && _persistentMapped) || !persistentMapped, "glGenericVertexData error: persistent map flag is not compatible with object details!");

    _elementCount[buffer] = elementCount;
    _elementSize[buffer] = elementSize;
    size_t bufferSize = elementCount * elementSize;

    GL_API::setActiveBuffer(GL_ARRAY_BUFFER, _bufferObjects[buffer]);
    if (persistentMapped){
        GLenum flag = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
        glBufferStorage(GL_ARRAY_BUFFER, bufferSize * 3, NULL, flag);

        _bufferPersistentData[buffer] = glMapBufferRange(GL_ARRAY_BUFFER, 0, bufferSize * 3, flag);
        if (data != nullptr){
            void* dst = nullptr;
            _lockManager->WaitForLockedRange(0, bufferSize * 3);
                dst = (unsigned char*)_bufferPersistentData[buffer] + bufferSize * 0;
                memcpy(dst, data, bufferSize);
                dst = (unsigned char*)_bufferPersistentData[buffer] + bufferSize * 1;
                memcpy(dst, data, bufferSize);
                dst = (unsigned char*)_bufferPersistentData[buffer] + bufferSize * 2;
                memcpy(dst, data, bufferSize);
            _lockManager->WaitForLockedRange(0, bufferSize * 3);
        }
    } else{
        GLenum flag = isFeedbackBuffer(buffer) ? (dynamic ? (stream ? GL_STREAM_COPY : GL_DYNAMIC_COPY) : GL_STATIC_COPY) :
                                                 (dynamic ? (stream ? GL_STREAM_DRAW : GL_DYNAMIC_DRAW) : GL_STATIC_DRAW);

        glBufferData(GL_ARRAY_BUFFER, _elementCount[buffer] * elementSize, data, flag);
    }
    _bufferSet[buffer] = true;
    _bufferPersistent[buffer] = persistentMapped;
}

void glGenericVertexData::UpdateBuffer(U32 buffer, U32 elementCount, void* data, U32 elementCountOffset, bool dynamic, bool stream) {
    size_t dataCurrentSize = elementCount * _elementSize[buffer];
    size_t offset = elementCountOffset * _elementSize[buffer];
    if (!_bufferPersistent[buffer]){
        glNamedBufferSubDataEXT(_bufferObjects[buffer], offset, dataCurrentSize, data);
    }else{
        GL_API::setActiveBuffer(GL_ARRAY_BUFFER, _bufferObjects[buffer]);
        size_t bufferSize = _elementCount[buffer] * _elementSize[buffer];

        _lockManager->WaitForLockedRange(_startDestOffset[buffer], bufferSize);

        void* dst = (unsigned char*)_bufferPersistentData[buffer] + _startDestOffset[buffer] + offset;
         memcpy(dst, data, dataCurrentSize);

        // Lock this area for the future.
        _lockManager->LockRange(_startDestOffset[buffer], bufferSize);
            
        _startDestOffset[buffer] = (_startDestOffset[buffer] + bufferSize) % (bufferSize * 3);
        _readOffset[buffer] = (_startDestOffset[buffer] + bufferSize) % (bufferSize * 3);
    }
}

void glGenericVertexData::SetAttributeInternal(AttributeDescriptor& descriptor){
    DIVIDE_ASSERT(_elementSize[descriptor._parentBuffer] != 0, "glGenericVertexData error: attribute's parent buffer has an invalid element size!");

    if(!descriptor._dirty) return;

    if(!descriptor._wasSet){
        glEnableVertexAttribArray(descriptor._index);
        descriptor._wasSet = true;
    }
    if (!_persistentMapped){
        GL_API::setActiveBuffer(GL_ARRAY_BUFFER, _bufferObjects[descriptor._parentBuffer]);
    }
    glVertexAttribPointer(descriptor._index, descriptor._componentsPerElement, glDataFormat[descriptor._type], descriptor._normalized ? GL_TRUE : GL_FALSE,
                          (GLsizei)descriptor._stride, (GLvoid*)(descriptor._elementCountOffset * _elementSize[descriptor._parentBuffer]));
    if (descriptor._divisor > 0) glVertexAttribDivisor(descriptor._index, descriptor._divisor);

    descriptor._dirty = false;
}

void glGenericVertexData::SetAttributes(bool feedbackPass) {
    attributeMap& map = feedbackPass ? _attributeMapFdbk : _attributeMapDraw;
    FOR_EACH(attributeMap::value_type& it, map){
        SetAttributeInternal(it.second);
    }
}

U32 glGenericVertexData::GetFeedbackPrimitiveCount(U8 queryID) {
    DIVIDE_ASSERT(queryID < _numQueries && !_bufferObjects.empty(), "glGenericVertexData error: Current object isn't ready for query processing!");

    U32 queryEntry = _doubleBufferedQuery ? _currentReadQuery : _currentWriteQuery;
    if (_resultAvailable[queryEntry][queryID]){
        // get the result of the previous query about the generated primitive count
        glGetQueryObjectuiv(_feedbackQueries[queryEntry][queryID], GL_QUERY_RESULT, &_prevResult[queryID]);
        _resultAvailable[queryEntry][queryID] = false;
    }

    return _prevResult[queryID];
}

Commits for Divide-Framework/trunk/Source Code/Hardware/Video/OpenGL/Buffers/VertexBuffer/glGenericVertexData.cpp

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

280 Diff Diff IonutCava picture IonutCava Sat 28 Jun, 2014 19:11:37 +0000

[Ionut] [[BR]]
- Improve ClipPlane management (bunch equations into the matrix shader buffer) [[BR]]
- Immediate mode emulation primitives set their own state based on specified hash instead of relying on state functions to be defined [[BR]]
- updateStates() function is now called right before the actual draw call to facilitate future batching work [[BR]]
- added tracked bool values (similar to those in ShaderInfo) to SGN class to fix selection bug. [[BR]]
— will probably create a “stateTracker” class to allow multiple classes to use tracked variables without all of the code duplication [[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]]

276 Diff Diff IonutCava picture IonutCava Fri 13 Jun, 2014 18:23:32 +0000

[Ionut][[BR]]
- Terrain now inherits from Object3D (instead of directly from SceneNode) [[BR]]
- Restructured RenderInstance to support multiple draw commands [[BR]]
— Multiple draw commands per instance are separated to different LoD batches automatically on render [[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]]

272 Diff Diff IonutCava picture IonutCava Wed 11 Jun, 2014 11:30:51 +0000

[Ionut] [[BR]]
- Added initial code for Forward+ rendering (grid based light culling) [[BR]]
- Simplified Light class [[BR]]
- Moved more light uniform data to ShaderBuffers [[BR]]
- Double buffered grass rendering [[BR]]
- Added a better attribute interface to GenericVertexData [[BR]]
- Fixed screenshot capture [[BR]]
- Fixed texture double-bind checks [[BR]]

271 Diff Diff IonutCava picture IonutCava Fri 06 Jun, 2014 19:27:02 +0000

[Ionut] [[BR]]
- Preparing GVD class to support persistent mapping [[BR]]
- Updates to grass buffer. Better eat up the cost of waiting for culling result than going through the severe visual artifacts cause by skipping draws [[BR]]

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]]

267 k1ngp1n picture k1ngp1n Wed 28 May, 2014 11:08:12 +0000

[Ionut] [[BR]]
- Improved texture handling: [[BR]]
— No more texture unbind (except when clearing the frame state) [[BR]]
— Moved texture bind checking to the GL_API class [[BR]]
- Fixed a crash related to vegetation loading due to compiler optimizations [[BR]]
- removed all “releaseMaterial” functions as they are no longer needed [[BR]]