Skip to content
Open
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
2 changes: 1 addition & 1 deletion externals/dawn
Submodule dawn updated from 56a4ae to a690fe
3 changes: 2 additions & 1 deletion packages/webgpu/cpp/rnwgpu/RNWebGPUManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ RNWebGPUManager::RNWebGPUManager(
std::make_shared<margelo::CallInvokerDispatcher>(_jsCallInvoker);
margelo::Dispatcher::installRuntimeGlobalDispatcher(*_jsRuntime, dispatcher);

auto gpu = std::make_shared<GPU>();
// Use the singleton GPU instance instead of creating a new one
auto gpu = GPU::getInstance();
auto rnWebGPU = std::make_shared<RNWebGPU>(gpu, _platformContext);
_gpu = gpu->get();
_jsRuntime->global().setProperty(
Expand Down
6 changes: 5 additions & 1 deletion packages/webgpu/cpp/rnwgpu/api/GPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

namespace rnwgpu {

// Static member definitions for singleton pattern
std::shared_ptr<GPU> GPU::_instance = nullptr;
std::once_flag GPU::_onceFlag;

std::future<std::variant<std::nullptr_t, std::shared_ptr<GPUAdapter>>>
GPU::requestAdapter(
std::optional<std::shared_ptr<GPURequestAdapterOptions>> options) {
Expand Down Expand Up @@ -50,7 +54,7 @@ GPU::requestAdapter(
std::unordered_set<std::string> GPU::getWgslLanguageFeatures() {
wgpu::SupportedWGSLLanguageFeatures supportedFeatures = {};
_instance.GetWGSLLanguageFeatures(&supportedFeatures);

std::unordered_set<std::string> result;
for (size_t i = 0; i < supportedFeatures.featureCount; i++) {
wgpu::WGSLLanguageFeatureName feature = supportedFeatures.features[i];
Expand Down
33 changes: 26 additions & 7 deletions packages/webgpu/cpp/rnwgpu/api/GPU.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include <atomic>
#include <future>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_set>
#include <variant>
Expand All @@ -27,15 +29,17 @@ namespace m = margelo;

class GPU : public m::HybridObject {
public:
GPU() : HybridObject("GPU") {
wgpu::InstanceDescriptor instanceDesc;
instanceDesc.capabilities.timedWaitAnyEnable = true;
instanceDesc.capabilities.timedWaitAnyMaxCount = 64;
_instance = wgpu::CreateInstance(&instanceDesc);
auto instance = &_instance;
_async = std::make_shared<AsyncRunner>(instance);
// Singleton pattern - get the single instance
static std::shared_ptr<GPU> getInstance() {
std::call_once(_onceFlag,
[]() { _instance = std::shared_ptr<GPU>(new GPU()); });
return _instance;
}

// Delete copy constructor and assignment operator
GPU(const GPU &) = delete;
GPU &operator=(const GPU &) = delete;

public:
std::string getBrand() { return _name; }

Expand All @@ -57,6 +61,21 @@ class GPU : public m::HybridObject {

inline const wgpu::Instance get() { return _instance; }

private:
// Private constructor for singleton pattern
GPU() : HybridObject("GPU") {
wgpu::InstanceDescriptor instanceDesc;
instanceDesc.capabilities.timedWaitAnyEnable = true;
instanceDesc.capabilities.timedWaitAnyMaxCount = 64;
_instance = wgpu::CreateInstance(&instanceDesc);
auto instance = &_instance;
_async = std::make_shared<AsyncRunner>(instance);
}

// Static members for singleton pattern
static std::shared_ptr<GPU> _instance;
static std::once_flag _onceFlag;

private:
wgpu::Instance _instance;
std::shared_ptr<AsyncRunner> _async;
Expand Down
5 changes: 3 additions & 2 deletions packages/webgpu/cpp/rnwgpu/api/descriptors/Unions.h
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ static void convertJSUnionToEnum(const std::string &inUnion,
} else if (inUnion == "dawn-native") {
*outEnum = wgpu::FeatureName::DawnNative;
} else if (inUnion == "chromium-experimental-timestamp-query-inside-passes") {
*outEnum = wgpu::FeatureName::ChromiumExperimentalTimestampQueryInsidePasses;
*outEnum =
wgpu::FeatureName::ChromiumExperimentalTimestampQueryInsidePasses;
} else if (inUnion == "implicit-device-synchronization") {
*outEnum = wgpu::FeatureName::ImplicitDeviceSynchronization;
} else if (inUnion == "transient-attachments") {
Expand Down Expand Up @@ -613,7 +614,7 @@ static void convertEnumToJSUnion(wgpu::FeatureName inEnum,
case wgpu::FeatureName::ANGLETextureSharing:
*outUnion = "angle-texture-sharing";
break;
case wgpu::FeatureName::ChromiumExperimentalSubgroupMatrix:
case wgpu::FeatureName::ChromiumExperimentalSubgroupMatrix:
*outUnion = "chromium-experimental-subgroups-matrix";
break;
case wgpu::FeatureName::PixelLocalStorageCoherent:
Expand Down
Loading