-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathGPUBuffer.cpp
More file actions
97 lines (83 loc) · 3.17 KB
/
Copy pathGPUBuffer.cpp
File metadata and controls
97 lines (83 loc) · 3.17 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
#include "GPUBuffer.h"
#include <memory>
#include <utility>
#include "Convertors.h"
namespace rnwgpu {
std::shared_ptr<ArrayBuffer>
GPUBuffer::getMappedRange(std::optional<size_t> o, std::optional<size_t> size) {
auto offset = o.value_or(0);
uint64_t s = size.has_value() ? size.value() : (_instance.GetSize() - offset);
uint64_t start = offset;
uint64_t end = offset + s;
// for (auto& mapping : mappings_) {
// if (mapping.Intersects(start, end)) {
// Errors::OperationError(env).ThrowAsJavaScriptException();
// return {};
// }
// }
auto *ptr =
(_instance.GetUsage() & wgpu::BufferUsage::MapWrite)
? _instance.GetMappedRange(offset, s)
: const_cast<void *>(_instance.GetConstMappedRange(offset, s));
if (!ptr) {
throw std::runtime_error("Failed to get getMappedRange");
}
auto array_buffer = std::make_shared<ArrayBuffer>(ptr, s, 1);
// TODO(crbug.com/dawn/1135): Ownership here is the wrong way around.
// mappings_.emplace_back(Mapping{start, end,
// Napi::Persistent(array_buffer)});
return array_buffer;
}
void GPUBuffer::destroy() { _instance.Destroy(); }
async::AsyncTaskHandle GPUBuffer::mapAsync(uint64_t modeIn,
std::optional<uint64_t> offset,
std::optional<uint64_t> size) {
Convertor conv;
wgpu::MapMode mode;
if (!conv(mode, modeIn)) {
throw std::runtime_error("Couldn't get MapMode");
}
uint64_t rangeSize = size.has_value()
? size.value()
: (_instance.GetSize() - offset.value_or(0));
auto bufferHandle = _instance;
uint64_t resolvedOffset = offset.value_or(0);
return _async->postTask(
[bufferHandle, mode, resolvedOffset,
rangeSize](const async::AsyncTaskHandle::ResolveFunction &resolve,
const async::AsyncTaskHandle::RejectFunction &reject)
-> wgpu::Future {
return bufferHandle.MapAsync(
mode, resolvedOffset, rangeSize, wgpu::CallbackMode::WaitAnyOnly,
[resolve, reject](wgpu::MapAsyncStatus status,
wgpu::StringView message) {
switch (status) {
case wgpu::MapAsyncStatus::Success:
resolve(nullptr);
break;
case wgpu::MapAsyncStatus::CallbackCancelled:
reject("MapAsyncStatus::CallbackCancelled");
break;
case wgpu::MapAsyncStatus::Error:
reject("MapAsyncStatus::Error");
break;
case wgpu::MapAsyncStatus::Aborted:
reject("MapAsyncStatus::Aborted");
break;
default:
reject("MapAsyncStatus: " +
std::to_string(static_cast<int>(status)));
break;
}
});
});
}
void GPUBuffer::unmap() { _instance.Unmap(); }
uint64_t GPUBuffer::getSize() { return _instance.GetSize(); }
double GPUBuffer::getUsage() {
return static_cast<double>(_instance.GetUsage());
}
wgpu::BufferMapState GPUBuffer::getMapState() {
return _instance.GetMapState();
}
} // namespace rnwgpu