前言
使用 SPIR-V 专业化常量从单个“超级”着色器创建具有不同照明路径的多个管道。
一、CPP文件
1.类以及变量
给shader使用同一个uniform buffer
class VulkanExample: public VulkanExampleBase
{
public:
vkglTF::Model scene;
vks::Texture2D colormap;
vks::Buffer uniformBuffer;
// Same uniform buffer layout as shader
struct UBOVS {
glm::mat4 projection;
glm::mat4 modelView;
glm::vec4 lightPos = glm::vec4(0.0f, -2.0f, 1.0f, 0.0f);
} uboVS;
VkPipelineLayout pipelineLayout;
VkDescriptorSet descriptorSet;
VkDescriptorSetLayout descriptorSetLayout;
struct {
VkPipeline phong;
VkPipeline toon;
VkPipeline textured;
} pipelines;
2.函数
buildCommandBuffers
void buildCommandBuffers()
{
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
VkClearValue clearValues[2];
clearValues[0].color = defaultClearColor;
clearValues[1].depthStencil = { 1.0f, 0 };
VkRenderPassBeginInfo renderPassBeginInfo = vks::initializers::renderPassBeginInfo();
renderPassBeginInfo.renderPass = renderPass;
renderPassBeginInfo.renderArea.offset.x = 0;
renderPassBeginInfo.renderArea.offset.y = 0;
renderPassBeginInfo.renderArea.extent.width = width;
renderPassBeginInfo.renderArea.extent.height = height;
renderPassBeginInfo.clearValueCount = 2;
renderPassBeginInfo.pClearValues = clearValues;
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
{
// Set target frame buffer
renderPassBeginInfo.framebuffer = frameBuffers[i];
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0);
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
VkDeviceSize offsets[1] = { 0 };
// Left
VkViewport viewport = vks::initializers::viewport((float) width / 3.0f, (float) height, 0.0f, 1.0f);
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.phong);
scene.draw(drawCmdBuffers[i]);
// Center
viewport.x = (float)width / 3.0f;
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.toon);
scene.draw(drawCmdBuffers[i]);
// Right
viewport.x = (float)width / 3.0f + (float)width / 3.0f;
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.textured);
scene.draw(drawCmdBuffers[i]);
drawUI(drawCmdBuffers[i]);
vkCmdEndRenderPass(drawCmdBuffers[i]);
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
}
}
preparePipelines:使用specializationData 专业化数据。着色器阶段的每个着色器常量对应于一个地图条目。基于特化常量的着色器绑定由新的“constant_id”布局限定符标记。分配的专用化信息是着色器阶段(modul)的一部分,必须在创建模块后和创建管道之前进行设置。最后给每一个shader都创建渲染管线。
void preparePipelines()
{
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE, 0);
VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
VkPipelineColorBlendStateCreateInfo colorBlendState = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
VkPipelineDepthStencilStateCreateInfo depthStencilState = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);
VkPipelineViewportStateCreateInfo viewportState = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
VkPipelineMultisampleStateCreateInfo multisampleState = vks::initializers::pipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT, 0);
std::vector<VkDynamicState> dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR, VK_DYNAMIC_STATE_LINE_WIDTH };
VkPipelineDynamicStateCreateInfo dynamicState = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables);
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
VkGraphicsPipelineCreateInfo pipelineCI = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass, 0);
pipelineCI.pInputAssemblyState = &inputAssemblyState;
pipelineCI.pRasterizationState = &rasterizationState;
pipelineCI.pColorBlendState = &colorBlendState;
pipelineCI.pMultisampleState = &multisampleState;
pipelineCI.pViewportState = &viewportState;
pipelineCI.pDepthStencilState = &depthStencilState;
pipelineCI.pDynamicState = &dynamicState;
pipelineCI.stageCount = static_cast<uint32_t>(shaderStages.size());
pipelineCI.pStages = shaderStages.data();
pipelineCI.pVertexInputState = vkglTF::Vertex::getPipelineVertexInputState({ vkglTF::VertexComponent::Position, vkglTF::VertexComponent::Normal, vkglTF::VertexComponent::UV, vkglTF::VertexComponent::Color });
// Prepare specialization data
// Host data to take specialization constants from
struct SpecializationData {
// Sets the lighting model used in the fragment "uber" shader
uint32_t lightingModel;
// Parameter for the toon shading part of the fragment shader
float toonDesaturationFactor = 0.5f;
} specializationData;
// Each shader constant of a shader stage corresponds to one map entry
std::array<VkSpecializationMapEntry, 2> specializationMapEntries;
// Shader bindings based on specialization constants are marked by the new "constant_id" layout qualifier:
// layout (constant_id = 0) const int LIGHTING_MODEL = 0;
// layout (constant_id = 1) const float PARAM_TOON_DESATURATION = 0.0f;
// Map entry for the lighting model to be used by the fragment shader
specializationMapEntries[0].constantID = 0;
specializationMapEntries[0].size = sizeof(specializationData.lightingModel);
specializationMapEntries[0].offset = 0;
// Map entry for the toon shader parameter
specializationMapEntries[1].constantID = 1;
specializationMapEntries[1].size = sizeof(specializationData.toonDesaturationFactor);
specializationMapEntries[1].offset = offsetof(SpecializationData, toonDesaturationFactor);
// Prepare specialization info block for the shader stage
VkSpecializationInfo specializationInfo{};
specializationInfo.dataSize = sizeof(specializationData);
specializationInfo.mapEntryCount = static_cast<uint32_t>(specializationMapEntries.size());
specializationInfo.pMapEntries = specializationMapEntries.data();
specializationInfo.pData = &specializationData;
// Create pipelines
// All pipelines will use the same "uber" shader and specialization constants to change branching and parameters of that shader
shaderStages[0] = loadShader(getShadersPath() + "specializationconstants/uber.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getShadersPath() + "specializationconstants/uber.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
// Specialization info is assigned is part of the shader stage (modul) and must be set after creating the module and before creating the pipeline
shaderStages[1].pSpecializationInfo = &specializationInfo;
// Solid phong shading
specializationData.lightingModel = 0;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.phong));
// Phong and textured
specializationData.lightingModel = 1;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.toon));
// Textured discard
specializationData.lightingModel = 2;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.textured));
}
二、Shader文件
1.fragShader
代码如下(示例):
#version 450
layout (binding = 1) uniform sampler2D samplerColormap;
layout (binding = 2) uniform sampler2D samplerDiscard;
layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec3 inColor;
layout (location = 2) in vec2 inUV;
layout (location = 3) in vec3 inViewVec;
layout (location = 4) in vec3 inLightVec;
layout (location = 0) out vec4 outFragColor;
// We use this constant to control the flow of the shader depending on the
// lighting model selected at pipeline creation time
layout (constant_id = 0) const int LIGHTING_MODEL = 0;
// Parameter for the toon shading part of the shader
layout (constant_id = 1) const float PARAM_TOON_DESATURATION = 0.0f;
void main()
{
switch (LIGHTING_MODEL) {
case 0: // Phong
{
vec3 ambient = inColor * vec3(0.25);
vec3 N = normalize(inNormal);
vec3 L = normalize(inLightVec);
vec3 V = normalize(inViewVec);
vec3 R = reflect(-L, N);
vec3 diffuse = max(dot(N, L), 0.0) * inColor;
vec3 specular = pow(max(dot(R, V), 0.0), 32.0) * vec3(0.75);
outFragColor = vec4(ambient + diffuse * 1.75 + specular, 1.0);
break;
}
case 1: // Toon
{
vec3 N = normalize(inNormal);
vec3 L = normalize(inLightVec);
float intensity = dot(N,L);
vec3 color;
if (intensity > 0.98)
color = inColor * 1.5;
else if (intensity > 0.9)
color = inColor * 1.0;
else if (intensity > 0.5)
color = inColor * 0.6;
else if (intensity > 0.25)
color = inColor * 0.4;
else
color = inColor * 0.2;
// Desaturate a bit
color = vec3(mix(color, vec3(dot(vec3(0.2126,0.7152,0.0722), color)), PARAM_TOON_DESATURATION));
outFragColor.rgb = color;
break;
}
case 2: // Textured
{
vec4 color = texture(samplerColormap, inUV).rrra;
vec3 ambient = color.rgb * vec3(0.25) * inColor;
vec3 N = normalize(inNormal);
vec3 L = normalize(inLightVec);
vec3 V = normalize(inViewVec);
vec3 R = reflect(-L, N);
vec3 diffuse = max(dot(N, L), 0.0) * color.rgb;
float specular = pow(max(dot(R, V), 0.0), 32.0) * color.a;
outFragColor = vec4(ambient + diffuse + vec3(specular), 1.0);
break;
}
}
}
2.vertShader
代码如下(示例):
#version 450
layout (location = 0) in vec3 inPos;
layout (location = 1) in vec3 inNormal;
layout (location = 2) in vec2 inUV;
layout (location = 3) in vec3 inColor;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
vec4 lightPos;
} ubo;
layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec3 outColor;
layout (location = 2) out vec2 outUV;
layout (location = 3) out vec3 outViewVec;
layout (location = 4) out vec3 outLightVec;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
outNormal = inNormal;
outColor = inColor;
outUV = inUV;
gl_Position = ubo.projection * ubo.model * vec4(inPos.xyz, 1.0);
vec4 pos = ubo.model * vec4(inPos, 1.0);
outNormal = mat3(ubo.model) * inNormal;
vec3 lPos = mat3(ubo.model) * ubo.lightPos.xyz;
outLightVec = lPos - pos.xyz;
outViewVec = -pos.xyz;
}