-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathGPU.cpp
More file actions
161 lines (147 loc) · 6.34 KB
/
Copy pathGPU.cpp
File metadata and controls
161 lines (147 loc) · 6.34 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
#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/CallInvokerScheduler.h"
#include "rnwgpu/async/GpuEventLoop.h"
namespace rnwgpu {
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};
wgpu::InstanceLimits limits{.timedWaitAnyMaxCount = 64};
instanceDesc.requiredLimits = &limits;
// Expose Dawn's experimental adapter features. Several features needed by
// our Android external-texture path (YCbCrVulkanSamplers,
// OpaqueYCbCrAndroidForExternalTexture) are tagged Experimental in Dawn's
// feature table and are otherwise filtered out of adapter.features by
// PhysicalDeviceBase::GetSupportedFeatures. The allow_unsafe_apis toggle
// disables that filter so the features become visible;
// expose_wgsl_experimental_features is the parallel toggle for WGSL language
// features.
//
// Trade-off: these are instance-level Dawn toggles, so they apply to every
// adapter/device created from this instance, not just the external-texture
// path. There is no finer-grained, per-feature mechanism to surface these.
// The exposure is acceptable because the toggle only *un-hides* experimental
// features in adapter.features; it does not enable any of them. Nothing
// experimental becomes active unless application code explicitly lists that
// feature in requiredFeatures at device creation, so the default behavior of
// a device that asks for no experimental features is unchanged.
static const char *const kEnabledToggles[] = {
"allow_unsafe_apis",
"expose_wgsl_experimental_features",
};
wgpu::DawnTogglesDescriptor toggles;
toggles.enabledToggleCount = std::size(kEnabledToggles);
toggles.enabledToggles = kEnabledToggles;
instanceDesc.nextInChain = &toggles;
_instance = wgpu::CreateInstance(&instanceDesc);
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(
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;
return _async->postTask(
[this, aOptions](const async::AsyncTaskHandle::ResolveFunction &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), context);
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