Added shadow mapping example
This commit is contained in:
parent
9ecbaca69f
commit
e45c90f180
19 changed files with 3577 additions and 0 deletions
688
data/models/vulkanscene_shadow.dae
Normal file
688
data/models/vulkanscene_shadow.dae
Normal file
File diff suppressed because one or more lines are too long
7
data/shaders/shadowmapping/generate-spirv.bat
Normal file
7
data/shaders/shadowmapping/generate-spirv.bat
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
glslangvalidator -V offscreen.vert -o offscreen.vert.spv
|
||||
glslangvalidator -V offscreen.frag -o offscreen.frag.spv
|
||||
glslangvalidator -V quad.vert -o quad.vert.spv
|
||||
glslangvalidator -V quad.frag -o quad.frag.spv
|
||||
glslangvalidator -V scene.vert -o scene.vert.spv
|
||||
glslangvalidator -V scene.frag -o scene.frag.spv
|
||||
|
||||
13
data/shaders/shadowmapping/offscreen.frag
Normal file
13
data/shaders/shadowmapping/offscreen.frag
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#version 450
|
||||
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_ARB_shading_language_420pack : enable
|
||||
|
||||
layout(location = 0) out vec4 color;
|
||||
//layout(location = 0) out float fragmentdepth;
|
||||
|
||||
void main()
|
||||
{
|
||||
// fragmentdepth = gl_FragCoord.z;
|
||||
color = vec4(1.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
BIN
data/shaders/shadowmapping/offscreen.frag.spv
Normal file
BIN
data/shaders/shadowmapping/offscreen.frag.spv
Normal file
Binary file not shown.
16
data/shaders/shadowmapping/offscreen.vert
Normal file
16
data/shaders/shadowmapping/offscreen.vert
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#version 450
|
||||
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_ARB_shading_language_420pack : enable
|
||||
|
||||
layout (location = 0) in vec3 inPos;
|
||||
|
||||
layout (binding = 0) uniform UBO
|
||||
{
|
||||
mat4 depthMVP;
|
||||
} ubo;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = ubo.depthMVP * vec4(inPos, 1.0);
|
||||
}
|
||||
BIN
data/shaders/shadowmapping/offscreen.vert.spv
Normal file
BIN
data/shaders/shadowmapping/offscreen.vert.spv
Normal file
Binary file not shown.
24
data/shaders/shadowmapping/quad.frag
Normal file
24
data/shaders/shadowmapping/quad.frag
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#version 450
|
||||
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_ARB_shading_language_420pack : enable
|
||||
|
||||
layout (binding = 1) uniform sampler2D samplerColor;
|
||||
|
||||
layout (location = 0) in vec2 inUV;
|
||||
|
||||
layout (location = 0) out vec4 outFragColor;
|
||||
|
||||
float LinearizeDepth(float depth)
|
||||
{
|
||||
float n = 1.0; // camera z near
|
||||
float f = 128.0; // camera z far
|
||||
float z = depth;
|
||||
return (2.0 * n) / (f + n - z * (f - n));
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
float depth = texture(samplerColor, inUV).r;
|
||||
outFragColor = vec4(vec3(1.0-LinearizeDepth(depth)), 1.0);
|
||||
}
|
||||
BIN
data/shaders/shadowmapping/quad.frag.spv
Normal file
BIN
data/shaders/shadowmapping/quad.frag.spv
Normal file
Binary file not shown.
21
data/shaders/shadowmapping/quad.vert
Normal file
21
data/shaders/shadowmapping/quad.vert
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#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;
|
||||
|
||||
void main()
|
||||
{
|
||||
outUV = inUV;
|
||||
gl_Position = ubo.projection * ubo.model * vec4(inPos.xyz, 1.0);
|
||||
}
|
||||
BIN
data/shaders/shadowmapping/quad.vert.spv
Normal file
BIN
data/shaders/shadowmapping/quad.vert.spv
Normal file
Binary file not shown.
69
data/shaders/shadowmapping/scene.frag
Normal file
69
data/shaders/shadowmapping/scene.frag
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#version 450
|
||||
|
||||
#extension GL_ARB_separate_shader_objects : enable
|
||||
#extension GL_ARB_shading_language_420pack : enable
|
||||
|
||||
layout (binding = 1) uniform sampler2D shadowMap;
|
||||
|
||||
layout (location = 0) in vec3 inNormal;
|
||||
layout (location = 1) in vec3 inColor;
|
||||
layout (location = 2) in vec3 inViewVec;
|
||||
layout (location = 3) in vec3 inLightVec;
|
||||
layout (location = 4) in vec4 inShadowCoord;
|
||||
|
||||
layout (location = 0) out vec4 outFragColor;
|
||||
|
||||
#define ambient 0.1
|
||||
|
||||
float textureProj(vec4 P, vec2 off)
|
||||
{
|
||||
float shadow = 1.0;
|
||||
vec4 shadowCoord = P / P.w;
|
||||
if ( shadowCoord.z > -1.0 && shadowCoord.z < 1.0 )
|
||||
{
|
||||
float dist = texture( shadowMap, shadowCoord.st + off ).r;
|
||||
if ( shadowCoord.w > 0.0 && dist < shadowCoord.z )
|
||||
{
|
||||
shadow = ambient;
|
||||
}
|
||||
}
|
||||
return shadow;
|
||||
}
|
||||
|
||||
float filterPCF(vec4 sc)
|
||||
{
|
||||
ivec2 texDim = textureSize(shadowMap, 0);
|
||||
float scale = 1.5;
|
||||
float dx = scale * 1.0 / float(texDim.x);
|
||||
float dy = scale * 1.0 / float(texDim.y);
|
||||
|
||||
float shadowFactor = 0.0;
|
||||
int count = 0;
|
||||
int range = 1;
|
||||
|
||||
for (int x = -range; x <= range; x++)
|
||||
{
|
||||
for (int y = -range; y <= range; y++)
|
||||
{
|
||||
shadowFactor += textureProj(sc, vec2(dx*x, dy*y));
|
||||
count++;
|
||||
}
|
||||
|
||||
}
|
||||
return shadowFactor / count;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
float shadow = filterPCF(inShadowCoord / inShadowCoord.w);
|
||||
|
||||
vec3 N = normalize(inNormal);
|
||||
vec3 L = normalize(inLightVec);
|
||||
vec3 V = normalize(inViewVec);
|
||||
vec3 R = normalize(-reflect(L, N));
|
||||
vec3 diffuse = max(dot(N, L), ambient) * inColor;
|
||||
// vec3 specular = pow(max(dot(R, V), 0.0), 50.0) * vec3(0.75);
|
||||
|
||||
outFragColor = vec4(diffuse * shadow, 1.0);
|
||||
|
||||
}
|
||||
BIN
data/shaders/shadowmapping/scene.frag.spv
Normal file
BIN
data/shaders/shadowmapping/scene.frag.spv
Normal file
Binary file not shown.
47
data/shaders/shadowmapping/scene.vert
Normal file
47
data/shaders/shadowmapping/scene.vert
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#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 (location = 2) in vec3 inColor;
|
||||
layout (location = 3) in vec3 inNormal;
|
||||
|
||||
layout (binding = 0) uniform UBO
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 view;
|
||||
mat4 model;
|
||||
mat4 lightSpace;
|
||||
vec3 lightPos;
|
||||
} ubo;
|
||||
|
||||
layout (location = 0) out vec3 outNormal;
|
||||
layout (location = 1) out vec3 outColor;
|
||||
layout (location = 2) out vec3 outViewVec;
|
||||
layout (location = 3) out vec3 outLightVec;
|
||||
layout (location = 4) out vec4 outShadowCoord;
|
||||
|
||||
|
||||
const mat4 biasMat = mat4(
|
||||
0.5, 0.0, 0.0, 0.0,
|
||||
0.0, 0.5, 0.0, 0.0,
|
||||
0.0, 0.0, 1.0, 0.0,
|
||||
0.5, 0.5, 0.0, 1.0 );
|
||||
|
||||
void main()
|
||||
{
|
||||
outColor = inColor;
|
||||
outNormal = inNormal;
|
||||
|
||||
gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPos.xyz, 1.0);
|
||||
|
||||
vec4 pos = ubo.model * vec4(inPos, 1.0);
|
||||
outNormal = mat3(ubo.model) * inNormal;
|
||||
outLightVec = normalize(ubo.lightPos - inPos);
|
||||
outViewVec = -pos.xyz;
|
||||
|
||||
outShadowCoord = ( biasMat * ubo.lightSpace * ubo.model ) * vec4(inPos, 1.0);
|
||||
}
|
||||
|
||||
BIN
data/shaders/shadowmapping/scene.vert.spv
Normal file
BIN
data/shaders/shadowmapping/scene.vert.spv
Normal file
Binary file not shown.
1358
shadowmapping/shadowmap.cpp
Normal file
1358
shadowmapping/shadowmap.cpp
Normal file
File diff suppressed because it is too large
Load diff
1194
shadowmapping/shadowmapping.cpp
Normal file
1194
shadowmapping/shadowmapping.cpp
Normal file
File diff suppressed because it is too large
Load diff
92
shadowmapping/shadowmapping.vcxproj
Normal file
92
shadowmapping/shadowmapping.vcxproj
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
<?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>{BF22CE41-AA83-45FE-813A-451E2EDF58D9}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
</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\</IntDir>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;VK_PROTOTYPES;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_PROTOTYPES;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="shadowmapping.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\base\vulkandebug.h" />
|
||||
<ClInclude Include="..\base\vulkanexamplebase.h" />
|
||||
<ClInclude Include="..\base\vulkantools.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
42
shadowmapping/shadowmapping.vcxproj.filters
Normal file
42
shadowmapping/shadowmapping.vcxproj.filters
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
<?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>
|
||||
</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="shadowmapping.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>
|
||||
</Project>
|
||||
|
|
@ -55,6 +55,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "distancefieldfonts", "dista
|
|||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "multithreading", "multithreading\multithreading.vcxproj", "{F5703C3C-F495-4D5C-BAA5-A90C4E20C7B7}"
|
||||
EndProject
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "shadowmapping", "shadowmapping\shadowmapping.vcxproj", "{BF22CE41-AA83-45FE-813A-451E2EDF58D9}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
|
|
@ -165,6 +167,10 @@ Global
|
|||
{F5703C3C-F495-4D5C-BAA5-A90C4E20C7B7}.Debug|x64.Build.0 = Debug|x64
|
||||
{F5703C3C-F495-4D5C-BAA5-A90C4E20C7B7}.Release|x64.ActiveCfg = Release|x64
|
||||
{F5703C3C-F495-4D5C-BAA5-A90C4E20C7B7}.Release|x64.Build.0 = Release|x64
|
||||
{BF22CE41-AA83-45FE-813A-451E2EDF58D9}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{BF22CE41-AA83-45FE-813A-451E2EDF58D9}.Debug|x64.Build.0 = Debug|x64
|
||||
{BF22CE41-AA83-45FE-813A-451E2EDF58D9}.Release|x64.ActiveCfg = Release|x64
|
||||
{BF22CE41-AA83-45FE-813A-451E2EDF58D9}.Release|x64.Build.0 = Release|x64
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue