Skip to content

Commit

Permalink
[hgiWebGPU]: Modify staging buffer implementation (PixarAnimationStud…
Browse files Browse the repository at this point in the history
…ios#1124)

* [hgiWebGPU]: Modify staging buffer implementation
- Postpone staging buffer creation until required
- Use ABSL_ENABLE_INSTALL options for dawn cmake
build to avoid manual copy
  • Loading branch information
kohakukun authored and GitHub Enterprise committed Jul 18, 2024
1 parent 3566f69 commit a31e0d3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
12 changes: 1 addition & 11 deletions build_scripts/build_usd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,7 @@ def InstallDawn(context, force, buildArgs):
'-DDAWN_BUILD_SAMPLES=OFF',
'-DDAWN_ENABLE_INSTALL=ON',
'-DDAWN_USE_GLFW=OFF',
'-DABSL_ENABLE_INSTALL=ON'
]
if Windows():
cmakeOptions.append('-DBUILD_SHARED_LIBS=OFF')
Expand Down Expand Up @@ -1797,17 +1798,6 @@ def InstallDawn(context, force, buildArgs):
CopyFiles(context, "src/dawn/common/{buildConfig}*.*".format(buildConfig=buildConfigFolder), "lib")
CopyFiles(context, "third_party/spirv-tools/source/{buildConfig}*SPIRV-Tools.*".format(buildConfig=buildConfigFolder), "lib")
CopyFiles(context, "third_party/spirv-tools/source/opt/{buildConfig}*SPIRV-Tools-opt.*".format(buildConfig=buildConfigFolder), "lib")
CopyFiles(context, "third_party/abseil/absl/base/{buildConfig}*.*".format(buildConfig=buildConfigFolder), "lib")
CopyFiles(context, "third_party/abseil/absl/container/{buildConfig}*.*".format(buildConfig=buildConfigFolder), "lib")
CopyFiles(context, "third_party/abseil/absl/crc/{buildConfig}*.*".format(buildConfig=buildConfigFolder), "lib")
CopyFiles(context, "third_party/abseil/absl/debugging/{buildConfig}*.*".format(buildConfig=buildConfigFolder), "lib")
CopyFiles(context, "third_party/abseil/absl/hash/{buildConfig}*.*".format(buildConfig=buildConfigFolder), "lib")
CopyFiles(context, "third_party/abseil/absl/numeric/{buildConfig}*.*".format(buildConfig=buildConfigFolder), "lib")
CopyFiles(context, "third_party/abseil/absl/profiling/{buildConfig}*.*".format(buildConfig=buildConfigFolder), "lib")
CopyFiles(context, "third_party/abseil/absl/strings/{buildConfig}*.*".format(buildConfig=buildConfigFolder), "lib")
CopyFiles(context, "third_party/abseil/absl/synchronization/{buildConfig}*.*".format(buildConfig=buildConfigFolder), "lib")
CopyFiles(context, "third_party/abseil/absl/time/{buildConfig}*.*".format(buildConfig=buildConfigFolder), "lib")
CopyFiles(context, "third_party/abseil/absl/types/{buildConfig}*.*".format(buildConfig=buildConfigFolder), "lib")
CopyFiles(context, "src/tint/{buildConfig}*.*".format(buildConfig=buildConfigFolder), "lib")
# Extra include files
CopyFiles(context, "gen/include/dawn/*.*", "include/dawn")
Expand Down
30 changes: 16 additions & 14 deletions pxr/imaging/hgiWebGPU/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,17 @@ HgiWebGPUBuffer::HgiWebGPUBuffer(HgiWebGPU *hgi, HgiBufferDesc const & desc)
bufferDesc.label = desc.debugName.c_str();
bufferDesc.usage = HgiWebGPUConversions::GetBufferUsage(desc.usage);

if( desc.usage & HgiBufferUsageIndex32 || desc.usage & HgiBufferUsageVertex || desc.usage & HgiBufferUsageUniform || desc.usage & HgiBufferUsageStorage)
bufferDesc.usage |= wgpu::BufferUsage::CopyDst;
else
bufferDesc.usage |= wgpu::BufferUsage::MapRead;

// TODO: We basically need to add all usages since we don't know at this point in time in which stage this will be used.
// For example, the pxr/imaging/hdSt/vboMemoryManager.cpp#351 sets HgiBufferUsageUniform | HgiBufferUsageVertex
// for the points buffer, which is then later used in the pxr/imaging/hdSt/smoothNormals.cpp#75 with a resource
// binding of the type storage, resulting in incompatible types
bufferDesc.usage |= wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Storage | wgpu::BufferUsage::Index;
// There is no information on how the buffer will be used after creation so, we add the possibility to use it
// as a src or destination for copy operations.
bufferDesc.usage |= wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::CopyDst;

bufferDesc.size = desc.byteSize;
wgpu::Device device = hgi->GetPrimaryDevice();
_bufferHandle = device.CreateBuffer(&bufferDesc);

_cpuStagingBuffer.resize(desc.byteSize);

if (desc.initialData) {
wgpu::Queue queue = hgi->GetQueue();
queue.WriteBuffer(_bufferHandle, 0, desc.initialData, desc.byteSize);
memcpy(_cpuStagingBuffer.data(), desc.initialData, desc.byteSize);
}

_descriptor.initialData = nullptr;
Expand All @@ -73,6 +63,11 @@ HgiWebGPUBuffer::HgiWebGPUBuffer(HgiWebGPU *hgi, HgiBufferDesc const & desc)
HgiWebGPUBuffer::~HgiWebGPUBuffer()
{
_bufferHandle = nullptr;

if (_cpuStaging) {
free(_cpuStaging);
_cpuStaging = nullptr;
}
}

size_t
Expand All @@ -90,7 +85,14 @@ HgiWebGPUBuffer::GetRawResource() const
void*
HgiWebGPUBuffer::GetCPUStagingAddress()
{
return static_cast<void *>(_cpuStagingBuffer.data());
if (!_cpuStaging) {
_cpuStaging = malloc(_descriptor.byteSize);
}

// This lets the client code memcpy into the cpu staging buffer directly.
// The staging data must be explicitly copied to the GPU buffer
// via CopyBufferCpuToGpu cmd by the client.
return _cpuStaging;
}

PXR_NAMESPACE_CLOSE_SCOPE
2 changes: 1 addition & 1 deletion pxr/imaging/hgiWebGPU/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class HgiWebGPUBuffer final : public HgiBuffer {
HgiWebGPUBuffer(const HgiWebGPUBuffer&) = delete;

wgpu::Buffer _bufferHandle;
std::vector<uint8_t> _cpuStagingBuffer;
void* _cpuStaging;
};


Expand Down

0 comments on commit a31e0d3

Please sign in to comment.