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/Device/PixelProcessor.cpp b/src/Device/PixelProcessor.cpp
index cc86873..1996bb6 100644
--- a/src/Device/PixelProcessor.cpp
+++ b/src/Device/PixelProcessor.cpp
@@ -97,6 +97,11 @@
context->renderTargetLayer[index] = layer;
}
+ Surface *PixelProcessor::getRenderTarget(int index)
+ {
+ return context->renderTarget[index];
+ }
+
void PixelProcessor::setDepthBuffer(Surface *depthBuffer, unsigned int layer)
{
context->depthBuffer = depthBuffer;
diff --git a/src/Device/PixelProcessor.hpp b/src/Device/PixelProcessor.hpp
index 536f324..e51fa18 100644
--- a/src/Device/PixelProcessor.hpp
+++ b/src/Device/PixelProcessor.hpp
@@ -153,6 +153,7 @@
void lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[]);
void setRenderTarget(int index, Surface *renderTarget, unsigned int layer = 0);
+ Surface *getRenderTarget(int index);
void setDepthBuffer(Surface *depthBuffer, unsigned int layer = 0);
void setStencilBuffer(Surface *stencilBuffer, unsigned int layer = 0);
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;
diff --git a/src/Vulkan/VkFramebuffer.cpp b/src/Vulkan/VkFramebuffer.cpp
index 1adadce..1d0f81d 100644
--- a/src/Vulkan/VkFramebuffer.cpp
+++ b/src/Vulkan/VkFramebuffer.cpp
@@ -94,6 +94,11 @@
}
}
+ImageView *Framebuffer::getAttachment(uint32_t index) const
+{
+ return attachments[index];
+}
+
size_t Framebuffer::ComputeRequiredAllocationSize(const VkFramebufferCreateInfo* pCreateInfo)
{
return pCreateInfo->attachmentCount * sizeof(void*);
diff --git a/src/Vulkan/VkFramebuffer.hpp b/src/Vulkan/VkFramebuffer.hpp
index 2e16e99..c615763 100644
--- a/src/Vulkan/VkFramebuffer.hpp
+++ b/src/Vulkan/VkFramebuffer.hpp
@@ -34,6 +34,7 @@
void clear(const VkClearAttachment& attachment, const VkClearRect& rect);
static size_t ComputeRequiredAllocationSize(const VkFramebufferCreateInfo* pCreateInfo);
+ ImageView *getAttachment(uint32_t index) const;
private:
RenderPass* renderPass;
diff --git a/src/Vulkan/VkImage.hpp b/src/Vulkan/VkImage.hpp
index eb8cc94..68ca016 100644
--- a/src/Vulkan/VkImage.hpp
+++ b/src/Vulkan/VkImage.hpp
@@ -48,6 +48,7 @@
void clear(const VkClearValue& clearValue, const VkRect2D& renderArea, const VkImageSubresourceRange& subresourceRange);
void clear(const VkClearColorValue& color, const VkImageSubresourceRange& subresourceRange);
void clear(const VkClearDepthStencilValue& color, const VkImageSubresourceRange& subresourceRange);
+ sw::Surface* asSurface(const VkImageAspectFlags& flags, uint32_t mipLevel, uint32_t layer) const;
VkImageType getImageType() const { return imageType; }
VkFormat getFormat() const { return format; }
@@ -74,7 +75,6 @@
VkFormat getClearFormat() const;
void clear(void* pixelData, VkFormat format, const VkImageSubresourceRange& subresourceRange, VkImageAspectFlags aspectMask);
void clear(void* pixelData, VkFormat format, const VkRect2D& renderArea, const VkImageSubresourceRange& subresourceRange, VkImageAspectFlags aspectMask);
- sw::Surface* asSurface(const VkImageAspectFlags& flags, uint32_t mipLevel, uint32_t layer) const;
DeviceMemory* deviceMemory = nullptr;
VkDeviceSize memoryOffset = 0;
diff --git a/src/Vulkan/VkImageView.cpp b/src/Vulkan/VkImageView.cpp
index 630ad49..c0ac795 100644
--- a/src/Vulkan/VkImageView.cpp
+++ b/src/Vulkan/VkImageView.cpp
@@ -119,4 +119,9 @@
image->clear(clearValue, renderArea.rect, sr);
}
+sw::Surface *ImageView::asSurface()
+{
+ return image->asSurface(subresourceRange.aspectMask, subresourceRange.baseArrayLayer, subresourceRange.baseMipLevel);
+}
+
}
\ No newline at end of file
diff --git a/src/Vulkan/VkImageView.hpp b/src/Vulkan/VkImageView.hpp
index 26be628..8eb3ee5 100644
--- a/src/Vulkan/VkImageView.hpp
+++ b/src/Vulkan/VkImageView.hpp
@@ -34,6 +34,8 @@
void clear(const VkClearValue& clearValues, const VkImageAspectFlags aspectMask, const VkRect2D& renderArea);
void clear(const VkClearValue& clearValue, const VkImageAspectFlags aspectMask, const VkClearRect& renderArea);
+ sw::Surface *asSurface();
+
private:
bool imageTypesMatch(VkImageType imageType) const;