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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
   Copyright (c) 2017 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 "glHardwareQueryPool.h"
#include "Rendering/Camera/Headers/Frustum.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"
#include "Platform/Video/OpenGL/Buffers/PixelBuffer/Headers/glPixelBuffer.h"
#include "Platform/Video/OpenGL/Buffers/VertexBuffer/Headers/glVAOPool.h"

#include <unordered_set>

struct glslopt_ctx;
struct FONScontext;
struct ImDrawData;

namespace CEGUI {
    class OpenGL3Renderer;
};

namespace Divide {

namespace Time {
    class ProfileTimer;
};

static const U32 MAX_ACTIVE_TEXTURE_SLOTS = 64;

enum class WindowType : U32;

class DisplayWindow;
class RenderStateBlock;
class glHardwareQueryRing;
class GenericVertexData;

namespace GLUtil {
    class glVAOCache;
};

struct GLConfig {
};

/// OpenGL implementation of the RenderAPIWrapper
class GL_API final : public RenderAPIWrapper {
    friend class glShader;
    friend class glTexture;
    friend class GLUtil::glVAOCache;
    friend class glIMPrimitive;
    friend class glFramebuffer;
    friend class glVertexArray;
    friend class glShaderProgram;
    friend class glSamplerObject;
    friend class glGenericVertexData;

public:
    GL_API(GFXDevice& context);
    ~GL_API();

protected:
    /// Try and create a valid OpenGL context taking in account the specified
    /// command line arguments
    ErrorCode initRenderingAPI(I32 argc, char** argv, Configuration& config) override;
    /// Clear everything that was setup in initRenderingAPI()
    void closeRenderingAPI() override;
    /// Prepare the GPU for rendering a frame
    void beginFrame() override;
    /// Finish rendering the current frame
    void endFrame() override;

    /// 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 TextElementBatch& batch);
    void drawIMGUI(ImDrawData* data);

    bool draw(const GenericDrawCommand& cmd);

    /// Sets the current state block to the one passed as a param
    size_t setStateBlock(size_t stateBlockHash) override;

    void flushCommandBuffer(GFX::CommandBuffer& commandBuffer) override;

    /// Return the time it took to render a single frame (in nanoseconds). Only
    /// works in GPU validation builds
    U32 getFrameDurationGPU() override;

    /// Return the size in pixels that we can render to. This differs from the window size on Retina displays
    vec2<U16> getDrawableSize(const DisplayWindow& window) const override;

    U32 getHandleFromCEGUITexture(const CEGUI::Texture& textureIn) const override;

    /// Return the OpenGL framebuffer handle bound and assigned for the specified usage
    inline static GLuint getActiveFB(RenderTarget::RenderTargetUsage usage) {
        return s_activeFBID[to_U32(usage)];
    }
    /// Try to find the requested font in the font cache. Load on cache miss.
    I32 getFont(const stringImpl& fontName);

    /// 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);
    bool makeTextureResident(const TextureData& textureData, U8 binding);

    bool changeViewportInternal(const Rect<I32>& viewport) override;

public:
    /// Makes sure that the calling thread has a valid GL context. If not, a new one is created.
    static void createOrValidateContextForCurrentThread(GFXDevice& context);

    /// Queue a mipmap recalculation
    static void queueComputeMipMap(GLuint textureHandle);
    /// Enable or disable primitive restart and ensure that the correct index size is used
    static void togglePrimitiveRestart(bool state);
    /// Enable or disable primitive rasterization
    static void toggleRasterization(bool state);
    /// Set the number of vertices per patch used in tessellation
    static void setPatchVertexCount(U32 count);
    /// 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);
    /// Set a new depth range. Default is 0 - 1 with 0 mapping to the near plane and 1 to the far plane
    static void setDepthRange(F32 nearVal, F32 farVal);
    static void setBlending(bool enable, const BlendingProperties& blendingProperties, bool force = false);
    inline static void setBlending(bool enable, bool force = false) {
        setBlending(enable, s_blendPropertiesGlobal, force);
    }
    /// Set the blending properties for the specified draw buffer
    static void setBlending(GLuint drawBufferIdx, bool enable, const BlendingProperties& blendingProperties, bool force = false);

    inline static void setBlending(GLuint drawBufferIdx, bool enable, bool force = false) {
        setBlending(drawBufferIdx, enable, s_blendProperties[drawBufferIdx], force);
    }

    static void setBlendColour(const UColour& blendColour, bool force = false);
    /// 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
    static void activateStateBlock(const RenderStateBlock& newBlock,
                                   const RenderStateBlock& oldBlock);
    static void activateStateBlock(const RenderStateBlock& newBlock);
    /// Pixel pack and unpack alignment is usually changed by textures, PBOs, etc
    static bool setPixelPackUnpackAlignment(GLint packAlignment = 4,
                                            GLint unpackAlignment = 4) {
        return (setPixelPackAlignment(packAlignment) &&
                setPixelUnpackAlignment(unpackAlignment));
    }
    /// Pixel pack alignment is usually changed by textures, PBOs, etc
    static bool setPixelPackAlignment(GLint packAlignment = 4, GLint rowLength = 0,
                                      GLint skipRows = 0, GLint skipPixels = 0);
    /// Pixel unpack alignment is usually changed by textures, PBOs, etc
    static bool setPixelUnpackAlignment(GLint unpackAlignment = 4, 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, 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, GLuint* samplerHandles);

