Skip to content

Instantly share code, notes, and snippets.

@iwrestledabeerone
Created February 22, 2022 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwrestledabeerone/90f517fd963ec492ef40d27499f9a4e7 to your computer and use it in GitHub Desktop.
Save iwrestledabeerone/90f517fd963ec492ef40d27499f9a4e7 to your computer and use it in GitHub Desktop.
static const uint32_t resolvePipelineVertShaderCode[] =
{
/*
#version 450
void main()
{
// 0 -> -1, -1
// 1 -> 3, -1
// 2 -> -1, 3
gl_Position = vec4( 4 * ( gl_VertexIndex & 1 ) - 1, 2 * ( gl_VertexIndex & 2 ) - 1, 0, 1 );
}
*/
0x07230203, 0x00010000, 0x000d000a, 0x00000024, 0x00000000, 0x00020011, 0x00000001, 0x0006000b,
0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, 0x00000000, 0x00000001,
0x0007000f, 0x00000000, 0x00000004, 0x6e69616d, 0x00000000, 0x0000000d, 0x00000012, 0x00050048,
0x0000000b, 0x00000000, 0x0000000b, 0x00000000, 0x00050048, 0x0000000b, 0x00000001, 0x0000000b,
0x00000001, 0x00050048, 0x0000000b, 0x00000002, 0x0000000b, 0x00000003, 0x00050048, 0x0000000b,
0x00000003, 0x0000000b, 0x00000004, 0x00030047, 0x0000000b, 0x00000002, 0x00040047, 0x00000012,
0x0000000b, 0x0000002a, 0x00020013, 0x00000002, 0x00030021, 0x00000003, 0x00000002, 0x00030016,
0x00000006, 0x00000020, 0x00040017, 0x00000007, 0x00000006, 0x00000004, 0x00040015, 0x00000008,
0x00000020, 0x00000000, 0x0004002b, 0x00000008, 0x00000009, 0x00000001, 0x0004001c, 0x0000000a,
0x00000006, 0x00000009, 0x0006001e, 0x0000000b, 0x00000007, 0x00000006, 0x0000000a, 0x0000000a,
0x00040020, 0x0000000c, 0x00000003, 0x0000000b, 0x0004003b, 0x0000000c, 0x0000000d, 0x00000003,
0x00040015, 0x0000000e, 0x00000020, 0x00000001, 0x0004002b, 0x0000000e, 0x0000000f, 0x00000000,
0x0004002b, 0x0000000e, 0x00000010, 0x00000004, 0x00040020, 0x00000011, 0x00000001, 0x0000000e,
0x0004003b, 0x00000011, 0x00000012, 0x00000001, 0x0004002b, 0x0000000e, 0x00000014, 0x00000001,
0x0004002b, 0x0000000e, 0x00000019, 0x00000002, 0x0004002b, 0x00000006, 0x0000001f, 0x00000000,
0x0004002b, 0x00000006, 0x00000020, 0x3f800000, 0x00040020, 0x00000022, 0x00000003, 0x00000007,
0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x0004003d,
0x0000000e, 0x00000013, 0x00000012, 0x000500c7, 0x0000000e, 0x00000015, 0x00000013, 0x00000014,
0x00050084, 0x0000000e, 0x00000016, 0x00000010, 0x00000015, 0x00050082, 0x0000000e, 0x00000017,
0x00000016, 0x00000014, 0x0004006f, 0x00000006, 0x00000018, 0x00000017, 0x000500c7, 0x0000000e,
0x0000001b, 0x00000013, 0x00000019, 0x00050084, 0x0000000e, 0x0000001c, 0x00000019, 0x0000001b,
0x00050082, 0x0000000e, 0x0000001d, 0x0000001c, 0x00000014, 0x0004006f, 0x00000006, 0x0000001e,
0x0000001d, 0x00070050, 0x00000007, 0x00000021, 0x00000018, 0x0000001e, 0x0000001f, 0x00000020,
0x00050041, 0x00000022, 0x00000023, 0x0000000d, 0x0000000f, 0x0003003e, 0x00000023, 0x00000021,
0x000100fd, 0x00010038,
};
static const uint32_t resolvePipelineFragShaderCode[] =
{
/*
#version 450
#define RESOLVE_MODE_FIRST_SAMPLE 0
#define RESOLVE_MODE_MIN 1
#define RESOLVE_MODE_MAX 2
layout ( input_attachment_index = 0, binding = 0 ) uniform subpassInputMS msDepth;
layout ( constant_id = 0 ) const int SAMPLE_COUNT = 1;
layout ( constant_id = 1 ) const int RESOLVE_MODE = RESOLVE_MODE_FIRST_SAMPLE;
void main()
{
float result = 0;
if( RESOLVE_MODE == RESOLVE_MODE_FIRST_SAMPLE )
{
result = subpassLoad( msDepth, 0 ).r;
}
else if( RESOLVE_MODE == RESOLVE_MODE_MIN )
{
result = 1;
for( int i = 0; i < SAMPLE_COUNT; ++i )
{
result = min( result, subpassLoad( msDepth, i ).r );
}
}
else if( RESOLVE_MODE == RESOLVE_MODE_MAX )
{
result = 0;
for( int i = 0; i < SAMPLE_COUNT; ++i )
{
result = max( result, subpassLoad( msDepth, i ).r );
}
}
gl_FragDepth = result;
}
*/
0x07230203, 0x00010000, 0x000d000a, 0x00000053, 0x00000000, 0x00020011, 0x00000001, 0x00020011,
0x00000028, 0x0006000b, 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e,
0x00000000, 0x00000001, 0x0006000f, 0x00000004, 0x00000004, 0x6e69616d, 0x00000000, 0x0000004a,
0x00030010, 0x00000004, 0x00000007, 0x00030010, 0x00000004, 0x0000000c, 0x00040047, 0x0000000b,
0x00000001, 0x00000001, 0x00040047, 0x00000013, 0x00000022, 0x00000000, 0x00040047, 0x00000013,
0x00000021, 0x00000000, 0x00040047, 0x00000013, 0x0000002b, 0x00000000, 0x00040047, 0x0000002a,
0x00000001, 0x00000000, 0x00040047, 0x0000004a, 0x0000000b, 0x00000016, 0x00020013, 0x00000002,
0x00030021, 0x00000003, 0x00000002, 0x00030016, 0x00000006, 0x00000020, 0x0004002b, 0x00000006,
0x00000009, 0x00000000, 0x00040015, 0x0000000a, 0x00000020, 0x00000001, 0x00040032, 0x0000000a,
0x0000000b, 0x00000000, 0x0004002b, 0x0000000a, 0x0000000c, 0x00000000, 0x00020014, 0x0000000d,
0x00060034, 0x0000000d, 0x0000000e, 0x000000aa, 0x0000000b, 0x0000000c, 0x00090019, 0x00000011,
0x00000006, 0x00000006, 0x00000000, 0x00000000, 0x00000001, 0x00000002, 0x00000000, 0x00040020,
0x00000012, 0x00000000, 0x00000011, 0x0004003b, 0x00000012, 0x00000013, 0x00000000, 0x00040017,
0x00000015, 0x0000000a, 0x00000002, 0x0005002c, 0x00000015, 0x00000016, 0x0000000c, 0x0000000c,
0x00040017, 0x00000017, 0x00000006, 0x00000004, 0x0004002b, 0x0000000a, 0x0000001d, 0x00000001,
0x00060034, 0x0000000d, 0x0000001e, 0x000000aa, 0x0000000b, 0x0000001d, 0x0004002b, 0x00000006,
0x00000021, 0x3f800000, 0x00040032, 0x0000000a, 0x0000002a, 0x00000001, 0x0004002b, 0x0000000a,
0x00000035, 0x00000002, 0x00060034, 0x0000000d, 0x00000036, 0x000000aa, 0x0000000b, 0x00000035,
0x00040020, 0x00000049, 0x00000003, 0x00000006, 0x0004003b, 0x00000049, 0x0000004a, 0x00000003,
0x00050036, 0x00000002, 0x00000004, 0x00000000, 0x00000003, 0x000200f8, 0x00000005, 0x000300f7,
0x00000010, 0x00000000, 0x000400fa, 0x0000000e, 0x0000000f, 0x0000001c, 0x000200f8, 0x0000000f,
0x0004003d, 0x00000011, 0x00000014, 0x00000013, 0x00070062, 0x00000017, 0x00000018, 0x00000014,
0x00000016, 0x00000040, 0x0000000c, 0x00050051, 0x00000006, 0x0000001b, 0x00000018, 0x00000000,
0x000200f9, 0x00000010, 0x000200f8, 0x0000001c, 0x000300f7, 0x00000020, 0x00000000, 0x000400fa,
0x0000001e, 0x0000001f, 0x00000034, 0x000200f8, 0x0000001f, 0x000200f9, 0x00000024, 0x000200f8,
0x00000024, 0x000700f5, 0x00000006, 0x0000004f, 0x00000021, 0x0000001f, 0x00000031, 0x00000025,
0x000700f5, 0x0000000a, 0x0000004e, 0x0000000c, 0x0000001f, 0x00000033, 0x00000025, 0x000500b1,
0x0000000d, 0x0000002b, 0x0000004e, 0x0000002a, 0x000400f6, 0x00000026, 0x00000025, 0x00000000,
0x000400fa, 0x0000002b, 0x00000025, 0x00000026, 0x000200f8, 0x00000025, 0x0004003d, 0x00000011,
0x0000002d, 0x00000013, 0x00070062, 0x00000017, 0x0000002f, 0x0000002d, 0x00000016, 0x00000040,
0x0000004e, 0x00050051, 0x00000006, 0x00000030, 0x0000002f, 0x00000000, 0x0007000c, 0x00000006,
0x00000031, 0x00000001, 0x00000025, 0x0000004f, 0x00000030, 0x00050080, 0x0000000a, 0x00000033,
0x0000004e, 0x0000001d, 0x000200f9, 0x00000024, 0x000200f8, 0x00000026, 0x000200f9, 0x00000020,
0x000200f8, 0x00000034, 0x000300f7, 0x00000038, 0x00000000, 0x000400fa, 0x00000036, 0x00000037,
0x00000038, 0x000200f8, 0x00000037, 0x000200f9, 0x0000003a, 0x000200f8, 0x0000003a, 0x000700f5,
0x00000006, 0x0000004d, 0x00000009, 0x00000037, 0x00000046, 0x0000003b, 0x000700f5, 0x0000000a,
0x0000004c, 0x0000000c, 0x00000037, 0x00000048, 0x0000003b, 0x000500b1, 0x0000000d, 0x00000040,
0x0000004c, 0x0000002a, 0x000400f6, 0x0000003c, 0x0000003b, 0x00000000, 0x000400fa, 0x00000040,
0x0000003b, 0x0000003c, 0x000200f8, 0x0000003b, 0x0004003d, 0x00000011, 0x00000042, 0x00000013,
0x00070062, 0x00000017, 0x00000044, 0x00000042, 0x00000016, 0x00000040, 0x0000004c, 0x00050051,
0x00000006, 0x00000045, 0x00000044, 0x00000000, 0x0007000c, 0x00000006, 0x00000046, 0x00000001,
0x00000028, 0x0000004d, 0x00000045, 0x00050080, 0x0000000a, 0x00000048, 0x0000004c, 0x0000001d,
0x000200f9, 0x0000003a, 0x000200f8, 0x0000003c, 0x000200f9, 0x00000038, 0x000200f8, 0x00000038,
0x000700f5, 0x00000006, 0x00000052, 0x00000009, 0x00000034, 0x0000004d, 0x0000003c, 0x000200f9,
0x00000020, 0x000200f8, 0x00000020, 0x000700f5, 0x00000006, 0x00000051, 0x0000004f, 0x00000026,
0x00000052, 0x00000038, 0x000200f9, 0x00000010, 0x000200f8, 0x00000010, 0x000700f5, 0x00000006,
0x00000050, 0x0000001b, 0x0000000f, 0x00000051, 0x00000020, 0x0003003e, 0x0000004a, 0x00000050,
0x000100fd, 0x00010038,
};
void mainPassDraw(VkDevice device, VkCommandBuffer cmdBuffer, VkImageView *attachmentViews, uint32_t fbWidth, uint32_t fbHeight, int32_t resolveMode)
{
//
// Render pass creation
//
VkAttachmentDescription attachments[8] = {};
// Multisampled color
attachments[0].format = VK_FORMAT_B10G11R11_UFLOAT_PACK32;
attachments[0].samples = VK_SAMPLE_COUNT_4_BIT;
attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[0].initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
attachments[0].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
// Multisampled normals
attachments[1].format = VK_FORMAT_R8G8_UNORM;
attachments[1].samples = VK_SAMPLE_COUNT_4_BIT;
attachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[1].initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
attachments[1].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
// Multisampled object mark
attachments[2].format = VK_FORMAT_R8_UNORM;
attachments[2].samples = VK_SAMPLE_COUNT_4_BIT;
attachments[2].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[2].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[2].initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
attachments[2].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
// Multisampled depth
attachments[3].format = VK_FORMAT_D32_SFLOAT;
attachments[3].samples = VK_SAMPLE_COUNT_4_BIT;
attachments[3].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[3].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[3].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
attachments[3].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
// Resolved color
attachments[4].format = VK_FORMAT_B10G11R11_UFLOAT_PACK32;
attachments[4].samples = VK_SAMPLE_COUNT_1_BIT;
attachments[4].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[4].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachments[4].initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
attachments[4].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
// Resolved normals
attachments[5].format = VK_FORMAT_R8G8_UNORM;
attachments[5].samples = VK_SAMPLE_COUNT_1_BIT;
attachments[5].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[5].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachments[5].initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
attachments[5].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
// Resolved object mark
attachments[6].format = VK_FORMAT_R8_UNORM;
attachments[6].samples = VK_SAMPLE_COUNT_1_BIT;
attachments[6].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[6].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachments[6].initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
attachments[6].finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
// Resolved depth
attachments[7].format = VK_FORMAT_D32_SFLOAT;
attachments[7].samples = VK_SAMPLE_COUNT_1_BIT;
attachments[7].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[7].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachments[7].initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
attachments[7].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
VkAttachmentReference attachmentRefs[9] =
{
{0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL}, // Multisampled color
{1, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL}, // Multisampled normals
{2, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL}, // Multisampled object mark
{3, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL}, // Multisampled depth
{4, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL}, // Resolved color
{5, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL}, // Resolved normals
{6, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL}, // Resolved object mark
{7, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL}, // Resolved depth
{3, VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL}, // Multisampled depth input
};
VkClearValue clearValues[4] = {};
clearValues[3].depthStencil.depth = 1.0f;
VkSubpassDescription subpasses[2] = {};
subpasses[0].pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpasses[0].colorAttachmentCount = 3;
subpasses[0].pColorAttachments = attachmentRefs + 0;
subpasses[0].pDepthStencilAttachment = attachmentRefs + 3;
subpasses[0].pResolveAttachments = attachmentRefs + 4;
subpasses[1].pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpasses[1].inputAttachmentCount = 1;
subpasses[1].pInputAttachments = attachmentRefs + 8;
subpasses[1].pDepthStencilAttachment = attachmentRefs + 7;
VkSubpassDependency dependency = {};
dependency.srcSubpass = 0;
dependency.dstSubpass = 1;
dependency.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
dependency.srcStageMask = VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT;
dependency.srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
dependency.dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
dependency.dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
VkRenderPassCreateInfo renderPassInfo = {};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassInfo.attachmentCount = 8;
renderPassInfo.pAttachments = attachments;
renderPassInfo.subpassCount = 2;
renderPassInfo.pSubpasses = subpasses;
renderPassInfo.dependencyCount = 1;
renderPassInfo.pDependencies = &dependency;
VkRenderPass mainPass = nullptr;
vkCreateRenderPass( device, &renderPassInfo, nullptr, &mainPass );
//
// Framebuffer creation
//
VkFramebufferCreateInfo framebufferInfo = { VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO };
framebufferInfo.renderPass = mainPass;
framebufferInfo.attachmentCount = 8;
framebufferInfo.pAttachments = attachmentViews;
framebufferInfo.width = fbWidth;
framebufferInfo.height = fbHeight;
framebufferInfo.layers = 1;
VkFramebuffer framebuffer = nullptr;
vkCreateFramebuffer( device, &framebufferInfo, nullptr, &framebuffer );
//
// Resolve pipeline creation
//
int32_t resolveParams[2] =
{
4, // sample count
resolveMode,
};
VkPipelineShaderStageCreateInfo stages[2] = {};
stages[0].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
stages[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
stages[0].pName = "main";
VkShaderModuleCreateInfo vertShaderCreateInfo = { VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO };
vertShaderCreateInfo.codeSize = sizeof( resolvePipelineVertShaderCode );
vertShaderCreateInfo.pCode = resolvePipelineVertShaderCode;
vkCreateShaderModule( device, &vertShaderCreateInfo, nullptr, &stages[0].module );
VkSpecializationMapEntry specializationMap[2] = {};
specializationMap[0].constantID = 0;
specializationMap[0].offset = 0;
specializationMap[0].size = sizeof( int32_t );
specializationMap[1].constantID = 1;
specializationMap[1].offset = sizeof( int32_t );
specializationMap[1].size = sizeof( int32_t );
VkSpecializationInfo specializationInfo = {};
specializationInfo.mapEntryCount = 2;
specializationInfo.pMapEntries = specializationMap;
specializationInfo.dataSize = sizeof( resolveParams );
specializationInfo.pData = resolveParams;
stages[1].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
stages[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
stages[1].pName = "main";
stages[1].pSpecializationInfo = &specializationInfo;
VkShaderModuleCreateInfo fragShaderCreateInfo = { VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO };
fragShaderCreateInfo.codeSize = sizeof( resolvePipelineFragShaderCode );
fragShaderCreateInfo.pCode = resolvePipelineVertShaderCode;
vkCreateShaderModule( device, &fragShaderCreateInfo, nullptr, &stages[1].module );
VkPipelineVertexInputStateCreateInfo vertexInputState = { VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO };
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = { VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO };
inputAssemblyState.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
VkPipelineViewportStateCreateInfo viewportState = { VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO };
viewportState.viewportCount = 1;
viewportState.scissorCount = 1;
VkPipelineRasterizationStateCreateInfo rasterizationState = { VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO };
rasterizationState.polygonMode = VK_POLYGON_MODE_FILL;
VkPipelineMultisampleStateCreateInfo multisampleState = { VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO };
multisampleState.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineDepthStencilStateCreateInfo depthStencilState = { VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO };
depthStencilState.depthTestEnable = true;
depthStencilState.depthWriteEnable = true;
depthStencilState.depthCompareOp = VK_COMPARE_OP_ALWAYS;
VkDynamicState dynamicStates[2] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
VkPipelineDynamicStateCreateInfo dynamicState = { VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO };
dynamicState.dynamicStateCount = 2;
dynamicState.pDynamicStates = dynamicStates;
VkDescriptorSetLayoutBinding layoutBinding = {};
layoutBinding.binding = 0;
layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT;
layoutBinding.descriptorCount = 1;
layoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
VkDescriptorSetLayoutCreateInfo layoutInfo = { VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO };
layoutInfo.bindingCount = 1;
layoutInfo.pBindings = &layoutBinding;
VkDescriptorSetLayout descriptorSetLayout = nullptr;
vkCreateDescriptorSetLayout( device, &layoutInfo, nullptr, &descriptorSetLayout );
VkPipelineLayoutCreateInfo pipelineLayoutInfo = { VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO };
pipelineLayoutInfo.setLayoutCount = 1;
pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout;
VkPipelineLayout pipelineLayout = nullptr;
vkCreatePipelineLayout( device, &pipelineLayoutInfo, nullptr, &pipelineLayout );
VkGraphicsPipelineCreateInfo pipelineCreateInfo = { VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO };
pipelineCreateInfo.stageCount = 2;
pipelineCreateInfo.pStages = stages;
pipelineCreateInfo.pVertexInputState = &vertexInputState;
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
pipelineCreateInfo.pViewportState = &viewportState;
pipelineCreateInfo.pRasterizationState = &rasterizationState;
pipelineCreateInfo.pMultisampleState = &multisampleState;
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
pipelineCreateInfo.pDynamicState = &dynamicState;
pipelineCreateInfo.layout = pipelineLayout;
pipelineCreateInfo.renderPass = mainPass;
pipelineCreateInfo.subpass = 1;
VkPipeline resolvePipeline = nullptr;
vkCreateGraphicsPipelines( device, nullptr, 1, &pipelineCreateInfo, nullptr, &resolvePipeline );
//
// Resolve descriptor set creation / update
//
VkDescriptorPoolSize size = {};
size.type = VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT;
size.descriptorCount = 1;
VkDescriptorPoolCreateInfo poolInfo = { VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO };
poolInfo.maxSets = 1;
poolInfo.poolSizeCount = 1;
poolInfo.pPoolSizes = &size;
VkDescriptorPool descriptorPool = nullptr;
vkCreateDescriptorPool( device, &poolInfo, nullptr, &descriptorPool );
VkDescriptorSetAllocateInfo allocInfo = { VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO };
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = 1;
allocInfo.pSetLayouts = &descriptorSetLayout;
VkDescriptorSet descriptorSet = nullptr;
vkAllocateDescriptorSets( device, &allocInfo, &descriptorSet );
VkDescriptorImageInfo imageInfo = {};
imageInfo.sampler = nullptr;
imageInfo.imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
imageInfo.imageView = attachmentViews[3];
VkWriteDescriptorSet descriptorWrite = { VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET };
descriptorWrite.dstSet = descriptorSet;
descriptorWrite.dstBinding = 0;
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT;
descriptorWrite.dstArrayElement = 0;
descriptorWrite.descriptorCount = 1;
descriptorWrite.pImageInfo = &imageInfo;
vkUpdateDescriptorSets( device, 1, &descriptorWrite, 0, nullptr );
//
// Subpass 0: A bunch of draws to multisampled targets. Only opaque geometry, many graphics pipelines, z test and z write are always enabled
//
VkRenderPassBeginInfo renderPassBegin = { VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO };
renderPassBegin.renderPass = mainPass;
renderPassBegin.framebuffer = framebuffer;
renderPassBegin.renderArea.offset.x = 0;
renderPassBegin.renderArea.offset.y = 0;
renderPassBegin.renderArea.extent.width = fbWidth;
renderPassBegin.renderArea.extent.height = fbHeight;
renderPassBegin.clearValueCount = 4;
renderPassBegin.pClearValues = clearValues;
vkCmdBeginRenderPass( cmdBuffer, &renderPassBegin, VK_SUBPASS_CONTENTS_INLINE );
VkViewport viewport = { 0, 0, ( float )fbWidth, ( float )fbHeight, 0, 1 };
VkRect2D scissorRect = { 0, 0, fbWidth, fbHeight };
vkCmdSetViewport( cmdBuffer, 0, 1, &viewport );
vkCmdSetScissor( cmdBuffer, 0, 1, &scissorRect );
//
// Subpass 1: multisampled depth resolve
//
vkCmdNextSubpass( cmdBuffer, VK_SUBPASS_CONTENTS_INLINE );
vkCmdBindPipeline( cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, resolvePipeline );
vkCmdBindDescriptorSets( cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr );
vkCmdDraw( cmdBuffer, 3, 1, 0, 0 );
vkCmdEndRenderPass( cmdBuffer );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment