[Feat] (MG_State/SamplerState): Implement SamplerState.

This commit is contained in:
BZLZHH
2025-11-15 23:53:54 +08:00
parent c664815fee
commit 3809a9f52f
10 changed files with 315 additions and 104 deletions
+2
View File
@@ -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
+34
View File
@@ -338,6 +338,40 @@ namespace MobileGL {
return m_framebufferState.ValidateFramebufferObject(index);
}
// Sampler
Vector<Uint> GLContext::GenSamplerNames(Uint number) {
return m_samplerState.GenerateNames(number);
}
SharedPtr<SamplerObject> GLContext::GetSamplerObject(Uint index) {
return m_samplerState.GetSamplerObject(index);
}
SharedPtr<SamplerObject> 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;
+12
View File
@@ -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<Uint> GenSamplerNames(Uint number);
SharedPtr<SamplerObject> GetSamplerObject(Uint index);
SharedPtr<SamplerObject> 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
@@ -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
@@ -0,0 +1,90 @@
#pragma once
#include <Includes.h>
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
@@ -0,0 +1,41 @@
#include "SamplerState.h"
namespace MobileGL {
namespace MG_State {
namespace GLState {
SamplerState::SamplerState() : m_indexGenerator(1024, 1) {}
Vector<Uint> SamplerState::GenerateNames(Uint number) {
Vector<Uint> names(number);
m_indexGenerator.Generate(number, names.data());
return names;
}
SharedPtr<SamplerObject> SamplerState::GetSamplerObject(Uint index) {
auto it = m_samplerObjects.find(index);
return it != m_samplerObjects.end() ? it->second : nullptr;
}
SharedPtr<SamplerObject> SamplerState::CreateSamplerObject(Uint index) {
auto sampler = MakeShared<SamplerObject>(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();
}
}
}
}
@@ -0,0 +1,26 @@
#pragma once
#include "SamplerObject.h"
#include <MG_Util/Miscellany/IndexGenerator.h>
#include <Includes.h>
namespace MobileGL {
namespace MG_State {
namespace GLState {
class SamplerState {
public:
SamplerState();
Vector<Uint> GenerateNames(Uint number);
SharedPtr<SamplerObject> GetSamplerObject(Uint index);
SharedPtr<SamplerObject> CreateSamplerObject(Uint index);
void MarkSamplerObjectForDeletion(Uint index);
Bool ValidateName(Uint index) const;
Bool ValidateSamplerObject(Uint index) const;
private:
UnorderedMap<Uint, SharedPtr<SamplerObject>> m_samplerObjects;
IndexGenerator<Uint> m_indexGenerator;
};
} // namespace GLState
} // namespace MG_State
} // namespace MobileGL
@@ -1,102 +0,0 @@
#pragma once
#include "MG_Util/Types.h"
#include <Includes.h>
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
@@ -1,6 +1,6 @@
#pragma once
#include "MG_Util/Types.h"
#include "SamplerObject.h"
#include "../SamplerState/SamplerObject.h"
#include <Includes.h>
#include <MG_Util/Math/VectorTypes.h>
@@ -1,6 +1,6 @@
#pragma once
#include <Includes.h>
#include "MG_State/GLState/TextureState/SamplerObject.h"
#include "MG_State/GLState/SamplerState/SamplerObject.h"
#include "MG_Util/Types.h"
#include "TextureObject.h"