Skip to content

Instantly share code, notes, and snippets.

@mlfarrell
Last active November 15, 2023 00:18
Show Gist options
  • Save mlfarrell/3abef090625e68932ae5fb4ba62cdae9 to your computer and use it in GitHub Desktop.
Save mlfarrell/3abef090625e68932ae5fb4ba62cdae9 to your computer and use it in GitHub Desktop.
dynamic rendering method
void RenderingCommandList::beginRendering(RenderTarget *target, uint32_t imageIndex)
{
VkClearValue &clearColorValue = target->getClearColorValue(), &clearDepthValue = target->getClearDepthValue();
VkRenderingAttachmentInfo attachmentInfo = {}, depthAttachmentInfo = {};
attachmentInfo.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO;
if(!target->getImages().empty())
{
attachmentInfo.imageView = target->getImages()[imageIndex]->getImageView();
attachmentInfo.imageLayout = VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL_KHR;
attachmentInfo.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachmentInfo.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachmentInfo.clearValue = clearColorValue;
}
if(target->getDepthAttachment())
{
depthAttachmentInfo.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO;
depthAttachmentInfo.imageView = target->getDepthAttachment()->getImageView();
depthAttachmentInfo.imageLayout = VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL_KHR;
depthAttachmentInfo.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
depthAttachmentInfo.storeOp = (target->shouldStoreDepth()) ? VK_ATTACHMENT_STORE_OP_STORE : VK_ATTACHMENT_STORE_OP_DONT_CARE;
depthAttachmentInfo.clearValue = clearDepthValue;
}
VkExtent2D swapchainExtent = target->getExtent();
VkRenderingInfo info = {};
info.sType = VK_STRUCTURE_TYPE_RENDERING_INFO;
info.renderArea = { { 0, 0 }, { swapchainExtent.width, swapchainExtent.height } };
info.layerCount = 1;
if(!target->getImages().empty())
{
info.colorAttachmentCount = 1;
info.pColorAttachments = &attachmentInfo;
}
if(target->getDepthAttachment())
{
info.pDepthAttachment = &depthAttachmentInfo;
}
if(target->isSwapchain())
{
//... pipeline barrier
vkCmdPipelineBarrier(
commandBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0, nullptr, 0, nullptr, 1,
startRenderingBarrier);
}
else if(target->isOffscreen())
{
//.. pipeline barrier
}
vkCmdBeginRendering(commandBuffer, &info);
}
void RenderingCommandList::endRendering()
{
DebugAssert(currentRenderTarget);
vkCmdEndRendering(commandBuffer);
if(currentRenderTarget->isSwapchain())
{
//pipeline barrier..
}
else if(currentRenderTarget->isOffscreen())
{
//pipeline barrier..
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment