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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
   Copyright (c) 2015 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 _GL_WRAPPER_H_
#define _GL_WRAPPER_H_

#include "glResources.h"
#include "Platform/Video/Headers/IMPrimitive.h"
#include "Platform/Video/OpenGL/Shaders/Headers/glShaderProgram.h"
#include "Platform/Video/OpenGL/Shaders/Headers/glShader.h"
#include "Platform/Video/OpenGL/Textures/Headers/glSamplerObject.h"
#include "Platform/Video/OpenGL/Textures/Headers/glTexture.h"
#include "Platform/Video/Buffers/Framebuffer/Headers/Framebuffer.h"

struct glslopt_ctx;
struct FONScontext;

namespace CEGUI {
    class OpenGL3Renderer;
};

namespace Divide {
    static const U32 MAX_ACTIVE_TEXTURE_SLOTS = 64;

    enum class WindowType : U32;

class glHardwareQuery : public HardwareQuery {
public:
    glHardwareQuery();
    ~glHardwareQuery();

    void create();
    void destroy();
    inline U32 getID() const { return _queryID; }

private:
    U32 _queryID;
};

struct ImageBindSettings {
    GLuint _texture;
    GLint  _level;
    GLboolean _layered;
    GLint _layer;
    GLenum _access;
    GLenum _format;

    bool operator==(const ImageBindSettings& other) const {
        return _texture == other._texture &&
               _level == other._level &&
               _layered == other._layered &&
               _layer == other._layer &&
               _access == other._access &&
               _format == other._format;
    }

    bool operator!=(const ImageBindSettings& other) const {
        return !(*this == other);
    }
};

/// OpenGL implementation of the RenderAPIWrapper
DEFINE_SINGLETON_EXT1_W_SPECIFIER(GL_API, RenderAPIWrapper, final)
    friend class glShader;
    friend class glTexture;
    friend class glIMPrimitive;
    friend class glFramebuffer;
    friend class glVertexArray;
    friend class glShaderProgram;
    friend class glSamplerObject;
    friend class glGenericVertexData;

  protected:
    GL_API();
    ~GL_API();

