Started work on MSAA with deferred rendering example

This commit is contained in:
saschawillems 2016-09-28 20:54:45 +02:00
parent 3cd79a896c
commit 116952bec8
16 changed files with 1677 additions and 0 deletions

View file

@ -0,0 +1,55 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 1) uniform sampler2DMS samplerPosition;
layout (binding = 2) uniform sampler2DMS samplerNormal;
layout (binding = 3) uniform sampler2DMS samplerAlbedo;
layout (location = 0) in vec3 inUV;
layout (location = 0) out vec4 outFragColor;
#define NUM_SAMPLES 8
vec4 resolve(sampler2DMS tex, ivec2 uv)
{
vec4 result = vec4(0.0);
int count = 0;
for (int i = 0; i < NUM_SAMPLES; i++)
{
vec4 val = texelFetch(tex, uv, i);
result += val;
count++;
}
return result / float(NUM_SAMPLES);
}
void main()
{
ivec2 attDim = textureSize(samplerPosition);
ivec2 UV = ivec2(inUV.st * attDim * 2.0);
highp int index = 0;
if (inUV.s > 0.5)
{
index = 1;
UV.s -= attDim.x;
}
if (inUV.t > 0.5)
{
index = 2;
UV.t -= attDim.y;
}
vec3 components[3];
components[0] = resolve(samplerPosition, UV).rgb;
components[1] = resolve(samplerNormal, UV).rgb;
components[2] = resolve(samplerAlbedo, UV).rgb;
// Uncomment to display specular component
//components[2] = vec3(texture(samplerAlbedo, inUV.st).a);
// Select component depending on UV
outFragColor.rgb = components[index];
}

Binary file not shown.

View file

@ -0,0 +1,23 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
} ubo;
layout (location = 0) out vec3 outUV;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
outUV = vec3((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2, 0.0);
gl_Position = vec4(outUV.st * 2.0f - 1.0f, 0.0f, 1.0f);
}

Binary file not shown.

View file

@ -0,0 +1,103 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 1) uniform sampler2DMS samplerPosition;
layout (binding = 2) uniform sampler2DMS samplerNormal;
layout (binding = 3) uniform sampler2DMS samplerAlbedo;
layout (location = 0) in vec2 inUV;
layout (location = 0) out vec4 outFragcolor;
struct Light {
vec4 position;
vec3 color;
float radius;
};
layout (binding = 4) uniform UBO
{
Light lights[6];
vec4 viewPos;
ivec2 windowSize;
} ubo;
layout (constant_id = 0) const int NUM_SAMPLES = 8;
#define NUM_LIGHTS 6
// Manual resolve for MSAA samples
vec4 resolve(sampler2DMS tex, ivec2 uv)
{
vec4 result = vec4(0.0);
for (int i = 0; i < NUM_SAMPLES; i++)
{
vec4 val = texelFetch(tex, uv, i);
result += val;
}
// Average resolved samples
return result / float(NUM_SAMPLES);
}
vec3 calculateLighting(vec3 pos, vec3 normal, vec4 albedo)
{
vec3 result = vec3(0.0);
for(int i = 0; i < NUM_LIGHTS; ++i)
{
// Vector to light
vec3 L = ubo.lights[i].position.xyz - pos;
// Distance from light to fragment position
float dist = length(L);
// Viewer to fragment
vec3 V = ubo.viewPos.xyz - pos;
V = normalize(V);
// Light to fragment
L = normalize(L);
// Attenuation
float atten = ubo.lights[i].radius / (pow(dist, 2.0) + 1.0);
// Diffuse part
vec3 N = normalize(normal);
float NdotL = max(0.0, dot(N, L));
vec3 diff = ubo.lights[i].color * albedo.rgb * NdotL * atten;
// Specular part
vec3 R = reflect(-L, N);
float NdotR = max(0.0, dot(R, V));
vec3 spec = ubo.lights[i].color * albedo.a * pow(NdotR, 8.0) * atten;
result += diff + spec;
}
return result;
}
void main()
{
ivec2 attDim = textureSize(samplerPosition);
ivec2 UV = ivec2(inUV * attDim);
#define ambient 0.15
// Ambient part
vec4 alb = resolve(samplerAlbedo, UV);
vec3 fragColor = vec3(0.0);
// Calualte lighting for every MSAA sample
for (int i = 0; i < NUM_SAMPLES; i++)
{
vec3 pos = texelFetch(samplerPosition, UV, i).rgb;
vec3 normal = texelFetch(samplerNormal, UV, i).rgb;
vec4 albedo = texelFetch(samplerAlbedo, UV, i);
fragColor += calculateLighting(pos, normal, albedo);
}
fragColor = (alb.rgb * ambient) + fragColor / float(NUM_SAMPLES);
outFragcolor = vec4(fragColor, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,26 @@
#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 vec2 inUV;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
} ubo;
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,33 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 1) uniform sampler2D samplerColor;
layout (binding = 2) uniform sampler2D samplerNormalMap;
layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec2 inUV;
layout (location = 2) in vec3 inColor;
layout (location = 3) in vec3 inWorldPos;
layout (location = 4) in vec3 inTangent;
layout (location = 0) out vec4 outPosition;
layout (location = 1) out vec4 outNormal;
layout (location = 2) out vec4 outAlbedo;
void main()
{
outPosition = vec4(inWorldPos, 1.0);
// Calculate normal in tangent space
vec3 N = normalize(inNormal);
N.y = -N.y;
vec3 T = normalize(inTangent);
vec3 B = cross(N, T);
mat3 TBN = mat3(T, B, N);
vec3 tnorm = TBN * normalize(texture(samplerNormalMap, inUV).xyz * 2.0 - vec3(1.0));
outNormal = vec4(tnorm, 1.0);
outAlbedo = texture(samplerColor, inUV);
}

