Skip to content

Commit

Permalink
WebGPU: reworked fence signal value
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Aug 10, 2024
1 parent 0d598a0 commit 692b89a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 40 deletions.
8 changes: 3 additions & 5 deletions Graphics/GraphicsEngineWebGPU/include/FenceWebGPUImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,12 @@ class FenceWebGPUImpl final : public FenceBase<EngineWebGPUImplTraits>
/// Implementation of IFence::Wait() in WebGPU backend.
void DILIGENT_CALL_TYPE Wait(Uint64 Value) override final;

void AppendSyncPoints(const std::vector<RefCntAutoPtr<SyncPointWebGPUImpl>>& SyncPoints, Uint64 EventOnComplete);

void RequestedValue(Uint64 Value);
void AppendSyncPoints(std::vector<RefCntAutoPtr<SyncPointWebGPUImpl>> SyncPoints, Uint64 Value);

private:
using SyncPointEvent = std::pair<Uint64, std::vector<RefCntAutoPtr<SyncPointWebGPUImpl>>>;
using SyncPointGroup = std::pair<Uint64, std::vector<RefCntAutoPtr<SyncPointWebGPUImpl>>>;
std::atomic<Uint64> m_RequestedFenceValue{0};
std::deque<SyncPointEvent> m_SyncPoints;
std::deque<SyncPointGroup> m_SyncGroups;
};

} // namespace Diligent
12 changes: 3 additions & 9 deletions Graphics/GraphicsEngineWebGPU/src/DeviceContextWebGPUImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1405,13 +1405,6 @@ void DeviceContextWebGPUImpl::Flush()
{
DeviceContextWebGPUImpl* pDeviceCxt = static_cast<DeviceContextWebGPUImpl*>(pUserData);

for (auto& [Signal, Fence] : pDeviceCxt->m_SignalFences)
{
auto* pFenceWebGPU = Fence.RawPtr<FenceWebGPUImpl>();
pFenceWebGPU->RequestedValue(Signal);
}
pDeviceCxt->m_SignalFences.clear();

for (auto& MemPage : pDeviceCxt->m_DynamicMemPages)
MemPage.Recycle();
pDeviceCxt->m_DynamicMemPages.clear();
Expand All @@ -1428,9 +1421,10 @@ void DeviceContextWebGPUImpl::Flush()

for (const auto& [Signal, Fence] : m_SignalFences)
{
auto* pFenceWebGPU = Fence.RawPtr<FenceWebGPUImpl>();
pFenceWebGPU->AppendSyncPoints(SyncPoints, Signal);
FenceWebGPUImpl* pFenceWebGPU = Fence.RawPtr<FenceWebGPUImpl>();
pFenceWebGPU->AppendSyncPoints(std::move(SyncPoints), Signal);
}
m_SignalFences.clear();

GetQueryManager().ResolveQuerySet(m_pDevice, GetCommandEncoder());

Expand Down
45 changes: 19 additions & 26 deletions Graphics/GraphicsEngineWebGPU/src/FenceWebGPUImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,32 @@ FenceWebGPUImpl::FenceWebGPUImpl(IReferenceCounters* pRefCounters,

Uint64 FenceWebGPUImpl::GetCompletedValue()
{
if (!m_SyncPoints.empty())
while (!m_SyncGroups.empty())
{
auto IsSyncPointCompleted = [](const std::vector<RefCntAutoPtr<SyncPointWebGPUImpl>>& SyncPoints) {
for (const auto& pSyncPoint : SyncPoints)
if (!pSyncPoint->IsTriggered())
return false;
return true;
};

while (!m_SyncPoints.empty())
SyncPointGroup& SyncGroup = m_SyncGroups.front();
auto& SyncPoints = SyncGroup.second;
while (!SyncPoints.empty())
{
const auto& SyncPoint = m_SyncPoints.front();
if (IsSyncPointCompleted(SyncPoint.second))
if (SyncPoints.front()->IsTriggered())
{
UpdateLastCompletedFenceValue(SyncPoint.first);
m_SyncPoints.pop_front();
std::swap(SyncPoints.front(), SyncPoints.back());
SyncPoints.pop_back();
}
else
{
return m_LastCompletedFenceValue.load();
break;
}
}

UpdateLastCompletedFenceValue(m_RequestedFenceValue.load());
if (SyncPoints.empty())
{
UpdateLastCompletedFenceValue(SyncGroup.first);
m_SyncGroups.pop_front();
}
else
{
break;
}
}

return m_LastCompletedFenceValue.load();
Expand All @@ -84,18 +86,9 @@ void FenceWebGPUImpl::Wait(Uint64 Value)
m_pDevice->DeviceTick();
}

void FenceWebGPUImpl::AppendSyncPoints(const std::vector<RefCntAutoPtr<SyncPointWebGPUImpl>>& SyncPoints, Uint64 EventOnComplete)
{
m_SyncPoints.emplace_back(std::make_pair(EventOnComplete, SyncPoints));
}

void FenceWebGPUImpl::RequestedValue(Uint64 Value)
void FenceWebGPUImpl::AppendSyncPoints(std::vector<RefCntAutoPtr<SyncPointWebGPUImpl>> SyncPoints, Uint64 Value)
{
DvpSignal(Value);
if (!m_SyncPoints.empty())
m_RequestedFenceValue.store(Value);
else
UpdateLastCompletedFenceValue(Value);
m_SyncGroups.emplace_back(std::make_pair(Value, std::move(SyncPoints)));
}

} // namespace Diligent

0 comments on commit 692b89a

Please sign in to comment.