-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #946 from cloudflare/w4
webgpu continued
- Loading branch information
Showing
41 changed files
with
1,552 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ WhitespaceSensitiveMacros: | |
- JSG_TS_DEFINE | ||
- JSG_STRUCT_TS_OVERRIDE | ||
- JSG_STRUCT_TS_DEFINE | ||
PointerAlignment: Left |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) 2017-2022 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
|
||
#include "gpu-adapter-info.h" | ||
#include "workerd/jsg/exception.h" | ||
|
||
namespace workerd::api::gpu { | ||
|
||
GPUAdapterInfo::GPUAdapterInfo(WGPUAdapterProperties properties) | ||
: vendor_(kj::str(properties.vendorName)), | ||
architecture_(kj::str(properties.architecture)), | ||
device_(kj::str(properties.name)), | ||
description_(kj::str(properties.driverDescription)) {} | ||
|
||
} // namespace workerd::api::gpu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) 2017-2022 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <webgpu/webgpu_cpp.h> | ||
#include <workerd/jsg/jsg.h> | ||
|
||
namespace workerd::api::gpu { | ||
|
||
class GPUAdapterInfo : public jsg::Object { | ||
public: | ||
explicit GPUAdapterInfo(WGPUAdapterProperties); | ||
JSG_RESOURCE_TYPE(GPUAdapterInfo) { | ||
JSG_READONLY_PROTOTYPE_PROPERTY(vendor, getVendor); | ||
JSG_READONLY_PROTOTYPE_PROPERTY(architecture, getArchitecture); | ||
JSG_READONLY_PROTOTYPE_PROPERTY(device, getDevice); | ||
JSG_READONLY_PROTOTYPE_PROPERTY(description, getDescription); | ||
} | ||
|
||
private: | ||
kj::String vendor_; | ||
kj::String architecture_; | ||
kj::String device_; | ||
kj::String description_; | ||
kj::StringPtr getVendor() { return vendor_; }; | ||
kj::StringPtr getArchitecture() { return architecture_; }; | ||
kj::StringPtr getDevice() { return device_; }; | ||
kj::StringPtr getDescription() { return description_; }; | ||
}; | ||
|
||
} // namespace workerd::api::gpu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) 2017-2022 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
|
||
#include "gpu-async-runner.h" | ||
#include "workerd/io/io-context.h" | ||
#include <kj/common.h> | ||
#include <kj/debug.h> | ||
|
||
#define BUSY_LOOP_DELAY_MS 50 | ||
|
||
namespace workerd::api::gpu { | ||
|
||
void AsyncRunner::Begin() { | ||
KJ_ASSERT(count_ != std::numeric_limits<decltype(count_)>::max()); | ||
if (count_++ == 0) { | ||
QueueTick(); | ||
} | ||
} | ||
|
||
void AsyncRunner::End() { | ||
KJ_ASSERT(count_ > 0); | ||
count_--; | ||
} | ||
|
||
void AsyncRunner::QueueTick() { | ||
if (tick_queued_) { | ||
return; | ||
} | ||
tick_queued_ = true; | ||
|
||
IoContext::current().setTimeoutImpl( | ||
timeoutIdGenerator, false, | ||
[this](jsg::Lock &js) mutable { | ||
this->tick_queued_ = false; | ||
if (this->count_ > 0) { | ||
this->device_.Tick(); | ||
QueueTick(); | ||
} | ||
}, | ||
BUSY_LOOP_DELAY_MS); | ||
} | ||
|
||
} // namespace workerd::api::gpu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) 2017-2022 Cloudflare, Inc. | ||
// Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
// https://opensource.org/licenses/Apache-2.0 | ||
// Based on the dawn node bindings | ||
|
||
#pragma once | ||
|
||
#include "workerd/io/io-context.h" | ||
#include <kj/timer.h> | ||
#include <webgpu/webgpu_cpp.h> | ||
|
||
namespace workerd::api::gpu { | ||
|
||
// AsyncRunner is used to poll a wgpu::Device with calls to Tick() while there | ||
// are asynchronous tasks in flight. | ||
class AsyncRunner : public kj::Refcounted { | ||
public: | ||
AsyncRunner(wgpu::Device device) : device_(device){}; | ||
|
||
// Begin() should be called when a new asynchronous task is started. | ||
// If the number of executing asynchronous tasks transitions from 0 to 1, then | ||
// a function will be scheduled on the main JavaScript thread to call | ||
// wgpu::Device::Tick() whenever the thread is idle. This will be repeatedly | ||
// called until the number of executing asynchronous tasks reaches 0 again. | ||
void Begin(); | ||
|
||
// End() should be called once the asynchronous task has finished. | ||
// Every call to Begin() should eventually result in a call to End(). | ||
void End(); | ||
|
||
private: | ||
void QueueTick(); | ||
wgpu::Device const device_; | ||
uint64_t count_ = 0; | ||
bool tick_queued_ = false; | ||
TimeoutId::Generator timeoutIdGenerator; | ||
}; | ||
|
||
// AsyncTask is a RAII helper for calling AsyncRunner::Begin() on construction, | ||
// and AsyncRunner::End() on destruction. | ||
class AsyncTask { | ||
public: | ||
inline AsyncTask(AsyncTask &&) = default; | ||
|
||
// Constructor. | ||
// Calls AsyncRunner::Begin() | ||
explicit inline AsyncTask(kj::Own<AsyncRunner> runner) | ||
: runner_(std::move(runner)) { | ||
runner_->Begin(); | ||
} | ||
|
||
// Destructor. | ||
// Calls AsyncRunner::End() | ||
inline ~AsyncTask() { runner_->End(); } | ||
|
||
private: | ||
KJ_DISALLOW_COPY(AsyncTask); | ||
kj::Own<AsyncRunner> runner_; | ||
}; | ||
|
||
} // namespace workerd::api::gpu |
Oops, something went wrong.