Merge pull request #1391 from danginsburg/stencil_clear_fix

Fixes #1390 - when MVKRenderSubpass::populateClearAttachments determiā€¦
diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKRenderPass.h b/MoltenVK/MoltenVK/GPUObjects/MVKRenderPass.h
index c813fa6..18d2d24 100644
--- a/MoltenVK/MoltenVK/GPUObjects/MVKRenderPass.h
+++ b/MoltenVK/MoltenVK/GPUObjects/MVKRenderPass.h
@@ -201,6 +201,9 @@
     /** Returns whether this attachment should be cleared in the subpass. */
     bool shouldUseClearAttachment(MVKRenderSubpass* subpass);
 
+    /** If this is a depth attachment, the stencil load op may be different than the depth load op. */
+	VkAttachmentLoadOp getAttachmentStencilLoadOp() const;
+
 	/** Constructs an instance for the specified parent renderpass. */
 	MVKRenderPassAttachment(MVKRenderPass* renderPass,
 							const VkAttachmentDescription* pCreateInfo);
diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKRenderPass.mm b/MoltenVK/MoltenVK/GPUObjects/MVKRenderPass.mm
index 0443597..332efd1 100644
--- a/MoltenVK/MoltenVK/GPUObjects/MVKRenderPass.mm
+++ b/MoltenVK/MoltenVK/GPUObjects/MVKRenderPass.mm
@@ -425,7 +425,10 @@
 		MVKPixelFormats* pixFmts = _renderPass->getPixelFormats();
 		MTLPixelFormat mtlDSFmt = _renderPass->getPixelFormats()->getMTLPixelFormat(getDepthStencilFormat());
 		if (pixFmts->isDepthFormat(mtlDSFmt)) { cAtt.aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT; }
-		if (pixFmts->isStencilFormat(mtlDSFmt)) { cAtt.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT; }
+		if (pixFmts->isStencilFormat(mtlDSFmt) &&
+			_renderPass->_attachments[attIdx].getAttachmentStencilLoadOp() == VK_ATTACHMENT_LOAD_OP_CLEAR) {
+			cAtt.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
+		}
 		if (cAtt.aspectMask) { clearAtts.push_back(cAtt); }
 	}
 }
@@ -720,6 +723,10 @@
 	return (_info.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR);
 }
 
+VkAttachmentLoadOp MVKRenderPassAttachment::getAttachmentStencilLoadOp() const {
+	return _info.stencilLoadOp;
+}
+
 void MVKRenderPassAttachment::validateFormat() {
 	// Validate pixel format is supported
 	MVKPixelFormats* pixFmts = _renderPass->getPixelFormats();