-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathGPUBuffer.h
More file actions
87 lines (68 loc) · 2.61 KB
/
Copy pathGPUBuffer.h
File metadata and controls
87 lines (68 loc) · 2.61 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
#pragma once
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "Unions.h"
#include "NativeObject.h"
#include "rnwgpu/async/AsyncTaskHandle.h"
#include "rnwgpu/async/RuntimeContext.h"
#include "webgpu/webgpu_cpp.h"
#include "ArrayBuffer.h"
namespace rnwgpu {
namespace jsi = facebook::jsi;
class GPUBuffer : public NativeObject<GPUBuffer> {
public:
static constexpr const char *CLASS_NAME = "GPUBuffer";
explicit GPUBuffer(wgpu::Buffer instance,
std::shared_ptr<async::RuntimeContext> async,
std::string label)
: NativeObject(CLASS_NAME), _instance(instance), _async(async),
_label(label) {}
public:
std::string getBrand() { return CLASS_NAME; }
async::AsyncTaskHandle mapAsync(uint64_t modeIn,
std::optional<uint64_t> offset,
std::optional<uint64_t> size);
std::shared_ptr<ArrayBuffer> getMappedRange(std::optional<size_t> offset,
std::optional<size_t> size);
void unmap();
void destroy();
uint64_t getSize();
double getUsage();
wgpu::BufferMapState getMapState();
std::string getLabel() { return _label; }
void setLabel(const std::string &label) {
_label = label;
_instance.SetLabel(_label.c_str());
}
static void definePrototype(jsi::Runtime &runtime, jsi::Object &prototype) {
installGetter(runtime, prototype, "__brand", &GPUBuffer::getBrand);
installMethod(runtime, prototype, "mapAsync", &GPUBuffer::mapAsync);
installMethod(runtime, prototype, "getMappedRange",
&GPUBuffer::getMappedRange);
installMethod(runtime, prototype, "unmap", &GPUBuffer::unmap);
installMethod(runtime, prototype, "destroy", &GPUBuffer::destroy);
installGetter(runtime, prototype, "size", &GPUBuffer::getSize);
installGetter(runtime, prototype, "usage", &GPUBuffer::getUsage);
installGetter(runtime, prototype, "mapState", &GPUBuffer::getMapState);
installGetterSetter(runtime, prototype, "label", &GPUBuffer::getLabel,
&GPUBuffer::setLabel);
}
inline const wgpu::Buffer get() { return _instance; }
size_t getMemoryPressure() override { return static_cast<size_t>(getSize()); }
private:
wgpu::Buffer _instance;
std::shared_ptr<async::RuntimeContext> _async;
std::string _label;
struct Mapping {
uint64_t start;
uint64_t end;
inline bool Intersects(uint64_t s, uint64_t e) const {
return s < end && e > start;
}
std::shared_ptr<ArrayBuffer> buffer;
};
std::vector<Mapping> mappings;
};
} // namespace rnwgpu