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
#include "stdafx.h"

#include "Headers/glUniformBuffer.h"

#include "Platform/Video/Headers/GFXDevice.h"
#include "Platform/Video/OpenGL/Headers/GLWrapper.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/glGenericBuffer.h"
#include "Platform/Video/OpenGL/Buffers/Headers/glBufferLockManager.h"

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

#include <iomanip>

namespace Divide {

namespace {
    typedef std::pair<GLuint, GLuint> AtomicBufferBindConfig;

    hashMapImpl<GLuint, AtomicBufferBindConfig> g_currentBindConfig;

    bool SetIfDifferentBindRange(GLuint bindIndex, GLuint buffer, GLuint bindOffset) {
        AtomicBufferBindConfig targetCfg = std::make_pair(buffer, bindOffset);
        // If this is a new index, this will just create a default config
        AtomicBufferBindConfig& crtConfig = g_currentBindConfig[bindIndex];
        if (targetCfg == crtConfig) {
            return false;
        }

        crtConfig = targetCfg;
        glBindBufferRange(GL_ATOMIC_COUNTER_BUFFER, bindIndex, buffer, bindOffset, sizeof(GLuint));
        return true;
    }
};

class AtomicCounter : public RingBuffer
{
public:
    AtomicCounter(GFXDevice& context, U32 sizeFactor, const char* name);
    ~AtomicCounter();
    glGenericBuffer* _buffer;
};

AtomicCounter::AtomicCounter(GFXDevice& context, U32 sizeFactor, const char* name)
    : RingBuffer(sizeFactor)
{
    BufferParams params;
    params._usage = GL_ATOMIC_COUNTER_BUFFER;
    params._elementCount = 1;
    params._elementSizeInBytes = sizeof(GLuint);
    params._frequency = BufferUpdateFrequency::ONCE;
    params._name = name;
    params._ringSizeFactor = sizeFactor;
    params._data = NULL;

    _buffer = MemoryManager_NEW glGenericBuffer(context, params);
}

AtomicCounter::~AtomicCounter()
{
    MemoryManager::DELETE(_buffer);
}

IMPLEMENT_CUSTOM_ALLOCATOR(glUniformBuffer, 0, 0)
glUniformBuffer::glUniformBuffer(GFXDevice& context,
                                 const ShaderBufferDescriptor& descriptor)
    : ShaderBuffer(context, descriptor)
{
    _maxSize = _unbound ? GL_API::s_SSBMaxSize : GL_API::s_UBMaxSize;

    _allignedBufferSize = realign_offset(_bufferSize, alignmentRequirement(_unbound));

    assert(_allignedBufferSize < _maxSize);

    BufferImplParams implParams;
    implParams._dataSizeInBytes = _allignedBufferSize * queueLength();
    implParams._frequency = _frequency;
    implParams._initialData = descriptor._initialData;
    implParams._target = _unbound ? GL_SHADER_STORAGE_BUFFER : GL_UNIFORM_BUFFER;

    _buffer = MemoryManager_NEW glBufferImpl(context, implParams);
}

glUniformBuffer::~glUniformBuffer() 
{
    MemoryManager::DELETE_VECTOR(_atomicCounters);
    MemoryManager::DELETE(_buffer);
}

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

void glUniformBuffer::readData(ptrdiff_t offsetElementCount,
                               ptrdiff_t rangeElementCount,
                               bufferPtr result) const {

    if (rangeElementCount > 0) {
        ptrdiff_t range = rangeElementCount * _primitiveSize;
        ptrdiff_t offset = offsetElementCount * _primitiveSize;

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

        offset += queueWriteIndex() * _allignedBufferSize;

        _buffer->readData(offset, range, result);
    }
}

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

    if (rangeElementCount == 0) {
        return;
    }

    ptrdiff_t range = rangeElementCount * _primitiveSize;
    if (rangeElementCount == _primitiveCount) {
        range = _allignedBufferSize;
    }

    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->writeData(offset, range, data);
}

bool glUniformBuffer::bindRange(U32 bindIndex, U32 offsetElementCount, U32 rangeElementCount) {
    GLuint range = static_cast<GLuint>(rangeElementCount * _primitiveSize);
    GLuint offset = static_cast<GLuint>(offsetElementCount * _primitiveSize);
    offset += static_cast<GLuint>(queueReadIndex() * _allignedBufferSize);


    assert(range <= _maxSize &&
           "glUniformBuffer::bindRange: attempted to bind a larger shader block than is allowed on the current platform");

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

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

void glUniformBuffer::addAtomicCounter(U32 sizeFactor) {
    stringImpl name = Util::StringFormat("DVD_ATOMIC_BUFFER_%d_%d", getGUID(), _atomicCounters.size());
    _atomicCounters.emplace_back(MemoryManager_NEW AtomicCounter(_context, std::max(sizeFactor, 1u), name.c_str()));
}

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

    AtomicCounter* counter = _atomicCounters.at(counterIndex);
    GLuint result = 0;
    counter->_buffer->readData(1, 0, counter->queueReadIndex(), &result);
    return result;
}

