Track clear counts in vulkan render passes

Though not required by spec, the validation layers have started spitting
out warnings if the clearValueCount in BeginRenderPass is greater than
the number of attachments actually being cleared. This just adds tracking
of the count to appease the validation layers.

BUG=skia:

Change-Id: Iac6500df3ed5ad3f5df5f045c6e533bb021857aa
Reviewed-on: https://skia-review.googlesource.com/7401
Reviewed-by: Jim Van Verth <jvanverth@google.com>
Commit-Queue: Greg Daniel <egdaniel@google.com>
diff --git a/src/gpu/vk/GrVkCommandBuffer.cpp b/src/gpu/vk/GrVkCommandBuffer.cpp
index 91acf98..b1d20e2 100644
--- a/src/gpu/vk/GrVkCommandBuffer.cpp
+++ b/src/gpu/vk/GrVkCommandBuffer.cpp
@@ -345,7 +345,6 @@
 
 void GrVkPrimaryCommandBuffer::beginRenderPass(const GrVkGpu* gpu,
                                                const GrVkRenderPass* renderPass,
-                                               uint32_t clearCount,
                                                const VkClearValue* clearValues,
                                                const GrVkRenderTarget& target,
                                                const SkIRect& bounds,
@@ -365,7 +364,7 @@
     beginInfo.renderPass = renderPass->vkRenderPass();
     beginInfo.framebuffer = target.framebuffer()->framebuffer();
     beginInfo.renderArea = renderArea;
-    beginInfo.clearValueCount = clearCount;
+    beginInfo.clearValueCount = renderPass->clearValueCount();
     beginInfo.pClearValues = clearValues;
 
     VkSubpassContents contents = forSecondaryCB ? VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS
diff --git a/src/gpu/vk/GrVkCommandBuffer.h b/src/gpu/vk/GrVkCommandBuffer.h
index 8020c7d..e28df44 100644
--- a/src/gpu/vk/GrVkCommandBuffer.h
+++ b/src/gpu/vk/GrVkCommandBuffer.h
@@ -210,7 +210,6 @@
     // in the render pass.
     void beginRenderPass(const GrVkGpu* gpu,
                          const GrVkRenderPass* renderPass,
-                         uint32_t clearCount,
                          const VkClearValue* clearValues,
                          const GrVkRenderTarget& target,
                          const SkIRect& bounds,
diff --git a/src/gpu/vk/GrVkCopyManager.cpp b/src/gpu/vk/GrVkCopyManager.cpp
index b1f8f2a..269ec5e 100644
--- a/src/gpu/vk/GrVkCopyManager.cpp
+++ b/src/gpu/vk/GrVkCopyManager.cpp
@@ -320,7 +320,7 @@
     SkASSERT(renderPass->isCompatible(*rt->simpleRenderPass()));
 
 
-    cmdBuffer->beginRenderPass(gpu, renderPass, 0, nullptr, *rt, bounds, false);
+    cmdBuffer->beginRenderPass(gpu, renderPass, nullptr, *rt, bounds, false);
     cmdBuffer->bindPipeline(gpu, pipeline);
 
     // Uniform DescriptorSet, Sampler DescriptorSet, and vertex shader uniformBuffer
diff --git a/src/gpu/vk/GrVkGpu.cpp b/src/gpu/vk/GrVkGpu.cpp
index b94a379..9bb4c57 100644
--- a/src/gpu/vk/GrVkGpu.cpp
+++ b/src/gpu/vk/GrVkGpu.cpp
@@ -1838,10 +1838,7 @@
         pBounds = &adjustedBounds;
     }
 
-    // Currently it is fine for us to always pass in 1 for the clear count even if no attachment
-    // uses it. In the current state, we also only use the LOAD_OP_CLEAR for the color attachment
-    // which is always at the first attachment.
-    fCurrentCmdBuffer->beginRenderPass(this, renderPass, 1, colorClear, *target, *pBounds, true);
+    fCurrentCmdBuffer->beginRenderPass(this, renderPass, colorClear, *target, *pBounds, true);
     fCurrentCmdBuffer->executeCommands(this, buffer);
     fCurrentCmdBuffer->endRenderPass(this);
 
diff --git a/src/gpu/vk/GrVkRenderPass.cpp b/src/gpu/vk/GrVkRenderPass.cpp
index ee2d3d9..a6a6afb 100644
--- a/src/gpu/vk/GrVkRenderPass.cpp
+++ b/src/gpu/vk/GrVkRenderPass.cpp
@@ -84,6 +84,10 @@
         colorRef.attachment = currentAttachment++;
         colorRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
         subpassDesc.colorAttachmentCount = 1;
+
+        if (VK_ATTACHMENT_LOAD_OP_CLEAR == colorOp.fLoadOp) {
+            fClearValueCount++;
+        }
     } else {
         // I don't think there should ever be a time where we don't have a color attachment
         SkASSERT(false);
@@ -102,6 +106,9 @@
         // setup subpass use of attachment
         stencilRef.attachment = currentAttachment++;
         stencilRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
+        if (VK_ATTACHMENT_LOAD_OP_CLEAR == stencilOp.fLoadOp) {
+            fClearValueCount++;
+        }
     } else {
         stencilRef.attachment = VK_ATTACHMENT_UNUSED;
         stencilRef.layout = VK_IMAGE_LAYOUT_UNDEFINED;
@@ -147,7 +154,7 @@
 }
 
 void GrVkRenderPass::init(const GrVkGpu* gpu,
-                          const GrVkRenderTarget& target, 
+                          const GrVkRenderTarget& target,
                           const LoadStoreOps& colorOp,
                           const LoadStoreOps& stencilOp) {
     // Get attachment information from render target. This includes which attachments the render
@@ -183,29 +190,6 @@
     return false;
 }
 
