-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathGPUAdapter.cpp
More file actions
154 lines (143 loc) · 4.83 KB
/
GPUAdapter.cpp
File metadata and controls
154 lines (143 loc) · 4.83 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
#include "GPUAdapter.h"
#include <cstdio>
#include <memory>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
#include "Convertors.h"
#include "GPUFeatures.h"
#include "WGPULogger.h"
namespace rnwgpu {
std::future<std::shared_ptr<GPUDevice>> GPUAdapter::requestDevice(
std::optional<std::shared_ptr<GPUDeviceDescriptor>> descriptor) {
std::promise<std::shared_ptr<GPUDevice>> promise;
auto future = promise.get_future();
wgpu::Device device = nullptr;
wgpu::DeviceDescriptor aDescriptor;
Convertor conv;
if (!conv(aDescriptor, descriptor)) {
throw std::runtime_error("Failed to convert GPUDeviceDescriptor");
}
// Set device lost callback using new template API
aDescriptor.SetDeviceLostCallback(
wgpu::CallbackMode::AllowSpontaneous,
[](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";
}
Logger::logToConsole("GPU Device Lost (%s): %s", lostReason,
message.data);
});
// 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());
});
_instance.RequestDevice(
&aDescriptor, wgpu::CallbackMode::AllowSpontaneous,
[](wgpu::RequestDeviceStatus status, wgpu::Device device,
wgpu::StringView message, wgpu::Device *userdata) {
if (message.length) {
fprintf(stderr, "%s", message.data);
return;
}
if (status == wgpu::RequestDeviceStatus::Success) {
*userdata = std::move(device);
}
},
&device);
if (!device) {
throw std::runtime_error("Failed to request device");
}
device.SetLoggingCallback(
[creationRuntime = _creationRuntime](wgpu::LoggingType type,
wgpu::StringView message) {
const char *logLevel = "";
switch (type) {
case wgpu::LoggingType::Warning:
logLevel = "Warning";
Logger::warnToJavascriptConsole(
*creationRuntime, std::string(message.data, message.length));
break;
case wgpu::LoggingType::Error:
logLevel = "Error";
Logger::errorToJavascriptConsole(
*creationRuntime, std::string(message.data, message.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>(message.length), message.data);
}
});
std::string label =
descriptor.has_value() ? descriptor.value()->label.value_or("") : "";
promise.set_value(
std::make_shared<GPUDevice>(std::move(device), _async, label));
return future;
}
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