Added basic HDR rendering example (wip)

This commit is contained in:
saschawillems 2017-01-29 16:03:31 +01:00
parent 55771091de
commit cbfdfc904b
17 changed files with 1518 additions and 1 deletions

View file

@ -0,0 +1,66 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 0) uniform sampler2D samplerColor0;
layout (binding = 1) uniform sampler2D samplerColor1;
layout (location = 0) in vec2 inUV;
layout (location = 0) out vec4 outColor;
layout (constant_id = 0) const int dir = 0;
void main(void)
{
// From the OpenGL Super bible
const float weights[] = float[](0.0024499299678342,
0.0043538453346397,
0.0073599963704157,
0.0118349786570722,
0.0181026699707781,
0.0263392293891488,
0.0364543006660986,
0.0479932050577658,
0.0601029809166942,
0.0715974486241365,
0.0811305381519717,
0.0874493212267511,
0.0896631113333857,
0.0874493212267511,
0.0811305381519717,
0.0715974486241365,
0.0601029809166942,
0.0479932050577658,
0.0364543006660986,
0.0263392293891488,
0.0181026699707781,
0.0118349786570722,
0.0073599963704157,
0.0043538453346397,
0.0024499299678342);
const float blurScale = 0.003;
const float blurStrength = 1.0;
float ar = 1.0;
// Aspect ratio for vertical blur pass
if (dir == 1)
{
vec2 ts = textureSize(samplerColor1, 0);
ar = ts.y / ts.x;
}
vec2 P = inUV.yx - vec2(0, (weights.length() >> 1) * ar * blurScale);
vec4 color = vec4(0.0);
for (int i = 0; i < weights.length(); i++)
{
vec2 dv = vec2(0.0, i * blurScale) * ar;
color += texture(samplerColor1, P + dv) * weights[i] * blurStrength;
}
outColor = color;
}

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,16 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 0) uniform sampler2D samplerColor0;
layout (binding = 1) uniform sampler2D samplerColor1;
layout (location = 0) in vec2 inUV;
layout (location = 0) out vec4 outColor;
void main()
{
outColor = texture(samplerColor0, inUV);
}

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,101 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 1) uniform sampler2D samplerEnvMap;
layout (location = 0) in vec3 inUVW;
layout (location = 1) in vec3 inPos;
layout (location = 2) in vec3 inNormal;
layout (location = 3) in vec3 inViewVec;
layout (location = 4) in vec3 inLightVec;
layout (location = 5) in mat4 inInvModelView;
layout (location = 0) out vec4 outColor0;
layout (location = 1) out vec4 outColor1;
layout (constant_id = 0) const int type = 0;
#define PI 3.1415926
#define TwoPI (2.0 * PI)
layout (binding = 2) uniform UBO {
float exposure;
} ubo;
vec2 envMapEquirect(vec3 normal)
{
float phi = acos(-normal.y);
float theta = atan(-1.0 * normal.x, normal.z) + PI;
return vec2(theta / TwoPI, phi / PI);
}
void main()
{
vec4 color;
vec3 wcNormal;
switch (type) {
case 0: // Skybox
{
vec3 normal = normalize(inUVW);
color = texture(samplerEnvMap, envMapEquirect(normal));
}
break;
case 1: // Reflect
{
vec3 wViewVec = mat3(inInvModelView) * normalize(inViewVec);
vec3 normal = normalize(inNormal);
vec3 wNormal = mat3(inInvModelView) * normal;
float NdotL = max(dot(normal, inLightVec), 0.0);
vec3 eyeDir = normalize(inViewVec);
vec3 halfVec = normalize(inLightVec + eyeDir);
float NdotH = max(dot(normal, halfVec), 0.0);
float NdotV = max(dot(normal, eyeDir), 0.0);
float VdotH = max(dot(eyeDir, halfVec), 0.0);
// Geometric attenuation
float NH2 = 2.0 * NdotH;
float g1 = (NH2 * NdotV) / VdotH;
float g2 = (NH2 * NdotL) / VdotH;
float geoAtt = min(1.0, min(g1, g2));
const float F0 = 0.6;
const float k = 0.2;
// Fresnel (schlick approximation)
float fresnel = pow(1.0 - VdotH, 5.0);
fresnel *= (1.0 - F0);
fresnel += F0;
float spec = (fresnel * geoAtt) / (NdotV * NdotL * 3.14);
color = texture(samplerEnvMap, envMapEquirect(reflect(-wViewVec, wNormal)));
color = vec4(color.rgb * NdotL * (k + spec * (1.0 - k)), 1.0);
}
break;
case 2: // Refract
{
vec3 wViewVec = mat3(inInvModelView) * normalize(inViewVec);
vec3 wNormal = mat3(inInvModelView) * inNormal;
color = texture(samplerEnvMap, envMapEquirect(refract(-wViewVec, wNormal, 1.0/1.6)));
}
break;
}
// Color with manual exposure into attachment 0
outColor0.rgb = vec3(1.0) - exp(-color.rgb * ubo.exposure);
// Bright parts for bloom into attachment 1
float l = dot(outColor0.rgb, vec3(0.2126, 0.7152, 0.0722));
float threshold = 0.75;
outColor1.rgb = (l > threshold) ? outColor0.rgb : vec3(0.0);
outColor1.a = 1.0;
}

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 (location = 0) in vec3 inPos;
layout (location = 1) in vec3 inNormal;
layout (constant_id = 0) const int type = 0;
layout (binding = 0) uniform UBO {
mat4 projection;
mat4 modelview;
} ubo;
layout (location = 0) out vec3 outUVW;
layout (location = 1) out vec3 outPos;
layout (location = 2) out vec3 outNormal;
layout (location = 3) out vec3 outViewVec;
layout (location = 4) out vec3 outLightVec;
layout (location = 5) out mat4 outInvModelView;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
outUVW = inPos;
outUVW.x *= -1.0;
switch(type) {
case 0: // Skybox
outPos = vec3(mat3(ubo.modelview) * inPos);
gl_Position = vec4(ubo.projection * vec4(outPos, 1.0));
break;
case 1: // Object
outPos = vec3(ubo.modelview * vec4(inPos, 1.0));
gl_Position = ubo.projection * ubo.modelview * vec4(inPos.xyz, 1.0);
break;
}
outPos = vec3(ubo.modelview * vec4(inPos, 1.0));
outNormal = mat3(ubo.modelview) * inNormal;
outInvModelView = inverse(ubo.modelview);
vec3 lightPos = vec3(0.0f, -5.0f, 5.0f);
outLightVec = lightPos.xyz - outPos.xyz;
outViewVec = -outPos.xyz;
}