-void GrVkRenderPass::getBeginInfo(const GrVkRenderTarget& target,
-                                  VkRenderPassBeginInfo* beginInfo,
-                                  VkSubpassContents* contents) const {
-    SkASSERT(this->isCompatible(target));
-
-    VkRect2D renderArea;
-    renderArea.offset = { 0, 0 };
-    renderArea.extent = { (uint32_t)target.width(), (uint32_t)target.height() };
-
-    memset(beginInfo, 0, sizeof(VkRenderPassBeginInfo));
-    beginInfo->sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
-    beginInfo->pNext = nullptr;
-    beginInfo->renderPass = fRenderPass;
-    beginInfo->framebuffer = target.framebuffer()->framebuffer();
-    beginInfo->renderArea = renderArea;
-    beginInfo->clearValueCount = 0;
-    beginInfo->pClearValues = nullptr;
-
-    // Currently just assuming no secondary cmd buffers. This value will need to be update if we
-    // have them.
-    *contents = VK_SUBPASS_CONTENTS_INLINE;
-}
-
 bool GrVkRenderPass::isCompatible(const AttachmentsDescriptor& desc,
                                   const AttachmentFlags& flags) const {
     if (flags != fAttachmentFlags) {
diff --git a/src/gpu/vk/GrVkRenderPass.h b/src/gpu/vk/GrVkRenderPass.h
index d59b5fa..b3b6092 100644
--- a/src/gpu/vk/GrVkRenderPass.h
+++ b/src/gpu/vk/GrVkRenderPass.h
@@ -20,7 +20,7 @@
 
 class GrVkRenderPass : public GrVkResource {
 public:
-    GrVkRenderPass() : INHERITED(), fRenderPass(VK_NULL_HANDLE) {}
+    GrVkRenderPass() : INHERITED(), fRenderPass(VK_NULL_HANDLE), fClearValueCount(0) {}
 
     struct LoadStoreOps {
         VkAttachmentLoadOp  fLoadOp;
@@ -89,16 +89,6 @@
     bool colorAttachmentIndex(uint32_t* index) const;
     bool stencilAttachmentIndex(uint32_t* index) const;
 
-    // Sets the VkRenderPassBeginInfo and VkRenderPassContents need to begin a render pass.
-    // TODO: In the future I expect this function will also take an optional render area instead of
-    // defaulting to the entire render target.
-    // TODO: Figure out if load clear values should be passed into this function or should be stored
-    // on the GrVkRenderPass at create time since we'll know at that point if we want to do a load
-    // clear.
-    void getBeginInfo(const GrVkRenderTarget& target,
-                      VkRenderPassBeginInfo* beginInfo,
-                      VkSubpassContents* contents) const;
-
     // Returns whether or not the structure of a RenderTarget matches that of the VkRenderPass in
     // this object. Specifically this compares that the number of attachments, format of
     // attachments, and sample counts are all the same. This function is used in the creation of
@@ -114,6 +104,11 @@
 
     const VkExtent2D& granularity() const { return fGranularity; }
 
+    // Returns the number of clear colors needed to begin this render pass. Currently this will
+    // either only be 0 or 1 since we only ever clear the color attachment.
+    uint32_t clearValueCount() const { return fClearValueCount; }
+
+
     void genKey(GrProcessorKeyBuilder* b) const;
 
 #ifdef SK_TRACE_VK_RESOURCES
@@ -137,6 +132,7 @@
     AttachmentFlags       fAttachmentFlags;
     AttachmentsDescriptor fAttachmentsDescriptor;
     VkExtent2D            fGranularity;
+    uint32_t              fClearValueCount;
 
     typedef GrVkResource INHERITED;
 };