Skip to content

Commit db25bfe

Browse files
WebGPU: Reworked queries (close #605)
1 parent 1dc16c1 commit db25bfe

File tree

7 files changed

+134
-261
lines changed

7 files changed

+134
-261
lines changed

Graphics/GraphicsEngineWebGPU/include/DeviceContextWebGPUImpl.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ class DeviceContextWebGPUImpl final : public DeviceContextBase<EngineWebGPUImplT
302302

303303
QueryManagerWebGPU& GetQueryManager();
304304

305+
Uint64 GetNextFenceValue();
306+
307+
Uint64 GetCompletedFenceValue();
308+
305309
private:
306310
enum COMMAND_ENCODER_FLAGS : Uint32
307311
{
@@ -377,6 +381,8 @@ class DeviceContextWebGPUImpl final : public DeviceContextBase<EngineWebGPUImplT
377381
#endif
378382

379383
private:
384+
friend class QueryManagerWebGPU;
385+
380386
struct WebGPUEncoderState
381387
{
382388
enum CMD_ENCODER_STATE_FLAGS : Uint32

Graphics/GraphicsEngineWebGPU/include/QueryManagerWebGPU.hpp

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -55,99 +55,58 @@ class QueryManagerWebGPU
5555

5656
Uint32 AllocateQuery(QUERY_TYPE Type);
5757

58-
void ReleaseQuery(QUERY_TYPE Type, Uint32 Index);
58+
void DiscardQuery(QUERY_TYPE Type, Uint32 Index);
5959

6060
WGPUQuerySet GetQuerySet(QUERY_TYPE Type) const;
6161

62-
Uint32 GetReadbackBufferIdentifier(QUERY_TYPE Type, Uint64 EventValue) const;
62+
Uint64 GetQueryResult(QUERY_TYPE Type, Uint32 Index) const;
6363

64-
Uint64 GetQueryResult(QUERY_TYPE Type, Uint32 Index, Uint32 BufferIdentifier) const;
65-
66-
Uint64 GetNextEventValue(QUERY_TYPE Type);
67-
68-
void ResolveQuerySet(RenderDeviceWebGPUImpl* pDevice, WGPUCommandEncoder wgpuCmdEncoder);
69-
70-
void ReadbackQuerySet(RenderDeviceWebGPUImpl* pDevice);
71-
72-
void FinishFrame();
73-
74-
void WaitAllQuerySet(RenderDeviceWebGPUImpl* pDevice);
64+
void ResolveQuerySet(RenderDeviceWebGPUImpl* pDevice, DeviceContextWebGPUImpl* pDeviceContext);
7565

7666
private:
77-
class QuerySetInfo
67+
class QuerySetObject : public ObjectBase<IDeviceObject>, public WebGPUResourceBase
7868
{
7969
public:
80-
struct ReadbackBufferInfo
81-
{
82-
WebGPUBufferWrapper ReadbackBuffer;
83-
std::vector<Uint64> DataResult;
84-
Uint32 BufferIdentifier;
85-
Uint64 LastEventValue;
86-
Uint64 PendingEventValue;
87-
};
88-
using ReadbackBufferList = std::vector<ReadbackBufferInfo>;
89-
90-
public:
91-
QuerySetInfo() = default;
70+
QuerySetObject(IReferenceCounters* pRefCounters, RenderDeviceWebGPUImpl* pDevice, Uint32 HeapSize, QUERY_TYPE QueryType);
9271

93-
~QuerySetInfo();
94-
95-
// clang-format off
96-
QuerySetInfo (const QuerySetInfo&) = delete;
97-
QuerySetInfo ( QuerySetInfo&&) = delete;
98-
QuerySetInfo& operator = (const QuerySetInfo&) = delete;
99-
QuerySetInfo& operator = ( QuerySetInfo&&) = delete;
100-
// clang-format on
101-
102-
void Initialize(RenderDeviceWebGPUImpl* pDevice, Uint32 HeapSize, QUERY_TYPE Type);
72+
~QuerySetObject();
10373

10474
Uint32 Allocate();
10575

106-
void Release(Uint32 Index);
76+
void Discard(Uint32 Index);
10777

10878
QUERY_TYPE GetType() const;
10979

11080
Uint32 GetQueryCount() const;
11181

112-
Uint64 GetQueryResult(Uint32 Index, Uint32 BufferIdentifier) const;
82+
Uint64 GetQueryResult(Uint32 Index) const;
11383

11484
WGPUQuerySet GetWebGPUQuerySet() const;
11585

11686
Uint32 GetMaxAllocatedQueries() const;
11787

118-
bool IsNull() const;
119-
120-
Uint32 ResolveQueries(RenderDeviceWebGPUImpl* pDevice, WGPUCommandEncoder wgpuCmdEncoder);
88+
void ResolveQueries(RenderDeviceWebGPUImpl* pDevice, DeviceContextWebGPUImpl* pDeviceContext);
12189

122-
void ReadbackQueries(RenderDeviceWebGPUImpl* pDevice, Uint32 PendingRedbackIndex);
90+
const DeviceObjectAttribs& DILIGENT_CALL_TYPE GetDesc() const override;
12391

124-
void WaitAllQueries(RenderDeviceWebGPUImpl* pDevice, Uint32 PendingRedbackIndex);
92+
Int32 DILIGENT_CALL_TYPE GetUniqueID() const override;
12593

126-
ReadbackBufferInfo& FindAvailableReadbackBuffer(RenderDeviceWebGPUImpl* pDevice);
94+
void DILIGENT_CALL_TYPE SetUserData(IObject* pUserData) override;
12795

128-
Uint32 GetReadbackBufferIdentifier(Uint64 EventValue) const;
129-
130-
Uint64 GetNextEventValue();
131-
132-
Uint64 IncrementEventValue();
96+
IObject* DILIGENT_CALL_TYPE GetUserData() const override;
13397

13498
private:
99+
DeviceObjectAttribs m_Desc;
135100
WebGPUQuerySetWrapper m_wgpuQuerySet;
136101
WebGPUBufferWrapper m_wgpuResolveBuffer;
137102
std::vector<Uint32> m_AvailableQueries;
138-
ReadbackBufferList m_PendingReadbackBuffers;
139103

140104
QUERY_TYPE m_Type = QUERY_TYPE_UNDEFINED;
141105
Uint32 m_QueryCount = 0;
142106
Uint32 m_MaxAllocatedQueries = 0;
143-
Uint64 m_EventValue = 0;
144-
145-
static constexpr Uint32 MaxPendingBuffers = 16;
146107
};
147108

148-
std::array<QuerySetInfo, QUERY_TYPE_NUM_TYPES> m_QuerySets;
149-
std::array<Uint32, QUERY_TYPE_NUM_TYPES> m_PendingReadbackIndices;
150-
Uint32 m_ActiveQuerySets = 0;
109+
std::array<RefCntAutoPtr<QuerySetObject>, QUERY_TYPE_NUM_TYPES> m_QuerySets;
151110
};
152111

153112
} // namespace Diligent

Graphics/GraphicsEngineWebGPU/include/QueryWebGPUImpl.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ class QueryWebGPUImpl final : public QueryBase<EngineWebGPUImplTraits>
6565
private:
6666
bool AllocateQueries();
6767

68-
void ReleaseQueries();
68+
void DiscardQueries();
6969

7070
private:
71-
std::array<Uint32, 2> m_QuerySetIndices = {QueryManagerWebGPU::InvalidIndex, QueryManagerWebGPU::InvalidIndex};
72-
QueryManagerWebGPU* m_pQueryMgr = nullptr;
73-
Uint64 m_QueryEventValue = 0;
71+
std::array<Uint32, 2> m_QuerySetIndices = {QueryManagerWebGPU::InvalidIndex, QueryManagerWebGPU::InvalidIndex};
72+
QueryManagerWebGPU* m_pQueryMgr = nullptr;
73+
Uint64 m_QueryEndFenceValue = 0;
7474
};
7575

7676
} // namespace Diligent

Graphics/GraphicsEngineWebGPU/src/DeviceContextWebGPUImpl.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,10 +1266,8 @@ void DeviceContextWebGPUImpl::DeviceWaitForFence(IFence* pFence, Uint64 Value)
12661266

12671267
void DeviceContextWebGPUImpl::WaitForIdle()
12681268
{
1269-
EnqueueSignal(m_pFence, ++m_FenceValue);
12701269
Flush();
12711270
m_pFence->Wait(m_FenceValue);
1272-
GetQueryManager().WaitAllQuerySet(m_pDevice);
12731271
}
12741272

12751273
void DeviceContextWebGPUImpl::BeginQuery(IQuery* pQuery)
@@ -1372,6 +1370,7 @@ void DeviceContextWebGPUImpl::EndQuery(IQuery* pQuery)
13721370

13731371
void DeviceContextWebGPUImpl::Flush()
13741372
{
1373+
EnqueueSignal(m_pFence, ++m_FenceValue);
13751374
EndCommandEncoders();
13761375

13771376
for (auto& PendingWriteIt : m_PendingStagingWrites)
@@ -1403,6 +1402,8 @@ void DeviceContextWebGPUImpl::Flush()
14031402
pSyncPoint->Release();
14041403
};
14051404

