[Feat] (MG_State): Add external index getter for each object.

This commit is contained in:
BZLZHH
2025-11-01 11:18:35 +08:00
parent 8da681c720
commit 1269bc76f3
17 changed files with 61 additions and 33 deletions
@@ -211,7 +211,7 @@ namespace MobileGL {
GLsizei c = std::min((GLsizei)s.size(), maxCount); GLsizei c = std::min((GLsizei)s.size(), maxCount);
if (count) *count = c; if (count) *count = c;
for (GLsizei i = 0; i < c; ++i) { for (GLsizei i = 0; i < c; ++i) {
shaders[i] = s[i]->GetId(); shaders[i] = s[i]->GetExternalIndex();
} }
} }
@@ -25,7 +25,7 @@ namespace MobileGL::MG_Impl::GLImpl {
if (it != m_proxyTexturesMap.end()) { if (it != m_proxyTexturesMap.end()) {
m_proxyTexturesMap.erase(it); m_proxyTexturesMap.erase(it);
} }
m_proxyTexturesMap[target] = MakeShared<MG_State::GLState::TextureObject2D>(); m_proxyTexturesMap[target] = MakeShared<MG_State::GLState::TextureObject2D>(0);
return m_proxyTexturesMap[target]; return m_proxyTexturesMap[target];
} }
+3 -3
View File
@@ -10,13 +10,13 @@ namespace MobileGL {
// TODO: get real info in EGL // TODO: get real info in EGL
auto fbo0 = MG_State::pGLContext->CreateFramebufferObject(0); auto fbo0 = MG_State::pGLContext->CreateFramebufferObject(0);
auto colorTex = MakeShared<MG_State::GLState::TextureObject2D>(); auto colorTex = MakeShared<MG_State::GLState::TextureObject2D>(0);
colorTex->SetInternalFormat(TextureInternalFormat::RGBA8); colorTex->SetInternalFormat(TextureInternalFormat::RGBA8);
colorTex->SetMipmapLevel({{512, 512, 1}, 0, false, 0, {nullptr, 0}}); colorTex->SetMipmapLevel({{512, 512, 1}, 0, false, 0, {nullptr, 0}});
auto depthTex = MakeShared<MG_State::GLState::TextureObject2D>(); auto depthTex = MakeShared<MG_State::GLState::TextureObject2D>(0);
depthTex->SetInternalFormat(TextureInternalFormat::Depth32FStencil8); depthTex->SetInternalFormat(TextureInternalFormat::Depth32FStencil8);
depthTex->SetMipmapLevel({{512, 512, 1}, 0, false, 0, {nullptr, 0}}); depthTex->SetMipmapLevel({{512, 512, 1}, 0, false, 0, {nullptr, 0}});
auto stencilTex = MakeShared<MG_State::GLState::TextureObject2D>(); auto stencilTex = MakeShared<MG_State::GLState::TextureObject2D>(0);
stencilTex->SetInternalFormat(TextureInternalFormat::Depth32FStencil8); stencilTex->SetInternalFormat(TextureInternalFormat::Depth32FStencil8);
stencilTex->SetMipmapLevel({{512, 512, 1}, 0, false, 0, {nullptr, 0}}); stencilTex->SetMipmapLevel({{512, 512, 1}, 0, false, 0, {nullptr, 0}});
fbo0->AttachTexture(FramebufferAttachmentType::Color0, colorTex); fbo0->AttachTexture(FramebufferAttachmentType::Color0, colorTex);
@@ -4,8 +4,8 @@
namespace MobileGL { namespace MobileGL {
namespace MG_State { namespace MG_State {
namespace GLState { namespace GLState {
BufferObject::BufferObject() BufferObject::BufferObject(Uint externalIndex)
: m_id(0), m_size(0), m_usage(BufferUsage::StaticDraw), m_isMapped(false), : 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_mappingAccess(BufferMappingAccessBit::Null), m_dirtyRange({0, 0}), m_mappedRange({0, 0}),
m_dataPtr(MakeShared<Data>()) {} m_dataPtr(MakeShared<Data>()) {}
@@ -156,6 +156,10 @@ namespace MobileGL {
Flags<BufferMappingAccessBit> BufferObject::GetMappingAccess() const { Flags<BufferMappingAccessBit> BufferObject::GetMappingAccess() const {
return m_isMapped ? m_mappingAccess : BufferMappingAccessBit::Null; return m_isMapped ? m_mappingAccess : BufferMappingAccessBit::Null;
} }
Uint BufferObject::GetExternalIndex() const {
return m_externalIndex;
}
} // namespace GLState } // namespace GLState
} // namespace MG_State } // namespace MG_State
} // namespace MobileGL } // namespace MobileGL
@@ -53,7 +53,7 @@ namespace MobileGL {
public: public:
using TargetEnum = BufferTarget; using TargetEnum = BufferTarget;
BufferObject(); BufferObject(Uint externalIndex);
void Resize(SizeT size); void Resize(SizeT size);
void UploadData(DataPtr data, SizeT atOffset); void UploadData(DataPtr data, SizeT atOffset);
@@ -73,9 +73,10 @@ namespace MobileGL {
Range1D GetMappedRange() const; Range1D GetMappedRange() const;
const SharedPtr<Data> GetDataReadOnly() const; const SharedPtr<Data> GetDataReadOnly() const;
Flags<BufferMappingAccessBit> GetMappingAccess() const; Flags<BufferMappingAccessBit> GetMappingAccess() const;
Uint GetExternalIndex() const;
private: private:
Int m_id = 0; const Uint m_externalIndex = 0;
SizeT m_size = 0; SizeT m_size = 0;
BufferUsage m_usage = BufferUsage::StaticDraw; BufferUsage m_usage = BufferUsage::StaticDraw;
SharedPtr<Data> m_dataPtr; SharedPtr<Data> m_dataPtr;
@@ -24,7 +24,7 @@ namespace MobileGL {
} }
SharedPtr<BufferObject> BufferState::CreateBufferObject(Uint index) { SharedPtr<BufferObject> BufferState::CreateBufferObject(Uint index) {
auto bufferObject = MakeShared<BufferObject>(); auto bufferObject = MakeShared<BufferObject>(index);
m_bufferObjects[index] = bufferObject; m_bufferObjects[index] = bufferObject;
return bufferObject; return bufferObject;
} }
@@ -63,7 +63,7 @@ namespace MobileGL {
} }
// FramebufferObject // FramebufferObject
FramebufferObject::FramebufferObject() { FramebufferObject::FramebufferObject(Uint externalIndex) : m_externalIndex(externalIndex) {
m_attachments.fill(FramebufferAttachment(false)); m_attachments.fill(FramebufferAttachment(false));
} }
@@ -135,6 +135,9 @@ namespace MobileGL {
return m_drawBuffers; return m_drawBuffers;
} }
Uint FramebufferObject::GetExternalIndex() const {
return m_externalIndex;
}
} // namespace GLState } // namespace GLState
} // namespace MG_State } // namespace MG_State
} // namespace MobileGL } // namespace MobileGL
@@ -82,7 +82,7 @@ namespace MobileGL {
public: public:
using TargetEnum = FramebufferTarget; using TargetEnum = FramebufferTarget;
FramebufferObject(); FramebufferObject(Uint externalIndex);
void AttachTexture(FramebufferAttachmentType type, SharedPtr<ITextureObject> texture, int level = 0); void AttachTexture(FramebufferAttachmentType type, SharedPtr<ITextureObject> texture, int level = 0);
void AttachRenderbuffer(FramebufferAttachmentType type, void AttachRenderbuffer(FramebufferAttachmentType type,
@@ -95,8 +95,10 @@ namespace MobileGL {
Bool CheckCompleteness() const; Bool CheckCompleteness() const;
void SetDrawBuffers(const std::vector<FramebufferAttachmentType>& buffers); void SetDrawBuffers(const std::vector<FramebufferAttachmentType>& buffers);
const Vector<FramebufferAttachmentType>& GetDrawBuffers() const; const Vector<FramebufferAttachmentType>& GetDrawBuffers() const;
Uint GetExternalIndex() const;
private: private:
const Uint m_externalIndex = 0;
Array<FramebufferAttachment, Array<FramebufferAttachment,
static_cast<SizeT>(FramebufferAttachmentType::FramebufferAttachmentTypeCount)> static_cast<SizeT>(FramebufferAttachmentType::FramebufferAttachmentTypeCount)>
m_attachments; m_attachments;
@@ -32,7 +32,7 @@ namespace MobileGL {
return nullptr; return nullptr;
} }
} }
auto bufferObject = MakeShared<FramebufferObject>(); auto bufferObject = MakeShared<FramebufferObject>(index);
m_framebufferObjects[index] = bufferObject; m_framebufferObjects[index] = bufferObject;
return bufferObject; return bufferObject;
} }
@@ -9,7 +9,7 @@ namespace MobileGL {
namespace GLState { namespace GLState {
class ProgramObject { class ProgramObject {
public: public:
ProgramObject(const Uint id) : m_id(id) {} ProgramObject(const Uint externalIndex) : m_externalIndex(externalIndex) {}
bool ShaderIsAttached(SharedPtr<ShaderObject> shader); bool ShaderIsAttached(SharedPtr<ShaderObject> shader);
bool AttachShader(SharedPtr<ShaderObject> shader); bool AttachShader(SharedPtr<ShaderObject> shader);
SizeT DetachShader(SharedPtr<ShaderObject> shader); SizeT DetachShader(SharedPtr<ShaderObject> shader);
@@ -78,11 +78,14 @@ namespace MobileGL {
Vector<Vector<unsigned>>& GetGeneratedSpirv() { return m_generatedSpirv; } Vector<Vector<unsigned>>& GetGeneratedSpirv() { return m_generatedSpirv; }
Uint GetExternalIndex() const { return m_externalIndex; }
private: private:
void DoReflection(); void DoReflection();
void GenerateBinary(); void GenerateBinary();
void WaitUntilGenerationCompleted(); void WaitUntilGenerationCompleted();
const Uint m_id = 0;
const Uint m_externalIndex = 0;
Vector<SharedPtr<ShaderObject>> m_shaders; Vector<SharedPtr<ShaderObject>> m_shaders;
SharedPtr<glslang::TProgram> m_program; SharedPtr<glslang::TProgram> m_program;
@@ -16,13 +16,14 @@ namespace MobileGL {
namespace GLState { namespace GLState {
class ShaderObject { class ShaderObject {
public: 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(const String& source);
void SetShaderSource(String&& source); void SetShaderSource(String&& source);
void Compile(); void Compile();
void MarkAsDeleted(); void MarkAsDeleted();
Uint GetId() const { return m_id; } Uint GetExternalIndex() const { return m_externalIndex; }
ShaderStage GetShaderStage() const { return m_stage; } ShaderStage GetShaderStage() const { return m_stage; }
const String& GetShaderSource() const { return m_source; } const String& GetShaderSource() const { return m_source; }
SharedPtr<glslang::TShader> GetCompiledShader() const { return m_shader; } SharedPtr<glslang::TShader> GetCompiledShader() const { return m_shader; }
@@ -32,7 +33,7 @@ namespace MobileGL {
Bool GetDeleteStatus() const { return m_deleteStatus; } Bool GetDeleteStatus() const { return m_deleteStatus; }
private: private:
const Uint m_id = 0; const Uint m_externalIndex = 0;
const ShaderStage m_stage; const ShaderStage m_stage;
String m_source; String m_source;
SharedPtr<glslang::TShader> m_shader; SharedPtr<glslang::TShader> m_shader;
@@ -5,7 +5,8 @@ namespace MobileGL {
namespace MG_State { namespace MG_State {
namespace GLState { namespace GLState {
// TextureObjectBase implementations // TextureObjectBase implementations
TextureObjectBase::TextureObjectBase(TextureTarget target) : m_target(target) { TextureObjectBase::TextureObjectBase(TextureTarget target, Uint externalIndex)
: m_target(target), m_externalIndex(externalIndex) {
m_sampler = MakeShared<SamplerObject>(); m_sampler = MakeShared<SamplerObject>();
} }
@@ -75,7 +76,8 @@ namespace MobileGL {
} }
// TextureObject1D // TextureObject1D
TextureObject1D::TextureObject1D() : TextureObjectBase(TextureTarget::Texture1D) {} TextureObject1D::TextureObject1D(Uint externalIndex)
: TextureObjectBase(TextureTarget::Texture1D, externalIndex) {}
void TextureObject1D::SetMipmapImpl(const MipmapLevelInput& level) { void TextureObject1D::SetMipmapImpl(const MipmapLevelInput& level) {
if (level.size.x() > 0) { if (level.size.x() > 0) {
@@ -84,7 +86,8 @@ namespace MobileGL {
} }
// TextureObject2D // TextureObject2D
TextureObject2D::TextureObject2D() : TextureObjectBase(TextureTarget::Texture2D) {} TextureObject2D::TextureObject2D(Uint externalIndex)
: TextureObjectBase(TextureTarget::Texture2D, externalIndex) {}
void TextureObject2D::SetMipmapImpl(const MipmapLevelInput& level) { void TextureObject2D::SetMipmapImpl(const MipmapLevelInput& level) {
if (level.size.x() > 0 && level.size.y() > 0) { if (level.size.x() > 0 && level.size.y() > 0) {
@@ -93,7 +96,8 @@ namespace MobileGL {
} }
// TextureObject3D // TextureObject3D
TextureObject3D::TextureObject3D() : TextureObjectBase(TextureTarget::Texture3D) {} TextureObject3D::TextureObject3D(Uint externalIndex)
: TextureObjectBase(TextureTarget::Texture3D, externalIndex) {}
void TextureObject3D::SetMipmapImpl(const MipmapLevelInput& level) { void TextureObject3D::SetMipmapImpl(const MipmapLevelInput& level) {
if (level.size.x() > 0 && level.size.y() > 0 && level.size.z() > 0) { if (level.size.x() > 0 && level.size.y() > 0 && level.size.z() > 0) {
@@ -1,4 +1,5 @@
#pragma once #pragma once
#include "MG_Util/Types.h"
#include "SamplerObject.h" #include "SamplerObject.h"
#include <Includes.h> #include <Includes.h>
#include <MG_Util/Math/VectorTypes.h> #include <MG_Util/Math/VectorTypes.h>
@@ -213,11 +214,12 @@ namespace MobileGL {
virtual void SetInternalFormat(TextureInternalFormat format) = 0; virtual void SetInternalFormat(TextureInternalFormat format) = 0;
virtual Bool IsComplete() const = 0; virtual Bool IsComplete() const = 0;
virtual void UnmarkMipmapDirty(Int index) = 0; virtual void UnmarkMipmapDirty(Int index) = 0;
virtual Uint GetExternalIndex() const = 0;
}; };
class TextureObjectBase : public ITextureObject { class TextureObjectBase : public ITextureObject {
public: public:
TextureObjectBase(TextureTarget target); TextureObjectBase(TextureTarget target, Uint externalIndex);
virtual ~TextureObjectBase() = default; virtual ~TextureObjectBase() = default;
void SetMipmapLevel(const MipmapLevelInput& level) override; void SetMipmapLevel(const MipmapLevelInput& level) override;
@@ -230,10 +232,12 @@ namespace MobileGL {
void SetInternalFormat(TextureInternalFormat format) override; void SetInternalFormat(TextureInternalFormat format) override;
Bool IsComplete() const override; Bool IsComplete() const override;
void UnmarkMipmapDirty(Int index) override; void UnmarkMipmapDirty(Int index) override;
Uint GetExternalIndex() const override;
protected: protected:
virtual void SetMipmapImpl(const MipmapLevelInput& level) = 0; virtual void SetMipmapImpl(const MipmapLevelInput& level) = 0;
const Uint m_externalIndex;
const TextureTarget m_target = TextureTarget::Unknown; const TextureTarget m_target = TextureTarget::Unknown;
TextureInternalFormat m_internalFormat = TextureInternalFormat::Unknown; TextureInternalFormat m_internalFormat = TextureInternalFormat::Unknown;
Vector<MipmapLevelInternal> m_mipmaps = {}; Vector<MipmapLevelInternal> m_mipmaps = {};
@@ -242,7 +246,7 @@ namespace MobileGL {
class TextureObject1D : public TextureObjectBase { class TextureObject1D : public TextureObjectBase {
public: public:
explicit TextureObject1D(); explicit TextureObject1D(Uint externalIndex);
protected: protected:
void SetMipmapImpl(const MipmapLevelInput& level) override; void SetMipmapImpl(const MipmapLevelInput& level) override;
@@ -250,7 +254,7 @@ namespace MobileGL {
class TextureObject2D : public TextureObjectBase { class TextureObject2D : public TextureObjectBase {
public: public:
explicit TextureObject2D(); explicit TextureObject2D(Uint externalIndex);
protected: protected:
void SetMipmapImpl(const MipmapLevelInput& level) override; void SetMipmapImpl(const MipmapLevelInput& level) override;
@@ -258,7 +262,7 @@ namespace MobileGL {
class TextureObject3D : public TextureObjectBase { class TextureObject3D : public TextureObjectBase {
public: public:
explicit TextureObject3D(); explicit TextureObject3D(Uint externalIndex);
protected: protected:
void SetMipmapImpl(const MipmapLevelInput& level) override; void SetMipmapImpl(const MipmapLevelInput& level) override;
@@ -29,13 +29,13 @@ namespace MobileGL {
SharedPtr<ITextureObject> textureObject = nullptr; SharedPtr<ITextureObject> textureObject = nullptr;
switch (target) { switch (target) {
case TextureTarget::Texture1D: case TextureTarget::Texture1D:
textureObject = MakeShared<TextureObject1D>(); textureObject = MakeShared<TextureObject1D>(index);
break; break;
case TextureTarget::Texture2D: case TextureTarget::Texture2D:
textureObject = MakeShared<TextureObject2D>(); textureObject = MakeShared<TextureObject2D>(index);
break; break;
case TextureTarget::Texture3D: case TextureTarget::Texture3D:
textureObject = MakeShared<TextureObject3D>(); textureObject = MakeShared<TextureObject3D>(index);
break; break;
default: default:
// TODO: implement more texture types // TODO: implement more texture types
@@ -3,7 +3,7 @@
namespace MobileGL { namespace MobileGL {
namespace MG_State { namespace MG_State {
namespace GLState { namespace GLState {
VertexArrayObject::VertexArrayObject() { VertexArrayObject::VertexArrayObject(Uint externIndex) : m_externalIndex(externIndex) {
for (int index = 0; index < MAX_VERTEX_ATTRIBS; ++index) { for (int index = 0; index < MAX_VERTEX_ATTRIBS; ++index) {
auto& attr = m_attributes[index]; auto& attr = m_attributes[index];
attr.Enabled = false; attr.Enabled = false;
@@ -90,6 +90,10 @@ namespace MobileGL {
void VertexArrayObject::ClearDirtyAttributes() { void VertexArrayObject::ClearDirtyAttributes() {
m_dirtyAttributes.clear(); m_dirtyAttributes.clear();
} }
Uint VertexArrayObject::GetExternalIndex() const {
return m_externalIndex;
}
} // namespace GLState } // namespace GLState
} // namespace MG_State } // namespace MG_State
} // namespace MobileGL } // namespace MobileGL
@@ -20,7 +20,7 @@ namespace MobileGL {
public: public:
static constexpr int MAX_VERTEX_ATTRIBS = 16; static constexpr int MAX_VERTEX_ATTRIBS = 16;
VertexArrayObject(); VertexArrayObject(Uint externIndex);
void EnableAttribute(Uint index); void EnableAttribute(Uint index);
void DisableAttribute(Uint index); void DisableAttribute(Uint index);
@@ -38,10 +38,12 @@ namespace MobileGL {
const Vector<Uint>& GetDirtyAttributeIndices() const; const Vector<Uint>& GetDirtyAttributeIndices() const;
void ClearDirtyAttributes(); void ClearDirtyAttributes();
Uint GetExternalIndex() const;
private: private:
void MarkAttributeDirty(Uint index); void MarkAttributeDirty(Uint index);
const Uint m_externalIndex = 0;
Array<VertexAttribute, MAX_VERTEX_ATTRIBS> m_attributes; Array<VertexAttribute, MAX_VERTEX_ATTRIBS> m_attributes;
Vector<Uint> m_dirtyAttributes; Vector<Uint> m_dirtyAttributes;
BindingSlot<BufferObject> m_indexBufferBindingSlot; BindingSlot<BufferObject> m_indexBufferBindingSlot;
@@ -7,7 +7,7 @@ namespace MobileGL {
// Generate default VAO at index 0, which is not valid in core profile, but still remains for // Generate default VAO at index 0, which is not valid in core profile, but still remains for
// compatibility reasons. // compatibility reasons.
m_indexGenerator.Insert(0); m_indexGenerator.Insert(0);
auto defaultVAO = MakeShared<VertexArrayObject>(); auto defaultVAO = MakeShared<VertexArrayObject>(0);
m_vertexArrays.push_back(defaultVAO); m_vertexArrays.push_back(defaultVAO);
m_boundVertexArray = defaultVAO; m_boundVertexArray = defaultVAO;
} }
@@ -36,7 +36,7 @@ namespace MobileGL {
m_vertexArrays.reserve(std::bit_ceil(index + 1)); m_vertexArrays.reserve(std::bit_ceil(index + 1));
m_vertexArrays.resize(index + 1, nullptr); m_vertexArrays.resize(index + 1, nullptr);
} }
auto vao = m_vertexArrays[index] = MakeShared<VertexArrayObject>(); auto vao = m_vertexArrays[index] = MakeShared<VertexArrayObject>(index);
return vao; return vao;
} }