Skip to content

Commit

Permalink
WebGPU: do not use WGPU_WHOLE_MAP_SIZE due to emscripten-core/emscrip…
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Sep 27, 2024
1 parent b77dc25 commit 3704a46
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Graphics/GraphicsEngineWebGPU/src/BufferWebGPUImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ BufferWebGPUImpl::BufferWebGPUImpl(IReferenceCounters* pRefCounters,

if (wgpuBufferDesc.mappedAtCreation)
{
void* pData = wgpuBufferGetMappedRange(m_wgpuBuffer, 0, WGPU_WHOLE_MAP_SIZE);
memcpy(pData, pInitData->pData, StaticCast<size_t>(pInitData->DataSize));
const size_t DataSize = StaticCast<size_t>(pInitData->DataSize);
// Do NOT use WGPU_WHOLE_MAP_SIZE due to https://github.com/emscripten-core/emscripten/issues/20538
void* pData = wgpuBufferGetMappedRange(m_wgpuBuffer, 0, DataSize);
memcpy(pData, pInitData->pData, DataSize);
wgpuBufferUnmap(m_wgpuBuffer);
}
}
Expand Down
3 changes: 2 additions & 1 deletion Graphics/GraphicsEngineWebGPU/src/TextureWebGPUImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ TextureWebGPUImpl::TextureWebGPUImpl(IReferenceCounters* pRefCounters,
if (!wgpuUploadBuffer)
LOG_ERROR_AND_THROW("Failed to create WebGPU texture upload buffer");

uint8_t* pUploadData = static_cast<uint8_t*>(wgpuBufferGetMappedRange(wgpuUploadBuffer.Get(), 0, WGPU_WHOLE_MAP_SIZE));
// Do NOT use WGPU_WHOLE_MAP_SIZE due to https://github.com/emscripten-core/emscripten/issues/20538
uint8_t* pUploadData = static_cast<uint8_t*>(wgpuBufferGetMappedRange(wgpuUploadBuffer.Get(), 0, static_cast<size_t>(wgpuBufferDesc.size)));

WGPUCommandEncoderDescriptor wgpuEncoderDesc{};
WebGPUCommandEncoderWrapper wgpuCmdEncoder{wgpuDeviceCreateCommandEncoder(pDevice->GetWebGPUDevice(), &wgpuEncoderDesc)};
Expand Down
16 changes: 11 additions & 5 deletions Graphics/GraphicsEngineWebGPU/src/WebGPUResourceBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,11 @@ void WebGPUResourceBase::FlushPendingWrites(StagingBufferInfo& Buffer)
VERIFY_EXPR(m_StagingBuffers.size() == 1);
VERIFY(!Buffer.pSyncPoint, "Staging write buffers do not need sync points");

if (void* pData = wgpuBufferGetMappedRange(Buffer.wgpuBuffer, 0, WGPU_WHOLE_MAP_SIZE))
// Do NOT use WGPU_WHOLE_MAP_SIZE due to https://github.com/emscripten-core/emscripten/issues/20538
const size_t MappedDataSize = m_MappedData.size();
if (void* pData = wgpuBufferGetMappedRange(Buffer.wgpuBuffer, 0, MappedDataSize))
{
memcpy(pData, m_MappedData.data(), m_MappedData.size());
memcpy(pData, m_MappedData.data(), MappedDataSize);
}
else
{
Expand All @@ -186,9 +188,11 @@ void WebGPUResourceBase::ProcessAsyncReadback(StagingBufferInfo& Buffer)

if (MapStatus == WGPUBufferMapAsyncStatus_Success)
{
if (const void* pData = wgpuBufferGetConstMappedRange(BufferInfo.wgpuBuffer, 0, WGPU_WHOLE_MAP_SIZE))
// Do NOT use WGPU_WHOLE_MAP_SIZE due to https://github.com/emscripten-core/emscripten/issues/20538
const size_t MappedDataSize = BufferInfo.Resource.m_MappedData.size();
if (const void* pData = wgpuBufferGetConstMappedRange(BufferInfo.wgpuBuffer, 0, MappedDataSize))
{
memcpy(BufferInfo.Resource.m_MappedData.data(), pData, BufferInfo.Resource.m_MappedData.size());
memcpy(BufferInfo.Resource.m_MappedData.data(), pData, MappedDataSize);
}
else
{
Expand All @@ -205,7 +209,9 @@ void WebGPUResourceBase::ProcessAsyncReadback(StagingBufferInfo& Buffer)

// Keep the resource alive until the callback is called
m_Owner.AddRef();
wgpuBufferMapAsync(Buffer.wgpuBuffer, WGPUMapMode_Read, 0, WGPU_WHOLE_MAP_SIZE, MapAsyncCallback, &Buffer);
// Do NOT use WGPU_WHOLE_MAP_SIZE due to https://github.com/emscripten-core/emscripten/issues/20538
const size_t MappedDataSize = Buffer.Resource.m_MappedData.size();
wgpuBufferMapAsync(Buffer.wgpuBuffer, WGPUMapMode_Read, 0, MappedDataSize, MapAsyncCallback, &Buffer);
}

} // namespace Diligent

0 comments on commit 3704a46

Please sign in to comment.