Added cascaded shadow mapping example
This commit is contained in:
parent
f4281096ea
commit
3c150e18f3
14 changed files with 1081 additions and 0 deletions
17
data/shaders/shadowmappingcascade/debugshadowmap.frag
Normal file
17
data/shaders/shadowmappingcascade/debugshadowmap.frag
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#version 450
|
||||
|
||||
layout (binding = 1) uniform sampler2DArray shadowMap;
|
||||
|
||||
layout (location = 0) in vec2 inUV;
|
||||
|
||||
layout (location = 0) out vec4 outFragColor;
|
||||
|
||||
layout(push_constant) uniform PushConsts {
|
||||
uint cascadeIndex;
|
||||
} pushConsts;
|
||||
|
||||
void main()
|
||||
{
|
||||
float depth = texture(shadowMap, vec3(inUV, float(pushConsts.cascadeIndex))).r;
|
||||
outFragColor = vec4(vec3((depth)), 1.0);
|
||||
}
|
||||
BIN
data/shaders/shadowmappingcascade/debugshadowmap.frag.spv
Normal file
BIN
data/shaders/shadowmappingcascade/debugshadowmap.frag.spv
Normal file
Binary file not shown.
14
data/shaders/shadowmappingcascade/debugshadowmap.vert
Normal file
14
data/shaders/shadowmappingcascade/debugshadowmap.vert
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#version 450
|
||||
|
||||
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);
|
||||
}
|
||||
BIN
data/shaders/shadowmappingcascade/debugshadowmap.vert.spv
Normal file
BIN
data/shaders/shadowmappingcascade/debugshadowmap.vert.spv
Normal file
Binary file not shown.
5
data/shaders/shadowmappingcascade/depthpass.frag
Normal file
5
data/shaders/shadowmappingcascade/depthpass.frag
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#version 450
|
||||
|
||||
void main()
|
||||
{
|
||||
}
|
||||
BIN
data/shaders/shadowmappingcascade/depthpass.frag.spv
Normal file
BIN
data/shaders/shadowmappingcascade/depthpass.frag.spv
Normal file
Binary file not shown.
23
data/shaders/shadowmappingcascade/depthpass.vert
Normal file
23
data/shaders/shadowmappingcascade/depthpass.vert
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#version 450
|
||||
|
||||
layout (location = 0) in vec3 inPos;
|
||||
|
||||
// todo: pass via specialization constant
|
||||
#define SHADOW_MAP_CASCADE_COUNT 4
|
||||
|
||||
layout(push_constant) uniform PushConsts {
|
||||
uint cascadeIndex;
|
||||
} pushConsts;
|
||||
|
||||
layout (binding = 0) uniform UBO {
|
||||
mat4[SHADOW_MAP_CASCADE_COUNT] cascadeViewProjMat;
|
||||
} ubo;
|
||||
|
||||
out gl_PerVertex {
|
||||
vec4 gl_Position;
|
||||
};
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = ubo.cascadeViewProjMat[pushConsts.cascadeIndex] * vec4(inPos, 1.0);
|
||||
}
|
||||
BIN
data/shaders/shadowmappingcascade/depthpass.vert.spv
Normal file
BIN
data/shaders/shadowmappingcascade/depthpass.vert.spv
Normal file
Binary file not shown.
116
data/shaders/shadowmappingcascade/scene.frag
Normal file
116
data/shaders/shadowmappingcascade/scene.frag
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
#version 450
|
||||
|
||||
#define SHADOW_MAP_CASCADE_COUNT 4
|
||||
|
||||
layout (binding = 1) uniform sampler2DArray shadowMap;
|
||||
|
||||
layout (location = 0) in vec3 inNormal;
|
||||
layout (location = 1) in vec3 inColor;
|
||||
layout (location = 2) in vec3 inViewPos;
|
||||
layout (location = 3) in vec3 inPos;
|
||||
|
||||
layout (constant_id = 0) const int enablePCF = 0;
|
||||
|
||||
layout (location = 0) out vec4 outFragColor;
|
||||
|
||||
#define ambient 0.1
|
||||
|
||||
layout (binding = 2) uniform UBO {
|
||||
vec4 cascadeSplits;
|
||||
mat4 cascadeViewProjMat[SHADOW_MAP_CASCADE_COUNT];
|
||||
mat4 inverseViewMat;
|
||||
vec3 lightDir;
|
||||
float _pad;
|
||||
int colorCascades;
|
||||
} ubo;
|
||||
|
||||
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
|
||||
);
|
||||
|
||||
float textureProj(vec4 P, vec2 offset, uint cascadeIndex)
|
||||
{
|
||||
float shadow = 1.0;
|
||||
float bias = 0.005;
|
||||
|
||||
vec4 shadowCoord = P / P.w;
|
||||
if ( shadowCoord.z > -1.0 && shadowCoord.z < 1.0 ) {
|
||||
float dist = texture(shadowMap, vec3(shadowCoord.st + offset, cascadeIndex)).r;
|
||||
if (shadowCoord.w > 0 && dist < shadowCoord.z - bias) {
|
||||
shadow = ambient;
|
||||
}
|
||||
}
|
||||
return shadow;
|
||||
|
||||
}
|
||||
|
||||
float filterPCF(vec4 sc, uint cascadeIndex)
|
||||
{
|
||||
ivec2 texDim = textureSize(shadowMap, 0).xy;
|
||||
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), cascadeIndex);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return shadowFactor / count;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
// Get cascade index for the current fragment's view position
|
||||
uint cascadeIndex = 0;
|
||||
for(uint i = 0; i < SHADOW_MAP_CASCADE_COUNT - 1; ++i) {
|
||||
if(inViewPos.z < ubo.cascadeSplits[i]) {
|
||||
cascadeIndex = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Depth compare for shadowing
|
||||
vec4 shadowCoord = (biasMat * ubo.cascadeViewProjMat[cascadeIndex]) * vec4(inPos, 1.0);
|
||||
|
||||
float shadow = 0;
|
||||
if (enablePCF == 1) {
|
||||
shadow = filterPCF(shadowCoord / shadowCoord.w, cascadeIndex);
|
||||
} else {
|
||||
shadow = textureProj(shadowCoord / shadowCoord.w, vec2(0.0), cascadeIndex);
|
||||
}
|
||||
|
||||
// Directional light
|
||||
vec3 N = normalize(inNormal);
|
||||
vec3 L = normalize(-ubo.lightDir);
|
||||
vec3 H = normalize(L + inViewPos);
|
||||
float diffuse = max(dot(N, L), 0.0);
|
||||
vec3 lightColor = vec3(1.0);
|
||||
outFragColor.rgb = max(lightColor * (diffuse ), vec3(0.0));
|
||||
outFragColor.rgb *= shadow;
|
||||
|
||||
// Color cascades (if enabled)
|
||||
if (ubo.colorCascades == 1) {
|
||||
switch(cascadeIndex) {
|
||||
case 0 :
|
||||
outFragColor.rgb *= vec3(1.0f, 0.0f, 0.0f);
|
||||
break;
|
||||
case 1 :
|
||||
outFragColor.rgb *= vec3(0.0f, 1.0f, 0.0f);
|
||||
break;
|
||||
case 2 :
|
||||
outFragColor.rgb *= vec3(0.0f, 0.0f, 1.0f);
|
||||
break;
|
||||
case 3 :
|
||||
outFragColor.rgb *= vec3(1.0f, 1.0f, 0.0f);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
data/shaders/shadowmappingcascade/scene.frag.spv
Normal file
BIN
data/shaders/shadowmappingcascade/scene.frag.spv
Normal file
Binary file not shown.
31
data/shaders/shadowmappingcascade/scene.vert
Normal file
31
data/shaders/shadowmappingcascade/scene.vert
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#version 450
|
||||
|
||||
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;
|
||||
} ubo;
|
||||
|
||||
layout (location = 0) out vec3 outNormal;
|
||||
layout (location = 1) out vec3 outColor;
|
||||
layout (location = 2) out vec3 outViewPos;
|
||||
layout (location = 3) out vec3 outPos;
|
||||
|
||||
out gl_PerVertex {
|
||||
vec4 gl_Position;
|
||||
};
|
||||
|
||||
void main()
|
||||
{
|
||||
outColor = inColor;
|
||||
outNormal = inNormal;
|
||||
outPos = inPos;
|
||||
outViewPos = (ubo.view * vec4(inPos.xyz, 1.0)).xyz;
|
||||
gl_Position = ubo.projection * ubo.view * ubo.model * vec4(inPos.xyz, 1.0);
|
||||
}
|
||||
|
||||
BIN
data/shaders/shadowmappingcascade/scene.vert.spv
Normal file
BIN
data/shaders/shadowmappingcascade/scene.vert.spv
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue