forked from wcandillon/react-native-webgpu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPU.cpp
More file actions
168 lines (152 loc) · 6.69 KB
/
GPU.cpp
File metadata and controls
168 lines (152 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "GPU.h"
#include <cstdio>
#include <memory>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
#include "Convertors.h"
#include "JSIConverter.h"
#include "rnwgpu/async/JSIMicrotaskDispatcher.h"
namespace rnwgpu {
GPU::GPU(jsi::Runtime &runtime,
std::vector<std::string> enableToggles,
std::vector<std::string> disableToggles)
: NativeObject(CLASS_NAME),
_enableToggles(std::move(enableToggles)),
_disableToggles(std::move(disableToggles)) {
static const auto kTimedWaitAny = wgpu::InstanceFeatureName::TimedWaitAny;
wgpu::InstanceDescriptor instanceDesc{.requiredFeatureCount = 1,
.requiredFeatures = &kTimedWaitAny};
wgpu::InstanceLimits limits{.timedWaitAnyMaxCount = 64};
instanceDesc.requiredLimits = &limits;
// Build Dawn toggles descriptor and chain it if any toggles are specified
std::vector<const char *> enablePtrs, disablePtrs;
wgpu::DawnTogglesDescriptor togglesDesc;
if (!_enableToggles.empty() || !_disableToggles.empty()) {
for (const auto &s : _enableToggles) enablePtrs.push_back(s.c_str());
for (const auto &s : _disableToggles) disablePtrs.push_back(s.c_str());
togglesDesc.enabledToggles = enablePtrs.empty() ? nullptr : enablePtrs.data();
togglesDesc.enabledToggleCount = enablePtrs.size();
togglesDesc.disabledToggles = disablePtrs.empty() ? nullptr : disablePtrs.data();
togglesDesc.disabledToggleCount = disablePtrs.size();
togglesDesc.nextInChain = instanceDesc.nextInChain;
instanceDesc.nextInChain = &togglesDesc;
}
_instance = wgpu::CreateInstance(&instanceDesc);
auto dispatcher = std::make_shared<async::JSIMicrotaskDispatcher>(runtime);
_async = async::AsyncRunner::getOrCreate(runtime, _instance, dispatcher);
}
async::AsyncTaskHandle GPU::requestAdapter(
std::optional<std::shared_ptr<GPURequestAdapterOptions>> options) {
wgpu::RequestAdapterOptions aOptions;
Convertor conv;
if (!conv(aOptions, options)) {
throw std::runtime_error("Failed to convert GPUDeviceDescriptor");
}
#ifdef __APPLE__
constexpr auto kDefaultBackendType = wgpu::BackendType::Metal;
#else
constexpr auto kDefaultBackendType = wgpu::BackendType::Vulkan;
#endif
aOptions.backendType = kDefaultBackendType;
// Capture toggle strings by value so the lambda owns them
auto enableToggles = _enableToggles;
auto disableToggles = _disableToggles;
return _async->postTask(
[this, aOptions, enableToggles = std::move(enableToggles),
disableToggles = std::move(disableToggles)](
const async::AsyncTaskHandle::ResolveFunction &resolve,
const async::AsyncTaskHandle::RejectFunction &reject) {
// Build Dawn toggles chain inside the task so pointers remain valid
std::vector<const char *> enablePtrs, disablePtrs;
wgpu::DawnTogglesDescriptor togglesDesc;
auto localOptions = aOptions;
if (!enableToggles.empty() || !disableToggles.empty()) {
for (const auto &s : enableToggles) enablePtrs.push_back(s.c_str());
for (const auto &s : disableToggles) disablePtrs.push_back(s.c_str());
togglesDesc.enabledToggles = enablePtrs.empty() ? nullptr : enablePtrs.data();
togglesDesc.enabledToggleCount = enablePtrs.size();
togglesDesc.disabledToggles = disablePtrs.empty() ? nullptr : disablePtrs.data();
togglesDesc.disabledToggleCount = disablePtrs.size();
togglesDesc.nextInChain = localOptions.nextInChain;
localOptions.nextInChain = &togglesDesc;
}
_instance.RequestAdapter(
&localOptions, wgpu::CallbackMode::AllowProcessEvents,
[asyncRunner = _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 result =
std::variant<std::nullptr_t, std::shared_ptr<GPUAdapter>>(
adapterHost);
resolve([result =
std::move(result)](jsi::Runtime &runtime) mutable {
return JSIConverter<decltype(result)>::toJSI(runtime, result);
});
} else {
auto result =
std::variant<std::nullptr_t, std::shared_ptr<GPUAdapter>>(
nullptr);
resolve([result =
std::move(result)](jsi::Runtime &runtime) mutable {
return JSIConverter<decltype(result)>::toJSI(runtime, result);
});
}
});
});
}
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];
std::string name;
switch (feature) {
case wgpu::WGSLLanguageFeatureName::ReadonlyAndReadwriteStorageTextures:
name = "readonly_and_readwrite_storage_textures";
break;
case wgpu::WGSLLanguageFeatureName::Packed4x8IntegerDotProduct:
name = "packed_4x8_integer_dot_product";
break;
case wgpu::WGSLLanguageFeatureName::UnrestrictedPointerParameters:
name = "unrestricted_pointer_parameters";
break;
case wgpu::WGSLLanguageFeatureName::PointerCompositeAccess:
name = "pointer_composite_access";
break;
case wgpu::WGSLLanguageFeatureName::ChromiumTestingUnimplemented:
name = "chromium_testing_unimplemented";
break;
case wgpu::WGSLLanguageFeatureName::ChromiumTestingUnsafeExperimental:
name = "chromium_testing_unsafe_experimental";
break;
case wgpu::WGSLLanguageFeatureName::ChromiumTestingExperimental:
name = "chromium_testing_experimental";
break;
case wgpu::WGSLLanguageFeatureName::ChromiumTestingShippedWithKillswitch:
name = "chromium_testing_shipped_with_killswitch";
break;
case wgpu::WGSLLanguageFeatureName::ChromiumTestingShipped:
name = "chromium_testing_shipped";
break;
}
result.insert(name);
}
return result;
}
wgpu::TextureFormat GPU::getPreferredCanvasFormat() {
#if defined(__ANDROID__)
return wgpu::TextureFormat::RGBA8Unorm;
#else
return wgpu::TextureFormat::BGRA8Unorm;
#endif // defined(__ANDROID__)
}
} // namespace rnwgpu