    static bool deleteTextures(GLuint count, GLuint* textures);

    static bool deleteSamplers(GLuint count, GLuint* samplers);

    static bool deleteBuffers(GLuint count, GLuint* buffers);

    static bool deleteVAOs(GLuint count, GLuint* vaos);

    static bool deleteFramebuffers(GLuint count, GLuint* framebuffers);

    static bool deleteShaderPrograms(GLuint count, GLuint* programs);

    static size_t setStateBlockInternal(size_t stateBlockHash);

    /// 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);

    static void pushDebugMessage(const char* message, I32 id);
    static void popDebugMessage();

    static bool setScissor(I32 x, I32 y, I32 width, I32 height);
    inline static bool setScissor(const Rect<I32>& newScissorRect) {
        setScissor(newScissorRect.x, newScissorRect.y, newScissorRect.z, newScissorRect.w);
    }

    static bool setClearColour(const FColour& colour);
    inline static bool setClearColour(const UColour& colour) {
        return setClearColour(Util::ToFloatColour(colour));
    }

    /// Change the current viewport area. Redundancy check is performed in GFXDevice class
    static bool changeViewport(I32 x, I32 y, I32 width, I32 height);
    inline static bool changeViewport(const Rect<I32>& newViewport) {
        changeViewport(newViewport.x, newViewport.y, newViewport.z, newViewport.w);
    }

    static bool restoreViewport();
    static GLuint getBoundTextureHandle(GLuint slot);

    static void getActiveViewport(GLint* vp);

private:
    /// Prepare our shader loading system
    bool initShaders();
    /// Revert everything that was set up in "initShaders()"
    bool deInitShaders();

    bool switchWindow(I64 windowGUID);
    bool bindPipeline(const Pipeline& pipeline);
    void sendPushConstants(const PushConstants& pushConstants);
    void dispatchCompute(const ComputeParams& computeParams);

    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_base(ShaderType::COUNT) + 1> ShaderOffsetArray;
    void appendToShaderHeader(ShaderType type, const stringImpl& entry,
        ShaderOffsetArray& inOutOffset);
protected:
    /// Number of available texture units
    static GLint s_maxTextureUnits;
    /// Number of available attribute binding indices
    static GLint s_maxAttribBindings;
    /// Max number of texture attachments to an FBO
    static GLint s_maxFBOAttachments;

public:
    /// Shader block data
    static GLuint s_UBOffsetAlignment;
    static GLuint s_UBMaxSize;
    static GLuint s_SSBOffsetAlignment;
    static GLuint s_SSBMaxSize;
    static glHardwareQueryPool* s_hardwareQueryPool;
    static GLConfig s_glConfig;

private:
    GFXDevice& _context;
    /// 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)
    static GLint s_lineWidthLimit;
    /// Used to render points (e.g. to render full screen quads with geometry shaders)
    static GLuint s_dummyVAO;
    /// Preferred anisotropic filtering level
    static GLuint s_anisoLevel;
    /// A cache of all fonts used
    typedef hashMapImpl<U64, I32> FontCache;
    FontCache _fonts;
    hashAlg::pair<stringImpl, I32> _fontCache;
    static I64 s_activeWindowGUID;
    static Pipeline const* s_activePipeline;
    static glFramebuffer* s_activeRenderTarget;
    static glPixelBuffer* s_activePixelBuffer;
    /// Current active vertex array object's handle
    static GLuint s_activeVAOID;
    /// 0 - current framebuffer, 1 - current read only framebuffer, 2 - current write only framebuffer
    static GLuint s_activeFBID[3];
    /// VB, IB, SB, TB, UB, PUB, DIB
    static GLuint s_activeBufferID[7];
    static GLuint s_activeTransformFeedback;
    static GLint  s_activePackUnpackAlignments[2];
    static GLint  s_activePackUnpackRowLength[2];
    static GLint  s_activePackUnpackSkipPixels[2];
    static GLint  s_activePackUnpackSkipRows[2];
    static GLuint s_activeShaderProgram;
    static GLfloat s_depthNearVal;
    static GLfloat s_depthFarVal;
    static BlendingProperties s_blendPropertiesGlobal;
	static GLboolean s_blendEnabledGlobal;

    static vectorImpl<BlendingProperties> s_blendProperties;
    static vectorImpl<GLboolean> s_blendEnabled;

