Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
GLWrapper.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_GL_WRAPPER_H_
34#define DVD_GL_WRAPPER_H_
35
36#include "glStateTracker.h"
37#include "glHardwareQuery.h"
38
42
43struct FONScontext;
44struct ImDrawData;
45
46namespace Divide
47{
48
49namespace Time
50{
51 class ProfileTimer;
52}
53
54enum class ShaderResult : U8;
55
56class DisplayWindow;
57class glHardwareQueryRing;
58class glHardwareQueryPool;
59
60struct BufferLockEntry;
61
63
65class GL_API final : public RenderAPIWrapper {
66 friend class glShader;
67 friend class glTexture;
68 friend class glFramebuffer;
69 friend class glVertexArray;
70 friend class glShaderProgram;
71 friend class glSamplerObject;
72 friend class glGenericVertexData;
73
74 friend struct GLStateTracker;
75
76public:
77 // Auto-delete locks older than this number of frames
78 static constexpr U32 s_LockFrameLifetime = 3u; //(APP->Driver->GPU)
79
80public:
81 GL_API(GFXDevice& context);
82
83private:
85 ErrorCode initRenderingAPI(I32 argc, char** argv, Configuration& config) override;
87 void closeRenderingAPI() override;
88
89 [[nodiscard]] bool drawToWindow( DisplayWindow& window ) override;
90 void onRenderThreadLoopStart( ) override;
91 void onRenderThreadLoopEnd() override;
92 void prepareFlushWindow( DisplayWindow& window ) override;
93 void flushWindow( DisplayWindow& window) override;
94 [[nodiscard]] bool frameStarted() override;
95 [[nodiscard]] bool frameEnded() override;
96
98
99 void idle(bool fast) override;
100
101 void preFlushCommandBuffer(Handle<GFX::CommandBuffer> commandBuffer) override;
102
103 void flushCommand( GFX::CommandBase* cmd ) override;
104
105 void postFlushCommandBuffer(Handle<GFX::CommandBuffer> commandBuffer) override;
106
107 void onThreadCreated(const std::thread::id& threadID, bool isMainRenderThread ) override;
108
110 void clearStates(GLStateTracker& stateTracker) const;
111
112 [[nodiscard]] bool bindShaderResources( const DescriptorSetEntries& descriptorSetEntries ) override;
113
114 [[nodiscard]] bool makeTextureViewResident( gl46core::GLubyte bindingSlot, const ImageView& imageView, size_t imageViewHash, SamplerDescriptor sampler, size_t samplerHash ) const;
115
116 bool setViewportInternal(const Rect<I32>& viewport) override;
117 bool setScissorInternal( const Rect<I32>& scissor ) override;
118
119
121
122 [[nodiscard]] gl46core::GLuint getGLTextureView(ImageView srcView, size_t srcViewHash, U8 lifetimeInFrames) const;
123
124 void initDescriptorSets() override;
125
127
128 [[nodiscard]] RenderTarget_uptr newRT( const RenderTargetDescriptor& descriptor ) const override;
129 [[nodiscard]] GenericVertexData_ptr newGVD( U32 ringBufferLength, const std::string_view name ) const override;
130 [[nodiscard]] ShaderBuffer_uptr newSB( const ShaderBufferDescriptor& descriptor ) const override;
131
132public:
133 [[nodiscard]] static GLStateTracker& GetStateTracker() noexcept;
134 [[nodiscard]] static GLUtil::GLMemory::GLMemoryType GetMemoryTypeForUsage( gl46core::GLenum usage) noexcept;
135 [[nodiscard]] static GLUtil::GLMemory::DeviceAllocator& GetMemoryAllocator(GLUtil::GLMemory::GLMemoryType memoryType) noexcept;
136
137 static void QueueFlush() noexcept;
138
139 static void AddDebugMessage( const char* message, U32 id = U32_MAX );
140 static void PushDebugMessage( const char* message, U32 id = U32_MAX );
141 static void PopDebugMessage();
142
143 [[nodiscard]] static bool DeleteShaderPrograms( gl46core::GLuint count, gl46core::GLuint * programs);
144 [[nodiscard]] static bool DeleteSamplers( gl46core::GLuint count, gl46core::GLuint* samplers);
145 [[nodiscard]] static bool DeleteBuffers( gl46core::GLuint count, gl46core::GLuint* buffers);
146 [[nodiscard]] static bool DeleteFramebuffers( gl46core::GLuint count, gl46core::GLuint* framebuffers);
147
148 [[nodiscard]] static gl46core::GLuint GetSamplerHandle(SamplerDescriptor sampler, size_t& samplerHashInOut);
149
150 [[nodiscard]] static glHardwareQueryPool* GetHardwareQueryPool() noexcept;
151
152 [[nodiscard]] static gl46core::GLsync CreateFenceSync();
153 static void DestroyFenceSync( gl46core::GLsync& sync);
154
155protected:
156 static ShaderResult BindPipeline(GFXDevice& context, const Pipeline& pipeline);
157 static bool Draw( const GenericDrawCommand& cmd );
158
159private:
160
161 enum class GlobalQueryTypes : U8 {
166 GPU_TIME,
167 COUNT
168 };
169
171 {
174 U8 _index{ 0u };
175 };
176
178 {
179 gl46core::GLuint _handle{ GL_NULL_HANDLE };
180 gl46core::GLuint _sampler{ GL_NULL_HANDLE };
181 gl46core::GLubyte _slot{ INVALID_TEXTURE_BINDING };
182 };
183
184 static eastl::fixed_vector<TexBindEntry, 32, false> s_TexBindQueue;
185
187 static constexpr size_t InitialSamplerMapSize = 64u;
188
190 {
191 size_t _hash = SIZE_MAX;
192 gl46core::GLuint _glHandle = GL_NULL_HANDLE;
193 };
194 using SamplerObjectMap = eastl::fixed_vector<CachedSamplerEntry, InitialSamplerMapSize, true>;
195
196private:
199
202 std::array<glHardwareQueryRing_uptr, to_base(GlobalQueryTypes::COUNT)> _performanceQueries;
203 // OpenGL rendering is not thread-safe anyway, so this works
204 eastl::stack<HardwareQueryContext> _queryContext;
205 bool _runQueries{false};
206
207 bool _uniformsNeedLock{false};
209
210 gl46core::GLuint _dummyVAO{ GL_NULL_HANDLE };
211
212private:
213
214 static std::atomic_bool s_glFlushQueued;
215
219
222
224
225 static std::unique_ptr<glHardwareQueryPool> s_hardwareQueryPool;
226
228};
229
230}; // namespace Divide
231
232#endif //DVD_GL_WRAPPER_H_
#define FWD_DECLARE_MANAGED_STRUCT(T)
char * argv[]
Definition: main.cpp:8
Rough around the edges Adapter pattern abstracting the actual rendering API and access to the GPU.
Definition: GFXDevice.h:215
OpenGL implementation of the RenderAPIWrapper.
Definition: GLWrapper.h:65
static eastl::fixed_vector< TexBindEntry, 32, false > s_TexBindQueue
Definition: GLWrapper.h:184
GFXDevice & _context
Definition: GLWrapper.h:197
static std::atomic_bool s_glFlushQueued
Definition: GLWrapper.h:214
void onThreadCreated(const std::thread::id &threadID, bool isMainRenderThread) override
Definition: GLWrapper.cpp:1423
static void PopDebugMessage()
Definition: GLWrapper.cpp:1818
static glHardwareQueryPool * GetHardwareQueryPool() noexcept
Definition: GLWrapper.cpp:1983
static void DestroyFenceSync(gl46core::GLsync &sync)
Definition: GLWrapper.cpp:1998
gl46core::GLuint getGLTextureView(ImageView srcView, size_t srcViewHash, U8 lifetimeInFrames) const
Definition: GLWrapper.cpp:836
void onRenderThreadLoopStart() override
Definition: GLWrapper.cpp:552
void clearStates(GLStateTracker &stateTracker) const
Reset as much of the GL default state as possible within the limitations given.
Definition: GLWrapper.cpp:1462
static bool DeleteShaderPrograms(gl46core::GLuint count, gl46core::GLuint *programs)
Definition: GLWrapper.cpp:1829
GenericVertexData_ptr newGVD(U32 ringBufferLength, const std::string_view name) const override
Definition: GLWrapper.cpp:2014
static gl46core::GLsync CreateFenceSync()
Definition: GLWrapper.cpp:1988
static std::array< size_t, to_base(GLUtil::GLMemory::GLMemoryType::COUNT)> s_memoryAllocatorSizes
Definition: GLWrapper.h:221
static constexpr U32 s_LockFrameLifetime
Definition: GLWrapper.h:78
static void PushDebugMessage(const char *message, U32 id=U32_MAX)
Definition: GLWrapper.cpp:1806
void postFlushCommandBuffer(Handle< GFX::CommandBuffer > commandBuffer) override
Definition: GLWrapper.cpp:1402
gl46core::GLuint _dummyVAO
Definition: GLWrapper.h:210
bool setScissorInternal(const Rect< I32 > &scissor) override
Definition: GLWrapper.cpp:1669
static GLUtil::GLMemory::DeviceAllocator & GetMemoryAllocator(GLUtil::GLMemory::GLMemoryType memoryType) noexcept
Definition: GLWrapper.cpp:1778
Time::ProfileTimer & _swapBufferTimer
Definition: GLWrapper.h:198
bool _uniformsNeedLock
Definition: GLWrapper.h:207
static void AddDebugMessage(const char *message, U32 id=U32_MAX)
Definition: GLWrapper.cpp:1795
eastl::fixed_vector< CachedSamplerEntry, InitialSamplerMapSize, true > SamplerObjectMap
Definition: GLWrapper.h:194
std::array< glHardwareQueryEntry, to_base(QueryType::COUNT)> HardwareQueryContext
Definition: GLWrapper.h:186
GFX::MemoryBarrierCommand _uniformsMemCommand
Definition: GLWrapper.h:208
void closeRenderingAPI() override
Clear everything that was setup in initRenderingAPI()
Definition: GLWrapper.cpp:494
bool makeTextureViewResident(gl46core::GLubyte bindingSlot, const ImageView &imageView, size_t imageViewHash, SamplerDescriptor sampler, size_t samplerHash) const
Definition: GLWrapper.cpp:1614
static constexpr size_t InitialSamplerMapSize
Definition: GLWrapper.h:187
static SharedMutex s_samplerMapLock
Definition: GLWrapper.h:216
static GLStateTracker & GetStateTracker() noexcept
Definition: GLWrapper.cpp:1749
bool drawToWindow(DisplayWindow &window) override
Prepare the GPU for rendering a frame.
Definition: GLWrapper.cpp:545
static bool DeleteFramebuffers(gl46core::GLuint count, gl46core::GLuint *framebuffers)
Definition: GLWrapper.cpp:1908
static GLStateTracker s_stateTracker
Definition: GLWrapper.h:218
void onRenderThreadLoopEnd() override
Definition: GLWrapper.cpp:557
ErrorCode initRenderingAPI(I32 argc, char **argv, Configuration &config) override
Try and create a valid OpenGL context taking in account the specified command line arguments.
Definition: GLWrapper.cpp:123
static ShaderResult BindPipeline(GFXDevice &context, const Pipeline &pipeline)
Definition: GLWrapper.cpp:1674
void flushPushConstantsLocks()
Definition: GLWrapper.cpp:873
eastl::stack< HardwareQueryContext > _queryContext
Definition: GLWrapper.h:204
bool frameEnded() override
Definition: GLWrapper.cpp:622
static GLUtil::GLMemory::GLMemoryType GetMemoryTypeForUsage(gl46core::GLenum usage) noexcept
Definition: GLWrapper.cpp:1754
std::array< glHardwareQueryRing_uptr, to_base(GlobalQueryTypes::COUNT)> _performanceQueries
Hardware query objects used for performance measurements.
Definition: GLWrapper.h:202
static gl46core::GLuint GetSamplerHandle(SamplerDescriptor sampler, size_t &samplerHashInOut)
Return the OpenGL sampler object's handle for the given hash value.
Definition: GLWrapper.cpp:1931
ShaderBuffer_uptr newSB(const ShaderBufferDescriptor &descriptor) const override
Definition: GLWrapper.cpp:2019
void flushCommand(GFX::CommandBase *cmd) override
Definition: GLWrapper.cpp:889
void flushWindow(DisplayWindow &window) override
Definition: GLWrapper.cpp:566
void idle(bool fast) override
Definition: GLWrapper.cpp:677
static bool DeleteBuffers(gl46core::GLuint count, gl46core::GLuint *buffers)
Definition: GLWrapper.cpp:1879
static SamplerObjectMap s_samplerMap
Definition: GLWrapper.h:217
static U32 s_fenceSyncCounter[s_LockFrameLifetime]
Definition: GLWrapper.h:227
static void QueueFlush() noexcept
Definition: GLWrapper.cpp:1783
friend class glVertexArray
Definition: GLWrapper.h:69
bool _runQueries
Definition: GLWrapper.h:205
void preFlushCommandBuffer(Handle< GFX::CommandBuffer > commandBuffer) override
Definition: GLWrapper.cpp:883
bool frameStarted() override
Definition: GLWrapper.cpp:587
static std::array< GLUtil::GLMemory::DeviceAllocator, to_base(GLUtil::GLMemory::GLMemoryType::COUNT)> s_memoryAllocators
Definition: GLWrapper.h:220
void initDescriptorSets() override
Definition: GLWrapper.cpp:1418
static bool DeleteSamplers(gl46core::GLuint count, gl46core::GLuint *samplers)
Definition: GLWrapper.cpp:1851
void endPerformanceQueries()
Definition: GLWrapper.cpp:530
RenderTarget_uptr newRT(const RenderTargetDescriptor &descriptor) const override
Definition: GLWrapper.cpp:2009
static GLUtil::glTextureViewCache s_textureViewCache
Definition: GLWrapper.h:223
bool bindShaderResources(const DescriptorSetEntries &descriptorSetEntries) override
Definition: GLWrapper.cpp:1508
static std::unique_ptr< glHardwareQueryPool > s_hardwareQueryPool
Definition: GLWrapper.h:225
void prepareFlushWindow(DisplayWindow &window) override
Definition: GLWrapper.cpp:561
HardwareQueryContext _primitiveQueries
Definition: GLWrapper.h:200
void flushTextureBindQueue()
Definition: GLWrapper.cpp:735
bool setViewportInternal(const Rect< I32 > &viewport) override
Definition: GLWrapper.cpp:1664
Renderer Programming Interface.
OpenGL implementation of the ShaderProgram entity.
struct FONScontext FONScontext
Definition: fontstash.h:119
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::array< DescriptorSetEntry, to_base(DescriptorSetUsage::COUNT)> DescriptorSetEntries
void Draw()
int32_t I32
uint8_t U8
std::shared_mutex SharedMutex
Definition: SharedMutex.h:43
constexpr gl46core::GLuint GL_NULL_HANDLE
Invalid object value. Used to compare handles and determine if they were properly created.
Definition: glResources.h:105
constexpr U8 INVALID_TEXTURE_BINDING
constexpr U32 U32_MAX
uint32_t U32
constexpr auto to_base(const Type value) -> Type
Definition: GLWrapper.h:190
size_t _hash
Definition: GLWrapper.h:191
gl46core::GLuint _glHandle
Definition: GLWrapper.h:192
Definition: GLWrapper.h:178
gl46core::GLuint _sampler
Definition: GLWrapper.h:180
gl46core::GLubyte _slot
Definition: GLWrapper.h:181
gl46core::GLuint _handle
Definition: GLWrapper.h:179
Definition: GLWrapper.h:171
QueryType _type
Definition: GLWrapper.h:173
U8 _index
Definition: GLWrapper.h:174
glHardwareQueryRing * _query
Definition: GLWrapper.h:172