Compare commits
2 Commits
exv/varian
...
ImmediateG
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
44ef0af4a4 | ||
|
|
c9b5bfc4a4 |
@@ -316,6 +316,8 @@ if (FILAMENT_SUPPORTS_WEBGPU)
|
||||
src/webgpu/WebGPURenderPrimitive.h
|
||||
src/webgpu/WebGPURenderTarget.cpp
|
||||
src/webgpu/WebGPURenderTarget.h
|
||||
src/webgpu/WebGPUStagePool.cpp
|
||||
src/webgpu/WebGPUStagePool.h
|
||||
src/webgpu/WebGPUStrings.h
|
||||
src/webgpu/WebGPUSwapChain.cpp
|
||||
src/webgpu/WebGPUSwapChain.h
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
#include "WebGPUConstants.h"
|
||||
#include "WebGPUQueueManager.h"
|
||||
#include "WebGPUStagePool.h"
|
||||
|
||||
#include "DriverBase.h"
|
||||
#include <backend/BufferDescriptor.h>
|
||||
@@ -29,6 +30,7 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
|
||||
namespace filament::backend {
|
||||
|
||||
@@ -65,7 +67,7 @@ WebGPUBufferBase::WebGPUBufferBase(wgpu::Device const& device, const wgpu::Buffe
|
||||
// of 4 by padding with zeros.
|
||||
void WebGPUBufferBase::updateGPUBuffer(BufferDescriptor const& bufferDescriptor,
|
||||
const uint32_t byteOffset, wgpu::Device const& device,
|
||||
WebGPUQueueManager* const webGPUQueueManager) {
|
||||
WebGPUQueueManager* const webGPUQueueManager, WebGPUStagePool* const webGPUStagePool) {
|
||||
FILAMENT_CHECK_PRECONDITION(bufferDescriptor.buffer)
|
||||
<< "updateGPUBuffer called with a null buffer";
|
||||
FILAMENT_CHECK_PRECONDITION(bufferDescriptor.size + byteOffset <= mBuffer.GetSize())
|
||||
@@ -79,34 +81,54 @@ void WebGPUBufferBase::updateGPUBuffer(BufferDescriptor const& bufferDescriptor,
|
||||
// This may have some performance implications. That should be investigated later.
|
||||
assert_invariant(mBuffer.GetUsage() & wgpu::BufferUsage::CopyDst);
|
||||
|
||||
// Calculate some alignment related sizes
|
||||
// // Calculate some alignment related sizes
|
||||
const size_t remainder = bufferDescriptor.size % FILAMENT_WEBGPU_BUFFER_SIZE_MODULUS;
|
||||
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);
|
||||
Stage stage = webGPUStagePool->acquireBuffer(stagingBufferSize);
|
||||
|
||||
void* mappedRange = stagingBuffer.GetMappedRange();
|
||||
memcpy(mappedRange, bufferDescriptor.buffer, bufferDescriptor.size);
|
||||
std::string mappedRangeIsNull = stage.mappedRange
|
||||
? "no"
|
||||
: "yes";
|
||||
std::cout << "Run Yu: got mapped range on the staging buffer with 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);
|
||||
}
|
||||
|
||||
stagingBuffer.Unmap();
|
||||
stage.buffer.Unmap();
|
||||
|
||||
std::cout << "Run Yu: about to issue copy command with actual staging buffer of size "
|
||||
<< 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(stagingBuffer, 0, mBuffer,
|
||||
byteOffset, stagingBufferSize);
|
||||
webGPUQueueManager->getCommandEncoder().CopyBufferToBuffer(stage.buffer, 0, mBuffer,
|
||||
byteOffset,
|
||||
remainder == 0 ? bufferDescriptor.size
|
||||
: mainBulk + FILAMENT_WEBGPU_BUFFER_SIZE_MODULUS);
|
||||
webGPUQueueManager->flush();
|
||||
|
||||
struct UserData final {
|
||||
wgpu::Buffer stagingBuffer;
|
||||
WebGPUStagePool* webGPUStagePool;
|
||||
};
|
||||
auto userData = std::make_unique<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::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";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace filament::backend
|
||||
|
||||
@@ -25,6 +25,7 @@ namespace filament::backend {
|
||||
|
||||
class BufferDescriptor;
|
||||
class WebGPUQueueManager;
|
||||
class WebGPUStagePool;
|
||||
|
||||
/**
|
||||
* A base class for WebGPU buffer objects, providing common functionality for creating and
|
||||
@@ -40,7 +41,7 @@ public:
|
||||
* ensures the calls happen in the expected sequence.
|
||||
*/
|
||||
void updateGPUBuffer(BufferDescriptor const&, uint32_t byteOffset, wgpu::Device const& device,
|
||||
WebGPUQueueManager* const webGPUQueueManager);
|
||||
WebGPUQueueManager* const webGPUQueueManager, WebGPUStagePool* const webGPUStagePool);
|
||||
|
||||
[[nodiscard]] wgpu::Buffer const& getBuffer() const { return mBuffer; }
|
||||
|
||||
|
||||
@@ -107,6 +107,7 @@ WebGPUDriver::WebGPUDriver(WebGPUPlatform& platform,
|
||||
mAdapter{ mPlatform.requestAdapter(nullptr) },
|
||||
mDevice{ mPlatform.requestDevice(mAdapter) },
|
||||
mQueueManager{ mDevice },
|
||||
mStagePool{ mDevice },
|
||||
mPipelineLayoutCache{ mDevice },
|
||||
mPipelineCache{ mDevice },
|
||||
mRenderPassMipmapGenerator{ mDevice, &mQueueManager },
|
||||
@@ -856,7 +857,7 @@ void WebGPUDriver::updateIndexBuffer(Handle<HwIndexBuffer> indexBufferHandle,
|
||||
// draw calls are made.
|
||||
flush();
|
||||
handleCast<WebGPUIndexBuffer>(indexBufferHandle)
|
||||
->updateGPUBuffer(bufferDescriptor, byteOffset, mDevice, &mQueueManager);
|
||||
->updateGPUBuffer(bufferDescriptor, byteOffset, mDevice, &mQueueManager, &mStagePool);
|
||||
scheduleDestroy(std::move(bufferDescriptor));
|
||||
}
|
||||
|
||||
@@ -867,14 +868,14 @@ void WebGPUDriver::updateBufferObject(Handle<HwBufferObject> bufferObjectHandle,
|
||||
// draw calls are made.
|
||||
flush();
|
||||
handleCast<WebGPUBufferObject>(bufferObjectHandle)
|
||||
->updateGPUBuffer(bufferDescriptor, byteOffset, mDevice, &mQueueManager);
|
||||
->updateGPUBuffer(bufferDescriptor, byteOffset, mDevice, &mQueueManager, &mStagePool);
|
||||
scheduleDestroy(std::move(bufferDescriptor));
|
||||
}
|
||||
|
||||
void WebGPUDriver::updateBufferObjectUnsynchronized(Handle<HwBufferObject> bufferObjectHandle,
|
||||
BufferDescriptor&& bufferDescriptor, const uint32_t byteOffset) {
|
||||
handleCast<WebGPUBufferObject>(bufferObjectHandle)
|
||||
->updateGPUBuffer(bufferDescriptor, byteOffset, mDevice, &mQueueManager);
|
||||
->updateGPUBuffer(bufferDescriptor, byteOffset, mDevice, &mQueueManager, &mStagePool);
|
||||
scheduleDestroy(std::move(bufferDescriptor));
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "webgpu/WebGPUPipelineLayoutCache.h"
|
||||
#include "webgpu/WebGPURenderPassMipmapGenerator.h"
|
||||
#include "webgpu/WebGPUQueueManager.h"
|
||||
#include "webgpu/WebGPUStagePool.h"
|
||||
#include "webgpu/utils/AsyncTaskCounter.h"
|
||||
#include <backend/platforms/WebGPUPlatform.h>
|
||||
|
||||
@@ -81,6 +82,7 @@ private:
|
||||
wgpu::Device mDevice = nullptr;
|
||||
wgpu::Limits mDeviceLimits = {};
|
||||
WebGPUQueueManager mQueueManager;
|
||||
WebGPUStagePool mStagePool;
|
||||
void* mNativeWindow = nullptr;
|
||||
WebGPUSwapChain* mSwapChain = nullptr;
|
||||
uint64_t mNextFakeHandle = 1;
|
||||
|
||||
86
filament/backend/src/webgpu/WebGPUStagePool.cpp
Normal file
86
filament/backend/src/webgpu/WebGPUStagePool.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (C) 2025 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "WebGPUStagePool.h"
|
||||
|
||||
#include "WebGPUConstants.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace filament::backend {
|
||||
|
||||
WebGPUStagePool::WebGPUStagePool(wgpu::Device const& device) : mDevice(device) {}
|
||||
|
||||
WebGPUStagePool::~WebGPUStagePool() = default;
|
||||
|
||||
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()) {
|
||||
const Stage& fromPool = iter->second;
|
||||
std::cout << "Run Yu: found buffer in the pool with size " << fromPool.buffer.GetSize()
|
||||
<< std::endl;
|
||||
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 result;
|
||||
}
|
||||
}
|
||||
wgpu::Buffer newBuffer = createNewBuffer(requiredSize);
|
||||
return { .buffer = newBuffer, .mappedRange = newBuffer.GetMappedRange() };
|
||||
}
|
||||
|
||||
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;
|
||||
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.buffer.GetMapState();
|
||||
if (state != wgpu::BufferMapState::Mapped) {
|
||||
allMapped = false;
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
wgpu::Buffer WebGPUStagePool::createNewBuffer(size_t bufferSize) {
|
||||
std::cout << "Run Yu: creating new buffer with size " << bufferSize << std::endl;
|
||||
wgpu::BufferDescriptor descriptor{
|
||||
.label = "Filament WebGPU Staging Buffer",
|
||||
.usage = wgpu::BufferUsage::MapWrite | wgpu::BufferUsage::CopySrc,
|
||||
.size = bufferSize,
|
||||
.mappedAtCreation = true };
|
||||
return mDevice.CreateBuffer(&descriptor);
|
||||
}
|
||||
|
||||
} // namespace filament::backend
|
||||
49
filament/backend/src/webgpu/WebGPUStagePool.h
Normal file
49
filament/backend/src/webgpu/WebGPUStagePool.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (C) 2025 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef TNT_FILAMENT_BACKEND_WEBGPUSTAGEPOOL_H
|
||||
#define TNT_FILAMENT_BACKEND_WEBGPUSTAGEPOOL_H
|
||||
|
||||
#include <webgpu/webgpu_cpp.h>
|
||||
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
|
||||
namespace filament::backend {
|
||||
|
||||
struct Stage {
|
||||
wgpu::Buffer buffer;
|
||||
void* mappedRange;
|
||||
};
|
||||
|
||||
class WebGPUStagePool {
|
||||
public:
|
||||
WebGPUStagePool(wgpu::Device const& device);
|
||||
~WebGPUStagePool();
|
||||
|
||||
Stage acquireBuffer(size_t requiredSize);
|
||||
void addBufferToPool(wgpu::Buffer buffer, void* mappedRange);
|
||||
private:
|
||||
wgpu::Buffer createNewBuffer(size_t bufferSize);
|
||||
std::multimap<uint32_t, Stage> mBuffers;
|
||||
mutable std::mutex mMutex;
|
||||
|
||||
wgpu::Device mDevice;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // TNT_FILAMENT_BACKEND_WEBGPUSTAGEPOOL_H
|
||||
Reference in New Issue
Block a user