Compare commits
1 Commits
GetMappedR
...
ImmediateG
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
44ef0af4a4 |
@@ -86,43 +86,23 @@ void WebGPUBufferBase::updateGPUBuffer(BufferDescriptor const& bufferDescriptor,
|
||||
const size_t mainBulk = bufferDescriptor.size - remainder;
|
||||
const size_t stagingBufferSize =
|
||||
remainder == 0 ? bufferDescriptor.size : mainBulk + FILAMENT_WEBGPU_BUFFER_SIZE_MODULUS;
|
||||
//
|
||||
// // create a staging buffer
|
||||
// wgpu::BufferDescriptor descriptor{
|
||||
// .label = "Filament WebGPU Staging Buffer",
|
||||
// .usage = wgpu::BufferUsage::MapWrite | wgpu::BufferUsage::CopySrc,
|
||||
// .size = stagingBufferSize,
|
||||
// .mappedAtCreation = true };
|
||||
// wgpu::Buffer stagingBuffer = device.CreateBuffer(&descriptor);
|
||||
MappedStage mappedStage = webGPUStagePool->acquireBuffer(stagingBufferSize);
|
||||
|
||||
std::string mappedRangeIsNull = mappedStage.mappedRange
|
||||
Stage stage = webGPUStagePool->acquireBuffer(stagingBufferSize);
|
||||
|
||||
std::string mappedRangeIsNull = stage.mappedRange
|
||||
? "no"
|
||||
: "yes";
|
||||
std::cout << "Run Yu: got mapped range on the staging buffer with size "
|
||||
<< mappedStage.buffer.GetSize() << " and it is null? " << mappedRangeIsNull << std::endl;
|
||||
memcpy(mappedStage.mappedRange, bufferDescriptor.buffer, bufferDescriptor.size);
|
||||
<< stage.buffer.GetSize() << " and it is null? " << mappedRangeIsNull << std::endl;
|
||||
memcpy(stage.mappedRange, bufferDescriptor.buffer, bufferDescriptor.size);
|
||||
|
||||
// Make sure the padded memory is set to 0 to have deterministic behaviors
|
||||
// if (remainder != 0) {
|
||||
// uint8_t* paddingStart = static_cast<uint8_t*>(mappedRange) + bufferDescriptor.size;
|
||||
// memset(paddingStart, 0, FILAMENT_WEBGPU_BUFFER_SIZE_MODULUS - remainder);
|
||||
// }
|
||||
// size_t stagingBufferSize = stagingBuffer.GetSize();
|
||||
// if (stagingBufferSize != bufferDescriptor.size) {
|
||||
// assert(stagingBufferSize > bufferDescriptor.size);
|
||||
// assert(stagingBufferSize % FILAMENT_WEBGPU_BUFFER_SIZE_MODULUS == 0);
|
||||
// uint8_t* paddingStart = static_cast<uint8_t*>(mappedRange) + bufferDescriptor.size;
|
||||
// memset(paddingStart, 0, FILAMENT_WEBGPU_BUFFER_SIZE_MODULUS - (stagingBuffer.GetSize() - bufferDescriptor.size));
|
||||
// }
|
||||
|
||||
mappedStage.buffer.Unmap();
|
||||
stage.buffer.Unmap();
|
||||
|
||||
std::cout << "Run Yu: about to issue copy command with actual staging buffer of size "
|
||||
<< mappedStage.buffer.GetSize() << ", and computed size of " << stagingBufferSize
|
||||
<< stage.buffer.GetSize() << ", and computed size of " << stagingBufferSize
|
||||
<< ". The mBuffer size is " << mBuffer.GetSize() << std::endl;
|
||||
// Copy the staging buffer contents to the destination buffer.
|
||||
webGPUQueueManager->getCommandEncoder().CopyBufferToBuffer(mappedStage.buffer, 0, mBuffer,
|
||||
webGPUQueueManager->getCommandEncoder().CopyBufferToBuffer(stage.buffer, 0, mBuffer,
|
||||
byteOffset,
|
||||
remainder == 0 ? bufferDescriptor.size
|
||||
: mainBulk + FILAMENT_WEBGPU_BUFFER_SIZE_MODULUS);
|
||||
@@ -133,18 +113,22 @@ void WebGPUBufferBase::updateGPUBuffer(BufferDescriptor const& bufferDescriptor,
|
||||
WebGPUStagePool* webGPUStagePool;
|
||||
};
|
||||
auto userData = std::make_unique<UserData>(
|
||||
UserData{ .stagingBuffer = mappedStage.buffer, .webGPUStagePool = webGPUStagePool });
|
||||
mappedStage.buffer.MapAsync(
|
||||
wgpu::MapMode::Write, 0, stagingBufferSize, wgpu::CallbackMode::AllowSpontaneous,
|
||||
[](wgpu::MapAsyncStatus status, const char* message, UserData* userData) {
|
||||
UserData{ .stagingBuffer = stage.buffer, .webGPUStagePool = webGPUStagePool });
|
||||
stage.buffer.MapAsync(wgpu::MapMode::Write, 0, stagingBufferSize,
|
||||
wgpu::CallbackMode::AllowSpontaneous,
|
||||
[data = std::move(userData)](wgpu::MapAsyncStatus status, const char* message) {
|
||||
if (UTILS_LIKELY(status == wgpu::MapAsyncStatus::Success)) {
|
||||
std::unique_ptr<UserData> data(static_cast<UserData*>(userData));
|
||||
userData->webGPUStagePool->addBufferToPool(userData->stagingBuffer);
|
||||
std::cout << "Run Yu: successfully mapped a buffer with size "
|
||||
<< data->stagingBuffer.GetSize() << std::endl;
|
||||
void* mappedRange = data->stagingBuffer.GetMappedRange();
|
||||
if (!mappedRange) {
|
||||
std::cout << "Run Yu: MAPPED RANGE IS NULL RIGHT AWAY!!\n";
|
||||
}
|
||||
data->webGPUStagePool->addBufferToPool(data->stagingBuffer, mappedRange);
|
||||
} else {
|
||||
std::cout << "Run Yu: MAPPING UNSUCCESSFUL!!\n";
|
||||
}
|
||||
},
|
||||
userData.release());
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace filament::backend
|
||||
|
||||
@@ -26,52 +26,50 @@ WebGPUStagePool::WebGPUStagePool(wgpu::Device const& device) : mDevice(device) {
|
||||
|
||||
WebGPUStagePool::~WebGPUStagePool() = default;
|
||||
|
||||
MappedStage WebGPUStagePool::acquireBuffer(size_t requiredSize) {
|
||||
Stage WebGPUStagePool::acquireBuffer(size_t requiredSize) {
|
||||
std::cout << "Run Yu: required size in acquireBuffer: " << requiredSize << std::endl;
|
||||
std::cout << "Run Yu: the pool size is " << mBuffers.size() << std::endl;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mMutex);
|
||||
auto iter = mBuffers.lower_bound(requiredSize);
|
||||
if (iter != mBuffers.end()) {
|
||||
std::cout << "Run Yu: found buffer in the pool with size " << iter->second.GetSize()
|
||||
const Stage& fromPool = iter->second;
|
||||
std::cout << "Run Yu: found buffer in the pool with size " << fromPool.buffer.GetSize()
|
||||
<< std::endl;
|
||||
if (iter->second.GetMapState() != wgpu::BufferMapState::Mapped) {
|
||||
std::cout << "Run Yu: before GetMappedRange the buffer state is not mapped!\n";
|
||||
}
|
||||
MappedStage mappedStage = { .buffer = iter->second,
|
||||
.mappedRange = iter->second.GetMappedRange() };
|
||||
if (!mappedStage.mappedRange) {
|
||||
std::cout << "Run Yu: mapped range is null in acquireBuffer!\n";
|
||||
}
|
||||
if (mappedStage.buffer.GetMapState() != wgpu::BufferMapState::Mapped) {
|
||||
std::cout << "Run Yu: after GetMappedRange the buffer state is not mapped!\n";
|
||||
if (fromPool.buffer.GetMapState() != wgpu::BufferMapState::Mapped) {
|
||||
std::cout << "Run Yu: buffer from pool is not mapped!!" << std::endl;
|
||||
}
|
||||
|
||||
Stage result{ .buffer = fromPool.buffer, .mappedRange = fromPool.mappedRange };
|
||||
mBuffers.erase(iter);
|
||||
return mappedStage;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
wgpu::Buffer newBuffer = createNewBuffer(requiredSize);
|
||||
return { .buffer = newBuffer, .mappedRange = newBuffer.GetMappedRange() };
|
||||
}
|
||||
|
||||
void WebGPUStagePool::addBufferToPool(wgpu::Buffer buffer) {
|
||||
void WebGPUStagePool::addBufferToPool(wgpu::Buffer buffer, void* mappedRange) {
|
||||
std::lock_guard<std::mutex> lock(mMutex);
|
||||
std::cout << "Run Yu: adding buffer to the pool with size " << buffer.GetSize() << std::endl;
|
||||
mBuffers.insert({buffer.GetSize(), buffer});
|
||||
Stage stage {.buffer = buffer, .mappedRange = mappedRange};
|
||||
mBuffers.emplace(buffer.GetSize(), stage);
|
||||
std::cout << "Run Yu: added buffer to the pool with size " << buffer.GetSize() << std::endl;
|
||||
|
||||
bool allMapped = true;
|
||||
for (const auto& pair : mBuffers) {
|
||||
auto state = pair.second.GetMapState();
|
||||
auto state = pair.second.buffer.GetMapState();
|
||||
if (state != wgpu::BufferMapState::Mapped) {
|
||||
allMapped = false;
|
||||
std::cout << "Run Yu: the buffer with size " << pair.second.GetSize()
|
||||
std::cout << "Run Yu: the buffer with size " << pair.second.buffer.GetSize()
|
||||
<< " is not mapped but somehow was added to the pool, its state is "
|
||||
<< static_cast<int>(state) << std::endl;
|
||||
}
|
||||
}
|
||||
if (!allMapped) {
|
||||
std::cout << "Run Yu: found buffers that are not mapped\n";
|
||||
} else {
|
||||
std::cout << "Run Yu: all buffers are mapped\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
namespace filament::backend {
|
||||
|
||||
struct MappedStage {
|
||||
struct Stage {
|
||||
wgpu::Buffer buffer;
|
||||
void* mappedRange;
|
||||
};
|
||||
@@ -34,11 +34,11 @@ public:
|
||||
WebGPUStagePool(wgpu::Device const& device);
|
||||
~WebGPUStagePool();
|
||||
|
||||
MappedStage acquireBuffer(size_t requiredSize);
|
||||
void addBufferToPool(wgpu::Buffer buffer);
|
||||
Stage acquireBuffer(size_t requiredSize);
|
||||
void addBufferToPool(wgpu::Buffer buffer, void* mappedRange);
|
||||
private:
|
||||
wgpu::Buffer createNewBuffer(size_t bufferSize);
|
||||
std::multimap<uint32_t, wgpu::Buffer> mBuffers;
|
||||
std::multimap<uint32_t, Stage> mBuffers;
|
||||
mutable std::mutex mMutex;
|
||||
|
||||
wgpu::Device mDevice;
|
||||
|
||||
Reference in New Issue
Block a user