Skip to content
Snippets Groups Projects
Commit 07f23c39 authored by Alexander Gauggel's avatar Alexander Gauggel
Browse files

[#82] EVSM prototype

parent 423bc642
Branches
Tags
1 merge request!70Resolve "Voxel cone tracing"
...@@ -6,7 +6,7 @@ struct LightInfo{ ...@@ -6,7 +6,7 @@ struct LightInfo{
vec3 sunColor; vec3 sunColor;
float sunStrength; float sunStrength;
mat4 lightMatrix; mat4 lightMatrix;
float exponentialWarp; vec2 warps;
}; };
#endif // #ifndef LIGHT_INFO_INC #endif // #ifndef LIGHT_INFO_INC
\ No newline at end of file
...@@ -8,11 +8,12 @@ layout(set=0, binding=0) uniform LightInfoBuffer { ...@@ -8,11 +8,12 @@ layout(set=0, binding=0) uniform LightInfoBuffer {
LightInfo lightInfo; LightInfo lightInfo;
}; };
layout(location = 0) out float outExponentialDepth; layout(location = 0) out vec4 outMoments;
layout(location = 0) in vec4 passPos; layout(location = 0) in vec4 passPos;
void main() { void main() {
float z = passPos.z / passPos.w; float z = passPos.z / passPos.w;
outExponentialDepth = exp(z * lightInfo.exponentialWarp); vec2 zWarped = applyDepthWarp(z, lightInfo.warps);
outMoments = vec4(zWarped, zWarped*zWarped);
} }
\ No newline at end of file
...@@ -3,6 +3,32 @@ ...@@ -3,6 +3,32 @@
#include "lightInfo.inc" #include "lightInfo.inc"
vec2 applyDepthWarp(float z, vec2 warp){
z = 2 * z - 1;
float positive = exp( z * warp.x);
float negative = -exp(-z * warp.y);
return vec2(positive, negative);
}
float rescale(float a, float b, float v)
{
return clamp((v - a) / (b - a), 0, 1);
}
float reduceVSMBleeding(float shadowValue, float newMin)
{
return rescale(newMin, 1.0f, shadowValue);
}
float chebyshevInequality(float mean, float meanSquare, float sampleIn){
float variance = meanSquare - mean * mean;
float d = sampleIn - mean;
float pMax = clamp(variance / (variance + d*d), 0, 1);
pMax = reduceVSMBleeding(pMax, 0.1);
return pMax;
}
float shadowTest(vec3 worldPos, LightInfo lightInfo, texture2D shadowMap, sampler shadowMapSampler){ float shadowTest(vec3 worldPos, LightInfo lightInfo, texture2D shadowMap, sampler shadowMapSampler){
vec4 lightPos = lightInfo.lightMatrix * vec4(worldPos, 1); vec4 lightPos = lightInfo.lightMatrix * vec4(worldPos, 1);
lightPos /= lightPos.w; lightPos /= lightPos.w;
...@@ -12,11 +38,18 @@ float shadowTest(vec3 worldPos, LightInfo lightInfo, texture2D shadowMap, sample ...@@ -12,11 +38,18 @@ float shadowTest(vec3 worldPos, LightInfo lightInfo, texture2D shadowMap, sample
return 1; return 1;
} }
lightPos.z = clamp(lightPos.z, 0, 1); lightPos.z = clamp(lightPos.z, 0, 1);
vec2 warpedSample = applyDepthWarp(lightPos.z, lightInfo.warps);
// using exponential shadow mapping // using exponential variance shadow mapping
float shadowMapSample = texture(sampler2D(shadowMap, shadowMapSampler), lightPos.xy).r; vec4 shadowMapSample = texture(sampler2D(shadowMap, shadowMapSampler), lightPos.xy);
return clamp(exp(-lightInfo.exponentialWarp * lightPos.z) * shadowMapSample, 0, 1); vec2 positiveMoments = shadowMapSample.rb;
vec2 negativeMoments = shadowMapSample.ga;
float s1 = chebyshevInequality(positiveMoments.r, positiveMoments.g, warpedSample.x);
float s2 = chebyshevInequality(negativeMoments.r, negativeMoments.g, warpedSample.y);
return min(s1, s2);
} }
#endif // #ifndef SHADOW_MAPPING_INC #endif // #ifndef SHADOW_MAPPING_INC
\ No newline at end of file
...@@ -103,7 +103,7 @@ glm::mat4 computeShadowViewProjectionMatrix( ...@@ -103,7 +103,7 @@ glm::mat4 computeShadowViewProjectionMatrix(
return vulkanCorrectionMatrix * crop * view; return vulkanCorrectionMatrix * crop * view;
} }
const vk::Format shadowMapFormat = vk::Format::eR32Sfloat; const vk::Format shadowMapFormat = vk::Format::eR32G32B32A32Sfloat;
const vk::Format shadowMapDepthFormat = vk::Format::eD16Unorm; const vk::Format shadowMapDepthFormat = vk::Format::eD16Unorm;
const uint32_t shadowMapResolution = 2048; const uint32_t shadowMapResolution = 2048;
...@@ -156,7 +156,8 @@ void ShadowMapping::recordShadowMapRendering( ...@@ -156,7 +156,8 @@ void ShadowMapping::recordShadowMapRendering(
const glm::vec3& lightColor, const glm::vec3& lightColor,
float lightStrength, float lightStrength,
float maxShadowDistance, float maxShadowDistance,
float exponentialWarp, float exponentialWarpPositive,
float exponentialWarpNegative,
const std::vector<vkcv::Mesh>& meshes, const std::vector<vkcv::Mesh>& meshes,
const std::vector<glm::mat4>& modelMatrices, const std::vector<glm::mat4>& modelMatrices,
const vkcv::camera::Camera& camera, const vkcv::camera::Camera& camera,
...@@ -170,7 +171,8 @@ void ShadowMapping::recordShadowMapRendering( ...@@ -170,7 +171,8 @@ void ShadowMapping::recordShadowMapRendering(
std::cos(lightAngleRadian.x) * std::cos(lightAngleRadian.y), std::cos(lightAngleRadian.x) * std::cos(lightAngleRadian.y),
std::sin(lightAngleRadian.x), std::sin(lightAngleRadian.x),
std::cos(lightAngleRadian.x) * std::sin(lightAngleRadian.y))); std::cos(lightAngleRadian.x) * std::sin(lightAngleRadian.y)));
lightInfo.exponentialWarp = exponentialWarp; lightInfo.exponentialWarpPositive = exponentialWarpPositive;
lightInfo.exponentialWarpNegative = exponentialWarpNegative;
lightInfo.lightMatrix = computeShadowViewProjectionMatrix( lightInfo.lightMatrix = computeShadowViewProjectionMatrix(
lightInfo.direction, lightInfo.direction,
......
...@@ -13,7 +13,8 @@ struct LightInfo { ...@@ -13,7 +13,8 @@ struct LightInfo {
glm::vec3 sunColor; glm::vec3 sunColor;
float sunStrength; float sunStrength;
glm::mat4 lightMatrix; glm::mat4 lightMatrix;
float exponentialWarp; float exponentialWarpPositive;
float exponentialWarpNegative;
}; };
class ShadowMapping { class ShadowMapping {
...@@ -27,6 +28,7 @@ public: ...@@ -27,6 +28,7 @@ public:
float lightStrength, float lightStrength,
float maxShadowDistance, float maxShadowDistance,
float exponentialWarp, float exponentialWarp,
float exponentialWarpNegative,
const std::vector<vkcv::Mesh>& meshes, const std::vector<vkcv::Mesh>& meshes,
const std::vector<glm::mat4>& modelMatrices, const std::vector<glm::mat4>& modelMatrices,
const vkcv::camera::Camera& camera, const vkcv::camera::Camera& camera,
......
...@@ -325,11 +325,12 @@ int main(int argc, const char** argv) { ...@@ -325,11 +325,12 @@ int main(int argc, const char** argv) {
vkcv::gui::GUI gui(core, window); vkcv::gui::GUI gui(core, window);
glm::vec2 lightAnglesDegree = glm::vec2(90.f, 0.f); glm::vec2 lightAnglesDegree = glm::vec2(90.f, 0.f);
glm::vec3 lightColor = glm::vec3(1); glm::vec3 lightColor = glm::vec3(1);
float lightStrength = 25.f; float lightStrength = 25.f;
float maxShadowDistance = 30.f; float maxShadowDistance = 30.f;
float shadowExponentialWarp = 60.f; float shadowExponentialWarpPositive = 60.f;
float shadowExponentialWarpNegative = 60.f;
int voxelVisualisationMip = 0; int voxelVisualisationMip = 0;
float voxelizationExtent = 30.f; float voxelizationExtent = 30.f;
...@@ -378,7 +379,8 @@ int main(int argc, const char** argv) { ...@@ -378,7 +379,8 @@ int main(int argc, const char** argv) {
lightColor, lightColor,
lightStrength, lightStrength,
maxShadowDistance, maxShadowDistance,
shadowExponentialWarp, shadowExponentialWarpPositive,
shadowExponentialWarpNegative,
meshes, meshes,
modelMatrices, modelMatrices,
cameraManager.getActiveCamera(), cameraManager.getActiveCamera(),
...@@ -446,7 +448,8 @@ int main(int argc, const char** argv) { ...@@ -446,7 +448,8 @@ int main(int argc, const char** argv) {
ImGui::DragFloat("Sun strength", &lightStrength); ImGui::DragFloat("Sun strength", &lightStrength);
ImGui::DragFloat("Max shadow distance", &maxShadowDistance); ImGui::DragFloat("Max shadow distance", &maxShadowDistance);
maxShadowDistance = std::max(maxShadowDistance, 1.f); maxShadowDistance = std::max(maxShadowDistance, 1.f);
ImGui::DragFloat("Shadow exponential warp", &shadowExponentialWarp); ImGui::DragFloat("Shadow exponential warp positive", &shadowExponentialWarpPositive);
ImGui::DragFloat("Shadow exponential warp negative", &shadowExponentialWarpNegative);
ImGui::Checkbox("Draw voxel visualisation", &renderVoxelVis); ImGui::Checkbox("Draw voxel visualisation", &renderVoxelVis);
ImGui::SliderInt("Visualisation mip", &voxelVisualisationMip, 0, 7); ImGui::SliderInt("Visualisation mip", &voxelVisualisationMip, 0, 7);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment