Added PCF filtering toggle (second pipeline using specialization constants), use createBuffer from Vulkan device class

This commit is contained in:
saschawillems 2017-01-07 20:20:40 +01:00
parent 80da11e964
commit 89f82e328c
3 changed files with 69 additions and 13 deletions

View file

@ -53,6 +53,7 @@ class VulkanExample : public VulkanExampleBase
public: public:
bool displayShadowMap = false; bool displayShadowMap = false;
bool lightPOV = false; bool lightPOV = false;
bool filterPCF = true;
// Keep depth range as small as possible // Keep depth range as small as possible
// for better shadow map precision // for better shadow map precision
@ -105,7 +106,8 @@ public:
struct { struct {
VkPipeline quad; VkPipeline quad;
VkPipeline offscreen; VkPipeline offscreen;
VkPipeline scene; VkPipeline sceneShadow;
VkPipeline sceneShadowPCF;
} pipelines; } pipelines;
struct { struct {
@ -167,7 +169,8 @@ public:
vkDestroyPipeline(device, pipelines.quad, nullptr); vkDestroyPipeline(device, pipelines.quad, nullptr);
vkDestroyPipeline(device, pipelines.offscreen, nullptr); vkDestroyPipeline(device, pipelines.offscreen, nullptr);
vkDestroyPipeline(device, pipelines.scene, nullptr); vkDestroyPipeline(device, pipelines.sceneShadow, nullptr);
vkDestroyPipeline(device, pipelines.sceneShadowPCF, nullptr);
vkDestroyPipelineLayout(device, pipelineLayouts.quad, nullptr); vkDestroyPipelineLayout(device, pipelineLayouts.quad, nullptr);
vkDestroyPipelineLayout(device, pipelineLayouts.offscreen, nullptr); vkDestroyPipelineLayout(device, pipelineLayouts.offscreen, nullptr);
@ -419,7 +422,7 @@ public:
// 3D scene // 3D scene
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.quad, 0, 1, &descriptorSets.scene, 0, NULL); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayouts.quad, 0, 1, &descriptorSets.scene, 0, NULL);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.scene); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, (filterPCF) ? pipelines.sceneShadowPCF : pipelines.sceneShadow);
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.scene.vertices.buf, offsets); vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.scene.vertices.buf, offsets);
vkCmdBindIndexBuffer(drawCmdBuffers[i], meshes.scene.indices.buf, 0, VK_INDEX_TYPE_UINT32); vkCmdBindIndexBuffer(drawCmdBuffers[i], meshes.scene.indices.buf, 0, VK_INDEX_TYPE_UINT32);
@ -456,23 +459,25 @@ public:
}; };
#undef QUAD_COLOR_NORMAL #undef QUAD_COLOR_NORMAL
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
vertexBuffer.size() * sizeof(Vertex), vertexBuffer.size() * sizeof(Vertex),
vertexBuffer.data(),
&meshes.quad.vertices.buf, &meshes.quad.vertices.buf,
&meshes.quad.vertices.mem); &meshes.quad.vertices.mem,
vertexBuffer.data()));
// Setup indices // Setup indices
std::vector<uint32_t> indexBuffer = { 0,1,2, 2,3,0 }; std::vector<uint32_t> indexBuffer = { 0,1,2, 2,3,0 };
meshes.quad.indexCount = indexBuffer.size(); meshes.quad.indexCount = indexBuffer.size();
createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
indexBuffer.size() * sizeof(uint32_t), indexBuffer.size() * sizeof(uint32_t),
indexBuffer.data(),
&meshes.quad.indices.buf, &meshes.quad.indices.buf,
&meshes.quad.indices.mem); &meshes.quad.indices.mem,
indexBuffer.data()));
} }
void setupVertexDescriptions() void setupVertexDescriptions()
@ -662,7 +667,7 @@ public:
VkPipelineRasterizationStateCreateInfo rasterizationState = VkPipelineRasterizationStateCreateInfo rasterizationState =
vkTools::initializers::pipelineRasterizationStateCreateInfo( vkTools::initializers::pipelineRasterizationStateCreateInfo(
VK_POLYGON_MODE_FILL, VK_POLYGON_MODE_FILL,
VK_CULL_MODE_NONE, VK_CULL_MODE_BACK_BIT,
VK_FRONT_FACE_CLOCKWISE, VK_FRONT_FACE_CLOCKWISE,
0); 0);
@ -726,11 +731,19 @@ public:
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.quad)); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.quad));
// 3D scene // Scene rendering with shadows applied
shaderStages[0] = loadShader(getAssetPath() + "shaders/shadowmapping/scene.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); shaderStages[0] = loadShader(getAssetPath() + "shaders/shadowmapping/scene.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getAssetPath() + "shaders/shadowmapping/scene.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); shaderStages[1] = loadShader(getAssetPath() + "shaders/shadowmapping/scene.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
rasterizationState.cullMode = VK_CULL_MODE_BACK_BIT; // Use specialization constants to select between horizontal and vertical blur
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.scene)); uint32_t enablePCF = 0;
VkSpecializationMapEntry specializationMapEntry = vkTools::initializers::specializationMapEntry(0, 0, sizeof(uint32_t));
VkSpecializationInfo specializationInfo = vkTools::initializers::specializationInfo(1, &specializationMapEntry, sizeof(uint32_t), &enablePCF);
shaderStages[1].pSpecializationInfo = &specializationInfo;
// No filtering
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.sceneShadow));
// PCF filtering
enablePCF = 1;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.sceneShadowPCF));
// Offscreen pipeline // Offscreen pipeline
shaderStages[0] = loadShader(getAssetPath() + "shaders/shadowmapping/offscreen.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); shaderStages[0] = loadShader(getAssetPath() + "shaders/shadowmapping/offscreen.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
@ -923,6 +936,12 @@ public:
viewChanged(); viewChanged();
} }
void toogleFilterPCF()
{
filterPCF = !filterPCF;
buildCommandBuffers();
}
virtual void keyPressed(uint32_t keyCode) virtual void keyPressed(uint32_t keyCode)
{ {
switch (keyCode) switch (keyCode)
@ -935,6 +954,10 @@ public:
case GAMEPAD_BUTTON_X: case GAMEPAD_BUTTON_X:
toogleLightPOV(); toogleLightPOV();
break; break;
case KEY_F:
case GAMEPAD_BUTTON_Y:
toogleFilterPCF();
break;
} }
} }
@ -943,9 +966,11 @@ public:
#if defined(__ANDROID__) #if defined(__ANDROID__)
textOverlay->addText("\"Button A\" to toggle shadow map", 5.0f, 85.0f, VulkanTextOverlay::alignLeft); textOverlay->addText("\"Button A\" to toggle shadow map", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
textOverlay->addText("\"Button X\" to toggle light's pov", 5.0f, 100.0f, VulkanTextOverlay::alignLeft); textOverlay->addText("\"Button X\" to toggle light's pov", 5.0f, 100.0f, VulkanTextOverlay::alignLeft);
textOverlay->addText("\"Button Y\" to toggle PCF filtering", 5.0f, 115.0f, VulkanTextOverlay::alignLeft);
#else #else
textOverlay->addText("\"s\" to toggle shadow map", 5.0f, 85.0f, VulkanTextOverlay::alignLeft); textOverlay->addText("\"s\" to toggle shadow map", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
textOverlay->addText("\"l\" to toggle light's pov", 5.0f, 100.0f, VulkanTextOverlay::alignLeft); textOverlay->addText("\"l\" to toggle light's pov", 5.0f, 100.0f, VulkanTextOverlay::alignLeft);
textOverlay->addText("\"f\" to toggle PCF filtering", 5.0f, 115.0f, VulkanTextOverlay::alignLeft);
#endif #endif
} }

View file

@ -89,6 +89,14 @@
<ClInclude Include="..\base\vulkanexamplebase.h" /> <ClInclude Include="..\base\vulkanexamplebase.h" />
<ClInclude Include="..\base\vulkantools.h" /> <ClInclude Include="..\base\vulkantools.h" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="..\data\shaders\shadowmapping\offscreen.frag" />
<None Include="..\data\shaders\shadowmapping\offscreen.vert" />
<None Include="..\data\shaders\shadowmapping\quad.frag" />
<None Include="..\data\shaders\shadowmapping\quad.vert" />
<None Include="..\data\shaders\shadowmapping\scene.frag" />
<None Include="..\data\shaders\shadowmapping\scene.vert" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

View file

@ -13,6 +13,9 @@
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav</Extensions> <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav</Extensions>
</Filter> </Filter>
<Filter Include="Shaders">
<UniqueIdentifier>{e5ea34e5-d8c9-496c-a922-cc90b6777601}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\base\vulkandebug.cpp"> <ClCompile Include="..\base\vulkandebug.cpp">
@ -39,4 +42,24 @@
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="..\data\shaders\shadowmapping\offscreen.frag">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\shadowmapping\offscreen.vert">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\shadowmapping\quad.frag">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\shadowmapping\quad.vert">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\shadowmapping\scene.frag">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\shadowmapping\scene.vert">
<Filter>Shaders</Filter>
</None>
</ItemGroup>
</Project> </Project>