Wire up depth and stencil attachments to pipeline

- Fix broken use of VkImageCreateFlags in various places as an aspect mask
- Be consistent about layout of D+S images. Layout is now [aspect][layer][level].
- Allow fetching an offset into a particular aspect.

Fixes dEQP-VK.pipeline.depth.*

Bug: b/118619338
Change-Id: I46adc9c637882e7144945eaeacce9f087d53caf0
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/26011
Tested-by: Chris Forbes <chrisforbes@google.com>
Presubmit-Ready: Chris Forbes <chrisforbes@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
diff --git a/src/Vulkan/VkCommandBuffer.cpp b/src/Vulkan/VkCommandBuffer.cpp
index e116de4..9634c10 100644
--- a/src/Vulkan/VkCommandBuffer.cpp
+++ b/src/Vulkan/VkCommandBuffer.cpp
@@ -159,6 +159,38 @@
 	const VkIndexType indexType;
 };
 
+void CommandBuffer::ExecutionState::bindAttachments()
+{
+	// Binds all the attachments for the current subpass
+	// Ideally this would be performed by BeginRenderPass and NextSubpass, but
+	// there is too much stomping of the renderer's state by setContext() in
+	// draws.
+
+	for (auto i = 0u; i < renderPass->getCurrentSubpass().colorAttachmentCount; i++)
+	{
+		auto attachmentReference = renderPass->getCurrentSubpass().pColorAttachments[i];
+		if (attachmentReference.attachment != VK_ATTACHMENT_UNUSED)
+		{
+			auto attachment = renderPassFramebuffer->getAttachment(attachmentReference.attachment);
+			renderer->setRenderTarget(i, attachment, 0);
+		}
+	}
+
+	auto attachmentReference = renderPass->getCurrentSubpass().pDepthStencilAttachment;
+	if (attachmentReference && attachmentReference->attachment != VK_ATTACHMENT_UNUSED)
+	{
+		auto attachment = renderPassFramebuffer->getAttachment(attachmentReference->attachment);
+		if (attachment->hasDepthAspect())
+		{
+			renderer->setDepthBuffer(attachment, 0);
+		}
+		if (attachment->hasStencilAspect())
+		{
+			renderer->setStencilBuffer(attachment, 0);
+		}
+	}
+}
+
 struct Draw : public CommandBuffer::Command
 {
 	Draw(uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance)
@@ -189,15 +221,7 @@
 		executionState.renderer->setViewport(pipeline->getViewport());
 		executionState.renderer->setBlendConstant(pipeline->getBlendConstants());
 
-		for (auto i = 0u; i < executionState.renderPass->getCurrentSubpass().colorAttachmentCount; i++)
-		{
-			auto attachmentReference = executionState.renderPass->getCurrentSubpass().pColorAttachments[i];
-			if (attachmentReference.attachment != VK_ATTACHMENT_UNUSED)
-			{
-				auto attachment = executionState.renderPassFramebuffer->getAttachment(attachmentReference.attachment);
-				executionState.renderer->setRenderTarget(i, attachment, 0);
-			}
-		}
+		executionState.bindAttachments();
 
 		const uint32_t primitiveCount = pipeline->computePrimitiveCount(vertexCount);
 		const uint32_t lastInstance = firstInstance + instanceCount - 1;
@@ -247,15 +271,7 @@
 		executionState.renderer->setViewport(pipeline->getViewport());
 		executionState.renderer->setBlendConstant(pipeline->getBlendConstants());
 
-		for (auto i = 0u; i < executionState.renderPass->getCurrentSubpass().colorAttachmentCount; i++)
-		{
-			auto attachmentReference = executionState.renderPass->getCurrentSubpass().pColorAttachments[i];
-			if (attachmentReference.attachment != VK_ATTACHMENT_UNUSED)
-			{
-				auto attachment = executionState.renderPassFramebuffer->getAttachment(attachmentReference.attachment);
-				executionState.renderer->setRenderTarget(i, attachment, 0);
-			}
-		}
+		executionState.bindAttachments();
 
 		auto drawType = executionState.indexType == VK_INDEX_TYPE_UINT16
 				? (context.drawType | sw::DRAW_INDEXED16) : (context.drawType | sw::DRAW_INDEXED32);