Added SSAO example (wip)

This commit is contained in:
saschawillems 2016-10-28 21:16:21 +02:00
parent ad26540e4f
commit 9212a9e3cb
16 changed files with 1558 additions and 0 deletions

View file

@ -0,0 +1,28 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 0) uniform sampler2D samplerSSAO;
layout (location = 0) in vec2 inUV;
layout (location = 0) out float outFragColor;
const int blurSize = 4;
void main()
{
const int blurRange = 2;
vec2 texelSize = 1.0 / vec2(textureSize(samplerSSAO, 0));
float result = 0.0;
for (int x = -blurRange; x < blurRange; x++)
{
for (int y = -blurRange; y < blurRange; y++)
{
vec2 offset = vec2(float(x), float(y)) * texelSize;
result += texture(samplerSSAO, inUV + offset).r;
}
}
outFragColor = result / (blurRange * blurRange * blurRange * blurRange);
}

Binary file not shown.

View file

@ -0,0 +1,51 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 0) uniform sampler2D samplerposition;
layout (binding = 1) uniform sampler2D samplerNormal;
layout (binding = 2) uniform sampler2D samplerAlbedo;
layout (binding = 3) uniform sampler2D samplerSSAO;
layout (binding = 4) uniform sampler2D samplerSSAOBlur;
layout (binding = 5) uniform UBO
{
mat4 _dummy;
uint ssao;
uint ssaoOnly;
uint ssaoBlur;
} uboParams;
layout (location = 0) in vec2 inUV;
layout (location = 0) out vec4 outFragColor;
void main()
{
vec3 fragPos = texture(samplerposition, inUV).rgb;
vec3 normal = texture(samplerNormal, inUV).rgb;
vec4 albedo = texture(samplerAlbedo, inUV);
float ssao = (uboParams.ssaoBlur == 1) ? texture(samplerSSAOBlur, inUV).r : texture(samplerSSAO, inUV).r;
outFragColor = vec4(vec3(0.0), 1.0);
if (uboParams.ssaoOnly == 1)
{
outFragColor.rgb = ssao.rrr;
}
else
{
if (uboParams.ssao == 1)
{
outFragColor.rgb = ssao.rrr;
if (uboParams.ssaoOnly != 1)
outFragColor.rgb *= albedo.rgb;
}
else
{
outFragColor.rgb = albedo.rgb;
}
}
}

Binary file not shown.

View file

@ -0,0 +1,17 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (location = 0) out vec2 outUV;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
gl_Position = vec4(outUV * 2.0f - 1.0f, 0.0f, 1.0f);
}

Binary file not shown.

View file

@ -0,0 +1,29 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec2 inUV;
layout (location = 2) in vec3 inColor;
layout (location = 3) in vec3 inPos;
layout (location = 0) out vec4 outPosition;
layout (location = 1) out vec4 outNormal;
layout (location = 2) out vec4 outAlbedo;
const float NEAR_PLANE = 0.1f; //todo: specialization const
const float FAR_PLANE = 50.0f; //todo: specialization const
float linearDepth(float depth)
{
float z = depth * 2.0f - 1.0f;
return (2.0f * NEAR_PLANE * FAR_PLANE) / (FAR_PLANE + NEAR_PLANE - z * (FAR_PLANE - NEAR_PLANE));
}
void main()
{
outPosition = vec4(inPos, linearDepth(gl_FragCoord.z));
outNormal = vec4(normalize(inNormal) * 0.5 + 0.5, 1.0);
outAlbedo = vec4(inColor * 2.0, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,43 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (location = 0) in vec4 inPos;
layout (location = 1) in vec2 inUV;
layout (location = 2) in vec3 inColor;
layout (location = 3) in vec3 inNormal;
layout (location = 4) in vec3 inTangent;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
mat4 view;
} ubo;
layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec2 outUV;
layout (location = 2) out vec3 outColor;
layout (location = 3) out vec3 outPos;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
gl_Position = ubo.projection * ubo.view * ubo.model * inPos;
outUV = inUV;
// Vertex position in view space
outPos = vec3(ubo.view * ubo.model * inPos);
// Normal in view space
mat3 normalMatrix = transpose(inverse(mat3(ubo.view * ubo.model)));
outNormal = normalMatrix * inNormal;
outColor = inColor;
}

Binary file not shown.

View file

@ -0,0 +1,67 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 0) uniform sampler2D samplerPositionDepth;
layout (binding = 1) uniform sampler2D samplerNormal;
layout (binding = 2) uniform sampler2D ssaoNoise;
layout (binding = 3) uniform UBOSSAOKernel
{
vec4 samples[64]; // todo: specialization const
} uboSSAOKernel;
layout (binding = 4) uniform UBO
{
mat4 projection;
} ubo;
layout (location = 0) in vec2 inUV;
layout (location = 0) out float outFragColor;
// todo: specialization const
const int kernelSize = 64;
const float radius = 1.0;
void main()
{
// Get G-Buffer values
vec3 fragPos = texture(samplerPositionDepth, inUV).rgb;
vec3 normal = normalize(texture(samplerNormal, inUV).rgb) * 2.0 - 1.0f;
// Get a random vector using a noise lookup
ivec2 texDim = textureSize(samplerPositionDepth, 0);
const vec2 noiseUV = vec2(float(texDim.x)/4.0f, float(texDim.y)/4.0f) * inUV;
vec3 randomVec = texture(ssaoNoise, noiseUV).xyz * 2.0 - 1.0;
// Create TBN matrix
vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
vec3 bitangent = cross(tangent, normal);
mat3 TBN = mat3(tangent, bitangent, normal);
// Calculate occlusion value
float occlusion = 0.0f;
for(int i = 0; i < kernelSize; i++)
{
vec3 samplePos = TBN * uboSSAOKernel.samples[i].xyz;
samplePos = fragPos + samplePos * radius;
// project
vec4 offset = vec4(samplePos, 1.0f);
offset = ubo.projection * offset;
offset.xyz /= offset.w;
offset.xyz = offset.xyz * 0.5f + 0.5f;
float sampleDepth = -texture(samplerPositionDepth, offset.xy).w;
// Range check
float rangeCheck = smoothstep(0.0f, 1.0f, radius / abs(fragPos.z - sampleDepth));
occlusion += (sampleDepth >= samplePos.z ? 1.0f : 0.0f) * rangeCheck;
}
occlusion = 1.0 - (occlusion / float(kernelSize));
outFragColor = occlusion;
}

