From 2dd2d67dbcbbf0a241fd13d4fccbc049ee39bfd4 Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Wed, 5 May 2021 15:41:13 +0000 Subject: [PATCH] CommandBufferResourceUsage: track render and compute separately. This change is a preparation for making the compute pass track the synchronization scope usages per dispatch instead of for the whole pass. - Split the tracking of render and compute passes usages. - Rename PassTextureUsage to TextureSubresourceUsage since is it not per-pass - Add SyncScopeResourceUsage as a subclass of PassResourceUsage to start modifying some of the code to work with synchronization scopes (even if syncscope == pass at the moment). There are no functional changes. Bug: dawn:632 Change-Id: Ieeb6d70a44dc1c726f26989eebcd87e63e732785 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/49883 Reviewed-by: Corentin Wallez Commit-Queue: Corentin Wallez --- src/dawn_native/Buffer.cpp | 3 +- src/dawn_native/CommandEncoder.cpp | 18 +++++----- src/dawn_native/CommandEncoder.h | 3 +- src/dawn_native/CommandValidation.cpp | 18 +++------- src/dawn_native/CommandValidation.h | 4 +-- src/dawn_native/ComputePassEncoder.cpp | 3 +- src/dawn_native/EncodingContext.cpp | 36 +++++++++++++++----- src/dawn_native/EncodingContext.h | 14 +++++--- src/dawn_native/PassResourceUsage.h | 22 +++++++----- src/dawn_native/PassResourceUsageTracker.cpp | 11 +++--- src/dawn_native/PassResourceUsageTracker.h | 4 +-- src/dawn_native/Queue.cpp | 24 +++++++++---- src/dawn_native/RenderBundleEncoder.cpp | 2 +- src/dawn_native/RenderPassEncoder.cpp | 3 +- src/dawn_native/Texture.cpp | 2 +- src/dawn_native/d3d12/CommandBufferD3D12.cpp | 14 ++++---- src/dawn_native/d3d12/TextureD3D12.cpp | 2 +- src/dawn_native/d3d12/TextureD3D12.h | 2 +- src/dawn_native/metal/CommandBufferMTL.mm | 24 +++++++------ src/dawn_native/opengl/CommandBufferGL.cpp | 23 ++++++------- src/dawn_native/vulkan/CommandBufferVk.cpp | 18 +++++----- src/dawn_native/vulkan/TextureVk.cpp | 2 +- src/dawn_native/vulkan/TextureVk.h | 2 +- 23 files changed, 141 insertions(+), 113 deletions(-) diff --git a/src/dawn_native/Buffer.cpp b/src/dawn_native/Buffer.cpp index e8bb23f50d..3ddb72bd71 100644 --- a/src/dawn_native/Buffer.cpp +++ b/src/dawn_native/Buffer.cpp @@ -133,8 +133,7 @@ namespace dawn_native { mUsage(descriptor->usage), mState(BufferState::Unmapped) { // Add readonly storage usage if the buffer has a storage usage. The validation rules in - // ValidatePassResourceUsage will make sure we don't use both at the same - // time. + // ValidateSyncScopeResourceUsage will make sure we don't use both at the same time. if (mUsage & wgpu::BufferUsage::Storage) { mUsage |= kReadOnlyStorageBuffer; } diff --git a/src/dawn_native/CommandEncoder.cpp b/src/dawn_native/CommandEncoder.cpp index 97f7320e14..21060e4756 100644 --- a/src/dawn_native/CommandEncoder.cpp +++ b/src/dawn_native/CommandEncoder.cpp @@ -470,9 +470,9 @@ namespace dawn_native { } CommandBufferResourceUsage CommandEncoder::AcquireResourceUsages() { - return CommandBufferResourceUsage{mEncodingContext.AcquirePassUsages(), - std::move(mTopLevelBuffers), std::move(mTopLevelTextures), - std::move(mUsedQuerySets)}; + return CommandBufferResourceUsage{ + mEncodingContext.AcquireRenderPassUsages(), mEncodingContext.AcquireComputePassUsages(), + std::move(mTopLevelBuffers), std::move(mTopLevelTextures), std::move(mUsedQuerySets)}; } CommandIterator CommandEncoder::AcquireCommands() { @@ -936,21 +936,21 @@ namespace dawn_native { DAWN_TRY(device->ValidateIsAlive()); if (device->IsValidationEnabled()) { - DAWN_TRY( - ValidateFinish(mEncodingContext.GetIterator(), mEncodingContext.GetPassUsages())); + DAWN_TRY(ValidateFinish()); } return device->CreateCommandBuffer(this, descriptor); } // Implementation of the command buffer validation that can be precomputed before submit - MaybeError CommandEncoder::ValidateFinish(CommandIterator* commands, - const PerPassUsages& perPassUsages) const { + MaybeError CommandEncoder::ValidateFinish() const { TRACE_EVENT0(GetDevice()->GetPlatform(), Validation, "CommandEncoder::ValidateFinish"); DAWN_TRY(GetDevice()->ValidateObject(this)); - for (const PassResourceUsage& passUsage : perPassUsages) { - DAWN_TRY(ValidatePassResourceUsage(passUsage)); + for (const PassResourceUsage& passUsage : mEncodingContext.GetRenderPassUsages()) { + DAWN_TRY(ValidateSyncScopeResourceUsage(passUsage)); } + // TODO(dawn:632): The synchronization scopes of compute passes should be validated here + // once they are tracked per-dispatch. if (mDebugGroupStackSize != 0) { return DAWN_VALIDATION_ERROR("Each Push must be balanced by a corresponding Pop."); diff --git a/src/dawn_native/CommandEncoder.h b/src/dawn_native/CommandEncoder.h index a8bf6a0d28..13678f17ce 100644 --- a/src/dawn_native/CommandEncoder.h +++ b/src/dawn_native/CommandEncoder.h @@ -73,8 +73,7 @@ namespace dawn_native { ResultOrError> FinishInternal( const CommandBufferDescriptor* descriptor); - MaybeError ValidateFinish(CommandIterator* commands, - const PerPassUsages& perPassUsages) const; + MaybeError ValidateFinish() const; EncodingContext mEncodingContext; std::set mTopLevelBuffers; diff --git a/src/dawn_native/CommandValidation.cpp b/src/dawn_native/CommandValidation.cpp index 1e2fc38d3d..05e3a9e014 100644 --- a/src/dawn_native/CommandValidation.cpp +++ b/src/dawn_native/CommandValidation.cpp @@ -28,20 +28,10 @@ namespace dawn_native { - // Performs the per-pass usage validation checks - // This will eventually need to differentiate between render and compute passes. - // It will be valid to use a buffer both as uniform and storage in the same compute pass. - // TODO(yunchao.he@intel.com): add read/write usage tracking for compute - MaybeError ValidatePassResourceUsage(const PassResourceUsage& pass) { - // TODO(cwallez@chromium.org): Remove this special casing once the PassResourceUsage is a - // SyncScopeResourceUsage. - if (pass.passType != PassType::Render) { - return {}; - } - + // Performs validation of the "synchronization scope" rules of WebGPU. + MaybeError ValidateSyncScopeResourceUsage(const SyncScopeResourceUsage& scope) { // Buffers can only be used as single-write or multiple read. - for (size_t i = 0; i < pass.buffers.size(); ++i) { - wgpu::BufferUsage usage = pass.bufferUsages[i]; + for (wgpu::BufferUsage usage : scope.bufferUsages) { bool readOnly = IsSubset(usage, kReadOnlyBufferUsages); bool singleUse = wgpu::HasZeroOrOneBits(usage); @@ -53,7 +43,7 @@ namespace dawn_native { // Check that every single subresource is used as either a single-write usage or a // combination of readonly usages. - for (const PassTextureUsage& textureUsage : pass.textureUsages) { + for (const TextureSubresourceUsage& textureUsage : scope.textureUsages) { MaybeError error = {}; textureUsage.Iterate([&](const SubresourceRange&, const wgpu::TextureUsage& usage) { bool readOnly = IsSubset(usage, kReadOnlyTextureUsages); diff --git a/src/dawn_native/CommandValidation.h b/src/dawn_native/CommandValidation.h index 9c8a42f5ce..7b5fce2bba 100644 --- a/src/dawn_native/CommandValidation.h +++ b/src/dawn_native/CommandValidation.h @@ -24,10 +24,10 @@ namespace dawn_native { class QuerySetBase; - struct PassResourceUsage; + struct SyncScopeResourceUsage; struct TexelBlockInfo; - MaybeError ValidatePassResourceUsage(const PassResourceUsage& usage); + MaybeError ValidateSyncScopeResourceUsage(const SyncScopeResourceUsage& usage); MaybeError ValidateTimestampQuery(QuerySetBase* querySet, uint32_t queryIndex); diff --git a/src/dawn_native/ComputePassEncoder.cpp b/src/dawn_native/ComputePassEncoder.cpp index 04bb8ff56f..666235e7ef 100644 --- a/src/dawn_native/ComputePassEncoder.cpp +++ b/src/dawn_native/ComputePassEncoder.cpp @@ -55,7 +55,8 @@ namespace dawn_native { return {}; })) { - mEncodingContext->ExitPass(this, mUsageTracker.AcquireResourceUsage()); + mEncodingContext->ExitPass(this, mUsageTracker.AcquireResourceUsage(), + PassType::Compute); } } diff --git a/src/dawn_native/EncodingContext.cpp b/src/dawn_native/EncodingContext.cpp index 39b5d4c7bf..acdecd788e 100644 --- a/src/dawn_native/EncodingContext.cpp +++ b/src/dawn_native/EncodingContext.cpp @@ -76,25 +76,43 @@ namespace dawn_native { mCurrentEncoder = passEncoder; } - void EncodingContext::ExitPass(const ObjectBase* passEncoder, PassResourceUsage passUsage) { + void EncodingContext::ExitPass(const ObjectBase* passEncoder, + PassResourceUsage passUsage, + PassType type) { // Assert we're not at the top level. ASSERT(mCurrentEncoder != mTopLevelEncoder); // Assert the pass encoder is current. ASSERT(mCurrentEncoder == passEncoder); mCurrentEncoder = mTopLevelEncoder; - mPassUsages.push_back(std::move(passUsage)); + + if (type == PassType::Render) { + mRenderPassUsages.push_back(std::move(passUsage)); + } else { + mComputePassUsages.push_back(std::move(passUsage)); + } } - const PerPassUsages& EncodingContext::GetPassUsages() const { - ASSERT(!mWerePassUsagesAcquired); - return mPassUsages; + const RenderPassUsages& EncodingContext::GetRenderPassUsages() const { + ASSERT(!mWereRenderPassUsagesAcquired); + return mRenderPassUsages; } - PerPassUsages EncodingContext::AcquirePassUsages() { - ASSERT(!mWerePassUsagesAcquired); - mWerePassUsagesAcquired = true; - return std::move(mPassUsages); + RenderPassUsages EncodingContext::AcquireRenderPassUsages() { + ASSERT(!mWereRenderPassUsagesAcquired); + mWereRenderPassUsagesAcquired = true; + return std::move(mRenderPassUsages); + } + + const ComputePassUsages& EncodingContext::GetComputePassUsages() const { + ASSERT(!mWereComputePassUsagesAcquired); + return mComputePassUsages; + } + + ComputePassUsages EncodingContext::AcquireComputePassUsages() { + ASSERT(!mWereComputePassUsagesAcquired); + mWereComputePassUsagesAcquired = true; + return std::move(mComputePassUsages); } MaybeError EncodingContext::Finish() { diff --git a/src/dawn_native/EncodingContext.h b/src/dawn_native/EncodingContext.h index e74819a4cb..f181e57fb8 100644 --- a/src/dawn_native/EncodingContext.h +++ b/src/dawn_native/EncodingContext.h @@ -79,11 +79,13 @@ namespace dawn_native { // Functions to set current encoder state void EnterPass(const ObjectBase* passEncoder); - void ExitPass(const ObjectBase* passEncoder, PassResourceUsage passUsages); + void ExitPass(const ObjectBase* passEncoder, PassResourceUsage passUsages, PassType type); MaybeError Finish(); - const PerPassUsages& GetPassUsages() const; - PerPassUsages AcquirePassUsages(); + const RenderPassUsages& GetRenderPassUsages() const; + const ComputePassUsages& GetComputePassUsages() const; + RenderPassUsages AcquireRenderPassUsages(); + ComputePassUsages AcquireComputePassUsages(); private: bool IsFinished() const; @@ -101,8 +103,10 @@ namespace dawn_native { // CommandEncoder::Begin/EndPass. const ObjectBase* mCurrentEncoder; - PerPassUsages mPassUsages; - bool mWerePassUsagesAcquired = false; + RenderPassUsages mRenderPassUsages; + bool mWereRenderPassUsagesAcquired = false; + ComputePassUsages mComputePassUsages; + bool mWereComputePassUsagesAcquired = false; CommandAllocator mAllocator; CommandIterator mIterator; diff --git a/src/dawn_native/PassResourceUsage.h b/src/dawn_native/PassResourceUsage.h index 772f8c2e34..0f643468e4 100644 --- a/src/dawn_native/PassResourceUsage.h +++ b/src/dawn_native/PassResourceUsage.h @@ -30,27 +30,31 @@ namespace dawn_native { enum class PassType { Render, Compute }; // The texture usage inside passes must be tracked per-subresource. - using PassTextureUsage = SubresourceStorage; + using TextureSubresourceUsage = SubresourceStorage; - // Which resources are used by pass and how they are used. The command buffer validation - // pre-computes this information so that backends with explicit barriers don't have to - // re-compute it. - struct PassResourceUsage { - PassType passType; + // Which resources are used by a synchronization scope and how they are used. The command + // buffer validation pre-computes this information so that backends with explicit barriers + // don't have to re-compute it. + struct SyncScopeResourceUsage { std::vector buffers; std::vector bufferUsages; std::vector textures; - std::vector textureUsages; + std::vector textureUsages; + }; + // Additional data tracked per-pass. + struct PassResourceUsage : public SyncScopeResourceUsage { std::vector querySets; std::vector> queryAvailabilities; }; - using PerPassUsages = std::vector; + using RenderPassUsages = std::vector; + using ComputePassUsages = std::vector; struct CommandBufferResourceUsage { - PerPassUsages perPass; + RenderPassUsages renderPasses; + ComputePassUsages computePasses; std::set topLevelBuffers; std::set topLevelTextures; std::set usedQuerySets; diff --git a/src/dawn_native/PassResourceUsageTracker.cpp b/src/dawn_native/PassResourceUsageTracker.cpp index 81d72edf40..806d40be95 100644 --- a/src/dawn_native/PassResourceUsageTracker.cpp +++ b/src/dawn_native/PassResourceUsageTracker.cpp @@ -37,13 +37,13 @@ namespace dawn_native { TextureBase* texture = view->GetTexture(); const SubresourceRange& range = view->GetSubresourceRange(); - // Get or create a new PassTextureUsage for that texture (initially filled with + // Get or create a new TextureSubresourceUsage for that texture (initially filled with // wgpu::TextureUsage::None) auto it = mTextureUsages.emplace( std::piecewise_construct, std::forward_as_tuple(texture), std::forward_as_tuple(texture->GetFormat().aspects, texture->GetArrayLayers(), texture->GetNumMipLevels(), wgpu::TextureUsage::None)); - PassTextureUsage& textureUsage = it.first->second; + TextureSubresourceUsage& textureUsage = it.first->second; textureUsage.Update(range, [usage](const SubresourceRange&, wgpu::TextureUsage* storedUsage) { @@ -52,14 +52,14 @@ namespace dawn_native { } void PassResourceUsageTracker::AddTextureUsage(TextureBase* texture, - const PassTextureUsage& textureUsage) { - // Get or create a new PassTextureUsage for that texture (initially filled with + const TextureSubresourceUsage& textureUsage) { + // Get or create a new TextureSubresourceUsage for that texture (initially filled with // wgpu::TextureUsage::None) auto it = mTextureUsages.emplace( std::piecewise_construct, std::forward_as_tuple(texture), std::forward_as_tuple(texture->GetFormat().aspects, texture->GetArrayLayers(), texture->GetNumMipLevels(), wgpu::TextureUsage::None)); - PassTextureUsage* passTextureUsage = &it.first->second; + TextureSubresourceUsage* passTextureUsage = &it.first->second; passTextureUsage->Merge( textureUsage, [](const SubresourceRange&, wgpu::TextureUsage* storedUsage, @@ -86,7 +86,6 @@ namespace dawn_native { // Returns the per-pass usage for use by backends for APIs with explicit barriers. PassResourceUsage PassResourceUsageTracker::AcquireResourceUsage() { PassResourceUsage result; - result.passType = mPassType; result.buffers.reserve(mBufferUsages.size()); result.bufferUsages.reserve(mBufferUsages.size()); result.textures.reserve(mTextureUsages.size()); diff --git a/src/dawn_native/PassResourceUsageTracker.h b/src/dawn_native/PassResourceUsageTracker.h index cd54f8c346..d56d724bf5 100644 --- a/src/dawn_native/PassResourceUsageTracker.h +++ b/src/dawn_native/PassResourceUsageTracker.h @@ -38,7 +38,7 @@ namespace dawn_native { PassResourceUsageTracker(PassType passType); void BufferUsedAs(BufferBase* buffer, wgpu::BufferUsage usage); void TextureViewUsedAs(TextureViewBase* texture, wgpu::TextureUsage usage); - void AddTextureUsage(TextureBase* texture, const PassTextureUsage& textureUsage); + void AddTextureUsage(TextureBase* texture, const TextureSubresourceUsage& textureUsage); void TrackQueryAvailability(QuerySetBase* querySet, uint32_t queryIndex); const QueryAvailabilityMap& GetQueryAvailabilityMap() const; @@ -48,7 +48,7 @@ namespace dawn_native { private: PassType mPassType; std::map mBufferUsages; - std::map mTextureUsages; + std::map mTextureUsages; // Dedicated to track the availability of the queries used on render pass. The same query // cannot be written twice in same render pass, so each render pass also need to have its // own query availability map for validation. diff --git a/src/dawn_native/Queue.cpp b/src/dawn_native/Queue.cpp index c113c1efe1..c555d999fb 100644 --- a/src/dawn_native/Queue.cpp +++ b/src/dawn_native/Queue.cpp @@ -37,6 +37,18 @@ namespace dawn_native { namespace { + MaybeError ValidateSyncScopeUsedInSubmit(const SyncScopeResourceUsage& scope) { + for (const BufferBase* buffer : scope.buffers) { + DAWN_TRY(buffer->ValidateCanUseOnQueueNow()); + } + + for (const TextureBase* texture : scope.textures) { + DAWN_TRY(texture->ValidateCanUseInSubmitNow()); + } + + return {}; + } + void CopyTextureData(uint8_t* dstPointer, const uint8_t* srcPointer, uint32_t depth, @@ -410,13 +422,11 @@ namespace dawn_native { const CommandBufferResourceUsage& usages = commands[i]->GetResourceUsages(); - for (const PassResourceUsage& passUsages : usages.perPass) { - for (const BufferBase* buffer : passUsages.buffers) { - DAWN_TRY(buffer->ValidateCanUseOnQueueNow()); - } - for (const TextureBase* texture : passUsages.textures) { - DAWN_TRY(texture->ValidateCanUseInSubmitNow()); - } + for (const SyncScopeResourceUsage& scope : usages.renderPasses) { + DAWN_TRY(ValidateSyncScopeUsedInSubmit(scope)); + } + for (const SyncScopeResourceUsage& scope : usages.computePasses) { + DAWN_TRY(ValidateSyncScopeUsedInSubmit(scope)); } for (const BufferBase* buffer : usages.topLevelBuffers) { diff --git a/src/dawn_native/RenderBundleEncoder.cpp b/src/dawn_native/RenderBundleEncoder.cpp index 45647a42ca..8afcd32424 100644 --- a/src/dawn_native/RenderBundleEncoder.cpp +++ b/src/dawn_native/RenderBundleEncoder.cpp @@ -137,7 +137,7 @@ namespace dawn_native { const PassResourceUsage& usages) const { TRACE_EVENT0(GetDevice()->GetPlatform(), Validation, "RenderBundleEncoder::ValidateFinish"); DAWN_TRY(GetDevice()->ValidateObject(this)); - DAWN_TRY(ValidatePassResourceUsage(usages)); + DAWN_TRY(ValidateSyncScopeResourceUsage(usages)); return {}; } diff --git a/src/dawn_native/RenderPassEncoder.cpp b/src/dawn_native/RenderPassEncoder.cpp index 1ab90237d2..c5c52e4bb2 100644 --- a/src/dawn_native/RenderPassEncoder.cpp +++ b/src/dawn_native/RenderPassEncoder.cpp @@ -101,7 +101,8 @@ namespace dawn_native { allocator->Allocate(Command::EndRenderPass); return {}; })) { - mEncodingContext->ExitPass(this, mUsageTracker.AcquireResourceUsage()); + mEncodingContext->ExitPass(this, mUsageTracker.AcquireResourceUsage(), + PassType::Render); } } diff --git a/src/dawn_native/Texture.cpp b/src/dawn_native/Texture.cpp index 2e6eafb00c..f0ea858cef 100644 --- a/src/dawn_native/Texture.cpp +++ b/src/dawn_native/Texture.cpp @@ -441,7 +441,7 @@ namespace dawn_native { mIsSubresourceContentInitializedAtIndex = std::vector(subresourceCount, false); // Add readonly storage usage if the texture has a storage usage. The validation rules in - // ValidatePassResourceUsage will make sure we don't use both at the same time. + // ValidateSyncScopeResourceUsage will make sure we don't use both at the same time. if (mUsage & wgpu::TextureUsage::Storage) { mUsage |= kReadOnlyStorageTexture; } diff --git a/src/dawn_native/d3d12/CommandBufferD3D12.cpp b/src/dawn_native/d3d12/CommandBufferD3D12.cpp index 9e575a68e4..9d7f96d58c 100644 --- a/src/dawn_native/d3d12/CommandBufferD3D12.cpp +++ b/src/dawn_native/d3d12/CommandBufferD3D12.cpp @@ -682,8 +682,8 @@ namespace dawn_native { namespace d3d12 { } }; - const std::vector& passResourceUsages = GetResourceUsages().perPass; - uint32_t nextPassNumber = 0; + size_t nextComputePassNumber = 0; + size_t nextRenderPassNumber = 0; Command type; while (mCommands.NextCommandId(&type)) { @@ -691,12 +691,12 @@ namespace dawn_native { namespace d3d12 { case Command::BeginComputePass: { mCommands.NextCommand(); - PrepareResourcesForComputePass(commandContext, - passResourceUsages[nextPassNumber]); + PrepareResourcesForComputePass( + commandContext, GetResourceUsages().computePasses[nextComputePassNumber]); bindingTracker.SetInComputePass(true); DAWN_TRY(RecordComputePass(commandContext, &bindingTracker)); - nextPassNumber++; + nextComputePassNumber++; break; } @@ -705,14 +705,14 @@ namespace dawn_native { namespace d3d12 { mCommands.NextCommand(); const bool passHasUAV = PrepareResourcesForRenderPass( - commandContext, passResourceUsages[nextPassNumber]); + commandContext, GetResourceUsages().renderPasses[nextRenderPassNumber]); bindingTracker.SetInComputePass(false); LazyClearRenderPassAttachments(beginRenderPassCmd); DAWN_TRY(RecordRenderPass(commandContext, &bindingTracker, beginRenderPassCmd, passHasUAV)); - nextPassNumber++; + nextRenderPassNumber++; break; } diff --git a/src/dawn_native/d3d12/TextureD3D12.cpp b/src/dawn_native/d3d12/TextureD3D12.cpp index 6274801c20..11d9048dc1 100644 --- a/src/dawn_native/d3d12/TextureD3D12.cpp +++ b/src/dawn_native/d3d12/TextureD3D12.cpp @@ -776,7 +776,7 @@ namespace dawn_native { namespace d3d12 { void Texture::TrackUsageAndGetResourceBarrierForPass( CommandRecordingContext* commandContext, std::vector* barriers, - const PassTextureUsage& textureUsages) { + const TextureSubresourceUsage& textureUsages) { if (mResourceAllocation.GetInfo().mMethod != AllocationMethod::kExternal) { // Track the underlying heap to ensure residency. Heap* heap = ToBackend(mResourceAllocation.GetResourceHeap()); diff --git a/src/dawn_native/d3d12/TextureD3D12.h b/src/dawn_native/d3d12/TextureD3D12.h index 528dd21698..c7a652df07 100644 --- a/src/dawn_native/d3d12/TextureD3D12.h +++ b/src/dawn_native/d3d12/TextureD3D12.h @@ -64,7 +64,7 @@ namespace dawn_native { namespace d3d12 { void TrackUsageAndGetResourceBarrierForPass(CommandRecordingContext* commandContext, std::vector* barrier, - const PassTextureUsage& textureUsages); + const TextureSubresourceUsage& textureUsages); void TransitionUsageAndGetResourceBarrier(CommandRecordingContext* commandContext, std::vector* barrier, wgpu::TextureUsage usage, diff --git a/src/dawn_native/metal/CommandBufferMTL.mm b/src/dawn_native/metal/CommandBufferMTL.mm index f93737d748..0793f97cfd 100644 --- a/src/dawn_native/metal/CommandBufferMTL.mm +++ b/src/dawn_native/metal/CommandBufferMTL.mm @@ -551,25 +551,25 @@ namespace dawn_native { namespace metal { } MaybeError CommandBuffer::FillCommands(CommandRecordingContext* commandContext) { - const std::vector& passResourceUsages = GetResourceUsages().perPass; - size_t nextPassNumber = 0; + size_t nextComputePassNumber = 0; + size_t nextRenderPassNumber = 0; - auto LazyClearForPass = [](const PassResourceUsage& usages, + auto LazyClearForPass = [](const SyncScopeResourceUsage& scope, CommandRecordingContext* commandContext) { - for (size_t i = 0; i < usages.textures.size(); ++i) { - Texture* texture = ToBackend(usages.textures[i]); + for (size_t i = 0; i < scope.textures.size(); ++i) { + Texture* texture = ToBackend(scope.textures[i]); // Clear subresources that are not render attachments. Render attachments will be // cleared in RecordBeginRenderPass by setting the loadop to clear when the texture // subresource has not been initialized before the render pass. - usages.textureUsages[i].Iterate( + scope.textureUsages[i].Iterate( [&](const SubresourceRange& range, wgpu::TextureUsage usage) { if (usage & ~wgpu::TextureUsage::RenderAttachment) { texture->EnsureSubresourceContentInitialized(range); } }); } - for (BufferBase* bufferBase : usages.buffers) { + for (BufferBase* bufferBase : scope.buffers) { ToBackend(bufferBase)->EnsureDataInitialized(commandContext); } }; @@ -580,19 +580,21 @@ namespace dawn_native { namespace metal { case Command::BeginComputePass: { mCommands.NextCommand(); - LazyClearForPass(passResourceUsages[nextPassNumber], commandContext); + LazyClearForPass(GetResourceUsages().computePasses[nextComputePassNumber], + commandContext); commandContext->EndBlit(); DAWN_TRY(EncodeComputePass(commandContext)); - nextPassNumber++; + nextComputePassNumber++; break; } case Command::BeginRenderPass: { BeginRenderPassCmd* cmd = mCommands.NextCommand(); - LazyClearForPass(passResourceUsages[nextPassNumber], commandContext); + LazyClearForPass(GetResourceUsages().renderPasses[nextRenderPassNumber], + commandContext); commandContext->EndBlit(); LazyClearRenderPassAttachments(cmd); @@ -600,7 +602,7 @@ namespace dawn_native { namespace metal { DAWN_TRY(EncodeRenderPass(commandContext, descriptor.Get(), cmd->width, cmd->height)); - nextPassNumber++; + nextRenderPassNumber++; break; } diff --git a/src/dawn_native/opengl/CommandBufferGL.cpp b/src/dawn_native/opengl/CommandBufferGL.cpp index afb15a2ecb..54687a00d4 100644 --- a/src/dawn_native/opengl/CommandBufferGL.cpp +++ b/src/dawn_native/opengl/CommandBufferGL.cpp @@ -536,14 +536,14 @@ namespace dawn_native { namespace opengl { MaybeError CommandBuffer::Execute() { const OpenGLFunctions& gl = ToBackend(GetDevice())->gl; - auto TransitionForPass = [](const PassResourceUsage& usages) { - for (size_t i = 0; i < usages.textures.size(); i++) { - Texture* texture = ToBackend(usages.textures[i]); + auto LazyClearForPass = [](const SyncScopeResourceUsage& scope) { + for (size_t i = 0; i < scope.textures.size(); i++) { + Texture* texture = ToBackend(scope.textures[i]); // Clear subresources that are not render attachments. Render attachments will be // cleared in RecordBeginRenderPass by setting the loadop to clear when the texture // subresource has not been initialized before the render pass. - usages.textureUsages[i].Iterate( + scope.textureUsages[i].Iterate( [&](const SubresourceRange& range, wgpu::TextureUsage usage) { if (usage & ~wgpu::TextureUsage::RenderAttachment) { texture->EnsureSubresourceContentInitialized(range); @@ -551,34 +551,33 @@ namespace dawn_native { namespace opengl { }); } - for (BufferBase* bufferBase : usages.buffers) { + for (BufferBase* bufferBase : scope.buffers) { ToBackend(bufferBase)->EnsureDataInitialized(); } }; - const std::vector& passResourceUsages = GetResourceUsages().perPass; - uint32_t nextPassNumber = 0; + size_t nextComputePassNumber = 0; + size_t nextRenderPassNumber = 0; Command type; while (mCommands.NextCommandId(&type)) { switch (type) { case Command::BeginComputePass: { mCommands.NextCommand(); - TransitionForPass(passResourceUsages[nextPassNumber]); + LazyClearForPass(GetResourceUsages().computePasses[nextComputePassNumber]); DAWN_TRY(ExecuteComputePass()); - nextPassNumber++; + nextComputePassNumber++; break; } case Command::BeginRenderPass: { auto* cmd = mCommands.NextCommand(); - TransitionForPass(passResourceUsages[nextPassNumber]); - + LazyClearForPass(GetResourceUsages().renderPasses[nextRenderPassNumber]); LazyClearRenderPassAttachments(cmd); DAWN_TRY(ExecuteRenderPass(cmd)); - nextPassNumber++; + nextRenderPassNumber++; break; } diff --git a/src/dawn_native/vulkan/CommandBufferVk.cpp b/src/dawn_native/vulkan/CommandBufferVk.cpp index d154026a21..c014c85b17 100644 --- a/src/dawn_native/vulkan/CommandBufferVk.cpp +++ b/src/dawn_native/vulkan/CommandBufferVk.cpp @@ -596,8 +596,8 @@ namespace dawn_native { namespace vulkan { } }; - const std::vector& passResourceUsages = GetResourceUsages().perPass; - size_t nextPassNumber = 0; + size_t nextComputePassNumber = 0; + size_t nextRenderPassNumber = 0; Command type; while (mCommands.NextCommandId(&type)) { @@ -774,24 +774,26 @@ namespace dawn_native { namespace vulkan { case Command::BeginRenderPass: { BeginRenderPassCmd* cmd = mCommands.NextCommand(); - PrepareResourcesForRenderPass(device, recordingContext, - passResourceUsages[nextPassNumber]); + PrepareResourcesForRenderPass( + device, recordingContext, + GetResourceUsages().renderPasses[nextRenderPassNumber]); LazyClearRenderPassAttachments(cmd); DAWN_TRY(RecordRenderPass(recordingContext, cmd)); - nextPassNumber++; + nextRenderPassNumber++; break; } case Command::BeginComputePass: { mCommands.NextCommand(); - PrepareResourcesForComputePass(device, recordingContext, - passResourceUsages[nextPassNumber]); + PrepareResourcesForComputePass( + device, recordingContext, + GetResourceUsages().computePasses[nextComputePassNumber]); DAWN_TRY(RecordComputePass(recordingContext)); - nextPassNumber++; + nextComputePassNumber++; break; } diff --git a/src/dawn_native/vulkan/TextureVk.cpp b/src/dawn_native/vulkan/TextureVk.cpp index 59e753fd83..90f85f39f2 100644 --- a/src/dawn_native/vulkan/TextureVk.cpp +++ b/src/dawn_native/vulkan/TextureVk.cpp @@ -863,7 +863,7 @@ namespace dawn_native { namespace vulkan { } void Texture::TransitionUsageForPass(CommandRecordingContext* recordingContext, - const PassTextureUsage& textureUsages, + const TextureSubresourceUsage& textureUsages, std::vector* imageBarriers, VkPipelineStageFlags* srcStages, VkPipelineStageFlags* dstStages) { diff --git a/src/dawn_native/vulkan/TextureVk.h b/src/dawn_native/vulkan/TextureVk.h index 013a3b6217..908e468792 100644 --- a/src/dawn_native/vulkan/TextureVk.h +++ b/src/dawn_native/vulkan/TextureVk.h @@ -78,7 +78,7 @@ namespace dawn_native { namespace vulkan { VkPipelineStageFlags* srcStages, VkPipelineStageFlags* dstStages); void TransitionUsageForPass(CommandRecordingContext* recordingContext, - const PassTextureUsage& textureUsages, + const TextureSubresourceUsage& textureUsages, std::vector* imageBarriers, VkPipelineStageFlags* srcStages, VkPipelineStageFlags* dstStages);