Binary file not shown.

Binary file not shown.

1074
hdr/hdr.cpp Normal file

File diff suppressed because it is too large Load diff

103
hdr/hdr.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>{582913B3-9B37-48CA-AEBD-69023DA9F7C8}</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="hdr.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\base\vulkandebug.h" />
<ClInclude Include="..\base\vulkanexamplebase.h" />
<ClInclude Include="..\base\vulkantools.h" />
</ItemGroup>
<ItemGroup>
<None Include="..\data\shaders\hdr\bloom.frag" />
<None Include="..\data\shaders\hdr\bloom.vert" />
<None Include="..\data\shaders\hdr\composition.frag" />
<None Include="..\data\shaders\hdr\composition.vert" />
<None Include="..\data\shaders\hdr\gbuffer.frag" />
<None Include="..\data\shaders\hdr\gbuffer.vert" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

65
hdr/hdr.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</Extensions>
</Filter>
<Filter Include="Shaders">
<UniqueIdentifier>{3adf072b-32a9-412a-8eb5-5329a601df1a}</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="hdr.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\hdr\composition.frag">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\hdr\composition.vert">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\hdr\gbuffer.frag">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\hdr\gbuffer.vert">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\hdr\bloom.frag">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\hdr\bloom.vert">
<Filter>Shaders</Filter>
</None>
</ItemGroup>
</Project>

View file

@ -90,10 +90,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Base", "Base", "{09B9A54B-F
base\vulkanheightmap.hpp = base\vulkanheightmap.hpp base\vulkanheightmap.hpp = base\vulkanheightmap.hpp
base\VulkanInitializers.hpp = base\VulkanInitializers.hpp base\VulkanInitializers.hpp = base\VulkanInitializers.hpp
base\vulkanMeshLoader.hpp = base\vulkanMeshLoader.hpp base\vulkanMeshLoader.hpp = base\vulkanMeshLoader.hpp
base\VulkanModelLoader.hpp = base\VulkanModelLoader.hpp base\VulkanModel.hpp = base\VulkanModel.hpp
base\vulkanscene.hpp = base\vulkanscene.hpp base\vulkanscene.hpp = base\vulkanscene.hpp
base\vulkanswapchain.hpp = base\vulkanswapchain.hpp base\vulkanswapchain.hpp = base\vulkanswapchain.hpp
base\vulkantextoverlay.hpp = base\vulkantextoverlay.hpp base\vulkantextoverlay.hpp = base\vulkantextoverlay.hpp
base\VulkanTexture.hpp = base\VulkanTexture.hpp
base\vulkanTextureLoader.hpp = base\vulkanTextureLoader.hpp base\vulkanTextureLoader.hpp = base\vulkanTextureLoader.hpp
base\vulkantools.cpp = base\vulkantools.cpp base\vulkantools.cpp = base\vulkantools.cpp
base\vulkantools.h = base\vulkantools.h base\vulkantools.h = base\vulkantools.h
@ -137,6 +138,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dynamicuniformbuffer", "dyn
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "specializationconstants", "specializationconstants\specializationconstants.vcxproj", "{00F600D2-DF0E-4F07-B722-633F7C4B2F65}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "specializationconstants", "specializationconstants\specializationconstants.vcxproj", "{00F600D2-DF0E-4F07-B722-633F7C4B2F65}"
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "hdr", "hdr\hdr.vcxproj", "{582913B3-9B37-48CA-AEBD-69023DA9F7C8}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64 Debug|x64 = Debug|x64
@ -331,6 +334,10 @@ Global
{00F600D2-DF0E-4F07-B722-633F7C4B2F65}.Debug|x64.Build.0 = Debug|x64 {00F600D2-DF0E-4F07-B722-633F7C4B2F65}.Debug|x64.Build.0 = Debug|x64
{00F600D2-DF0E-4F07-B722-633F7C4B2F65}.Release|x64.ActiveCfg = Release|x64 {00F600D2-DF0E-4F07-B722-633F7C4B2F65}.Release|x64.ActiveCfg = Release|x64
{00F600D2-DF0E-4F07-B722-633F7C4B2F65}.Release|x64.Build.0 = Release|x64 {00F600D2-DF0E-4F07-B722-633F7C4B2F65}.Release|x64.Build.0 = Release|x64
{582913B3-9B37-48CA-AEBD-69023DA9F7C8}.Debug|x64.ActiveCfg = Debug|x64
{582913B3-9B37-48CA-AEBD-69023DA9F7C8}.Debug|x64.Build.0 = Debug|x64
{582913B3-9B37-48CA-AEBD-69023DA9F7C8}.Release|x64.ActiveCfg = Release|x64
{582913B3-9B37-48CA-AEBD-69023DA9F7C8}.Release|x64.Build.0 = Release|x64
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE