-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathGPU.h
More file actions
84 lines (64 loc) · 2.2 KB
/
GPU.h
File metadata and controls
84 lines (64 loc) · 2.2 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
#pragma once
#include <atomic>
#include <future>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_set>
#include <variant>
#include "Unions.h"
#include "RNFHybridObject.h"
#include "AsyncRunner.h"
#include "dawn/dawn_proc.h"
#include "dawn/native/DawnNative.h"
#include "webgpu/webgpu_cpp.h"
#include "GPUAdapter.h"
#include "GPURequestAdapterOptions.h"
#include <webgpu/webgpu.h>
namespace rnwgpu {
namespace m = margelo;
class GPU : public m::HybridObject {
public:
// Singleton pattern - get the single instance
static std::shared_ptr<GPU> getInstance() {
std::call_once(_onceFlag,
[]() { _instance = std::shared_ptr<GPU>(new GPU()); });
return _instance;
}
// Delete copy constructor and assignment operator
GPU(const GPU &) = delete;
GPU &operator=(const GPU &) = delete;
public:
std::string getBrand() { return _name; }
std::future<std::variant<std::nullptr_t, std::shared_ptr<GPUAdapter>>>
requestAdapter(
std::optional<std::shared_ptr<GPURequestAdapterOptions>> options);
wgpu::TextureFormat getPreferredCanvasFormat();
std::unordered_set<std::string> getWgslLanguageFeatures();
void loadHybridMethods() override {
registerHybridGetter("__brand", &GPU::getBrand, this);
registerHybridMethod("requestAdapter", &GPU::requestAdapter, this);
registerHybridMethod("getPreferredCanvasFormat",
&GPU::getPreferredCanvasFormat, this);
registerHybridGetter("wgslLanguageFeatures", &GPU::getWgslLanguageFeatures,
this);
}
inline const wgpu::Instance get() { return _instance; }
private:
// Private constructor for singleton pattern
GPU() : HybridObject("GPU") {
wgpu::InstanceDescriptor instanceDesc;
instanceDesc.capabilities.timedWaitAnyEnable = true;
instanceDesc.capabilities.timedWaitAnyMaxCount = 64;
_instance = wgpu::CreateInstance(&instanceDesc);
auto instance = &_instance;
_async = std::make_shared<AsyncRunner>(instance);
}
// Static members for singleton pattern
static std::shared_ptr<GPU> _instance;
static std::once_flag _onceFlag;
private:
wgpu::Instance _instance;
std::shared_ptr<AsyncRunner> _async;
};
} // namespace rnwgpu