Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/webgpu/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ add_library(${PACKAGE_NAME} SHARED
../cpp/jsi/Promise.cpp
../cpp/jsi/RuntimeLifecycleMonitor.cpp
../cpp/jsi/RuntimeAwareCache.cpp
../cpp/rnwgpu/async/AsyncRunner.cpp
../cpp/rnwgpu/async/RuntimeContext.cpp
../cpp/rnwgpu/async/AsyncTaskHandle.cpp
../cpp/rnwgpu/async/JSIMicrotaskDispatcher.cpp
../cpp/rnwgpu/async/CallInvokerScheduler.cpp
../cpp/rnwgpu/async/GpuEventLoop.cpp
)

target_include_directories(
Expand Down
4 changes: 2 additions & 2 deletions packages/webgpu/cpp/rnwgpu/RNWebGPUManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
#include "GPURenderPassEncoder.h"
#include "GPURenderPipeline.h"
#include "GPUSampler.h"
#include "GPUSharedTextureMemory.h"
#include "GPUShaderModule.h"
#include "GPUSharedTextureMemory.h"
#include "GPUSupportedLimits.h"
#include "GPUTexture.h"
#include "GPUTextureView.h"
Expand Down Expand Up @@ -63,7 +63,7 @@ RNWebGPUManager::RNWebGPUManager(
// Register main runtime for RuntimeAwareCache
BaseRuntimeAwareCache::setMainJsRuntime(_jsRuntime);

auto gpu = std::make_shared<GPU>(*_jsRuntime);
auto gpu = std::make_shared<GPU>(*_jsRuntime, _jsCallInvoker);
auto rnWebGPU =
std::make_shared<RNWebGPU>(gpu, _platformContext, _jsCallInvoker);
_gpu = gpu->get();
Expand Down
27 changes: 17 additions & 10 deletions packages/webgpu/cpp/rnwgpu/api/GPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@

#include "Convertors.h"
#include "JSIConverter.h"
#include "rnwgpu/async/JSIMicrotaskDispatcher.h"
#include "rnwgpu/async/CallInvokerScheduler.h"
#include "rnwgpu/async/GpuEventLoop.h"

namespace rnwgpu {

GPU::GPU(jsi::Runtime &runtime) : NativeObject(CLASS_NAME) {
GPU::GPU(jsi::Runtime &runtime,
std::shared_ptr<facebook::react::CallInvoker> callInvoker)
: NativeObject(CLASS_NAME) {
static const auto kTimedWaitAny = wgpu::InstanceFeatureName::TimedWaitAny;
wgpu::InstanceDescriptor instanceDesc{.requiredFeatureCount = 1,
.requiredFeatures = &kTimedWaitAny};
Expand Down Expand Up @@ -49,8 +52,11 @@ GPU::GPU(jsi::Runtime &runtime) : NativeObject(CLASS_NAME) {

_instance = wgpu::CreateInstance(&instanceDesc);

auto dispatcher = std::make_shared<async::JSIMicrotaskDispatcher>(runtime);
_async = async::AsyncRunner::getOrCreate(runtime, _instance, dispatcher);
auto scheduler =
std::make_shared<async::CallInvokerScheduler>(std::move(callInvoker));
auto eventLoop = std::make_shared<async::GpuEventLoop>(_instance);
_async = async::RuntimeContext::getOrCreate(runtime, std::move(scheduler),
std::move(eventLoop));
}

async::AsyncTaskHandle GPU::requestAdapter(
Expand All @@ -68,19 +74,20 @@ async::AsyncTaskHandle GPU::requestAdapter(
aOptions.backendType = kDefaultBackendType;
return _async->postTask(
[this, aOptions](const async::AsyncTaskHandle::ResolveFunction &resolve,
const async::AsyncTaskHandle::RejectFunction &reject) {
_instance.RequestAdapter(
&aOptions, wgpu::CallbackMode::AllowProcessEvents,
[asyncRunner = _async, resolve,
const async::AsyncTaskHandle::RejectFunction &reject)
-> wgpu::Future {
return _instance.RequestAdapter(
&aOptions, wgpu::CallbackMode::WaitAnyOnly,
[context = _async, resolve,
reject](wgpu::RequestAdapterStatus status, wgpu::Adapter adapter,
wgpu::StringView message) {
if (message.length) {
fprintf(stderr, "%s", message.data);
}

if (status == wgpu::RequestAdapterStatus::Success && adapter) {
auto adapterHost = std::make_shared<GPUAdapter>(
std::move(adapter), asyncRunner);
auto adapterHost =
std::make_shared<GPUAdapter>(std::move(adapter), context);
auto result =
std::variant<std::nullptr_t, std::shared_ptr<GPUAdapter>>(
adapterHost);
Expand Down
12 changes: 8 additions & 4 deletions packages/webgpu/cpp/rnwgpu/api/GPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

#include "NativeObject.h"

#include "rnwgpu/async/AsyncRunner.h"
#include "rnwgpu/async/AsyncTaskHandle.h"
#include "rnwgpu/async/RuntimeContext.h"

#include "webgpu/webgpu_cpp.h"

Expand All @@ -19,6 +19,10 @@

#include <webgpu/webgpu.h>

namespace facebook::react {
class CallInvoker;
} // namespace facebook::react

namespace rnwgpu {

namespace jsi = facebook::jsi;
Expand All @@ -27,7 +31,8 @@ class GPU : public NativeObject<GPU> {
public:
static constexpr const char *CLASS_NAME = "GPU";

explicit GPU(jsi::Runtime &runtime);
GPU(jsi::Runtime &runtime,
std::shared_ptr<facebook::react::CallInvoker> callInvoker);

public:
std::string getBrand() { return CLASS_NAME; }
Expand All @@ -48,11 +53,10 @@ class GPU : public NativeObject<GPU> {
}

inline const wgpu::Instance get() { return _instance; }
inline std::shared_ptr<async::AsyncRunner> getAsyncRunner() { return _async; }

private:
wgpu::Instance _instance;
std::shared_ptr<async::AsyncRunner> _async;
std::shared_ptr<async::RuntimeContext> _async;
};

} // namespace rnwgpu
20 changes: 9 additions & 11 deletions packages/webgpu/cpp/rnwgpu/api/GPUAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ async::AsyncTaskHandle GPUAdapter::requestDevice(
[this, aDescriptor, descriptor, label = std::move(label),
deviceLostBinding,
creationRuntime](const async::AsyncTaskHandle::ResolveFunction &resolve,
const async::AsyncTaskHandle::RejectFunction &reject) {
const async::AsyncTaskHandle::RejectFunction &reject)
-> wgpu::Future {
// Build a local mutable copy so we can chain Dawn's device toggles.
// The toggle name strings are owned by `descriptor` (captured above),
// and the const char* / DawnTogglesDescriptor locals live for the
Expand Down Expand Up @@ -162,12 +163,11 @@ async::AsyncTaskHandle GPUAdapter::requestDevice(
}
deviceDesc.nextInChain = &toggles;
}
_instance.RequestDevice(
&deviceDesc, wgpu::CallbackMode::AllowProcessEvents,
[asyncRunner = _async, resolve, reject, label, creationRuntime,
return _instance.RequestDevice(
&deviceDesc, wgpu::CallbackMode::WaitAnyOnly,
[context = _async, resolve, reject, label, creationRuntime,
deviceLostBinding](wgpu::RequestDeviceStatus status,
wgpu::Device device,
wgpu::StringView message) {
wgpu::Device device, wgpu::StringView message) {
if (message.length) {
fprintf(stderr, "%s", message.data);
}
Expand All @@ -191,14 +191,12 @@ async::AsyncTaskHandle GPUAdapter::requestDevice(
case wgpu::LoggingType::Warning:
logLevel = "Warning";
Logger::warnToJavascriptConsole(
*creationRuntime,
std::string(msg.data, msg.length));
*creationRuntime, std::string(msg.data, msg.length));
break;
case wgpu::LoggingType::Error:
logLevel = "Error";
Logger::errorToJavascriptConsole(
*creationRuntime,
std::string(msg.data, msg.length));
*creationRuntime, std::string(msg.data, msg.length));
break;
case wgpu::LoggingType::Verbose:
logLevel = "Verbose";
Expand All @@ -216,7 +214,7 @@ async::AsyncTaskHandle GPUAdapter::requestDevice(
creationRuntime);

auto deviceHost = std::make_shared<GPUDevice>(std::move(device),
asyncRunner, label);
context, label);
*deviceLostBinding = deviceHost;

// Register the device in the static registry so the uncaptured
Expand Down
6 changes: 3 additions & 3 deletions packages/webgpu/cpp/rnwgpu/api/GPUAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#include "NativeObject.h"

#include "rnwgpu/async/AsyncRunner.h"
#include "rnwgpu/async/AsyncTaskHandle.h"
#include "rnwgpu/async/RuntimeContext.h"

#include "webgpu/webgpu_cpp.h"

Expand All @@ -27,7 +27,7 @@ class GPUAdapter : public NativeObject<GPUAdapter> {
static constexpr const char *CLASS_NAME = "GPUAdapter";

explicit GPUAdapter(wgpu::Adapter instance,
std::shared_ptr<async::AsyncRunner> async)
std::shared_ptr<async::RuntimeContext> async)
: NativeObject(CLASS_NAME), _instance(instance), _async(async) {}

public:
Expand All @@ -53,7 +53,7 @@ class GPUAdapter : public NativeObject<GPUAdapter> {

private:
wgpu::Adapter _instance;
std::shared_ptr<async::AsyncRunner> _async;
std::shared_ptr<async::RuntimeContext> _async;
};

} // namespace rnwgpu
50 changes: 25 additions & 25 deletions packages/webgpu/cpp/rnwgpu/api/GPUBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,31 @@ async::AsyncTaskHandle GPUBuffer::mapAsync(uint64_t modeIn,
return _async->postTask(
[bufferHandle, mode, resolvedOffset,
rangeSize](const async::AsyncTaskHandle::ResolveFunction &resolve,
const async::AsyncTaskHandle::RejectFunction &reject) {
bufferHandle.MapAsync(mode, resolvedOffset, rangeSize,
wgpu::CallbackMode::AllowProcessEvents,
[resolve, reject](wgpu::MapAsyncStatus status,
wgpu::StringView message) {
switch (status) {
case wgpu::MapAsyncStatus::Success:
resolve(nullptr);
break;
case wgpu::MapAsyncStatus::CallbackCancelled:
reject("MapAsyncStatus::CallbackCancelled");
break;
case wgpu::MapAsyncStatus::Error:
reject("MapAsyncStatus::Error");
break;
case wgpu::MapAsyncStatus::Aborted:
reject("MapAsyncStatus::Aborted");
break;
default:
reject(
"MapAsyncStatus: " +
std::to_string(static_cast<int>(status)));
break;
}
});
const async::AsyncTaskHandle::RejectFunction &reject)
-> wgpu::Future {
return bufferHandle.MapAsync(
mode, resolvedOffset, rangeSize, wgpu::CallbackMode::WaitAnyOnly,
[resolve, reject](wgpu::MapAsyncStatus status,
wgpu::StringView message) {
switch (status) {
case wgpu::MapAsyncStatus::Success:
resolve(nullptr);
break;
case wgpu::MapAsyncStatus::CallbackCancelled:
reject("MapAsyncStatus::CallbackCancelled");
break;
case wgpu::MapAsyncStatus::Error:
reject("MapAsyncStatus::Error");
break;
case wgpu::MapAsyncStatus::Aborted:
reject("MapAsyncStatus::Aborted");
break;
default:
reject("MapAsyncStatus: " +
std::to_string(static_cast<int>(status)));
break;
}
});
});
}

Expand Down
6 changes: 3 additions & 3 deletions packages/webgpu/cpp/rnwgpu/api/GPUBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

#include "NativeObject.h"

#include "rnwgpu/async/AsyncRunner.h"
#include "rnwgpu/async/AsyncTaskHandle.h"
#include "rnwgpu/async/RuntimeContext.h"

#include "webgpu/webgpu_cpp.h"

Expand All @@ -25,7 +25,7 @@ class GPUBuffer : public NativeObject<GPUBuffer> {
static constexpr const char *CLASS_NAME = "GPUBuffer";

explicit GPUBuffer(wgpu::Buffer instance,
std::shared_ptr<async::AsyncRunner> async,
std::shared_ptr<async::RuntimeContext> async,
std::string label)
: NativeObject(CLASS_NAME), _instance(instance), _async(async),
_label(label) {}
Expand Down Expand Up @@ -71,7 +71,7 @@ class GPUBuffer : public NativeObject<GPUBuffer> {

private:
wgpu::Buffer _instance;
std::shared_ptr<async::AsyncRunner> _async;
std::shared_ptr<async::RuntimeContext> _async;
std::string _label;
struct Mapping {
uint64_t start;
Expand Down
Loading
Loading