Binary file not shown.

View file

@ -0,0 +1,52 @@
#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;
vec4 instancePos[3];
} ubo;
layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec2 outUV;
layout (location = 2) out vec3 outColor;
layout (location = 3) out vec3 outWorldPos;
layout (location = 4) out vec3 outTangent;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
vec4 tmpPos = vec4(inPos.xyz, 1.0) + ubo.instancePos[gl_InstanceIndex];
gl_Position = ubo.projection * ubo.view * ubo.model * tmpPos;
outUV = inUV;
outUV.t = 1.0 - outUV.t;
// Vertex position in world space
outWorldPos = vec3(ubo.model * tmpPos);
// GL to Vulkan coord space
outWorldPos.y = -outWorldPos.y;
// Normal in world space
mat3 mNormal = transpose(inverse(mat3(ubo.model)));
outNormal = mNormal * normalize(inNormal);
outTangent = mNormal * normalize(inTangent);
// Currently just vertex color
outColor = inColor;
}

Binary file not shown.

File diff suppressed because it is too large Load diff

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>{0CB44B34-A81F-4002-9AC7-E0EEA55D8A60}</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="deferredmultisampling.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\base\vulkandebug.h" />
<ClInclude Include="..\base\vulkanexamplebase.h" />
<ClInclude Include="..\base\vulkantools.h" />
</ItemGroup>
<ItemGroup>
<None Include="..\data\shaders\deferredmultisampling\debug.frag" />
<None Include="..\data\shaders\deferredmultisampling\debug.vert" />
<None Include="..\data\shaders\deferredmultisampling\deferred.frag" />
<None Include="..\data\shaders\deferredmultisampling\deferred.vert" />
<None Include="..\data\shaders\deferredmultisampling\mrt.frag" />
<None Include="..\data\shaders\deferredmultisampling\mrt.vert" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

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>{210c4466-75e2-4230-9332-1ac69de9dcec}</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="deferredmultisampling.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\deferredmultisampling\debug.frag">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\deferredmultisampling\debug.vert">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\deferredmultisampling\deferred.frag">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\deferredmultisampling\deferred.vert">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\deferredmultisampling\mrt.frag">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\shaders\deferredmultisampling\mrt.vert">
<Filter>Shaders</Filter>
</None>
</ItemGroup>
</Project>

View file

