mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-19 07:51:57 +09:00
[Feature] (P14): state ownership - multi-session, multi-context, share groups
Breaks the one process = one server = one session = one context equation
that the whole stack's correctness used to rest on. Design and per-slice
shape: docs/Disaggregated/design/11-state-ownership.md.
S1 wire: bind_context ring record (opcode 84) announces the current
context at the make-current edge; CreateContext/DestroyContext join the
SurfaceOp control frames; control revision 5 -> 6; the server keeps a
per-session token table and refuses unknown tokens by name.
S2 server: SessionRuntime per session with thread-scoped resolution
(singletons become fallbacks), per-thread gPipeInputs, per-session latch
domains, per-thread segment resolvers; the in-process unix supervisor
serves up to MOBILEGL_IPC_INPROC_MAX_SESSIONS (16) concurrent sessions;
surface mode is declared per session and the display lease refuses a
second on-screen session by name.
S3 frontend: ShareGroupState owns the shared object stores and name
generators (buffer/texture/renderbuffer/sampler/program), GLContext keeps
per-context bindings/errors/container objects; pGLContext becomes
thread-local with a process default context preserving the single-context
world verbatim; eglCreateContext builds a real GLContext and joins the
sharing context's group.
S4 backend: native EGL tuples keyed by (session, context token);
InitDisplayAndContext's unconditional teardown is gone - creating a
second context or surface no longer destroys the first; eglCreateContext
sharing is wired to the native driver; bind_context is the native switch
point.
S5 applier: MGPipeApplierState splits into MGPipeObjectRecords (per
share group) and MGPipeWorkingState (per context), resolved through a
{session, token} registry with a thread-local cached fast path.
Tests: MultiSessionTest (incl. two sessions x two contexts interleaved
with real clear/readback witnesses), ApplierOwnershipTest (with a
null-resolver negative control), ContextStateOwnershipTest,
ServerLoopEglTest native-context liveness; 99/99 of the acceptance set
green on the host build.
Known debts (documented in the design doc): twin registries still keyed
by handle alone, no per-record ContextSlot, process-global backend
generations, MakeCurrent dedup per current binding.
This commit is contained in:
@@ -233,6 +233,11 @@ namespace MobileGL {
|
||||
m_contextOwners.erase(ownerIt);
|
||||
}
|
||||
}
|
||||
// P14 S3: the thread's GL state goes back to what it was before this EGL
|
||||
// context was made current on it.
|
||||
if (currentIt->second.RestoreThreadGLState) {
|
||||
pGLContext = Move(currentIt->second.RestoreThreadGLState);
|
||||
}
|
||||
m_threadCurrents.erase(currentIt);
|
||||
DestroyPendingSurfaceIfUnused(drawSurface);
|
||||
DestroyPendingSurfaceIfUnused(readSurface);
|
||||
@@ -306,6 +311,12 @@ namespace MobileGL {
|
||||
m_contextOwners.erase(ownerIt);
|
||||
}
|
||||
}
|
||||
// Only the CALLING thread's TLS is reachable from here - another
|
||||
// thread's current is dropped from the table, and its own next EGL
|
||||
// call re-establishes the binding.
|
||||
if (threadIt->first == CurrentThreadKey() && threadIt->second.RestoreThreadGLState) {
|
||||
pGLContext = Move(threadIt->second.RestoreThreadGLState);
|
||||
}
|
||||
threadIt = m_threadCurrents.erase(threadIt);
|
||||
} else {
|
||||
++threadIt;
|
||||
@@ -654,6 +665,30 @@ namespace MobileGL {
|
||||
.MinorVersion = 0,
|
||||
};
|
||||
|
||||
// P14 S1: the wire identity, minted where the context is born. A context that
|
||||
// shares with an existing one joins ITS share group; EGL_NO_CONTEXT starts a new
|
||||
// one. Both tokens are 0-free by construction (the counters start at 1), because
|
||||
// 0 is the wire's "no context" on both planes.
|
||||
contextObject.ClientContextToken = m_nextClientContextToken++;
|
||||
SharedPtr<GLState::ShareGroupState> shareGroup;
|
||||
if (shareCtx != nullptr) {
|
||||
const auto* shared = TryGetContext(shareCtx);
|
||||
if (shared != nullptr && shared->GLStateObject) {
|
||||
shareGroup = shared->GLStateObject->GetShareGroup();
|
||||
contextObject.ShareGroupToken = shared->ShareGroupToken;
|
||||
} else {
|
||||
contextObject.ShareGroupToken = m_nextShareGroupToken++;
|
||||
}
|
||||
} else {
|
||||
contextObject.ShareGroupToken = m_nextShareGroupToken++;
|
||||
}
|
||||
if (!shareGroup) {
|
||||
shareGroup = MakeShared<GLState::ShareGroupState>(contextObject.ShareGroupToken);
|
||||
}
|
||||
// P14 S3: the context's own GL state, sharing the objects (and the name spaces)
|
||||
// of the group above, with bindings, errors and the current program of its own.
|
||||
contextObject.GLStateObject = MakeShared<GLState::GLContext>(shareGroup);
|
||||
|
||||
if (auto value = ParseAttribValue(attribList, EGL_CONTEXT_CLIENT_VERSION); value) {
|
||||
contextObject.ClientVersion = *value;
|
||||
contextObject.MajorVersion = *value;
|
||||
@@ -779,6 +814,37 @@ namespace MobileGL {
|
||||
return ctx && ctx->Display == display;
|
||||
}
|
||||
|
||||
// P14 S1. 0 for an unknown handle or a thread with no current context: 0 is the
|
||||
// wire's "no context" on both planes, so an answer that cannot be given is given as
|
||||
// the value that means "nothing is bound" rather than as a handle-shaped guess.
|
||||
Uint64 EGLContext::GetContextClientToken(EGLContextHandle context) const {
|
||||
const std::lock_guard<std::recursive_mutex> lock(m_mutex);
|
||||
const auto* ctx = TryGetContext(context);
|
||||
return ctx != nullptr ? ctx->ClientContextToken : 0;
|
||||
}
|
||||
|
||||
Uint64 EGLContext::GetContextShareGroupToken(EGLContextHandle context) const {
|
||||
const std::lock_guard<std::recursive_mutex> lock(m_mutex);
|
||||
const auto* ctx = TryGetContext(context);
|
||||
return ctx != nullptr ? ctx->ShareGroupToken : 0;
|
||||
}
|
||||
|
||||
SharedPtr<GLState::GLContext> EGLContext::GetContextGLState(EGLContextHandle context) const {
|
||||
const std::lock_guard<std::recursive_mutex> lock(m_mutex);
|
||||
const auto* ctx = TryGetContext(context);
|
||||
return ctx != nullptr ? ctx->GLStateObject : nullptr;
|
||||
}
|
||||
|
||||
Uint64 EGLContext::CurrentContextClientToken() const {
|
||||
const std::lock_guard<std::recursive_mutex> lock(m_mutex);
|
||||
const auto currentIt = m_threadCurrents.find(CurrentThreadKey());
|
||||
if (currentIt == m_threadCurrents.end()) {
|
||||
return 0;
|
||||
}
|
||||
const auto* ctx = TryGetContext(currentIt->second.Context);
|
||||
return ctx != nullptr ? ctx->ClientContextToken : 0;
|
||||
}
|
||||
|
||||
Bool EGLContext::IsCurrentContextOpenGLCoreProfile() const {
|
||||
const std::lock_guard<std::recursive_mutex> lock(m_mutex);
|
||||
auto currentIt = m_threadCurrents.find(CurrentThreadKey());
|
||||
@@ -1276,6 +1342,16 @@ namespace MobileGL {
|
||||
}
|
||||
}
|
||||
|
||||
// P14 S3: whatever this thread's GL state was before the first EGL context was
|
||||
// bound to it is what a later release goes back to.
|
||||
SharedPtr<GLState::GLContext> restoreThreadGLState;
|
||||
if (const auto currentIt = m_threadCurrents.find(threadKey);
|
||||
currentIt != m_threadCurrents.end()) {
|
||||
restoreThreadGLState = currentIt->second.RestoreThreadGLState;
|
||||
} else {
|
||||
restoreThreadGLState = pGLContext;
|
||||
}
|
||||
|
||||
ReleaseThreadUnlocked(threadKey);
|
||||
|
||||
m_threadCurrents[threadKey] = ThreadCurrentState{
|
||||
@@ -1283,9 +1359,15 @@ namespace MobileGL {
|
||||
.DrawSurface = draw,
|
||||
.ReadSurface = read,
|
||||
.Context = context,
|
||||
.RestoreThreadGLState = Move(restoreThreadGLState),
|
||||
};
|
||||
if (context != nullptr) {
|
||||
m_contextOwners[context] = threadKey;
|
||||
// P14 S3: this thread's GL calls land in this context's GLContext from here
|
||||
// on, and not before the switch above has been committed.
|
||||
if (const auto* contextObject = TryGetContext(context); contextObject != nullptr) {
|
||||
pGLContext = contextObject->GLStateObject;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#pragma once
|
||||
#include <Includes.h>
|
||||
#include <type_traits>
|
||||
#include <MG_State/GLState/Core.h>
|
||||
|
||||
namespace MobileGL {
|
||||
namespace MG_State {
|
||||
@@ -69,6 +70,20 @@ namespace MobileGL {
|
||||
Bool IsCurrentContextOpenGLCompatibilityProfile() const;
|
||||
EGLint GetCurrentContextFlags() const;
|
||||
|
||||
// P14 S1. The wire identity of a context, and of the context the CALLING THREAD
|
||||
// has current (0 when it has none - the release edge's value, and the value a
|
||||
// session that never crossed a bind_context answers with). Read by MG_Impl's two
|
||||
// producers: EGLImpl's CreateContext/DestroyContext control frames and PipeFill's
|
||||
// bind_context emission at the make-current edge.
|
||||
Uint64 GetContextClientToken(EGLContextHandle context) const;
|
||||
Uint64 GetContextShareGroupToken(EGLContextHandle context) const;
|
||||
Uint64 CurrentContextClientToken() const;
|
||||
|
||||
// P14 S3. The frontend GL state this context owns, so a caller outside MG_State
|
||||
// can reach the other half of a context it just made current. Null for an
|
||||
// unknown handle.
|
||||
SharedPtr<GLState::GLContext> GetContextGLState(EGLContextHandle context) const;
|
||||
|
||||
// Surface
|
||||
EGLSurfaceHandle CreateWindowSurface(EGLDisplayHandle display, EGLConfigHandle config,
|
||||
NativeWindowType window, const EGLint* attribList);
|
||||
@@ -180,6 +195,17 @@ namespace MobileGL {
|
||||
EGLDisplayHandle Display = EGL_NO_DISPLAY;
|
||||
EGLConfigHandle Config = nullptr;
|
||||
EGLContextHandle SharedContext = nullptr;
|
||||
// P14 S1 (docs/Disaggregated/design/11-state-ownership.md). The wire identity of
|
||||
// this context: `ClientContextToken` is what bind_context carries and what the
|
||||
// server's per-session context table is keyed by, `ShareGroupToken` the group
|
||||
// it was derived into (ShareContext's group, or one of its own). Minted here,
|
||||
// in the one place a context is born, so no caller can invent a second one.
|
||||
Uint64 ClientContextToken = 0;
|
||||
Uint64 ShareGroupToken = 0;
|
||||
// P14 S3. The GL state of THIS EGL context. Its object registries live in the
|
||||
// share group's ShareGroupState (shared with every context that shares with
|
||||
// this one); its bindings, errors and current program are its own.
|
||||
SharedPtr<GLState::GLContext> GLStateObject;
|
||||
EGLenum ClientAPI = EGL_OPENGL_API;
|
||||
EGLint ClientVersion = 1;
|
||||
EGLint MajorVersion = 1;
|
||||
@@ -226,6 +252,10 @@ namespace MobileGL {
|
||||
EGLSurfaceHandle DrawSurface = EGL_NO_SURFACE;
|
||||
EGLSurfaceHandle ReadSurface = EGL_NO_SURFACE;
|
||||
EGLContextHandle Context = nullptr;
|
||||
// P14 S3. What pGLContext held on this thread BEFORE the first eglMakeCurrent
|
||||
// bound an EGL context to it, so releasing restores exactly the binding the
|
||||
// thread had rather than a guessed default.
|
||||
SharedPtr<GLState::GLContext> RestoreThreadGLState;
|
||||
};
|
||||
|
||||
template <typename HandleType>
|
||||
@@ -269,6 +299,9 @@ namespace MobileGL {
|
||||
Uint64 m_nextContextHandle = 1;
|
||||
Uint64 m_nextSyncHandle = 1;
|
||||
Uint64 m_nextImageHandle = 1;
|
||||
// P14 S1. Dense from 1, never reused, never 0: 0 is "no context" on both planes.
|
||||
Uint64 m_nextClientContextToken = 1;
|
||||
Uint64 m_nextShareGroupToken = 1;
|
||||
|
||||
UnorderedMap<DisplayLookupKey, EGLDisplayHandle, DisplayLookupHasher> m_displayLookup;
|
||||
UnorderedMap<EGLDisplayHandle, DisplayObject> m_displays;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "BufferState.h"
|
||||
|
||||
namespace MobileGL::MG_State::GLState {
|
||||
BufferState::BufferState() : m_indexGenerator(1024, 1) {
|
||||
BufferState::BufferState() {
|
||||
for (SizeT i = 0; i < m_bindingSlots.size(); ++i) {
|
||||
m_bindingSlots[i] = BindingSlot<BufferObject>(GlobalBufferTargets[i]);
|
||||
}
|
||||
@@ -18,28 +18,6 @@ namespace MobileGL::MG_State::GLState {
|
||||
}
|
||||
}
|
||||
|
||||
const SharedPtr<BufferObject>& BufferState::GetBufferObject(Uint index) {
|
||||
auto it = m_bufferObjects.find(index);
|
||||
if (it != m_bufferObjects.end()) {
|
||||
return it->second;
|
||||
}
|
||||
static SharedPtr<BufferObject> nullBufferObject = nullptr;
|
||||
return nullBufferObject;
|
||||
}
|
||||
|
||||
void BufferState::GenerateNames(Uint number, Vector<Uint>& buffers) {
|
||||
buffers.resize(number);
|
||||
m_indexGenerator.Generate(number, buffers.data());
|
||||
}
|
||||
|
||||
const SharedPtr<BufferObject>& BufferState::CreateBufferObject(Uint index) {
|
||||
auto& bufferObj = m_bufferObjects[index];
|
||||
if (!bufferObj) {
|
||||
bufferObj = MakeShared<BufferObject>(index);
|
||||
}
|
||||
return bufferObj;
|
||||
}
|
||||
|
||||
BindingSlot<BufferObject>& BufferState::GetBindingSlot(BufferTarget target) {
|
||||
for (auto& bindingSlot : m_bindingSlots) {
|
||||
if (bindingSlot.GetTarget() == target) {
|
||||
@@ -50,53 +28,33 @@ namespace MobileGL::MG_State::GLState {
|
||||
return m_bindingSlots[0];
|
||||
}
|
||||
|
||||
void BufferState::MarkBufferObjectForDeletion(Uint index) {
|
||||
if (m_indexGenerator.IsValid(index)) {
|
||||
auto it = m_bufferObjects.find(index);
|
||||
if (it != m_bufferObjects.end()) {
|
||||
for (auto& bindingSlot : m_bindingSlots) {
|
||||
if (bindingSlot.GetBoundObject() == it->second) {
|
||||
bindingSlot.Bind(nullptr);
|
||||
}
|
||||
}
|
||||
for (auto& bindingPointArray : m_bufferBindPointTargets) {
|
||||
for (auto& bindingPoint : bindingPointArray) {
|
||||
if (bindingPoint.GetBoundObject() == it->second) {
|
||||
bindingPoint.Bind(nullptr);
|
||||
bindingPoint.ClearRange();
|
||||
void BufferState::UnbindBufferObject(const SharedPtr<BufferObject>& bufferObject) {
|
||||
if (!bufferObject) return;
|
||||
for (auto& bindingSlot : m_bindingSlots) {
|
||||
if (bindingSlot.GetBoundObject() == bufferObject) {
|
||||
bindingSlot.Bind(nullptr);
|
||||
}
|
||||
}
|
||||
for (auto& bindingPointArray : m_bufferBindPointTargets) {
|
||||
for (auto& bindingPoint : bindingPointArray) {
|
||||
if (bindingPoint.GetBoundObject() == bufferObject) {
|
||||
bindingPoint.Bind(nullptr);
|
||||
bindingPoint.ClearRange();
|
||||
#if MOBILEGL_PIPE_PUSH
|
||||
// An UNBIND, and the one writer of an indexed point that is not an
|
||||
// entry point (GL_Buffer.cpp's BindBuffer{Base,Range}_State and
|
||||
// Core.cpp's transform-feedback writers bump their own): without the
|
||||
// bump the target's window is never re-sent, the server goes on naming
|
||||
// this buffer's handle, and once its slot is reused at a newer
|
||||
// generation the next draw or dispatch that walks the window asks the
|
||||
// backend for the dead one - Espryt dies with Fatal{ProtocolCorruption,
|
||||
// "BackendSlotTable.Generation"} (DeletedBoundBufferScenario).
|
||||
NoteBindPointChanged(BufferBindPointTargets[static_cast<SizeT>(
|
||||
&bindingPointArray - m_bufferBindPointTargets.data())]);
|
||||
// An UNBIND, and the one writer of an indexed point that is not an
|
||||
// entry point (GL_Buffer.cpp's BindBuffer{Base,Range}_State and
|
||||
// Core.cpp's transform-feedback writers bump their own): without the
|
||||
// bump the target's window is never re-sent, the server goes on naming
|
||||
// this buffer's handle, and once its slot is reused at a newer
|
||||
// generation the next draw or dispatch that walks the window asks the
|
||||
// backend for the dead one - Espryt dies with Fatal{ProtocolCorruption,
|
||||
// "BackendSlotTable.Generation"} (DeletedBoundBufferScenario).
|
||||
NoteBindPointChanged(BufferBindPointTargets[static_cast<SizeT>(
|
||||
&bindingPointArray - m_bufferBindPointTargets.data())]);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
// Erase through the iterator already in hand: erase(key) would repeat the
|
||||
// find() above, and the successor scan that once made key-based
|
||||
// erase the cheaper of the two no longer happens here - erase(iterator)
|
||||
// hands back an unconverted proxy, and the scan is what converting it
|
||||
// would cost. The unbind loops above touch only the binding arrays, so
|
||||
// `it` is still live.
|
||||
m_bufferObjects.erase(it);
|
||||
}
|
||||
m_indexGenerator.Delete(index);
|
||||
}
|
||||
}
|
||||
|
||||
Bool BufferState::ValidateName(Uint index) const {
|
||||
return m_indexGenerator.IsValid(index);
|
||||
}
|
||||
|
||||
Bool BufferState::ValidateBufferObject(Uint index) const {
|
||||
return m_bufferObjects.find(index) != m_bufferObjects.end();
|
||||
}
|
||||
|
||||
BindingSlotRange1D<BufferObject>& BufferState::GetBindingPoint(BufferTarget target, Uint index) {
|
||||
|
||||
@@ -28,13 +28,12 @@ namespace MobileGL::MG_State::GLState {
|
||||
// costs the unused tail of three arrays.
|
||||
constexpr SizeT BufferBindingPointCount = 84;
|
||||
|
||||
// The per-context half of the buffer state: which buffer is bound where. The objects
|
||||
// themselves and their names belong to the share group (ShareGroupState).
|
||||
class BufferState {
|
||||
public:
|
||||
BufferState();
|
||||
|
||||
const SharedPtr<BufferObject>& GetBufferObject(Uint index);
|
||||
void GenerateNames(Uint number, Vector<Uint>& buffers);
|
||||
const SharedPtr<BufferObject>& CreateBufferObject(Uint index);
|
||||
BindingSlot<BufferObject>& GetBindingSlot(BufferTarget target);
|
||||
// For glBindBufferBase / glBindBufferRange
|
||||
BindingSlotRange1D<BufferObject>& GetBindingPoint(BufferTarget target, Uint index);
|
||||
@@ -60,9 +59,9 @@ namespace MobileGL::MG_State::GLState {
|
||||
if (it == BufferBindPointTargets.end()) return 0;
|
||||
return m_touchedBindPointCount[std::distance(BufferBindPointTargets.begin(), it)];
|
||||
}
|
||||
void MarkBufferObjectForDeletion(Uint index);
|
||||
Bool ValidateName(Uint index) const;
|
||||
Bool ValidateBufferObject(Uint index) const;
|
||||
// Detaches a deleted buffer from THIS context's bindings only (GL 4.6 core 6.1: a delete
|
||||
// resets the deleting context's bindings, and other contexts keep their own).
|
||||
void UnbindBufferObject(const SharedPtr<BufferObject>& bufferObject);
|
||||
|
||||
#if MOBILEGL_PIPE_PUSH
|
||||
// P2 brief D4: "did the contents of ANY buffer object move". One counter for every
|
||||
@@ -110,8 +109,6 @@ namespace MobileGL::MG_State::GLState {
|
||||
Uint64 m_anyBufferChangeGeneration = 0;
|
||||
Array<Uint64, BufferBindPointTargets.size()> m_bindPointGeneration{};
|
||||
#endif
|
||||
UnorderedMap<Uint, SharedPtr<BufferObject>> m_bufferObjects;
|
||||
IndexGenerator<Uint> m_indexGenerator;
|
||||
Array<BindingSlot<BufferObject>, GlobalBufferTargets.size()> m_bindingSlots;
|
||||
// TODO: query the count somewhere globally?
|
||||
// For glBindBufferBase / glBindBufferRange
|
||||
|
||||
@@ -18,9 +18,52 @@
|
||||
#include <MG_Pipe/PipeMutation.h>
|
||||
|
||||
namespace MobileGL::MG_State {
|
||||
namespace {
|
||||
// Leak-at-exit storage; see GlobalObjects.cpp.
|
||||
SharedPtr<GLState::GLContext>& DefaultGLContextSlot() {
|
||||
static SharedPtr<GLState::GLContext>* slot = new SharedPtr<GLState::GLContext>();
|
||||
return *slot;
|
||||
}
|
||||
|
||||
SharedPtr<GLState::FramebufferObject>& DefaultFramebufferSlot() {
|
||||
static SharedPtr<GLState::FramebufferObject>* slot = new SharedPtr<GLState::FramebufferObject>();
|
||||
return *slot;
|
||||
}
|
||||
}
|
||||
|
||||
const SharedPtr<GLState::GLContext>& ProcessDefaultGLContext() {
|
||||
auto& slot = DefaultGLContextSlot();
|
||||
if (!slot) {
|
||||
slot = MakeShared<GLState::GLContext>();
|
||||
}
|
||||
return slot;
|
||||
}
|
||||
|
||||
void ReleaseProcessDefaultGLContext() {
|
||||
auto& slot = DefaultGLContextSlot();
|
||||
// Only the calling thread's TLS is reachable here, and only when it is still holding the
|
||||
// fallback rather than an EGL context of its own.
|
||||
if (pGLContext == slot) pGLContext.reset();
|
||||
DefaultFramebufferSlot().reset();
|
||||
slot.reset();
|
||||
}
|
||||
|
||||
void SetDefaultFramebuffer(SharedPtr<GLState::FramebufferObject> framebuffer) {
|
||||
DefaultFramebufferSlot() = Move(framebuffer);
|
||||
}
|
||||
|
||||
const SharedPtr<GLState::FramebufferObject>& DefaultFramebuffer() {
|
||||
return DefaultFramebufferSlot();
|
||||
}
|
||||
|
||||
void Init() {
|
||||
MGLOG_D("Initializing MobileGL State...");
|
||||
pGLContext = MakeUnique<GLState::GLContext>();
|
||||
// Rebuilt on every Initialize(): the fallback context starts empty, so a thread that
|
||||
// never crossed an eglMakeCurrent cannot see the previous run's objects. The default
|
||||
// framebuffer is rebuilt too - MG_Impl::Init() installs the new one right after.
|
||||
DefaultFramebufferSlot().reset();
|
||||
DefaultGLContextSlot() = MakeShared<GLState::GLContext>();
|
||||
pGLContext = ProcessDefaultGLContext();
|
||||
pEGLContext = MakeUnique<EGLState::EGLContext>();
|
||||
}
|
||||
|
||||
@@ -89,11 +132,11 @@ namespace MobileGL::MG_State {
|
||||
|
||||
// Buffer
|
||||
void GLContext::GenBufferNames(Uint number, Vector<Uint>& buffers) {
|
||||
m_bufferState.GenerateNames(number, buffers);
|
||||
Group().GenerateBufferNames(number, buffers);
|
||||
}
|
||||
|
||||
const SharedPtr<BufferObject>& GLContext::GetBufferObject(Uint index) {
|
||||
return m_bufferState.GetBufferObject(index);
|
||||
return Group().GetBufferObject(index);
|
||||
}
|
||||
|
||||
BindingSlot<BufferObject>& GLContext::GetBufferBindingSlot(BufferTarget target) {
|
||||
@@ -111,7 +154,7 @@ namespace MobileGL::MG_State {
|
||||
}
|
||||
|
||||
const SharedPtr<BufferObject>& GLContext::CreateBufferObject(Uint index) {
|
||||
return m_bufferState.CreateBufferObject(index);
|
||||
return Group().CreateBufferObject(index);
|
||||
}
|
||||
|
||||
void GLContext::MarkBufferObjectForDeletion(Uint index) {
|
||||
@@ -123,7 +166,7 @@ namespace MobileGL::MG_State {
|
||||
// previous every-VAO scan was wrong per spec and O(VAOs) per delete —
|
||||
// with one VAO per chunk section, vanilla's steady buffer churn made it
|
||||
// dominate the render thread and FPS decay over session time.
|
||||
auto bufferObject = m_bufferState.GetBufferObject(index);
|
||||
const auto bufferObject = Group().GetBufferObject(index);
|
||||
const auto& vao = m_vertexArrayState.GetBoundVertexArray();
|
||||
if (vao != nullptr) {
|
||||
if (vao->GetIndexBufferBindingSlot().GetBoundObject() == bufferObject) {
|
||||
@@ -135,17 +178,18 @@ namespace MobileGL::MG_State {
|
||||
}
|
||||
}
|
||||
}
|
||||
m_bufferState.UnbindBufferObject(bufferObject);
|
||||
}
|
||||
|
||||
m_bufferState.MarkBufferObjectForDeletion(index);
|
||||
Group().MarkBufferObjectForDeletion(index);
|
||||
}
|
||||
|
||||
Bool GLContext::ValidateBufferName(Uint index) const {
|
||||
return m_bufferState.ValidateName(index);
|
||||
return m_shareGroup->ValidateBufferName(index);
|
||||
}
|
||||
|
||||
Bool GLContext::ValidateBufferObject(Uint index) const {
|
||||
return m_bufferState.ValidateBufferObject(index);
|
||||
return m_shareGroup->ValidateBufferObject(index);
|
||||
}
|
||||
|
||||
// VertexArray
|
||||
@@ -269,11 +313,11 @@ namespace MobileGL::MG_State {
|
||||
|
||||
// Texture
|
||||
void GLContext::GenTextureNames(Uint number, Vector<Uint>& textures) {
|
||||
m_textureState.GenerateNames(number, textures);
|
||||
Group().GenerateTextureNames(number, textures);
|
||||
}
|
||||
|
||||
const SharedPtr<ITextureObject>& GLContext::GetTextureObject(Uint index) {
|
||||
return m_textureState.GetTextureObject(index);
|
||||
return Group().GetTextureObject(index);
|
||||
}
|
||||
|
||||
const SharedPtr<ITextureObject>& GLContext::GetDefaultTextureObject(TextureTarget target) const {
|
||||
@@ -281,14 +325,14 @@ namespace MobileGL::MG_State {
|
||||
}
|
||||
|
||||
const SharedPtr<ITextureObject>& GLContext::CreateTextureObject(Uint index, TextureTarget target) {
|
||||
return m_textureState.CreateTextureObject(index, target);
|
||||
return Group().CreateTextureObject(index, target);
|
||||
}
|
||||
|
||||
const SharedPtr<ITextureObject>& GLContext::CreateTextureViewObject(
|
||||
Uint index, TextureTarget target, const SharedPtr<ITextureObject>& storageOwner, Uint minLevel,
|
||||
Uint numLevels, Uint minLayer, Uint numLayers) {
|
||||
return m_textureState.CreateTextureViewObject(index, target, storageOwner, minLevel, numLevels, minLayer,
|
||||
numLayers);
|
||||
return Group().CreateTextureViewObject(index, target, storageOwner, minLevel, numLevels, minLayer,
|
||||
numLayers);
|
||||
}
|
||||
|
||||
void GLContext::MarkTextureObjectForDeletion(Uint index) {
|
||||
@@ -302,7 +346,8 @@ namespace MobileGL::MG_State {
|
||||
// contents rather than those of whatever the application put in its place - the name
|
||||
// it deleted usually comes straight back from the next glGenTextures, so the two are
|
||||
// indistinguishable from the outside (KHR-GL32.packed_pixels read a stale gradient).
|
||||
if (const auto& textureObject = m_textureState.GetTextureObject(index)) {
|
||||
const auto textureObject = Group().GetTextureObject(index);
|
||||
if (textureObject) {
|
||||
for (SizeT targetIndex = 0; targetIndex < SizeT(FramebufferTarget::FramebufferTargetCount);
|
||||
++targetIndex) {
|
||||
const auto& framebuffer =
|
||||
@@ -318,7 +363,10 @@ namespace MobileGL::MG_State {
|
||||
}
|
||||
}
|
||||
}
|
||||
m_textureState.MarkTextureObjectForDeletion(index, IsRelaxedSemanticsActive());
|
||||
// This context's units only: the object is gone for every context in the share
|
||||
// group, but each of them detaches it from its own bindings (GL 4.6 core 8.1).
|
||||
m_textureState.UnbindTextureFromUnits(textureObject);
|
||||
Group().MarkTextureObjectForDeletion(index, IsRelaxedSemanticsActive());
|
||||
}
|
||||
|
||||
TextureUnit& GLContext::GetTextureUnitObject(Int unit) {
|
||||
@@ -334,11 +382,11 @@ namespace MobileGL::MG_State {
|
||||
}
|
||||
|
||||
Bool GLContext::ValidateTextureName(Uint index) const {
|
||||
return m_textureState.ValidateName(index);
|
||||
return m_shareGroup->ValidateTextureName(index);
|
||||
}
|
||||
|
||||
Bool GLContext::ValidateTextureObject(Uint index) const {
|
||||
return m_textureState.ValidateTextureObject(index);
|
||||
return m_shareGroup->ValidateTextureObject(index);
|
||||
}
|
||||
|
||||
Int GLContext::GetActiveTextureUnit() const {
|
||||
@@ -351,51 +399,67 @@ namespace MobileGL::MG_State {
|
||||
|
||||
// Program
|
||||
Uint GLContext::CreateProgram() {
|
||||
return m_programState.CreateProgram();
|
||||
return Group().Programs().CreateProgram();
|
||||
}
|
||||
|
||||
Uint GLContext::CreateShader(const ShaderStage stage) {
|
||||
return m_programState.CreateShader(stage);
|
||||
return Group().Programs().CreateShader(stage);
|
||||
}
|
||||
|
||||
void GLContext::MarkProgramForDeletion(const Uint index) {
|
||||
return m_programState.MarkProgramObjectForDeletion(index);
|
||||
return Group().Programs().MarkProgramObjectForDeletion(index, m_currentProgram);
|
||||
}
|
||||
|
||||
void GLContext::MarkShaderForDeletion(const Uint index) {
|
||||
return m_programState.MarkShaderObjectForDeletion(index);
|
||||
return Group().Programs().MarkShaderObjectForDeletion(index, m_currentProgram);
|
||||
}
|
||||
|
||||
void GLContext::ReleaseShaderNameIfOrphaned(const Uint index) {
|
||||
return m_programState.ReleaseShaderNameIfOrphaned(index);
|
||||
return Group().Programs().ReleaseShaderNameIfOrphaned(index, m_currentProgram);
|
||||
}
|
||||
|
||||
Bool GLContext::ValidateProgramName(const Uint index) const {
|
||||
return m_programState.ValidateProgramObject(index);
|
||||
return m_shareGroup->Programs().ValidateProgramObject(index);
|
||||
}
|
||||
|
||||
Bool GLContext::ValidateShaderName(const Uint index) const {
|
||||
return m_programState.ValidateShaderObject(index);
|
||||
return m_shareGroup->Programs().ValidateShaderObject(index);
|
||||
}
|
||||
|
||||
const SharedPtr<ProgramObject>& GLContext::GetProgramObject(const Uint index) {
|
||||
return m_programState.GetProgramObject(index);
|
||||
return Group().Programs().GetProgramObject(index);
|
||||
}
|
||||
|
||||
const SharedPtr<ShaderObject>& GLContext::GetShaderObject(const Uint index) {
|
||||
return m_programState.GetShaderObject(index);
|
||||
return Group().Programs().GetShaderObject(index);
|
||||
}
|
||||
|
||||
void GLContext::JoinAllPendingShaderWork() {
|
||||
m_programState.JoinAllPendingWork();
|
||||
Group().Programs().JoinAllPendingWork(m_currentProgram);
|
||||
}
|
||||
|
||||
void GLContext::UseProgram(Uint program) {
|
||||
return m_programState.UseProgram(program);
|
||||
const SharedPtr<ProgramObject> previous = m_currentProgram;
|
||||
|
||||
if (program == 0) m_currentProgram.reset();
|
||||
|
||||
auto& programs = Group().Programs();
|
||||
if (programs.HasProgramSlot(program)) {
|
||||
m_currentProgram = programs.GetProgramObject(program);
|
||||
}
|
||||
|
||||
// A deletion flagged while the program was current takes effect the moment it
|
||||
// stops being current.
|
||||
if (previous != nullptr && previous != m_currentProgram && previous->GetDeleteStatus()) {
|
||||
const Uint previousName = previous->GetExternalIndex();
|
||||
if (programs.HasProgramSlot(previousName) && programs.GetProgramObject(previousName) == previous) {
|
||||
programs.MarkProgramObjectForDeletion(previousName, m_currentProgram);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const SharedPtr<ProgramObject>& GLContext::GetCurrentProgram() {
|
||||
return m_programState.GetCurrentProgram();
|
||||
return m_currentProgram;
|
||||
}
|
||||
|
||||
// Copies every default-block uniform value `source` holds into the same-named uniform of
|
||||
@@ -608,7 +672,7 @@ namespace MobileGL::MG_State {
|
||||
|
||||
const SharedPtr<ProgramObject>& GLContext::GetProgramForDraw() {
|
||||
static const SharedPtr<ProgramObject> nullProgram = nullptr;
|
||||
const auto& currentProgram = m_programState.GetCurrentProgram();
|
||||
const auto& currentProgram = GetCurrentProgram();
|
||||
if (currentProgram) {
|
||||
// P1 join site J1, plain glUseProgram half. The backends read a program's
|
||||
// lifetimeId / backendStateVersion / UBO content version to decide whether
|
||||
@@ -762,7 +826,7 @@ namespace MobileGL::MG_State {
|
||||
|
||||
const SharedPtr<ProgramObject>& GLContext::GetProgramForDispatch() {
|
||||
static const SharedPtr<ProgramObject> nullProgram = nullptr;
|
||||
const auto& currentProgram = m_programState.GetCurrentProgram();
|
||||
const auto& currentProgram = GetCurrentProgram();
|
||||
if (currentProgram) {
|
||||
// Same join contract as GetProgramForDraw's glUseProgram half - see the note
|
||||
// there. A dispatch reads the same non-artifact versions a draw does.
|
||||
@@ -784,7 +848,7 @@ namespace MobileGL::MG_State {
|
||||
}
|
||||
|
||||
const SharedPtr<ProgramObject>& GLContext::GetProgramForUniform() {
|
||||
const auto& currentProgram = m_programState.GetCurrentProgram();
|
||||
const auto& currentProgram = GetCurrentProgram();
|
||||
if (currentProgram) return currentProgram;
|
||||
static const SharedPtr<ProgramObject> nullProgram = nullptr;
|
||||
if (m_boundProgramPipeline == 0) return nullProgram;
|
||||
@@ -1220,21 +1284,21 @@ namespace MobileGL::MG_State {
|
||||
|
||||
// Sampler
|
||||
void GLContext::GenSamplerNames(Uint number, Vector<Uint>& samplers) {
|
||||
m_samplerState.GenerateNames(number, samplers);
|
||||
Group().Samplers().GenerateNames(number, samplers);
|
||||
}
|
||||
|
||||
const SharedPtr<SamplerObject>& GLContext::GetSamplerObject(Uint index) {
|
||||
return m_samplerState.GetSamplerObject(index);
|
||||
return Group().Samplers().GetSamplerObject(index);
|
||||
}
|
||||
|
||||
const SharedPtr<SamplerObject>& GLContext::CreateSamplerObject(Uint index) {
|
||||
return m_samplerState.CreateSamplerObject(index);
|
||||
return Group().Samplers().CreateSamplerObject(index);
|
||||
}
|
||||
|
||||
void GLContext::MarkSamplerObjectForDeletion(Uint index) {
|
||||
// Unbind the sampler from all texture units
|
||||
if (ValidateSamplerObject(index)) {
|
||||
auto sampler = m_samplerState.GetSamplerObject(index);
|
||||
auto sampler = Group().Samplers().GetSamplerObject(index);
|
||||
for (Int unit = 0; unit < TextureState::MAX_TEXTURE_IMAGE_UNITS; ++unit) {
|
||||
auto& textureUnit = m_textureState.GetUnitObject(unit);
|
||||
if (textureUnit.GetSamplerObject() == sampler) {
|
||||
@@ -1242,24 +1306,24 @@ namespace MobileGL::MG_State {
|
||||
}
|
||||
}
|
||||
}
|
||||
m_samplerState.MarkSamplerObjectForDeletion(index);
|
||||
Group().Samplers().MarkSamplerObjectForDeletion(index);
|
||||
}
|
||||
|
||||
Bool GLContext::ValidateSamplerName(Uint index) const {
|
||||
return m_samplerState.ValidateName(index);
|
||||
return m_shareGroup->Samplers().ValidateName(index);
|
||||
}
|
||||
|
||||
Bool GLContext::ValidateSamplerObject(Uint index) const {
|
||||
return m_samplerState.ValidateSamplerObject(index);
|
||||
return m_shareGroup->Samplers().ValidateSamplerObject(index);
|
||||
}
|
||||
|
||||
// Renderbuffer
|
||||
void GLContext::GenRenderbufferNames(Uint number, Vector<Uint>& renderbuffers) {
|
||||
m_renderbufferState.GenerateNames(number, renderbuffers);
|
||||
Group().GenerateRenderbufferNames(number, renderbuffers);
|
||||
}
|
||||
|
||||
const SharedPtr<RenderbufferObject>& GLContext::GetRenderbufferObject(Uint index) {
|
||||
return m_renderbufferState.GetRenderbufferObject(index);
|
||||
return Group().GetRenderbufferObject(index);
|
||||
}
|
||||
|
||||
BindingSlot<RenderbufferObject>& GLContext::GetRenderbufferBindingSlot(RenderbufferTarget target) {
|
||||
@@ -1267,19 +1331,21 @@ namespace MobileGL::MG_State {
|
||||
}
|
||||
|
||||
const SharedPtr<RenderbufferObject>& GLContext::CreateRenderbufferObject(Uint index) {
|
||||
return m_renderbufferState.CreateRenderbufferObject(index);
|
||||
return Group().CreateRenderbufferObject(index);
|
||||
}
|
||||
|
||||
void GLContext::MarkRenderbufferObjectForDeletion(Uint index) {
|
||||
m_renderbufferState.MarkRenderbufferObjectForDeletion(index);
|
||||
const auto renderbufferObject = Group().GetRenderbufferObject(index);
|
||||
m_renderbufferState.UnbindRenderbufferObject(renderbufferObject);
|
||||
Group().MarkRenderbufferObjectForDeletion(index);
|
||||
}
|
||||
|
||||
Bool GLContext::ValidateRenderbufferName(Uint index) const {
|
||||
return m_renderbufferState.ValidateName(index);
|
||||
return m_shareGroup->ValidateRenderbufferName(index);
|
||||
}
|
||||
|
||||
Bool GLContext::ValidateRenderbufferObject(Uint index) const {
|
||||
return m_renderbufferState.ValidateRenderbufferObject(index);
|
||||
return m_shareGroup->ValidateRenderbufferObject(index);
|
||||
}
|
||||
|
||||
Uint64 GLContext::AllocateTransformFeedbackLifetimeId() {
|
||||
@@ -1288,12 +1354,23 @@ namespace MobileGL::MG_State {
|
||||
return nextId.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
GLContext::GLContext() {
|
||||
GLContext::GLContext() : GLContext(MakeShared<ShareGroupState>()) {}
|
||||
|
||||
GLContext::GLContext(SharedPtr<ShareGroupState> shareGroup)
|
||||
: m_shareGroup(shareGroup ? Move(shareGroup) : MakeShared<ShareGroupState>()) {
|
||||
// The default transform feedback object (name 0) exists from the start of the context
|
||||
// (GL 4.6 core 13.2.1), but nothing binds it, so nothing else would materialise it.
|
||||
// Materialising it here is what lets GetBoundTransformFeedbackLifetimeId() be a plain
|
||||
// const read instead of an operator[] insert on the draw path.
|
||||
m_boundTransformFeedbackLifetimeId = m_transformFeedbackObjects[0].lifetimeId;
|
||||
InstallDefaultFramebuffer(DefaultFramebuffer());
|
||||
}
|
||||
|
||||
void GLContext::InstallDefaultFramebuffer(const SharedPtr<FramebufferObject>& framebuffer) {
|
||||
if (!framebuffer) return;
|
||||
m_framebufferState.RegisterFramebufferObject(0, framebuffer);
|
||||
m_framebufferState.GetBindingSlot(FramebufferTarget::Draw).Bind(framebuffer);
|
||||
m_framebufferState.GetBindingSlot(FramebufferTarget::Read).Bind(framebuffer);
|
||||
}
|
||||
|
||||
Bool GLContext::HasOpenTransformFeedbackSpan(Uint64 lifetimeId) const {
|
||||
@@ -1562,5 +1639,5 @@ namespace MobileGL::MG_State {
|
||||
} // namespace GLState
|
||||
|
||||
// Leak-at-exit storage; see GlobalObjects.cpp.
|
||||
UniquePtr<GLState::GLContext>& pGLContext = *new UniquePtr<GLState::GLContext>();
|
||||
thread_local SharedPtr<GLState::GLContext> pGLContext;
|
||||
} // namespace MobileGL::MG_State
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "ProgramState/ProgramState.h"
|
||||
#include "ProgramState/ProgramPipelineObject.h"
|
||||
#include "SamplerState/SamplerState.h"
|
||||
#include "ShareGroupState.h"
|
||||
#include "TextureState/TextureState.h"
|
||||
#include "FramebufferState/FramebufferState.h"
|
||||
#include "VertexArrayState/VertexArrayState.h"
|
||||
@@ -76,7 +77,19 @@ namespace MobileGL {
|
||||
|
||||
class GLContext {
|
||||
public:
|
||||
// A context with a share group of its own. Created through EGL, a context
|
||||
// either starts a new group or joins the one its share context belongs to;
|
||||
// this constructor is the standalone shape the tests and the process default
|
||||
// context use.
|
||||
GLContext();
|
||||
explicit GLContext(SharedPtr<ShareGroupState> shareGroup);
|
||||
|
||||
// The GL objects this context shares with every context that shares with it.
|
||||
const SharedPtr<ShareGroupState>& GetShareGroup() const { return m_shareGroup; }
|
||||
|
||||
// Installs the process-wide default framebuffer (GL name 0) into this context:
|
||||
// its table entry and the Draw/Read bindings the context starts with.
|
||||
void InstallDefaultFramebuffer(const SharedPtr<FramebufferObject>& framebuffer);
|
||||
|
||||
// Error
|
||||
void RecordError(ErrorCode code, UniquePtr<ErrorInfo> info);
|
||||
@@ -188,7 +201,7 @@ namespace MobileGL {
|
||||
// P1 stage 6: the per-context index of adoptable compile nodes, for its
|
||||
// adoption counter. Diagnostics and tests only - no GL entry point reads it.
|
||||
ShaderCompileAdoptionMap& GetShaderCompileAdoptionMap() {
|
||||
return m_programState.GetShaderCompileAdoptionMap();
|
||||
return Group().Programs().GetShaderCompileAdoptionMap();
|
||||
}
|
||||
void UseProgram(Uint program);
|
||||
const SharedPtr<ProgramObject>& GetCurrentProgram();
|
||||
@@ -606,6 +619,10 @@ namespace MobileGL {
|
||||
void InvalidateCompileEnv();
|
||||
|
||||
private:
|
||||
ShareGroupState& Group() const { return *m_shareGroup; }
|
||||
|
||||
// The GL objects shared with every context that shares with this one.
|
||||
SharedPtr<ShareGroupState> m_shareGroup;
|
||||
// State Components
|
||||
ErrorState m_errorState;
|
||||
BufferState m_bufferState;
|
||||
@@ -685,10 +702,11 @@ namespace MobileGL {
|
||||
IndexGenerator<Uint> m_programPipelineNames;
|
||||
Uint m_boundProgramPipeline = 0;
|
||||
TextureState m_textureState;
|
||||
ProgramState m_programState;
|
||||
// The program this context has in use. Program STATE (the objects) belongs to
|
||||
// the share group; which of them is current does not.
|
||||
SharedPtr<ProgramObject> m_currentProgram;
|
||||
RenderState m_renderState;
|
||||
FramebufferState m_framebufferState;
|
||||
SamplerState m_samplerState;
|
||||
RenderbufferState m_renderbufferState;
|
||||
|
||||
mutable SharedPtr<const MG_Util::ShaderTranspiler::CompileEnv> m_compileEnv;
|
||||
@@ -698,7 +716,32 @@ namespace MobileGL {
|
||||
};
|
||||
} // namespace GLState
|
||||
|
||||
extern UniquePtr<GLState::GLContext>& pGLContext;
|
||||
// P14 S3 (docs/Disaggregated/design/11-state-ownership.md): the GL state the CALLING
|
||||
// THREAD's GL entry points read and write. eglMakeCurrent points it at that EGL
|
||||
// context's GLContext, and the EGL release edge puts it back to whatever it was, so a
|
||||
// thread that never crossed an eglMakeCurrent keeps the process default MG_State::Init()
|
||||
// gave it. NULL on a thread for which neither has happened, the same answer a process
|
||||
// that never reached MG_State::Init() (the spawn server image) has always given.
|
||||
extern thread_local SharedPtr<GLState::GLContext> pGLContext;
|
||||
|
||||
// The fallback pGLContext starts at and an EGL release edge restores to: the process's
|
||||
// one context for every thread that never bound an EGL context to itself, created on
|
||||
// demand and owned here (leak-at-exit, like every other process global).
|
||||
const SharedPtr<GLState::GLContext>& ProcessDefaultGLContext();
|
||||
|
||||
// Drops the process default context and this thread's binding to it, and the default
|
||||
// framebuffer with them. Called on the teardown edge (the last eglTerminate, before
|
||||
// MobileGL::Destroy()) so the objects MG_State::Init() built do not outlive
|
||||
// glslang::FinalizeProcess() through a thread that kept its fallback binding.
|
||||
void ReleaseProcessDefaultGLContext();
|
||||
|
||||
// P14 S3. GL framebuffer 0, built by MG_Impl once the backend is up (it needs texture
|
||||
// objects, which need the backend's resource plumbing) and handed back here so every
|
||||
// GLContext a later eglCreateContext builds starts with the same default framebuffer
|
||||
// bound. Until the backend grows per-context native tuples there is exactly one such
|
||||
// object, which is what the backend's own default-framebuffer identity tests assume.
|
||||
void SetDefaultFramebuffer(SharedPtr<GLState::FramebufferObject> framebuffer);
|
||||
const SharedPtr<GLState::FramebufferObject>& DefaultFramebuffer();
|
||||
|
||||
// True when relaxed GL semantics apply. Strict core rules are enforced only when the
|
||||
// current EGL context explicitly requested a core profile (core bit in
|
||||
|
||||
@@ -46,6 +46,14 @@ namespace MobileGL::MG_State::GLState {
|
||||
return framebufferObject;
|
||||
}
|
||||
|
||||
void FramebufferState::RegisterFramebufferObject(Uint index, SharedPtr<FramebufferObject> object) {
|
||||
if (!object) return;
|
||||
if (index == 0 && !m_indexGenerator.IsValid(0)) {
|
||||
m_indexGenerator.Insert(0);
|
||||
}
|
||||
m_framebufferObjects[index] = Move(object);
|
||||
}
|
||||
|
||||
BindingSlot<FramebufferObject>& FramebufferState::GetBindingSlot(FramebufferTarget target) {
|
||||
for (auto& bindingSlot : m_bindingSlots) {
|
||||
if (bindingSlot.GetTarget() == target) {
|
||||
|
||||
@@ -20,6 +20,9 @@ namespace MobileGL::MG_State::GLState {
|
||||
const SharedPtr<FramebufferObject>& GetFramebufferObject(Uint index);
|
||||
void GenerateNames(Uint number, Vector<Uint>& framebuffers);
|
||||
const SharedPtr<FramebufferObject>& CreateFramebufferObject(Uint index);
|
||||
// Puts an already-built object at `index` without the "name 0 is created once" rule,
|
||||
// so a context can adopt the process-wide default framebuffer.
|
||||
void RegisterFramebufferObject(Uint index, SharedPtr<FramebufferObject> object);
|
||||
BindingSlot<FramebufferObject>& GetBindingSlot(FramebufferTarget target);
|
||||
void MarkFramebufferObjectForDeletion(Uint index);
|
||||
Bool ValidateName(Uint index) const;
|
||||
|
||||
@@ -25,19 +25,24 @@ namespace MobileGL::MG_State::GLState {
|
||||
return m_programObjects[id];
|
||||
}
|
||||
|
||||
void ProgramState::MarkProgramObjectForDeletion(const Uint program) {
|
||||
Bool ProgramState::HasProgramSlot(const Uint program) const {
|
||||
return CheckIndexAvail(program, m_programObjects);
|
||||
}
|
||||
|
||||
void ProgramState::MarkProgramObjectForDeletion(const Uint program,
|
||||
const SharedPtr<ProgramObject>& currentProgram) {
|
||||
if (!CheckIndexAvail(program, m_programObjects)) return; // FIXME: add error reporting here
|
||||
auto& programObject = m_programObjects[program];
|
||||
if (programObject != nullptr) {
|
||||
programObject->MarkAsDeleted();
|
||||
// A program in use is only FLAGGED: its name (and every program query) stays
|
||||
// valid until it stops being current, at which point UseProgram finishes the job.
|
||||
if (programObject == m_currentProgram) return;
|
||||
DestroyProgramSlot(program);
|
||||
if (programObject == currentProgram) return;
|
||||
DestroyProgramSlot(program, currentProgram);
|
||||
}
|
||||
}
|
||||
|
||||
void ProgramState::DestroyProgramSlot(const Uint program) {
|
||||
void ProgramState::DestroyProgramSlot(const Uint program, const SharedPtr<ProgramObject>& currentProgram) {
|
||||
auto& programObject = m_programObjects[program];
|
||||
// P1 join site J4/J5 (glDeleteProgram, and the deferred destroy UseProgram performs
|
||||
// when a deletion-flagged program stops being current). The program's name is about
|
||||
@@ -55,7 +60,7 @@ namespace MobileGL::MG_State::GLState {
|
||||
for (const auto& shader : attachedShaders) {
|
||||
const Uint shaderName = shader->GetExternalIndex();
|
||||
if (CheckIndexAvail(shaderName, m_shaderObjects) && m_shaderObjects[shaderName] == shader) {
|
||||
ReleaseShaderNameIfOrphaned(shaderName);
|
||||
ReleaseShaderNameIfOrphaned(shaderName, currentProgram);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,25 +69,6 @@ namespace MobileGL::MG_State::GLState {
|
||||
return CheckIndexAvail(program, m_programObjects) && m_programObjects[program] != nullptr;
|
||||
}
|
||||
|
||||
void ProgramState::UseProgram(Uint program) {
|
||||
const SharedPtr<ProgramObject> previous = m_currentProgram;
|
||||
|
||||
if (program == 0) m_currentProgram.reset();
|
||||
|
||||
if (CheckIndexAvail(program, m_programObjects)) {
|
||||
m_currentProgram = m_programObjects[program];
|
||||
}
|
||||
|
||||
// A deletion flagged while the program was current takes effect the moment it
|
||||
// stops being current.
|
||||
if (previous != nullptr && previous != m_currentProgram && previous->GetDeleteStatus()) {
|
||||
const Uint previousName = previous->GetExternalIndex();
|
||||
if (CheckIndexAvail(previousName, m_programObjects) && m_programObjects[previousName] == previous) {
|
||||
DestroyProgramSlot(previousName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Uint ProgramState::CreateShader(ShaderStage stage) {
|
||||
Uint shaderId = 0;
|
||||
m_programShaderNameGenerator.Generate(1, &shaderId);
|
||||
@@ -100,7 +86,7 @@ namespace MobileGL::MG_State::GLState {
|
||||
return m_shaderObjects[shader];
|
||||
}
|
||||
|
||||
void ProgramState::JoinAllPendingWork() {
|
||||
void ProgramState::JoinAllPendingWork(const SharedPtr<ProgramObject>& currentProgram) {
|
||||
// Programs first: a link joins the compiles it depends on, so the shader pass that
|
||||
// follows finds most of them already settled. The reverse order would be correct but
|
||||
// would wait on each compile twice - once here, once inside the link's own prologue.
|
||||
@@ -126,10 +112,10 @@ namespace MobileGL::MG_State::GLState {
|
||||
// The currently-used program is reachable through m_programObjects unless
|
||||
// glDeleteProgram already freed its slot while it stayed current. Nothing else holds
|
||||
// a GL-visible name for it, but a draw would still join it, so settle it here too.
|
||||
if (m_currentProgram) m_currentProgram->JoinLinkAndSpirv();
|
||||
if (currentProgram) currentProgram->JoinLinkAndSpirv();
|
||||
}
|
||||
|
||||
void ProgramState::MarkShaderObjectForDeletion(Uint shader) {
|
||||
void ProgramState::MarkShaderObjectForDeletion(Uint shader, const SharedPtr<ProgramObject>& currentProgram) {
|
||||
if (!CheckIndexAvail(shader, m_shaderObjects)) return;
|
||||
auto& shaderObject = m_shaderObjects[shader];
|
||||
if (shaderObject != nullptr) {
|
||||
@@ -138,11 +124,12 @@ namespace MobileGL::MG_State::GLState {
|
||||
// from every program. The GL CTS compiles shaders through exactly this
|
||||
// create-attach-delete-source-compile sequence (uniform_block.common.name_matching).
|
||||
shaderObject->MarkAsDeleted();
|
||||
ReleaseShaderNameIfOrphaned(shader);
|
||||
ReleaseShaderNameIfOrphaned(shader, currentProgram);
|
||||
}
|
||||
}
|
||||
|
||||
Bool ProgramState::ShaderHasGLVisibleAttachment(const SharedPtr<ShaderObject>& shaderObject) const {
|
||||
Bool ProgramState::ShaderHasGLVisibleAttachment(const SharedPtr<ShaderObject>& shaderObject,
|
||||
const SharedPtr<ProgramObject>& currentProgram) const {
|
||||
for (const auto& programObject : m_programObjects) {
|
||||
if (programObject != nullptr && programObject->ShaderIsAttachedGLVisible(shaderObject)) {
|
||||
return true;
|
||||
@@ -150,14 +137,14 @@ namespace MobileGL::MG_State::GLState {
|
||||
}
|
||||
// A program deleted while current vacates its table slot but stays alive as the
|
||||
// current program; its attachments still count.
|
||||
return m_currentProgram != nullptr && m_currentProgram->ShaderIsAttachedGLVisible(shaderObject);
|
||||
return currentProgram != nullptr && currentProgram->ShaderIsAttachedGLVisible(shaderObject);
|
||||
}
|
||||
|
||||
void ProgramState::ReleaseShaderNameIfOrphaned(Uint shader) {
|
||||
void ProgramState::ReleaseShaderNameIfOrphaned(Uint shader, const SharedPtr<ProgramObject>& currentProgram) {
|
||||
if (!CheckIndexAvail(shader, m_shaderObjects)) return;
|
||||
auto& shaderObject = m_shaderObjects[shader];
|
||||
if (shaderObject == nullptr || !shaderObject->GetDeleteStatus()) return;
|
||||
if (ShaderHasGLVisibleAttachment(shaderObject)) return;
|
||||
if (ShaderHasGLVisibleAttachment(shaderObject, currentProgram)) return;
|
||||
// The name is about to go, so nothing can observe this shader's compile through THIS
|
||||
// object any more and a job still in flight for it is pure waste - unless another
|
||||
// shader object adopted the same node (stage 6) or a pending link pinned it, which is
|
||||
|
||||
@@ -14,28 +14,31 @@
|
||||
#include "ShaderPreprocessCache.h"
|
||||
|
||||
namespace MobileGL::MG_State::GLState {
|
||||
// The program and shader objects of one share group, and the name space they share. The
|
||||
// program a context has in use is context state and lives on GLContext - the operations
|
||||
// that need it (a delete of the current program, the deferred destroy UseProgram
|
||||
// performs, the orphan test) take it as a parameter.
|
||||
class ProgramState {
|
||||
public:
|
||||
// This function WILL actually create the program object.
|
||||
// To retrieve created program object, use GetProgramObject()
|
||||
Uint CreateProgram();
|
||||
const SharedPtr<ProgramObject>& GetProgramObject(Uint id);
|
||||
void MarkProgramObjectForDeletion(Uint program);
|
||||
void MarkProgramObjectForDeletion(Uint program, const SharedPtr<ProgramObject>& currentProgram);
|
||||
Bool ValidateProgramObject(Uint program) const;
|
||||
|
||||
void UseProgram(Uint program);
|
||||
// Whether `program` names a slot in the table at all, i.e. in range - not whether that
|
||||
// slot currently holds an object.
|
||||
Bool HasProgramSlot(Uint program) const;
|
||||
|
||||
Uint CreateShader(ShaderStage stage);
|
||||
const SharedPtr<ShaderObject>& GetShaderObject(Uint shader);
|
||||
void MarkShaderObjectForDeletion(Uint shader);
|
||||
void MarkShaderObjectForDeletion(Uint shader, const SharedPtr<ProgramObject>& currentProgram);
|
||||
// Frees a deletion-flagged shader's name once no program holds a GL-visible
|
||||
// attachment to it (the deferred half of glDeleteShader-while-attached).
|
||||
void ReleaseShaderNameIfOrphaned(Uint shader);
|
||||
void ReleaseShaderNameIfOrphaned(Uint shader, const SharedPtr<ProgramObject>& currentProgram);
|
||||
Bool ValidateShaderObject(Uint shader) const;
|
||||
|
||||
const SharedPtr<ProgramObject>& GetCurrentProgram() const { return m_currentProgram; }
|
||||
|
||||
// Joins every outstanding compile and link this context still owns, publishing each
|
||||
// Joins every outstanding compile and link this share group still owns, publishing each
|
||||
// one's artifacts through the ordinary gates. The single caller is
|
||||
// glMaxShaderCompilerThreadsKHR(0): GL_KHR_parallel_shader_compile requires a zero
|
||||
// count to leave nothing in flight, so that every subsequent
|
||||
@@ -46,7 +49,7 @@ namespace MobileGL::MG_State::GLState {
|
||||
// compilation straight back. Nodes belonging to objects this context has already
|
||||
// dropped are not joined - nothing can observe them, and waiting on them would make
|
||||
// a GL call's cost depend on garbage.
|
||||
void JoinAllPendingWork();
|
||||
void JoinAllPendingWork(const SharedPtr<ProgramObject>& currentProgram);
|
||||
|
||||
// P0b layer 2. Exposed for tests and diagnostics; the GL frontend never touches it
|
||||
// directly - shader objects reach it through the pointer they are handed at
|
||||
@@ -59,10 +62,11 @@ namespace MobileGL::MG_State::GLState {
|
||||
ShaderCompileAdoptionMap& GetShaderCompileAdoptionMap() { return *m_shaderCompileAdoptionMap; }
|
||||
|
||||
private:
|
||||
Bool ShaderHasGLVisibleAttachment(const SharedPtr<ShaderObject>& shaderObject) const;
|
||||
Bool ShaderHasGLVisibleAttachment(const SharedPtr<ShaderObject>& shaderObject,
|
||||
const SharedPtr<ProgramObject>& currentProgram) const;
|
||||
// Frees the name slot and releases orphaned attached shaders; the immediate half
|
||||
// of glDeleteProgram (deferred while the program is current).
|
||||
void DestroyProgramSlot(Uint program);
|
||||
void DestroyProgramSlot(Uint program, const SharedPtr<ProgramObject>& currentProgram);
|
||||
|
||||
template <typename T>
|
||||
static Bool CheckIndexAvail(const SizeT idx, const Vector<T>& vec) {
|
||||
@@ -96,7 +100,5 @@ namespace MobileGL::MG_State::GLState {
|
||||
|
||||
Vector<SharedPtr<ProgramObject>> m_programObjects;
|
||||
Vector<SharedPtr<ShaderObject>> m_shaderObjects;
|
||||
|
||||
SharedPtr<ProgramObject> m_currentProgram;
|
||||
};
|
||||
} // namespace MobileGL::MG_State::GLState
|
||||
|
||||
@@ -10,34 +10,12 @@
|
||||
#include "MG_Util/Types.h"
|
||||
|
||||
namespace MobileGL::MG_State::GLState {
|
||||
RenderbufferState::RenderbufferState() : m_indexGenerator(1024, 1) {
|
||||
RenderbufferState::RenderbufferState() {
|
||||
for (SizeT i = 0; i < m_bindingSlots.size(); ++i) {
|
||||
m_bindingSlots[i] = BindingSlot<RenderbufferObject>(static_cast<RenderbufferTarget>(i));
|
||||
}
|
||||
}
|
||||
|
||||
const SharedPtr<RenderbufferObject>& RenderbufferState::GetRenderbufferObject(Uint index) {
|
||||
auto it = m_renderbufferObjects.find(index);
|
||||
if (it != m_renderbufferObjects.end()) {
|
||||
return it->second;
|
||||
}
|
||||
static SharedPtr<RenderbufferObject> nullRenderbufferObject = nullptr;
|
||||
return nullRenderbufferObject;
|
||||
}
|
||||
|
||||
void RenderbufferState::GenerateNames(Uint number, Vector<Uint>& renderbuffers) {
|
||||
renderbuffers.resize(number);
|
||||
m_indexGenerator.Generate(number, renderbuffers.data());
|
||||
}
|
||||
|
||||
const SharedPtr<RenderbufferObject>& RenderbufferState::CreateRenderbufferObject(Uint index) {
|
||||
auto& bufferObject = m_renderbufferObjects[index];
|
||||
if (!bufferObject) {
|
||||
bufferObject = MakeShared<RenderbufferObject>(index);
|
||||
}
|
||||
return bufferObject;
|
||||
}
|
||||
|
||||
BindingSlot<RenderbufferObject>& RenderbufferState::GetBindingSlot(RenderbufferTarget target) {
|
||||
for (auto& bindingSlot : m_bindingSlots) {
|
||||
if (bindingSlot.GetTarget() == target) {
|
||||
@@ -48,26 +26,12 @@ namespace MobileGL::MG_State::GLState {
|
||||
return m_bindingSlots[0];
|
||||
}
|
||||
|
||||
void RenderbufferState::MarkRenderbufferObjectForDeletion(Uint index) {
|
||||
if (m_indexGenerator.IsValid(index)) {
|
||||
auto it = m_renderbufferObjects.find(index);
|
||||
if (it != m_renderbufferObjects.end()) {
|
||||
for (auto& bindingSlot : m_bindingSlots) {
|
||||
if (bindingSlot.GetBoundObject() == it->second) {
|
||||
bindingSlot.Bind(nullptr);
|
||||
}
|
||||
}
|
||||
m_renderbufferObjects.erase(index);
|
||||
void RenderbufferState::UnbindRenderbufferObject(const SharedPtr<RenderbufferObject>& renderbufferObject) {
|
||||
if (!renderbufferObject) return;
|
||||
for (auto& bindingSlot : m_bindingSlots) {
|
||||
if (bindingSlot.GetBoundObject() == renderbufferObject) {
|
||||
bindingSlot.Bind(nullptr);
|
||||
}
|
||||
m_indexGenerator.Delete(index);
|
||||
}
|
||||
}
|
||||
|
||||
Bool RenderbufferState::ValidateName(Uint index) const {
|
||||
return m_indexGenerator.IsValid(index);
|
||||
}
|
||||
|
||||
Bool RenderbufferState::ValidateRenderbufferObject(Uint index) const {
|
||||
return m_renderbufferObjects.find(index) != m_renderbufferObjects.end();
|
||||
}
|
||||
} // namespace MobileGL::MG_State::GLState
|
||||
|
||||
@@ -12,21 +12,16 @@
|
||||
#include "RenderbufferObject.h"
|
||||
|
||||
namespace MobileGL::MG_State::GLState {
|
||||
// The per-context half of the renderbuffer state: which renderbuffer is bound. The
|
||||
// objects and their names belong to the share group (ShareGroupState).
|
||||
class RenderbufferState {
|
||||
public:
|
||||
RenderbufferState();
|
||||
|
||||
const SharedPtr<RenderbufferObject>& GetRenderbufferObject(Uint index);
|
||||
void GenerateNames(Uint number, Vector<Uint>& renderbuffers);
|
||||
const SharedPtr<RenderbufferObject>& CreateRenderbufferObject(Uint index);
|
||||
BindingSlot<RenderbufferObject>& GetBindingSlot(RenderbufferTarget target);
|
||||
void MarkRenderbufferObjectForDeletion(Uint index);
|
||||
Bool ValidateName(Uint index) const;
|
||||
Bool ValidateRenderbufferObject(Uint index) const;
|
||||
void UnbindRenderbufferObject(const SharedPtr<RenderbufferObject>& renderbufferObject);
|
||||
|
||||
private:
|
||||
UnorderedMap<Uint, SharedPtr<RenderbufferObject>> m_renderbufferObjects;
|
||||
IndexGenerator<Uint> m_indexGenerator;
|
||||
Array<BindingSlot<RenderbufferObject>, static_cast<SizeT>(RenderbufferTarget::RenderbufferTargetCount)>
|
||||
m_bindingSlots;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,188 @@
|
||||
// MobileGL - MobileGL/MG_State/GLState/ShareGroupState.cpp
|
||||
// Copyright (c) 2025-2026 MobileGL-Dev
|
||||
// Licensed under the GNU Lesser General Public License v3.0:
|
||||
// https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
// https://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// End of Source File Header
|
||||
|
||||
#include "ShareGroupState.h"
|
||||
|
||||
#include "TextureState/TextureObject1D.h"
|
||||
#include "TextureState/TextureObject2D.h"
|
||||
#include "TextureState/TextureObject3D.h"
|
||||
#include "TextureState/TextureObject2DCube.h"
|
||||
#include "TextureState/TextureObjectBuffer.h"
|
||||
#include "TextureState/TextureObjectStubs.h"
|
||||
#include "TextureState/TextureObjectView.h"
|
||||
|
||||
namespace MobileGL::MG_State::GLState {
|
||||
SharedPtr<ITextureObject> MakeTextureObjectForTarget(Uint index, TextureTarget target) {
|
||||
switch (target) {
|
||||
case TextureTarget::Texture1D:
|
||||
return MakeShared<TextureObject1D>(index);
|
||||
case TextureTarget::TextureCubeMap:
|
||||
return MakeShared<TextureObject2DCube>(index);
|
||||
case TextureTarget::Texture2D:
|
||||
return MakeShared<TextureObject2D>(index);
|
||||
case TextureTarget::Texture3D:
|
||||
return MakeShared<TextureObject3D>(index);
|
||||
case TextureTarget::TextureBuffer:
|
||||
return MakeShared<TextureObjectBuffer>(index);
|
||||
|
||||
// These texture types are still stubbed:
|
||||
case TextureTarget::TextureRectangle:
|
||||
return MakeShared<TextureObjectRectangle>(index);
|
||||
case TextureTarget::Texture2DMultisample:
|
||||
return MakeShared<TextureObject2DMultisample>(index);
|
||||
case TextureTarget::Texture1DArray:
|
||||
return MakeShared<TextureObject1DArray>(index);
|
||||
case TextureTarget::Texture2DArray:
|
||||
return MakeShared<TextureObject2DArray>(index);
|
||||
case TextureTarget::TextureCubeMapArray:
|
||||
return MakeShared<TextureObjectCubeMapArray>(index);
|
||||
case TextureTarget::Texture2DMultisampleArray:
|
||||
return MakeShared<TextureObject2DMultisampleArray>(index);
|
||||
default:
|
||||
MOBILEGL_ASSERT(false, "Unimplemented texture type when creating texture object!: %d", (int)target);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// Buffer
|
||||
const SharedPtr<BufferObject>& ShareGroupState::GetBufferObject(Uint index) {
|
||||
auto it = m_bufferObjects.find(index);
|
||||
if (it != m_bufferObjects.end()) {
|
||||
return it->second;
|
||||
}
|
||||
static SharedPtr<BufferObject> nullBufferObject = nullptr;
|
||||
return nullBufferObject;
|
||||
}
|
||||
|
||||
void ShareGroupState::GenerateBufferNames(Uint number, Vector<Uint>& buffers) {
|
||||
buffers.resize(number);
|
||||
m_bufferNames.Generate(number, buffers.data());
|
||||
}
|
||||
|
||||
const SharedPtr<BufferObject>& ShareGroupState::CreateBufferObject(Uint index) {
|
||||
auto& bufferObj = m_bufferObjects[index];
|
||||
if (!bufferObj) {
|
||||
bufferObj = MakeShared<BufferObject>(index);
|
||||
}
|
||||
return bufferObj;
|
||||
}
|
||||
|
||||
void ShareGroupState::MarkBufferObjectForDeletion(Uint index) {
|
||||
if (m_bufferNames.IsValid(index)) {
|
||||
m_bufferObjects.erase(index);
|
||||
m_bufferNames.Delete(index);
|
||||
}
|
||||
}
|
||||
|
||||
Bool ShareGroupState::ValidateBufferName(Uint index) const {
|
||||
return m_bufferNames.IsValid(index);
|
||||
}
|
||||
|
||||
Bool ShareGroupState::ValidateBufferObject(Uint index) const {
|
||||
return m_bufferObjects.find(index) != m_bufferObjects.end();
|
||||
}
|
||||
|
||||
// Texture
|
||||
void ShareGroupState::GenerateTextureNames(Uint number, Vector<Uint>& textures) {
|
||||
textures.resize(number);
|
||||
m_textureNames.Generate(number, textures.data());
|
||||
}
|
||||
|
||||
const SharedPtr<ITextureObject>& ShareGroupState::GetTextureObject(Uint index) {
|
||||
auto it = m_textureObjects.find(index);
|
||||
if (it != m_textureObjects.end()) {
|
||||
return it->second;
|
||||
}
|
||||
static SharedPtr<ITextureObject> nullTextureObject = nullptr;
|
||||
return nullTextureObject;
|
||||
}
|
||||
|
||||
const SharedPtr<ITextureObject>& ShareGroupState::CreateTextureObject(Uint index, TextureTarget target) {
|
||||
auto& textureObject = m_textureObjects[index];
|
||||
textureObject = MakeTextureObjectForTarget(index, target);
|
||||
if (!textureObject) {
|
||||
static SharedPtr<ITextureObject> nullTextureObject = nullptr;
|
||||
return nullTextureObject;
|
||||
}
|
||||
return textureObject;
|
||||
}
|
||||
|
||||
const SharedPtr<ITextureObject>& ShareGroupState::CreateTextureViewObject(
|
||||
Uint index, TextureTarget target, const SharedPtr<ITextureObject>& storageOwner, Uint minLevel,
|
||||
Uint numLevels, Uint minLayer, Uint numLayers) {
|
||||
MOBILEGL_ASSERT(storageOwner != nullptr, "CreateTextureViewObject: storage owner is null");
|
||||
auto& textureObject = m_textureObjects[index];
|
||||
textureObject = MakeShared<TextureObjectView>(index, target, storageOwner, minLevel, numLevels, minLayer,
|
||||
numLayers);
|
||||
return textureObject;
|
||||
}
|
||||
|
||||
void ShareGroupState::MarkTextureObjectForDeletion(Uint index, Bool keepUnboundReservation) {
|
||||
if (m_textureNames.IsValid(index)) {
|
||||
auto it = m_textureObjects.find(index);
|
||||
if (it != m_textureObjects.end()) {
|
||||
m_textureObjects.erase(it);
|
||||
m_textureNames.Delete(index);
|
||||
} else if (!keepUnboundReservation) {
|
||||
// GL 3.3 core 3.8.1 makes a deleted name unused again even when GenTextures only
|
||||
// reserved it and no bind ever instantiated an object (so a later bind of it must
|
||||
// fail), and the reservation has to return to the free list.
|
||||
m_textureNames.Delete(index);
|
||||
}
|
||||
// Relaxed semantics: legacy apps may delete a generated name before its first
|
||||
// bind, then bind and populate that same name. Keep such a reservation alive there;
|
||||
// a real texture object reaching deletion still releases its index above.
|
||||
}
|
||||
}
|
||||
|
||||
Bool ShareGroupState::ValidateTextureName(Uint index) const {
|
||||
return m_textureNames.IsValid(index);
|
||||
}
|
||||
|
||||
Bool ShareGroupState::ValidateTextureObject(Uint index) const {
|
||||
return m_textureObjects.find(index) != m_textureObjects.end();
|
||||
}
|
||||
|
||||
// Renderbuffer
|
||||
const SharedPtr<RenderbufferObject>& ShareGroupState::GetRenderbufferObject(Uint index) {
|
||||
auto it = m_renderbufferObjects.find(index);
|
||||
if (it != m_renderbufferObjects.end()) {
|
||||
return it->second;
|
||||
}
|
||||
static SharedPtr<RenderbufferObject> nullRenderbufferObject = nullptr;
|
||||
return nullRenderbufferObject;
|
||||
}
|
||||
|
||||
void ShareGroupState::GenerateRenderbufferNames(Uint number, Vector<Uint>& renderbuffers) {
|
||||
renderbuffers.resize(number);
|
||||
m_renderbufferNames.Generate(number, renderbuffers.data());
|
||||
}
|
||||
|
||||
const SharedPtr<RenderbufferObject>& ShareGroupState::CreateRenderbufferObject(Uint index) {
|
||||
auto& bufferObject = m_renderbufferObjects[index];
|
||||
if (!bufferObject) {
|
||||
bufferObject = MakeShared<RenderbufferObject>(index);
|
||||
}
|
||||
return bufferObject;
|
||||
}
|
||||
|
||||
void ShareGroupState::MarkRenderbufferObjectForDeletion(Uint index) {
|
||||
if (m_renderbufferNames.IsValid(index)) {
|
||||
m_renderbufferObjects.erase(index);
|
||||
m_renderbufferNames.Delete(index);
|
||||
}
|
||||
}
|
||||
|
||||
Bool ShareGroupState::ValidateRenderbufferName(Uint index) const {
|
||||
return m_renderbufferNames.IsValid(index);
|
||||
}
|
||||
|
||||
Bool ShareGroupState::ValidateRenderbufferObject(Uint index) const {
|
||||
return m_renderbufferObjects.find(index) != m_renderbufferObjects.end();
|
||||
}
|
||||
} // namespace MobileGL::MG_State::GLState
|
||||
@@ -0,0 +1,81 @@
|
||||
// MobileGL - MobileGL/MG_State/GLState/ShareGroupState.h
|
||||
// Copyright (c) 2025-2026 MobileGL-Dev
|
||||
// Licensed under the GNU Lesser General Public License v3.0:
|
||||
// https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
// https://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// End of Source File Header
|
||||
|
||||
#pragma once
|
||||
#include <Includes.h>
|
||||
#include <MG_Util/Miscellany/IndexGenerator.h>
|
||||
#include "BufferState/BufferObject.h"
|
||||
#include "ProgramState/ProgramState.h"
|
||||
#include "RenderbufferState/RenderbufferObject.h"
|
||||
#include "SamplerState/SamplerState.h"
|
||||
#include "TextureState/TextureObject.h"
|
||||
|
||||
namespace MobileGL::MG_State::GLState {
|
||||
// The GL object registries and name spaces a set of sharing contexts share, i.e. one EGL
|
||||
// share group (GL 4.6 core 5.1). Buffers, textures, renderbuffers, samplers, programs and
|
||||
// shaders live here; glGen* names are drawn from this object's generators.
|
||||
//
|
||||
// What is NOT here is what GL scopes to a single context - bindings, texture units, the
|
||||
// current program, errors, and the container objects (VAO/FBO/query/pipeline/transform
|
||||
// feedback) - so two contexts sharing a group see the same objects and their own bindings.
|
||||
class ShareGroupState {
|
||||
public:
|
||||
ShareGroupState() = default;
|
||||
explicit ShareGroupState(Uint64 token) : m_token(token) {}
|
||||
|
||||
// P14 S1's shareGroupToken: the identity the wire uses, 0 for a group no EGL context
|
||||
// was made for.
|
||||
Uint64 GetToken() const { return m_token; }
|
||||
|
||||
// Buffer
|
||||
const SharedPtr<BufferObject>& GetBufferObject(Uint index);
|
||||
void GenerateBufferNames(Uint number, Vector<Uint>& buffers);
|
||||
const SharedPtr<BufferObject>& CreateBufferObject(Uint index);
|
||||
void MarkBufferObjectForDeletion(Uint index);
|
||||
Bool ValidateBufferName(Uint index) const;
|
||||
Bool ValidateBufferObject(Uint index) const;
|
||||
|
||||
// Texture
|
||||
void GenerateTextureNames(Uint number, Vector<Uint>& textures);
|
||||
const SharedPtr<ITextureObject>& GetTextureObject(Uint index);
|
||||
const SharedPtr<ITextureObject>& CreateTextureObject(Uint index, TextureTarget target);
|
||||
const SharedPtr<ITextureObject>& CreateTextureViewObject(Uint index, TextureTarget target,
|
||||
const SharedPtr<ITextureObject>& storageOwner,
|
||||
Uint minLevel, Uint numLevels, Uint minLayer,
|
||||
Uint numLayers);
|
||||
void MarkTextureObjectForDeletion(Uint index, Bool keepUnboundReservation);
|
||||
Bool ValidateTextureName(Uint index) const;
|
||||
Bool ValidateTextureObject(Uint index) const;
|
||||
|
||||
// Renderbuffer
|
||||
const SharedPtr<RenderbufferObject>& GetRenderbufferObject(Uint index);
|
||||
void GenerateRenderbufferNames(Uint number, Vector<Uint>& renderbuffers);
|
||||
const SharedPtr<RenderbufferObject>& CreateRenderbufferObject(Uint index);
|
||||
void MarkRenderbufferObjectForDeletion(Uint index);
|
||||
Bool ValidateRenderbufferName(Uint index) const;
|
||||
Bool ValidateRenderbufferObject(Uint index) const;
|
||||
|
||||
// Sampler objects carry no per-context state, so the whole table lives here.
|
||||
SamplerState& Samplers() { return m_samplerState; }
|
||||
|
||||
// Program and shader objects, one name space for both (GL 3.3 core 2.11).
|
||||
ProgramState& Programs() { return m_programState; }
|
||||
const ProgramState& Programs() const { return m_programState; }
|
||||
|
||||
private:
|
||||
Uint64 m_token = 0;
|
||||
UnorderedMap<Uint, SharedPtr<BufferObject>> m_bufferObjects;
|
||||
IndexGenerator<Uint> m_bufferNames{1024, 1};
|
||||
UnorderedMap<GLuint, SharedPtr<ITextureObject>> m_textureObjects;
|
||||
IndexGenerator<Uint> m_textureNames{1024, 1};
|
||||
UnorderedMap<Uint, SharedPtr<RenderbufferObject>> m_renderbufferObjects;
|
||||
IndexGenerator<Uint> m_renderbufferNames{1024, 1};
|
||||
SamplerState m_samplerState;
|
||||
ProgramState m_programState;
|
||||
};
|
||||
} // namespace MobileGL::MG_State::GLState
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "TextureObjectBuffer.h"
|
||||
#include "TextureObjectStubs.h"
|
||||
#include "TextureObjectView.h"
|
||||
#include "MG_State/GLState/ShareGroupState.h"
|
||||
|
||||
namespace MobileGL::MG_State::GLState {
|
||||
static std::atomic<Uint64> s_nextTextureStateContextId = 1;
|
||||
@@ -27,42 +28,10 @@ namespace MobileGL::MG_State::GLState {
|
||||
return s_nextTextureStateContextId.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
static SharedPtr<ITextureObject> MakeTextureObjectForTarget(Uint index, TextureTarget target) {
|
||||
switch (target) {
|
||||
case TextureTarget::Texture1D:
|
||||
return MakeShared<TextureObject1D>(index);
|
||||
case TextureTarget::TextureCubeMap:
|
||||
return MakeShared<TextureObject2DCube>(index);
|
||||
case TextureTarget::Texture2D:
|
||||
return MakeShared<TextureObject2D>(index);
|
||||
case TextureTarget::Texture3D:
|
||||
return MakeShared<TextureObject3D>(index);
|
||||
case TextureTarget::TextureBuffer:
|
||||
return MakeShared<TextureObjectBuffer>(index);
|
||||
|
||||
// These texture types are still stubbed:
|
||||
case TextureTarget::TextureRectangle:
|
||||
return MakeShared<TextureObjectRectangle>(index);
|
||||
case TextureTarget::Texture2DMultisample:
|
||||
return MakeShared<TextureObject2DMultisample>(index);
|
||||
case TextureTarget::Texture1DArray:
|
||||
return MakeShared<TextureObject1DArray>(index);
|
||||
case TextureTarget::Texture2DArray:
|
||||
return MakeShared<TextureObject2DArray>(index);
|
||||
case TextureTarget::TextureCubeMapArray:
|
||||
return MakeShared<TextureObjectCubeMapArray>(index);
|
||||
case TextureTarget::Texture2DMultisampleArray:
|
||||
return MakeShared<TextureObject2DMultisampleArray>(index);
|
||||
default:
|
||||
MOBILEGL_ASSERT(false, "Unimplemented texture type when creating texture object!: %d", (int)target);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
TextureState::TextureState() : m_contextId(AllocateContextId()), m_indexGenerator(1024, 1) {
|
||||
TextureState::TextureState() : m_contextId(AllocateContextId()) {
|
||||
// GL 3.3 core 3.8: each target owns one default texture object (name 0) per context,
|
||||
// shared across all texture units, and it is the initial binding of every unit/target
|
||||
// slot. It is created outside m_textureObjects so name-based paths (glIsTexture,
|
||||
// slot. It is created outside the share group's table so name-based paths (glIsTexture,
|
||||
// GenTextures/DeleteTextures, by-name DSA lookups) never see it.
|
||||
for (int i = 0; i < (int)TextureTarget::TextureTargetCount; ++i) {
|
||||
m_defaultTextureObjects[i] = MakeTextureObjectForTarget(0, static_cast<TextureTarget>(i));
|
||||
@@ -75,83 +44,35 @@ namespace MobileGL::MG_State::GLState {
|
||||
}
|
||||
}
|
||||
|
||||
const SharedPtr<ITextureObject>& TextureState::GetTextureObject(Uint index) {
|
||||
auto it = m_textureObjects.find(index);
|
||||
if (it != m_textureObjects.end()) {
|
||||
return it->second;
|
||||
}
|
||||
static SharedPtr<ITextureObject> nullTextureObject = nullptr;
|
||||
return nullTextureObject;
|
||||
}
|
||||
|
||||
const SharedPtr<ITextureObject>& TextureState::GetDefaultTextureObject(TextureTarget target) const {
|
||||
MOBILEGL_ASSERT(target > TextureTarget::Unknown && target < TextureTarget::TextureTargetCount,
|
||||
"GetDefaultTextureObject: invalid texture target %d", (int)target);
|
||||
return m_defaultTextureObjects[(int)target];
|
||||
}
|
||||
|
||||
void TextureState::GenerateNames(Uint number, Vector<Uint>& textures) {
|
||||
textures.resize(number);
|
||||
m_indexGenerator.Generate(number, textures.data());
|
||||
}
|
||||
|
||||
const SharedPtr<ITextureObject>& TextureState::CreateTextureObject(Uint index, TextureTarget target) {
|
||||
auto& textureObject = m_textureObjects[index];
|
||||
textureObject = MakeTextureObjectForTarget(index, target);
|
||||
if (!textureObject) {
|
||||
static SharedPtr<ITextureObject> nullTextureObject = nullptr;
|
||||
return nullTextureObject;
|
||||
}
|
||||
return textureObject;
|
||||
}
|
||||
|
||||
const SharedPtr<ITextureObject>& TextureState::CreateTextureViewObject(
|
||||
Uint index, TextureTarget target, const SharedPtr<ITextureObject>& storageOwner, Uint minLevel,
|
||||
Uint numLevels, Uint minLayer, Uint numLayers) {
|
||||
MOBILEGL_ASSERT(storageOwner != nullptr, "CreateTextureViewObject: storage owner is null");
|
||||
auto& textureObject = m_textureObjects[index];
|
||||
textureObject = MakeShared<TextureObjectView>(index, target, storageOwner, minLevel, numLevels, minLayer,
|
||||
numLayers);
|
||||
return textureObject;
|
||||
}
|
||||
|
||||
void TextureState::MarkTextureObjectForDeletion(Uint index, Bool keepUnboundReservation) {
|
||||
if (m_indexGenerator.IsValid(index)) {
|
||||
auto it = m_textureObjects.find(index);
|
||||
if (it != m_textureObjects.end()) {
|
||||
// Units past the touched high-water mark can never reference a texture.
|
||||
for (Int unit = 0; unit <= m_maxTouchedUnit; ++unit) {
|
||||
auto& bindingSlots = m_textureUnits[unit].GetAllBindingSlots();
|
||||
for (auto& bindingSlot : bindingSlots) {
|
||||
if (bindingSlot.GetBoundObject() == it->second) {
|
||||
// GL 3.3 core 3.8.1: deleting a bound texture rebinds zero, i.e. the
|
||||
// target's default texture object, on every unit it was bound to.
|
||||
bindingSlot.Bind(m_defaultTextureObjects[(int)bindingSlot.GetTarget()]);
|
||||
}
|
||||
}
|
||||
void TextureState::UnbindTextureFromUnits(const SharedPtr<ITextureObject>& textureObject) {
|
||||
if (!textureObject) return;
|
||||
// Units past the touched high-water mark can never reference a texture.
|
||||
for (Int unit = 0; unit <= m_maxTouchedUnit; ++unit) {
|
||||
auto& bindingSlots = m_textureUnits[unit].GetAllBindingSlots();
|
||||
for (auto& bindingSlot : bindingSlots) {
|
||||
if (bindingSlot.GetBoundObject() == textureObject) {
|
||||
// GL 3.3 core 3.8.1: deleting a bound texture rebinds zero, i.e. the
|
||||
// target's default texture object, on every unit it was bound to.
|
||||
bindingSlot.Bind(m_defaultTextureObjects[(int)bindingSlot.GetTarget()]);
|
||||
}
|
||||
for (Int unit = 0; unit <= m_maxTouchedUnit; ++unit) {
|
||||
auto& imageBinding = m_imageTextureBindings[unit];
|
||||
if (imageBinding.Texture == it->second) {
|
||||
imageBinding.Bind(nullptr, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8);
|
||||
}
|
||||
}
|
||||
// Deleting a texture unbinds it from every unit above; treat that as a binding
|
||||
// change so a cached sampled-texture set (which may hold this raw pointer) is
|
||||
// re-resolved instead of dangling.
|
||||
BumpTextureBindGeneration();
|
||||
m_textureObjects.erase(index);
|
||||
m_indexGenerator.Delete(index);
|
||||
} else if (!keepUnboundReservation) {
|
||||
// GL 3.3 core 3.8.1 makes a deleted name unused again even when GenTextures only
|
||||
// reserved it and no bind ever instantiated an object (so a later bind of it must
|
||||
// fail), and the reservation has to return to the free list.
|
||||
m_indexGenerator.Delete(index);
|
||||
}
|
||||
// Relaxed semantics: legacy apps may delete a generated name before its first
|
||||
// bind, then bind and populate that same name. Keep such a reservation alive there;
|
||||
// a real texture object reaching deletion still releases its index above.
|
||||
}
|
||||
for (Int unit = 0; unit <= m_maxTouchedUnit; ++unit) {
|
||||
auto& imageBinding = m_imageTextureBindings[unit];
|
||||
if (imageBinding.Texture == textureObject) {
|
||||
imageBinding.Bind(nullptr, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R8);
|
||||
}
|
||||
}
|
||||
// Deleting a texture unbinds it from every unit above; treat that as a binding
|
||||
// change so a cached sampled-texture set (which may hold this raw pointer) is
|
||||
// re-resolved instead of dangling.
|
||||
BumpTextureBindGeneration();
|
||||
}
|
||||
|
||||
TextureUnit& TextureState::GetUnitObject(Int unit) {
|
||||
@@ -179,12 +100,4 @@ namespace MobileGL::MG_State::GLState {
|
||||
void TextureState::SetActiveTextureUnit(Int unit) {
|
||||
m_activeTextureUnit = unit;
|
||||
}
|
||||
|
||||
Bool TextureState::ValidateName(Uint index) const {
|
||||
return m_indexGenerator.IsValid(index);
|
||||
}
|
||||
|
||||
Bool TextureState::ValidateTextureObject(Uint index) const {
|
||||
return m_textureObjects.find(index) != m_textureObjects.end();
|
||||
}
|
||||
} // namespace MobileGL::MG_State::GLState
|
||||
|
||||
@@ -15,6 +15,11 @@
|
||||
#include "TextureUnit.h"
|
||||
|
||||
namespace MobileGL::MG_State::GLState {
|
||||
// The one place a texture object's concrete type is chosen for a target. Shared because the
|
||||
// share group's texture table and the context's per-target default texture objects both
|
||||
// build through it.
|
||||
SharedPtr<ITextureObject> MakeTextureObjectForTarget(Uint index, TextureTarget target);
|
||||
|
||||
struct ImageTextureBinding {
|
||||
SharedPtr<ITextureObject> Texture;
|
||||
GLint Level = 0;
|
||||
@@ -45,6 +50,10 @@ namespace MobileGL::MG_State::GLState {
|
||||
}
|
||||
};
|
||||
|
||||
// The per-context half of the texture state: the texture units (and their per-target
|
||||
// bindings), the image unit bindings, the active unit, and the default texture objects
|
||||
// (name 0), which GL scopes to the context rather than the share group. The named texture
|
||||
// objects and their name space belong to the share group (ShareGroupState).
|
||||
class TextureState {
|
||||
public:
|
||||
// Capacity of the combined texture-unit state arrays (indexed by glActiveTexture unit).
|
||||
@@ -56,30 +65,20 @@ namespace MobileGL::MG_State::GLState {
|
||||
static constexpr int MAX_PER_STAGE_TEXTURE_IMAGE_UNITS = 32;
|
||||
|
||||
TextureState();
|
||||
void GenerateNames(Uint number, Vector<Uint>& textures);
|
||||
const SharedPtr<ITextureObject>& CreateTextureObject(Uint index, TextureTarget target);
|
||||
// glTextureView (GL 4.6 core 8.18). `storageOwner` must already be a texture with
|
||||
// immutable storage and must NOT itself be a view - the caller composes a view-of-a-view
|
||||
// onto the root first, and passes the composed (root-relative) level/layer range here.
|
||||
const SharedPtr<ITextureObject>& CreateTextureViewObject(Uint index, TextureTarget target,
|
||||
const SharedPtr<ITextureObject>& storageOwner,
|
||||
Uint minLevel, Uint numLevels, Uint minLayer,
|
||||
Uint numLayers);
|
||||
const SharedPtr<ITextureObject>& GetTextureObject(Uint index);
|
||||
// The context's default texture object (name 0) for `target`. GL 3.3 core 3.8: texture
|
||||
// zero names a real, per-target texture object shared by every texture unit; binding 0
|
||||
// binds it, and image/parameter calls on it must work like on any texture. It is not a
|
||||
// GenTextures name: it lives outside m_textureObjects (so glIsTexture(0) stays GL_FALSE
|
||||
// and by-name lookups keep failing for 0) and can never be deleted.
|
||||
// GenTextures name: it lives outside the share group's table (so glIsTexture(0) stays
|
||||
// GL_FALSE and by-name lookups keep failing for 0) and can never be deleted.
|
||||
const SharedPtr<ITextureObject>& GetDefaultTextureObject(TextureTarget target) const;
|
||||
TextureUnit& GetUnitObject(Int unit);
|
||||
ImageTextureBinding& GetImageTextureBinding(Int unit);
|
||||
const ImageTextureBinding& GetImageTextureBinding(Int unit) const;
|
||||
Int GetActiveTextureUnit() const;
|
||||
void SetActiveTextureUnit(Int unit);
|
||||
void MarkTextureObjectForDeletion(Uint index, Bool keepUnboundReservation);
|
||||
Bool ValidateName(Uint index) const;
|
||||
Bool ValidateTextureObject(Uint index) const;
|
||||
// Rebinds every unit and image unit that names `textureObject` back to the target's
|
||||
// default texture object; this context's half of a texture delete (GL 4.6 core 8.1).
|
||||
void UnbindTextureFromUnits(const SharedPtr<ITextureObject>& textureObject);
|
||||
|
||||
// High-water mark of texture units ever touched by a texture or sampler bind.
|
||||
// Units above it have provably-empty binding slots, so per-draw backend scans
|
||||
@@ -209,8 +208,6 @@ namespace MobileGL::MG_State::GLState {
|
||||
Int m_activeTextureUnit = 0;
|
||||
Array<TextureUnit, MAX_TEXTURE_IMAGE_UNITS> m_textureUnits;
|
||||
Array<ImageTextureBinding, MAX_TEXTURE_IMAGE_UNITS> m_imageTextureBindings;
|
||||
IndexGenerator<Uint> m_indexGenerator;
|
||||
UnorderedMap<GLuint, SharedPtr<ITextureObject>> m_textureObjects;
|
||||
// One default texture object (external name 0) per target, created with the context and
|
||||
// immortal for its lifetime; the initial binding of every unit/target slot.
|
||||
Array<SharedPtr<ITextureObject>, (int)TextureTarget::TextureTargetCount> m_defaultTextureObjects;
|
||||
|
||||
Reference in New Issue
Block a user