@@ -835,7 +835,8 @@ struct VulkanPixelHistoryCallback : public VulkanActionCallback
835835 // When dynamic rendering is in use, it doesn't create a new renderpass it just modifies the
836836 // passed in state and returns a NULL handle.
837837 VkRenderPass PatchRenderPass (VulkanRenderState &pipestate, bool &multiview,
838- VkFormat newColorFormat = VK_FORMAT_UNDEFINED, uint32_t colorIdx = 0 )
838+ VkFormat newColorFormat = VK_FORMAT_UNDEFINED, uint32_t attIdx = 0 ,
839+ uint32_t colorIdx = 0 )
839840 {
840841 if (pipestate.dynamicRendering .active )
841842 {
@@ -1007,21 +1008,27 @@ struct VulkanPixelHistoryCallback : public VulkanActionCallback
10071008 // If needed substitute the color attachment with the new format.
10081009 if (newColorFormat != VK_FORMAT_UNDEFINED)
10091010 {
1010- if (colorIdx < descs.size ())
1011+ if (attIdx < descs.size ())
10111012 {
10121013 // It is an existing attachment.
1013- descs[colorIdx ].format = newColorFormat;
1014+ descs[attIdx ].format = newColorFormat;
10141015 }
10151016 else
10161017 {
10171018 // We are adding a new color attachment.
10181019 VkAttachmentReference attRef = {};
10191020 attRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
1020- attRef.attachment = colorIdx;
1021- colorAttachments.push_back (attRef);
1021+ attRef.attachment = attIdx;
1022+ if (colorIdx < colorAttachments.size ())
1023+ colorAttachments[colorIdx] = attRef;
1024+ else
1025+ colorAttachments.push_back (attRef);
10221026 subpassDesc.colorAttachmentCount = (uint32_t )colorAttachments.size ();
10231027 subpassDesc.pColorAttachments = colorAttachments.data ();
10241028
1029+ RDCASSERT (subpassDesc.colorAttachmentCount <= 8 );
1030+
1031+ RDCASSERT (descs.size () == attIdx);
10251032 VkAttachmentDescription attDesc = {};
10261033 attDesc.format = newColorFormat;
10271034 attDesc.samples = m_CallbackInfo.samples ;
@@ -1082,7 +1089,8 @@ struct VulkanPixelHistoryCallback : public VulkanActionCallback
10821089 // passed in state and returns a NULL handle.
10831090 VkFramebuffer PatchFramebuffer (VulkanRenderState &pipestate, VkRenderPass newRp,
10841091 VkImageView newColorAtt = VK_NULL_HANDLE,
1085- VkFormat newColorFormat = VK_FORMAT_UNDEFINED, uint32_t colorIdx = 0 )
1092+ VkFormat newColorFormat = VK_FORMAT_UNDEFINED,
1093+ uint32_t attachIdx = 0 , uint32_t colorIdx = 0 )
10861094 {
10871095 if (pipestate.dynamicRendering .active )
10881096 {
@@ -1124,8 +1132,8 @@ struct VulkanPixelHistoryCallback : public VulkanActionCallback
11241132 // Either modify the existing color attachment view, or add a new one.
11251133 if (newColorAtt != VK_NULL_HANDLE)
11261134 {
1127- if (colorIdx < atts.size ())
1128- atts[colorIdx ] = newColorAtt;
1135+ if (attachIdx < atts.size ())
1136+ atts[attachIdx ] = newColorAtt;
11291137 else
11301138 atts.push_back (newColorAtt);
11311139 }
@@ -2634,14 +2642,18 @@ struct VulkanPixelHistoryPerFragmentCallback : VulkanPixelHistoryCallback
26342642 // Going to add another color attachment.
26352643 framebufferIndex = (uint32_t )prevState.GetFramebufferAttachments ().size ();
26362644 colorOutputIndex = (uint32_t )sub.colorAttachments .size ();
2645+
2646+ while (colorOutputIndex > 0 &&
2647+ sub.colorAttachments [colorOutputIndex - 1 ] == VK_ATTACHMENT_UNUSED)
2648+ colorOutputIndex--;
26372649 }
26382650 }
26392651
26402652 bool multiview = false ;
2641- VkRenderPass newRp =
2642- PatchRenderPass (state, multiview, VK_FORMAT_R32G32B32A32_SFLOAT, framebufferIndex);
2653+ VkRenderPass newRp = PatchRenderPass (state, multiview, VK_FORMAT_R32G32B32A32_SFLOAT,
2654+ framebufferIndex, colorOutputIndex );
26432655 PatchFramebuffer (state, newRp, m_CallbackInfo.subImageView , VK_FORMAT_R32G32B32A32_SFLOAT,
2644- framebufferIndex);
2656+ framebufferIndex, colorOutputIndex );
26452657
26462658 Pipelines pipes = CreatePerFragmentPipelines (curPipeline, newRp, eid, 0 ,
26472659 VK_FORMAT_R32G32B32A32_SFLOAT, colorOutputIndex);
0 commit comments