Skip to content

Commit b376137

Browse files
committed
Update TQ implementation based on PR comments
1 parent 658390f commit b376137

File tree

4 files changed

+36
-26
lines changed

4 files changed

+36
-26
lines changed

filament/backend/src/webgpu/WGPUTimerQuery.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,37 @@
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);
24+
void WGPUTimerQuery::beginTimeElapsedQuery() {
25+
status->elapsedNanoseconds = 0;
26+
status->available.store(false);
2827

29-
// Capture the timer query status via a weak_ptr because the MetalTimerQuery could be destroyed
28+
// Capture the timer query status via a weak_ptr because the WGPUTimerQuery could be destroyed
3029
// before the block executes.
31-
std::weak_ptr<WGPUTimerQuery::Status> status = query->status;
30+
std::weak_ptr<WGPUTimerQuery::Status> statusPtr = status;
3231

33-
if (auto s = status.lock()) {
34-
s->elapsed = std::chrono::steady_clock::now().time_since_epoch().count();
32+
if (auto s = statusPtr.lock()) {
33+
s->elapsedNanoseconds = std::chrono::steady_clock::now().time_since_epoch().count();
3534
}
36-
37-
;
3835
}
3936

40-
void WGPUTimerQuery::endTimeElapsedQuery(WGPUTimerQuery* query) {
37+
void WGPUTimerQuery::endTimeElapsedQuery() {
4138
// Capture the timer query status via a weak_ptr because the WGPUTimerQuery could be destroyed
4239
// 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;
40+
std::weak_ptr<WGPUTimerQuery::Status> statusPtr = status;
41+
if (auto s = statusPtr.lock()) {
42+
s->elapsedNanoseconds = std::chrono::steady_clock::now().time_since_epoch().count() - s->elapsedNanoseconds;
4643
s->available.store(true);
4744
}
4845
}
4946

50-
bool WGPUTimerQuery::getQueryResult(WGPUTimerQuery* query, uint64_t* outElapsedTime) {
51-
if (!query->status->available.load()) {
47+
bool WGPUTimerQuery::getQueryResult(uint64_t* outElapsedTime) {
48+
if (!status->available.load()) {
5249
return false;
5350
}
5451
if (outElapsedTime) {
55-
*outElapsedTime = query->status->elapsed;
52+
*outElapsedTime = status->elapsedNanoseconds;
5653
}
5754
return true;
5855
}

filament/backend/src/webgpu/WebGPUDriver.cpp

Lines changed: 14 additions & 4 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,11 +31,18 @@
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>
4048

@@ -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

@@ -372,17 +382,17 @@ void WebGPUDriver::destroyTimerQuery(Handle<HwTimerQuery> tqh) {
372382

373383
void WebGPUDriver::beginTimerQuery(Handle<HwTimerQuery> tqh) {
374384
auto* tq = handleCast<WGPUTimerQuery>(tqh);
375-
tq->beginTimeElapsedQuery(tq);
385+
tq->beginTimeElapsedQuery();
376386
}
377387

378388
void WebGPUDriver::endTimerQuery(Handle<HwTimerQuery> tqh) {
379389
auto* tq = handleCast<WGPUTimerQuery>(tqh);
380-
tq->endTimeElapsedQuery(tq);
390+
tq->endTimeElapsedQuery();
381391
}
382392

383393
TimerQueryResult WebGPUDriver::getTimerQueryValue(Handle<HwTimerQuery> tqh, uint64_t* elapsedTime) {
384394
auto* tq = handleCast<WGPUTimerQuery>(tqh);
385-
return tq->getQueryResult(tq, elapsedTime) ? TimerQueryResult::AVAILABLE
395+
return tq->getQueryResult(elapsedTime) ? TimerQueryResult::AVAILABLE
386396
: TimerQueryResult::NOT_READY;
387397
}
388398

filament/backend/src/webgpu/WebGPUDriver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ namespace filament::backend {
4040

4141
class WebGPUSwapChain;
4242
struct WGPURenderTarget;
43+
4344
/**
4445
* WebGPU backend (driver) implementation
4546
*/

filament/backend/src/webgpu/WebGPUHandles.h

Lines changed: 7 additions & 5 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 {
169171
std::atomic<bool> available{ false };
170-
std::atomic<uint64_t> elapsed{ 0 };// only valid if available is true
172+
std::atomic<uint64_t> elapsedNanoseconds{ 0 };// only valid if available is true
171173
};
172174

173175
std::shared_ptr<Status> status;

0 commit comments

Comments
 (0)