| 1 |
/* |
| 2 |
* Copyright 2022 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/gpu/graphite/dawn/DawnQueueManager.h" |
| 9 |
|
| 10 |
#include "src/gpu/graphite/dawn/DawnAsyncWait.h" |
| 11 |
#include "src/gpu/graphite/dawn/DawnCommandBuffer.h" |
| 12 |
#include "src/gpu/graphite/dawn/DawnGraphiteUtils.h" |
| 13 |
#include "src/gpu/graphite/dawn/DawnResourceProvider.h" |
| 14 |
#include "src/gpu/graphite/dawn/DawnSharedContext.h" |
| 15 |
|
| 16 |
namespace skgpu::graphite { |
| 17 |
namespace { |
| 18 |
#if defined(__EMSCRIPTEN__) |
| 19 |
// GpuWorkSubmission with AsyncWait. This is useful for wasm where wgpu::Future |
| 20 |
// is not available yet. |
| 21 |
class DawnWorkSubmissionWithAsyncWait final : public GpuWorkSubmission { |
| 22 |
public: |
| 23 |
DawnWorkSubmissionWithAsyncWait(std::unique_ptr<CommandBuffer> cmdBuffer, |
| 24 |
DawnQueueManager* queueManager, |
| 25 |
const DawnSharedContext* sharedContext); |
| 26 |
|
| 27 |
private: |
| 28 |
bool onIsFinished(const SharedContext* sharedContext) override; |
| 29 |
void onWaitUntilFinished(const SharedContext* sharedContext) override; |
| 30 |
|
| 31 |
DawnAsyncWait fAsyncWait; |
| 32 |
}; |
| 33 |
|
| 34 |
DawnWorkSubmissionWithAsyncWait::DawnWorkSubmissionWithAsyncWait( |
| 35 |
std::unique_ptr<CommandBuffer> cmdBuffer, |
| 36 |
DawnQueueManager* queueManager, |
| 37 |
const DawnSharedContext* sharedContext) |
| 38 |
: GpuWorkSubmission(std::move(cmdBuffer), queueManager), fAsyncWait(sharedContext) { |
| 39 |
queueManager->dawnQueue().OnSubmittedWorkDone( |
| 40 |
[](WGPUQueueWorkDoneStatus, void* userData) { |
| 41 |
auto asyncWaitPtr = static_cast<DawnAsyncWait*>(userData); |
| 42 |
asyncWaitPtr->signal(); |
| 43 |
}, |
| 44 |
&fAsyncWait); |
| 45 |
} |
| 46 |
|
| 47 |
bool DawnWorkSubmissionWithAsyncWait::onIsFinished(const SharedContext*) { |
| 48 |
return fAsyncWait.yieldAndCheck(); |
| 49 |
} |
| 50 |
|
| 51 |
void DawnWorkSubmissionWithAsyncWait::onWaitUntilFinished(const SharedContext*) { |
| 52 |
fAsyncWait.busyWait(); |
| 53 |
} |
| 54 |
|
| 55 |
#else |
| 56 |
|
| 57 |
// The version with wgpu::Future. This is not available in wasm yet so we have |
| 58 |
// to guard behind #if |
| 59 |
class DawnWorkSubmissionWithFuture final : public GpuWorkSubmission { |
| 60 |
public: |
| 61 |
DawnWorkSubmissionWithFuture(std::unique_ptr<CommandBuffer> cmdBuffer, |
| 62 |
DawnQueueManager* queueManager); |
| 63 |
|
| 64 |
private: |
| 65 |
bool onIsFinished(const SharedContext* sharedContext) override; |
| 66 |
void onWaitUntilFinished(const SharedContext* sharedContext) override; |
| 67 |
|
| 68 |
wgpu::Future fSubmittedWorkDoneFuture; |
| 69 |
}; |
| 70 |
|
| 71 |
DawnWorkSubmissionWithFuture::DawnWorkSubmissionWithFuture(std::unique_ptr<CommandBuffer> cmdBuffer, |
| 72 |
DawnQueueManager* queueManager) |
| 73 |
: GpuWorkSubmission(std::move(cmdBuffer), queueManager) { |
| 74 |
fSubmittedWorkDoneFuture = queueManager->dawnQueue().OnSubmittedWorkDone( |
| 75 |
wgpu::CallbackMode::WaitAnyOnly, [](wgpu::QueueWorkDoneStatus, wgpu::StringView) {}); |
| 76 |
} |
| 77 |
|
| 78 |
bool DawnWorkSubmissionWithFuture::onIsFinished(const SharedContext* sharedContext) { |
| 79 |
wgpu::FutureWaitInfo waitInfo{}; |
| 80 |
waitInfo.future = fSubmittedWorkDoneFuture; |
| 81 |
const auto& instance = static_cast<const DawnSharedContext*>(sharedContext)->instance(); |
| 82 |
if (instance.WaitAny(1, &waitInfo, /*timeoutNS=*/0) != wgpu::WaitStatus::Success) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
return waitInfo.completed; |
| 87 |
} |
| 88 |
|
| 89 |
void DawnWorkSubmissionWithFuture::onWaitUntilFinished(const SharedContext* sharedContext) { |
| 90 |
wgpu::FutureWaitInfo waitInfo{}; |
| 91 |
waitInfo.future = fSubmittedWorkDoneFuture; |
| 92 |
const auto& instance = static_cast<const DawnSharedContext*>(sharedContext)->instance(); |
| 93 |
[[maybe_unused]] auto status = |
| 94 |
instance.WaitAny(1, &waitInfo, /*timeoutNS=*/std::numeric_limits<uint64_t>::max()); |
| 95 |
SkASSERT(status == wgpu::WaitStatus::Success); |
| 96 |
SkASSERT(waitInfo.completed); |
| 97 |
} |
| 98 |
#endif // defined(__EMSCRIPTEN__) |
| 99 |
} // namespace |
| 100 |
|
| 101 |
DawnQueueManager::DawnQueueManager(wgpu::Queue queue, const SharedContext* sharedContext) |
| 102 |
: QueueManager(sharedContext), fQueue(std::move(queue)) {} |
| 103 |
|
| 104 |
void DawnQueueManager::tick() const { this->dawnSharedContext()->tick(); } |
| 105 |
|
| 106 |
const DawnSharedContext* DawnQueueManager::dawnSharedContext() const { |
| 107 |
return static_cast<const DawnSharedContext*>(fSharedContext); |
| 108 |
} |
| 109 |
|
| 110 |
std::unique_ptr<CommandBuffer> DawnQueueManager::getNewCommandBuffer( |
| 111 |
ResourceProvider* resourceProvider, Protected) { |
| 112 |
return DawnCommandBuffer::Make(dawnSharedContext(), |
| 113 |
static_cast<DawnResourceProvider*>(resourceProvider)); |
| 114 |
} |
| 115 |
|
| 116 |
QueueManager::OutstandingSubmission DawnQueueManager::onSubmitToGpu(const SubmitInfo&) { |
| 117 |
SkASSERT(fCurrentCommandBuffer); |
| 118 |
DawnCommandBuffer* dawnCmdBuffer = static_cast<DawnCommandBuffer*>(fCurrentCommandBuffer.get()); |
| 119 |
auto wgpuCmdBuffer = dawnCmdBuffer->finishEncoding(); |
| 120 |
if (!wgpuCmdBuffer) { |
| 121 |
fCurrentCommandBuffer->callFinishedProcs(/*success=*/false); |
| 122 |
return nullptr; |
| 123 |
} |
| 124 |
|
| 125 |
fQueue.Submit(/*commandCount=*/1, &wgpuCmdBuffer); |
| 126 |
|
| 127 |
#if defined(__EMSCRIPTEN__) |
| 128 |
return std::make_unique<DawnWorkSubmissionWithAsyncWait>( |
| 129 |
std::move(fCurrentCommandBuffer), this, dawnSharedContext()); |
| 130 |
#else |
| 131 |
return std::make_unique<DawnWorkSubmissionWithFuture>(std::move(fCurrentCommandBuffer), this); |
| 132 |
#endif |
| 133 |
} |
| 134 |
|
| 135 |
#if defined(GPU_TEST_UTILS) |
| 136 |
void DawnQueueManager::startCapture() { |
| 137 |
// TODO: Dawn doesn't have capturing feature yet. |
| 138 |
} |
| 139 |
|
| 140 |
void DawnQueueManager::stopCapture() { |
| 141 |
// TODO: Dawn doesn't have capturing feature yet. |
| 142 |
} |
| 143 |
#endif |
| 144 |
|
| 145 |
} // namespace skgpu::graphite |