Vulkan: Merge append/beginWriteResource.

The new API is named 'recordCommands'.

These two APIs were basically doing the same thing. We don't need to
have an understanding of creating a new graph node to know that we
want to record some Vulkan commands to a command buffer. The prior
design was actually masking a bug where we would allow appending
commands to a command graph node that had already started a render
pass. Fix this by adding a render pass check to recordCommands.

Also removes 'hasStartedWriteResource' since this method wasn't used
anywhere.

Also renames 'onResourceChanged' to 'finishCurrentCommands'.

Bug: angleproject:2828
Change-Id: I00bd5b893fcfc37172b6c1706cb2f5fc57e79f54
Reviewed-on: https://chromium-review.googlesource.com/1235654
Commit-Queue: Jamie Madill <jmadill@chromium.org>
Reviewed-by: Frank Henigman <fjhenigman@chromium.org>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
diff --git a/src/libANGLE/renderer/vulkan/BufferVk.cpp b/src/libANGLE/renderer/vulkan/BufferVk.cpp
index 1a4deb6..b41ca31 100644
--- a/src/libANGLE/renderer/vulkan/BufferVk.cpp
+++ b/src/libANGLE/renderer/vulkan/BufferVk.cpp
@@ -216,7 +216,7 @@
         // 'beginWriteResource' will stop any subsequent rendering from using the old buffer data,
         // by marking any current read operations / command buffers as 'finished'.
         vk::CommandBuffer *commandBuffer = nullptr;
-        ANGLE_TRY(beginWriteResource(contextVk, &commandBuffer));
+        ANGLE_TRY(recordCommands(contextVk, &commandBuffer));
 
         // Insert a barrier to ensure reads from the buffer are complete.
         // TODO(jmadill): Insert minimal barriers.
diff --git a/src/libANGLE/renderer/vulkan/CommandGraph.cpp b/src/libANGLE/renderer/vulkan/CommandGraph.cpp
index 9d91a80..3dd7467 100644
--- a/src/libANGLE/renderer/vulkan/CommandGraph.cpp
+++ b/src/libANGLE/renderer/vulkan/CommandGraph.cpp
@@ -81,28 +81,16 @@
     return mStoredQueueSerial;
 }
 
-bool CommandGraphResource::hasStartedWriteResource() const
-{
-    return hasChildlessWritingNode() &&
-           mCurrentWritingNode->getOutsideRenderPassCommands()->valid();
-}
-
-angle::Result CommandGraphResource::beginWriteResource(Context *context,
-                                                       CommandBuffer **commandBufferOut)
-{
-    onResourceChanged(context->getRenderer());
-    return mCurrentWritingNode->beginOutsideRenderPassRecording(
-        context, context->getRenderer()->getCommandPool(), commandBufferOut);
-}
-
-angle::Result CommandGraphResource::appendWriteResource(Context *context,
-                                                        CommandBuffer **commandBufferOut)
+angle::Result CommandGraphResource::recordCommands(Context *context,
+                                                   CommandBuffer **commandBufferOut)
 {
     updateQueueSerial(context->getRenderer()->getCurrentQueueSerial());
 
-    if (!hasChildlessWritingNode())
+    if (!hasChildlessWritingNode() || hasStartedRenderPass())
     {
-        return beginWriteResource(context, commandBufferOut);
+        finishCurrentCommands(context->getRenderer());
+        return mCurrentWritingNode->beginOutsideRenderPassRecording(
+            context, context->getRenderer()->getCommandPool(), commandBufferOut);
     }
 
     CommandBuffer *outsideRenderPassCommands = mCurrentWritingNode->getOutsideRenderPassCommands();
@@ -154,7 +142,7 @@
     return mCurrentWritingNode->beginInsideRenderPassRecording(context, commandBufferOut);
 }
 
