Separate oaque and masked pipelines

This commit is contained in:
Sascha Willems 2020-09-05 12:15:54 +02:00
parent 85bf87125b
commit 8d896cd04b
3 changed files with 44 additions and 19 deletions

View file

@ -43,7 +43,7 @@ void main()
mat3 TBN = mat3(T, B, N); mat3 TBN = mat3(T, B, N);
N = TBN * normalize(texture(samplerNormalMap, inUV).xyz * 2.0 - vec3(1.0)); N = TBN * normalize(texture(samplerNormalMap, inUV).xyz * 2.0 - vec3(1.0));
const float ambient = 0.1; const float ambient = 0.25;
vec3 L = normalize(inLightVec); vec3 L = normalize(inLightVec);
vec3 V = normalize(inViewVec); vec3 V = normalize(inViewVec);
vec3 R = reflect(-L, N); vec3 R = reflect(-L, N);
@ -52,7 +52,6 @@ void main()
outFragColor = vec4(diffuse * color.rgb + specular, color.a); outFragColor = vec4(diffuse * color.rgb + specular, color.a);
if (uboScene.colorShadingRates == 1) { if (uboScene.colorShadingRates == 1) {
outFragColor = vec4(diffuse * vec3(1.0) + specular, color.a);
if (gl_FragmentSizeNV.x == 1 && gl_FragmentSizeNV.y == 1) { if (gl_FragmentSizeNV.x == 1 && gl_FragmentSizeNV.y == 1) {
outFragColor.rgb *= vec3(0.5, 1.0, 0.5); outFragColor.rgb *= vec3(0.5, 1.0, 0.5);
} }

View file

@ -13,6 +13,7 @@ VulkanExample::VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
title = "Variable rate shading"; title = "Variable rate shading";
apiVersion = VK_VERSION_1_1; apiVersion = VK_VERSION_1_1;
camera.type = Camera::CameraType::firstperson; camera.type = Camera::CameraType::firstperson;
camera.flipY = true;
camera.setPosition(glm::vec3(0.0f, 1.0f, 0.0f)); camera.setPosition(glm::vec3(0.0f, 1.0f, 0.0f));
camera.setRotation(glm::vec3(0.0f, -90.0f, 0.0f)); camera.setRotation(glm::vec3(0.0f, -90.0f, 0.0f));
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f); camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f);
@ -69,17 +70,17 @@ void VulkanExample::buildCommandBuffers()
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr);
// POI: Bind the image that contains the shading rate patterns
if (enableShadingRate) { if (enableShadingRate) {
// POI: todo
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.shadingRate);
vkCmdBindShadingRateImageNV(drawCmdBuffers[i], shadingRateImage.view, VK_IMAGE_LAYOUT_SHADING_RATE_OPTIMAL_NV); vkCmdBindShadingRateImageNV(drawCmdBuffers[i], shadingRateImage.view, VK_IMAGE_LAYOUT_SHADING_RATE_OPTIMAL_NV);
} };
else
{
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.base);
}
scene.draw(drawCmdBuffers[i], vkglTF::RenderFlags::BindImages, pipelineLayout); // Render the scene
Pipelines& pipelines = enableShadingRate ? shadingRatePipelines : basePipelines;
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.opaque);
scene.draw(drawCmdBuffers[i], vkglTF::RenderFlags::BindImages | vkglTF::RenderFlags::RenderOpaqueNodes, pipelineLayout);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.masked);
scene.draw(drawCmdBuffers[i], vkglTF::RenderFlags::BindImages | vkglTF::RenderFlags::RenderAlphaMaskedNodes, pipelineLayout);
drawUI(drawCmdBuffers[i]); drawUI(drawCmdBuffers[i]);
vkCmdEndRenderPass(drawCmdBuffers[i]); vkCmdEndRenderPass(drawCmdBuffers[i]);
@ -90,7 +91,7 @@ void VulkanExample::buildCommandBuffers()
void VulkanExample::loadAssets() void VulkanExample::loadAssets()
{ {
vkglTF::descriptorBindingFlags = vkglTF::DescriptorBindingFlags::ImageBaseColor | vkglTF::DescriptorBindingFlags::ImageNormalMap; vkglTF::descriptorBindingFlags = vkglTF::DescriptorBindingFlags::ImageBaseColor | vkglTF::DescriptorBindingFlags::ImageNormalMap;
scene.loadFromFile(getAssetPath() + "models/sponza/sponza.gltf", vulkanDevice, queue, vkglTF::FileLoadingFlags::PreTransformVertices | vkglTF::FileLoadingFlags::FlipY); scene.loadFromFile(getAssetPath() + "models/sponza/sponza.gltf", vulkanDevice, queue, vkglTF::FileLoadingFlags::PreTransformVertices);
} }
void VulkanExample::setupDescriptors() void VulkanExample::setupDescriptors()
@ -123,7 +124,7 @@ void VulkanExample::setupDescriptors()
std::vector<VkWriteDescriptorSet> writeDescriptorSets = { std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &shaderData.buffer.descriptor), vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &shaderData.buffer.descriptor),
}; };
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL); vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, nullptr);
} }
// [POI] // [POI]
@ -288,8 +289,27 @@ void VulkanExample::preparePipelines()
shaderStages[0] = loadShader(getShadersPath() + "variablerateshading/scene.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); shaderStages[0] = loadShader(getShadersPath() + "variablerateshading/scene.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getShadersPath() + "variablerateshading/scene.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); shaderStages[1] = loadShader(getShadersPath() + "variablerateshading/scene.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
// Properties for alpha masked materials will be passed via specialization constants
struct SpecializationData {
bool alphaMask;
float alphaMaskCutoff;
} specializationData;
specializationData.alphaMask = false;
specializationData.alphaMaskCutoff = 0.5f;
const std::vector<VkSpecializationMapEntry> specializationMapEntries = {
vks::initializers::specializationMapEntry(0, offsetof(SpecializationData, alphaMask), sizeof(SpecializationData::alphaMask)),
vks::initializers::specializationMapEntry(1, offsetof(SpecializationData, alphaMaskCutoff), sizeof(SpecializationData::alphaMaskCutoff)),
};
VkSpecializationInfo specializationInfo = vks::initializers::specializationInfo(specializationMapEntries, sizeof(specializationData), &specializationData);
shaderStages[1].pSpecializationInfo = &specializationInfo;
// Create pipeline without shading rate // Create pipeline without shading rate
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.base)); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &basePipelines.opaque));
specializationData.alphaMask = true;
rasterizationStateCI.cullMode = VK_CULL_MODE_NONE;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &basePipelines.masked));
rasterizationStateCI.cullMode = VK_CULL_MODE_BACK_BIT;
specializationData.alphaMask = false;
// Create pipeline with shading rate enabled // Create pipeline with shading rate enabled
// [POI] Possible per-Viewport shading rate palette entries // [POI] Possible per-Viewport shading rate palette entries
@ -316,7 +336,10 @@ void VulkanExample::preparePipelines()
pipelineViewportShadingRateImageStateCI.viewportCount = 1; pipelineViewportShadingRateImageStateCI.viewportCount = 1;
pipelineViewportShadingRateImageStateCI.pShadingRatePalettes = &shadingRatePalette; pipelineViewportShadingRateImageStateCI.pShadingRatePalettes = &shadingRatePalette;
viewportStateCI.pNext = &pipelineViewportShadingRateImageStateCI; viewportStateCI.pNext = &pipelineViewportShadingRateImageStateCI;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.shadingRate)); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &shadingRatePipelines.opaque));
specializationData.alphaMask = true;
rasterizationStateCI.cullMode = VK_CULL_MODE_NONE;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &shadingRatePipelines.masked));
} }
void VulkanExample::prepareUniformBuffers() void VulkanExample::prepareUniformBuffers()

View file

@ -31,16 +31,19 @@ public:
glm::mat4 projection; glm::mat4 projection;
glm::mat4 view; glm::mat4 view;
glm::mat4 model = glm::mat4(1.0f); glm::mat4 model = glm::mat4(1.0f);
glm::vec4 lightPos = glm::vec4(0.0f, -5.0f, 0.0f, 1.0f); glm::vec4 lightPos = glm::vec4(0.0f, 2.5f, 0.0f, 1.0f);
glm::vec4 viewPos; glm::vec4 viewPos;
int32_t colorShadingRate; int32_t colorShadingRate;
} values; } values;
} shaderData; } shaderData;
struct Pipelines { struct Pipelines {
VkPipeline base; VkPipeline opaque;
VkPipeline shadingRate; VkPipeline masked;
} pipelines; };
Pipelines basePipelines;
Pipelines shadingRatePipelines;
VkPipelineLayout pipelineLayout; VkPipelineLayout pipelineLayout;
VkDescriptorSet descriptorSet; VkDescriptorSet descriptorSet;