Compare commits

...

1 Commits

Author SHA1 Message Date
Run Yu
8c1167ddb8 Use GetMappedRange early but still get null pointer sometimes 2025-11-21 13:34:57 -05:00
3 changed files with 31 additions and 21 deletions

View File

@@ -94,15 +94,14 @@ void WebGPUBufferBase::updateGPUBuffer(BufferDescriptor const& bufferDescriptor,
// .size = stagingBufferSize,
// .mappedAtCreation = true };
// wgpu::Buffer stagingBuffer = device.CreateBuffer(&descriptor);
wgpu::Buffer stagingBuffer = webGPUStagePool->acquireBuffer(stagingBufferSize);
MappedStage mappedStage = webGPUStagePool->acquireBuffer(stagingBufferSize);
void* mappedRange = stagingBuffer.GetMappedRange();
std::string mappedRangeIsNull = mappedRange
std::string mappedRangeIsNull = mappedStage.mappedRange
? "no"
: "yes";
std::cout << "Run Yu: got mapped range on the staging buffer with size "
<< stagingBuffer.GetSize() << " and it is null? " << mappedRangeIsNull << std::endl;
memcpy(mappedRange, bufferDescriptor.buffer, bufferDescriptor.size);
<< mappedStage.buffer.GetSize() << " and it is null? " << mappedRangeIsNull << std::endl;
memcpy(mappedStage.mappedRange, bufferDescriptor.buffer, bufferDescriptor.size);
// Make sure the padded memory is set to 0 to have deterministic behaviors
// if (remainder != 0) {
@@ -117,13 +116,13 @@ void WebGPUBufferBase::updateGPUBuffer(BufferDescriptor const& bufferDescriptor,
// memset(paddingStart, 0, FILAMENT_WEBGPU_BUFFER_SIZE_MODULUS - (stagingBuffer.GetSize() - bufferDescriptor.size));
// }
stagingBuffer.Unmap();
mappedStage.buffer.Unmap();
std::cout << "Run Yu: about to issue copy command with actual staging buffer of size "
<< stagingBuffer.GetSize() << ", and computed size of " << stagingBufferSize
<< mappedStage.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(stagingBuffer, 0, mBuffer,
webGPUQueueManager->getCommandEncoder().CopyBufferToBuffer(mappedStage.buffer, 0, mBuffer,
byteOffset,
remainder == 0 ? bufferDescriptor.size
: mainBulk + FILAMENT_WEBGPU_BUFFER_SIZE_MODULUS);
@@ -134,8 +133,8 @@ void WebGPUBufferBase::updateGPUBuffer(BufferDescriptor const& bufferDescriptor,
WebGPUStagePool* webGPUStagePool;
};
auto userData = std::make_unique<UserData>(
UserData{ .stagingBuffer = stagingBuffer, .webGPUStagePool = webGPUStagePool });
stagingBuffer.MapAsync(
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) {
if (UTILS_LIKELY(status == wgpu::MapAsyncStatus::Success)) {

View File

@@ -26,24 +26,32 @@ WebGPUStagePool::WebGPUStagePool(wgpu::Device const& device) : mDevice(device) {
WebGPUStagePool::~WebGPUStagePool() = default;
wgpu::Buffer WebGPUStagePool::acquireBuffer(size_t requiredSize) {
MappedStage 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()) {
wgpu::Buffer bufferFromPool = iter->second;
std::cout << "Run Yu: found buffer in the pool with size " << bufferFromPool.GetSize()
std::cout << "Run Yu: found buffer in the pool with size " << iter->second.GetSize()
<< std::endl;
mBuffers.erase(iter);
if (bufferFromPool.GetMapState() != wgpu::BufferMapState::Mapped) {
std::cout << "Run Yu: buffer from pool is not mapped!!" << std::endl;
if (iter->second.GetMapState() != wgpu::BufferMapState::Mapped) {
std::cout << "Run Yu: before GetMappedRange the buffer state is not mapped!\n";
}
return bufferFromPool;
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";
}
mBuffers.erase(iter);
return mappedStage;
}
}
return createNewBuffer(requiredSize);
wgpu::Buffer newBuffer = createNewBuffer(requiredSize);
return { .buffer = newBuffer, .mappedRange = newBuffer.GetMappedRange() };
}
void WebGPUStagePool::addBufferToPool(wgpu::Buffer buffer) {
@@ -64,8 +72,6 @@ void WebGPUStagePool::addBufferToPool(wgpu::Buffer buffer) {
}
if (!allMapped) {
std::cout << "Run Yu: found buffers that are not mapped\n";
} else {
std::cout << "Run Yu: all buffers are mapped\n";
}
}

View File

@@ -24,12 +24,17 @@
namespace filament::backend {
struct MappedStage {
wgpu::Buffer buffer;
void* mappedRange;
};
class WebGPUStagePool {
public:
WebGPUStagePool(wgpu::Device const& device);
~WebGPUStagePool();
wgpu::Buffer acquireBuffer(size_t requiredSize);
MappedStage acquireBuffer(size_t requiredSize);
void addBufferToPool(wgpu::Buffer buffer);
private:
wgpu::Buffer createNewBuffer(size_t bufferSize);