Skip to content

Commit

Permalink
WebGPU: Reworked queue managment
Browse files Browse the repository at this point in the history
  • Loading branch information
MikhailGorobets committed Jul 5, 2024
1 parent 3ee4385 commit 9b045e0
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ class AttachmentCleanerWebGPU

void ResetDynamicUniformBuffer();

void ClearColor(WGPURenderPassEncoder wgpuCmdEncoder,
void ClearColor(WGPUQueue wgpuQueue,
WGPURenderPassEncoder wgpuCmdEncoder,
const RenderPassInfo& RPInfo,
COLOR_MASK ColorMask,
Uint32 RTIndex,
const float Color[]);

void ClearDepthStencil(WGPURenderPassEncoder wgpuCmdEncoder,
void ClearDepthStencil(WGPUQueue wgpuQueue,
WGPURenderPassEncoder wgpuCmdEncoder,
const RenderPassInfo& RPInfo,
CLEAR_DEPTH_STENCIL_FLAGS Flags,
float Depth,
Expand All @@ -90,7 +92,8 @@ class AttachmentCleanerWebGPU

WebGPURenderPipelineWrapper CreatePSO(const ClearPSOHashKey& Key) const;

void ClearAttachment(WGPURenderPassEncoder wgpuCmdEncoder,
void ClearAttachment(WGPUQueue wgpuQueue,
WGPURenderPassEncoder wgpuCmdEncoder,
const ClearPSOHashKey& Key,
std::array<float, 8>& ClearData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ class DeviceContextWebGPUImpl final : public DeviceContextBase<EngineWebGPUImplT
using DynamicMemoryPageList = std::vector<DynamicMemoryManagerWebGPU::Page>;
using MappedTextureCache = std::unordered_map<MappedTextureKey, MappedTexture, MappedTextureKey::Hasher>;

WGPUQueue m_wgpuQueue = nullptr;
WebGPUQueueWrapper m_wgpuQueue;
WebGPUCommandEncoderWrapper m_wgpuCommandEncoder;
WebGPURenderPassEncoderWrapper m_wgpuRenderPassEncoder;
WebGPUComputePassEncoderWrapper m_wgpuComputePassEncoder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class GenerateMipsHelperWebGPU
GenerateMipsHelperWebGPU& operator = ( GenerateMipsHelperWebGPU&&) = delete;
// clang-format on

void GenerateMips(WGPUComputePassEncoder wgpuCmdEncoder, TextureViewWebGPUImpl* pTexView);
void GenerateMips(WGPUQueue wgpuQueue, WGPUComputePassEncoder wgpuCmdEncoder, TextureViewWebGPUImpl* pTexView);

private:
using UAVFormats = std::array<TEXTURE_FORMAT, 4>;
Expand Down
27 changes: 14 additions & 13 deletions Graphics/GraphicsEngineWebGPU/include/WebGPUObjectWrappers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,22 @@ class WebGPUObjectWrapper
DECLARE_WEBGPU_WRAPPER(WebGPUInstance, WGPUInstance, wgpuInstance)
DECLARE_WEBGPU_WRAPPER(WebGPUAdapter, WGPUAdapter, wgpuAdapter)
DECLARE_WEBGPU_WRAPPER(WebGPUDevice, WGPUDevice, wgpuDevice)
DECLARE_WEBGPU_WRAPPER(WebGPUQueue, WGPUQueue, wgpuQueue)
DECLARE_WEBGPU_WRAPPER(WebGPUSurface, WGPUSurface, wgpuSurface)
DECLARE_WEBGPU_WRAPPER(WebGPUTexture, WGPUTexture, wgpuTexture)
DECLARE_WEBGPU_WRAPPER(WebGPUTextureView, WGPUTextureView, wgpuTextureView)
DECLARE_WEBGPU_WRAPPER(WebGPUBuffer, WGPUBuffer, wgpuBuffer);
DECLARE_WEBGPU_WRAPPER(WebGPUSampler, WGPUSampler, wgpuSampler);
DECLARE_WEBGPU_WRAPPER(WebGPUShaderModule, WGPUShaderModule, wgpuShaderModule);
DECLARE_WEBGPU_WRAPPER(WebGPUBindGroupLayout, WGPUBindGroupLayout, wgpuBindGroupLayout);
DECLARE_WEBGPU_WRAPPER(WebGPUPipelineLayout, WGPUPipelineLayout, wgpuPipelineLayout);
DECLARE_WEBGPU_WRAPPER(WebGPURenderPipeline, WGPURenderPipeline, wgpuRenderPipeline);
DECLARE_WEBGPU_WRAPPER(WebGPUComputePipeline, WGPUComputePipeline, wgpuComputePipeline);
DECLARE_WEBGPU_WRAPPER(WebGPUCommandBuffer, WGPUCommandBuffer, wgpuCommandBuffer);
DECLARE_WEBGPU_WRAPPER(WebGPUCommandEncoder, WGPUCommandEncoder, wgpuCommandEncoder);
DECLARE_WEBGPU_WRAPPER(WebGPURenderPassEncoder, WGPURenderPassEncoder, wgpuRenderPassEncoder);
DECLARE_WEBGPU_WRAPPER(WebGPUComputePassEncoder, WGPUComputePassEncoder, wgpuComputePassEncoder);
DECLARE_WEBGPU_WRAPPER(WebGPUBindGroup, WGPUBindGroup, wgpuBindGroup);
DECLARE_WEBGPU_WRAPPER(WebGPUQuerySet, WGPUQuerySet, wgpuQuerySet);
DECLARE_WEBGPU_WRAPPER(WebGPUBuffer, WGPUBuffer, wgpuBuffer)
DECLARE_WEBGPU_WRAPPER(WebGPUSampler, WGPUSampler, wgpuSampler)
DECLARE_WEBGPU_WRAPPER(WebGPUShaderModule, WGPUShaderModule, wgpuShaderModule)
DECLARE_WEBGPU_WRAPPER(WebGPUBindGroupLayout, WGPUBindGroupLayout, wgpuBindGroupLayout)
DECLARE_WEBGPU_WRAPPER(WebGPUPipelineLayout, WGPUPipelineLayout, wgpuPipelineLayout)
DECLARE_WEBGPU_WRAPPER(WebGPURenderPipeline, WGPURenderPipeline, wgpuRenderPipeline)
DECLARE_WEBGPU_WRAPPER(WebGPUComputePipeline, WGPUComputePipeline, wgpuComputePipeline)
DECLARE_WEBGPU_WRAPPER(WebGPUCommandBuffer, WGPUCommandBuffer, wgpuCommandBuffer)
DECLARE_WEBGPU_WRAPPER(WebGPUCommandEncoder, WGPUCommandEncoder, wgpuCommandEncoder)
DECLARE_WEBGPU_WRAPPER(WebGPURenderPassEncoder, WGPURenderPassEncoder, wgpuRenderPassEncoder)
DECLARE_WEBGPU_WRAPPER(WebGPUComputePassEncoder, WGPUComputePassEncoder, wgpuComputePassEncoder)
DECLARE_WEBGPU_WRAPPER(WebGPUBindGroup, WGPUBindGroup, wgpuBindGroup)
DECLARE_WEBGPU_WRAPPER(WebGPUQuerySet, WGPUQuerySet, wgpuQuerySet)

} // namespace Diligent
16 changes: 8 additions & 8 deletions Graphics/GraphicsEngineWebGPU/src/AttachmentCleanerWebGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ void AttachmentCleanerWebGPU::ResetDynamicUniformBuffer()
m_CurrBufferOffset = 0;
}

void AttachmentCleanerWebGPU::ClearColor(WGPURenderPassEncoder wgpuCmdEncoder,
void AttachmentCleanerWebGPU::ClearColor(WGPUQueue wgpuQueue,
WGPURenderPassEncoder wgpuCmdEncoder,
const RenderPassInfo& RPInfo,
COLOR_MASK ColorMask,
Uint32 RTIndex,
Expand All @@ -190,10 +191,11 @@ void AttachmentCleanerWebGPU::ClearColor(WGPURenderPassEncoder wgpuCmdEncoder,
Key.DepthState = m_wgpuDisableDepth;

std::array<float, 8> ClearData = {Color[0], Color[1], Color[2], Color[3]};
ClearAttachment(wgpuCmdEncoder, Key, ClearData);
ClearAttachment(wgpuQueue, wgpuCmdEncoder, Key, ClearData);
}

void AttachmentCleanerWebGPU::ClearDepthStencil(WGPURenderPassEncoder wgpuCmdEncoder,
void AttachmentCleanerWebGPU::ClearDepthStencil(WGPUQueue wgpuQueue,
WGPURenderPassEncoder wgpuCmdEncoder,
const RenderPassInfo& RPInfo,
CLEAR_DEPTH_STENCIL_FLAGS Flags,
float Depth,
Expand All @@ -215,7 +217,7 @@ void AttachmentCleanerWebGPU::ClearDepthStencil(WGPURenderPassEncoder wgpuCm
}

std::array<float, 8> ClearData = {0, 0, 0, 0, Depth};
ClearAttachment(wgpuCmdEncoder, Key, ClearData);
ClearAttachment(wgpuQueue, wgpuCmdEncoder, Key, ClearData);
}

WebGPURenderPipelineWrapper AttachmentCleanerWebGPU::CreatePSO(const ClearPSOHashKey& Key) const
Expand Down Expand Up @@ -277,7 +279,7 @@ WebGPURenderPipelineWrapper AttachmentCleanerWebGPU::CreatePSO(const ClearPSOHas
return wgpuPipeline;
}

void AttachmentCleanerWebGPU::ClearAttachment(WGPURenderPassEncoder wgpuCmdEncoder, const ClearPSOHashKey& Key, std::array<float, 8>& ClearData)
void AttachmentCleanerWebGPU::ClearAttachment(WGPUQueue wgpuQueue, WGPURenderPassEncoder wgpuCmdEncoder, const ClearPSOHashKey& Key, std::array<float, 8>& ClearData)
{
auto Iter = m_PSOCache.find(Key);
if (Iter == m_PSOCache.end())
Expand All @@ -293,9 +295,7 @@ void AttachmentCleanerWebGPU::ClearAttachment(WGPURenderPassEncoder wgpuCmdEncod
Uint32 DynamicOffsets[] = {m_CurrBufferOffset};
m_CurrBufferOffset += m_BufferElementSize;
VERIFY(m_CurrBufferOffset < m_BufferMaxElementCount * m_BufferElementSize, "Buffer offset more then buffer size");

WGPUQueue wgpuQueue = wgpuDeviceGetQueue(m_wgpuDevice);
wgpuQueueWriteBuffer(wgpuQueue, m_wgpuBuffer.Get(), DynamicOffsets[0], ClearData.data(), sizeof(float) * ClearData.size());
wgpuQueueWriteBuffer(wgpuQueue, m_wgpuBuffer, DynamicOffsets[0], ClearData.data(), sizeof(float) * ClearData.size());

wgpuRenderPassEncoderSetPipeline(wgpuCmdEncoder, wgpuPipelineState);
wgpuRenderPassEncoderSetBindGroup(wgpuCmdEncoder, 0, m_PipelineResourceLayout.wgpuBindGroup.Get(), _countof(DynamicOffsets), DynamicOffsets);
Expand Down
6 changes: 5 additions & 1 deletion Graphics/GraphicsEngineWebGPU/src/BufferWebGPUImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ BufferWebGPUImpl::BufferWebGPUImpl(IReferenceCounters* pRefCounters,
LOG_ERROR_AND_THROW("Failed to create WebGPU buffer ", " '", m_Desc.Name ? m_Desc.Name : "", '\'');

if (InitializeBuffer)
wgpuQueueWriteBuffer(wgpuDeviceGetQueue(pDevice->GetWebGPUDevice()), m_wgpuBuffer.Get(), 0, pInitData->pData, StaticCast<size_t>(pInitData->DataSize));
{
VERIFY_EXPR(pDevice->GetNumImmediateContexts() == 1);
auto pContext = pDevice->GetImmediateContext(0);
wgpuQueueWriteBuffer(pContext->GetWebGPUQueue(), m_wgpuBuffer, 0, pInitData->pData, StaticCast<size_t>(pInitData->DataSize));
}
}

SetState(RESOURCE_STATE_UNDEFINED);
Expand Down
15 changes: 10 additions & 5 deletions Graphics/GraphicsEngineWebGPU/src/DeviceContextWebGPUImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ DeviceContextWebGPUImpl::DeviceContextWebGPUImpl(IReferenceCounters* p
}
// clang-format on
{
m_wgpuQueue = wgpuDeviceGetQueue(pDevice->GetWebGPUDevice());
m_wgpuQueue.Reset(wgpuDeviceGetQueue(pDevice->GetWebGPUDevice()));
(void)m_ActiveQueriesCounter;

pDevice->CreateFence({}, &m_pFence);
Expand Down Expand Up @@ -1073,6 +1073,10 @@ void DeviceContextWebGPUImpl::Flush()
}
pDeviceCxt->m_SignalFences.clear();

for (auto& MemPage : pDeviceCxt->m_DynamicMemPages)
MemPage.Recycle();
pDeviceCxt->m_DynamicMemPages.clear();

for (auto& MemPage : pDeviceCxt->m_UploadMemPages)
MemPage.Recycle();
pDeviceCxt->m_UploadMemPages.clear();
Expand All @@ -1086,8 +1090,9 @@ void DeviceContextWebGPUImpl::Flush()
WebGPUCommandBufferWrapper wgpuCmdBuffer{wgpuCommandEncoderFinish(GetCommandEncoder(), &wgpuCmdBufferDesc)};
DEV_CHECK_ERR(wgpuCmdBuffer != nullptr, "Failed to finish command encoder");

wgpuQueueOnSubmittedWorkDone(m_wgpuQueue, WorkDoneCallback, this);
wgpuQueueSubmit(m_wgpuQueue, 1, &wgpuCmdBuffer.Get());
wgpuQueueOnSubmittedWorkDone(m_wgpuQueue, WorkDoneCallback, this);

m_wgpuCommandEncoder.Reset(nullptr);
}
}
Expand Down Expand Up @@ -1195,7 +1200,7 @@ void DeviceContextWebGPUImpl::GenerateMips(ITextureView* pTexView)

auto& MipGenerator = m_pDevice->GetMipsGenerator();
auto CmdEncoder = GetComputePassCommandEncoder();
MipGenerator.GenerateMips(CmdEncoder, ClassPtrCast<TextureViewWebGPUImpl>(pTexView));
MipGenerator.GenerateMips(m_wgpuQueue, CmdEncoder, ClassPtrCast<TextureViewWebGPUImpl>(pTexView));
}

void DeviceContextWebGPUImpl::FinishFrame()
Expand Down Expand Up @@ -1539,10 +1544,10 @@ void DeviceContextWebGPUImpl::ClearAttachment(Int32 RTIndex,
}

if (RTIndex >= 0)
AttachmentCleaner.ClearColor(m_wgpuRenderPassEncoder, RPInfo, ColorMask, RTIndex, ClearData);
AttachmentCleaner.ClearColor(m_wgpuQueue, m_wgpuRenderPassEncoder, RPInfo, ColorMask, RTIndex, ClearData);
else
{
AttachmentCleaner.ClearDepthStencil(m_wgpuRenderPassEncoder, RPInfo, DSFlags, ClearData[0], Stencil);
AttachmentCleaner.ClearDepthStencil(m_wgpuQueue, m_wgpuRenderPassEncoder, RPInfo, DSFlags, ClearData[0], Stencil);
if ((DSFlags & CLEAR_STENCIL_FLAG) != 0)
m_EncoderState.Invalidate(WebGPUEncoderState::CMD_ENCODER_STATE_STENCIL_REF);
}
Expand Down
1 change: 1 addition & 0 deletions Graphics/GraphicsEngineWebGPU/src/EngineFactoryWebGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ void EngineFactoryWebGPUImpl::AttachToWebGPUDevice(WGPUInstance
0 // Queue id
})};
pDeviceContextWebGPU->QueryInterface(IID_DeviceContext, reinterpret_cast<IObject**>(ppImmediateContext));
pRenderDeviceWebGPU->SetImmediateContext(0, pDeviceContextWebGPU);
}
catch (const std::runtime_error&)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ GenerateMipsHelperWebGPU::GenerateMipsHelperWebGPU(WGPUDevice wgpuDevice) :
InitializePlaceholderTextures();
}

void GenerateMipsHelperWebGPU::GenerateMips(WGPUComputePassEncoder wgpuCmdEncoder, TextureViewWebGPUImpl* pTexView)
void GenerateMipsHelperWebGPU::GenerateMips(WGPUQueue wgpuQueue, WGPUComputePassEncoder wgpuCmdEncoder, TextureViewWebGPUImpl* pTexView)
{
auto* pTexWGPU = pTexView->GetTexture<TextureWebGPUImpl>();
const auto& TexDesc = pTexWGPU->GetDesc();
Expand All @@ -288,7 +288,6 @@ void GenerateMipsHelperWebGPU::GenerateMips(WGPUComputePassEncoder wgpuCmdEncode
m_CurrBufferOffset += m_BufferElementSize;
VERIFY(m_CurrBufferOffset < m_BufferMaxElementCount * m_BufferElementSize, "Buffer offset more then buffer size");

WGPUQueue wgpuQueue = wgpuDeviceGetQueue(m_wgpuDevice);
wgpuQueueWriteBuffer(wgpuQueue, m_wgpuBuffer.Get(), DynamicOffset, &BufferData, sizeof(BufferData));
return DynamicOffset;
};
Expand Down
7 changes: 4 additions & 3 deletions Graphics/GraphicsEngineWebGPU/src/SwapChainWebGPUImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class WebGPUSwapChainPresentCommand
m_IsInitializedResources = true;
}

void Execute(ITextureViewWebGPU* pTexture, ISwapChainWebGPU* pSwapChain)
void Execute(ITextureViewWebGPU* pTexture, ISwapChainWebGPU* pSwapChain, IDeviceContextWebGPU* pDeviceContext)
{
WGPUSurfaceTexture wgpuSurfaceTexture{};
wgpuSurfaceGetCurrentTexture(pSwapChain->GetWebGPUSurface(), &wgpuSurfaceTexture);
Expand Down Expand Up @@ -252,7 +252,8 @@ class WebGPUSwapChainPresentCommand

WGPUCommandBufferDescriptor wgpuCmdBufferDesc{};
WebGPUCommandBufferWrapper wgpuCmdBuffer{wgpuCommandEncoderFinish(wgpuCmdEncoder, &wgpuCmdBufferDesc)};
wgpuQueueSubmit(wgpuDeviceGetQueue(m_pRenderDevice->GetWebGPUDevice()), 1, &wgpuCmdBuffer.Get());

wgpuQueueSubmit(pDeviceContext->GetWebGPUQueue(), 1, &wgpuCmdBuffer.Get());
wgpuSurfacePresent(pSwapChain->GetWebGPUSurface());
wgpuTextureRelease(wgpuSurfaceTexture.texture);
}
Expand Down Expand Up @@ -315,7 +316,7 @@ void SwapChainWebGPUImpl::Present(Uint32 SyncInterval)
auto* pImmediateCtxWebGPU = pDeviceContext.RawPtr<DeviceContextWebGPUImpl>();

pImmediateCtxWebGPU->Flush();
m_pCmdPresent->Execute(m_pBackBufferSRV, this);
m_pCmdPresent->Execute(m_pBackBufferSRV, this, pImmediateCtxWebGPU);

if (m_SwapChainDesc.IsPrimary)
{
Expand Down
4 changes: 3 additions & 1 deletion Graphics/GraphicsEngineWebGPU/src/TextureWebGPUImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,11 @@ TextureWebGPUImpl::TextureWebGPUImpl(IReferenceCounters* pRefCounters,

wgpuBufferUnmap(wgpuUploadBuffer.Get());

VERIFY_EXPR(pDevice->GetNumImmediateContexts() == 1);
WGPUCommandBufferDescriptor wgpuCmdBufferDesc{};
WebGPUCommandBufferWrapper wgpuCmdBuffer{wgpuCommandEncoderFinish(wgpuCmdEncoder, &wgpuCmdBufferDesc)};
wgpuQueueSubmit(wgpuDeviceGetQueue(pDevice->GetWebGPUDevice()), 1, &wgpuCmdBuffer.Get());
auto pContext = pDevice->GetImmediateContext(0);
wgpuQueueSubmit(pContext->GetWebGPUQueue(), 1, &wgpuCmdBuffer.Get());
}
}
else if (m_Desc.Usage == USAGE_STAGING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ void ClearRenderTargetReferenceWebGPU(ISwapChain* pSwapChain, const float ClearC

WGPURenderPassEncoder wgpuRenderPass = wgpuCommandEncoderBeginRenderPass(wgpuCmdEncoder, &wgpuRenderPassDesc);
wgpuRenderPassEncoderEnd(wgpuRenderPass);

pEnvWebGPU->SubmitCommandEncoder(wgpuCmdEncoder);

wgpuRenderPassEncoderRelease(wgpuRenderPass);
wgpuCommandEncoderRelease(wgpuCmdEncoder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ void ComputeShaderReferenceWebGPU(ISwapChain* pSwapChain)
wgpuComputePassEncoderSetBindGroup(wgpuComputePassEncoder, 0, wgpuBindGroup, 0, nullptr);
wgpuComputePassEncoderDispatchWorkgroups(wgpuComputePassEncoder, (SCDesc.Width + 15) / 16, (SCDesc.Height + 15) / 16, 1);
wgpuComputePassEncoderEnd(wgpuComputePassEncoder);

pEnvWebGPU->SubmitCommandEncoder(wgpuCmdEncoder);

wgpuComputePassEncoderRelease(wgpuComputePassEncoder);
wgpuCommandEncoderRelease(wgpuCmdEncoder);
wgpuBindGroupRelease(wgpuBindGroup);
wgpuComputePipelineRelease(wgpuComputePipeline);
wgpuPipelineLayoutRelease(wgpuPipelineLayout);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ void RenderDrawCommandReferenceWebGPU(ISwapChain* pSwapChain, const float* pClea
TriRender.Draw(wgpuRenderPassEncoder);
wgpuRenderPassEncoderEnd(wgpuRenderPassEncoder);
pEnvWebGPU->SubmitCommandEncoder(wgpuCmdEncoder);

wgpuRenderPassEncoderRelease(wgpuRenderPassEncoder);
wgpuCommandEncoderRelease(wgpuCmdEncoder);
}

void RenderPassMSResolveReferenceWebGPU(ISwapChain* pSwapChain, const float* pClearColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ void TestingEnvironmentWebGPU::SubmitCommandEncoder(WGPUCommandEncoder wgpuCmdEn
#endif
}
}
wgpuCommandEncoderRelease(wgpuCmdEncoder);
}

GPUTestingEnvironment* CreateTestingEnvironmentWebGPU(const GPUTestingEnvironment::CreateInfo& CI,
Expand Down

0 comments on commit 9b045e0

Please sign in to comment.