Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
glStateTracker.h
Go to the documentation of this file.
1/*
2Copyright (c) 2018 DIVIDE-Studio
3Copyright (c) 2009 Ionut Cava
4
5This file is part of DIVIDE Framework.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software
9and associated documentation files (the "Software"), to deal in the Software
10without restriction,
11including without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense,
13and/or sell copies of the Software, and to permit persons to whom the
14Software is furnished to do so,
15subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED,
22INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23PARTICULAR PURPOSE AND NONINFRINGEMENT.
24IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25DAMAGES OR OTHER LIABILITY,
26WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27IN CONNECTION WITH THE SOFTWARE
28OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30*/
31
32#pragma once
33#ifndef DVD_GL_STATE_TRACKER_H_
34#define DVD_GL_STATE_TRACKER_H_
35
36#include "glResources.h"
41
42namespace Divide {
43
44 class Pipeline;
45 class glFramebuffer;
46 class glShaderProgram;
47
49 static constexpr U8 MAX_BOUND_TEXTURE_UNITS = 32u;
50
52
53 enum class BindResult : U8 {
54 JUST_BOUND = 0,
56 FAILED,
57 COUNT
58 };
59
60 void setDefaultState();
61
62 void setBlending(const BlendingSettings& blendingProperties);
65 void setBlending( gl46core::GLuint drawBufferIdx, const BlendingSettings& blendingProperties);
66 void resetBlending(const gl46core::GLuint drawBufferIdx) { setBlending(drawBufferIdx, _blendProperties[drawBufferIdx]); }
67 void setBlendColour(const UColour4& blendColour);
70 bool activateStateBlock(const RenderStateBlock& newBlock);
71
73 void setVertexFormat(const AttributeMap& attributes, const size_t attributeHash);
74
76 [[nodiscard]] BindResult setActiveBuffer( gl46core::GLenum target, gl46core::GLuint bufferHandle);
78 [[nodiscard]] BindResult setActiveBuffer( gl46core::GLenum target, gl46core::GLuint bufferHandle, gl46core::GLuint& previousID);
79
80 [[nodiscard]] BindResult setActiveBufferIndex( gl46core::GLenum target, gl46core::GLuint bufferHandle, gl46core::GLuint bindIndex );
81 [[nodiscard]] BindResult setActiveBufferIndex( gl46core::GLenum target, gl46core::GLuint bufferHandle, gl46core::GLuint bindIndex, gl46core::GLuint& previousID);
83 [[nodiscard]] BindResult setActiveBufferIndexRange( gl46core::GLenum target, gl46core::GLuint bufferHandle, gl46core::GLuint bindIndex, size_t offsetInBytes, size_t rangeInBytes );
84 [[nodiscard]] BindResult setActiveBufferIndexRange( gl46core::GLenum target, gl46core::GLuint bufferHandle, gl46core::GLuint bindIndex, size_t offsetInBytes, size_t rangeInBytes, gl46core::GLuint& previousID);
87 [[nodiscard]] BindResult setActiveFB(RenderTarget::Usage usage, gl46core::GLuint ID);
90 [[nodiscard]] BindResult setActiveFB(RenderTarget::Usage usage, gl46core::GLuint ID, gl46core::GLuint& previousID);
92 [[nodiscard]] BindResult setActiveProgram( gl46core::GLuint programHandle);
94 [[nodiscard]] BindResult setActiveShaderPipeline( gl46core::GLuint pipelineHandle);
96 [[nodiscard]] bool unbindTexture(TextureType type, gl46core::GLuint handle);
97 [[nodiscard]] bool unbindTextures();
100 [[nodiscard]] BindResult bindTexture( gl46core::GLubyte unit, gl46core::GLuint handle, gl46core::GLuint samplerHandle = 0u);
101 [[nodiscard]] BindResult bindTextureImage( gl46core::GLubyte unit, gl46core::GLuint handle, gl46core::GLint level, bool layered, gl46core::GLint layer, gl46core::GLenum access, gl46core::GLenum format);
103 [[nodiscard]] BindResult bindTextures( gl46core::GLubyte unitOffset, gl46core::GLuint textureCount, const gl46core::GLuint* textureHandles, const gl46core::GLuint* samplerHandles);
104 [[nodiscard]] BindResult setStateBlock(const RenderStateBlock& stateBlock);
107 [[nodiscard]] BindResult bindSamplers( gl46core::GLubyte unitOffset, gl46core::GLuint samplerCount, const gl46core::GLuint* samplerHandles);
109 [[nodiscard]] BindResult bindActiveBuffer( gl46core::GLuint location, gl46core::GLuint bufferID, size_t offset, size_t stride);
110 [[nodiscard]] BindResult bindActiveBuffers( gl46core::GLuint location, gl46core::GLsizei count, gl46core::GLuint* bufferIDs, gl46core::GLintptr* offset, gl46core::GLsizei* strides);
111
113 bool setPixelPackAlignment( const PixelAlignment& pixelPackAlignment );
115 bool setPixelUnpackAlignment( const PixelAlignment& pixelUnpackAlignment );
116 bool setScissor(const Rect<I32>& newScissorRect);
117 bool setScissor(const I32 x, const I32 y, const I32 width, const I32 height) { return setScissor({ x, y, width, height }); }
119 bool setViewport(const Rect<I32>& viewport);
120 bool setViewport(const I32 x, const I32 y, const I32 width, const I32 height) { return setViewport({ x, y, width, height }); }
121 bool setClearColour(const FColour4& colour);
122 bool setClearColour(const UColour4& colour) { return setClearColour(Util::ToFloatColour(colour)); }
123 bool setClearDepth(F32 value);
124
125 inline const RenderStateBlock& getActiveState() const { return _activeState; }
126
127 bool setAlphaToCoverage(bool state);
128 bool setDepthWrite(bool state);
129
130 [[nodiscard]] gl46core::GLuint getBoundTextureHandle(U8 slot) const noexcept;
131 [[nodiscard]] gl46core::GLuint getBoundSamplerHandle(U8 slot) const noexcept;
132 [[nodiscard]] gl46core::GLuint getBoundProgramHandle() const noexcept;
133 [[nodiscard]] gl46core::GLuint getBoundBuffer( gl46core::GLenum target, gl46core::GLuint bindIndex) const noexcept;
134 [[nodiscard]] gl46core::GLuint getBoundBuffer( gl46core::GLenum target, gl46core::GLuint bindIndex, size_t& offsetOut, size_t& rangeOut) const noexcept;
135
136 void getActiveViewport(Rect<I32>& viewportOut) const noexcept;
137
138 bool* _enabledAPIDebugging{nullptr};
139 bool* _assertOnAPIError{nullptr};
140
141 public:
143 {
145 size_t _offset{ 0u };
146 size_t _range{ 0u };
147 };
148
150
154
156
157
158 Pipeline const* _activePipeline{ nullptr };
160
166 gl46core::GLuint _activeFBID[3] { GL_NULL_HANDLE,
170 std::array<gl46core::GLuint, 13> _activeBufferID = create_array<13, gl46core::GLuint>(GL_NULL_HANDLE);
171 gl46core::GLuint _activeVAOIB{GL_NULL_HANDLE};
173
176
177 gl46core::GLuint _activeShaderProgramHandle{ 0u }; //GLUtil::_invalidObjectID;
178 gl46core::GLuint _activeShaderPipelineHandle{ 0u };//GLUtil::_invalidObjectID;
181 gl46core::GLboolean _blendEnabledGlobal{ gl46core::GL_FALSE };
182
183 // 32 buffer bindings for now
184 using BindConfig = std::array<BindConfigEntry, 32>;
185 using PerBufferConfig = std::array<BindConfig, 14>;
187
190 UColour4 _blendColour{ 0, 0, 0, 0 };
191 Rect<I32> _activeViewport{ -1, -1, -1, -1 };
192 Rect<I32> _activeScissor{ -1, -1, -1, -1 };
195
196 using TextureBoundMapDef = std::array<gl46core::GLuint, MAX_BOUND_TEXTURE_UNITS>;
198
201
202 using SamplerBoundMapDef = std::array<gl46core::GLuint, MAX_BOUND_TEXTURE_UNITS>;
204
206 eastl::queue<std::pair<gl46core::GLsync, U64>> _endFrameFences;
208
209 size_t _attributeHash{ 0u };
210
211 }; //struct GLStateTracker
212
214}; //namespace Divide
215
216
217#endif //DVD_GL_STATE_TRACKER_H_
#define FWD_DECLARE_MANAGED_STRUCT(T)
OpenGL implementation of the ShaderProgram entity.
constexpr U8 MAX_DEBUG_SCOPE_DEPTH
Maximum number of nested debug scopes we support in the renderer.
Definition: config.h:147
FColour4 ToFloatColour(const UColour4 &byteColour) noexcept
Definition: MathHelper.cpp:268
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
int32_t I32
uint8_t U8
constexpr gl46core::GLuint GL_NULL_HANDLE
Invalid object value. Used to compare handles and determine if they were properly created.
Definition: glResources.h:105
eastl::vector< Type > vector
Definition: Vector.h:42
constexpr RenderTargetID INVALID_RENDER_TARGET_ID
U32 RenderTargetID
uint32_t U32
uint64_t U64
constexpr auto to_base(const Type value) -> Type
U32 _handle
size_t _range
size_t _offset
gl46core::GLuint getBoundBuffer(gl46core::GLenum target, gl46core::GLuint bindIndex) const noexcept
std::array< BindConfig, 14 > PerBufferConfig
RenderTargetID _activeRenderTargetID
gl46core::GLuint getBoundProgramHandle() const noexcept
void setVertexFormat(const AttributeMap &attributes, const size_t attributeHash)
BindResult setActiveBufferIndex(gl46core::GLenum target, gl46core::GLuint bufferHandle, gl46core::GLuint bindIndex)
BlendingSettings _blendPropertiesGlobal
std::array< gl46core::GLuint, MAX_BOUND_TEXTURE_UNITS > SamplerBoundMapDef
eastl::queue< std::pair< gl46core::GLsync, U64 > > _endFrameFences
static constexpr U8 MAX_BOUND_TEXTURE_UNITS
bool setAlphaToCoverage(bool state)
BindResult setActiveBuffer(gl46core::GLenum target, gl46core::GLuint bufferHandle)
Single place to change buffer objects for every target available.
vector< gl46core::GLboolean > _blendEnabled
RenderStateBlock _activeState
BindResult bindTextureImage(gl46core::GLubyte unit, gl46core::GLuint handle, gl46core::GLint level, bool layered, gl46core::GLint layer, gl46core::GLenum access, gl46core::GLenum format)
void setBlendColour(const UColour4 &blendColour)
bool setViewport(const Rect< I32 > &viewport)
Change the current viewport area. Redundancy check is performed in GFXDevice class.
Pipeline const * _activePipeline
bool setClearColour(const UColour4 &colour)
PerBufferConfig _currentBindConfig
void setBlending(const BlendingSettings &blendingProperties)
BindResult setStateBlock(const RenderStateBlock &stateBlock)
bool unbindTexture(TextureType type, gl46core::GLuint handle)
Returns true if the texture was bound. If the texture was not bound, no state is changed.
void resetBlending(const gl46core::GLuint drawBufferIdx)
BindResult bindActiveBuffer(gl46core::GLuint location, gl46core::GLuint bufferID, size_t offset, size_t stride)
Modify buffer bindings for the active vao.
gl46core::GLuint _activeShaderPipelineHandle
gl46core::GLuint _activeFBID[3]
0 - current framebuffer, 1 - current read only framebuffer, 2 - current write only framebuffer
BindResult setActiveBufferIndexRange(gl46core::GLenum target, gl46core::GLuint bufferHandle, gl46core::GLuint bindIndex, size_t offsetInBytes, size_t rangeInBytes)
Same as normal setActiveBuffer but handles proper binding of different ranges.
PixelAlignment _unpackAlignment
gl46core::GLboolean _blendEnabledGlobal
std::array< AttributeDescriptor, to_base(AttribLocation::COUNT)> AttributeSettings
vector< ImageBindSettings > ImageBoundMapDef
bool setScissor(const I32 x, const I32 y, const I32 width, const I32 height)
bool activateStateBlock(const RenderStateBlock &newBlock)
glShaderProgram * _activeShaderProgram
bool setPixelPackAlignment(const PixelAlignment &pixelPackAlignment)
Pixel pack alignment is usually changed by textures, PBOs, etc.
TextureBoundMapDef _textureBoundMap
gl46core::GLuint _activeVAOIB
BindResult bindActiveBuffers(gl46core::GLuint location, gl46core::GLsizei count, gl46core::GLuint *bufferIDs, gl46core::GLintptr *offset, gl46core::GLsizei *strides)
PrimitiveTopology _activeTopology
std::array< gl46core::GLuint, MAX_BOUND_TEXTURE_UNITS > TextureBoundMapDef
const RenderStateBlock & getActiveState() const
bool setScissor(const Rect< I32 > &newScissorRect)
void getActiveViewport(Rect< I32 > &viewportOut) const noexcept
void setPrimitiveTopology(PrimitiveTopology topology)
vec2< U16 > _activeRenderTargetDimensions
BindResult bindSamplers(gl46core::GLubyte unitOffset, gl46core::GLuint samplerCount, const gl46core::GLuint *samplerHandles)
gl46core::GLuint _activeShaderProgramHandle
ImageBoundMapDef _imageBoundMap
bool setPixelUnpackAlignment(const PixelAlignment &pixelUnpackAlignment)
Pixel unpack alignment is usually changed by textures, PBOs, etc.
AttributeSettings _currentAttributes
gl46core::GLuint getBoundTextureHandle(U8 slot) const noexcept
BindResult bindTexture(gl46core::GLubyte unit, gl46core::GLuint handle, gl46core::GLuint samplerHandle=0u)
Bind a texture specified by a GL handle and GL type to the specified unit using the sampler object de...
SamplerBoundMapDef _samplerBoundMap
BindResult setActiveShaderPipeline(gl46core::GLuint pipelineHandle)
Change the currently active shader pipeline. Returns false if the pipeline was already bound.
bool setDepthWrite(bool state)
DebugScope _debugScope[Config::MAX_DEBUG_SCOPE_DEPTH]
std::array< BindConfigEntry, 32 > BindConfig
std::array< gl46core::GLuint, 13 > _activeBufferID
VB, IB, SB, TB, UB, PUB, DIB.
vector< BlendingSettings > _blendProperties
PixelAlignment _packAlignment
BindResult bindTextures(gl46core::GLubyte unitOffset, gl46core::GLuint textureCount, const gl46core::GLuint *textureHandles, const gl46core::GLuint *samplerHandles)
Bind multiple textures specified by an array of handles and an offset unit.
BindResult setActiveProgram(gl46core::GLuint programHandle)
Change the currently active shader program. Returns false if the program was already bound.
bool setClearColour(const FColour4 &colour)
BindResult setActiveFB(RenderTarget::Usage usage, gl46core::GLuint ID)
glFramebuffer * _activeRenderTarget
bool setViewport(const I32 x, const I32 y, const I32 width, const I32 height)
bool setClearDepth(F32 value)
gl46core::GLuint getBoundSamplerHandle(U8 slot) const noexcept
DebugScope _lastInsertedDebugMessage