react-native-webgpu 0.26.0 → 0.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/CMakeLists.txt +2 -0
- package/cpp/rnwgpu/ArrayBuffer.h +47 -2
- package/cpp/rnwgpu/DawnEventPump.cpp +227 -0
- package/cpp/rnwgpu/DawnEventPump.h +173 -0
- package/cpp/rnwgpu/WebGPURuntime.cpp +6 -8
- package/cpp/rnwgpu/api/GPU.cpp +1 -1
- package/cpp/rnwgpu/api/GPUAdapter.cpp +1 -1
- package/cpp/rnwgpu/api/GPUBuffer.cpp +100 -27
- package/cpp/rnwgpu/api/GPUBuffer.h +55 -6
- package/cpp/rnwgpu/api/GPUDevice.cpp +57 -55
- package/cpp/rnwgpu/api/GPUDevice.h +6 -0
- package/cpp/rnwgpu/api/GPUQueue.cpp +7 -2
- package/cpp/rnwgpu/api/GPUQueue.h +8 -2
- package/cpp/rnwgpu/api/GPUShaderModule.cpp +1 -1
- package/cpp/rnwgpu/api/RNWebGPU.h +3 -3
- package/cpp/rnwgpu/async/AsyncTaskHandle.h +3 -2
- package/cpp/rnwgpu/async/DeviceTicker.cpp +105 -0
- package/cpp/rnwgpu/async/DeviceTicker.h +71 -0
- package/cpp/rnwgpu/async/RuntimeContext.cpp +56 -70
- package/cpp/rnwgpu/async/RuntimeContext.h +37 -18
- package/lib/commonjs/NativeBufferView.js.map +1 -1
- package/lib/commonjs/NativeBufferViewNativeComponent.web.js +114 -0
- package/lib/commonjs/NativeBufferViewNativeComponent.web.js.map +1 -0
- package/lib/commonjs/WebPolyfillGPUModule.js +79 -0
- package/lib/commonjs/WebPolyfillGPUModule.js.map +1 -1
- package/lib/commonjs/main/index.web.js +11 -0
- package/lib/commonjs/main/index.web.js.map +1 -1
- package/lib/commonjs/utils.js +29 -0
- package/lib/commonjs/utils.js.map +1 -1
- package/lib/module/NativeBufferView.js.map +1 -1
- package/lib/module/NativeBufferViewNativeComponent.web.js +107 -0
- package/lib/module/NativeBufferViewNativeComponent.web.js.map +1 -0
- package/lib/module/WebPolyfillGPUModule.js +80 -1
- package/lib/module/WebPolyfillGPUModule.js.map +1 -1
- package/lib/module/main/index.web.js +1 -0
- package/lib/module/main/index.web.js.map +1 -1
- package/lib/module/utils.js +27 -0
- package/lib/module/utils.js.map +1 -1
- package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.web.d.ts +4 -0
- package/lib/typescript/lib/commonjs/NativeBufferViewNativeComponent.web.d.ts.map +1 -0
- package/lib/typescript/lib/commonjs/utils.d.ts +2 -0
- package/lib/typescript/lib/commonjs/utils.d.ts.map +1 -1
- package/lib/typescript/lib/module/NativeBufferViewNativeComponent.web.d.ts +5 -0
- package/lib/typescript/lib/module/NativeBufferViewNativeComponent.web.d.ts.map +1 -0
- package/lib/typescript/lib/module/main/index.web.d.ts +1 -0
- package/lib/typescript/lib/module/utils.d.ts +2 -0
- package/lib/typescript/lib/module/utils.d.ts.map +1 -1
- package/lib/typescript/src/NativeBufferView.d.ts +6 -1
- package/lib/typescript/src/NativeBufferView.d.ts.map +1 -1
- package/lib/typescript/src/NativeBufferViewNativeComponent.web.d.ts +14 -0
- package/lib/typescript/src/NativeBufferViewNativeComponent.web.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +1 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/main/index.web.d.ts +1 -0
- package/lib/typescript/src/main/index.web.d.ts.map +1 -1
- package/lib/typescript/src/utils.d.ts +2 -0
- package/lib/typescript/src/utils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/NativeBufferView.tsx +6 -1
- package/src/NativeBufferViewNativeComponent.web.ts +119 -0
- package/src/WebPolyfillGPUModule.ts +75 -1
- package/src/index.tsx +2 -1
- package/src/main/index.web.tsx +1 -0
- package/src/utils.ts +25 -0
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
#include "GPUBuffer.h"
|
|
2
2
|
|
|
3
|
+
#include <cstring>
|
|
3
4
|
#include <memory>
|
|
5
|
+
#include <string>
|
|
4
6
|
#include <utility>
|
|
5
7
|
|
|
6
8
|
#include "Convertors.h"
|
|
@@ -9,33 +11,71 @@ namespace rnwgpu {
|
|
|
9
11
|
|
|
10
12
|
std::shared_ptr<ArrayBuffer>
|
|
11
13
|
GPUBuffer::getMappedRange(std::optional<size_t> o, std::optional<size_t> size) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
14
|
+
std::lock_guard<std::mutex> lock(_mappingsMutex);
|
|
15
|
+
const size_t offset = o.value_or(0);
|
|
16
|
+
const uint64_t bufferSize = _instance.GetSize();
|
|
17
|
+
// The rest of the buffer when no size is given.
|
|
18
|
+
const size_t length =
|
|
19
|
+
size.has_value()
|
|
20
|
+
? size.value()
|
|
21
|
+
: static_cast<size_t>(offset < bufferSize ? bufferSize - offset : 0);
|
|
22
|
+
|
|
23
|
+
// Dawn validates the range. The const pointer is there for every kind of
|
|
24
|
+
// mapping; asking for the writable one of a read mapping logs an error.
|
|
25
|
+
const void *mapped = _instance.GetConstMappedRange(offset, length);
|
|
26
|
+
if (mapped == nullptr) {
|
|
27
|
+
throw std::runtime_error(
|
|
28
|
+
"GPUBuffer.getMappedRange(): the buffer is not mapped, or the range is "
|
|
29
|
+
"not within the mapped one (its offset must be a multiple of 8 and its "
|
|
30
|
+
"size a multiple of 4)");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Dawn accepted the range, so it lies within the buffer and cannot wrap.
|
|
34
|
+
const uint64_t start = offset;
|
|
35
|
+
const uint64_t end = start + length;
|
|
36
|
+
for (const auto &mapping : _mappings) {
|
|
37
|
+
if (mapping.intersects(start, end)) {
|
|
38
|
+
throw std::runtime_error(
|
|
39
|
+
"GPUBuffer.getMappedRange(): the range overlaps one that "
|
|
40
|
+
"getMappedRange() already returned for this mapping");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// See Mapping: script gets memory of our own, never Dawn's.
|
|
45
|
+
std::shared_ptr<ArrayBuffer> block;
|
|
46
|
+
if (_writable) {
|
|
47
|
+
// At creation the buffer holds zeroes, and so does a new block.
|
|
48
|
+
block = ArrayBuffer::allocate(length, _mappedAtCreation);
|
|
49
|
+
} else {
|
|
50
|
+
if (_readBlock == nullptr) {
|
|
51
|
+
_readBlock =
|
|
52
|
+
ArrayBuffer::allocate(static_cast<size_t>(bufferSize), false);
|
|
53
|
+
}
|
|
54
|
+
if (_readBlock != nullptr) {
|
|
55
|
+
block = ArrayBuffer::slice(_readBlock, offset, length);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (block == nullptr) {
|
|
59
|
+
throw std::runtime_error(
|
|
60
|
+
"GPUBuffer.getMappedRange(): could not allocate the mapped range");
|
|
30
61
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return
|
|
62
|
+
if (!_mappedAtCreation && length > 0) {
|
|
63
|
+
std::memcpy(block->data(), mapped, length);
|
|
64
|
+
}
|
|
65
|
+
_mappings.push_back(Mapping{start, end, block});
|
|
66
|
+
return block;
|
|
36
67
|
}
|
|
37
68
|
|
|
38
|
-
void GPUBuffer::destroy() {
|
|
69
|
+
void GPUBuffer::destroy() {
|
|
70
|
+
std::lock_guard<std::mutex> lock(_mappingsMutex);
|
|
71
|
+
// Nothing is written back to a buffer that is going away. The views keep
|
|
72
|
+
// their blocks.
|
|
73
|
+
_mappings.clear();
|
|
74
|
+
_readBlock.reset();
|
|
75
|
+
_mappedAtCreation = false;
|
|
76
|
+
_writable = false;
|
|
77
|
+
_instance.Destroy();
|
|
78
|
+
}
|
|
39
79
|
|
|
40
80
|
async::AsyncTaskHandle GPUBuffer::mapAsync(jsi::Runtime &runtime,
|
|
41
81
|
uint64_t modeIn,
|
|
@@ -52,6 +92,16 @@ async::AsyncTaskHandle GPUBuffer::mapAsync(jsi::Runtime &runtime,
|
|
|
52
92
|
auto bufferHandle = _instance;
|
|
53
93
|
uint64_t resolvedOffset = offset.value_or(0);
|
|
54
94
|
|
|
95
|
+
{
|
|
96
|
+
// Dawn does not say which kind of mapping a buffer is in. A buffer can
|
|
97
|
+
// only become mapped from the unmapped state: a call made in any other
|
|
98
|
+
// state is refused and leaves the mapping as it is.
|
|
99
|
+
std::lock_guard<std::mutex> lock(_mappingsMutex);
|
|
100
|
+
if (_instance.GetMapState() == wgpu::BufferMapState::Unmapped) {
|
|
101
|
+
_writable = mode == wgpu::MapMode::Write;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
55
105
|
// Post to the CALLING runtime's context, not the one captured at buffer
|
|
56
106
|
// creation (_async): the buffer may have been created on another runtime and
|
|
57
107
|
// boxed across (e.g. device created on the main JS runtime, mapAsync called
|
|
@@ -65,7 +115,8 @@ async::AsyncTaskHandle GPUBuffer::mapAsync(jsi::Runtime &runtime,
|
|
|
65
115
|
rangeSize](const async::AsyncTaskHandle::ResolveFunction &resolve,
|
|
66
116
|
const async::AsyncTaskHandle::RejectFunction &reject) {
|
|
67
117
|
return bufferHandle.MapAsync(
|
|
68
|
-
mode, resolvedOffset, rangeSize,
|
|
118
|
+
mode, resolvedOffset, rangeSize,
|
|
119
|
+
wgpu::CallbackMode::AllowSpontaneous,
|
|
69
120
|
[resolve, reject](wgpu::MapAsyncStatus status,
|
|
70
121
|
wgpu::StringView message) {
|
|
71
122
|
switch (status) {
|
|
@@ -87,10 +138,32 @@ async::AsyncTaskHandle GPUBuffer::mapAsync(jsi::Runtime &runtime,
|
|
|
87
138
|
break;
|
|
88
139
|
}
|
|
89
140
|
});
|
|
90
|
-
}
|
|
141
|
+
},
|
|
142
|
+
_ticker);
|
|
91
143
|
}
|
|
92
144
|
|
|
93
|
-
void GPUBuffer::unmap() {
|
|
145
|
+
void GPUBuffer::unmap() {
|
|
146
|
+
std::lock_guard<std::mutex> lock(_mappingsMutex);
|
|
147
|
+
if (_writable) {
|
|
148
|
+
for (const auto &mapping : _mappings) {
|
|
149
|
+
const size_t length = static_cast<size_t>(mapping.end - mapping.start);
|
|
150
|
+
if (length == 0) {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
// Asked for now rather than remembered: Dawn has no pointer left once
|
|
154
|
+
// the mapping is gone behind our back (its device was destroyed, say),
|
|
155
|
+
// and then there is nowhere to write.
|
|
156
|
+
void *target = _instance.GetMappedRange(mapping.start, length);
|
|
157
|
+
if (target != nullptr) {
|
|
158
|
+
std::memcpy(target, mapping.block->data(), length);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
_mappings.clear();
|
|
163
|
+
_mappedAtCreation = false;
|
|
164
|
+
_writable = false;
|
|
165
|
+
_instance.Unmap();
|
|
166
|
+
}
|
|
94
167
|
|
|
95
168
|
uint64_t GPUBuffer::getSize() { return _instance.GetSize(); }
|
|
96
169
|
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
3
|
#include <memory>
|
|
4
|
+
#include <mutex>
|
|
4
5
|
#include <optional>
|
|
5
6
|
#include <string>
|
|
7
|
+
#include <utility>
|
|
6
8
|
#include <vector>
|
|
7
9
|
|
|
8
10
|
#include "Unions.h"
|
|
@@ -10,6 +12,7 @@
|
|
|
10
12
|
#include "NativeObject.h"
|
|
11
13
|
|
|
12
14
|
#include "rnwgpu/async/AsyncTaskHandle.h"
|
|
15
|
+
#include "rnwgpu/async/DeviceTicker.h"
|
|
13
16
|
#include "rnwgpu/async/RuntimeContext.h"
|
|
14
17
|
|
|
15
18
|
#include "webgpu/webgpu_cpp.h"
|
|
@@ -26,9 +29,11 @@ public:
|
|
|
26
29
|
|
|
27
30
|
explicit GPUBuffer(wgpu::Buffer instance,
|
|
28
31
|
std::shared_ptr<async::RuntimeContext> async,
|
|
29
|
-
std::
|
|
32
|
+
std::shared_ptr<async::DeviceTicker> ticker,
|
|
33
|
+
std::string label, bool mappedAtCreation)
|
|
30
34
|
: NativeObject(CLASS_NAME), _instance(instance), _async(async),
|
|
31
|
-
_label(label)
|
|
35
|
+
_ticker(std::move(ticker)), _label(label),
|
|
36
|
+
_mappedAtCreation(mappedAtCreation), _writable(mappedAtCreation) {}
|
|
32
37
|
|
|
33
38
|
public:
|
|
34
39
|
std::string getBrand() { return CLASS_NAME; }
|
|
@@ -68,21 +73,65 @@ public:
|
|
|
68
73
|
|
|
69
74
|
inline const wgpu::Buffer get() { return _instance; }
|
|
70
75
|
|
|
71
|
-
size_t getMemoryPressure() override {
|
|
76
|
+
size_t getMemoryPressure() override {
|
|
77
|
+
// A buffer that can be mapped for reading comes with the block its ranges
|
|
78
|
+
// are read from (see Mapping).
|
|
79
|
+
const bool readable =
|
|
80
|
+
static_cast<bool>(_instance.GetUsage() & wgpu::BufferUsage::MapRead);
|
|
81
|
+
return static_cast<size_t>(getSize()) * (readable ? 2 : 1);
|
|
82
|
+
}
|
|
72
83
|
|
|
73
84
|
private:
|
|
74
85
|
wgpu::Buffer _instance;
|
|
75
86
|
std::shared_ptr<async::RuntimeContext> _async;
|
|
87
|
+
// Has the buffer's device asked while a mapAsync() is in flight.
|
|
88
|
+
std::shared_ptr<async::DeviceTicker> _ticker;
|
|
76
89
|
std::string _label;
|
|
90
|
+
|
|
91
|
+
// A range handed to script by getMappedRange(), until the next unmap() or
|
|
92
|
+
// destroy().
|
|
93
|
+
//
|
|
94
|
+
// Script never sees Dawn's memory. A JS ArrayBuffer cannot be detached from
|
|
95
|
+
// here (the web detaches it in unmap()), so a view outlives the mapping, and
|
|
96
|
+
// Dawn's pointer does not: it may point into a staging buffer that unmap()
|
|
97
|
+
// frees, or into a buffer that destroy() frees. Each range is therefore
|
|
98
|
+
// memory of our own, which the ArrayBuffer keeps alive, and the bytes are
|
|
99
|
+
// copied: from Dawn when the range is handed out, to Dawn in unmap() when
|
|
100
|
+
// the mapping can be written. Chrome does the same across its process
|
|
101
|
+
// boundary: the page works on shared memory that the GPU process copies
|
|
102
|
+
// from and to the mapped buffer
|
|
103
|
+
// (gpu/command_buffer/service/dawn_service_memory_transfer_service.cc).
|
|
104
|
+
//
|
|
105
|
+
// Where that memory comes from depends on what unmap() does with it:
|
|
106
|
+
// - A writable mapping gets a new block each time. What script writes
|
|
107
|
+
// through a view of an earlier mapping must never reach the buffer.
|
|
108
|
+
// - A read mapping is served from one block, kept for as long as the buffer
|
|
109
|
+
// (_readBlock), as Chrome keeps one for a mappable buffer
|
|
110
|
+
// (dawn/wire/client/Buffer.cpp). Nothing is written back from it, so all
|
|
111
|
+
// that a view of an earlier mapping can do is show what was mapped last.
|
|
112
|
+
// A new block per mapping would cost its allocation and, above all, a
|
|
113
|
+
// collection each time: it has to be charged to the garbage collector.
|
|
77
114
|
struct Mapping {
|
|
78
115
|
uint64_t start;
|
|
79
116
|
uint64_t end;
|
|
80
|
-
|
|
117
|
+
std::shared_ptr<ArrayBuffer> block;
|
|
118
|
+
inline bool intersects(uint64_t s, uint64_t e) const {
|
|
81
119
|
return s < end && e > start;
|
|
82
120
|
}
|
|
83
|
-
std::shared_ptr<ArrayBuffer> buffer;
|
|
84
121
|
};
|
|
85
|
-
|
|
122
|
+
// A buffer can be reached from several runtimes, hence from several threads.
|
|
123
|
+
// Guards the four members below.
|
|
124
|
+
std::mutex _mappingsMutex;
|
|
125
|
+
std::vector<Mapping> _mappings;
|
|
126
|
+
// As large as the buffer, made by the first read mapping. Only the ranges
|
|
127
|
+
// handed out are ever written to, so the rest of it costs no memory.
|
|
128
|
+
std::shared_ptr<ArrayBuffer> _readBlock;
|
|
129
|
+
// From createBuffer({ mappedAtCreation: true }) to the first unmap(): the
|
|
130
|
+
// buffer holds zeroes, so a range does not need to be read from it.
|
|
131
|
+
bool _mappedAtCreation;
|
|
132
|
+
// Whether unmap() writes the ranges to the buffer: the mapping was made at
|
|
133
|
+
// creation, or with GPUMapMode.WRITE.
|
|
134
|
+
bool _writable;
|
|
86
135
|
};
|
|
87
136
|
|
|
88
137
|
} // namespace rnwgpu
|
|
@@ -264,8 +264,9 @@ GPUDevice::createBuffer(std::shared_ptr<GPUBufferDescriptor> descriptor) {
|
|
|
264
264
|
"GPUDevice::createBuffer(): Error with GPUBufferDescriptor");
|
|
265
265
|
}
|
|
266
266
|
auto result = _instance.CreateBuffer(&desc);
|
|
267
|
-
return std::make_shared<GPUBuffer>(result, _async,
|
|
268
|
-
descriptor->label.value_or("")
|
|
267
|
+
return std::make_shared<GPUBuffer>(result, _async, _ticker,
|
|
268
|
+
descriptor->label.value_or(""),
|
|
269
|
+
static_cast<bool>(desc.mappedAtCreation));
|
|
269
270
|
}
|
|
270
271
|
|
|
271
272
|
std::shared_ptr<GPUSupportedLimits> GPUDevice::getLimits() {
|
|
@@ -286,9 +287,9 @@ std::shared_ptr<GPUAdapterInfo> GPUDevice::getAdapterInfo() {
|
|
|
286
287
|
|
|
287
288
|
std::shared_ptr<GPUQueue> GPUDevice::getQueue() {
|
|
288
289
|
std::call_once(_queueOnce, [this] {
|
|
289
|
-
_queue =
|
|
290
|
-
|
|
291
|
-
|
|
290
|
+
_queue = std::make_shared<GPUQueue>(_instance, _instance.GetQueue(), _async,
|
|
291
|
+
_ticker, _videoFrameBlit, _label,
|
|
292
|
+
weak_from_this());
|
|
292
293
|
});
|
|
293
294
|
return _queue;
|
|
294
295
|
}
|
|
@@ -562,32 +563,32 @@ async::AsyncTaskHandle GPUDevice::createComputePipelineAsync(
|
|
|
562
563
|
// thread that requested it (see GPUBuffer::mapAsync).
|
|
563
564
|
auto context =
|
|
564
565
|
async::RuntimeContext::getOrCreate(runtime, _async->instance());
|
|
565
|
-
return context->postTask(
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
566
|
+
return context->postTask(
|
|
567
|
+
[device = _instance, desc, descriptor,
|
|
568
|
+
pipelineHolder](const async::AsyncTaskHandle::ResolveFunction &resolve,
|
|
569
|
+
const async::AsyncTaskHandle::RejectFunction &reject) {
|
|
570
|
+
(void)descriptor;
|
|
571
|
+
return device.CreateComputePipelineAsync(
|
|
572
|
+
&desc, wgpu::CallbackMode::AllowSpontaneous,
|
|
573
|
+
[pipelineHolder, resolve,
|
|
574
|
+
reject](wgpu::CreatePipelineAsyncStatus status,
|
|
575
|
+
wgpu::ComputePipeline pipeline, wgpu::StringView msg) {
|
|
576
|
+
if (status == wgpu::CreatePipelineAsyncStatus::Success &&
|
|
577
|
+
pipeline) {
|
|
578
|
+
pipelineHolder->_instance = pipeline;
|
|
579
|
+
resolve([pipelineHolder](jsi::Runtime &runtime) mutable {
|
|
580
|
+
return JSIConverter<std::shared_ptr<GPUComputePipeline>>::
|
|
581
|
+
toJSI(runtime, pipelineHolder);
|
|
582
|
+
});
|
|
583
|
+
} else {
|
|
584
|
+
std::string error = msg.length
|
|
585
|
+
? std::string(msg.data, msg.length)
|
|
586
|
+
: "Failed to create compute pipeline";
|
|
587
|
+
reject(std::move(error));
|
|
588
|
+
}
|
|
582
589
|
});
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
? std::string(msg.data, msg.length)
|
|
586
|
-
: "Failed to create compute pipeline";
|
|
587
|
-
reject(std::move(error));
|
|
588
|
-
}
|
|
589
|
-
});
|
|
590
|
-
});
|
|
590
|
+
},
|
|
591
|
+
_ticker);
|
|
591
592
|
}
|
|
592
593
|
|
|
593
594
|
async::AsyncTaskHandle GPUDevice::createRenderPipelineAsync(
|
|
@@ -609,31 +610,32 @@ async::AsyncTaskHandle GPUDevice::createRenderPipelineAsync(
|
|
|
609
610
|
// thread that requested it (see GPUBuffer::mapAsync).
|
|
610
611
|
auto context =
|
|
611
612
|
async::RuntimeContext::getOrCreate(runtime, _async->instance());
|
|
612
|
-
return context->postTask(
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
613
|
+
return context->postTask(
|
|
614
|
+
[device = _instance, desc, descriptor,
|
|
615
|
+
pipelineHolder](const async::AsyncTaskHandle::ResolveFunction &resolve,
|
|
616
|
+
const async::AsyncTaskHandle::RejectFunction &reject) {
|
|
617
|
+
(void)descriptor;
|
|
618
|
+
return device.CreateRenderPipelineAsync(
|
|
619
|
+
&desc, wgpu::CallbackMode::AllowSpontaneous,
|
|
620
|
+
[pipelineHolder, resolve,
|
|
621
|
+
reject](wgpu::CreatePipelineAsyncStatus status,
|
|
622
|
+
wgpu::RenderPipeline pipeline, wgpu::StringView msg) {
|
|
623
|
+
if (status == wgpu::CreatePipelineAsyncStatus::Success &&
|
|
624
|
+
pipeline) {
|
|
625
|
+
pipelineHolder->_instance = pipeline;
|
|
626
|
+
resolve([pipelineHolder](jsi::Runtime &runtime) mutable {
|
|
627
|
+
return JSIConverter<std::shared_ptr<GPURenderPipeline>>::
|
|
628
|
+
toJSI(runtime, pipelineHolder);
|
|
629
|
+
});
|
|
630
|
+
} else {
|
|
631
|
+
std::string error = msg.length
|
|
632
|
+
? std::string(msg.data, msg.length)
|
|
633
|
+
: "Failed to create render pipeline";
|
|
634
|
+
reject(std::move(error));
|
|
635
|
+
}
|
|
629
636
|
});
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
: "Failed to create render pipeline";
|
|
633
|
-
reject(std::move(error));
|
|
634
|
-
}
|
|
635
|
-
});
|
|
636
|
-
});
|
|
637
|
+
},
|
|
638
|
+
_ticker);
|
|
637
639
|
}
|
|
638
640
|
|
|
639
641
|
void GPUDevice::pushErrorScope(wgpu::ErrorFilter filter) {
|
|
@@ -653,7 +655,7 @@ async::AsyncTaskHandle GPUDevice::popErrorScope(jsi::Runtime &runtime) {
|
|
|
653
655
|
const async::AsyncTaskHandle::RejectFunction
|
|
654
656
|
&reject) {
|
|
655
657
|
return device.PopErrorScope(
|
|
656
|
-
wgpu::CallbackMode::
|
|
658
|
+
wgpu::CallbackMode::AllowSpontaneous,
|
|
657
659
|
[resolve, reject](wgpu::PopErrorScopeStatus status,
|
|
658
660
|
wgpu::ErrorType type, wgpu::StringView message) {
|
|
659
661
|
if (status == wgpu::PopErrorScopeStatus::Error ||
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
#include "NativeObject.h"
|
|
19
19
|
|
|
20
20
|
#include "rnwgpu/async/AsyncTaskHandle.h"
|
|
21
|
+
#include "rnwgpu/async/DeviceTicker.h"
|
|
21
22
|
#include "rnwgpu/async/RuntimeContext.h"
|
|
22
23
|
|
|
23
24
|
#include "webgpu/webgpu_cpp.h"
|
|
@@ -75,6 +76,8 @@ public:
|
|
|
75
76
|
std::shared_ptr<async::RuntimeContext> async,
|
|
76
77
|
std::string label, Origin origin)
|
|
77
78
|
: NativeObject(CLASS_NAME), _instance(instance), _async(async),
|
|
79
|
+
_ticker(std::make_shared<async::DeviceTicker>(
|
|
80
|
+
instance, async, origin == Origin::Requested)),
|
|
78
81
|
_label(label), _origin(origin) {}
|
|
79
82
|
|
|
80
83
|
~GPUDevice() override {
|
|
@@ -377,6 +380,9 @@ private:
|
|
|
377
380
|
|
|
378
381
|
wgpu::Device _instance;
|
|
379
382
|
std::shared_ptr<async::RuntimeContext> _async;
|
|
383
|
+
// Has the device asked about its work in flight, shared with its queue and
|
|
384
|
+
// its buffers.
|
|
385
|
+
std::shared_ptr<async::DeviceTicker> _ticker;
|
|
380
386
|
std::string _label;
|
|
381
387
|
const Origin _origin;
|
|
382
388
|
// device.queue is [SameObject]: made on the first read, then returned on
|
|
@@ -34,6 +34,10 @@ void GPUQueue::submit(
|
|
|
34
34
|
return;
|
|
35
35
|
}
|
|
36
36
|
_instance.Submit(bufs_size, bufs.data());
|
|
37
|
+
// The submit ticked the device, with this work still in flight: once
|
|
38
|
+
// nothing else is submitted for a while, the device is asked again, so
|
|
39
|
+
// that Dawn frees what the work was using (see DawnEventPump).
|
|
40
|
+
_ticker->workSubmitted();
|
|
37
41
|
// Now and then, what the pipelines compiled since the last flush added to
|
|
38
42
|
// Dawn's pipeline cache goes to the disk (see
|
|
39
43
|
// GPUDevice::flushPipelineCacheIfDue).
|
|
@@ -102,7 +106,7 @@ async::AsyncTaskHandle GPUQueue::onSubmittedWorkDone(jsi::Runtime &runtime) {
|
|
|
102
106
|
[queue](const async::AsyncTaskHandle::ResolveFunction &resolve,
|
|
103
107
|
const async::AsyncTaskHandle::RejectFunction &reject) {
|
|
104
108
|
return queue.OnSubmittedWorkDone(
|
|
105
|
-
wgpu::CallbackMode::
|
|
109
|
+
wgpu::CallbackMode::AllowSpontaneous,
|
|
106
110
|
[resolve, reject](wgpu::QueueWorkDoneStatus status,
|
|
107
111
|
wgpu::StringView message) {
|
|
108
112
|
if (status == wgpu::QueueWorkDoneStatus::Success) {
|
|
@@ -114,7 +118,8 @@ async::AsyncTaskHandle GPUQueue::onSubmittedWorkDone(jsi::Runtime &runtime) {
|
|
|
114
118
|
reject(std::move(error));
|
|
115
119
|
}
|
|
116
120
|
});
|
|
117
|
-
}
|
|
121
|
+
},
|
|
122
|
+
_ticker);
|
|
118
123
|
}
|
|
119
124
|
|
|
120
125
|
void GPUQueue::copyVideoFrameToTexture(
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
#include "NativeObject.h"
|
|
11
11
|
|
|
12
12
|
#include "rnwgpu/async/AsyncTaskHandle.h"
|
|
13
|
+
#include "rnwgpu/async/DeviceTicker.h"
|
|
13
14
|
#include "rnwgpu/async/RuntimeContext.h"
|
|
14
15
|
|
|
15
16
|
#include "webgpu/webgpu_cpp.h"
|
|
@@ -35,11 +36,13 @@ public:
|
|
|
35
36
|
|
|
36
37
|
explicit GPUQueue(wgpu::Device device, wgpu::Queue instance,
|
|
37
38
|
std::shared_ptr<async::RuntimeContext> async,
|
|
39
|
+
std::shared_ptr<async::DeviceTicker> ticker,
|
|
38
40
|
std::shared_ptr<VideoFrameBlit> videoFrameBlit,
|
|
39
41
|
std::string label, std::weak_ptr<GPUDevice> deviceHost)
|
|
40
42
|
: NativeObject(CLASS_NAME), _device(device), _instance(instance),
|
|
41
|
-
_async(async),
|
|
42
|
-
|
|
43
|
+
_async(async), _ticker(std::move(ticker)),
|
|
44
|
+
_videoFrameBlit(std::move(videoFrameBlit)), _label(label),
|
|
45
|
+
_deviceHost(std::move(deviceHost)) {}
|
|
43
46
|
|
|
44
47
|
public:
|
|
45
48
|
std::string getBrand() { return CLASS_NAME; }
|
|
@@ -104,6 +107,9 @@ private:
|
|
|
104
107
|
wgpu::Device _device;
|
|
105
108
|
wgpu::Queue _instance;
|
|
106
109
|
std::shared_ptr<async::RuntimeContext> _async;
|
|
110
|
+
// Has the device asked while onSubmittedWorkDone() is in flight, and a
|
|
111
|
+
// while after the last submit().
|
|
112
|
+
std::shared_ptr<async::DeviceTicker> _ticker;
|
|
107
113
|
std::shared_ptr<VideoFrameBlit> _videoFrameBlit;
|
|
108
114
|
std::string _label;
|
|
109
115
|
// The device's wrapper, which submit() has flush its pipeline cache now and
|
|
@@ -20,7 +20,7 @@ GPUShaderModule::getCompilationInfo(jsi::Runtime &runtime) {
|
|
|
20
20
|
const async::AsyncTaskHandle::RejectFunction &reject) {
|
|
21
21
|
auto result = std::make_shared<GPUCompilationInfo>();
|
|
22
22
|
return module.GetCompilationInfo(
|
|
23
|
-
wgpu::CallbackMode::
|
|
23
|
+
wgpu::CallbackMode::AllowSpontaneous,
|
|
24
24
|
[result, resolve,
|
|
25
25
|
reject](wgpu::CompilationInfoRequestStatus status,
|
|
26
26
|
const wgpu::CompilationInfo *compilationInfo) {
|
|
@@ -140,9 +140,9 @@ public:
|
|
|
140
140
|
// Wrap an externally created WGPUDevice (passed as a BigInt pointer, e.g.
|
|
141
141
|
// Skia's Graphite device from Skia.getNativeDevice()) in a GPUDevice.
|
|
142
142
|
// AddRefs the handle, so the original owner keeps its reference. Only sound
|
|
143
|
-
// because the process links a single Dawn copy.
|
|
144
|
-
//
|
|
145
|
-
//
|
|
143
|
+
// because the process links a single Dawn copy. Dawn is asked about the
|
|
144
|
+
// device's async work on the instance that owns it (RuntimeContext is per
|
|
145
|
+
// runtime and per instance), which is not necessarily the shared one:
|
|
146
146
|
// WebGPURuntime adopts Skia's instance only when Skia's getter is reachable
|
|
147
147
|
// as the instance is first used (on Android, only once librnskia.so is
|
|
148
148
|
// loaded), and other exporters have their own.
|
|
@@ -20,8 +20,9 @@ class RuntimeContext;
|
|
|
20
20
|
* Represents a pending asynchronous WebGPU operation that can be converted into
|
|
21
21
|
* a JavaScript Promise.
|
|
22
22
|
*
|
|
23
|
-
* The resolve and reject functions run on any thread (
|
|
24
|
-
*
|
|
23
|
+
* The resolve and reject functions run on any thread (whichever one Dawn
|
|
24
|
+
* completes the operation on) and never touch JSI: they hand the settling to
|
|
25
|
+
* the owning
|
|
25
26
|
* RuntimeContext, which runs it on its runtime's own thread (see
|
|
26
27
|
* RuntimeContext::post).
|
|
27
28
|
*/
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
#include "DeviceTicker.h"
|
|
2
|
+
|
|
3
|
+
#include <atomic>
|
|
4
|
+
#include <cstring>
|
|
5
|
+
#include <memory>
|
|
6
|
+
#include <utility>
|
|
7
|
+
|
|
8
|
+
#include "dawn/native/DawnNative.h"
|
|
9
|
+
|
|
10
|
+
#include "RuntimeContext.h"
|
|
11
|
+
|
|
12
|
+
namespace rnwgpu::async {
|
|
13
|
+
|
|
14
|
+
namespace {
|
|
15
|
+
|
|
16
|
+
bool hasToggle(const wgpu::Device &device, const char *name) {
|
|
17
|
+
for (const char *toggle : dawn::native::GetTogglesUsed(device.Get())) {
|
|
18
|
+
if (std::strcmp(toggle, name) == 0) {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
} // namespace
|
|
26
|
+
|
|
27
|
+
DeviceTicker::DeviceTicker(wgpu::Device device,
|
|
28
|
+
std::weak_ptr<RuntimeContext> context, bool owned)
|
|
29
|
+
: _device(std::move(device)), _context(std::move(context)), _owned(owned),
|
|
30
|
+
_synchronized(
|
|
31
|
+
_device.HasFeature(wgpu::FeatureName::ImplicitDeviceSynchronization)),
|
|
32
|
+
_spontaneous(hasToggle(_device, "spontaneous_queue_events")) {}
|
|
33
|
+
|
|
34
|
+
std::shared_ptr<void> DeviceTicker::operationStarted() {
|
|
35
|
+
if (_spontaneous) {
|
|
36
|
+
return nullptr;
|
|
37
|
+
}
|
|
38
|
+
if (_synchronized) {
|
|
39
|
+
return DawnEventPump::shared().operationStarted({_device.Get(), nullptr},
|
|
40
|
+
poll());
|
|
41
|
+
}
|
|
42
|
+
auto context = _context.lock();
|
|
43
|
+
if (context == nullptr) {
|
|
44
|
+
// The runtime that could tick the device is gone, and its promises with
|
|
45
|
+
// it.
|
|
46
|
+
return nullptr;
|
|
47
|
+
}
|
|
48
|
+
return DawnEventPump::shared().operationStarted(
|
|
49
|
+
{_device.Get(), context.get()}, poll());
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
void DeviceTicker::workSubmitted() {
|
|
53
|
+
if (!_owned) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (_synchronized) {
|
|
57
|
+
DawnEventPump::shared().workSubmitted({_device.Get(), nullptr}, poll());
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (auto context = _context.lock()) {
|
|
61
|
+
DawnEventPump::shared().workSubmitted({_device.Get(), context.get()},
|
|
62
|
+
poll());
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const DawnEventPump::Poll &DeviceTicker::poll() {
|
|
67
|
+
std::call_once(_pollOnce, [this] {
|
|
68
|
+
if (_synchronized) {
|
|
69
|
+
_poll = [device = _device]() {
|
|
70
|
+
return dawn::native::DeviceTick(device.Get());
|
|
71
|
+
};
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
// The tick is the runtime thread's to make, so the pump posts it there:
|
|
75
|
+
// one at a time, so that a busy runtime does not find a pile of them,
|
|
76
|
+
// and without waking a runtime that polls its mailbox, which is polling
|
|
77
|
+
// already while it awaits a promise. What Dawn answers there is not
|
|
78
|
+
// waited for: the poll says there is work left, and the lazy polls
|
|
79
|
+
// after a submit are all made, a handful of them. The poll holds this
|
|
80
|
+
// ticker, and its device, weakly: their last reference must go on the
|
|
81
|
+
// runtime's thread, not on the pump's.
|
|
82
|
+
auto posted = std::make_shared<std::atomic<bool>>(false);
|
|
83
|
+
_poll = [weak = weak_from_this(), weakContext = _context, posted]() {
|
|
84
|
+
auto context = weakContext.lock();
|
|
85
|
+
if (context == nullptr || weak.expired()) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
if (posted->exchange(true, std::memory_order_acq_rel)) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
context->post(
|
|
92
|
+
[weak, posted](jsi::Runtime &) {
|
|
93
|
+
posted->store(false, std::memory_order_release);
|
|
94
|
+
if (auto self = weak.lock()) {
|
|
95
|
+
dawn::native::DeviceTick(self->_device.Get());
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
RuntimeContext::Wake::No);
|
|
99
|
+
return true;
|
|
100
|
+
};
|
|
101
|
+
});
|
|
102
|
+
return _poll;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
} // namespace rnwgpu::async
|