-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathGPUQueue.cpp
More file actions
172 lines (150 loc) · 5.94 KB
/
GPUQueue.cpp
File metadata and controls
172 lines (150 loc) · 5.94 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "GPUQueue.h"
#include <limits>
#include <memory>
#include <vector>
#include "Convertors.h"
#include "../SurfaceRegistry.h"
namespace rnwgpu {
struct BufferSource {
void *data;
size_t size; // in bytes
size_t bytesPerElement; // 1 for ArrayBuffers
};
void GPUQueue::submit(
std::vector<std::shared_ptr<GPUCommandBuffer>> commandBuffers) {
std::vector<wgpu::CommandBuffer> bufs(commandBuffers.size());
for (size_t i = 0; i < commandBuffers.size(); i++) {
bufs[i] = commandBuffers[i]->get();
}
Convertor conv;
uint32_t bufs_size;
if (!conv(bufs_size, bufs.size())) {
return;
}
_instance.Submit(bufs_size, bufs.data());
SurfaceRegistry::getInstance().presentPendingSurfaces();
}
void GPUQueue::writeBuffer(std::shared_ptr<GPUBuffer> buffer,
uint64_t bufferOffset,
std::shared_ptr<ArrayBuffer> data,
std::optional<uint64_t> dataOffsetElements,
std::optional<size_t> sizeElements) {
wgpu::Buffer buf = buffer->get();
BufferSource src{.data = data->_data,
.size = data->_size,
.bytesPerElement = data->_bytesPerElement};
// Note that in the JS semantics of WebGPU, writeBuffer works in number of
// elements of the typed arrays.
if (dataOffsetElements >
static_cast<uint64_t>(src.size / src.bytesPerElement)) {
throw std::runtime_error("dataOffset is larger than data's size.");
return;
}
uint64_t dataOffset = dataOffsetElements.value_or(0) * src.bytesPerElement;
src.data = reinterpret_cast<uint8_t *>(src.data) + dataOffset;
src.size -= dataOffset;
// Size defaults to dataSize - dataOffset. Instead of computing in elements,
// we directly use it in bytes, and convert the provided value, if any, in
// bytes.
uint64_t size64 = static_cast<uint64_t>(src.size);
if (sizeElements.has_value()) {
if (sizeElements.value() >
std::numeric_limits<uint64_t>::max() / src.bytesPerElement) {
throw std::runtime_error("size overflows.");
return;
}
size64 = sizeElements.value() * src.bytesPerElement;
}
if (size64 > static_cast<uint64_t>(src.size)) {
throw std::runtime_error("size + dataOffset is larger than data's size.");
return;
}
if (size64 % 4 != 0) {
throw std::runtime_error("size is not a multiple of 4 bytes.");
return;
}
assert(size64 <= std::numeric_limits<size_t>::max());
_instance.WriteBuffer(buf, bufferOffset, src.data,
static_cast<size_t>(size64));
}
async::AsyncTaskHandle GPUQueue::onSubmittedWorkDone() {
auto queue = _instance;
return _async->postTask([
queue
](const async::AsyncTaskHandle::ResolveFunction& resolve,
const async::AsyncTaskHandle::RejectFunction& reject) {
queue.OnSubmittedWorkDone(
wgpu::CallbackMode::AllowProcessEvents,
[resolve, reject](wgpu::QueueWorkDoneStatus status, wgpu::StringView message) {
if (status == wgpu::QueueWorkDoneStatus::Success) {
resolve(nullptr);
} else {
std::string error = message.length
? std::string(message.data, message.length)
: "Queue work did not complete successfully";
reject(std::move(error));
}
});
});
}
void GPUQueue::copyExternalImageToTexture(
std::shared_ptr<GPUImageCopyExternalImage> source,
std::shared_ptr<GPUImageCopyTextureTagged> destination,
std::shared_ptr<GPUExtent3D> size) {
wgpu::TexelCopyTextureInfo dst{};
wgpu::TexelCopyBufferLayout layout{};
wgpu::Extent3D sz{};
Convertor conv;
uint32_t bytesPerPixel =
source->source->getSize() /
(source->source->getWidth() * source->source->getHeight());
auto dataLayout = std::make_shared<GPUImageDataLayout>(GPUImageDataLayout{
std::optional<double>{0.0},
std::optional<double>{
static_cast<double>(bytesPerPixel * source->source->getWidth())},
std::optional<double>{static_cast<double>(source->source->getHeight())}});
if (!conv(dst.aspect, destination->aspect) ||
!conv(dst.mipLevel, destination->mipLevel) ||
!conv(dst.origin, destination->origin) ||
!conv(dst.texture, destination->texture) ||
!conv(layout, dataLayout) || //
!conv(sz, size)) {
throw std::runtime_error("Invalid input for GPUQueue::writeTexture()");
}
if (source->flipY) {
// Calculate the row size and total size
uint32_t rowSize = bytesPerPixel * source->source->getWidth();
uint32_t totalSize = source->source->getSize();
// Create a new buffer for the flipped data
std::vector<uint8_t> flippedData(totalSize);
// Flip the data vertically
for (uint32_t row = 0; row < source->source->getHeight(); ++row) {
std::memcpy(flippedData.data() +
(source->source->getHeight() - 1 - row) * rowSize,
static_cast<const uint8_t *>(source->source->getData()) +
row * rowSize,
rowSize);
}
// Use the flipped data for writing to texture
_instance.WriteTexture(&dst, flippedData.data(), totalSize, &layout, &sz);
} else {
_instance.WriteTexture(&dst, source->source->getData(),
source->source->getSize(), &layout, &sz);
}
}
void GPUQueue::writeTexture(std::shared_ptr<GPUImageCopyTexture> destination,
std::shared_ptr<ArrayBuffer> data,
std::shared_ptr<GPUImageDataLayout> dataLayout,
std::shared_ptr<GPUExtent3D> size) {
wgpu::TexelCopyTextureInfo dst{};
wgpu::TexelCopyBufferLayout layout{};
wgpu::Extent3D sz{};
Convertor conv;
if (!conv(dst, destination) || //
!conv(layout, dataLayout) || //
!conv(sz, size)) {
throw std::runtime_error("Invalid input for GPUQueue::writeTexture()");
}
_instance.WriteTexture(&dst, data->_data, data->_size, &layout, &sz);
}
} // namespace rnwgpu