Set render targets correctly when drawing

There are some horrible hacks in here -- the draw command should not be
doing anywhere near this much work -- but this gets us to first
triangle.

Passes: dEQP-VK.api.smoke.triangle

Bug: b/124177079
Change-Id: I4240cb8cdce2f4bbb804e88e66d1695ab0b0e41e
Reviewed-on: https://swiftshader-review.googlesource.com/c/25212
Tested-by: Chris Forbes <chrisforbes@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
diff --git a/src/Vulkan/VkCommandBuffer.cpp b/src/Vulkan/VkCommandBuffer.cpp
index f659a01..4578fe5 100644
--- a/src/Vulkan/VkCommandBuffer.cpp
+++ b/src/Vulkan/VkCommandBuffer.cpp
@@ -17,6 +17,7 @@
 #include "VkEvent.hpp"
 #include "VkFramebuffer.hpp"
 #include "VkImage.hpp"
+#include "VkImageView.hpp"
 #include "VkPipeline.hpp"
 #include "VkRenderPass.hpp"
 #include "Device/Renderer.hpp"
@@ -164,6 +165,16 @@
 		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->asSurface(), 0);
+			}
+		}
+
 		const uint32_t primitiveCount = pipeline->computePrimitiveCount(vertexCount);
 		const uint32_t lastInstance = firstInstance + instanceCount - 1;
 		for(uint32_t instance = firstInstance; instance <= lastInstance; instance++)
@@ -171,6 +182,22 @@
 			executionState.renderer->setInstanceID(instance);
 			executionState.renderer->draw(context.drawType, 0, primitiveCount);
 		}
+
+		// Wait for completion. We should be able to get rid of this eventually.
+		executionState.renderer->synchronize();
+
+		// Renderer has finished touching the color attachments; destroy the temporary Surface objects.
+		// We shouldn't need to do any of this at draw time.
+		for (auto i = 0u; i < executionState.renderPass->getCurrentSubpass().colorAttachmentCount; i++)
+		{
+			auto attachmentReference = executionState.renderPass->getCurrentSubpass().pColorAttachments[i];
+			if (attachmentReference.attachment != VK_ATTACHMENT_UNUSED)
+			{
+				auto surface = executionState.renderer->getRenderTarget(i);
+				executionState.renderer->setRenderTarget(i, nullptr, 0);
+				delete surface;
+			}
+		}
 	}
 
 	uint32_t vertexCount;