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
#include "Headers/glUniformBuffer.h"

#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/Video/OpenGL/Headers/glResources.h"
#include "Platform/Video/Shaders/Headers/ShaderProgram.h"
#include "Platform/Video/OpenGL/Buffers/Headers/glBufferImpl.h"
#include "Platform/Video/OpenGL/Buffers/Headers/glBufferLockManager.h"
#include "Core/Headers/ParamHandler.h"

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

namespace Divide {

IMPLEMENT_CUSTOM_ALLOCATOR(glUniformBuffer, 0, 0)
glUniformBuffer::glUniformBuffer(GFXDevice& context,
                                 const U32 ringBufferLength,
                                 bool unbound,
                                 bool persistentMapped,
                                 BufferUpdateFrequency frequency)
    : ShaderBuffer(context, ringBufferLength, unbound, persistentMapped, frequency),
      _mappedBuffer(nullptr),
      _alignment(0),
      _allignedBufferSize(0),
      _target(_unbound ? GL_SHADER_STORAGE_BUFFER : GL_UNIFORM_BUFFER)
{
    _updated = false;
    _alignmentRequirement = _unbound ? ParamHandler::instance().getParam<I32>(_ID("rendering.SSBOAligment"), 32)
                                     : ParamHandler::instance().getParam<I32>(_ID("rendering.UBOAligment"), 32);
    if (persistentMapped) {
        _buffer = MemoryManager_NEW glPersistentBuffer(_target);
    } else {
        _buffer = MemoryManager_NEW glRegularBuffer(_target);
    }
}

glUniformBuffer::~glUniformBuffer() 
{
    destroy();
    MemoryManager::DELETE(_buffer);
}

GLuint glUniformBuffer::bufferID() const {
    return _buffer->bufferID();
}

void glUniformBuffer::destroy() {
    _buffer->destroy();
    for (AtomicCounter& counter : _atomicCounters) {
        glDeleteBuffers(1, &counter._handle);
    }
    _atomicCounters.clear();
}

void glUniformBuffer::create(U32 primitiveCount, ptrdiff_t primitiveSize, U32 sizeFactor) {
    ShaderBuffer::create(primitiveCount, primitiveSize, sizeFactor);
    _allignedBufferSize = realign_offset(_bufferSize, _alignmentRequirement);
    _buffer->create(_frequency, _allignedBufferSize * sizeFactor * queueLength());
}

void glUniformBuffer::getData(ptrdiff_t offsetElementCount,
                              ptrdiff_t rangeElementCount,
                              bufferPtr result) const {
    if (rangeElementCount > 0) {
        ptrdiff_t range = rangeElementCount * _primitiveSize;
        ptrdiff_t offset = offsetElementCount * _primitiveSize;

        DIVIDE_ASSERT(offset + range <= (ptrdiff_t)_allignedBufferSize,
            "glUniformBuffer::UpdateData error: was called with an "
            "invalid range (buffer overflow)!");

        offset += queueWriteIndex() * _allignedBufferSize;

        glGetNamedBufferSubData(bufferID(), offset, range, result);
    }
}

void glUniformBuffer::updateData(ptrdiff_t offsetElementCount,
                                 ptrdiff_t rangeElementCount,
                                 const bufferPtr data) {

    if (rangeElementCount == 0) {
        return;
    }

    ptrdiff_t range = rangeElementCount * _primitiveSize;
    ptrdiff_t offset = offsetElementCount * _primitiveSize;

    DIVIDE_ASSERT(offset + range <= (ptrdiff_t)_allignedBufferSize,
        "glUniformBuffer::UpdateData error: was called with an "
        "invalid range (buffer overflow)!");

    offset += queueWriteIndex() * _allignedBufferSize;

    _buffer->updateData(offset, range, data);
}

bool glUniformBuffer::bindRange(U32 bindIndex, U32 offsetElementCount, U32 rangeElementCount) {
    if (rangeElementCount == 0)  {
        return false;
    }

    GLuint range = static_cast<GLuint>(rangeElementCount * _primitiveSize);
    GLuint offset = static_cast<GLuint>(offsetElementCount * _primitiveSize);
    offset += static_cast<GLuint>(queueReadIndex() * _allignedBufferSize);

    return _buffer->bindRange(bindIndex, offset, range);
}

bool glUniformBuffer::bind(U32 bindIndex) {
    return bindRange(bindIndex, 0, _primitiveCount);
}

void glUniformBuffer::addAtomicCounter(U32 sizeFactor) {
    vectorImpl<GLuint> data(sizeFactor, 0);

    GLuint atomicsBuffer;
    glCreateBuffers(1, &atomicsBuffer);
    glNamedBufferData(atomicsBuffer, sizeof(GLuint) * sizeFactor, data.data(), GL_DYNAMIC_READ);
    _atomicCounters.push_back({atomicsBuffer, sizeFactor, 0, 1 % sizeFactor});
}

U32 glUniformBuffer::getAtomicCounter(U32 counterIndex) {
    if (counterIndex >= to_uint(_atomicCounters.size())) {
        return 0;
    }

    GLuint result = 0;
    AtomicCounter& counter = _atomicCounters.at(counterIndex);
    glGetNamedBufferSubData(counter._handle, counter._readHead * sizeof(GLuint), sizeof(GLuint), &result);
    return result;
}

void glUniformBuffer::bindAtomicCounter(U32 counterIndex, U32 bindIndex) {
    if (counterIndex >= to_uint(_atomicCounters.size())) {
        return;
    }
    AtomicCounter& counter = _atomicCounters.at(counterIndex);
    glBindBufferRange(GL_ATOMIC_COUNTER_BUFFER, bindIndex, counter._handle, counter._writeHead * sizeof(GLuint), sizeof(GLuint));
    counter._writeHead = (counter._writeHead + 1) % counter._sizeFactor;
    counter._readHead = (counter._readHead + 1) % counter._sizeFactor;
}

void glUniformBuffer::resetAtomicCounter(U32 counterIndex) {
    if (counterIndex > to_uint(_atomicCounters.size())) {
        return;
    }
    AtomicCounter& counter = _atomicCounters.at(counterIndex);
    vectorImpl<GLuint> data(counter._sizeFactor, 0);
    glNamedBufferSubData(counter._handle, counter._readHead * sizeof(GLuint), sizeof(GLuint), data.data());
}

void glUniformBuffer::printInfo(const ShaderProgram* shaderProgram,
                                U32 bindIndex) {
    GLuint prog = shaderProgram->getID();
    GLuint block_index = bindIndex;

    if (prog <= 0 || _unbound) {
        return;
    }

    // Fetch uniform block name:
    GLint name_length;
    glGetActiveUniformBlockiv(prog, block_index, GL_UNIFORM_BLOCK_NAME_LENGTH,
                              &name_length);
    if (name_length <= 0) {
        return;
    }

    stringImpl block_name(name_length, 0);
    glGetActiveUniformBlockName(prog, block_index, name_length, NULL,
                                &block_name[0]);

    GLint block_size;
    glGetActiveUniformBlockiv(prog, block_index, GL_UNIFORM_BLOCK_DATA_SIZE,
                              &block_size);
    // Fetch info on each active uniform:
    GLint active_uniforms = 0;
    glGetActiveUniformBlockiv(
        prog, block_index, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &active_uniforms);

    vectorImpl<GLuint> uniform_indices(active_uniforms, 0);
    glGetActiveUniformBlockiv(prog, block_index,
                              GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES,
                              reinterpret_cast<GLint*>(uniform_indices.data()));

    vectorImpl<GLint> name_lengths(uniform_indices.size(), 0);
    glGetActiveUniformsiv(prog, (GLsizei)uniform_indices.size(),
                          &uniform_indices[0], GL_UNIFORM_NAME_LENGTH,
                          &name_lengths[0]);

    vectorImpl<GLint> offsets(uniform_indices.size(), 0);
    glGetActiveUniformsiv(prog, (GLsizei)uniform_indices.size(),
                          &uniform_indices[0], GL_UNIFORM_OFFSET, &offsets[0]);

    vectorImpl<GLint> types(uniform_indices.size(), 0);
    glGetActiveUniformsiv(prog, (GLsizei)uniform_indices.size(),
                          &uniform_indices[0], GL_UNIFORM_TYPE, &types[0]);

    vectorImpl<GLint> sizes(uniform_indices.size(), 0);
    glGetActiveUniformsiv(prog, (GLsizei)uniform_indices.size(),
                          &uniform_indices[0], GL_UNIFORM_SIZE, &sizes[0]);

    vectorImpl<GLint> strides(uniform_indices.size(), 0);
    glGetActiveUniformsiv(prog, (GLsizei)uniform_indices.size(),
                          &uniform_indices[0], GL_UNIFORM_ARRAY_STRIDE,
                          &strides[0]);

    // Build a string detailing each uniform in the block:
    vectorImpl<stringImpl> uniform_details;
    uniform_details.reserve(uniform_indices.size());
    for (vectorAlg::vecSize i = 0; i < uniform_indices.size(); ++i) {
        GLuint const uniform_index = uniform_indices[i];

        stringImpl name(name_lengths[i], 0);
        glGetActiveUniformName(prog, uniform_index, name_lengths[i], NULL,
                               &name[0]);

        ostringstreamImpl details;
        details << std::setfill('0') << std::setw(4) << offsets[i] << ": "
                << std::setfill(' ') << std::setw(5) << types[i] << " "
                << name;

        if (sizes[i] > 1) {
            details << "[" << sizes[i] << "]";
        }

        details << "\n";

        uniform_details.push_back(details.str());
    }

    // Sort uniform detail string alphabetically. (Since the detail strings
    // start with the uniform's byte offset, this will order the uniforms in
    // the order they are laid out in memory:
    std::sort(std::begin(uniform_details), std::end(uniform_details));

    // Output details:
    Console::printfn("%s ( %d )", block_name.c_str(), block_size);
    for (vectorImpl<stringImpl>::iterator detail = std::begin(uniform_details);
         detail != std::end(uniform_details); ++detail) {
        Console::printfn("%s", (*detail).c_str());
    }
}

};  // namespace Divide

