From 1269bc76f3b10200ac846e1baf39e85b8007dfe9 Mon Sep 17 00:00:00 2001 From: BZLZHH Date: Sat, 1 Nov 2025 10:47:59 +0800 Subject: [PATCH] [Feat] (MG_State): Add external index getter for each object. --- MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp | 2 +- MobileGL/MG_Impl/GLImpl/Texture/ProxyTexture.cpp | 2 +- MobileGL/MG_Impl/Init.cpp | 6 +++--- .../MG_State/GLState/BufferState/BufferObject.cpp | 8 ++++++-- MobileGL/MG_State/GLState/BufferState/BufferObject.h | 5 +++-- .../MG_State/GLState/BufferState/BufferState.cpp | 2 +- .../GLState/FramebufferState/FramebufferObject.cpp | 5 ++++- .../GLState/FramebufferState/FramebufferObject.h | 4 +++- .../GLState/FramebufferState/FramebufferState.cpp | 2 +- .../MG_State/GLState/ProgramState/ProgramObject.h | 7 +++++-- .../MG_State/GLState/ProgramState/ShaderObject.h | 7 ++++--- .../MG_State/GLState/TextureState/TextureObject.cpp | 12 ++++++++---- .../MG_State/GLState/TextureState/TextureObject.h | 12 ++++++++---- .../MG_State/GLState/TextureState/TextureState.cpp | 6 +++--- .../GLState/VertexArrayState/VertexArrayObject.cpp | 6 +++++- .../GLState/VertexArrayState/VertexArrayObject.h | 4 +++- .../GLState/VertexArrayState/VertexArrayState.cpp | 4 ++-- 17 files changed, 61 insertions(+), 33 deletions(-) diff --git a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp index 342e5aec..2b072e74 100644 --- a/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp +++ b/MobileGL/MG_Impl/GLImpl/Program/GL_Program.cpp @@ -211,7 +211,7 @@ namespace MobileGL { GLsizei c = std::min((GLsizei)s.size(), maxCount); if (count) *count = c; for (GLsizei i = 0; i < c; ++i) { - shaders[i] = s[i]->GetId(); + shaders[i] = s[i]->GetExternalIndex(); } } diff --git a/MobileGL/MG_Impl/GLImpl/Texture/ProxyTexture.cpp b/MobileGL/MG_Impl/GLImpl/Texture/ProxyTexture.cpp index 94d54a30..5f338376 100644 --- a/MobileGL/MG_Impl/GLImpl/Texture/ProxyTexture.cpp +++ b/MobileGL/MG_Impl/GLImpl/Texture/ProxyTexture.cpp @@ -25,7 +25,7 @@ namespace MobileGL::MG_Impl::GLImpl { if (it != m_proxyTexturesMap.end()) { m_proxyTexturesMap.erase(it); } - m_proxyTexturesMap[target] = MakeShared(); + m_proxyTexturesMap[target] = MakeShared(0); return m_proxyTexturesMap[target]; } diff --git a/MobileGL/MG_Impl/Init.cpp b/MobileGL/MG_Impl/Init.cpp index dcdab740..398ef3d6 100644 --- a/MobileGL/MG_Impl/Init.cpp +++ b/MobileGL/MG_Impl/Init.cpp @@ -10,13 +10,13 @@ namespace MobileGL { // TODO: get real info in EGL auto fbo0 = MG_State::pGLContext->CreateFramebufferObject(0); - auto colorTex = MakeShared(); + auto colorTex = MakeShared(0); colorTex->SetInternalFormat(TextureInternalFormat::RGBA8); colorTex->SetMipmapLevel({{512, 512, 1}, 0, false, 0, {nullptr, 0}}); - auto depthTex = MakeShared(); + auto depthTex = MakeShared(0); depthTex->SetInternalFormat(TextureInternalFormat::Depth32FStencil8); depthTex->SetMipmapLevel({{512, 512, 1}, 0, false, 0, {nullptr, 0}}); - auto stencilTex = MakeShared(); + auto stencilTex = MakeShared(0); stencilTex->SetInternalFormat(TextureInternalFormat::Depth32FStencil8); stencilTex->SetMipmapLevel({{512, 512, 1}, 0, false, 0, {nullptr, 0}}); fbo0->AttachTexture(FramebufferAttachmentType::Color0, colorTex); diff --git a/MobileGL/MG_State/GLState/BufferState/BufferObject.cpp b/MobileGL/MG_State/GLState/BufferState/BufferObject.cpp index d67ca8cd..31b66d12 100644 --- a/MobileGL/MG_State/GLState/BufferState/BufferObject.cpp +++ b/MobileGL/MG_State/GLState/BufferState/BufferObject.cpp @@ -4,8 +4,8 @@ namespace MobileGL { namespace MG_State { namespace GLState { - BufferObject::BufferObject() - : m_id(0), m_size(0), m_usage(BufferUsage::StaticDraw), m_isMapped(false), + BufferObject::BufferObject(Uint externalIndex) + : m_externalIndex(externalIndex), m_size(0), m_usage(BufferUsage::StaticDraw), m_isMapped(false), m_mappingAccess(BufferMappingAccessBit::Null), m_dirtyRange({0, 0}), m_mappedRange({0, 0}), m_dataPtr(MakeShared()) {} @@ -156,6 +156,10 @@ namespace MobileGL { Flags BufferObject::GetMappingAccess() const { return m_isMapped ? m_mappingAccess : BufferMappingAccessBit::Null; } + + Uint BufferObject::GetExternalIndex() const { + return m_externalIndex; + } } // namespace GLState } // namespace MG_State } // namespace MobileGL \ No newline at end of file diff --git a/MobileGL/MG_State/GLState/BufferState/BufferObject.h b/MobileGL/MG_State/GLState/BufferState/BufferObject.h index 4adca4ae..7da9f539 100644 --- a/MobileGL/MG_State/GLState/BufferState/BufferObject.h +++ b/MobileGL/MG_State/GLState/BufferState/BufferObject.h @@ -53,7 +53,7 @@ namespace MobileGL { public: using TargetEnum = BufferTarget; - BufferObject(); + BufferObject(Uint externalIndex); void Resize(SizeT size); void UploadData(DataPtr data, SizeT atOffset); @@ -73,9 +73,10 @@ namespace MobileGL { Range1D GetMappedRange() const; const SharedPtr GetDataReadOnly() const; Flags GetMappingAccess() const; + Uint GetExternalIndex() const; private: - Int m_id = 0; + const Uint m_externalIndex = 0; SizeT m_size = 0; BufferUsage m_usage = BufferUsage::StaticDraw; SharedPtr m_dataPtr; diff --git a/MobileGL/MG_State/GLState/BufferState/BufferState.cpp b/MobileGL/MG_State/GLState/BufferState/BufferState.cpp index 25d0e2fd..e417603f 100644 --- a/MobileGL/MG_State/GLState/BufferState/BufferState.cpp +++ b/MobileGL/MG_State/GLState/BufferState/BufferState.cpp @@ -24,7 +24,7 @@ namespace MobileGL { } SharedPtr BufferState::CreateBufferObject(Uint index) { - auto bufferObject = MakeShared(); + auto bufferObject = MakeShared(index); m_bufferObjects[index] = bufferObject; return bufferObject; } diff --git a/MobileGL/MG_State/GLState/FramebufferState/FramebufferObject.cpp b/MobileGL/MG_State/GLState/FramebufferState/FramebufferObject.cpp index 0b055d18..c0fe683f 100644 --- a/MobileGL/MG_State/GLState/FramebufferState/FramebufferObject.cpp +++ b/MobileGL/MG_State/GLState/FramebufferState/FramebufferObject.cpp @@ -63,7 +63,7 @@ namespace MobileGL { } // FramebufferObject - FramebufferObject::FramebufferObject() { + FramebufferObject::FramebufferObject(Uint externalIndex) : m_externalIndex(externalIndex) { m_attachments.fill(FramebufferAttachment(false)); } @@ -135,6 +135,9 @@ namespace MobileGL { return m_drawBuffers; } + Uint FramebufferObject::GetExternalIndex() const { + return m_externalIndex; + } } // namespace GLState } // namespace MG_State } // namespace MobileGL diff --git a/MobileGL/MG_State/GLState/FramebufferState/FramebufferObject.h b/MobileGL/MG_State/GLState/FramebufferState/FramebufferObject.h index b1ea3af9..e0e0d088 100644 --- a/MobileGL/MG_State/GLState/FramebufferState/FramebufferObject.h +++ b/MobileGL/MG_State/GLState/FramebufferState/FramebufferObject.h @@ -82,7 +82,7 @@ namespace MobileGL { public: using TargetEnum = FramebufferTarget; - FramebufferObject(); + FramebufferObject(Uint externalIndex); void AttachTexture(FramebufferAttachmentType type, SharedPtr texture, int level = 0); void AttachRenderbuffer(FramebufferAttachmentType type, @@ -95,8 +95,10 @@ namespace MobileGL { Bool CheckCompleteness() const; void SetDrawBuffers(const std::vector& buffers); const Vector& GetDrawBuffers() const; + Uint GetExternalIndex() const; private: + const Uint m_externalIndex = 0; Array(FramebufferAttachmentType::FramebufferAttachmentTypeCount)> m_attachments; diff --git a/MobileGL/MG_State/GLState/FramebufferState/FramebufferState.cpp b/MobileGL/MG_State/GLState/FramebufferState/FramebufferState.cpp index 06ca2a12..e8614cc3 100644 --- a/MobileGL/MG_State/GLState/FramebufferState/FramebufferState.cpp +++ b/MobileGL/MG_State/GLState/FramebufferState/FramebufferState.cpp @@ -32,7 +32,7 @@ namespace MobileGL { return nullptr; } } - auto bufferObject = MakeShared(); + auto bufferObject = MakeShared(index); m_framebufferObjects[index] = bufferObject; return bufferObject; } diff --git a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h index 1a90c4d9..3b1426f4 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h +++ b/MobileGL/MG_State/GLState/ProgramState/ProgramObject.h @@ -9,7 +9,7 @@ namespace MobileGL { namespace GLState { class ProgramObject { public: - ProgramObject(const Uint id) : m_id(id) {} + ProgramObject(const Uint externalIndex) : m_externalIndex(externalIndex) {} bool ShaderIsAttached(SharedPtr shader); bool AttachShader(SharedPtr shader); SizeT DetachShader(SharedPtr shader); @@ -78,11 +78,14 @@ namespace MobileGL { Vector>& GetGeneratedSpirv() { return m_generatedSpirv; } + Uint GetExternalIndex() const { return m_externalIndex; } + private: void DoReflection(); void GenerateBinary(); void WaitUntilGenerationCompleted(); - const Uint m_id = 0; + + const Uint m_externalIndex = 0; Vector> m_shaders; SharedPtr m_program; diff --git a/MobileGL/MG_State/GLState/ProgramState/ShaderObject.h b/MobileGL/MG_State/GLState/ProgramState/ShaderObject.h index 5aff6ffc..0e26f425 100644 --- a/MobileGL/MG_State/GLState/ProgramState/ShaderObject.h +++ b/MobileGL/MG_State/GLState/ProgramState/ShaderObject.h @@ -16,13 +16,14 @@ namespace MobileGL { namespace GLState { class ShaderObject { public: - ShaderObject(const ShaderStage stage, const Uint id) : m_stage(stage), m_id(id) {} + ShaderObject(const ShaderStage stage, const Uint externalIndex) + : m_stage(stage), m_externalIndex(externalIndex) {} void SetShaderSource(const String& source); void SetShaderSource(String&& source); void Compile(); void MarkAsDeleted(); - Uint GetId() const { return m_id; } + Uint GetExternalIndex() const { return m_externalIndex; } ShaderStage GetShaderStage() const { return m_stage; } const String& GetShaderSource() const { return m_source; } SharedPtr GetCompiledShader() const { return m_shader; } @@ -32,7 +33,7 @@ namespace MobileGL { Bool GetDeleteStatus() const { return m_deleteStatus; } private: - const Uint m_id = 0; + const Uint m_externalIndex = 0; const ShaderStage m_stage; String m_source; SharedPtr m_shader; diff --git a/MobileGL/MG_State/GLState/TextureState/TextureObject.cpp b/MobileGL/MG_State/GLState/TextureState/TextureObject.cpp index c60629ea..59fa4625 100644 --- a/MobileGL/MG_State/GLState/TextureState/TextureObject.cpp +++ b/MobileGL/MG_State/GLState/TextureState/TextureObject.cpp @@ -5,7 +5,8 @@ namespace MobileGL { namespace MG_State { namespace GLState { // TextureObjectBase implementations - TextureObjectBase::TextureObjectBase(TextureTarget target) : m_target(target) { + TextureObjectBase::TextureObjectBase(TextureTarget target, Uint externalIndex) + : m_target(target), m_externalIndex(externalIndex) { m_sampler = MakeShared(); } @@ -75,7 +76,8 @@ namespace MobileGL { } // TextureObject1D - TextureObject1D::TextureObject1D() : TextureObjectBase(TextureTarget::Texture1D) {} + TextureObject1D::TextureObject1D(Uint externalIndex) + : TextureObjectBase(TextureTarget::Texture1D, externalIndex) {} void TextureObject1D::SetMipmapImpl(const MipmapLevelInput& level) { if (level.size.x() > 0) { @@ -84,7 +86,8 @@ namespace MobileGL { } // TextureObject2D - TextureObject2D::TextureObject2D() : TextureObjectBase(TextureTarget::Texture2D) {} + TextureObject2D::TextureObject2D(Uint externalIndex) + : TextureObjectBase(TextureTarget::Texture2D, externalIndex) {} void TextureObject2D::SetMipmapImpl(const MipmapLevelInput& level) { if (level.size.x() > 0 && level.size.y() > 0) { @@ -93,7 +96,8 @@ namespace MobileGL { } // TextureObject3D - TextureObject3D::TextureObject3D() : TextureObjectBase(TextureTarget::Texture3D) {} + TextureObject3D::TextureObject3D(Uint externalIndex) + : TextureObjectBase(TextureTarget::Texture3D, externalIndex) {} void TextureObject3D::SetMipmapImpl(const MipmapLevelInput& level) { if (level.size.x() > 0 && level.size.y() > 0 && level.size.z() > 0) { diff --git a/MobileGL/MG_State/GLState/TextureState/TextureObject.h b/MobileGL/MG_State/GLState/TextureState/TextureObject.h index b6a89f93..249e14cd 100644 --- a/MobileGL/MG_State/GLState/TextureState/TextureObject.h +++ b/MobileGL/MG_State/GLState/TextureState/TextureObject.h @@ -1,4 +1,5 @@ #pragma once +#include "MG_Util/Types.h" #include "SamplerObject.h" #include #include @@ -213,11 +214,12 @@ namespace MobileGL { virtual void SetInternalFormat(TextureInternalFormat format) = 0; virtual Bool IsComplete() const = 0; virtual void UnmarkMipmapDirty(Int index) = 0; + virtual Uint GetExternalIndex() const = 0; }; class TextureObjectBase : public ITextureObject { public: - TextureObjectBase(TextureTarget target); + TextureObjectBase(TextureTarget target, Uint externalIndex); virtual ~TextureObjectBase() = default; void SetMipmapLevel(const MipmapLevelInput& level) override; @@ -230,10 +232,12 @@ namespace MobileGL { void SetInternalFormat(TextureInternalFormat format) override; Bool IsComplete() const override; void UnmarkMipmapDirty(Int index) override; + Uint GetExternalIndex() const override; protected: virtual void SetMipmapImpl(const MipmapLevelInput& level) = 0; + const Uint m_externalIndex; const TextureTarget m_target = TextureTarget::Unknown; TextureInternalFormat m_internalFormat = TextureInternalFormat::Unknown; Vector m_mipmaps = {}; @@ -242,7 +246,7 @@ namespace MobileGL { class TextureObject1D : public TextureObjectBase { public: - explicit TextureObject1D(); + explicit TextureObject1D(Uint externalIndex); protected: void SetMipmapImpl(const MipmapLevelInput& level) override; @@ -250,7 +254,7 @@ namespace MobileGL { class TextureObject2D : public TextureObjectBase { public: - explicit TextureObject2D(); + explicit TextureObject2D(Uint externalIndex); protected: void SetMipmapImpl(const MipmapLevelInput& level) override; @@ -258,7 +262,7 @@ namespace MobileGL { class TextureObject3D : public TextureObjectBase { public: - explicit TextureObject3D(); + explicit TextureObject3D(Uint externalIndex); protected: void SetMipmapImpl(const MipmapLevelInput& level) override; diff --git a/MobileGL/MG_State/GLState/TextureState/TextureState.cpp b/MobileGL/MG_State/GLState/TextureState/TextureState.cpp index a01830d7..a3fd5686 100644 --- a/MobileGL/MG_State/GLState/TextureState/TextureState.cpp +++ b/MobileGL/MG_State/GLState/TextureState/TextureState.cpp @@ -29,13 +29,13 @@ namespace MobileGL { SharedPtr textureObject = nullptr; switch (target) { case TextureTarget::Texture1D: - textureObject = MakeShared(); + textureObject = MakeShared(index); break; case TextureTarget::Texture2D: - textureObject = MakeShared(); + textureObject = MakeShared(index); break; case TextureTarget::Texture3D: - textureObject = MakeShared(); + textureObject = MakeShared(index); break; default: // TODO: implement more texture types diff --git a/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayObject.cpp b/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayObject.cpp index 84f53235..ee9d24e8 100644 --- a/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayObject.cpp +++ b/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayObject.cpp @@ -3,7 +3,7 @@ namespace MobileGL { namespace MG_State { namespace GLState { - VertexArrayObject::VertexArrayObject() { + VertexArrayObject::VertexArrayObject(Uint externIndex) : m_externalIndex(externIndex) { for (int index = 0; index < MAX_VERTEX_ATTRIBS; ++index) { auto& attr = m_attributes[index]; attr.Enabled = false; @@ -90,6 +90,10 @@ namespace MobileGL { void VertexArrayObject::ClearDirtyAttributes() { m_dirtyAttributes.clear(); } + + Uint VertexArrayObject::GetExternalIndex() const { + return m_externalIndex; + } } // namespace GLState } // namespace MG_State } // namespace MobileGL diff --git a/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayObject.h b/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayObject.h index 2c1ab1e3..76135add 100644 --- a/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayObject.h +++ b/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayObject.h @@ -20,7 +20,7 @@ namespace MobileGL { public: static constexpr int MAX_VERTEX_ATTRIBS = 16; - VertexArrayObject(); + VertexArrayObject(Uint externIndex); void EnableAttribute(Uint index); void DisableAttribute(Uint index); @@ -38,10 +38,12 @@ namespace MobileGL { const Vector& GetDirtyAttributeIndices() const; void ClearDirtyAttributes(); + Uint GetExternalIndex() const; private: void MarkAttributeDirty(Uint index); + const Uint m_externalIndex = 0; Array m_attributes; Vector m_dirtyAttributes; BindingSlot m_indexBufferBindingSlot; diff --git a/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayState.cpp b/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayState.cpp index 6f3f58bf..a4d6d0bf 100644 --- a/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayState.cpp +++ b/MobileGL/MG_State/GLState/VertexArrayState/VertexArrayState.cpp @@ -7,7 +7,7 @@ namespace MobileGL { // Generate default VAO at index 0, which is not valid in core profile, but still remains for // compatibility reasons. m_indexGenerator.Insert(0); - auto defaultVAO = MakeShared(); + auto defaultVAO = MakeShared(0); m_vertexArrays.push_back(defaultVAO); m_boundVertexArray = defaultVAO; } @@ -36,7 +36,7 @@ namespace MobileGL { m_vertexArrays.reserve(std::bit_ceil(index + 1)); m_vertexArrays.resize(index + 1, nullptr); } - auto vao = m_vertexArrays[index] = MakeShared(); + auto vao = m_vertexArrays[index] = MakeShared(index); return vao; }