From 8cb23933b12e61a5a66bce75d37e8f62dbe97f0d Mon Sep 17 00:00:00 2001 From: Natasha Lee Date: Fri, 9 Aug 2019 17:56:30 +0000 Subject: [PATCH] Ensure clearing attachments is done via renderpass loadop Clear through loadop instead of standalone clear operation to optimize efficiency on modern desktop GPUs and mobile GPUs. Removed clear calls in TransitionForPass for render pass to help optimize clearing using loadop instead. Compute pass textures and sampled textures are still cleared in transition. Bug: dawn:145 Change-Id: I84082bdea3ed7be75683389132d8b296051731b7 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/8641 Reviewed-by: Austin Eng Reviewed-by: Kai Ninomiya Commit-Queue: Natasha Lee --- src/dawn_native/d3d12/CommandBufferD3D12.cpp | 32 ++++++++++++++------ src/dawn_native/opengl/CommandBufferGL.cpp | 10 ++++-- src/dawn_native/opengl/TextureGL.cpp | 10 ++++-- src/dawn_native/opengl/TextureGL.h | 3 +- src/dawn_native/vulkan/CommandBufferVk.cpp | 26 ++++++++++++---- src/tests/end2end/TextureZeroInitTests.cpp | 22 +++++++------- 6 files changed, 71 insertions(+), 32 deletions(-) diff --git a/src/dawn_native/d3d12/CommandBufferD3D12.cpp b/src/dawn_native/d3d12/CommandBufferD3D12.cpp index e041f8c774..60a23d03be 100644 --- a/src/dawn_native/d3d12/CommandBufferD3D12.cpp +++ b/src/dawn_native/d3d12/CommandBufferD3D12.cpp @@ -503,10 +503,13 @@ namespace dawn_native { namespace d3d12 { for (size_t i = 0; i < usages.textures.size(); ++i) { Texture* texture = ToBackend(usages.textures[i]); - // TODO(natlee@microsoft.com): Update clearing here when subresource tracking is - // implemented - texture->EnsureSubresourceContentInitialized( - commandList, 0, texture->GetNumMipLevels(), 0, texture->GetArrayLayers()); + // Clear textures that are not output attachments. Output attachments will be + // cleared during record render pass if the texture subresource has not been + // initialized before the render pass. + if (!(usages.textureUsages[i] & dawn::TextureUsageBit::OutputAttachment)) { + texture->EnsureSubresourceContentInitialized( + commandList, 0, texture->GetNumMipLevels(), 0, texture->GetArrayLayers()); + } } for (size_t i = 0; i < usages.textures.size(); ++i) { @@ -869,15 +872,26 @@ namespace dawn_native { namespace d3d12 { // Load op - color ASSERT(view->GetLevelCount() == 1); ASSERT(view->GetLayerCount() == 1); - if (attachmentInfo.loadOp == dawn::LoadOp::Clear) { + if (attachmentInfo.loadOp == dawn::LoadOp::Clear || + (attachmentInfo.loadOp == dawn::LoadOp::Load && + !view->GetTexture()->IsSubresourceContentInitialized( + view->GetBaseMipLevel(), 1, view->GetBaseArrayLayer(), 1))) { D3D12_CPU_DESCRIPTOR_HANDLE handle = args.RTVs[i]; commandList->ClearRenderTargetView(handle, &attachmentInfo.clearColor.r, 0, nullptr); - } else if (attachmentInfo.loadOp == dawn::LoadOp::Load && view->GetTexture()) { - ToBackend(view->GetTexture()) - ->EnsureSubresourceContentInitialized(commandList, view->GetBaseMipLevel(), - 1, view->GetBaseArrayLayer(), 1); } + + TextureView* resolveView = ToBackend(attachmentInfo.resolveTarget.Get()); + if (resolveView != nullptr) { + // We need to set the resolve target to initialized so that it does not get + // cleared later in the pipeline. The texture will be resolved from the source + // color attachment, which will be correctly initialized. + ToBackend(resolveView->GetTexture()) + ->SetIsSubresourceContentInitialized( + resolveView->GetBaseMipLevel(), resolveView->GetLevelCount(), + resolveView->GetBaseArrayLayer(), resolveView->GetLayerCount()); + } + switch (attachmentInfo.storeOp) { case dawn::StoreOp::Store: { view->GetTexture()->SetIsSubresourceContentInitialized( diff --git a/src/dawn_native/opengl/CommandBufferGL.cpp b/src/dawn_native/opengl/CommandBufferGL.cpp index f8317bef0f..32ee0bf15b 100644 --- a/src/dawn_native/opengl/CommandBufferGL.cpp +++ b/src/dawn_native/opengl/CommandBufferGL.cpp @@ -362,8 +362,14 @@ namespace dawn_native { namespace opengl { auto TransitionForPass = [](const PassResourceUsage& usages) { for (size_t i = 0; i < usages.textures.size(); i++) { Texture* texture = ToBackend(usages.textures[i]); - texture->EnsureSubresourceContentInitialized(0, texture->GetNumMipLevels(), 0, - texture->GetArrayLayers()); + // We count the lazy clears for non output attachment textures and depth stencil + // textures in order to match the backdoor lazy clear counts in Vulkan and D3D12. + bool isLazyClear = + ((!(usages.textureUsages[i] & dawn::TextureUsageBit::OutputAttachment) && + texture->GetFormat().IsColor()) || + texture->GetFormat().HasDepthOrStencil()); + texture->EnsureSubresourceContentInitialized( + 0, texture->GetNumMipLevels(), 0, texture->GetArrayLayers(), isLazyClear); } }; diff --git a/src/dawn_native/opengl/TextureGL.cpp b/src/dawn_native/opengl/TextureGL.cpp index 06a11bd42e..67c436a58b 100644 --- a/src/dawn_native/opengl/TextureGL.cpp +++ b/src/dawn_native/opengl/TextureGL.cpp @@ -221,20 +221,24 @@ namespace dawn_native { namespace opengl { nullptr); } } - SetIsSubresourceContentInitialized(baseMipLevel, levelCount, baseArrayLayer, layerCount); - GetDevice()->IncrementLazyClearCountForTesting(); } void Texture::EnsureSubresourceContentInitialized(uint32_t baseMipLevel, uint32_t levelCount, uint32_t baseArrayLayer, - uint32_t layerCount) { + uint32_t layerCount, + bool isLazyClear) { if (!GetDevice()->IsToggleEnabled(Toggle::LazyClearResourceOnFirstUse)) { return; } if (!IsSubresourceContentInitialized(baseMipLevel, levelCount, baseArrayLayer, layerCount)) { ClearTexture(baseMipLevel, levelCount, baseArrayLayer, layerCount); + if (isLazyClear) { + GetDevice()->IncrementLazyClearCountForTesting(); + } + SetIsSubresourceContentInitialized(baseMipLevel, levelCount, baseArrayLayer, + layerCount); } } diff --git a/src/dawn_native/opengl/TextureGL.h b/src/dawn_native/opengl/TextureGL.h index 4b8798e462..40d82e8a91 100644 --- a/src/dawn_native/opengl/TextureGL.h +++ b/src/dawn_native/opengl/TextureGL.h @@ -40,7 +40,8 @@ namespace dawn_native { namespace opengl { void EnsureSubresourceContentInitialized(uint32_t baseMipLevel, uint32_t levelCount, uint32_t baseArrayLayer, - uint32_t layerCount); + uint32_t layerCount, + bool isLazyClear = true); private: void DestroyImpl() override; diff --git a/src/dawn_native/vulkan/CommandBufferVk.cpp b/src/dawn_native/vulkan/CommandBufferVk.cpp index 66a0dd260f..28e9058c01 100644 --- a/src/dawn_native/vulkan/CommandBufferVk.cpp +++ b/src/dawn_native/vulkan/CommandBufferVk.cpp @@ -214,6 +214,18 @@ namespace dawn_native { namespace vulkan { view->GetBaseMipLevel(), 1, view->GetBaseArrayLayer(), 1)) { loadOp = dawn::LoadOp::Clear; } + + if (hasResolveTarget) { + // We need to set the resolve target to initialized so that it does not get + // cleared later in the pipeline. The texture will be resolved from the + // source color attachment, which will be correctly initialized. + TextureView* resolveView = ToBackend(attachmentInfo.resolveTarget.Get()); + ToBackend(resolveView->GetTexture()) + ->SetIsSubresourceContentInitialized( + resolveView->GetBaseMipLevel(), resolveView->GetLevelCount(), + resolveView->GetBaseArrayLayer(), resolveView->GetLayerCount()); + } + switch (attachmentInfo.storeOp) { case dawn::StoreOp::Store: { view->GetTexture()->SetIsSubresourceContentInitialized( @@ -355,15 +367,17 @@ namespace dawn_native { namespace vulkan { } for (size_t i = 0; i < usages.textures.size(); ++i) { Texture* texture = ToBackend(usages.textures[i]); - - // TODO(natlee@microsoft.com): Update clearing here when subresource tracking is - // implemented - texture->EnsureSubresourceContentInitialized( - recordingContext, 0, texture->GetNumMipLevels(), 0, texture->GetArrayLayers()); + // Clear textures that are not output attachments. Output attachments will be + // cleared in RecordBeginRenderPass by setting the loadop to clear when the + // texture subresource has not been initialized before the render pass. + if (!(usages.textureUsages[i] & dawn::TextureUsageBit::OutputAttachment)) { + texture->EnsureSubresourceContentInitialized(recordingContext, 0, + texture->GetNumMipLevels(), 0, + texture->GetArrayLayers()); + } texture->TransitionUsageNow(recordingContext, usages.textureUsages[i]); } }; - const std::vector& passResourceUsages = GetResourceUsages().perPass; size_t nextPassNumber = 0; diff --git a/src/tests/end2end/TextureZeroInitTests.cpp b/src/tests/end2end/TextureZeroInitTests.cpp index 77e82c28ca..bef10dc7b8 100644 --- a/src/tests/end2end/TextureZeroInitTests.cpp +++ b/src/tests/end2end/TextureZeroInitTests.cpp @@ -121,7 +121,7 @@ TEST_P(TextureZeroInitTest, RenderingMipMapClearsToZero) { pass.EndPass(); } dawn::CommandBuffer commands = encoder.Finish(); - EXPECT_LAZY_CLEAR(1u, queue.Submit(1, &commands)); + EXPECT_LAZY_CLEAR(0u, queue.Submit(1, &commands)); uint32_t mipSize = kSize >> 2; std::vector expected(mipSize * mipSize, {0, 0, 0, 0}); @@ -149,7 +149,7 @@ TEST_P(TextureZeroInitTest, RenderingArrayLayerClearsToZero) { pass.EndPass(); } dawn::CommandBuffer commands = encoder.Finish(); - EXPECT_LAZY_CLEAR(1u, queue.Submit(1, &commands)); + EXPECT_LAZY_CLEAR(0u, queue.Submit(1, &commands)); std::vector expected(kSize * kSize, {0, 0, 0, 0}); @@ -328,8 +328,8 @@ TEST_P(TextureZeroInitTest, RenderingLoadingDepth) { pass.Draw(6, 1, 0, 0); pass.EndPass(); dawn::CommandBuffer commandBuffer = encoder.Finish(); - // Expect 2 lazy clears, one for the srcTexture and one for the depthStencilTexture - EXPECT_LAZY_CLEAR(2u, queue.Submit(1, &commandBuffer)); + // Expect 1 lazy clear for the depthStencilTexture + EXPECT_LAZY_CLEAR(1u, queue.Submit(1, &commandBuffer)); // Expect the texture to be red because depth test passed. std::vector expected(kSize * kSize, {255, 0, 0, 255}); @@ -363,8 +363,8 @@ TEST_P(TextureZeroInitTest, RenderingLoadingStencil) { pass.Draw(6, 1, 0, 0); pass.EndPass(); dawn::CommandBuffer commandBuffer = encoder.Finish(); - // Expect 2 lazy clears, one for srcTexture and one for depthStencilTexture. - EXPECT_LAZY_CLEAR(2u, queue.Submit(1, &commandBuffer)); + // Expect 1 lazy clear for depthStencilTexture. + EXPECT_LAZY_CLEAR(1u, queue.Submit(1, &commandBuffer)); // Expect the texture to be red because stencil test passed. std::vector expected(kSize * kSize, {255, 0, 0, 255}); @@ -397,8 +397,8 @@ TEST_P(TextureZeroInitTest, RenderingLoadingDepthStencil) { pass.Draw(6, 1, 0, 0); pass.EndPass(); dawn::CommandBuffer commandBuffer = encoder.Finish(); - // Expect 2 lazy clears, one for srcTexture and one for depthStencilTexture. - EXPECT_LAZY_CLEAR(2u, queue.Submit(1, &commandBuffer)); + // Expect 1 lazy clear for depthStencilTexture. + EXPECT_LAZY_CLEAR(1u, queue.Submit(1, &commandBuffer)); // Expect the texture to be red because both depth and stencil tests passed. std::vector expected(kSize * kSize, {255, 0, 0, 255}); @@ -419,7 +419,7 @@ TEST_P(TextureZeroInitTest, ColorAttachmentsClear) { pass.EndPass(); dawn::CommandBuffer commands = encoder.Finish(); - EXPECT_LAZY_CLEAR(1u, queue.Submit(1, &commands)); + EXPECT_LAZY_CLEAR(0u, queue.Submit(1, &commands)); std::vector expected(kSize * kSize, {0, 0, 0, 0}); EXPECT_TEXTURE_RGBA8_EQ(expected.data(), renderPass.color, 0, 0, kSize, kSize, 0, 0); @@ -487,8 +487,8 @@ TEST_P(TextureZeroInitTest, RenderPassSampledTextureClear) { pass.Draw(6, 1, 0, 0); pass.EndPass(); dawn::CommandBuffer commands = encoder.Finish(); - // Expect 2 lazy clears, one for the sampled texture src and one for the rendered target texture - EXPECT_LAZY_CLEAR(2u, queue.Submit(1, &commands)); + // Expect 1 lazy clear for sampled texture + EXPECT_LAZY_CLEAR(1u, queue.Submit(1, &commands)); // Expect the rendered texture to be cleared std::vector expectedWithZeros(kSize * kSize, {0, 0, 0, 0});