-void CommandGraphResource::onResourceChanged(RendererVk *renderer)
+void CommandGraphResource::finishCurrentCommands(RendererVk *renderer)
 {
     CommandGraphNode *newCommands = renderer->getCommandGraph()->allocateNode();
     onWriteImpl(newCommands, renderer->getCurrentQueueSerial());
diff --git a/src/libANGLE/renderer/vulkan/CommandGraph.h b/src/libANGLE/renderer/vulkan/CommandGraph.h
index 16a7d15..2ac17ef 100644
--- a/src/libANGLE/renderer/vulkan/CommandGraph.h
+++ b/src/libANGLE/renderer/vulkan/CommandGraph.h
@@ -125,15 +125,8 @@
   protected:
     // Allocates a write node via getNewWriteNode and returns a started command buffer.
     // The started command buffer will render outside of a RenderPass.
-    angle::Result beginWriteResource(Context *context, CommandBuffer **commandBufferOut);
-
-    // Check if we have started writing outside a RenderPass.
-    bool hasStartedWriteResource() const;
-
-    // Starts rendering to an existing command buffer for the resource.
-    // The started command buffer will render outside of a RenderPass.
-    // Calls beginWriteResource if we have not yet started writing.
-    angle::Result appendWriteResource(Context *context, CommandBuffer **commandBufferOut);
+    // Will append to an existing command buffer/graph node if possible.
+    angle::Result recordCommands(Context *context, CommandBuffer **commandBufferOut);
 
     // Begins a command buffer on the current graph node for in-RenderPass rendering.
     // Currently only called from FramebufferVk::getCommandBufferForDraw.
@@ -152,7 +145,7 @@
     const gl::Rectangle &getRenderPassRenderArea() const;
 
     // Called when 'this' object changes, but we'd like to start a new command buffer later.
-    void onResourceChanged(RendererVk *renderer);
+    void finishCurrentCommands(RendererVk *renderer);
 
     // Get the current queue serial for this resource. Only used to release resources.
     Serial getStoredQueueSerial() const;
diff --git a/src/libANGLE/renderer/vulkan/FramebufferVk.cpp b/src/libANGLE/renderer/vulkan/FramebufferVk.cpp
index dbd9405..c45a9e9 100644
--- a/src/libANGLE/renderer/vulkan/FramebufferVk.cpp
+++ b/src/libANGLE/renderer/vulkan/FramebufferVk.cpp
@@ -232,7 +232,7 @@
     // Standard Depth/stencil clear without scissor.
     if (clearDepth || clearStencil)
     {
-        ANGLE_TRY(beginWriteResource(contextVk, &commandBuffer));
+        ANGLE_TRY(recordCommands(contextVk, &commandBuffer));
 
         const VkClearDepthStencilValue &clearDepthStencilValue =
             contextVk->getClearDepthStencilValue().depthStencil;
@@ -255,7 +255,7 @@
 
     if (!commandBuffer)
     {
-        ANGLE_TRY(beginWriteResource(contextVk, &commandBuffer));
+        ANGLE_TRY(recordCommands(contextVk, &commandBuffer));
     }
 
     // TODO(jmadill): Support gaps in RenderTargets. http://anglebug.com/2394
@@ -482,7 +482,7 @@
 
     // Reinitialize the commandBuffer after a read pixels because it calls
     // renderer->finish which makes command buffers obsolete.
-    ANGLE_TRY(beginWriteResource(contextVk, &commandBuffer));
+    ANGLE_TRY(recordCommands(contextVk, &commandBuffer));
 
     // We read the bytes of the image in a buffer, now we have to copy them into the
     // destination target.
@@ -516,7 +516,7 @@
     bool blitStencilBuffer                   = (mask & GL_STENCIL_BUFFER_BIT) != 0;
 
     vk::CommandBuffer *commandBuffer = nullptr;
-    ANGLE_TRY(beginWriteResource(contextVk, &commandBuffer));
+    ANGLE_TRY(recordCommands(contextVk, &commandBuffer));
     FramebufferVk *sourceFramebufferVk = vk::GetImpl(sourceFramebuffer);
     bool flipSource                    = contextVk->isViewportFlipEnabledForReadFBO();
     bool flipDest                      = contextVk->isViewportFlipEnabledForDrawFBO();
@@ -772,7 +772,7 @@
 
     // Will freeze the current set of dependencies on this FBO. The next time we render we will
     // create a new entry in the command graph.
-    onResourceChanged(renderer);
+    finishCurrentCommands(renderer);
 
     contextVk->invalidateCurrentPipeline();
 
@@ -879,7 +879,7 @@
                                                        bool clearStencil)
 {
     // Trigger a new command node to ensure overlapping writes happen sequentially.
-    onResourceChanged(contextVk->getRenderer());
+    finishCurrentCommands(contextVk->getRenderer());
 
     // This command can only happen inside a render pass, so obtain one if its already happening
     // or create a new one if not.
@@ -980,7 +980,7 @@
     vk::ShaderLibrary *shaderLibrary = renderer->getShaderLibrary();
 
     // Trigger a new command node to ensure overlapping writes happen sequentially.
-    onResourceChanged(renderer);
+    finishCurrentCommands(renderer);
 
     const vk::ShaderAndSerial *fullScreenQuad = nullptr;
     ANGLE_TRY(shaderLibrary->getShader(contextVk, vk::InternalShaderID::FullScreenQuad_vert,
@@ -1040,7 +1040,7 @@
     pipeline->updateSerial(renderer->getCurrentQueueSerial());
 
     vk::CommandBuffer *writeCommands = nullptr;
-    ANGLE_TRY(appendWriteResource(contextVk, &writeCommands));
+    ANGLE_TRY(recordCommands(contextVk, &writeCommands));
 
     // If the format of the framebuffer does not have an alpha channel, we need to make sure we does
     // not affect the alpha channel of the type we're using to emulate the format.
@@ -1099,7 +1099,7 @@
     std::vector<VkClearValue> attachmentClearValues;
 
     vk::CommandBuffer *writeCommands = nullptr;
-    ANGLE_TRY(appendWriteResource(contextVk, &writeCommands));
+    ANGLE_TRY(recordCommands(contextVk, &writeCommands));
 
     vk::RenderPassDesc renderPassDesc;
 
@@ -1152,7 +1152,7 @@
     RendererVk *renderer = contextVk->getRenderer();
 
     vk::CommandBuffer *commandBuffer = nullptr;
-    ANGLE_TRY(beginWriteResource(contextVk, &commandBuffer));
+    ANGLE_TRY(recordCommands(contextVk, &commandBuffer));
 
     // Note that although we're reading from the image, we need to update the layout below.
 
diff --git a/src/libANGLE/renderer/vulkan/RenderbufferVk.cpp b/src/libANGLE/renderer/vulkan/RenderbufferVk.cpp
index 3759003..74bd4b8 100644
--- a/src/libANGLE/renderer/vulkan/RenderbufferVk.cpp
+++ b/src/libANGLE/renderer/vulkan/RenderbufferVk.cpp
@@ -87,7 +87,7 @@
 
         // TODO(jmadill): Fold this into the RenderPass load/store ops. http://anglebug.com/2361
         vk::CommandBuffer *commandBuffer = nullptr;
-        ANGLE_TRY(beginWriteResource(contextVk, &commandBuffer));
+        ANGLE_TRY(recordCommands(contextVk, &commandBuffer));
 
         if (isDepthOrStencilFormat)
         {
diff --git a/src/libANGLE/renderer/vulkan/SurfaceVk.cpp b/src/libANGLE/renderer/vulkan/SurfaceVk.cpp
index e1a58d0..bd7a94b 100644
--- a/src/libANGLE/renderer/vulkan/SurfaceVk.cpp
+++ b/src/libANGLE/renderer/vulkan/SurfaceVk.cpp
@@ -480,7 +480,7 @@
 
     // Allocate a command buffer for clearing our images to black.
     vk::CommandBuffer *commandBuffer = nullptr;
-    ANGLE_TRY(beginWriteResource(displayVk, &commandBuffer));
+    ANGLE_TRY(recordCommands(displayVk, &commandBuffer));
 
     VkClearColorValue transparentBlack;
     transparentBlack.float32[0] = 0.0f;
@@ -565,7 +565,7 @@
     RendererVk *renderer = displayVk->getRenderer();
 
     vk::CommandBuffer *swapCommands = nullptr;
-    ANGLE_TRY(beginWriteResource(displayVk, &swapCommands));
+    ANGLE_TRY(recordCommands(displayVk, &swapCommands));
 
     SwapchainImage &image = mSwapchainImages[mCurrentSwapchainImageIndex];
 
diff --git a/src/libANGLE/renderer/vulkan/TextureVk.cpp b/src/libANGLE/renderer/vulkan/TextureVk.cpp
index c35bfff..b106f92 100644
--- a/src/libANGLE/renderer/vulkan/TextureVk.cpp
+++ b/src/libANGLE/renderer/vulkan/TextureVk.cpp
@@ -475,7 +475,7 @@
     }
 
     // Create a new graph node to store image initialization commands.
-    onResourceChanged(renderer);
+    finishCurrentCommands(renderer);
 
     // Handle initial data.
     if (pixels)
@@ -503,7 +503,7 @@
         gl::Offset(area.x, area.y, area.z), formatInfo, unpack, type, pixels));
 
     // Create a new graph node to store image initialization commands.
-    onResourceChanged(contextVk->getRenderer());
+    finishCurrentCommands(contextVk->getRenderer());
 
     return gl::NoError();
 }
@@ -629,7 +629,7 @@
         gl::Extents(clippedSourceArea.width, clippedSourceArea.height, 1), internalFormat,
         framebufferVk));
 