@ -109,6 +109,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "texture3d", "texture3d\text
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "texturesparseresidency", "texturesparseresidency\texturesparseresidency.vcxproj", "{1FA0178C-F5E9-4B2E-A488-14F310F8DBD9}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "texturesparseresidency", "texturesparseresidency\texturesparseresidency.vcxproj", "{1FA0178C-F5E9-4B2E-A488-14F310F8DBD9}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "deferred", "deferred", "{460EE42F-4178-49EF-9AC0-415599B80303}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "deferredmultisampling", "deferredmultisampling\deferredmultisampling.vcxproj", "{0CB44B34-A81F-4002-9AC7-E0EEA55D8A60}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64 Debug|x64 = Debug|x64
@ -271,6 +275,10 @@ Global
{1FA0178C-F5E9-4B2E-A488-14F310F8DBD9}.Debug|x64.Build.0 = Debug|x64 {1FA0178C-F5E9-4B2E-A488-14F310F8DBD9}.Debug|x64.Build.0 = Debug|x64
{1FA0178C-F5E9-4B2E-A488-14F310F8DBD9}.Release|x64.ActiveCfg = Release|x64 {1FA0178C-F5E9-4B2E-A488-14F310F8DBD9}.Release|x64.ActiveCfg = Release|x64
{1FA0178C-F5E9-4B2E-A488-14F310F8DBD9}.Release|x64.Build.0 = Release|x64 {1FA0178C-F5E9-4B2E-A488-14F310F8DBD9}.Release|x64.Build.0 = Release|x64
{0CB44B34-A81F-4002-9AC7-E0EEA55D8A60}.Debug|x64.ActiveCfg = Debug|x64
{0CB44B34-A81F-4002-9AC7-E0EEA55D8A60}.Debug|x64.Build.0 = Debug|x64
{0CB44B34-A81F-4002-9AC7-E0EEA55D8A60}.Release|x64.ActiveCfg = Release|x64
{0CB44B34-A81F-4002-9AC7-E0EEA55D8A60}.Release|x64.Build.0 = Release|x64
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -283,10 +291,13 @@ Global
{5A049556-EA1E-4985-8D0B-3748E5D56F68} = {6B47BC47-0394-429E-9441-867EC23DFCD4} {5A049556-EA1E-4985-8D0B-3748E5D56F68} = {6B47BC47-0394-429E-9441-867EC23DFCD4}
{0D4DE16B-71FE-4B17-8310-3FFFB9E873B7} = {A8492D6D-5243-456E-8173-39B99F1FEA9C} {0D4DE16B-71FE-4B17-8310-3FFFB9E873B7} = {A8492D6D-5243-456E-8173-39B99F1FEA9C}
{5E512F97-14DB-4CEE-A495-BD8AED789521} = {A8492D6D-5243-456E-8173-39B99F1FEA9C} {5E512F97-14DB-4CEE-A495-BD8AED789521} = {A8492D6D-5243-456E-8173-39B99F1FEA9C}
{AF2911F4-33BF-465E-9298-E46D58B929EF} = {460EE42F-4178-49EF-9AC0-415599B80303}
{8B1C24E5-CC00-484C-9F6F-8FFCBDA3AA30} = {6B47BC47-0394-429E-9441-867EC23DFCD4} {8B1C24E5-CC00-484C-9F6F-8FFCBDA3AA30} = {6B47BC47-0394-429E-9441-867EC23DFCD4}
{6C6E48B1-5946-4754-9E31-E1C989EC25FE} = {C93DE675-A816-4412-8E9F-1EFE9AC07750} {6C6E48B1-5946-4754-9E31-E1C989EC25FE} = {C93DE675-A816-4412-8E9F-1EFE9AC07750}
{BFEB15A3-D252-4AF7-8CA8-B9396F374DDB} = {460EE42F-4178-49EF-9AC0-415599B80303}
{24AD09B4-6ABE-4A82-83E7-6A7799A88762} = {A8492D6D-5243-456E-8173-39B99F1FEA9C} {24AD09B4-6ABE-4A82-83E7-6A7799A88762} = {A8492D6D-5243-456E-8173-39B99F1FEA9C}
{D36B82DD-9114-44D2-A9C9-8DF0F8544BD5} = {A8492D6D-5243-456E-8173-39B99F1FEA9C} {D36B82DD-9114-44D2-A9C9-8DF0F8544BD5} = {A8492D6D-5243-456E-8173-39B99F1FEA9C}
{1FA0178C-F5E9-4B2E-A488-14F310F8DBD9} = {A8492D6D-5243-456E-8173-39B99F1FEA9C} {1FA0178C-F5E9-4B2E-A488-14F310F8DBD9} = {A8492D6D-5243-456E-8173-39B99F1FEA9C}
{0CB44B34-A81F-4002-9AC7-E0EEA55D8A60} = {460EE42F-4178-49EF-9AC0-415599B80303}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal