From ef599fb0f431ebe030d066edd710305e833ff9e7 Mon Sep 17 00:00:00 2001 From: Charlie Lao Date: Tue, 11 Feb 2020 18:19:16 -0800 Subject: [PATCH] Vulkan: Fold glClear into draw call when command graph is disabled This sets ContextVk::mRenderPassCommandBuffer properly when glClear get called so that it wonr end up start anotyer render pass during setupDraw call. Bug: angleproject:4395 Change-Id: Id2110719ad4d70b4c410062466d32381fb5e1f88 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2050966 Commit-Queue: Charlie Lao Reviewed-by: Jamie Madill --- src/libANGLE/renderer/vulkan/ContextVk.cpp | 91 +++++++++++++++++++ src/libANGLE/renderer/vulkan/ContextVk.h | 6 ++ .../renderer/vulkan/FramebufferVk.cpp | 17 +++- 3 files changed, 111 insertions(+), 3 deletions(-) diff --git a/src/libANGLE/renderer/vulkan/ContextVk.cpp b/src/libANGLE/renderer/vulkan/ContextVk.cpp index 055e76a2c..e74e39bc8 100644 --- a/src/libANGLE/renderer/vulkan/ContextVk.cpp +++ b/src/libANGLE/renderer/vulkan/ContextVk.cpp @@ -46,6 +46,10 @@ namespace rx namespace { +// The value to assign an alpha channel that's emulated. The type is unsigned int, though it will +// automatically convert to the actual data type. +constexpr unsigned int kEmulatedAlphaValue = 1; + // For shader uniforms such as gl_DepthRange and the viewport size. struct GraphicsDriverUniforms { @@ -2188,6 +2192,93 @@ angle::Result ContextVk::drawElementsIndirect(const gl::Context *context, return angle::Result::Continue; } +angle::Result ContextVk::clearWithRenderPassOp( + const gl::Rectangle &clearArea, + gl::DrawBufferMask clearColorBuffers, + bool clearDepth, + bool clearStencil, + const VkClearColorValue &clearColorValue, + const VkClearDepthStencilValue &clearDepthStencilValue) +{ + ASSERT(!commandGraphEnabled()); + // Start a new render pass if: + // + // - no render pass has started, + // - there is a render pass started but it contains commands; we cannot modify its ops, so new + // render pass is needed, + // - the current render area doesn't match the clear area. We need the render area to be + // exactly as specified by the scissor for the loadOp to clear only that area. See + // ContextVk::updateScissor for more information. + vk::FramebufferHelper *framebuffer = mDrawFramebuffer->getFramebuffer(); + if (!framebuffer->valid() || !framebuffer->renderPassStartedButEmpty() || + framebuffer->getRenderPassRenderArea() != clearArea) + { + mGraphicsDirtyBits |= mNewGraphicsCommandBufferDirtyBits; + + ANGLE_TRY(mDrawFramebuffer->startNewRenderPass(this, clearArea, &mRenderPassCommandBuffer)); + } + else + { + mOutsideRenderPassCommands.flushToPrimary(&mPrimaryCommands); + } + + size_t attachmentIndexVk = 0; + + // Go through clearColorBuffers and set the appropriate loadOp and clear values. + for (size_t colorIndexGL : mDrawFramebuffer->getState().getEnabledDrawBuffers()) + { + if (clearColorBuffers.test(colorIndexGL)) + { + RenderTargetVk *renderTarget = mDrawFramebuffer->getColorDrawRenderTarget(colorIndexGL); + + // If the render target doesn't have alpha, but its emulated format has it, clear the + // alpha to 1. + VkClearColorValue value = clearColorValue; + if (mDrawFramebuffer->getEmulatedAlphaAttachmentMask()[colorIndexGL]) + { + const vk::Format &format = renderTarget->getImageFormat(); + if (format.vkFormatIsInt) + { + if (format.vkFormatIsUnsigned) + { + value.uint32[3] = kEmulatedAlphaValue; + } + else + { + value.int32[3] = kEmulatedAlphaValue; + } + } + else + { + value.float32[3] = kEmulatedAlphaValue; + } + } + + mRenderPassCommands.clearRenderPassColorAttachment(attachmentIndexVk, value); + } + ++attachmentIndexVk; + } + + // Set the appropriate loadOp and clear values for depth and stencil. + RenderTargetVk *depthStencilRenderTarget = mDrawFramebuffer->getDepthStencilRenderTarget(); + if (depthStencilRenderTarget) + { + if (clearDepth) + { + getRenderPassCommandBuffer().clearRenderPassDepthAttachment( + attachmentIndexVk, clearDepthStencilValue.depth); + } + + if (clearStencil) + { + getRenderPassCommandBuffer().clearRenderPassStencilAttachment( + attachmentIndexVk, clearDepthStencilValue.stencil); + } + } + + return angle::Result::Continue; +} + gl::GraphicsResetStatus ContextVk::getResetStatus() { if (mRenderer->isDeviceLost()) diff --git a/src/libANGLE/renderer/vulkan/ContextVk.h b/src/libANGLE/renderer/vulkan/ContextVk.h index e7b513ab7..c8714d69b 100644 --- a/src/libANGLE/renderer/vulkan/ContextVk.h +++ b/src/libANGLE/renderer/vulkan/ContextVk.h @@ -314,6 +314,12 @@ class ContextVk : public ContextImpl, public vk::Context, public vk::RenderPassO gl::PrimitiveMode mode, gl::DrawElementsType type, const void *indirect) override; + angle::Result clearWithRenderPassOp(const gl::Rectangle &clearArea, + gl::DrawBufferMask clearColorBuffers, + bool clearDepth, + bool clearStencil, + const VkClearColorValue &clearColorValue, + const VkClearDepthStencilValue &clearDepthStencilValue); // Device loss gl::GraphicsResetStatus getResetStatus() override; diff --git a/src/libANGLE/renderer/vulkan/FramebufferVk.cpp b/src/libANGLE/renderer/vulkan/FramebufferVk.cpp index 4542f9fc3..81e7b0857 100644 --- a/src/libANGLE/renderer/vulkan/FramebufferVk.cpp +++ b/src/libANGLE/renderer/vulkan/FramebufferVk.cpp @@ -292,9 +292,19 @@ angle::Result FramebufferVk::clearImpl(const gl::Context *context, { clearBuffersWithRenderPassLoadOp = clearColorBuffers; } - ANGLE_TRY(clearWithRenderPassOp( - contextVk, scissoredRenderArea, clearBuffersWithRenderPassLoadOp, clearDepth, - clearStencilWithRenderPassLoadOp, clearColorValue, modifiedDepthStencilValue)); + + if (contextVk->commandGraphEnabled()) + { + ANGLE_TRY(clearWithRenderPassOp( + contextVk, scissoredRenderArea, clearBuffersWithRenderPassLoadOp, clearDepth, + clearStencilWithRenderPassLoadOp, clearColorValue, modifiedDepthStencilValue)); + } + else + { + ANGLE_TRY(contextVk->clearWithRenderPassOp( + scissoredRenderArea, clearBuffersWithRenderPassLoadOp, clearDepth, + clearStencilWithRenderPassLoadOp, clearColorValue, modifiedDepthStencilValue)); + } // Fallback to other methods for whatever isn't cleared here. clearDepth = false; @@ -1246,6 +1256,7 @@ angle::Result FramebufferVk::clearWithRenderPassOp( const VkClearColorValue &clearColorValue, const VkClearDepthStencilValue &clearDepthStencilValue) { + ASSERT(contextVk->commandGraphEnabled()); // Start a new render pass if: // // - no render pass has started,