void glUniformBuffer::bindAtomicCounter(U32 counterIndex, U32 bindIndex) {
    if (counterIndex >= to_U32(_atomicCounters.size())) {
        return;
    }

    AtomicCounter* counter = _atomicCounters.at(counterIndex);

    SetIfDifferentBindRange(bindIndex,
                            counter->_buffer->bufferHandle(),
                            counter->queueWriteIndex() * sizeof(GLuint));
    counter->incQueue();
}

void glUniformBuffer::resetAtomicCounter(U32 counterIndex) {
    if (counterIndex > to_U32(_atomicCounters.size())) {
        return;
    }
    constexpr U32 defaultValue = 0;
    AtomicCounter* counter = _atomicCounters.at(counterIndex);
    counter->_buffer->writeData(1, 0, counter->queueWriteIndex(), (bufferPtr)(&defaultValue));
}

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());
    }
}

void glUniformBuffer::onGLInit() {
    ShaderBuffer::_boundAlignmentRequirement = GL_API::s_UBOffsetAlignment;
    ShaderBuffer::_unboundAlignmentRequirement = GL_API::s_SSBOffsetAlignment;
}

};  // namespace Divide

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

Diff revisions: vs.
Revision Author Commited Message
909 Diff Diff IonutCava picture IonutCava Thu 29 Jun, 2017 23:57:18 +0000

[Ionut]
- Rework RenderTarget class
— RTAttachment now deal with textures directly
— RT size needs to be specified upfront
- Application class is no longer a Singleton but passed around in the PlatformContext

902 Diff Diff IonutCava picture IonutCava Sun 25 Jun, 2017 16:56:57 +0000

[Ionut]
- Rework input system a bit for easier maintenance and readability
- Toggle AntTweakBar display using CTRL+F11

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

893 Diff Diff IonutCava picture IonutCava Sun 18 Jun, 2017 17:33:07 +0000

[Ionut]
- Initial implementation of a PipelineStateObject (holds shader program, rasterizer state, etc)
- Rework PCH implementation a bit because VS2017 no longer has a /ZM option

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

873 Diff Diff IonutCava picture IonutCava Thu 09 Mar, 2017 16:11:50 +0000

[IonutCava]
- Add support for swapping threadpool implementations
- Add 3 threadpool implementations: boost priority pool, boost fifo pool and CTPL (https://github.com/vit-vit/CTPL)

867 Diff Diff IonutCava picture IonutCava Mon 20 Feb, 2017 17:01:12 +0000

[IonutCava]
- Rename to_XNN and to_const_XNN functions to reflect the target data type they are converting to.
- Cleanup Angle namespace and remove all “inDegrees” parameters from rotation functions. Use aliases to differentiate between Degree and Radian parameters.
- Profile guided optimizations.

830 Diff Diff IonutCava picture IonutCava Wed 25 Jan, 2017 17:29:44 +0000

[IonutCava]
- Add a new Platform element: File
— Move all predefined paths to FileManagement System
- Split MathHelper into MathHelper and StringHelper

827 Diff Diff IonutCava picture IonutCava Sun 22 Jan, 2017 21:59:36 +0000

[IonutCava]
- ShaderBuffers now decide heuristically if they get persistently mapped or not (depends on size. Current limit is 512Kb for subData calls.
- Separate visible node command generation into 2 steps: onRender and getDrawPackage. onRender allows the creation of parallel tasks that should finish by the time the second call, getDrawPackage reaches the same node
- Update ParticleEmitter to take into account multiple players and increase parallelism at the cost of memory consumption and complexity.
- Add a default bounding box for Players (add extents to SceneTransform nodes)
- All buffers (ShaderBuffer, d3dConstantBuffer, glUniformBuffer, glBufferImpl, glGenericBuffer, etc) use BufferParams for creation.
— Constructor and create calls have been merged together

826 IonutCava picture IonutCava Fri 20 Jan, 2017 17:11:41 +0000

[IonutCava]
- Persistent buffer optimisations