From ea098c04094f2adea82f2f7de2380d0d90c5e147 Mon Sep 17 00:00:00 2001 From: BZLZHH Date: Tue, 3 Feb 2026 17:50:46 +0800 Subject: [PATCH] [Feat|Refactor] (MG_State/RenderState): Versioning for RenderStateParameters. --- MobileGL/MG_Impl/GLImpl/Getter/GL_Getter.cpp | 4 +- MobileGL/MG_State/GLState/Core.cpp | 8 + MobileGL/MG_State/GLState/Core.h | 2 + .../GLState/RenderState/RenderState.cpp | 197 +++++++++--------- .../GLState/RenderState/RenderState.h | 16 +- .../GLToMG/RenderStateEnumConverter.cpp | 4 +- .../MGToGL/RenderStateEnumConverter.cpp | 4 +- .../MGToStr/RenderStateEnumConverter.cpp | 8 +- 8 files changed, 125 insertions(+), 118 deletions(-) diff --git a/MobileGL/MG_Impl/GLImpl/Getter/GL_Getter.cpp b/MobileGL/MG_Impl/GLImpl/Getter/GL_Getter.cpp index 6c15d1ad..d9bc6ebb 100644 --- a/MobileGL/MG_Impl/GLImpl/Getter/GL_Getter.cpp +++ b/MobileGL/MG_Impl/GLImpl/Getter/GL_Getter.cpp @@ -567,7 +567,7 @@ namespace MobileGL { *params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::PackImageHeight); break; case GL_PACK_LSB_FIRST: - *params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::PackLsbFirst); + *params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::PackLSBFirst); break; case GL_PACK_ROW_LENGTH: *params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::PackRowLength); @@ -839,7 +839,7 @@ namespace MobileGL { *params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::UnpackImageHeight); break; case GL_UNPACK_LSB_FIRST: - *params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::UnpackLsbFirst); + *params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::UnpackLSBFirst); break; case GL_UNPACK_ROW_LENGTH: *params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::UnpackRowLength); diff --git a/MobileGL/MG_State/GLState/Core.cpp b/MobileGL/MG_State/GLState/Core.cpp index 206edfc5..87e313f0 100644 --- a/MobileGL/MG_State/GLState/Core.cpp +++ b/MobileGL/MG_State/GLState/Core.cpp @@ -220,6 +220,14 @@ namespace MobileGL { } // RenderState + Uint GLContext::GetRenderStateParametersVersion() const { + return m_renderState.GetVersion(); + } + + const RenderStateParameters& GLContext::GetRenderStateParameters() const { + return m_renderState.GetAllParameters(); + } + void GLContext::SetViewport(IntVec4 viewport) { m_renderState.SetViewport(viewport); } diff --git a/MobileGL/MG_State/GLState/Core.h b/MobileGL/MG_State/GLState/Core.h index a0f258b2..9e7f4001 100644 --- a/MobileGL/MG_State/GLState/Core.h +++ b/MobileGL/MG_State/GLState/Core.h @@ -86,6 +86,8 @@ namespace MobileGL { SharedPtr GetCurrentProgram(); // RenderState + Uint GetRenderStateParametersVersion() const; + const RenderStateParameters& GetRenderStateParameters() const; void SetViewport(IntVec4 viewport); // x, y, width, height const IntVec4& GetViewport() const; // x, y, width, height void SetCapability(CapabilityInput cap, Bool enabled); diff --git a/MobileGL/MG_State/GLState/RenderState/RenderState.cpp b/MobileGL/MG_State/GLState/RenderState/RenderState.cpp index 970ffb5e..d913b6eb 100644 --- a/MobileGL/MG_State/GLState/RenderState/RenderState.cpp +++ b/MobileGL/MG_State/GLState/RenderState/RenderState.cpp @@ -13,9 +13,20 @@ namespace MobileGL { namespace GLState { RenderState::RenderState() {} + Uint RenderState::GetVersion() const { + return m_version; + } + + const RenderStateParameters& RenderState::GetAllParameters() const { + return m_parameters; + } + // -------------------- Rasterization -------------------- void RenderState::SetViewport(IntVec4 viewport) { + if (m_parameters.Viewport == viewport) return; + m_parameters.Viewport = viewport; + ++m_version; } const IntVec4& RenderState::GetViewport() const { @@ -24,34 +35,33 @@ namespace MobileGL { // -------------------- Capabilities -------------------- void RenderState::SetCapability(CapabilityInput cap, Bool enabled) { +#define SET_CAPABILITY(capability, flag) \ + case CapabilityInput::capability: \ + if (m_parameters.capability##Enabled == flag) break; \ + m_parameters.capability##Enabled = flag; \ + ++m_version; \ + break; + switch (cap) { - case CapabilityInput::Blend: - m_parameters.BlendEnabled = enabled; - break; - case CapabilityInput::DepthTest: - m_parameters.DepthTestEnabled = enabled; - break; - case CapabilityInput::CullFace: - m_parameters.CullFaceEnabled = enabled; - break; - case CapabilityInput::ScissorTest: - m_parameters.ScissorTestEnabled = enabled; - break; + SET_CAPABILITY(Blend, enabled); + SET_CAPABILITY(DepthTest, enabled); + SET_CAPABILITY(CullFace, enabled); + SET_CAPABILITY(ScissorTest, enabled); default: // not supported currently break; } +#undef SET_CAPABILITY } Bool RenderState::IsCapabilityEnabled(CapabilityInput cap) const { +#define RETURN_CAPABILITY(capability) \ + case CapabilityInput::capability: \ + return m_parameters.capability##Enabled; switch (cap) { - case CapabilityInput::Blend: - return m_parameters.BlendEnabled; - case CapabilityInput::DepthTest: - return m_parameters.DepthTestEnabled; - case CapabilityInput::CullFace: - return m_parameters.CullFaceEnabled; - case CapabilityInput::ScissorTest: - return m_parameters.ScissorTestEnabled; + RETURN_CAPABILITY(Blend); + RETURN_CAPABILITY(DepthTest); + RETURN_CAPABILITY(CullFace); + RETURN_CAPABILITY(ScissorTest); default: return false; } @@ -60,10 +70,15 @@ namespace MobileGL { // -------------------- Blending -------------------- void RenderState::SetBlendFunc(BlendFactor srcRGB, BlendFactor dstRGB, BlendFactor srcAlpha, BlendFactor dstAlpha) { + if (m_parameters.SrcFactorRGB == srcRGB && m_parameters.DstFactorRGB == dstRGB && + m_parameters.SrcFactorAlpha == srcAlpha && m_parameters.DstFactorAlpha == dstAlpha) + return; + m_parameters.SrcFactorRGB = srcRGB; m_parameters.DstFactorRGB = dstRGB; m_parameters.SrcFactorAlpha = srcAlpha; m_parameters.DstFactorAlpha = dstAlpha; + ++m_version; } void RenderState::GetBlendFunc(BlendFactor& srcRGB, BlendFactor& dstRGB, BlendFactor& srcAlpha, @@ -76,7 +91,10 @@ namespace MobileGL { // -------------------- Depth -------------------- void RenderState::SetDepthFunc(DepthTestFunc func) { + if (m_parameters.DepthFunc == func) return; + m_parameters.DepthFunc = func; + ++m_version; } DepthTestFunc RenderState::GetDepthFunc() const { @@ -84,7 +102,10 @@ namespace MobileGL { } void RenderState::SetDepthMask(Bool flag) { + if (m_parameters.DepthMask == flag) return; + m_parameters.DepthMask = flag; + ++m_version; } Bool RenderState::GetDepthMask() const { @@ -102,7 +123,10 @@ namespace MobileGL { // -------------------- Clear State -------------------- void RenderState::SetClearColor(FloatVec4 color) { + if (m_parameters.ClearColor == color) return; + m_parameters.ClearColor = color; + ++m_version; } const FloatVec4& RenderState::GetClearColor() const { @@ -110,7 +134,10 @@ namespace MobileGL { } void RenderState::SetClearDepth(Float depth) { + if (m_parameters.ClearDepth == depth) return; + m_parameters.ClearDepth = depth; + ++m_version; } Float RenderState::GetClearDepth() const { @@ -119,56 +146,29 @@ namespace MobileGL { // -------------------- Pixel Store -------------------- void RenderState::SetPixelStoreParam(PixelStoreParam param, Int value) { +#define SET_PIXEL_STORE_PARAM(paramNameHead, paramNameTail, val) \ + case PixelStoreParam::paramNameHead##paramNameTail: \ + if (m_pixelStore##paramNameHead##Parameters.paramNameTail == val) break; \ + m_pixelStore##paramNameHead##Parameters.paramNameTail = val; \ + break; + switch (param) { - case PixelStoreParam::PackAlignment: - m_parameters.PackParameters.Alignment = value; - break; - case PixelStoreParam::PackRowLength: - m_parameters.PackParameters.RowLength = value; - break; - case PixelStoreParam::PackImageHeight: - m_parameters.PackParameters.ImageHeight = value; - break; - case PixelStoreParam::PackSkipPixels: - m_parameters.PackParameters.SkipPixels = value; - break; - case PixelStoreParam::PackSkipRows: - m_parameters.PackParameters.SkipRows = value; - break; - case PixelStoreParam::PackSkipImages: - m_parameters.PackParameters.SkipImages = value; - break; - case PixelStoreParam::PackSwapBytes: - m_parameters.PackParameters.SwapBytes = value != 0; - break; - case PixelStoreParam::PackLsbFirst: - m_parameters.PackParameters.LSBFirst = value != 0; - break; - case PixelStoreParam::UnpackAlignment: - m_parameters.UnpackParameters.Alignment = value; - break; - case PixelStoreParam::UnpackRowLength: - m_parameters.UnpackParameters.RowLength = value; - break; - case PixelStoreParam::UnpackImageHeight: - m_parameters.UnpackParameters.ImageHeight = value; - break; - case PixelStoreParam::UnpackSkipPixels: - m_parameters.UnpackParameters.SkipPixels = value; - break; - case PixelStoreParam::UnpackSkipRows: - m_parameters.UnpackParameters.SkipRows = value; - break; - case PixelStoreParam::UnpackSkipImages: - m_parameters.UnpackParameters.SkipImages = value; - break; - case PixelStoreParam::UnpackSwapBytes: - m_parameters.UnpackParameters.SwapBytes = value != 0; - MGLOG_D("%s: SwapBytes = %s", __func__, value ? "true" : "false"); - break; - case PixelStoreParam::UnpackLsbFirst: - m_parameters.UnpackParameters.LSBFirst = value != 0; - break; + SET_PIXEL_STORE_PARAM(Pack, Alignment, value); + SET_PIXEL_STORE_PARAM(Pack, RowLength, value); + SET_PIXEL_STORE_PARAM(Pack, ImageHeight, value); + SET_PIXEL_STORE_PARAM(Pack, SkipPixels, value); + SET_PIXEL_STORE_PARAM(Pack, SkipRows, value); + SET_PIXEL_STORE_PARAM(Pack, SkipImages, value); + SET_PIXEL_STORE_PARAM(Pack, SwapBytes, value != 0); + SET_PIXEL_STORE_PARAM(Pack, LSBFirst, value != 0); + SET_PIXEL_STORE_PARAM(Unpack, Alignment, value); + SET_PIXEL_STORE_PARAM(Unpack, RowLength, value); + SET_PIXEL_STORE_PARAM(Unpack, ImageHeight, value); + SET_PIXEL_STORE_PARAM(Unpack, SkipPixels, value); + SET_PIXEL_STORE_PARAM(Unpack, SkipRows, value); + SET_PIXEL_STORE_PARAM(Unpack, SkipImages, value); + SET_PIXEL_STORE_PARAM(Unpack, SwapBytes, value != 0); + SET_PIXEL_STORE_PARAM(Unpack, LSBFirst, value != 0); default: MOBILEGL_ASSERT(false, "Invalid PixelStoreParam enum: %d", static_cast(param)); return; @@ -176,39 +176,26 @@ namespace MobileGL { } Int RenderState::GetPixelStoreParam(PixelStoreParam param) const { +#define RETURN_PIXEL_STORE_PARAM(paramNameHead, paramNameTail) \ + case PixelStoreParam::paramNameHead##paramNameTail: \ + return m_pixelStore##paramNameHead##Parameters.paramNameTail; switch (param) { - case PixelStoreParam::PackAlignment: - return m_parameters.PackParameters.Alignment; - case PixelStoreParam::PackRowLength: - return m_parameters.PackParameters.RowLength; - case PixelStoreParam::PackImageHeight: - return m_parameters.PackParameters.ImageHeight; - case PixelStoreParam::PackSkipPixels: - return m_parameters.PackParameters.SkipPixels; - case PixelStoreParam::PackSkipRows: - return m_parameters.PackParameters.SkipRows; - case PixelStoreParam::PackSkipImages: - return m_parameters.PackParameters.SkipImages; - case PixelStoreParam::PackSwapBytes: - return m_parameters.PackParameters.SwapBytes ? 1 : 0; - case PixelStoreParam::PackLsbFirst: - return m_parameters.PackParameters.LSBFirst ? 1 : 0; - case PixelStoreParam::UnpackAlignment: - return m_parameters.UnpackParameters.Alignment; - case PixelStoreParam::UnpackRowLength: - return m_parameters.UnpackParameters.RowLength; - case PixelStoreParam::UnpackImageHeight: - return m_parameters.UnpackParameters.ImageHeight; - case PixelStoreParam::UnpackSkipPixels: - return m_parameters.UnpackParameters.SkipPixels; - case PixelStoreParam::UnpackSkipRows: - return m_parameters.UnpackParameters.SkipRows; - case PixelStoreParam::UnpackSkipImages: - return m_parameters.UnpackParameters.SkipImages; - case PixelStoreParam::UnpackSwapBytes: - return m_parameters.UnpackParameters.SwapBytes ? 1 : 0; - case PixelStoreParam::UnpackLsbFirst: - return m_parameters.UnpackParameters.LSBFirst ? 1 : 0; + RETURN_PIXEL_STORE_PARAM(Pack, Alignment); + RETURN_PIXEL_STORE_PARAM(Pack, RowLength); + RETURN_PIXEL_STORE_PARAM(Pack, ImageHeight); + RETURN_PIXEL_STORE_PARAM(Pack, SkipPixels); + RETURN_PIXEL_STORE_PARAM(Pack, SkipRows); + RETURN_PIXEL_STORE_PARAM(Pack, SkipImages); + RETURN_PIXEL_STORE_PARAM(Pack, SwapBytes); + RETURN_PIXEL_STORE_PARAM(Pack, LSBFirst); + RETURN_PIXEL_STORE_PARAM(Unpack, Alignment); + RETURN_PIXEL_STORE_PARAM(Unpack, RowLength); + RETURN_PIXEL_STORE_PARAM(Unpack, ImageHeight); + RETURN_PIXEL_STORE_PARAM(Unpack, SkipPixels); + RETURN_PIXEL_STORE_PARAM(Unpack, SkipRows); + RETURN_PIXEL_STORE_PARAM(Unpack, SkipImages); + RETURN_PIXEL_STORE_PARAM(Unpack, SwapBytes); + RETURN_PIXEL_STORE_PARAM(Unpack, LSBFirst); default: MOBILEGL_ASSERT(false, "Invalid PixelStoreParam enum: %d", static_cast(param)); return 0; @@ -216,12 +203,15 @@ namespace MobileGL { } PixelStoreParameters RenderState::GetPixelStoreParameters(Bool isUnpack) const { - return isUnpack ? m_parameters.UnpackParameters : m_parameters.PackParameters; + return isUnpack ? m_pixelStoreUnpackParameters : m_pixelStorePackParameters; } // -------------------- Cull Face -------------------- void RenderState::SetCullFaceMode(CullFaceMode mode) { + if (m_parameters.CullFaceModeSetting == mode) return; + m_parameters.CullFaceModeSetting = mode; + ++m_version; } CullFaceMode RenderState::GetCullFaceMode() const { @@ -230,7 +220,10 @@ namespace MobileGL { // --------------------- Scissor --------------------- void RenderState::SetScissorBox(IntVec4 box) { + if (m_parameters.ScissorBox == box) return; + m_parameters.ScissorBox = box; + ++m_version; } const IntVec4& RenderState::GetScissorBox() const { diff --git a/MobileGL/MG_State/GLState/RenderState/RenderState.h b/MobileGL/MG_State/GLState/RenderState/RenderState.h index 952ea8e0..c97f92c9 100644 --- a/MobileGL/MG_State/GLState/RenderState/RenderState.h +++ b/MobileGL/MG_State/GLState/RenderState/RenderState.h @@ -52,7 +52,7 @@ namespace MobileGL { PackSkipPixels, PackSkipImages, PackSwapBytes, - PackLsbFirst, + PackLSBFirst, // Unpack Parameters UnpackAlignment, @@ -62,7 +62,7 @@ namespace MobileGL { UnpackSkipPixels, UnpackSkipImages, UnpackSwapBytes, - UnpackLsbFirst, + UnpackLSBFirst, PixelStoreParamCount, Unknown = -1 @@ -150,10 +150,6 @@ namespace MobileGL { FloatVec4 ClearColor = FloatVec4(0.0f, 0.0f, 0.0f, 1.0f); Float ClearDepth = 1.0f; - // Pixel Store - PixelStoreParameters PackParameters; - PixelStoreParameters UnpackParameters; - // Cull Face Bool CullFaceEnabled = false; CullFaceMode CullFaceModeSetting = CullFaceMode::Back; @@ -169,6 +165,9 @@ namespace MobileGL { public: RenderState(); + Uint GetVersion() const; + const RenderStateParameters& GetAllParameters() const; + // Rasterization void SetViewport(IntVec4 viewport); // x, y, width, height const IntVec4& GetViewport() const; // x, y, width, height @@ -212,7 +211,12 @@ namespace MobileGL { const IntVec4& GetScissorBox() const; // x, y, width, height private: + Uint16 m_version = 0; RenderStateParameters m_parameters; + + // Pixel Store + PixelStoreParameters m_pixelStorePackParameters; + PixelStoreParameters m_pixelStoreUnpackParameters; }; } // namespace GLState } // namespace MG_State diff --git a/MobileGL/MG_Util/Converters/GLToMG/RenderStateEnumConverter.cpp b/MobileGL/MG_Util/Converters/GLToMG/RenderStateEnumConverter.cpp index 7eac9a58..3ebace71 100644 --- a/MobileGL/MG_Util/Converters/GLToMG/RenderStateEnumConverter.cpp +++ b/MobileGL/MG_Util/Converters/GLToMG/RenderStateEnumConverter.cpp @@ -85,7 +85,7 @@ namespace MobileGL { case GL_PACK_SWAP_BYTES: return PixelStoreParam::PackSwapBytes; case GL_PACK_LSB_FIRST: - return PixelStoreParam::PackLsbFirst; + return PixelStoreParam::PackLSBFirst; case GL_UNPACK_ALIGNMENT: return PixelStoreParam::UnpackAlignment; case GL_UNPACK_ROW_LENGTH: @@ -101,7 +101,7 @@ namespace MobileGL { case GL_UNPACK_SWAP_BYTES: return PixelStoreParam::UnpackSwapBytes; case GL_UNPACK_LSB_FIRST: - return PixelStoreParam::UnpackLsbFirst; + return PixelStoreParam::UnpackLSBFirst; default: return PixelStoreParam::Unknown; } diff --git a/MobileGL/MG_Util/Converters/MGToGL/RenderStateEnumConverter.cpp b/MobileGL/MG_Util/Converters/MGToGL/RenderStateEnumConverter.cpp index ac37b256..3cd42eeb 100644 --- a/MobileGL/MG_Util/Converters/MGToGL/RenderStateEnumConverter.cpp +++ b/MobileGL/MG_Util/Converters/MGToGL/RenderStateEnumConverter.cpp @@ -85,7 +85,7 @@ namespace MobileGL { return GL_PACK_SKIP_IMAGES; case PixelStoreParam::PackSwapBytes: return GL_PACK_SWAP_BYTES; - case PixelStoreParam::PackLsbFirst: + case PixelStoreParam::PackLSBFirst: return GL_PACK_LSB_FIRST; case PixelStoreParam::UnpackAlignment: return GL_UNPACK_ALIGNMENT; @@ -101,7 +101,7 @@ namespace MobileGL { return GL_UNPACK_SKIP_IMAGES; case PixelStoreParam::UnpackSwapBytes: return GL_UNPACK_SWAP_BYTES; - case PixelStoreParam::UnpackLsbFirst: + case PixelStoreParam::UnpackLSBFirst: return GL_UNPACK_LSB_FIRST; default: return GL_UNKNOWN_MGL; diff --git a/MobileGL/MG_Util/Converters/MGToStr/RenderStateEnumConverter.cpp b/MobileGL/MG_Util/Converters/MGToStr/RenderStateEnumConverter.cpp index d057072c..f985aa8f 100644 --- a/MobileGL/MG_Util/Converters/MGToStr/RenderStateEnumConverter.cpp +++ b/MobileGL/MG_Util/Converters/MGToStr/RenderStateEnumConverter.cpp @@ -84,8 +84,8 @@ namespace MobileGL { return "PackSkipImages"; case PixelStoreParam::PackSwapBytes: return "PackSwapBytes"; - case PixelStoreParam::PackLsbFirst: - return "PackLsbFirst"; + case PixelStoreParam::PackLSBFirst: + return "PackLSBFirst"; case PixelStoreParam::UnpackAlignment: return "UnpackAlignment"; case PixelStoreParam::UnpackRowLength: @@ -100,8 +100,8 @@ namespace MobileGL { return "UnpackSkipImages"; case PixelStoreParam::UnpackSwapBytes: return "UnpackSwapBytes"; - case PixelStoreParam::UnpackLsbFirst: - return "UnpackLsbFirst"; + case PixelStoreParam::UnpackLSBFirst: + return "UnpackLSBFirst"; default: return "Unknown"; }