1405+
GetQueryManager().ResolveQuerySet(m_pDevice, this);
1406+
14061407
RefCntAutoPtr<SyncPointWebGPUImpl> pWorkDoneSyncPoint{MakeNewRCObj<SyncPointWebGPUImpl>()()};
14071408

14081409
std::vector<RefCntAutoPtr<SyncPointWebGPUImpl>> SyncPoints;
@@ -1416,8 +1417,6 @@ void DeviceContextWebGPUImpl::Flush()
14161417
}
14171418
m_SignaledFences.clear();
14181419

1419-
GetQueryManager().ResolveQuerySet(m_pDevice, GetCommandEncoder());
1420-
14211420
WGPUCommandBufferDescriptor wgpuCmdBufferDesc{};
14221421
WebGPUCommandBufferWrapper wgpuCmdBuffer{wgpuCommandEncoderFinish(GetCommandEncoder(), &wgpuCmdBufferDesc)};
14231422
DEV_CHECK_ERR(wgpuCmdBuffer != nullptr, "Failed to finish command encoder");
@@ -1426,8 +1425,6 @@ void DeviceContextWebGPUImpl::Flush()
14261425
wgpuQueueOnSubmittedWorkDone(m_wgpuQueue, WorkDoneCallback, pWorkDoneSyncPoint.Detach());
14271426
m_wgpuCommandEncoder.Reset(nullptr);
14281427

1429-
GetQueryManager().ReadbackQuerySet(m_pDevice);
1430-
14311428
for (auto& PendingReadIt : m_PendingStagingReads)
14321429
{
14331430
PendingReadIt.first->Resource.ProcessAsyncReadback(*PendingReadIt.first);
@@ -1599,8 +1596,6 @@ void DeviceContextWebGPUImpl::FinishFrame()
15991596
if (!m_MappedTextures.empty())
16001597
LOG_ERROR_MESSAGE("There are mapped textures in the device context when finishing the frame. All dynamic resources must be used in the same frame in which they are mapped.");
16011598

1602-
GetQueryManager().FinishFrame();
1603-
16041599
m_pDevice->DeviceTick();
16051600

16061601
TDeviceContextBase::EndFrame();
@@ -2296,4 +2291,14 @@ QueryManagerWebGPU& DeviceContextWebGPUImpl::GetQueryManager()
22962291
return m_pDevice->GetQueryManager();
22972292
}
22982293

2294+
Uint64 DeviceContextWebGPUImpl::GetNextFenceValue()
2295+
{
2296+
return m_FenceValue + 1;
2297+
}
2298+
2299+
Uint64 DeviceContextWebGPUImpl::GetCompletedFenceValue()
2300+
{
2301+
return m_pFence->GetCompletedValue();
2302+
}
2303+
22992304
} // namespace Diligent

0 commit comments

Comments
 (0)