Commits for Divide-Framework/trunk/Source Code/Platform/Video/OpenGL/Buffers/ShaderBuffer/glUniformBuffer.cpp

Diff revisions: vs.
Revision Author Commited Message
727 Diff Diff IonutCava picture IonutCava Tue 14 Jun, 2016 16:01:38 +0000

[IonutCava]
- Remove node and cmd buffers from GFXDevice and add them as a new BufferData struct to RenderPass class.
— Each render pass holds its own buffers
- Improvements / fixes to the CSM code
- Added a global toggle to enable/disable custom memory allocators

720 Diff Diff IonutCava picture IonutCava Mon 06 Jun, 2016 15:51:06 +0000

[IonutCava]
- PVS-Studio guided static analysis fixes and optimizations
- Added a flag to SceneState to bypass the Save/Load system not needed for certain types of scenes (menus, cinematics, etc)

713 Diff Diff IonutCava picture IonutCava Wed 25 May, 2016 15:43:38 +0000

[IonutCava]
- Removed all unique_ptr’s from the code with classic new/delete pairs. They were seriously not needed.
- Added the concept of SceneComponent to scene specific classes: aiManager, lightPool, sceneGraph, physicsInterface etc
— This allowed the removal of the global GET_ACTIVE_SCENEGRAPH call;

