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