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

#include "Hardware/Video/OpenGL/Headers/glResources.h"
#include "Hardware/Video/OpenGL/Buffers/Headers/glBufferLockManager.h"
#include "Hardware/Video/Headers/GFXDevice.h"
#include "core.h"

#include <iomanip> 

namespace Divide {

glUniformBuffer::glUniformBuffer(bool unbound, bool persistentMapped) : ShaderBuffer(unbound, persistentMapped), 
                                                                        _mappedBuffer(nullptr),
                                                                        _lockManager(nullptr),
                                                                        _UBOid(0)
{
    if (Config::Profile::DISABLE_PERSISTENT_BUFFER) {
        persistentMapped = _persistentMapped = false;
    }

    if (persistentMapped) {
        _lockManager = MemoryManager_NEW glBufferLockManager(true);
    }
}

glUniformBuffer::~glUniformBuffer()
{
    if (_UBOid > 0) {
        if (_persistentMapped) {
            GL_API::setActiveBuffer(_target, _UBOid);
            glUnmapBuffer(_target);
        }
        glDeleteBuffers(1, &_UBOid);
        _UBOid = 0;
    }

    MemoryManager::DELETE( _lockManager );
}

void glUniformBuffer::Create(U32 primitiveCount, ptrdiff_t primitiveSize) {
    DIVIDE_ASSERT(_UBOid == 0, "glUniformBuffer error: Tried to double create current UBO");
    ShaderBuffer::Create(primitiveCount, primitiveSize);

    glGenBuffers(1, &_UBOid); // Generate the buffer
    DIVIDE_ASSERT(_UBOid != 0, "glUniformBuffer error: UBO creation failed");
    
    _target = _unbound ? GL_SHADER_STORAGE_BUFFER : GL_UNIFORM_BUFFER;

    if (_persistentMapped) {
        GLenum usageFlag = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
        glNamedBufferStorageEXT(_UBOid, _bufferSize, NULL, usageFlag | GL_DYNAMIC_STORAGE_BIT);
        _mappedBuffer = glMapNamedBufferRangeEXT(_UBOid, 0, _bufferSize, usageFlag);
    }else{
        glNamedBufferDataEXT(_UBOid, _bufferSize, NULL, GL_DYNAMIC_DRAW);
    }
}

void glUniformBuffer::DiscardAllData() {
    glInvalidateBufferData(_UBOid);
}

void glUniformBuffer::DiscardSubData(ptrdiff_t offset, ptrdiff_t size) {
    DIVIDE_ASSERT(offset + size <= (GLsizeiptr)_bufferSize, 
                  "glUniformBuffer error: DiscardSubData was called with an invalid range (buffer overflow)!");
    glInvalidateBufferSubData(_UBOid, offset, size);
}

void glUniformBuffer::UpdateData(GLintptr offset, GLsizeiptr size, const void *data, const bool invalidateBuffer) const {
    DIVIDE_ASSERT(offset + size <= (GLsizeiptr)_bufferSize, 
                  "glUniformBuffer error: ChangeSubData was called with an invalid range (buffer overflow)!");

    if (invalidateBuffer) {
        if (_persistentMapped) {
            //glNamedBufferSubDataEXT(_UBOid, 0, _bufferSize, NULL);
        } else {
            glNamedBufferDataEXT(_UBOid, _bufferSize, NULL, GL_DYNAMIC_DRAW);
        }
    }

    if (size == 0 || !data) {
        return;
    }

    if (_persistentMapped) {
        GL_API::setActiveBuffer(_target, _UBOid);
        _lockManager->WaitForLockedRange(offset, size);
            void* dst = (U8*) _mappedBuffer + offset;
            memcpy(dst, data, size);
        _lockManager->LockRange(offset, size);
    } else {
        glNamedBufferSubDataEXT(_UBOid, offset, size, data);
    }
}

bool glUniformBuffer::BindRange(ShaderBufferLocation bindIndex, U32 offsetElementCount, U32 rangeElementCount) const {
    DIVIDE_ASSERT(_UBOid != 0, "glUniformBuffer error: Tried to bind an uninitialized UBO");

    glBindBufferRange(_target, bindIndex, _UBOid, _primitiveSize * offsetElementCount, _primitiveSize * rangeElementCount);

    return true;
}

bool glUniformBuffer::Bind(ShaderBufferLocation bindIndex) const {
    DIVIDE_ASSERT(_UBOid != 0, "glUniformBuffer error: Tried to bind an uninitialized UBO");

    glBindBufferBase(_target, bindIndex, _UBOid);

    return true;
}

void glUniformBuffer::PrintInfo(const ShaderProgram* shaderProgram, ShaderBufferLocation bindIndex) {
    GLuint prog = shaderProgram->getId();
    GLuint block_index = bindIndex;

    if (prog <= 0 || block_index < 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]);

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

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

        details << "\n";
        uniform_details.push_back(stringAlg::toBase(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(uniform_details.begin(), uniform_details.end());

    // Output details:
    PRINT_FN("%s ( %d )", block_name.c_str(), block_size);
    for (auto detail = uniform_details.begin(); detail != uniform_details.end(); ++detail) {
        PRINT_FN("%s", (*detail).c_str());
    }
}

}; //namespace Divide

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

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)

330 Diff Diff IonutCava picture IonutCava Fri 10 Oct, 2014 17:19:11 +0000

[Ionut]
- New rendering algorithm :
— Perform second pass for visible nodes gathering all render commands and uploading them to an indirect draw buffer
— Render geometry with indirect draw commands
— Removed per-node range binding of the node buffer. Instead, bind the entire buffer once and index data in shaders
— Use “baseInstance” parameter from IndirectDrawCommand as a node buffer index
— Prefer DSA extensions for updating buffer data where applicable
- Moved all rendering specific code from SceneGraphNode to a new RenderingComponent
- Optimized “toString” utility function

325 Diff Diff IonutCava picture IonutCava Wed 17 Sep, 2014 20:06:13 +0000

[Ionut]
- Reworked transform system:
— Parent transform chain system moved from Transform class to the PhysicsComponent
— PhysicsComponent now returns all transform values needed both global and local (still needs heavy optimization and caching)

- Reworked SceneGraph node management:
— Proper ref counting of SceneNodes and proper removing of resource (ResourceCache is now empty on destruction, as it should be)
— Removed parent transform tracking as that’s the PhysicsComponent’s responsibility
— Only nodes loaded via the ResourceCache are allowed to be added to the SceneGraph (added proper loaders for Impostors, Sky, etc)

- Optimized some of the math classes (mat4, mat3)

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)

296 Diff Diff k1ngp1n picture k1ngp1n Sun 03 Aug, 2014 14:54:21 +0000

[Ionut]
- nVidia compatibility fixes (GUI, shadows, post-processing)
- Texture bind slots are now part of ShaderProgram class
- Renamed ForwardRenderer class to ForwardPlusRenderer and moved ligh grid creation to it
- More code cleanups and comments

293 Diff Diff k1ngp1n picture k1ngp1n Tue 29 Jul, 2014 20:01:28 +0000

[Ionut]
- Code cleanup and comments in GLFWWrapper (for starters. more to follow)
- Reworked main loop logic (no more delegates and rendering api querying)
- Replaced all GLvoid with regular void
- replaced all boost::this_thread::sleep (deprecated calls) with boost::this_thread::sleep_for

292 Diff Diff k1ngp1n picture k1ngp1n Mon 28 Jul, 2014 20:23:56 +0000

[Ionut]
- More nVidia compatibility issues (including the ability to disable persistent mapped buffer altogether. Thanks nsight ....)
- Cross-platform compatibility fixes for types (custom types now mirror types in stdint.h and are now typdefed instead of defined)
- Removed redundant files

290 Diff Diff IonutCava picture IonutCava Sun 27 Jul, 2014 14:54:01 +0000

[Ionut]
- Small change to the resource loading thread
- Fixed array texture loading in framebuffer
- Temporarily disabled shadows (caused an assert)
- Fixed double close call for LightManager
- Removed unused options from config.xml

289 IonutCava picture IonutCava Fri 25 Jul, 2014 17:46:28 +0000

[Ionut]
- Added transform interpolation support to avoid object stuttering when frame rate varies (see Transform.cpp)
- Updated the quaternion class to be a wrapper around a regular vec4 to take advantage of it’s built in functions (length, dot, cross, etc)