diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a2f8170..a353fd77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -141,6 +141,8 @@ set(SOURCE_FILES MobileGL/MG_State/GLState/RenderState/RenderState.cpp MobileGL/MG_State/GLState/FramebufferState/FramebufferObject.cpp MobileGL/MG_State/GLState/FramebufferState/FramebufferState.cpp + MobileGL/MG_State/GLState/SamplerState/SamplerObject.cpp + MobileGL/MG_State/GLState/SamplerState/SamplerState.cpp ) add_library(${CMAKE_PROJECT_NAME} SHARED diff --git a/MobileGL/MG_State/GLState/Core.cpp b/MobileGL/MG_State/GLState/Core.cpp index d75233a5..8a1f8a3b 100644 --- a/MobileGL/MG_State/GLState/Core.cpp +++ b/MobileGL/MG_State/GLState/Core.cpp @@ -338,6 +338,40 @@ namespace MobileGL { return m_framebufferState.ValidateFramebufferObject(index); } + // Sampler + Vector GLContext::GenSamplerNames(Uint number) { + return m_samplerState.GenerateNames(number); + } + + SharedPtr GLContext::GetSamplerObject(Uint index) { + return m_samplerState.GetSamplerObject(index); + } + + SharedPtr GLContext::CreateSamplerObject(Uint index) { + return m_samplerState.CreateSamplerObject(index); + } + + void GLContext::MarkSamplerObjectForDeletion(Uint index) { + // Unbind the sampler from all texture units + if (ValidateSamplerObject(index)) { + auto sampler = m_samplerState.GetSamplerObject(index); + for (Int unit = 0; unit < TextureState::MAX_TEXTURE_IMAGE_UNITS; ++unit) { + auto& textureUnit = m_textureState.GetUnitObject(unit); + if (textureUnit.GetSamplerObject() == sampler) { + textureUnit.SetSamplerObject(nullptr); + } + } + } + m_samplerState.MarkSamplerObjectForDeletion(index); + } + + Bool GLContext::ValidateSamplerName(Uint index) const { + return m_samplerState.ValidateName(index); + } + + Bool GLContext::ValidateSamplerObject(Uint index) const { + return m_samplerState.ValidateSamplerObject(index); + } } // namespace GLState GLState::GLContext* pGLContext; diff --git a/MobileGL/MG_State/GLState/Core.h b/MobileGL/MG_State/GLState/Core.h index edbc9c41..8bafd22d 100644 --- a/MobileGL/MG_State/GLState/Core.h +++ b/MobileGL/MG_State/GLState/Core.h @@ -4,6 +4,7 @@ #include "ErrorState/Error.h" #include "BufferState/BufferState.h" #include "ProgramState/ProgramState.h" +#include "SamplerState/SamplerState.h" #include "TextureState/TextureState.h" #include "FramebufferState/FramebufferState.h" #include "VertexArrayState/VertexArrayState.h" @@ -109,6 +110,14 @@ namespace MobileGL { Bool ValidateFramebufferName(Uint index) const; Bool ValidateFramebufferObject(Uint index) const; + // Sampler + Vector GenSamplerNames(Uint number); + SharedPtr GetSamplerObject(Uint index); + SharedPtr CreateSamplerObject(Uint index); + void MarkSamplerObjectForDeletion(Uint index); + Bool ValidateSamplerName(Uint index) const; + Bool ValidateSamplerObject(Uint index) const; + private: // Error ErrorState m_errorState; @@ -130,6 +139,9 @@ namespace MobileGL { // Framebuffer FramebufferState m_framebufferState; + + // Sampler + SamplerState m_samplerState; }; } // namespace GLState diff --git a/MobileGL/MG_State/GLState/SamplerState/SamplerObject.cpp b/MobileGL/MG_State/GLState/SamplerState/SamplerObject.cpp new file mode 100644 index 00000000..a11ac085 --- /dev/null +++ b/MobileGL/MG_State/GLState/SamplerState/SamplerObject.cpp @@ -0,0 +1,108 @@ +#include "SamplerObject.h" + +namespace MobileGL { + namespace MG_State { + namespace GLState { + + SamplerObject::SamplerObject(Uint externalIndex) + : m_externalIndex(externalIndex), m_wrapS(SamplerWrapMode::Repeat), m_wrapT(SamplerWrapMode::Repeat), + m_wrapR(SamplerWrapMode::Repeat), m_minFilter(SamplerFilterMode::Linear), + m_magFilter(SamplerFilterMode::Linear), m_mipmapMode(SamplerMipmapMode::None), m_minLod(0.0f), + m_maxLod(1000.0f), m_lodBias(0.0f), m_compareMode(SamplerCompareMode::None), + m_compareFunc(SamplerCompareFunc::Less) {} + + void SamplerObject::SetWrapS(SamplerWrapMode mode) { + m_wrapS = mode; + } + + void SamplerObject::SetWrapT(SamplerWrapMode mode) { + m_wrapT = mode; + } + + void SamplerObject::SetWrapR(SamplerWrapMode mode) { + m_wrapR = mode; + } + + void SamplerObject::SetMinFilter(SamplerFilterMode mode) { + m_minFilter = mode; + } + + void SamplerObject::SetMagFilter(SamplerFilterMode mode) { + m_magFilter = mode; + } + + void SamplerObject::SetMipmapMode(SamplerMipmapMode mode) { + m_mipmapMode = mode; + } + + void SamplerObject::SetLodRange(Float minLod, Float maxLod) { + if (minLod > maxLod) { + THROW_EXCEPTION("minLod cannot be greater than maxLod"); + } + m_minLod = minLod; + m_maxLod = maxLod; + } + + void SamplerObject::SetLodBias(Float bias) { + m_lodBias = bias; + } + + void SamplerObject::SetSamplerCompareFunc(SamplerCompareFunc func) { + m_compareFunc = func; + } + + void SamplerObject::SetCompareMode(SamplerCompareMode mode) { + m_compareMode = mode; + } + + SamplerWrapMode SamplerObject::GetWrapS() const { + return m_wrapS; + } + + SamplerWrapMode SamplerObject::GetWrapT() const { + return m_wrapT; + } + + SamplerWrapMode SamplerObject::GetWrapR() const { + return m_wrapR; + } + + SamplerFilterMode SamplerObject::GetMinFilter() const { + return m_minFilter; + } + + SamplerFilterMode SamplerObject::GetMagFilter() const { + return m_magFilter; + } + + SamplerMipmapMode SamplerObject::GetMipmapMode() const { + return m_mipmapMode; + } + + Float SamplerObject::GetMinLod() const { + return m_minLod; + } + + Float SamplerObject::GetMaxLod() const { + return m_maxLod; + } + + Float SamplerObject::GetLodBias() const { + return m_lodBias; + } + + SamplerCompareMode SamplerObject::GetCompareMode() const { + return m_compareMode; + } + + SamplerCompareFunc SamplerObject::GetSamplerCompareFunc() const { + return m_compareFunc; + } + + Uint SamplerObject::GetExternalIndex() const { + return m_externalIndex; + } + + } // namespace GLState + } // namespace MG_State +} // namespace MobileGL diff --git a/MobileGL/MG_State/GLState/SamplerState/SamplerObject.h b/MobileGL/MG_State/GLState/SamplerState/SamplerObject.h new file mode 100644 index 00000000..04dced30 --- /dev/null +++ b/MobileGL/MG_State/GLState/SamplerState/SamplerObject.h @@ -0,0 +1,90 @@ +#pragma once +#include + +namespace MobileGL { + enum class SamplerFilterMode { + Nearest, + Linear, + SamplerFilterCount, + Unknown = -1 + }; + + enum class SamplerMipmapMode { + None, + Nearest, + SamplerMipmapModeCount, + Unknown = -1 + }; + + enum class SamplerWrapMode { + ClampToEdge, + MirroredRepeat, + Repeat, + ClampToBorder, + SamplerWrapModeCount, + Unknown = -1 + }; + + enum class SamplerCompareMode { + None, + CompareToTexture, + SamplerCompareModeCount, + Unknown = -1 + }; + + enum class SamplerCompareFunc { + Never, + Less, + Equal, + LessEqual, + Greater, + NotEqual, + GreaterEqual, + Always, + SamplerCompareFuncCount, + Unknown = -1 + }; + + namespace MG_State { + namespace GLState { + class SamplerObject { + public: + SamplerObject(Uint externalIndex); + + void SetWrapS(SamplerWrapMode mode); + void SetWrapT(SamplerWrapMode mode); + void SetWrapR(SamplerWrapMode mode); + void SetMinFilter(SamplerFilterMode mode); + void SetMagFilter(SamplerFilterMode mode); + void SetMipmapMode(SamplerMipmapMode mode); + void SetLodRange(Float minLod, Float maxLod); + void SetLodBias(Float bias); + void SetSamplerCompareFunc(SamplerCompareFunc func); + void SetCompareMode(SamplerCompareMode mode); + + SamplerWrapMode GetWrapS() const; + SamplerWrapMode GetWrapT() const; + SamplerWrapMode GetWrapR() const; + SamplerFilterMode GetMinFilter() const; + SamplerFilterMode GetMagFilter() const; + SamplerMipmapMode GetMipmapMode() const; + Float GetMinLod() const; + Float GetMaxLod() const; + Float GetLodBias() const; + SamplerCompareMode GetCompareMode() const; + SamplerCompareFunc GetSamplerCompareFunc() const; + Uint GetExternalIndex() const; + + private: + const Uint m_externalIndex; + SamplerWrapMode m_wrapS, m_wrapT, m_wrapR; + SamplerFilterMode m_minFilter, m_magFilter; + SamplerMipmapMode m_mipmapMode; + Float m_minLod, m_maxLod; + Float m_lodBias; + SamplerCompareMode m_compareMode; + SamplerCompareFunc m_compareFunc; + }; + } // namespace GLState + } // namespace MG_State +} // namespace MobileGL diff --git a/MobileGL/MG_State/GLState/SamplerState/SamplerState.cpp b/MobileGL/MG_State/GLState/SamplerState/SamplerState.cpp new file mode 100644 index 00000000..805e75c8 --- /dev/null +++ b/MobileGL/MG_State/GLState/SamplerState/SamplerState.cpp @@ -0,0 +1,41 @@ +#include "SamplerState.h" + +namespace MobileGL { + namespace MG_State { + namespace GLState { + SamplerState::SamplerState() : m_indexGenerator(1024, 1) {} + + Vector SamplerState::GenerateNames(Uint number) { + Vector names(number); + m_indexGenerator.Generate(number, names.data()); + return names; + } + + SharedPtr SamplerState::GetSamplerObject(Uint index) { + auto it = m_samplerObjects.find(index); + return it != m_samplerObjects.end() ? it->second : nullptr; + } + + SharedPtr SamplerState::CreateSamplerObject(Uint index) { + auto sampler = MakeShared(index); + m_samplerObjects[index] = sampler; + return sampler; + } + + void SamplerState::MarkSamplerObjectForDeletion(Uint index) { + if (m_indexGenerator.IsValid(index)) { + m_samplerObjects.erase(index); + m_indexGenerator.Delete(index); + } + } + + Bool SamplerState::ValidateName(Uint index) const { + return m_indexGenerator.IsValid(index); + } + + Bool SamplerState::ValidateSamplerObject(Uint index) const { + return m_samplerObjects.find(index) != m_samplerObjects.end(); + } + } + } +} diff --git a/MobileGL/MG_State/GLState/SamplerState/SamplerState.h b/MobileGL/MG_State/GLState/SamplerState/SamplerState.h new file mode 100644 index 00000000..ba8c4b97 --- /dev/null +++ b/MobileGL/MG_State/GLState/SamplerState/SamplerState.h @@ -0,0 +1,26 @@ +#pragma once +#include "SamplerObject.h" +#include +#include + +namespace MobileGL { + namespace MG_State { + namespace GLState { + class SamplerState { + public: + SamplerState(); + + Vector GenerateNames(Uint number); + SharedPtr GetSamplerObject(Uint index); + SharedPtr CreateSamplerObject(Uint index); + void MarkSamplerObjectForDeletion(Uint index); + Bool ValidateName(Uint index) const; + Bool ValidateSamplerObject(Uint index) const; + + private: + UnorderedMap> m_samplerObjects; + IndexGenerator m_indexGenerator; + }; + } // namespace GLState + } // namespace MG_State +} // namespace MobileGL diff --git a/MobileGL/MG_State/GLState/TextureState/SamplerObject.h b/MobileGL/MG_State/GLState/TextureState/SamplerObject.h deleted file mode 100644 index 2cc966d0..00000000 --- a/MobileGL/MG_State/GLState/TextureState/SamplerObject.h +++ /dev/null @@ -1,102 +0,0 @@ -#pragma once -#include "MG_Util/Types.h" -#include - -namespace MobileGL { - enum class SamplerFilterMode { - Nearest, - Linear, - SamplerFilterCount, - Unknown = -1 - }; - - enum class SamplerMipmapMode { - None, - Nearest, - SamplerMipmapModeCount, - Unknown = -1 - }; - - enum class SamplerWrapMode { - ClampToEdge, - MirroredRepeat, - Repeat, - ClampToBorder, - SamplerWrapModeCount, - Unknown = -1 - }; - - enum class SamplerCompareMode { - None, - CompareToTexture, - SamplerCompareModeCount, - Unknown = -1 - }; - - enum class SamplerCompareFunc { - Never, - Less, - Equal, - LessEqual, - Greater, - NotEqual, - GreaterEqual, - Always, - SamplerCompareFuncCount, - Unknown = -1 - }; - - namespace MG_State { - namespace GLState { - class SamplerObject { - public: - SamplerObject(Uint externalIndex) - : m_wrapS(SamplerWrapMode::Repeat), m_wrapT(SamplerWrapMode::Repeat), - m_wrapR(SamplerWrapMode::Repeat), m_minFilter(SamplerFilterMode::Linear), - m_magFilter(SamplerFilterMode::Linear), m_mipmapMode(SamplerMipmapMode::None), m_minLod(0.0f), - m_maxLod(1000.0f), m_lodBias(0.0f), m_compareMode(SamplerCompareMode::None), - m_compareFunc(SamplerCompareFunc::Less), m_externalIndex(externalIndex) {} - - void SetWrapS(SamplerWrapMode mode) { m_wrapS = mode; } - void SetWrapT(SamplerWrapMode mode) { m_wrapT = mode; } - void SetWrapR(SamplerWrapMode mode) { m_wrapR = mode; } - void SetMinFilter(SamplerFilterMode mode) { m_minFilter = mode; } - void SetMagFilter(SamplerFilterMode mode) { m_magFilter = mode; } - void SetMipmapMode(SamplerMipmapMode mode) { m_mipmapMode = mode; } - void SetLodRange(Float minLod, Float maxLod) { - if (minLod > maxLod) { - THROW_EXCEPTION("minLod cannot be greater than maxLod"); - } - m_minLod = minLod; - m_maxLod = maxLod; - } - void SetLodBias(Float bias) { m_lodBias = bias; } - void SetSamplerCompareFunc(SamplerCompareFunc func) { m_compareFunc = func; } - void SetCompareMode(SamplerCompareMode mode) { m_compareMode = mode; } - - SamplerWrapMode GetWrapS() const { return m_wrapS; } - SamplerWrapMode GetWrapT() const { return m_wrapT; } - SamplerWrapMode GetWrapR() const { return m_wrapR; } - SamplerFilterMode GetMinFilter() const { return m_minFilter; } - SamplerFilterMode GetMagFilter() const { return m_magFilter; } - SamplerMipmapMode GetMipmapMode() const { return m_mipmapMode; } - Float GetMinLod() const { return m_minLod; } - Float GetMaxLod() const { return m_maxLod; } - Float GetLodBias() const { return m_lodBias; } - SamplerCompareMode GetCompareMode() const { return m_compareMode; } - SamplerCompareFunc GetSamplerCompareFunc() const { return m_compareFunc; } - Uint GetExternalIndex() const { return m_externalIndex; } - - private: - const Uint m_externalIndex = 0; - SamplerWrapMode m_wrapS, m_wrapT, m_wrapR; - SamplerFilterMode m_minFilter, m_magFilter; - SamplerMipmapMode m_mipmapMode; - Float m_minLod, m_maxLod; - Float m_lodBias; - SamplerCompareMode m_compareMode; - SamplerCompareFunc m_compareFunc; - }; - } // namespace GLState - } // namespace MG_State -} // namespace MobileGL \ No newline at end of file diff --git a/MobileGL/MG_State/GLState/TextureState/TextureObject.h b/MobileGL/MG_State/GLState/TextureState/TextureObject.h index 878eef9a..11c1ee11 100644 --- a/MobileGL/MG_State/GLState/TextureState/TextureObject.h +++ b/MobileGL/MG_State/GLState/TextureState/TextureObject.h @@ -1,6 +1,6 @@ #pragma once #include "MG_Util/Types.h" -#include "SamplerObject.h" +#include "../SamplerState/SamplerObject.h" #include #include diff --git a/MobileGL/MG_State/GLState/TextureState/TextureUnit.h b/MobileGL/MG_State/GLState/TextureState/TextureUnit.h index 84136fd5..a4d9a043 100644 --- a/MobileGL/MG_State/GLState/TextureState/TextureUnit.h +++ b/MobileGL/MG_State/GLState/TextureState/TextureUnit.h @@ -1,6 +1,6 @@ #pragma once #include -#include "MG_State/GLState/TextureState/SamplerObject.h" +#include "MG_State/GLState/SamplerState/SamplerObject.h" #include "MG_Util/Types.h" #include "TextureObject.h"