712 Diff Diff IonutCava picture IonutCava Tue 24 May, 2016 16:18:37 +0000

[IonutCava]
- Added the concept of “buffer” to be used by GenericVertexData and ShaderBuffer without caring if it’s persistent or not.
— Persistent buffers handle their own locking instead of relying on the parent class
- HiZ occlusion culling fixes and optimizations

671 Diff Diff IonutCava picture IonutCava Fri 08 Apr, 2016 16:26:49 +0000

[Ionut]
- Custom memory allocation system part 1:
— Add http://www.codeproject.com/Articles/1089905/A-Custom-STL-std-allocator-Replacement-Improves-Pe
— Add custom allocators to strings and vectors
— Add custom allocators to a few test classes
— No support for aligned memory yet, so vec4<F32> and mat4<F32> vectors use the old allocation system
- Replace more std::async call with our custom task system

670 Diff Diff IonutCava picture IonutCava Wed 06 Apr, 2016 16:21:35 +0000

[IonutCava]
- getInstance() renamed to instance() for simplicity (should really go away altogether, but that’s a different issue)
- some particle emitter threading updates

640 Diff Diff IonutCava picture IonutCava Tue 09 Feb, 2016 17:18:17 +0000

[IonutCava]
- More Octree updates
- Fixed bounding box collision check
- Added initial intersection code
- Added on-demand flush call for fence locking system

631 Diff Diff IonutCava picture IonutCava Sun 24 Jan, 2016 20:28:21 +0000

[IonutCava]
- Fix and optimize particle rendering system
- Temporarily disable Z_PRE_PASS system for testing
- Improve thread pool destruction system a bit
- Fix texture loading when used as fb attachment
- Forward+ renderer now takes the entire light buffer (no need to split up by light type)
- Start work on occlusion culling fixes:
— ToDo: Take into account batching system
— Fix gl_DrawIDARB usage

629 Diff Diff IonutCava picture IonutCava Thu 21 Jan, 2016 17:08:04 +0000

[IonutCava]
- New environment mapping system:
— Use previous frame’s list of nodes sorted front to back
— Update reflection for each if they meet the necessary criteria
- Cleanup particle system classes
- Cleanup GenericVertexData class
- Improve argument forwarding to frustum culling tasks

591 IonutCava picture IonutCava Wed 02 Dec, 2015 17:17:23 +0000

[IonutCava]
- More work on the ForwardPlus renderer
- Singleton cleanup
- First steps for reducing scope of GFXDevice class