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
/*
   Copyright (c) 2016 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 "config.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/RenderTarget/Headers/RenderTarget.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 DisplayWindow;

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

    void create();
    void destroy();
    inline U32 getID() const { return _queryID; }
    inline bool enabled() const { return _enabled; }
    inline void enabled(bool state) { _enabled = state; }
protected:
    bool _enabled;
    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_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;
    /// Prepare the GPU for rendering a frame
    void beginFrame() override;
    /// Finish rendering the current frame
    void endFrame(bool swapBuffers) override;
    /// Create and return a new IM emulation primitive. The callee is responsible
    /// for it's deletion!
    IMPrimitive* newIMP(GFXDevice& context) const override;
    /// Create and return a new framebuffer. The callee is responsible for it's
    /// deletion!
    RenderTarget* newRT(GFXDevice& context, bool multisampled) const override;
    /// Create and return a new vertex array (VAO + VB + IB). The callee is
    /// responsible for it's deletion!
    VertexBuffer* newVB(GFXDevice& context) const override;
    /// Create and return a new pixel buffer using the requested format. The callee
    /// is responsible for it's deletion!
    PixelBuffer* newPB(GFXDevice& context, const PBType& type) const override;
    /// Create and return a new generic vertex data object
    /// The callee is responsible for it's deletion!
    GenericVertexData* newGVD(GFXDevice& context, const U32 ringBufferLength = 1) 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(GFXDevice& context,
                        const U32 ringBufferLength = 1,
                        const bool unbound = false,
                        const bool persistentMapped = true,
                        BufferUpdateFrequency frequency =
                            BufferUpdateFrequency::ONCE) const override;
    /// Create and return a new texture. The callee is responsible for it's deletion!
    Texture* newTexture(GFXDevice& context, const stringImpl& name, const stringImpl& resourceLocation, TextureType type, bool asyncLoad) const override;
    /// Create and return a new shader program.
    /// The callee is responsible for it's deletion!
    ShaderProgram* newShaderProgram(GFXDevice& context, const stringImpl& name, const stringImpl& resourceLocation, bool asyncLoad) const override;

    inline void toggleDepthWrites(bool state) override {
        glDepthMask(state ? GL_TRUE : GL_FALSE);
    }

    /// 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 size_t 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>& position) override;
    void draw(const GenericDrawCommand& cmd) 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 syncToThread(std::thread::id threadID) 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(RenderTarget::RenderTargetUsage 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);
    /// 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(RenderTarget::RenderTargetUsage usage, GLuint ID);
    /// Switch the current framebuffer by binding it as either a R/W buffer, read
    /// buffer or write buffer
    static bool setActiveFB(RenderTarget::RenderTargetUsage 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, size_t 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, size_t 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(size_t samplerHash);
    /// Modify buffer bindings for a specific vao
    static bool bindActiveBuffer(GLuint vaoID,
                                 GLuint location,
                                 GLuint bufferID,
                                 GLintptr offset,
                                 GLsizei stride);

  private:
    ErrorCode createGLContext(const DisplayWindow& window);
    ErrorCode destroyGLContext();
    /// 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 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<ULL, 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;
    /// 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<size_t, MAX_ACTIVE_TEXTURE_SLOTS> samplerBoundMapDef;
    static samplerBoundMapDef _samplerBoundMap;
    /// /*sampler hash value*/ /*sampler object*/
    typedef hashMapImpl<size_t, glSamplerObject*> samplerObjectMap;
    static SharedLock _samplerMapLock;
    static samplerObjectMap _samplerMap;

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

    CEGUI::OpenGL3Renderer* _GUIGLrenderer;


    Time::ProfileTimer& _swapBufferTimer;
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
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

725 Diff Diff IonutCava picture IonutCava Fri 10 Jun, 2016 12:44:46 +0000

[IonutCava]
- Removed GFX drawPoints and drawTriangle calls and replaced them with a single draw command that takes a GenericDrawCommand parameter.
- Updated CSM code. Shadows now render in the correct position again. Still has issues.

718 Diff Diff IonutCava picture IonutCava Thu 02 Jun, 2016 16:02:48 +0000

[IonutCava]
- Code cleanup:
— Rename Framebuffer to RenderTarget. Only OpenGL uses the Framebuffer nomenclature.
— Remove base Shader class as only OpenGL will have separate shaders (and eventually move to pipeline objects) as Direct3D uses FX files and may end up with different structure
— Remove drawBox3D, drawSphere3D and drawLines from GFXDevice class and add them as member functions to the IMPrimitive class (renamed to fromXYZ)
— Pull some elements from SceneManager and move them to standalone classes (W.I.P.)

715 Diff Diff IonutCava picture IonutCava Fri 27 May, 2016 16:19:22 +0000

[IonutCava]
- Added initial Fade In / Fade Out post effect (used for scene transitions for now)
- Added a GUI Interface that handles a list of GUI elements (used by GUI and SceneGUIElements)
- Fixed some race conditions related to multi-threaded scene loading (a lot remain to be fixed)
- Improved per-thread GPU sync system a bit
- Simplified Singleton define macros (although we should get rid of singletons all together)

714 Diff Diff IonutCava picture IonutCava Thu 26 May, 2016 16:26:23 +0000

[IonutCava]
- Initial code for multithreaded scene load:
— Tasks can have a “sync with gpu” flag that will cause them to use a shared context (so they can call GL functions)
- Added per scene GUI lists that get passed to the main GUI class (still need a base class to hold the map and accessors to avoid code duplication)
- Re-enabled threading unit tests

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

703 Diff Diff IonutCava picture IonutCava Wed 11 May, 2016 15:44:57 +0000

[IonutCava]
- Finish XML-based control bindings:
— Allow per bind input params (key id, mouse button index, joystick control data, etc)
— Allow mouse binding (with up to 7 buttons)
— Allow up to 4 joysticks (for now) with 120+ button, POV, Axis, Slider and Vector binding support for each
- Change all hashes in code from U32 to size_t for compatibility with STL/Boost

692 Diff Diff IonutCava picture IonutCava Wed 27 Apr, 2016 16:24:26 +0000

[IonutCava]
- Resource system cleanup:
— Resource name and resource file location (if any) is now passed only via explicit constructors! (ResourceDescriptors already contain this data, so why use setters?)
- Fix a bug with ortho projection matrix calculation (set m[3][3] to 1. this was skipped when the identity() call was changed to zero() in the ortho() function)
— This also fixed text rendering

690 Diff Diff IonutCava picture IonutCava Sat 23 Apr, 2016 21:16:35 +0000

[IonutCava]
- Fixed a bug with continuous loading/unloading of reflection targets
- Added more profile timers

657 IonutCava picture IonutCava Sun 06 Mar, 2016 18:54:25 +0000

[Ionut]
- Small corrections to the resolution change system and its relation to the GUI rendering system