forked from wcandillon/react-native-webgpu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPUDeviceDescriptor.h
More file actions
121 lines (106 loc) · 4.34 KB
/
Copy pathGPUDeviceDescriptor.h
File metadata and controls
121 lines (106 loc) · 4.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
#pragma once
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "webgpu/webgpu_cpp.h"
#include "JSIConverter.h"
#include "RnFeatures.h"
#include "WGPULogger.h"
#include "GPUDawnTogglesDescriptor.h"
#include "GPUQueueDescriptor.h"
namespace jsi = facebook::jsi;
namespace rnwgpu {
struct GPUDeviceDescriptor {
std::optional<std::vector<wgpu::FeatureName>>
requiredFeatures; // Iterable<GPUFeatureName>
std::optional<std::map<std::string, double>>
requiredLimits; // Record< string, GPUSize64 >
std::optional<std::shared_ptr<GPUQueueDescriptor>>
defaultQueue; // GPUQueueDescriptor
std::optional<std::string> label; // string
// Non-standard Dawn-only device toggles, chained onto the wgpu::Device
// descriptor in GPUAdapter::requestDevice.
std::optional<std::shared_ptr<GPUDawnTogglesDescriptor>> dawnToggles;
};
} // namespace rnwgpu
namespace rnwgpu {
// We add this extra convertor because we found so library that are sending
// invalid feature elements
template <> struct JSIConverter<std::vector<wgpu::FeatureName>> {
static std::vector<wgpu::FeatureName>
fromJSI(jsi::Runtime &runtime, const jsi::Value &arg, bool outOfBounds) {
jsi::Array array = arg.asObject(runtime).asArray(runtime);
size_t length = array.size(runtime);
std::vector<wgpu::FeatureName> vector;
vector.reserve(length);
for (size_t i = 0; i < length; ++i) {
jsi::Value elementValue = array.getValueAtIndex(runtime, i);
if (!elementValue.isString()) {
continue;
}
auto str = elementValue.asString(runtime).utf8(runtime);
// Expand react-native-wgpu's umbrella feature into the platform's
// backing Dawn features before they reach RequestDevice.
if (str == kRnNativeTextureFeature) {
for (auto f : rnNativeTextureBackingFeatures()) {
vector.emplace_back(f);
}
continue;
}
vector.emplace_back(JSIConverter<wgpu::FeatureName>::fromJSI(
runtime, elementValue, outOfBounds));
}
return vector;
}
static jsi::Value toJSI(jsi::Runtime &runtime,
std::shared_ptr<rnwgpu::GPUDeviceDescriptor> arg) {
throw std::runtime_error(
"Invalid JSIConverter<std::vector<wgpu::FeatureName>>::toJSI()");
}
};
template <> struct JSIConverter<std::shared_ptr<rnwgpu::GPUDeviceDescriptor>> {
static std::shared_ptr<rnwgpu::GPUDeviceDescriptor>
fromJSI(jsi::Runtime &runtime, const jsi::Value &arg, bool outOfBounds) {
auto result = std::make_unique<rnwgpu::GPUDeviceDescriptor>();
if (!outOfBounds && arg.isObject()) {
auto value = arg.getObject(runtime);
if (value.hasProperty(runtime, "requiredFeatures")) {
auto prop = value.getProperty(runtime, "requiredFeatures");
result->requiredFeatures = JSIConverter<
std::optional<std::vector<wgpu::FeatureName>>>::fromJSI(runtime,
prop,
false);
}
if (value.hasProperty(runtime, "requiredLimits")) {
auto prop = value.getProperty(runtime, "requiredLimits");
result->requiredLimits =
JSIConverter<std::optional<std::map<std::string, double>>>::fromJSI(
runtime, prop, false);
}
if (value.hasProperty(runtime, "defaultQueue")) {
auto prop = value.getProperty(runtime, "defaultQueue");
result->defaultQueue =
JSIConverter<std::optional<std::shared_ptr<GPUQueueDescriptor>>>::
fromJSI(runtime, prop, false);
}
if (value.hasProperty(runtime, "label")) {
auto prop = value.getProperty(runtime, "label");
result->label = JSIConverter<std::optional<std::string>>::fromJSI(
runtime, prop, false);
}
if (value.hasProperty(runtime, "dawnToggles")) {
auto prop = value.getProperty(runtime, "dawnToggles");
result->dawnToggles = JSIConverter<
std::optional<std::shared_ptr<GPUDawnTogglesDescriptor>>>::fromJSI(
runtime, prop, false);
}
}
return result;
}
static jsi::Value toJSI(jsi::Runtime &runtime,
std::shared_ptr<rnwgpu::GPUDeviceDescriptor> arg) {
throw std::runtime_error("Invalid GPUDeviceDescriptor::toJSI()");
}
};
} // namespace rnwgpu