-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathGPUAdapter.cpp
More file actions
179 lines (165 loc) · 6.36 KB
/
GPUAdapter.cpp
File metadata and controls
179 lines (165 loc) · 6.36 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
169
170
171
172
173
174
175
176
177
178
179
#include "GPUAdapter.h"
#include <cstdio>
#include <memory>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
#include "Convertors.h"
#include "GPUFeatures.h"
#include "RNFJSIConverter.h"
#include "WGPULogger.h"
namespace rnwgpu {
async::AsyncTaskHandle GPUAdapter::requestDevice(
std::optional<std::shared_ptr<GPUDeviceDescriptor>> descriptor) {
wgpu::DeviceDescriptor aDescriptor;
Convertor conv;
if (!conv(aDescriptor, descriptor)) {
throw std::runtime_error("Failed to convert GPUDeviceDescriptor");
}
auto deviceLostBinding = std::make_shared<std::weak_ptr<GPUDevice>>();
// Set device lost callback using new template API
aDescriptor.SetDeviceLostCallback(
wgpu::CallbackMode::AllowSpontaneous,
[deviceLostBinding](const wgpu::Device & /*device*/,
wgpu::DeviceLostReason reason,
wgpu::StringView message) {
const char *lostReason = "";
switch (reason) {
case wgpu::DeviceLostReason::Destroyed:
lostReason = "Destroyed";
break;
case wgpu::DeviceLostReason::Unknown:
lostReason = "Unknown";
break;
default:
lostReason = "Unknown";
}
std::string msg =
message.length ? std::string(message.data, message.length) : "";
Logger::logToConsole("GPU Device Lost (%s): %s", lostReason,
msg.c_str());
if (auto deviceHost = deviceLostBinding->lock()) {
deviceHost->notifyDeviceLost(reason, std::move(msg));
}
});
// Set uncaptured error callback using new template API
aDescriptor.SetUncapturedErrorCallback([](const wgpu::Device &device,
wgpu::ErrorType type,
wgpu::StringView message) {
const char *errorType = "";
switch (type) {
case wgpu::ErrorType::Validation:
errorType = "Validation";
break;
case wgpu::ErrorType::OutOfMemory:
errorType = "Out of Memory";
break;
case wgpu::ErrorType::Internal:
errorType = "Internal";
break;
case wgpu::ErrorType::Unknown:
errorType = "Unknown";
break;
default:
errorType = "Unknown";
}
std::string fullMessage =
message.length > 0 ? std::string(errorType) + ": " +
std::string(message.data, message.length)
: "no message";
fprintf(stderr, "%s", fullMessage.c_str());
});
std::string label =
descriptor.has_value() ? descriptor.value()->label.value_or("") : "";
auto creationRuntime = getCreationRuntime();
return _async->postTask(
[this, aDescriptor, descriptor, label = std::move(label),
deviceLostBinding,
creationRuntime](const async::AsyncTaskHandle::ResolveFunction &resolve,
const async::AsyncTaskHandle::RejectFunction &reject) {
(void)descriptor;
_instance.RequestDevice(
&aDescriptor, wgpu::CallbackMode::AllowProcessEvents,
[asyncRunner = _async, resolve, reject, label, creationRuntime,
deviceLostBinding](wgpu::RequestDeviceStatus status,
wgpu::Device device,
wgpu::StringView message) mutable {
if (message.length) {
fprintf(stderr, "%s", message.data);
}
if (status != wgpu::RequestDeviceStatus::Success || !device) {
std::string error =
message.length ? std::string(message.data, message.length)
: "Failed to request device";
reject(std::move(error));
return;
}
device.SetLoggingCallback([creationRuntime](
wgpu::LoggingType type,
wgpu::StringView msg) {
if (creationRuntime == nullptr) {
return;
}
const char *logLevel = "";
switch (type) {
case wgpu::LoggingType::Warning:
logLevel = "Warning";
Logger::warnToJavascriptConsole(
*creationRuntime, std::string(msg.data, msg.length));
break;
case wgpu::LoggingType::Error:
logLevel = "Error";
Logger::errorToJavascriptConsole(
*creationRuntime, std::string(msg.data, msg.length));
break;
case wgpu::LoggingType::Verbose:
logLevel = "Verbose";
break;
case wgpu::LoggingType::Info:
logLevel = "Info";
break;
default:
logLevel = "Unknown";
Logger::logToConsole("%s: %.*s", logLevel,
static_cast<int>(msg.length), msg.data);
}
});
auto deviceHost = std::make_shared<GPUDevice>(std::move(device),
asyncRunner, label);
*deviceLostBinding = deviceHost;
resolve([deviceHost = std::move(deviceHost)](
jsi::Runtime &runtime) mutable {
return margelo::JSIConverter<std::shared_ptr<GPUDevice>>::toJSI(
runtime, deviceHost);
});
});
});
}
std::unordered_set<std::string> GPUAdapter::getFeatures() {
wgpu::SupportedFeatures supportedFeatures;
_instance.GetFeatures(&supportedFeatures);
std::unordered_set<std::string> result;
for (size_t i = 0; i < supportedFeatures.featureCount; ++i) {
auto feature = supportedFeatures.features[i];
std::string name;
convertEnumToJSUnion(feature, &name);
if (name != "") {
result.insert(name);
}
}
return result;
}
std::shared_ptr<GPUSupportedLimits> GPUAdapter::getLimits() {
wgpu::Limits limits{};
if (!_instance.GetLimits(&limits)) {
throw std::runtime_error("Failed to get limits");
}
return std::make_shared<GPUSupportedLimits>(limits);
}
std::shared_ptr<GPUAdapterInfo> GPUAdapter::getInfo() {
wgpu::AdapterInfo info = {};
_instance.GetInfo(&info);
return std::make_shared<GPUAdapterInfo>(info);
}
} // namespace rnwgpu