[Feat|Refactor] (MG_State/RenderState): Versioning for RenderStateParameters.

This commit is contained in:
BZLZHH
2026-02-03 17:50:46 +08:00
parent fc9653f460
commit ea098c0409
8 changed files with 125 additions and 118 deletions
+2 -2
View File
@@ -567,7 +567,7 @@ namespace MobileGL {
*params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::PackImageHeight); *params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::PackImageHeight);
break; break;
case GL_PACK_LSB_FIRST: case GL_PACK_LSB_FIRST:
*params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::PackLsbFirst); *params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::PackLSBFirst);
break; break;
case GL_PACK_ROW_LENGTH: case GL_PACK_ROW_LENGTH:
*params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::PackRowLength); *params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::PackRowLength);
@@ -839,7 +839,7 @@ namespace MobileGL {
*params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::UnpackImageHeight); *params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::UnpackImageHeight);
break; break;
case GL_UNPACK_LSB_FIRST: case GL_UNPACK_LSB_FIRST:
*params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::UnpackLsbFirst); *params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::UnpackLSBFirst);
break; break;
case GL_UNPACK_ROW_LENGTH: case GL_UNPACK_ROW_LENGTH:
*params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::UnpackRowLength); *params = MG_State::pGLContext->GetPixelStoreParam(PixelStoreParam::UnpackRowLength);
+8
View File
@@ -220,6 +220,14 @@ namespace MobileGL {
} }
// RenderState // RenderState
Uint GLContext::GetRenderStateParametersVersion() const {
return m_renderState.GetVersion();
}
const RenderStateParameters& GLContext::GetRenderStateParameters() const {
return m_renderState.GetAllParameters();
}
void GLContext::SetViewport(IntVec4 viewport) { void GLContext::SetViewport(IntVec4 viewport) {
m_renderState.SetViewport(viewport); m_renderState.SetViewport(viewport);
} }
+2
View File
@@ -86,6 +86,8 @@ namespace MobileGL {
SharedPtr<ProgramObject> GetCurrentProgram(); SharedPtr<ProgramObject> GetCurrentProgram();
// RenderState // RenderState
Uint GetRenderStateParametersVersion() const;
const RenderStateParameters& GetRenderStateParameters() const;
void SetViewport(IntVec4 viewport); // x, y, width, height void SetViewport(IntVec4 viewport); // x, y, width, height
const IntVec4& GetViewport() const; // x, y, width, height const IntVec4& GetViewport() const; // x, y, width, height
void SetCapability(CapabilityInput cap, Bool enabled); void SetCapability(CapabilityInput cap, Bool enabled);
@@ -13,9 +13,20 @@ namespace MobileGL {
namespace GLState { namespace GLState {
RenderState::RenderState() {} RenderState::RenderState() {}
Uint RenderState::GetVersion() const {
return m_version;
}
const RenderStateParameters& RenderState::GetAllParameters() const {
return m_parameters;
}
// -------------------- Rasterization -------------------- // -------------------- Rasterization --------------------
void RenderState::SetViewport(IntVec4 viewport) { void RenderState::SetViewport(IntVec4 viewport) {
if (m_parameters.Viewport == viewport) return;
m_parameters.Viewport = viewport; m_parameters.Viewport = viewport;
++m_version;
} }
const IntVec4& RenderState::GetViewport() const { const IntVec4& RenderState::GetViewport() const {
@@ -24,34 +35,33 @@ namespace MobileGL {
// -------------------- Capabilities -------------------- // -------------------- Capabilities --------------------
void RenderState::SetCapability(CapabilityInput cap, Bool enabled) { 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) { switch (cap) {
case CapabilityInput::Blend: SET_CAPABILITY(Blend, enabled);
m_parameters.BlendEnabled = enabled; SET_CAPABILITY(DepthTest, enabled);
break; SET_CAPABILITY(CullFace, enabled);
case CapabilityInput::DepthTest: SET_CAPABILITY(ScissorTest, enabled);
m_parameters.DepthTestEnabled = enabled;
break;
case CapabilityInput::CullFace:
m_parameters.CullFaceEnabled = enabled;
break;
case CapabilityInput::ScissorTest:
m_parameters.ScissorTestEnabled = enabled;
break;
default: // not supported currently default: // not supported currently
break; break;
} }
#undef SET_CAPABILITY
} }
Bool RenderState::IsCapabilityEnabled(CapabilityInput cap) const { Bool RenderState::IsCapabilityEnabled(CapabilityInput cap) const {
#define RETURN_CAPABILITY(capability) \
case CapabilityInput::capability: \
return m_parameters.capability##Enabled;
switch (cap) { switch (cap) {
case CapabilityInput::Blend: RETURN_CAPABILITY(Blend);
return m_parameters.BlendEnabled; RETURN_CAPABILITY(DepthTest);
case CapabilityInput::DepthTest: RETURN_CAPABILITY(CullFace);
return m_parameters.DepthTestEnabled; RETURN_CAPABILITY(ScissorTest);
case CapabilityInput::CullFace:
return m_parameters.CullFaceEnabled;
case CapabilityInput::ScissorTest:
return m_parameters.ScissorTestEnabled;
default: default:
return false; return false;
} }
@@ -60,10 +70,15 @@ namespace MobileGL {
// -------------------- Blending -------------------- // -------------------- Blending --------------------
void RenderState::SetBlendFunc(BlendFactor srcRGB, BlendFactor dstRGB, BlendFactor srcAlpha, void RenderState::SetBlendFunc(BlendFactor srcRGB, BlendFactor dstRGB, BlendFactor srcAlpha,
BlendFactor dstAlpha) { 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.SrcFactorRGB = srcRGB;
m_parameters.DstFactorRGB = dstRGB; m_parameters.DstFactorRGB = dstRGB;
m_parameters.SrcFactorAlpha = srcAlpha; m_parameters.SrcFactorAlpha = srcAlpha;
m_parameters.DstFactorAlpha = dstAlpha; m_parameters.DstFactorAlpha = dstAlpha;
++m_version;
} }
void RenderState::GetBlendFunc(BlendFactor& srcRGB, BlendFactor& dstRGB, BlendFactor& srcAlpha, void RenderState::GetBlendFunc(BlendFactor& srcRGB, BlendFactor& dstRGB, BlendFactor& srcAlpha,
@@ -76,7 +91,10 @@ namespace MobileGL {
// -------------------- Depth -------------------- // -------------------- Depth --------------------
void RenderState::SetDepthFunc(DepthTestFunc func) { void RenderState::SetDepthFunc(DepthTestFunc func) {
if (m_parameters.DepthFunc == func) return;
m_parameters.DepthFunc = func; m_parameters.DepthFunc = func;
++m_version;
} }
DepthTestFunc RenderState::GetDepthFunc() const { DepthTestFunc RenderState::GetDepthFunc() const {
@@ -84,7 +102,10 @@ namespace MobileGL {
} }
void RenderState::SetDepthMask(Bool flag) { void RenderState::SetDepthMask(Bool flag) {
if (m_parameters.DepthMask == flag) return;
m_parameters.DepthMask = flag; m_parameters.DepthMask = flag;
++m_version;
} }
Bool RenderState::GetDepthMask() const { Bool RenderState::GetDepthMask() const {
@@ -102,7 +123,10 @@ namespace MobileGL {
// -------------------- Clear State -------------------- // -------------------- Clear State --------------------
void RenderState::SetClearColor(FloatVec4 color) { void RenderState::SetClearColor(FloatVec4 color) {
if (m_parameters.ClearColor == color) return;
m_parameters.ClearColor = color; m_parameters.ClearColor = color;
++m_version;
} }
const FloatVec4& RenderState::GetClearColor() const { const FloatVec4& RenderState::GetClearColor() const {
@@ -110,7 +134,10 @@ namespace MobileGL {
} }
void RenderState::SetClearDepth(Float depth) { void RenderState::SetClearDepth(Float depth) {
if (m_parameters.ClearDepth == depth) return;
m_parameters.ClearDepth = depth; m_parameters.ClearDepth = depth;
++m_version;
} }
Float RenderState::GetClearDepth() const { Float RenderState::GetClearDepth() const {
@@ -119,56 +146,29 @@ namespace MobileGL {
// -------------------- Pixel Store -------------------- // -------------------- Pixel Store --------------------
void RenderState::SetPixelStoreParam(PixelStoreParam param, Int value) { 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) { switch (param) {
case PixelStoreParam::PackAlignment: SET_PIXEL_STORE_PARAM(Pack, Alignment, value);
m_parameters.PackParameters.Alignment = value; SET_PIXEL_STORE_PARAM(Pack, RowLength, value);
break; SET_PIXEL_STORE_PARAM(Pack, ImageHeight, value);
case PixelStoreParam::PackRowLength: SET_PIXEL_STORE_PARAM(Pack, SkipPixels, value);
m_parameters.PackParameters.RowLength = value; SET_PIXEL_STORE_PARAM(Pack, SkipRows, value);
break; SET_PIXEL_STORE_PARAM(Pack, SkipImages, value);
case PixelStoreParam::PackImageHeight: SET_PIXEL_STORE_PARAM(Pack, SwapBytes, value != 0);
m_parameters.PackParameters.ImageHeight = value; SET_PIXEL_STORE_PARAM(Pack, LSBFirst, value != 0);
break; SET_PIXEL_STORE_PARAM(Unpack, Alignment, value);
case PixelStoreParam::PackSkipPixels: SET_PIXEL_STORE_PARAM(Unpack, RowLength, value);
m_parameters.PackParameters.SkipPixels = value; SET_PIXEL_STORE_PARAM(Unpack, ImageHeight, value);
break; SET_PIXEL_STORE_PARAM(Unpack, SkipPixels, value);
case PixelStoreParam::PackSkipRows: SET_PIXEL_STORE_PARAM(Unpack, SkipRows, value);
m_parameters.PackParameters.SkipRows = value; SET_PIXEL_STORE_PARAM(Unpack, SkipImages, value);
break; SET_PIXEL_STORE_PARAM(Unpack, SwapBytes, value != 0);
case PixelStoreParam::PackSkipImages: SET_PIXEL_STORE_PARAM(Unpack, LSBFirst, value != 0);
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;
default: default:
MOBILEGL_ASSERT(false, "Invalid PixelStoreParam enum: %d", static_cast<int>(param)); MOBILEGL_ASSERT(false, "Invalid PixelStoreParam enum: %d", static_cast<int>(param));
return; return;
@@ -176,39 +176,26 @@ namespace MobileGL {
} }
Int RenderState::GetPixelStoreParam(PixelStoreParam param) const { 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) { switch (param) {
case PixelStoreParam::PackAlignment: RETURN_PIXEL_STORE_PARAM(Pack, Alignment);
return m_parameters.PackParameters.Alignment; RETURN_PIXEL_STORE_PARAM(Pack, RowLength);
case PixelStoreParam::PackRowLength: RETURN_PIXEL_STORE_PARAM(Pack, ImageHeight);
return m_parameters.PackParameters.RowLength; RETURN_PIXEL_STORE_PARAM(Pack, SkipPixels);
case PixelStoreParam::PackImageHeight: RETURN_PIXEL_STORE_PARAM(Pack, SkipRows);
return m_parameters.PackParameters.ImageHeight; RETURN_PIXEL_STORE_PARAM(Pack, SkipImages);
case PixelStoreParam::PackSkipPixels: RETURN_PIXEL_STORE_PARAM(Pack, SwapBytes);
return m_parameters.PackParameters.SkipPixels; RETURN_PIXEL_STORE_PARAM(Pack, LSBFirst);
case PixelStoreParam::PackSkipRows: RETURN_PIXEL_STORE_PARAM(Unpack, Alignment);
return m_parameters.PackParameters.SkipRows; RETURN_PIXEL_STORE_PARAM(Unpack, RowLength);
case PixelStoreParam::PackSkipImages: RETURN_PIXEL_STORE_PARAM(Unpack, ImageHeight);
return m_parameters.PackParameters.SkipImages; RETURN_PIXEL_STORE_PARAM(Unpack, SkipPixels);
case PixelStoreParam::PackSwapBytes: RETURN_PIXEL_STORE_PARAM(Unpack, SkipRows);
return m_parameters.PackParameters.SwapBytes ? 1 : 0; RETURN_PIXEL_STORE_PARAM(Unpack, SkipImages);
case PixelStoreParam::PackLsbFirst: RETURN_PIXEL_STORE_PARAM(Unpack, SwapBytes);
return m_parameters.PackParameters.LSBFirst ? 1 : 0; RETURN_PIXEL_STORE_PARAM(Unpack, LSBFirst);
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;
default: default:
MOBILEGL_ASSERT(false, "Invalid PixelStoreParam enum: %d", static_cast<int>(param)); MOBILEGL_ASSERT(false, "Invalid PixelStoreParam enum: %d", static_cast<int>(param));
return 0; return 0;
@@ -216,12 +203,15 @@ namespace MobileGL {
} }
PixelStoreParameters RenderState::GetPixelStoreParameters(Bool isUnpack) const { PixelStoreParameters RenderState::GetPixelStoreParameters(Bool isUnpack) const {
return isUnpack ? m_parameters.UnpackParameters : m_parameters.PackParameters; return isUnpack ? m_pixelStoreUnpackParameters : m_pixelStorePackParameters;
} }
// -------------------- Cull Face -------------------- // -------------------- Cull Face --------------------
void RenderState::SetCullFaceMode(CullFaceMode mode) { void RenderState::SetCullFaceMode(CullFaceMode mode) {
if (m_parameters.CullFaceModeSetting == mode) return;
m_parameters.CullFaceModeSetting = mode; m_parameters.CullFaceModeSetting = mode;
++m_version;
} }
CullFaceMode RenderState::GetCullFaceMode() const { CullFaceMode RenderState::GetCullFaceMode() const {
@@ -230,7 +220,10 @@ namespace MobileGL {
// --------------------- Scissor --------------------- // --------------------- Scissor ---------------------
void RenderState::SetScissorBox(IntVec4 box) { void RenderState::SetScissorBox(IntVec4 box) {
if (m_parameters.ScissorBox == box) return;
m_parameters.ScissorBox = box; m_parameters.ScissorBox = box;
++m_version;
} }
const IntVec4& RenderState::GetScissorBox() const { const IntVec4& RenderState::GetScissorBox() const {
@@ -52,7 +52,7 @@ namespace MobileGL {
PackSkipPixels, PackSkipPixels,
PackSkipImages, PackSkipImages,
PackSwapBytes, PackSwapBytes,
PackLsbFirst, PackLSBFirst,
// Unpack Parameters // Unpack Parameters
UnpackAlignment, UnpackAlignment,
@@ -62,7 +62,7 @@ namespace MobileGL {
UnpackSkipPixels, UnpackSkipPixels,
UnpackSkipImages, UnpackSkipImages,
UnpackSwapBytes, UnpackSwapBytes,
UnpackLsbFirst, UnpackLSBFirst,
PixelStoreParamCount, PixelStoreParamCount,
Unknown = -1 Unknown = -1
@@ -150,10 +150,6 @@ namespace MobileGL {
FloatVec4 ClearColor = FloatVec4(0.0f, 0.0f, 0.0f, 1.0f); FloatVec4 ClearColor = FloatVec4(0.0f, 0.0f, 0.0f, 1.0f);
Float ClearDepth = 1.0f; Float ClearDepth = 1.0f;
// Pixel Store
PixelStoreParameters PackParameters;
PixelStoreParameters UnpackParameters;
// Cull Face // Cull Face
Bool CullFaceEnabled = false; Bool CullFaceEnabled = false;
CullFaceMode CullFaceModeSetting = CullFaceMode::Back; CullFaceMode CullFaceModeSetting = CullFaceMode::Back;
@@ -169,6 +165,9 @@ namespace MobileGL {
public: public:
RenderState(); RenderState();
Uint GetVersion() const;
const RenderStateParameters& GetAllParameters() const;
// Rasterization // Rasterization
void SetViewport(IntVec4 viewport); // x, y, width, height void SetViewport(IntVec4 viewport); // x, y, width, height
const IntVec4& GetViewport() const; // 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 const IntVec4& GetScissorBox() const; // x, y, width, height
private: private:
Uint16 m_version = 0;
RenderStateParameters m_parameters; RenderStateParameters m_parameters;
// Pixel Store
PixelStoreParameters m_pixelStorePackParameters;
PixelStoreParameters m_pixelStoreUnpackParameters;
}; };
} // namespace GLState } // namespace GLState
} // namespace MG_State } // namespace MG_State
@@ -85,7 +85,7 @@ namespace MobileGL {
case GL_PACK_SWAP_BYTES: case GL_PACK_SWAP_BYTES:
return PixelStoreParam::PackSwapBytes; return PixelStoreParam::PackSwapBytes;
case GL_PACK_LSB_FIRST: case GL_PACK_LSB_FIRST:
return PixelStoreParam::PackLsbFirst; return PixelStoreParam::PackLSBFirst;
case GL_UNPACK_ALIGNMENT: case GL_UNPACK_ALIGNMENT:
return PixelStoreParam::UnpackAlignment; return PixelStoreParam::UnpackAlignment;
case GL_UNPACK_ROW_LENGTH: case GL_UNPACK_ROW_LENGTH:
@@ -101,7 +101,7 @@ namespace MobileGL {
case GL_UNPACK_SWAP_BYTES: case GL_UNPACK_SWAP_BYTES:
return PixelStoreParam::UnpackSwapBytes; return PixelStoreParam::UnpackSwapBytes;
case GL_UNPACK_LSB_FIRST: case GL_UNPACK_LSB_FIRST:
return PixelStoreParam::UnpackLsbFirst; return PixelStoreParam::UnpackLSBFirst;
default: default:
return PixelStoreParam::Unknown; return PixelStoreParam::Unknown;
} }
@@ -85,7 +85,7 @@ namespace MobileGL {
return GL_PACK_SKIP_IMAGES; return GL_PACK_SKIP_IMAGES;
case PixelStoreParam::PackSwapBytes: case PixelStoreParam::PackSwapBytes:
return GL_PACK_SWAP_BYTES; return GL_PACK_SWAP_BYTES;
case PixelStoreParam::PackLsbFirst: case PixelStoreParam::PackLSBFirst:
return GL_PACK_LSB_FIRST; return GL_PACK_LSB_FIRST;
case PixelStoreParam::UnpackAlignment: case PixelStoreParam::UnpackAlignment:
return GL_UNPACK_ALIGNMENT; return GL_UNPACK_ALIGNMENT;
@@ -101,7 +101,7 @@ namespace MobileGL {
return GL_UNPACK_SKIP_IMAGES; return GL_UNPACK_SKIP_IMAGES;
case PixelStoreParam::UnpackSwapBytes: case PixelStoreParam::UnpackSwapBytes:
return GL_UNPACK_SWAP_BYTES; return GL_UNPACK_SWAP_BYTES;
case PixelStoreParam::UnpackLsbFirst: case PixelStoreParam::UnpackLSBFirst:
return GL_UNPACK_LSB_FIRST; return GL_UNPACK_LSB_FIRST;
default: default:
return GL_UNKNOWN_MGL; return GL_UNKNOWN_MGL;
@@ -84,8 +84,8 @@ namespace MobileGL {
return "PackSkipImages"; return "PackSkipImages";
case PixelStoreParam::PackSwapBytes: case PixelStoreParam::PackSwapBytes:
return "PackSwapBytes"; return "PackSwapBytes";
case PixelStoreParam::PackLsbFirst: case PixelStoreParam::PackLSBFirst:
return "PackLsbFirst"; return "PackLSBFirst";
case PixelStoreParam::UnpackAlignment: case PixelStoreParam::UnpackAlignment:
return "UnpackAlignment"; return "UnpackAlignment";
case PixelStoreParam::UnpackRowLength: case PixelStoreParam::UnpackRowLength:
@@ -100,8 +100,8 @@ namespace MobileGL {
return "UnpackSkipImages"; return "UnpackSkipImages";
case PixelStoreParam::UnpackSwapBytes: case PixelStoreParam::UnpackSwapBytes:
return "UnpackSwapBytes"; return "UnpackSwapBytes";
case PixelStoreParam::UnpackLsbFirst: case PixelStoreParam::UnpackLSBFirst:
return "UnpackLsbFirst"; return "UnpackLSBFirst";
default: default:
return "Unknown"; return "Unknown";
} }