-
Notifications
You must be signed in to change notification settings - Fork 3
/
RenderingSemaphore.h
59 lines (46 loc) · 1.28 KB
/
RenderingSemaphore.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2023 The Khronos Group
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <condition_variable>
#include <mutex>
namespace visionaray {
struct RenderingSemaphore
{
RenderingSemaphore() = default;
void arrayMapAcquire();
void arrayMapRelease();
void frameStart();
void frameEnd();
private:
std::mutex m_mutex;
std::condition_variable m_conditionArrays;
std::condition_variable m_conditionFrame;
unsigned long m_arraysMapped{0};
bool m_frameInFlight{false};
};
// Inlined definitions ////////////////////////////////////////////////////////
inline void RenderingSemaphore::arrayMapAcquire()
{
std::unique_lock<std::mutex> frameLock(m_mutex);
m_conditionFrame.wait(frameLock, [&]() { return !m_frameInFlight; });
m_arraysMapped++;
}
inline void RenderingSemaphore::arrayMapRelease()
{
std::lock_guard<std::mutex> lock(m_mutex);
m_arraysMapped--;
if (m_arraysMapped == 0)
m_conditionArrays.notify_one();
}
inline void RenderingSemaphore::frameStart()
{
std::unique_lock<std::mutex> arraysLock(m_mutex);
m_conditionArrays.wait(arraysLock, [&]() { return m_arraysMapped == 0; });
m_frameInFlight = true;
}
inline void RenderingSemaphore::frameEnd()
{
m_frameInFlight = false;
m_conditionFrame.notify_all();
}
} // namespace visionaray