    /// Try and create a valid OpenGL context taking in account the specified
    /// command line arguments
    ErrorCode initRenderingAPI(I32 argc, char** argv) override;
    /// Clear everything that was setup in initRenderingAPI()
    void closeRenderingAPI() override;
    /// Prepare our shader loading system
    bool initShaders() override;
    /// Revert everything that was set up in "initShaders()"
    bool deInitShaders() override;
    /// Window positioning is handled by SDL
    void setWindowPosition(U16 w, U16 h);
    /// Centering is also easier via SDL
    void centerWindowPosition();
    /// Mouse positioning is handled by SDL
    void setCursorPosition(U16 x, U16 y) override;
    /// Prepare the GPU for rendering a frame
    void beginFrame() override;
    /// Finish rendering the current frame
    void endFrame() override;
    /// Create and return a new IM emulation primitive. The callee is responsible
    /// for it's deletion!
    IMPrimitive* newIMP() const override;
    /// Create and return a new framebuffer. The callee is responsible for it's
    /// deletion!
    Framebuffer* newFB(bool multisampled) const override;
    /// Create and return a new vertex array (VAO + VB + IB). The callee is
    /// responsible for it's deletion!
    VertexBuffer* newVB() const override;
    /// Create and return a new pixel buffer using the requested format. The callee
    /// is responsible for it's deletion!
    PixelBuffer* newPB(const PBType& type) const override;
    /// Create and return a new generic vertex data object and, optionally set it as
    /// persistently mapped.
    /// The callee is responsible for it's deletion!
    GenericVertexData* newGVD(const bool persistentMapped) const override;
    /// Create and return a new shader buffer. The callee is responsible for it's
    /// deletion!
    /// The OpenGL implementation creates either an 'Uniform Buffer Object' if
    /// unbound is false
    /// or a 'Shader Storage Block Object' otherwise
    ShaderBuffer* newSB(const stringImpl& bufferName, 
                        const U32 ringBufferLength = 1,
                        const bool unbound = false,
                        const bool persistentMapped = true,
                        BufferUpdateFrequency frequency =
                            BufferUpdateFrequency::ONCE) const override;
    /// Create and return a new texture array (optionally, flipped vertically). The
    /// callee is responsible for it's deletion!
    Texture* newTextureArray() const override;
    /// Create and return a new 2D texture (optionally, flipped vertically). The
    /// callee is responsible for it's deletion!
    Texture* newTexture2D() const override;
    /// Create and return a new cube texture (optionally, flipped vertically). The
    /// callee is responsible for it's deletion!
    Texture* newTextureCubemap() const override;
    /// Create and return a new shader program.
    /// The callee is responsible for it's deletion!
    ShaderProgram* newShaderProgram() const override;
    /// Create and return a new shader of the specified type by loading the
    /// specified name (optionally, post load optimised).
    /// The callee is responsible for it's deletion!
    Shader* newShader(const stringImpl& name, const ShaderType& type,
                      const bool optimise = false) const override;
    /// Return a new OpenGL hardware query
    HardwareQuery* newHardwareQuery() const override;
    /// Enable or disable rasterization (useful for transform feedback)
    inline void toggleRasterization(bool state) override {
        state ? glDisable(GL_RASTERIZER_DISCARD) : glEnable(GL_RASTERIZER_DISCARD);
    }
    /// Verify if we have a sampler object created and available for the given
    /// descriptor
    static U32 getOrCreateSamplerObject(const SamplerDescriptor& descriptor);
    /// Clipping planes are only enabled/disabled if they differ from the current
    /// state
    void updateClipPlanes() override;
    /// Text rendering is handled exclusively by Mikko Mononen's FontStash library
    /// (https://github.com/memononen/fontstash)
    /// with his OpenGL frontend adapted for core context profiles
    void drawText(const TextLabel& textLabel, const vec2<F32>& relativeOffset) override;
    /// Rendering points is universally useful, so we have a function, and a VAO,
    /// dedicated to this process
    void drawPoints(U32 numPoints) override;
    /// Renderinga single triangle is also universally useful, 
    /// so we have a function dedicated to this process
    void drawTriangle() override;
    /// This functions should be run in a separate, consumer thread.
    /// The main app thread, the producer, adds tasks via a lock-free queue that is
    /// checked every 20 ms
    void threadedLoadCallback() override;
    /// Return the time it took to render a single frame (in nanoseconds). Only
    /// works in debug builds
    GLuint64 getFrameDurationGPU() override;
    /// Return the OpenGL framebuffer handle bound and assigned for the specified
    /// usage
    inline static GLuint getActiveFB(Framebuffer::FramebufferUsage usage) {
        return _activeFBID[to_uint(usage)];
    }
    /// Try to find the requested font in the font cache. Load on cache miss.
    I32 getFont(const stringImpl& fontName);
    /// Internally change window size
    void changeWindowSize(U16 w, U16 h);
    /// Change rendering resolution
    void changeResolution(U16 w, U16 h) override;
    /// Change the current viewport area. Redundancy check is performed in GFXDevice
    /// class
    void changeViewport(const vec4<GLint>& newViewport) const override;
    /// Reset as much of the GL default state as possible within the limitations
    /// given
    void clearStates();
    void registerCommandBuffer(const ShaderBuffer& commandBuffer) const override;

    bool makeTexturesResident(const TextureDataContainer& textureData) override;
    bool makeTextureResident(const TextureData& textureData) override;