Binary file not shown.

1148
ssao/ssao.cpp Normal file

File diff suppressed because it is too large Load diff

103
ssao/ssao.vcxproj Normal file
View file

@ -0,0 +1,103 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{43FCECF5-F5E1-45DB-972A-73942D459C0A}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)\bin\</OutDir>
<IntDir>$(SolutionDir)\bin\intermediate\$(ProjectName)\$(ConfigurationName)</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)\bin\</OutDir>
<IntDir>$(SolutionDir)\bin\intermediate\$(ProjectName)\$(ConfigurationName)</IntDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;VK_USE_PLATFORM_WIN32_KHR;_USE_MATH_DEFINES;NOMINMAX;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>..\base;..\external\glm;..\external\gli;..\external\assimp;..\external;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/FS %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem>
<AdditionalDependencies>..\libs\vulkan\vulkan-1.lib;..\libs\assimp\assimp.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;VK_USE_PLATFORM_WIN32_KHR;_USE_MATH_DEFINES;NOMINMAX;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<AdditionalIncludeDirectories>..\base;..\external\glm;..\external\gli;..\external\assimp;..\external;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>..\libs\vulkan\vulkan-1.lib;..\libs\assimp\assimp.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\base\vulkandebug.cpp" />
<ClCompile Include="..\base\vulkanexamplebase.cpp" />
<ClCompile Include="..\base\vulkantools.cpp" />
<ClCompile Include="ssao.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\base\vulkandebug.h" />
<ClInclude Include="..\base\vulkanexamplebase.h" />
<ClInclude Include="..\base\vulkantools.h" />
</ItemGroup>
<ItemGroup>
<None Include="..\data\shaders\ssao\blur.frag" />
<None Include="..\data\shaders\ssao\composition.frag" />
<None Include="..\data\shaders\ssao\fullscreen.vert" />
<None Include="..\data\shaders\ssao\gbuffer.frag" />
<None Include="..\data\shaders\ssao\gbuffer.vert" />
<None Include="..\data\shaders\ssao\ssao.frag" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

65
ssao/ssao.vcxproj.filters Normal file
View file

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<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;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="Shaders">
<UniqueIdentifier>{08257928-84ac-4091-98b2-2187fb618654}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\base\vulkandebug.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\base\vulkanexamplebase.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\base\vulkantools.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ssao.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\base\vulkandebug.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\base\vulkanexamplebase.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\base\vulkantools.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\data\shaders\ssao\gbuffer.frag">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\ssao\ssao.frag">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\ssao\fullscreen.vert">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\ssao\gbuffer.vert">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\ssao\blur.frag">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\ssao\composition.frag">
<Filter>Shaders</Filter>
</None>
</ItemGroup>
</Project>

View file

@ -89,6 +89,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Base", "Base", "{09B9A54B-F
base\vulkanframebuffer.hpp = base\vulkanframebuffer.hpp
base\vulkanheightmap.hpp = base\vulkanheightmap.hpp
base\vulkanMeshLoader.hpp = base\vulkanMeshLoader.hpp
base\vulkanscene.hpp = base\vulkanscene.hpp
base\vulkanswapchain.hpp = base\vulkanswapchain.hpp
base\vulkantextoverlay.hpp = base\vulkantextoverlay.hpp
base\vulkanTextureLoader.hpp = base\vulkanTextureLoader.hpp
@ -118,6 +119,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "computecullandlod", "comput
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "subpasses", "subpasses\subpasses.vcxproj", "{DE1DEF4B-307E-45C2-90CC-8023327294E2}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ssao", "ssao\ssao.vcxproj", "{43FCECF5-F5E1-45DB-972A-73942D459C0A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
@ -292,6 +295,10 @@ Global
{DE1DEF4B-307E-45C2-90CC-8023327294E2}.Debug|x64.Build.0 = Debug|x64
{DE1DEF4B-307E-45C2-90CC-8023327294E2}.Release|x64.ActiveCfg = Release|x64
{DE1DEF4B-307E-45C2-90CC-8023327294E2}.Release|x64.Build.0 = Release|x64
{43FCECF5-F5E1-45DB-972A-73942D459C0A}.Debug|x64.ActiveCfg = Debug|x64
{43FCECF5-F5E1-45DB-972A-73942D459C0A}.Debug|x64.Build.0 = Debug|x64
{43FCECF5-F5E1-45DB-972A-73942D459C0A}.Release|x64.ActiveCfg = Release|x64
{43FCECF5-F5E1-45DB-972A-73942D459C0A}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE