From be46765c560cf62b45c70979f5bd95c5825307d6 Mon Sep 17 00:00:00 2001 From: BZLZHH Date: Sat, 6 Sep 2025 10:02:01 +0800 Subject: [PATCH] [Feat] (MG_State/RenderState): Implement RenderState. --- CMakeLists.txt | 1 + MobileGL/MG_State/GLState/Core.cpp | 94 +++++++++ MobileGL/MG_State/GLState/Core.h | 34 +++- .../GLState/RenderState/RenderState.cpp | 145 ++++++++++++++ .../GLState/RenderState/RenderState.h | 188 ++++++++++++++++++ 5 files changed, 460 insertions(+), 2 deletions(-) create mode 100644 MobileGL/MG_State/GLState/RenderState/RenderState.cpp create mode 100644 MobileGL/MG_State/GLState/RenderState/RenderState.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ecc1352..8fa42e53 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,6 +80,7 @@ set(SOURCE_FILES MobileGL/MG_State/GLState/ProgramState/ProgramObject.cpp MobileGL/MG_State/GLState/ProgramState/ShaderObject.cpp MobileGL/MG_State/GLState/ProgramState/ProgramState.cpp + MobileGL/MG_State/GLState/RenderState/RenderState.cpp ) add_library(${CMAKE_PROJECT_NAME} SHARED diff --git a/MobileGL/MG_State/GLState/Core.cpp b/MobileGL/MG_State/GLState/Core.cpp index a00fe537..82f14231 100644 --- a/MobileGL/MG_State/GLState/Core.cpp +++ b/MobileGL/MG_State/GLState/Core.cpp @@ -119,6 +119,7 @@ namespace MobileGL { SharedPtr GLContext::GetBoundVertexArray() { return m_vertexArrayState.GetBoundVertexArray(); } + // Texture Vector GLContext::GenTextureNames(Uint number) { return m_textureState.GenerateNames(number); @@ -156,6 +157,7 @@ namespace MobileGL { m_textureState.SetActiveTextureUnit(unit); } + // Program Uint GLContext::CreateProgram() { return m_programState.CreateProgram(); } @@ -195,6 +197,98 @@ namespace MobileGL { SharedPtr GLContext::GetCurrentProgram() { return m_programState.GetCurrentProgram(); } + + // RenderState + void GLContext::SetViewport(IntVec4 viewport) { + m_renderState.SetViewport(viewport); + } + + const IntVec4& GLContext::GetViewport() const { + return m_renderState.GetViewport(); + } + + void GLContext::SetCapability(CapabilityInput cap, Bool enabled) { + m_renderState.SetCapability(cap, enabled); + } + + Bool GLContext::IsCapabilityEnabled(CapabilityInput cap) const { + return m_renderState.IsCapabilityEnabled(cap); + } + + void GLContext::SetBlendFunc(BlendFactor srcRGB, BlendFactor dstRGB, BlendFactor srcAlpha, + BlendFactor dstAlpha) { + m_renderState.SetBlendFunc(srcRGB, dstRGB, srcAlpha, dstAlpha); + } + + void GLContext::GetBlendFunc(BlendFactor& srcRGB, BlendFactor& dstRGB, BlendFactor& srcAlpha, + BlendFactor& dstAlpha) const { + m_renderState.GetBlendFunc(srcRGB, dstRGB, srcAlpha, dstAlpha); + } + + void GLContext::SetDepthFunc(DepthFunc func) { + m_renderState.SetDepthFunc(func); + } + + DepthFunc GLContext::GetDepthFunc() const { + return m_renderState.GetDepthFunc(); + } + + void GLContext::SetDepthMask(Bool flag) { + m_renderState.SetDepthMask(flag); + } + + Bool GLContext::GetDepthMask() const { + return m_renderState.GetDepthMask(); + } + + void GLContext::SetColorMask(BoolVec4 mask) { + m_renderState.SetColorMask(mask); + } + + const BoolVec4 GLContext::GetColorMask() const { + return m_renderState.GetColorMask(); + } + + void GLContext::SetClearColor(FloatVec4 color) { + m_renderState.SetClearColor(color); + } + + const FloatVec4& GLContext::GetClearColor() const { + return m_renderState.GetClearColor(); + } + + void GLContext::SetClearDepth(Float depth) { + m_renderState.SetClearDepth(depth); + } + + Float GLContext::GetClearDepth() const { + return m_renderState.GetClearDepth(); + } + + void GLContext::SetPixelStoreParam(PixelStoreParam param, Int value) { + m_renderState.SetPixelStoreParam(param, value); + } + + Int GLContext::GetPixelStoreParam(PixelStoreParam param) const { + return m_renderState.GetPixelStoreParam(param); + } + + void GLContext::SetCullFaceMode(CullFaceMode mode) { + m_renderState.SetCullFaceMode(mode); + } + + CullFaceMode GLContext::GetCullFaceMode() const { + return m_renderState.GetCullFaceMode(); + } + + void GLContext::SetCullFaceEnabled(Bool enabled) { + m_renderState.SetCullFaceEnabled(enabled); + } + + Bool GLContext::IsCullFaceEnabled() const { + return m_renderState.IsCullFaceEnabled(); + } + } // namespace GLState GLState::GLContext* pGLContext; diff --git a/MobileGL/MG_State/GLState/Core.h b/MobileGL/MG_State/GLState/Core.h index 25ccea97..3edf8163 100644 --- a/MobileGL/MG_State/GLState/Core.h +++ b/MobileGL/MG_State/GLState/Core.h @@ -1,11 +1,12 @@ #pragma once #include -#include "BufferState/BufferState.h" #include "MG_Util/Types.h" -#include "VertexArrayState/VertexArrayState.h" #include "ErrorState/Error.h" +#include "BufferState/BufferState.h" #include "ProgramState/ProgramState.h" #include "TextureState/TextureState.h" +#include "VertexArrayState/VertexArrayState.h" +#include "MG_State/GLState/RenderState/RenderState.h" namespace MobileGL { namespace MG_State { @@ -65,6 +66,32 @@ namespace MobileGL { SharedPtr GetShaderObject(Uint index); void UseProgram(Uint program); SharedPtr GetCurrentProgram(); + + // RenderState + void SetViewport(IntVec4 viewport); // x, y, width, height + const IntVec4& GetViewport() const; // x, y, width, height + void SetCapability(CapabilityInput cap, Bool enabled); + Bool IsCapabilityEnabled(CapabilityInput cap) const; + void SetBlendFunc(BlendFactor srcRGB, BlendFactor dstRGB, BlendFactor srcAlpha, BlendFactor dstAlpha); + void GetBlendFunc(BlendFactor& srcRGB, BlendFactor& dstRGB, BlendFactor& srcAlpha, + BlendFactor& dstAlpha) const; + void SetDepthFunc(DepthFunc func); + DepthFunc GetDepthFunc() const; + void SetDepthMask(Bool flag); + Bool GetDepthMask() const; + void SetColorMask(BoolVec4 mask); + const BoolVec4 GetColorMask() const; + void SetClearColor(FloatVec4 color); + const FloatVec4& GetClearColor() const; + void SetClearDepth(Float depth); + Float GetClearDepth() const; + void SetPixelStoreParam(PixelStoreParam param, Int value); + Int GetPixelStoreParam(PixelStoreParam param) const; + void SetCullFaceMode(CullFaceMode mode); + CullFaceMode GetCullFaceMode() const; + void SetCullFaceEnabled(Bool enabled); + Bool IsCullFaceEnabled() const; + private: // Error ErrorState m_errorState; @@ -80,6 +107,9 @@ namespace MobileGL { // Program ProgramState m_programState; + + // RenderState + RenderState m_renderState; }; } // namespace GLState diff --git a/MobileGL/MG_State/GLState/RenderState/RenderState.cpp b/MobileGL/MG_State/GLState/RenderState/RenderState.cpp new file mode 100644 index 00000000..84087ec5 --- /dev/null +++ b/MobileGL/MG_State/GLState/RenderState/RenderState.cpp @@ -0,0 +1,145 @@ +#include "RenderState.h" + +namespace MobileGL { + namespace MG_State { + namespace GLState { + RenderState::RenderState() { + for (int i = 0; i < static_cast(PixelStoreParam::PixelStoreParamCount); ++i) { + switch (static_cast(i)) { + case PixelStoreParam::PackAlignment: + case PixelStoreParam::UnpackAlignment: + m_pixelStoreParams[i] = 4; + break; + + default: + m_pixelStoreParams[i] = 0; + break; + } + } + } + + // -------------------- Rasterization -------------------- + void RenderState::SetViewport(IntVec4 viewport) { + m_viewport = viewport; + } + + const IntVec4& RenderState::GetViewport() const { + return m_viewport; + } + + // -------------------- Capabilities -------------------- + void RenderState::SetCapability(CapabilityInput cap, Bool enabled) { + switch (cap) { + case CapabilityInput::Blend: + m_blendEnabled = enabled; + break; + case CapabilityInput::DepthTest: + m_depthTestEnabled = enabled; + break; + case CapabilityInput::CullFace: + m_cullFaceEnabled = enabled; + break; + default: // not supported currently + break; + } + } + + Bool RenderState::IsCapabilityEnabled(CapabilityInput cap) const { + switch (cap) { + case CapabilityInput::Blend: + return m_blendEnabled; + case CapabilityInput::DepthTest: + return m_depthTestEnabled; + default: + return false; + } + } + + // -------------------- Blending -------------------- + void RenderState::SetBlendFunc(BlendFactor srcRGB, BlendFactor dstRGB, BlendFactor srcAlpha, + BlendFactor dstAlpha) { + m_srcFactorRGB = srcRGB; + m_dstFactorRGB = dstRGB; + m_srcFactorAlpha = srcAlpha; + m_dstFactorAlpha = dstAlpha; + } + + void RenderState::GetBlendFunc(BlendFactor& srcRGB, BlendFactor& dstRGB, BlendFactor& srcAlpha, + BlendFactor& dstAlpha) const { + srcRGB = m_srcFactorRGB; + dstRGB = m_dstFactorRGB; + srcAlpha = m_srcFactorAlpha; + dstAlpha = m_dstFactorAlpha; + } + + // -------------------- Depth -------------------- + void RenderState::SetDepthFunc(DepthFunc func) { + m_depthFunc = func; + } + + DepthFunc RenderState::GetDepthFunc() const { + return m_depthFunc; + } + + void RenderState::SetDepthMask(Bool flag) { + m_depthMask = flag; + } + + Bool RenderState::GetDepthMask() const { + return m_depthMask; + } + + // -------------------- Color Mask -------------------- + void RenderState::SetColorMask(BoolVec4 mask) { + m_colorMask = mask; + } + + const BoolVec4 RenderState::GetColorMask() const { + return m_colorMask; + } + + // -------------------- Clear State -------------------- + void RenderState::SetClearColor(FloatVec4 color) { + m_clearColor = color; + } + + const FloatVec4& RenderState::GetClearColor() const { + return m_clearColor; + } + + void RenderState::SetClearDepth(Float depth) { + m_clearDepth = depth; + } + + Float RenderState::GetClearDepth() const { + return m_clearDepth; + } + + // -------------------- Pixel Store -------------------- + void RenderState::SetPixelStoreParam(PixelStoreParam param, Int value) { + m_pixelStoreParams[static_cast(param)] = value; + } + + Int RenderState::GetPixelStoreParam(PixelStoreParam param) const { + return m_pixelStoreParams[static_cast(param)]; + } + + // -------------------- Cull Face -------------------- + void RenderState::SetCullFaceMode(CullFaceMode mode) { + m_cullFaceMode = mode; + } + + CullFaceMode RenderState::GetCullFaceMode() const { + return m_cullFaceMode; + } + + void RenderState::SetCullFaceEnabled(Bool enabled) { + m_cullFaceEnabled = enabled; + } + + Bool RenderState::IsCullFaceEnabled() const { + return m_cullFaceEnabled; + } + } // namespace GLState + } // namespace MG_State +} // namespace MobileGL diff --git a/MobileGL/MG_State/GLState/RenderState/RenderState.h b/MobileGL/MG_State/GLState/RenderState/RenderState.h new file mode 100644 index 00000000..d250067d --- /dev/null +++ b/MobileGL/MG_State/GLState/RenderState/RenderState.h @@ -0,0 +1,188 @@ +#pragma once +#include "MG_Util/Math/VectorTypes.h" +#include + +namespace MobileGL { + namespace MG_State { + namespace GLState { + enum class BlendFactor { + Zero, + One, + SrcColor, + OneMinusSrcColor, + DstColor, + OneMinusDstColor, + SrcAlpha, + OneMinusSrcAlpha, + DstAlpha, + OneMinusDstAlpha, + ConstantColor, + OneMinusConstantColor, + ConstantAlpha, + OneMinusConstantAlpha, + BlendFactorCount, + Unknown = -1 + }; + + enum class DepthFunc { + Never, + Less, + Equal, + LessEqual, + Greater, + NotEqual, + GreaterEqual, + Always, + DepthFuncCount, + Unknown = -1 + }; + + enum class PixelStoreParam { + // Pack Parameters + PackAlignment, + PackRowLength, + PackImageHeight, + PackSkipRows, + PackSkipPixels, + PackSkipImages, + PackSwapBytes, + PackLsbFirst, + + // Unpack Parameters + UnpackAlignment, + UnpackRowLength, + UnpackImageHeight, + UnpackSkipRows, + UnpackSkipPixels, + UnpackSkipImages, + UnpackSwapBytes, + UnpackLsbFirst, + + PixelStoreParamCount, + Unknown = -1 + }; + + enum class CullFaceMode { + Front, + Back, + FrontAndBack, + CullFaceModeCount, + Unknown = -1 + }; + + enum class CapabilityInput { + Blend, + ClipDistance0, + ClipDistance1, + ClipDistance2, + ClipDistance3, + ClipDistance4, + ClipDistance5, + ClipDistance6, + ClipDistance7, + ColorLogicOp, + CullFace, + DebugOutput, + DebugOutputSynchronous, + DepthClamp, + DepthTest, + Dither, + FramebufferSrgb, + LineSmooth, + Multisample, + PolygonOffsetFill, + PolygonOffsetLine, + PolygonOffsetPoint, + PolygonSmooth, + PrimitiveRestart, + PrimitiveRestartFixedIndex, + RasterizerDiscard, + SampleAlphaToCoverage, + SampleAlphaToOne, + SampleCoverage, + SampleShading, + SampleMask, + ScissorTest, + StencilTest, + TextureCubeMapSeamless, + ProgramPointSize, + CapabilityInputCount, + Unknown = -1 + }; + + class RenderState { + public: + RenderState(); + + // Rasterization + void SetViewport(IntVec4 viewport); // x, y, width, height + const IntVec4& GetViewport() const; // x, y, width, height + + // Capabilities + void SetCapability(CapabilityInput cap, Bool enabled); + Bool IsCapabilityEnabled(CapabilityInput cap) const; + + // Blending + void SetBlendFunc(BlendFactor srcRGB, BlendFactor dstRGB, BlendFactor srcAlpha, BlendFactor dstAlpha); + void GetBlendFunc(BlendFactor& srcRGB, BlendFactor& dstRGB, BlendFactor& srcAlpha, + BlendFactor& dstAlpha) const; + + // Depth + void SetDepthFunc(DepthFunc func); + DepthFunc GetDepthFunc() const; + void SetDepthMask(Bool flag); + Bool GetDepthMask() const; + + // Color Mask + void SetColorMask(BoolVec4 mask); + const BoolVec4 GetColorMask() const; + + // Clear State + void SetClearColor(FloatVec4 color); + const FloatVec4& GetClearColor() const; + void SetClearDepth(Float depth); + Float GetClearDepth() const; + + // Pixel Store + void SetPixelStoreParam(PixelStoreParam param, Int value); + Int GetPixelStoreParam(PixelStoreParam param) const; + + // Cull Face + void SetCullFaceMode(CullFaceMode mode); + CullFaceMode GetCullFaceMode() const; + void SetCullFaceEnabled(Bool enabled); + Bool IsCullFaceEnabled() const; + + private: + // Rasterization + IntVec4 m_viewport = IntVec4(0, 0, 0, 0); // x, y, width, height + + // Blending + Bool m_blendEnabled = false; + BlendFactor m_srcFactorRGB = BlendFactor::One; + BlendFactor m_dstFactorRGB = BlendFactor::Zero; + BlendFactor m_srcFactorAlpha = BlendFactor::One; + BlendFactor m_dstFactorAlpha = BlendFactor::Zero; + + // Depth + Bool m_depthTestEnabled = false; + DepthFunc m_depthFunc = DepthFunc::Less; + Bool m_depthMask = true; + + // Color Mask + BoolVec4 m_colorMask = BoolVec4(true, true, true, true); + + // Clear State + FloatVec4 m_clearColor = FloatVec4(0.0f, 0.0f, 0.0f, 1.0f); + Float m_clearDepth = 1.0f; + + // Pixel Store + Array(PixelStoreParam::PixelStoreParamCount)> m_pixelStoreParams; + + // Cull Face + Bool m_cullFaceEnabled = false; + CullFaceMode m_cullFaceMode = CullFaceMode::Back; + }; + } // namespace GLState + } // namespace MG_State +} // namespace MobileGL \ No newline at end of file