Skip to content

Commit 097d165

Browse files
committed
Address TQ PR Comments
1 parent 658390f commit 097d165

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed

filament/backend/src/webgpu/WGPUTimerQuery.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,35 @@
1919

2020
#include <chrono>
2121

22-
2322
namespace filament::backend {
2423

25-
void WGPUTimerQuery::beginTimeElapsedQuery(WGPUTimerQuery* query) {
26-
query->status->elapsed = 0;
27-
query->status->available.store(false);
28-
29-
// Capture the timer query status via a weak_ptr because the MetalTimerQuery could be destroyed
24+
void WGPUTimerQuery::beginTimeElapsedQuery() {
25+
status->elapsedNanoseconds = 0;
26+
// Capture the timer query status via a weak_ptr because the WGPUTimerQuery could be destroyed
3027
// before the block executes.
31-
std::weak_ptr<WGPUTimerQuery::Status> status = query->status;
28+
std::weak_ptr<WGPUTimerQuery::Status> statusPtr = status;
3229

33-
if (auto s = status.lock()) {
34-
s->elapsed = std::chrono::steady_clock::now().time_since_epoch().count();
30+
if (auto s = statusPtr.lock()) {
31+
s->elapsedNanoseconds = std::chrono::steady_clock::now().time_since_epoch().count();
3532
}
36-
37-
;
3833
}
3934

40-
void WGPUTimerQuery::endTimeElapsedQuery(WGPUTimerQuery* query) {
35+
void WGPUTimerQuery::endTimeElapsedQuery() {
4136
// Capture the timer query status via a weak_ptr because the WGPUTimerQuery could be destroyed
4237
// before the block executes.
43-
std::weak_ptr<WGPUTimerQuery::Status> status = query->status;
44-
if (auto s = status.lock()) {
45-
s->elapsed = std::chrono::steady_clock::now().time_since_epoch().count() - s->elapsed;
46-
s->available.store(true);
38+
std::weak_ptr<WGPUTimerQuery::Status> statusPtr = status;
39+
if (auto s = statusPtr.lock()) {
40+
s->previousElapsed = s->elapsedNanoseconds = std::chrono::steady_clock::now().time_since_epoch().count() - s->elapsedNanoseconds;
4741
}
4842
}
4943

50-
bool WGPUTimerQuery::getQueryResult(WGPUTimerQuery* query, uint64_t* outElapsedTime) {
51-
if (!query->status->available.load()) {
44+
bool WGPUTimerQuery::getQueryResult(uint64_t* outElapsedTime) {
45+
if (status->previousElapsed == 0) {
5246
return false;
5347
}
5448
if (outElapsedTime) {
55-
*outElapsedTime = query->status->elapsed;
49+
*outElapsedTime = status->previousElapsed;
50+
status->previousElapsed = 0;
5651
}
5752
return true;
5853
}

filament/backend/src/webgpu/WebGPUDriver.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include "webgpu/WebGPUDriver.h"
18+
1819
#include "WebGPUSwapChain.h"
1920
#include "webgpu/WebGPUConstants.h"
2021
#include "webgpu/WebGPUHandles.h"
@@ -30,14 +31,21 @@
3031
#include <utils/CString.h>
3132
#include <utils/ostream.h>
3233

34+
#if FWGPU_ENABLED(FWGPU_PRINT_SYSTEM)
35+
#include <dawn/webgpu_cpp_print.h>
36+
#endif
3337
#include <webgpu/webgpu_cpp.h>
3438

3539
#include <algorithm>
3640
#include <cstddef>
37-
41+
#include <cstdint>
42+
#if FWGPU_ENABLED(FWGPU_PRINT_SYSTEM)
43+
#include <sstream>
44+
#include <string_view>
45+
#endif
3846
#include <utility>
3947
#include <variant>
40-
48+
#include <iostream>
4149
namespace filament::backend {
4250

4351
namespace {
@@ -357,6 +365,8 @@ Handle<HwFence> WebGPUDriver::createFenceS() noexcept {
357365
}
358366

359367
Handle<HwTimerQuery> WebGPUDriver::createTimerQueryS() noexcept {
368+
// The handle must be constructed here, as a synchronous call to getTimerQueryValue might happen
369+
// before createTimerQueryR is executed.
360370
return allocAndConstructHandle<WGPUTimerQuery, HwTimerQuery>();
361371
}
362372

@@ -371,18 +381,16 @@ void WebGPUDriver::destroyTimerQuery(Handle<HwTimerQuery> tqh) {
371381
}
372382

373383
void WebGPUDriver::beginTimerQuery(Handle<HwTimerQuery> tqh) {
374-
auto* tq = handleCast<WGPUTimerQuery>(tqh);
375-
tq->beginTimeElapsedQuery(tq);
384+
mTimerQuery = handleCast<WGPUTimerQuery>(tqh);
376385
}
377386

378387
void WebGPUDriver::endTimerQuery(Handle<HwTimerQuery> tqh) {
379-
auto* tq = handleCast<WGPUTimerQuery>(tqh);
380-
tq->endTimeElapsedQuery(tq);
388+
mTimerQuery = handleCast<WGPUTimerQuery>(tqh);
381389
}
382390

383391
TimerQueryResult WebGPUDriver::getTimerQueryValue(Handle<HwTimerQuery> tqh, uint64_t* elapsedTime) {
384392
auto* tq = handleCast<WGPUTimerQuery>(tqh);
385-
return tq->getQueryResult(tq, elapsedTime) ? TimerQueryResult::AVAILABLE
393+
return tq->getQueryResult(elapsedTime) ? TimerQueryResult::AVAILABLE
386394
: TimerQueryResult::NOT_READY;
387395
}
388396

@@ -719,6 +727,13 @@ void WebGPUDriver::endRenderPass(int) {
719727
.label = "command_buffer",
720728
};
721729
mCommandBuffer = mCommandEncoder.Finish(&commandBufferDescriptor);
730+
mQueue.OnSubmittedWorkDone(wgpu::CallbackMode::AllowSpontaneous, [this](auto const& status) {
731+
if (status == wgpu::QueueWorkDoneStatus::Success) {
732+
if (mTimerQuery) {
733+
mTimerQuery->endTimeElapsedQuery();
734+
}
735+
}
736+
});
722737
assert_invariant(mCommandBuffer);
723738
}
724739

@@ -741,6 +756,7 @@ void WebGPUDriver::makeCurrent(Handle<HwSwapChain> drawSch, Handle<HwSwapChain>
741756

742757
void WebGPUDriver::commit(Handle<HwSwapChain> sch) {
743758
mCommandEncoder = nullptr;
759+
mTimerQuery->beginTimeElapsedQuery();
744760
mQueue.Submit(1, &mCommandBuffer);
745761
mCommandBuffer = nullptr;
746762
mTextureView = nullptr;

filament/backend/src/webgpu/WebGPUDriver.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
namespace filament::backend {
4040

4141
class WebGPUSwapChain;
42+
class WGPUTimerQuery;
4243
struct WGPURenderTarget;
44+
4345
/**
4446
* WebGPU backend (driver) implementation
4547
*/
@@ -92,7 +94,7 @@ class WebGPUDriver final : public DriverBase {
9294
wgpu::RenderPassEncoder mRenderPassEncoder = nullptr;
9395
wgpu::CommandBuffer mCommandBuffer = nullptr;
9496
WGPURenderTarget* mDefaultRenderTarget = nullptr;
95-
97+
WGPUTimerQuery* mTimerQuery = nullptr;
9698

9799
/*
98100
* Driver interface

filament/backend/src/webgpu/WebGPUHandles.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525

2626
#include <utils/FixedCapacityVector.h>
2727

28+
#include <webgpu/webgpu_cpp.h>
29+
2830
#include <cstdint>
2931
#include <vector>
30-
#include <webgpu/webgpu_cpp.h>
3132

3233
namespace filament::backend {
3334

@@ -161,13 +162,14 @@ class WGPUTimerQuery : public HwTimerQuery {
161162
WGPUTimerQuery()
162163
: status(std::make_shared<Status>()) {}
163164

164-
void beginTimeElapsedQuery(WGPUTimerQuery* timerQuery);
165-
void endTimeElapsedQuery(WGPUTimerQuery* timerQuery);
166-
bool getQueryResult(WGPUTimerQuery* query, uint64_t* outElapsedTime);
165+
void beginTimeElapsedQuery();
166+
void endTimeElapsedQuery();
167+
bool getQueryResult(uint64_t* outElapsedTimeNanoseconds);
167168

169+
private:
168170
struct Status {
169-
std::atomic<bool> available{ false };
170-
std::atomic<uint64_t> elapsed{ 0 };// only valid if available is true
171+
std::atomic<uint64_t> elapsedNanoseconds{ 0 };
172+
std::atomic<uint64_t> previousElapsed{ 0 };
171173
};
172174

173175
std::shared_ptr<Status> status;

0 commit comments

Comments
 (0)