react-native-webgpu 0.25.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/README.md +9 -1
- 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 +26 -5
- package/cpp/rnwgpu/api/descriptors/CreateNativeBufferFromPointerOptions.h +46 -0
- package/cpp/rnwgpu/api/descriptors/GPUExternalTextureDescriptor.h +21 -15
- 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/index.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/commonjs/video.js +1 -1
- package/lib/commonjs/video.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/index.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/module/video.js +1 -1
- package/lib/module/video.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/commonjs/video.d.ts +1 -1
- package/lib/typescript/lib/commonjs/video.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/lib/module/video.d.ts +1 -1
- package/lib/typescript/lib/module/video.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 +3 -3
- 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/types.d.ts +12 -0
- package/lib/typescript/src/types.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/lib/typescript/src/video.d.ts +2 -2
- package/lib/typescript/src/video.d.ts.map +1 -1
- package/package.json +1 -1
- package/scripts/prepare-apple-binaries.sh +9 -3
- package/src/NativeBufferView.tsx +6 -1
- package/src/NativeBufferViewNativeComponent.web.ts +119 -0
- package/src/WebPolyfillGPUModule.ts +75 -1
- package/src/index.tsx +7 -2
- package/src/main/index.web.tsx +1 -0
- package/src/types.ts +13 -0
- package/src/utils.ts +25 -0
- package/src/video.ts +6 -1
package/README.md
CHANGED
|
@@ -13,7 +13,15 @@ WebGPU for React Native, powered by Dawn. See [Documentation](https://wcandillon
|
|
|
13
13
|
|
|
14
14
|
<div align="center">
|
|
15
15
|
<a href="https://expo.dev/" target="_blank" rel="sponsored">
|
|
16
|
-
<img width="80" height="80" alt="Expo" src="https://
|
|
16
|
+
<img width="80" height="80" alt="Expo" src="https://firebasestorage.googleapis.com/v0/b/start-react-native.appspot.com/o/expo.png?alt=media&token=6e688636-6a41-4d6a-b3ad-ec8ae1308b00" />
|
|
17
|
+
</a>
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
## Gold Sponsors
|
|
21
|
+
|
|
22
|
+
<div align="center">
|
|
23
|
+
<a href="https://hark.com/" target="_blank" rel="sponsored">
|
|
24
|
+
<img width="80" height="80" alt="Hark" src="https://firebasestorage.googleapis.com/v0/b/start-react-native.appspot.com/o/hark.png?alt=media&token=936e04d3-2cdb-4350-bbc6-13c99bc135f3" />
|
|
17
25
|
</a>
|
|
18
26
|
</div>
|
|
19
27
|
|
package/android/CMakeLists.txt
CHANGED
|
@@ -56,6 +56,7 @@ add_library(${PACKAGE_NAME} SHARED
|
|
|
56
56
|
../cpp/rnwgpu/api/GPUComputePipeline.cpp
|
|
57
57
|
../cpp/rnwgpu/api/GPUCanvasContext.cpp
|
|
58
58
|
../cpp/rnwgpu/DawnBlobCache.cpp
|
|
59
|
+
../cpp/rnwgpu/DawnEventPump.cpp
|
|
59
60
|
../cpp/rnwgpu/GpuPurge.cpp
|
|
60
61
|
../cpp/rnwgpu/NativeBufferViewRegistry.cpp
|
|
61
62
|
../cpp/rnwgpu/AnimationFrames.cpp
|
|
@@ -64,6 +65,7 @@ add_library(${PACKAGE_NAME} SHARED
|
|
|
64
65
|
../cpp/jsi/Promise.cpp
|
|
65
66
|
../cpp/jsi/JSICache.cpp
|
|
66
67
|
../cpp/rnwgpu/async/RuntimeContext.cpp
|
|
68
|
+
../cpp/rnwgpu/async/DeviceTicker.cpp
|
|
67
69
|
../cpp/rnwgpu/async/AsyncTaskHandle.cpp
|
|
68
70
|
)
|
|
69
71
|
|
package/cpp/rnwgpu/ArrayBuffer.h
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
|
|
4
4
|
#include <cmath>
|
|
5
5
|
#include <cstdint>
|
|
6
|
+
#include <cstdlib>
|
|
6
7
|
#include <memory>
|
|
8
|
+
#include <utility>
|
|
7
9
|
|
|
8
10
|
#include "JSIConverter.h"
|
|
9
11
|
|
|
@@ -12,18 +14,53 @@ namespace rnwgpu {
|
|
|
12
14
|
namespace jsi = facebook::jsi;
|
|
13
15
|
|
|
14
16
|
struct ArrayBuffer : jsi::MutableBuffer {
|
|
17
|
+
// A view of memory that someone else owns and keeps alive.
|
|
15
18
|
ArrayBuffer(void *data, size_t size, size_t bytesPerElement)
|
|
16
19
|
: _data(data), _size(size), _bytesPerElement(bytesPerElement) {}
|
|
17
20
|
|
|
18
|
-
|
|
21
|
+
// A block of `size` bytes that the buffer owns, and that lives for as long
|
|
22
|
+
// as the JS ArrayBuffer made from it can be reached. Zeroed on request,
|
|
23
|
+
// otherwise left for the caller to fill. Null when the memory cannot be had.
|
|
24
|
+
static std::shared_ptr<ArrayBuffer> allocate(size_t size, bool zeroed) {
|
|
25
|
+
// Never a null pointer, even for an empty block.
|
|
26
|
+
const size_t bytes = size > 0 ? size : 1;
|
|
27
|
+
void *storage = zeroed ? std::calloc(bytes, 1) : std::malloc(bytes);
|
|
28
|
+
if (storage == nullptr) {
|
|
29
|
+
return nullptr;
|
|
30
|
+
}
|
|
31
|
+
auto buffer = std::make_shared<ArrayBuffer>(storage, size, 1);
|
|
32
|
+
buffer->_storage = storage;
|
|
33
|
+
return buffer;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// A part of `block`, which it keeps alive.
|
|
37
|
+
static std::shared_ptr<ArrayBuffer> slice(std::shared_ptr<ArrayBuffer> block,
|
|
38
|
+
size_t offset, size_t size) {
|
|
39
|
+
auto part = std::make_shared<ArrayBuffer>(block->data() + offset, size, 1);
|
|
40
|
+
part->_block = std::move(block);
|
|
41
|
+
return part;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
ArrayBuffer(const ArrayBuffer &) = delete;
|
|
45
|
+
ArrayBuffer &operator=(const ArrayBuffer &) = delete;
|
|
46
|
+
|
|
47
|
+
~ArrayBuffer() override { std::free(_storage); }
|
|
19
48
|
|
|
20
49
|
size_t size() const override { return _size; }
|
|
21
50
|
|
|
22
51
|
uint8_t *data() override { return static_cast<uint8_t *>(_data); }
|
|
23
52
|
|
|
53
|
+
// How much of the memory it describes the buffer owns: all of a block made
|
|
54
|
+
// with allocate(), none of a view or of a part of a block.
|
|
55
|
+
size_t ownedSize() const { return _storage != nullptr ? _size : 0; }
|
|
56
|
+
|
|
24
57
|
void *_data;
|
|
25
58
|
size_t _size;
|
|
26
59
|
size_t _bytesPerElement;
|
|
60
|
+
|
|
61
|
+
private:
|
|
62
|
+
void *_storage = nullptr;
|
|
63
|
+
std::shared_ptr<ArrayBuffer> _block;
|
|
27
64
|
};
|
|
28
65
|
|
|
29
66
|
static std::shared_ptr<ArrayBuffer>
|
|
@@ -110,7 +147,15 @@ template <> struct JSIConverter<std::shared_ptr<ArrayBuffer>> {
|
|
|
110
147
|
|
|
111
148
|
static jsi::Value toJSI(jsi::Runtime &runtime,
|
|
112
149
|
std::shared_ptr<ArrayBuffer> arg) {
|
|
113
|
-
|
|
150
|
+
const size_t owned = arg->ownedSize();
|
|
151
|
+
jsi::ArrayBuffer result(runtime, std::move(arg));
|
|
152
|
+
if (owned > 0) {
|
|
153
|
+
// The engine does not count memory it did not allocate: without this a
|
|
154
|
+
// loop over large mapped ranges would pile up blocks before any
|
|
155
|
+
// collection ran.
|
|
156
|
+
result.setExternalMemoryPressure(runtime, owned);
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
114
159
|
}
|
|
115
160
|
};
|
|
116
161
|
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
#include "DawnEventPump.h"
|
|
2
|
+
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <functional>
|
|
5
|
+
#include <memory>
|
|
6
|
+
#include <system_error>
|
|
7
|
+
#include <utility>
|
|
8
|
+
#include <vector>
|
|
9
|
+
|
|
10
|
+
#if defined(__APPLE__) || defined(__ANDROID__) || defined(__linux__)
|
|
11
|
+
#include <pthread.h>
|
|
12
|
+
#endif
|
|
13
|
+
|
|
14
|
+
namespace rnwgpu {
|
|
15
|
+
|
|
16
|
+
DawnEventPump::Duration
|
|
17
|
+
DawnEventPump::Policy::awaitedInterval(Duration sinceStart) const {
|
|
18
|
+
for (const auto &step : awaited) {
|
|
19
|
+
if (sinceStart < step.until) {
|
|
20
|
+
return step.interval;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return awaited.back().interval;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
std::size_t
|
|
27
|
+
DawnEventPump::SourceHash::operator()(const Source &source) const noexcept {
|
|
28
|
+
const auto device = std::hash<const void *>()(source.device);
|
|
29
|
+
const auto thread = std::hash<const void *>()(source.thread);
|
|
30
|
+
return device ^
|
|
31
|
+
(thread + 0x9e3779b97f4a7c15ULL + (device << 6) + (device >> 2));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
DawnEventPump &DawnEventPump::shared() {
|
|
35
|
+
static auto *pump = new DawnEventPump();
|
|
36
|
+
return *pump;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
DawnEventPump::DawnEventPump() : DawnEventPump(Policy{}) {}
|
|
40
|
+
|
|
41
|
+
DawnEventPump::DawnEventPump(const Policy &policy) : _policy(policy) {}
|
|
42
|
+
|
|
43
|
+
DawnEventPump::~DawnEventPump() {
|
|
44
|
+
{
|
|
45
|
+
std::lock_guard<std::mutex> lock(_mutex);
|
|
46
|
+
_stopping = true;
|
|
47
|
+
}
|
|
48
|
+
_condition.notify_one();
|
|
49
|
+
if (_thread.joinable()) {
|
|
50
|
+
_thread.join();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
std::shared_ptr<void> DawnEventPump::operationStarted(const Source &source,
|
|
55
|
+
const Poll &poll) {
|
|
56
|
+
{
|
|
57
|
+
std::lock_guard<std::mutex> lock(_mutex);
|
|
58
|
+
// Throws when no thread is to be had: nothing would learn of the
|
|
59
|
+
// operation's completion, and the caller must not promise it.
|
|
60
|
+
startThread();
|
|
61
|
+
const auto now = Clock::now();
|
|
62
|
+
Entry &entry = entryFor(source, poll, now);
|
|
63
|
+
entry.awaited++;
|
|
64
|
+
entry.awaitedSince = now;
|
|
65
|
+
entry.due = now + _policy.awaitedInterval(Duration::zero());
|
|
66
|
+
wakeFor(entry.due);
|
|
67
|
+
}
|
|
68
|
+
// The pump itself as the token's address: it is only there to make the
|
|
69
|
+
// token a non-null one.
|
|
70
|
+
return std::shared_ptr<void>(
|
|
71
|
+
this, [this, source](void *) { operationFinished(source); });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
void DawnEventPump::operationFinished(const Source &source) {
|
|
75
|
+
std::lock_guard<std::mutex> lock(_mutex);
|
|
76
|
+
auto it = _entries.find(source);
|
|
77
|
+
if (it == _entries.end()) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
Entry &entry = it->second;
|
|
81
|
+
if (--entry.awaited > 0) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
// Later than the poll the thread sleeps until: it finds out when it wakes.
|
|
85
|
+
// The entry is dropped there too, never here: this may be a callback of
|
|
86
|
+
// Dawn, where the last reference to its device must not go.
|
|
87
|
+
const auto now = Clock::now();
|
|
88
|
+
entry.due =
|
|
89
|
+
now + (entry.idlePolls == kNoIdlePolls ? _policy.linger
|
|
90
|
+
: _policy.idle[entry.idlePolls]);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
void DawnEventPump::workSubmitted(const Source &source, const Poll &poll) {
|
|
94
|
+
try {
|
|
95
|
+
std::lock_guard<std::mutex> lock(_mutex);
|
|
96
|
+
startThread();
|
|
97
|
+
const auto now = Clock::now();
|
|
98
|
+
Entry &entry = entryFor(source, poll, now);
|
|
99
|
+
entry.idlePolls = 0;
|
|
100
|
+
entry.work++;
|
|
101
|
+
if (entry.awaited == 0) {
|
|
102
|
+
// Usually later than before (a render loop submits again before the
|
|
103
|
+
// poll is due), which wakes nobody.
|
|
104
|
+
entry.due = now + _policy.idle[0];
|
|
105
|
+
wakeFor(entry.due);
|
|
106
|
+
}
|
|
107
|
+
} catch (const std::system_error &) {
|
|
108
|
+
// No thread to be had: Dawn frees what this work was using with the next
|
|
109
|
+
// tick, whoever makes it.
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
DawnEventPump::Entry &DawnEventPump::entryFor(const Source &source,
|
|
114
|
+
const Poll &poll,
|
|
115
|
+
Clock::time_point now) {
|
|
116
|
+
auto [it, inserted] = _entries.try_emplace(source);
|
|
117
|
+
if (inserted) {
|
|
118
|
+
it->second.poll = std::make_shared<const Poll>(poll);
|
|
119
|
+
it->second.due = now + _policy.linger;
|
|
120
|
+
}
|
|
121
|
+
return it->second;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
void DawnEventPump::wakeFor(Clock::time_point due) {
|
|
125
|
+
if (due < _wakeAt) {
|
|
126
|
+
_wakeAt = due;
|
|
127
|
+
_condition.notify_one();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
void DawnEventPump::startThread() {
|
|
132
|
+
if (_started) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
_thread = std::thread([this]() { run(); });
|
|
136
|
+
_started = true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
void DawnEventPump::run() {
|
|
140
|
+
#if defined(__APPLE__)
|
|
141
|
+
pthread_setname_np("Dawn Event Pump");
|
|
142
|
+
#elif defined(__ANDROID__) || defined(__linux__)
|
|
143
|
+
pthread_setname_np(pthread_self(), "Dawn Event Pump");
|
|
144
|
+
#endif
|
|
145
|
+
struct Due {
|
|
146
|
+
Source source;
|
|
147
|
+
std::shared_ptr<const Poll> poll;
|
|
148
|
+
unsigned work = 0;
|
|
149
|
+
bool more = false;
|
|
150
|
+
};
|
|
151
|
+
std::vector<Due> due;
|
|
152
|
+
std::vector<std::shared_ptr<const Poll>> dropped;
|
|
153
|
+
std::unique_lock<std::mutex> lock(_mutex);
|
|
154
|
+
for (;;) {
|
|
155
|
+
if (_stopping) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const auto now = Clock::now();
|
|
159
|
+
auto next = Clock::time_point::max();
|
|
160
|
+
for (auto it = _entries.begin(); it != _entries.end();) {
|
|
161
|
+
Entry &entry = it->second;
|
|
162
|
+
if (entry.due > now) {
|
|
163
|
+
next = std::min(next, entry.due);
|
|
164
|
+
++it;
|
|
165
|
+
} else if (entry.awaited == 0 && entry.idlePolls == kNoIdlePolls) {
|
|
166
|
+
// Nothing left to poll for, and nothing came since.
|
|
167
|
+
dropped.push_back(std::move(entry.poll));
|
|
168
|
+
it = _entries.erase(it);
|
|
169
|
+
} else {
|
|
170
|
+
due.push_back({it->first, entry.poll, entry.work});
|
|
171
|
+
++it;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (due.empty() && dropped.empty()) {
|
|
175
|
+
_wakeAt = next;
|
|
176
|
+
if (next == Clock::time_point::max()) {
|
|
177
|
+
_condition.wait(lock);
|
|
178
|
+
} else {
|
|
179
|
+
_condition.wait_until(lock, next);
|
|
180
|
+
}
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
// Awake until the next sleep: nobody needs to wake the thread meanwhile.
|
|
184
|
+
_wakeAt = Clock::time_point::min();
|
|
185
|
+
lock.unlock();
|
|
186
|
+
// The polls run, and those of the dropped sources go, with no lock held:
|
|
187
|
+
// a poll runs callbacks of Dawn, which may start or finish operations
|
|
188
|
+
// here, and the last reference to a device may be a poll's.
|
|
189
|
+
dropped.clear();
|
|
190
|
+
for (auto &item : due) {
|
|
191
|
+
item.more = (*item.poll)();
|
|
192
|
+
}
|
|
193
|
+
lock.lock();
|
|
194
|
+
const auto after = Clock::now();
|
|
195
|
+
for (auto &item : due) {
|
|
196
|
+
auto it = _entries.find(item.source);
|
|
197
|
+
if (it == _entries.end()) {
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
Entry &entry = it->second;
|
|
201
|
+
if (entry.awaited > 0) {
|
|
202
|
+
entry.due = after + _policy.awaitedInterval(after - entry.awaitedSince);
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
if (entry.work != item.work) {
|
|
206
|
+
// Work came while the poll ran, and set when to ask about it.
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
if (entry.idlePolls != kNoIdlePolls) {
|
|
210
|
+
// A lazy poll, or the poll that completed the last awaited operation:
|
|
211
|
+
// either way Dawn said whether it has anything left.
|
|
212
|
+
entry.idlePolls = item.more ? entry.idlePolls + 1 : kNoIdlePolls;
|
|
213
|
+
if (entry.idlePolls >= static_cast<int>(_policy.idle.size())) {
|
|
214
|
+
entry.idlePolls = kNoIdlePolls;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
entry.due = after + (entry.idlePolls == kNoIdlePolls
|
|
218
|
+
? _policy.linger
|
|
219
|
+
: _policy.idle[entry.idlePolls]);
|
|
220
|
+
}
|
|
221
|
+
// The entries still hold their polls: nothing is destroyed here, with the
|
|
222
|
+
// lock held.
|
|
223
|
+
due.clear();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
} // namespace rnwgpu
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <array>
|
|
4
|
+
#include <chrono>
|
|
5
|
+
#include <condition_variable>
|
|
6
|
+
#include <cstddef>
|
|
7
|
+
#include <functional>
|
|
8
|
+
#include <memory>
|
|
9
|
+
#include <mutex>
|
|
10
|
+
#include <thread>
|
|
11
|
+
#include <unordered_map>
|
|
12
|
+
|
|
13
|
+
namespace rnwgpu {
|
|
14
|
+
|
|
15
|
+
// Asks Dawn about the devices that have work in flight, so that nothing ever
|
|
16
|
+
// has to wait in Dawn for it.
|
|
17
|
+
//
|
|
18
|
+
// Dawn completes an operation (a buffer mapped, submitted work done) when it
|
|
19
|
+
// learns that the GPU is through with it. Metal tells it; on the other
|
|
20
|
+
// backends nobody does, and Dawn only finds out when it is asked: a tick of
|
|
21
|
+
// the device, which never waits. A tick is also when Dawn frees what it had to
|
|
22
|
+
// keep until submitted work completed, and runs the tasks it deferred.
|
|
23
|
+
//
|
|
24
|
+
// Waiting in Dawn instead (Instance::WaitAny with a timeout) is ruled out. On
|
|
25
|
+
// Vulkan the wait holds the list of the queue's fences for as long as it
|
|
26
|
+
// lasts, which a submit needs: a thread waiting for 200 ms of GPU work held
|
|
27
|
+
// the next submit of every other thread for those 200 ms. Dawn also refuses
|
|
28
|
+
// to wait for futures of different sources at once off Metal.
|
|
29
|
+
//
|
|
30
|
+
// So one thread polls, and only while there is something to learn:
|
|
31
|
+
//
|
|
32
|
+
// - Often, while an operation someone awaits is in flight on a device whose
|
|
33
|
+
// completions nothing reports (operationStarted): its callback runs within
|
|
34
|
+
// a poll of the GPU being done.
|
|
35
|
+
// - A few times, lazily, after work nobody awaits (workSubmitted): an app
|
|
36
|
+
// that submitted its last frame has Dawn free what that frame was using,
|
|
37
|
+
// and nothing polls while frames keep coming, since each submit ticks.
|
|
38
|
+
//
|
|
39
|
+
// What a poll does is the caller's, which knows the device and the thread
|
|
40
|
+
// that may use it. The pump knows when. A poll costs a thread waking up, a
|
|
41
|
+
// few tens of microseconds on a phone, far more than the tick it makes.
|
|
42
|
+
//
|
|
43
|
+
// Precedents: Chrome polls Dawn from its GPU thread while there is polling
|
|
44
|
+
// work (WebGPUDecoderImpl::PerformPollingWork), Dawn's Node binding from the
|
|
45
|
+
// event loop while promises are pending (AsyncRunner). A thread of its own
|
|
46
|
+
// here, because the timers of a JS runtime fire on frames. It stands in for
|
|
47
|
+
// the thread Dawn means to have for this (the spontaneous_queue_events
|
|
48
|
+
// toggle, crbug.com/412761228), and goes the day Dawn completes queue events
|
|
49
|
+
// by itself on every backend.
|
|
50
|
+
class DawnEventPump {
|
|
51
|
+
public:
|
|
52
|
+
using Clock = std::chrono::steady_clock;
|
|
53
|
+
using Duration = Clock::duration;
|
|
54
|
+
|
|
55
|
+
// Asks Dawn about one device without waiting, and returns whether Dawn has
|
|
56
|
+
// work left on it. Called on the pump's thread, with no lock of the pump
|
|
57
|
+
// held: it may call back into the pump. It is destroyed on that thread
|
|
58
|
+
// too, never inside a callback of Dawn.
|
|
59
|
+
using Poll = std::function<bool()>;
|
|
60
|
+
|
|
61
|
+
// What is polled: a device, and the thread it has to be polled on when
|
|
62
|
+
// that is not the pump's. Addresses, used as an identity only.
|
|
63
|
+
struct Source {
|
|
64
|
+
const void *device = nullptr;
|
|
65
|
+
const void *thread = nullptr;
|
|
66
|
+
bool operator==(const Source &other) const {
|
|
67
|
+
return device == other.device && thread == other.thread;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// When to poll.
|
|
72
|
+
struct Policy {
|
|
73
|
+
// While an operation is awaited: the interval between polls, by the time
|
|
74
|
+
// since the latest operation started. Short work is learned of at once,
|
|
75
|
+
// long work without spinning for it. The last step has no end.
|
|
76
|
+
struct Step {
|
|
77
|
+
Duration until;
|
|
78
|
+
Duration interval;
|
|
79
|
+
};
|
|
80
|
+
std::array<Step, 5> awaited = {{
|
|
81
|
+
{std::chrono::microseconds(500), std::chrono::microseconds(100)},
|
|
82
|
+
{std::chrono::milliseconds(4), std::chrono::microseconds(250)},
|
|
83
|
+
{std::chrono::milliseconds(32), std::chrono::milliseconds(1)},
|
|
84
|
+
{std::chrono::milliseconds(128), std::chrono::milliseconds(2)},
|
|
85
|
+
{Duration::max(), std::chrono::milliseconds(4)},
|
|
86
|
+
}};
|
|
87
|
+
// After work nobody awaits: the delay before each poll, the first one
|
|
88
|
+
// counted from the latest work. Longer than a frame, so that a render
|
|
89
|
+
// loop is never polled. The polls end with the list, or as soon as Dawn
|
|
90
|
+
// has nothing left.
|
|
91
|
+
std::array<Duration, 8> idle = {{
|
|
92
|
+
std::chrono::milliseconds(100),
|
|
93
|
+
std::chrono::milliseconds(200),
|
|
94
|
+
std::chrono::milliseconds(400),
|
|
95
|
+
std::chrono::milliseconds(800),
|
|
96
|
+
std::chrono::milliseconds(1600),
|
|
97
|
+
std::chrono::milliseconds(1600),
|
|
98
|
+
std::chrono::milliseconds(1600),
|
|
99
|
+
std::chrono::milliseconds(1600),
|
|
100
|
+
}};
|
|
101
|
+
// How long a source with nothing left to poll for is kept, with its poll:
|
|
102
|
+
// operations often come one after the other.
|
|
103
|
+
Duration linger = std::chrono::seconds(1);
|
|
104
|
+
|
|
105
|
+
Duration awaitedInterval(Duration sinceStart) const;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// The pump of this library, never destroyed: an operation may be in flight
|
|
109
|
+
// while the process exits.
|
|
110
|
+
static DawnEventPump &shared();
|
|
111
|
+
|
|
112
|
+
DawnEventPump();
|
|
113
|
+
explicit DawnEventPump(const Policy &policy);
|
|
114
|
+
// Stops the thread. Only a pump of its own (a test's) is ever destroyed.
|
|
115
|
+
~DawnEventPump();
|
|
116
|
+
|
|
117
|
+
DawnEventPump(const DawnEventPump &) = delete;
|
|
118
|
+
DawnEventPump &operator=(const DawnEventPump &) = delete;
|
|
119
|
+
|
|
120
|
+
// An operation someone awaits is in flight on `source`: it is polled often
|
|
121
|
+
// until the returned token goes. `poll` is copied if the source is new,
|
|
122
|
+
// and not looked at otherwise. Any thread, a callback of Dawn included,
|
|
123
|
+
// and so is letting go of the token.
|
|
124
|
+
[[nodiscard]] std::shared_ptr<void> operationStarted(const Source &source,
|
|
125
|
+
const Poll &poll);
|
|
126
|
+
|
|
127
|
+
// Work nobody awaits was handed to `source` (a submit): it is polled
|
|
128
|
+
// lazily, until Dawn has nothing left on it. Any thread.
|
|
129
|
+
void workSubmitted(const Source &source, const Poll &poll);
|
|
130
|
+
|
|
131
|
+
private:
|
|
132
|
+
struct SourceHash {
|
|
133
|
+
std::size_t operator()(const Source &source) const noexcept;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
struct Entry {
|
|
137
|
+
std::shared_ptr<const Poll> poll;
|
|
138
|
+
// Operations in flight, and when the latest of them started.
|
|
139
|
+
int awaited = 0;
|
|
140
|
+
Clock::time_point awaitedSince;
|
|
141
|
+
// Lazy polls made since the latest work, kNoIdlePolls when none is due.
|
|
142
|
+
int idlePolls = kNoIdlePolls;
|
|
143
|
+
// Counts the work handed over (workSubmitted): what a poll learned says
|
|
144
|
+
// nothing of work that came while it ran.
|
|
145
|
+
unsigned work = 0;
|
|
146
|
+
// When the source is next looked at: polled, or dropped when nothing is
|
|
147
|
+
// left to poll for.
|
|
148
|
+
Clock::time_point due;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
static constexpr int kNoIdlePolls = -1;
|
|
152
|
+
|
|
153
|
+
Entry &entryFor(const Source &source, const Poll &poll,
|
|
154
|
+
Clock::time_point now);
|
|
155
|
+
void operationFinished(const Source &source);
|
|
156
|
+
// Wakes the thread when `due` comes before what it sleeps until. Called
|
|
157
|
+
// with the lock held.
|
|
158
|
+
void wakeFor(Clock::time_point due);
|
|
159
|
+
void startThread();
|
|
160
|
+
void run();
|
|
161
|
+
|
|
162
|
+
const Policy _policy;
|
|
163
|
+
std::mutex _mutex;
|
|
164
|
+
std::condition_variable _condition;
|
|
165
|
+
std::unordered_map<Source, Entry, SourceHash> _entries;
|
|
166
|
+
// What the thread sleeps until.
|
|
167
|
+
Clock::time_point _wakeAt = Clock::time_point::max();
|
|
168
|
+
bool _started = false;
|
|
169
|
+
bool _stopping = false;
|
|
170
|
+
std::thread _thread;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
} // namespace rnwgpu
|
|
@@ -17,8 +17,8 @@ wgpu::Instance createInstance() {
|
|
|
17
17
|
// used: on Android it only succeeds when librnskia.so is loaded before
|
|
18
18
|
// react-native-webgpu installs (see the RTLD_NOLOAD lookup below), so the
|
|
19
19
|
// native-module init order decides whether the instance is shared.
|
|
20
|
-
// RNWebGPU.importDevice does not depend on it:
|
|
21
|
-
//
|
|
20
|
+
// RNWebGPU.importDevice does not depend on it: Dawn is asked about an
|
|
21
|
+
// imported device's async work on the instance that owns it (see
|
|
22
22
|
// async::RuntimeContext).
|
|
23
23
|
// Both packages must link the same single Dawn copy (they vendor the same
|
|
24
24
|
// artifact) for the handles they exchange to be meaningful.
|
|
@@ -42,12 +42,10 @@ wgpu::Instance createInstance() {
|
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
wgpu::InstanceLimits limits{.timedWaitAnyMaxCount = 64};
|
|
50
|
-
instanceDesc.requiredLimits = &limits;
|
|
45
|
+
// No TimedWaitAny: nothing here waits in Dawn (see DawnEventPump), and
|
|
46
|
+
// without the feature Dawn turns down a wait with a timeout rather than
|
|
47
|
+
// letting one hold up the submits of every thread.
|
|
48
|
+
wgpu::InstanceDescriptor instanceDesc{};
|
|
51
49
|
|
|
52
50
|
// Expose Dawn's experimental adapter features. Several features needed by
|
|
53
51
|
// our Android external-texture path (YCbCrVulkanSamplers,
|
package/cpp/rnwgpu/api/GPU.cpp
CHANGED
|
@@ -40,7 +40,7 @@ async::AsyncTaskHandle GPU::requestAdapter(
|
|
|
40
40
|
context](const async::AsyncTaskHandle::ResolveFunction &resolve,
|
|
41
41
|
const async::AsyncTaskHandle::RejectFunction &reject) {
|
|
42
42
|
return _instance.RequestAdapter(
|
|
43
|
-
&aOptions, wgpu::CallbackMode::
|
|
43
|
+
&aOptions, wgpu::CallbackMode::AllowSpontaneous,
|
|
44
44
|
[context, resolve, reject](wgpu::RequestAdapterStatus status,
|
|
45
45
|
wgpu::Adapter adapter,
|
|
46
46
|
wgpu::StringView message) {
|
|
@@ -326,7 +326,7 @@ async::AsyncTaskHandle GPUAdapter::requestDevice(
|
|
|
326
326
|
WGPU_DAWN_CACHE_DEVICE_DESCRIPTOR_INIT;
|
|
327
327
|
DawnBlobCache::chain(deviceDesc, blobCache);
|
|
328
328
|
return _instance.RequestDevice(
|
|
329
|
-
&deviceDesc, wgpu::CallbackMode::
|
|
329
|
+
&deviceDesc, wgpu::CallbackMode::AllowSpontaneous,
|
|
330
330
|
[context, resolve, reject, label, creationRuntime,
|
|
331
331
|
deviceLostBinding](wgpu::RequestDeviceStatus status,
|
|
332
332
|
wgpu::Device device, wgpu::StringView message) {
|