-    onResourceChanged(renderer);
+    finishCurrentCommands(renderer);
     framebufferVk->addReadDependency(this);
     return angle::Result::Continue();
 }
@@ -679,7 +679,7 @@
                       unpackUnmultiplyAlpha);
 
     // Create a new graph node to store image initialization commands.
-    onResourceChanged(contextVk->getRenderer());
+    finishCurrentCommands(contextVk->getRenderer());
 
     return angle::Result::Continue();
 }
@@ -687,7 +687,7 @@
 angle::Result TextureVk::getCommandBufferForWrite(ContextVk *contextVk,
                                                   vk::CommandBuffer **commandBufferOut)
 {
-    ANGLE_TRY(appendWriteResource(contextVk, commandBufferOut));
+    ANGLE_TRY(recordCommands(contextVk, commandBufferOut));
     return angle::Result::Continue();
 }
 
@@ -999,7 +999,7 @@
     }
 
     // We're changing this textureVk content, make sure we let the graph know.
-    onResourceChanged(renderer);
+    finishCurrentCommands(renderer);
 
     return gl::NoError();
 }
diff --git a/src/libANGLE/renderer/vulkan/vk_helpers.cpp b/src/libANGLE/renderer/vulkan/vk_helpers.cpp
index e375506..68cadd6 100644
--- a/src/libANGLE/renderer/vulkan/vk_helpers.cpp
+++ b/src/libANGLE/renderer/vulkan/vk_helpers.cpp
@@ -425,7 +425,7 @@
     std::array<VkBufferCopy, 2> copies = {{copy1, copy2}};
 
     vk::CommandBuffer *commandBuffer;
-    ANGLE_TRY(beginWriteResource(context, &commandBuffer));
+    ANGLE_TRY(recordCommands(context, &commandBuffer));
 
     elementArrayBufferVk->addReadDependency(this);
     commandBuffer->copyBuffer(elementArrayBufferVk->getVkBuffer().getHandle(), *bufferHandleOut, 2,