Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
glLockManager.cpp
Go to the documentation of this file.
1
2
4
7
9
10namespace Divide
11{
12 constexpr gl46core::GLuint64 kOneSecondInNanoSeconds = 1000000000;
13
14 glSyncObject::glSyncObject( const U8 flag, const U64 frameIdx )
15 : SyncObject(flag, frameIdx)
16 {
17 }
18
20 {
21 DIVIDE_ASSERT( _syncObject == nullptr);
22 }
23
25 {
27
30 }
31
33 {
34 waitForLockedRange( 0u, SIZE_MAX );
35 }
36
37
39 {
41
42 bool ret = false;
43 if ( entry._ptr == nullptr )
44 {
45 entry._ptr = std::make_unique<glSyncObject>(flag, frameIdx);
46 ret = true;
47 }
48 else if ( entry._ptr->_frameNumber == SyncObject::INVALID_FRAME_NUMBER )
49 {
50 glSyncObject* glSync = static_cast<glSyncObject*>(entry._ptr.get());
51 DIVIDE_ASSERT( glSync->_syncObject == nullptr );
52 glSync->_frameNumber = frameIdx;
53 glSync->_flag = flag;
54 ret = true;
55 }
56
57 if ( ret )
58 {
59 static_cast<glSyncObject*>(entry._ptr.get())->_syncObject = GL_API::CreateFenceSync();
60 }
61
62 return ret;
63 }
64
65 static bool Wait( gl46core::GLsync sync, U8& retryCount )
66 {
68
69 gl46core::GLuint64 waitTimeout = 0u;
70 gl46core::SyncObjectMask waitFlags = gl46core::SyncObjectMask::GL_NONE_BIT;
71 while ( true )
72 {
74 PROFILE_TAG( "RetryCount", retryCount );
75
76 const gl46core::GLenum waitRet = gl46core::glClientWaitSync( sync, waitFlags, waitTimeout );
77 if ( waitRet == gl46core::GL_ALREADY_SIGNALED || waitRet == gl46core::GL_CONDITION_SATISFIED )
78 {
79 return true;
80 }
81 DIVIDE_ASSERT( waitRet != gl46core::GL_WAIT_FAILED, "glLockManager::wait error: Not sure what to do here. Probably raise an exception or something." );
82
83 if ( retryCount == 1 )
84 {
85 //ToDo: Do I need this here? -Ionut
87 }
88
89 // After the first time, need to start flushing, and wait for a looong time.
90 waitFlags = gl46core::SyncObjectMask::GL_SYNC_FLUSH_COMMANDS_BIT;
91 waitTimeout = kOneSecondInNanoSeconds;
92
93 if ( ++retryCount > g_MaxLockWaitRetries )
94 {
95 if ( waitRet != gl46core::GL_TIMEOUT_EXPIRED ) [[unlikely]]
96 {
97 Console::errorfn(LOCALE_STR("ERROR_GL_LOCK_WAIT_TIMEOUT"));
98 }
99
100 break;
101 }
102 }
103
104 return false;
105 }
106
107 bool glLockManager::waitForLockedRangeLocked( const SyncObject_uptr& sync, const BufferRange& testRange, const BufferLockInstance& lock )
108 {
110
111 glSyncObject* glSync = static_cast<glSyncObject*>(sync.get());
112
113 if ( glSync->_syncObject == nullptr ||
115 {
116 // Lock expired from underneath us
117 return true;
118 }
119
120 U8 retryCount = 0u;
121 if ( Wait( glSync->_syncObject, retryCount ) ) [[likely]]
122 {
123 if ( retryCount > g_MaxLockWaitRetries - 1 )
124 {
125 Console::errorfn(LOCALE_STR("ERROR_GL_LOCK_WAIT_RETRY"), testRange._startOffset, testRange._length, retryCount);
126 }
127
128 return LockManager::waitForLockedRangeLocked(sync, testRange, lock);
129 }
130
131 return false;
132 }
133};//namespace Divide
#define LOCALE_STR(X)
Definition: Localization.h:91
#define DIVIDE_ASSERT(...)
#define PROFILE_SCOPE_AUTO(CATEGORY)
Definition: Profiler.h:87
#define PROFILE_TAG(NAME,...)
Definition: Profiler.h:88
#define PROFILE_SCOPE(NAME, CATEGORY)
Definition: Profiler.h:86
static void DestroyFenceSync(gl46core::GLsync &sync)
Definition: GLWrapper.cpp:1998
static gl46core::GLsync CreateFenceSync()
Definition: GLWrapper.cpp:1988
static GLStateTracker & GetStateTracker() noexcept
Definition: GLWrapper.cpp:1749
static void QueueFlush() noexcept
Definition: GLWrapper.cpp:1783
virtual bool waitForLockedRangeLocked(const SyncObject_uptr &sync, const BufferRange &testRange, const BufferLockInstance &lock)
bool waitForLockedRange(size_t lockBeginBytes, size_t lockLength)
Returns false if we encountered an error.
static bool InitLockPoolEntry(BufferLockPoolEntry &entry, U8 flag, U64 frameIdx)
bool waitForLockedRangeLocked(const SyncObject_uptr &sync, const BufferRange &testRange, const BufferLockInstance &lock) override
constexpr U8 MAX_FRAMES_IN_FLIGHT
Maximum number of active frames until we start waiting on a fence/sync.
Definition: config.h:100
constexpr Optick::Category::Type Graphics
Definition: Profiler.h:60
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
constexpr U8 g_MaxLockWaitRetries
Definition: LockManager.h:44
void Wait(const Task &task, TaskPool &pool)
Definition: Task.cpp:20
uint8_t U8
constexpr gl46core::GLuint64 kOneSecondInNanoSeconds
Project const SceneEntry & entry
Definition: DefaultScene.h:41
uint64_t U64
static NO_INLINE void errorfn(const char *format, T &&... args)
Definition: LockManager.h:83
static constexpr U64 INVALID_FRAME_NUMBER
Definition: LockManager.h:48
virtual void reset()
Definition: LockManager.cpp:24
void reset() override
gl46core::GLsync _syncObject
Definition: glLockManager.h:47
glSyncObject(U8 flag, U64 frameIdx)