    static UColour   s_blendColour;
    static Rect<I32> s_activeViewport;
    static Rect<I32> s_previousViewport;
    static Rect<I32> s_activeScissor;
    static FColour   s_activeClearColour;

    /// The main VAO pool. We use a pool to avoid multithreading issues with VAO states
    static GLUtil::glVAOPool s_vaoPool;

    /// Boolean value used to verify if primitive restart index is enabled or
    /// disabled
    static bool s_primitiveRestartEnabled;
    static bool s_rasterizationEnabled;
    static U32  s_patchVertexCount;

    static size_t s_currentStateBlockHash;
    static size_t s_previousStateBlockHash;

    /// Current state of all available clipping planes
    std::array<bool, to_base(Frustum::FrustPlane::COUNT)> _activeClipPlanes;
    /// Hardware query objects used for performance measurements
    std::shared_ptr<glHardwareQueryRing> _elapsedTimeQuery;

    /// Duration in milliseconds to render a frame
    GLuint FRAME_DURATION_GPU;
    /// FontStash's context
    FONScontext* _fonsContext;
    /// /*texture slot*/ /*texture handle*/
    typedef std::array<GLuint, MAX_ACTIVE_TEXTURE_SLOTS> textureBoundMapDef;
    static textureBoundMapDef s_textureBoundMap;

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

    static SharedLock s_mipmapQueueLock;
    static std::unordered_set<GLuint> s_mipmapQueue;

    /// /*texture slot*/ /*sampler handle*/
    typedef std::array<GLuint, MAX_ACTIVE_TEXTURE_SLOTS> samplerBoundMapDef;
    static samplerBoundMapDef s_samplerBoundMap;
    /// /*sampler hash value*/ /*sampler object*/
    typedef hashMapImpl<size_t, glSamplerObject> samplerObjectMap;
    static SharedLock s_samplerMapLock;
    static samplerObjectMap s_samplerMap;

    static VAOBindings s_vaoBufferData;
    static bool s_opengl46Supported;

    I32 _glswState;
    CEGUI::OpenGL3Renderer* _GUIGLrenderer;
    GenericVertexData* _IMGUIBuffer;
    Time::ProfileTimer& _swapBufferTimer;
};

};  // namespace Divide
#endif

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

Diff revisions: vs.
Revision Author Commited Message
1004 Diff Diff IonutCava picture IonutCava Wed 20 Dec, 2017 17:16:32 +0000

[Ionut]
- More GUI cleanup (added multiple GUIButton events)
- Typedef vec4<U8> and vec4<F32> to UColour and FColour respectively for easier to read code.

999 Diff Diff IonutCava picture IonutCava Thu 14 Dec, 2017 00:27:08 +0000

[Ionut]
- Simply GenericVertexData class a lot
- Move IMGUI to GenericVertexData based rendering (still has rendering issues)
- Fix some crash issues on shutdown if the rendering API failed to initialize
- Disable some GL debugging flags as they seem to cause issues with the new 17.12.1 AMD driver

988 Diff Diff IonutCava picture IonutCava Thu 30 Nov, 2017 23:08:51 +0000

[Ionut]
- Add a Rect class (basic wrapper around a vec4)
- More work on MouseEvent system

983 Diff Diff IonutCava picture IonutCava Sun 26 Nov, 2017 23:34:26 +0000

[Ionut]
- Finishing up CEGUI RTT rendering. Still have alpha issues

981 Diff Diff IonutCava picture IonutCava Thu 23 Nov, 2017 18:24:51 +0000

[Ionut]
- More work on porting CEGUI to RTT

980 Diff Diff IonutCava picture IonutCava Wed 22 Nov, 2017 17:18:46 +0000

[Ionut]
- Started porting CEGUI to RTT

976 Diff Diff IonutCava picture IonutCava Sun 19 Nov, 2017 23:10:18 +0000

[Ionut]
- Texture mipmaps are now compute before bind if they need a refresh (to avoid recalculation if they’re not used)
- More work on the editor: cleanup, load/save functionality, etc
- Input scaling: Mouse position is now relative to the scene preview position in editor mode. (the scene preview rect is used to re-map X/Y absolute and relative mouse positions)

970 Diff Diff IonutCava picture IonutCava Mon 13 Nov, 2017 17:16:44 +0000

[Ionut]
- ImWindow <-> DisplayWindow fixes

969 Diff Diff IonutCava picture IonutCava Mon 13 Nov, 2017 00:15:57 +0000

[Ionut]
- Further improve DisplayWindow <-> Input relationship
- Add Flip-Y 2D camera (D3D style projection)
- Fix IMGUI rendering
- Add IMGUI sample and IMWINDOW sample rendering at the same time
- Improve IMWINDOW multi-window support
— Bug: nothing rendering in child platform windows
- Add draw command to move render context from one window to another

963 IonutCava picture IonutCava Sun 05 Nov, 2017 18:31:51 +0000

[Ionut]
- More multi-window fixes
- Profile guided performance improvements