  public:
    /// Enable or disable primitive restart and ensure that the correct index size
    /// is used
    static void togglePrimitiveRestart(bool state);
    /// Set the currently active texture unit
    static bool setActiveTextureUnit(GLushort unit);
    /// Set the currently active texture unit
    static bool setActiveTextureUnit(GLushort unit, GLuint& previousUnit);
    /// Switch the currently active vertex array object
    static bool setActiveVAO(GLuint ID);
    /// Switch the currently active vertex array object
    static bool setActiveVAO(GLuint ID, GLuint& previousID);
    /// Single place to change buffer objects for every target available
    static bool setActiveBuffer(GLenum target, GLuint ID);
    /// Single place to change buffer objects for every target available
    static bool setActiveBuffer(GLenum target, GLuint ID, GLuint& previousID);
    /// Switch the current framebuffer by binding it as either a R/W buffer, read
    /// buffer or write buffer
    static bool setActiveFB(Framebuffer::FramebufferUsage usage, GLuint ID);
    /// Switch the current framebuffer by binding it as either a R/W buffer, read
    /// buffer or write buffer
    static bool setActiveFB(Framebuffer::FramebufferUsage usage, GLuint ID, GLuint& previousID);
    /// Bind the specified transform feedback object
    static bool setActiveTransformFeedback(GLuint ID);
    /// Bind the specified transform feedback object
    static bool setActiveTransformFeedback(GLuint ID, GLuint& previousID);
    /// Change the currently active shader program.
    static bool setActiveProgram(GLuint programHandle);
    /// A state block should contain all rendering state changes needed for the next
    /// draw call.
    /// Some may be redundant, so we check each one individually
    void activateStateBlock(const RenderStateBlock& newBlock,
                            const RenderStateBlock& oldBlock) const override;
    /// Pixel pack and unpack alignment is usually changed by textures, PBOs, etc
    static bool setPixelPackUnpackAlignment(GLint packAlignment = 1,
                                            GLint unpackAlignment = 1) {
        return (setPixelPackAlignment(packAlignment) &&
                setPixelUnpackAlignment(unpackAlignment));
    }
    /// Pixel pack alignment is usually changed by textures, PBOs, etc
    static bool setPixelPackAlignment(GLint packAlignment = 1, GLint rowLength = 0,
                                      GLint skipRows = 0, GLint skipPixels = 0);
    /// Pixel unpack alignment is usually changed by textures, PBOs, etc
    static bool setPixelUnpackAlignment(GLint unpackAlignment = 1,
                                        GLint rowLength = 0, GLint skipRows = 0,
                                        GLint skipPixels = 0);
    /// Bind a texture specified by a GL handle and GL type to the specified
    /// unit
    /// using the sampler object defined by hash value
    static bool bindTexture(GLushort unit, GLuint handle, GLenum target,
                            U32 samplerHash = 0);
    static bool bindTextureImage(GLushort unit, GLuint handle, GLint level,
                                 bool layered, GLint layer, GLenum access,
                                 GLenum format);
    /// Bind multiple textures specified by an array of handles and an offset
    /// unit
    static bool bindTextures(GLushort unitOffset, GLuint textureCount,
                             GLuint* textureHandles, GLenum* targets,
                             GLuint* samplerHandles);

    /// Bind the sampler object described by the hash value to the specified
    /// unit
    static bool bindSampler(GLushort unit, U32 samplerHash);
    /// Bind multiple samplers described by the array of hash values to the
    /// consecutive texture units starting from the specified offset
    static bool bindSamplers(GLushort unitOffset, GLuint samplerCount,
                             GLuint* samplerHandles);
    /// Return the OpenGL sampler object's handle for the given hash value
    static GLuint getSamplerHandle(U32 samplerHash);
    /// Modify buffer bindings for a specific vao
    static bool bindActiveBuffer(GLuint vaoID,
                                 GLuint location,
                                 GLuint bufferID,
                                 GLintptr offset,
                                 GLsizei stride);

  private:
    ErrorCode createWindow();
    ErrorCode createGLContext();
    ErrorCode destroyWindow();
    ErrorCode destroyGLContext();
    void handleChangeWindowType(WindowType newWindowType);
    void pollWindowEvents();
    /// FontStash library initialization
    bool createFonsContext();
    /// FontStash library deinitialization
    void deleteFonsContext();
    /// Use GLSW to append tokens to shaders. Use ShaderType::COUNT to append to
    /// all stages
    typedef std::array<GLint, to_const_uint(ShaderType::COUNT) + 1> ShaderOffsetArray;
    void appendToShaderHeader(ShaderType type, const stringImpl& entry,
                              ShaderOffsetArray& inOutOffset);

  private:
    /// The current rendering window type
    WindowType _crtWindowType;
    /// The previous Text3D node's font face size
    GLfloat _prevSizeNode;
    /// The previous plain text string's font face size
    GLfloat _prevSizeString;
    /// The previous Text3D node's line width
    GLfloat _prevWidthNode;
    /// The previous plain text string's line width
    GLfloat _prevWidthString;
    /// Line width limit (hardware upper limit)
    GLint _lineWidthLimit;
    /// Used to render points (e.g. to render full screen quads with geometry
    /// shaders)
    GLuint _dummyVAO;
    /// Number of available texture units
    static GLint _maxTextureUnits;
    /// Used to store all of the indirect draw commands
    static GLuint _indirectDrawBuffer;
    /// A cache of all fonts used
    typedef hashMapImpl<stringImpl, I32> FontCache;
    FontCache _fonts;
    hashAlg::pair<stringImpl, I32> _fontCache;
    /// Current active vertex array object's handle
    static GLuint _activeVAOID;
    /// 0 - current framebuffer, 1 - current read only framebuffer, 2 - current
    /// write only framebuffer
    static GLuint _activeFBID[3];
    /// VB, IB, SB, TB, UB, PUB, DIB
    static GLuint _activeBufferID[7];
    static GLuint _activeTextureUnit;
    static GLuint _activeTransformFeedback;
    static GLint _activePackUnpackAlignments[2];
    static GLint _activePackUnpackRowLength[2];
    static GLint _activePackUnpackSkipPixels[2];
    static GLint _activePackUnpackSkipRows[2];
    static GLuint _activeShaderProgram;
    /// Boolean value used to verify if primitive restart index is enabled or
    /// disabled
    static bool _primitiveRestartEnabled;
    /// Toggle CEGUI rendering on/off (e.g. to check raw application rendering
    /// performance)
    bool _enableCEGUIRendering;
    /// Did we generate the window move event?
    bool _internalMoveEvent;
    /// Did we resize the window via an OS call?
    bool _externalResizeEvent;
    /// Current state of all available clipping planes
    std::array<bool, Config::MAX_CLIP_PLANES> _activeClipPlanes;
    /// Performance counters: front x 2 and back x 2
    static const GLint PERFORMANCE_COUNTER_BUFFERS = 4;
    /// Number of queries
    static const GLint PERFORMANCE_COUNTERS = 1;
    /// Unique handle for every query object
    glHardwareQuery _queryID[PERFORMANCE_COUNTER_BUFFERS][PERFORMANCE_COUNTERS];
    /// Current query ID used for writing to
    GLuint _queryBackBuffer;
    /// Current query ID used for reading from
    GLuint _queryFrontBuffer;
    /// Duration in nanoseconds to render a frame
    GLuint64 FRAME_DURATION_GPU;
    /// FontStash's context
    FONScontext* _fonsContext;
    /// /*texture slot*/ /*<texture handle , texture type>*/
    typedef std::array<std::pair<GLuint, GLenum>, MAX_ACTIVE_TEXTURE_SLOTS> textureBoundMapDef;
    static textureBoundMapDef _textureBoundMap;

    typedef std::array<ImageBindSettings, MAX_ACTIVE_TEXTURE_SLOTS> imageBoundMapDef;
    static imageBoundMapDef _imageBoundMap;

    /// /*texture slot*/ /*sampler hash value*/
    typedef std::array<U32, MAX_ACTIVE_TEXTURE_SLOTS> samplerBoundMapDef;
    static samplerBoundMapDef _samplerBoundMap;
    /// /*sampler hash value*/ /*sampler object*/
    typedef hashMapImpl<U32, glSamplerObject*> samplerObjectMap;
    static samplerObjectMap _samplerMap;

    typedef std::tuple<GLuint, GLuint, GLintptr, GLsizei> BufferBindingParams;
    typedef hashMapImpl<GLuint, BufferBindingParams> VAOBufferData;
    static VAOBufferData _vaoBufferData;

    CEGUI::OpenGL3Renderer* _GUIGLrenderer;

END_SINGLETON

};  // namespace Divide
#endif

Commits for Divide-Framework/trunk/Source Code/Platform/Video/OpenGL/Headers/GLWrapper.h

Diff revisions: vs.
Revision Author Commited Message
557 Diff Diff IonutCava picture IonutCava Tue 10 Nov, 2015 12:00:20 +0000

[IonutCava]
- Gather all rendering calls from glVertexArray and glGenericVertexData into a single function in glResources: submitRenderCommand.
— This allows both classes to interpret GenericDrawCommands in the same way
- Use unsigned ints for all hasehs instead of size_t to avoid negative number hashes (useful for debugging)

556 Diff Diff IonutCava picture IonutCava Mon 09 Nov, 2015 23:10:09 +0000

[IonutCava]
- Fixed faulty VB partitioning code
- Cleanup in state management system:
— Remove some useless clearing of buffers (e.g. in glimBatch code)
— Changing a VAO will invalidate the element buffer cache
-— ToDo: Investigate all states that need to invalidate cache on VAO change
- Don’t add RenderingComponent to the SceneRoot node

Known bug:
- If we are rendering only the skybox, it is not rendered properly until a different node comes into view. Needs investigation. Not state/shader or texture issue!

553 Diff Diff IonutCava picture IonutCava Sun 08 Nov, 2015 21:48:10 +0000

[IonutCava]
- Improve VBO allocation system
- Disable mipmap generation for blur FBO in CascadedShadowMaps
- Code cleanup

552 Diff Diff IonutCava picture IonutCava Fri 06 Nov, 2015 17:07:22 +0000

[Ionut]
- Use UNIT0 and UNIT1 during depth pass for transparency texture to reduce texture binds between calls
- Allow different VAOs with different attributes per render stage per glVertexArray instance
- Initial work on multiple glVertexArray instances per single VBO (attempt to get VBOs to the 4MB mark) - Currently crashes the build
- Fix a bug on exit if background tasks depended on scene elements. Wait for the main threadpool to clear before destroying the scene.
- Use a different node buffer per rendering stage to avoid waiting too much on fence syncs

545 Diff Diff IonutCava picture IonutCava Tue 03 Nov, 2015 17:15:43 +0000

[IonutCava]
- Reimplement single pass layered shadow rendering
- Cleanup frambuffer class and use clearBuffer instead of clearColor.
- Clean ShaderProgram uniform calls a bit

543 Diff Diff IonutCava picture IonutCava Fri 30 Oct, 2015 17:18:18 +0000

[IonutCava]
- More profile guided optimizations
- Attempt to render all CSM splits in a single pass using geometry shader based instancing (unfinished)

542 Diff Diff IonutCava picture IonutCava Thu 29 Oct, 2015 23:44:59 +0000

[IonutCava]
- Improved multi-threaded culling
- Increased nodeBuffer size to avoid different render stages to override each-other’s data and wait for locks
- Added a indirect command buffer per render stage

527 Diff Diff IonutCava picture IonutCava Fri 16 Oct, 2015 16:01:06 +0000

[IonutCava]
- EXPERIMENTAL Added Compute based HiZ occlusion culling
- Some fixes to shader line counting system

524 Diff Diff IonutCava picture IonutCava Thu 08 Oct, 2015 16:14:58 +0000

[IonutCava]
- Profiler guided optimizations:
— Replace most hashMaps with vectors where the common access pattern is linear (e.g. SceneGraphNode’s child list)
— Specify ShaderBuffer access patters at creation time to properly inform the rendering API
— Fix and optimise the LockManager & BufferLockManager classes
— Add a AtomicWrapper class that allows assignment and copy-constructions for atomic variables
-— This allows us to use vector of atomic bools for BB creation for SkinnedSubMesh objects

516 IonutCava picture IonutCava Tue 29 Sep, 2015 18:21:34 +0000

[IonutCava]
- Fixed VBO partitioning system
- Replaced some DELEGATE_BIND calls with lambda expressions
- Some multithreaded rendering work (still not functional)