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

[#82] Shadow blur, but wrong

parent 53ed92ff
No related branches found
No related tags found
1 merge request!70Resolve "Voxel cone tracing"
...@@ -6,9 +6,9 @@ ...@@ -6,9 +6,9 @@
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
layout(set=0, binding=0) uniform texture2DMS srcTexture; layout(set=0, binding=0) uniform texture2DMS srcTexture;
layout(set=0, binding=1) uniform sampler depthSampler; layout(set=0, binding=1) uniform sampler depthSampler;
layout(set=0, binding=2, r11f_g11f_b10f) uniform image2D outImage; layout(set=0, binding=2, rgba16) uniform image2D outImage;
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
......
...@@ -6,7 +6,6 @@ struct LightInfo{ ...@@ -6,7 +6,6 @@ struct LightInfo{
vec3 sunColor; vec3 sunColor;
float sunStrength; float sunStrength;
mat4 lightMatrix; mat4 lightMatrix;
vec2 warps;
}; };
#endif // #ifndef LIGHT_INFO_INC #endif // #ifndef LIGHT_INFO_INC
\ No newline at end of file
#version 450
#extension GL_GOOGLE_include_directive : enable
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
layout(set=0, binding=0) uniform texture2D srcTexture;
layout(set=0, binding=1) uniform sampler depthSampler;
layout(set=0, binding=2, rgba16) uniform image2D outImage;
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
void main(){
if(any(greaterThanEqual(gl_GlobalInvocationID.xy, imageSize(outImage)))){
return;
}
ivec2 coord = ivec2(gl_GlobalInvocationID.xy);
int blurRadius = 0;
int minOffset = -(blurRadius-1) / 2;
int maxOffset = -minOffset;
vec2 pixelSize = vec2(1) / textureSize(sampler2D(srcTexture, depthSampler), 0);
float wTotal = 0;
vec4 moments = vec4(0);
float weights1D[3] = { 0.5, 0.25, 0.125 }; // gaussian
for(int x = minOffset; x <= maxOffset; x++){
for(int y = minOffset; y <= maxOffset; y++){
vec2 uv = (coord + ivec2(x, y)) * pixelSize;
uv += 0.5 * pixelSize * sign(vec2(x, y)); // half pixel shift to take advantage of bilinear filtering
float w = weights1D[abs(x)] * weights1D[abs(y)];
moments += w * texture(sampler2D(srcTexture, depthSampler), uv);
wTotal += w;
}
}
moments /= wTotal;
imageStore(outImage, coord, moments);
}
\ No newline at end of file
...@@ -25,6 +25,16 @@ vkcv::ShaderProgram loadDepthToMomentsShader() { ...@@ -25,6 +25,16 @@ vkcv::ShaderProgram loadDepthToMomentsShader() {
return shader; return shader;
} }
vkcv::ShaderProgram loadShadowBlurShader() {
vkcv::ShaderProgram shader;
vkcv::shader::GLSLCompiler compiler;
compiler.compile(vkcv::ShaderStage::COMPUTE, "resources/shaders/shadowBlur.comp",
[&](vkcv::ShaderStage shaderStage, const std::filesystem::path& path) {
shader.addShader(shaderStage, path);
});
return shader;
}
glm::mat4 computeShadowViewProjectionMatrix( glm::mat4 computeShadowViewProjectionMatrix(
const glm::vec3& lightDirection, const glm::vec3& lightDirection,
const vkcv::camera::Camera& camera, const vkcv::camera::Camera& camera,
...@@ -121,6 +131,7 @@ const vkcv::Multisampling msaa = vkcv::Multisampling::MSAA4 ...@@ -121,6 +131,7 @@ const vkcv::Multisampling msaa = vkcv::Multisampling::MSAA4
ShadowMapping::ShadowMapping(vkcv::Core* corePtr, const vkcv::VertexLayout& vertexLayout) : ShadowMapping::ShadowMapping(vkcv::Core* corePtr, const vkcv::VertexLayout& vertexLayout) :
m_corePtr(corePtr), m_corePtr(corePtr),
m_shadowMap(corePtr->createImage(shadowMapFormat, shadowMapResolution, shadowMapResolution, 1, false, true)), m_shadowMap(corePtr->createImage(shadowMapFormat, shadowMapResolution, shadowMapResolution, 1, false, true)),
m_shadowMapIntermediate(corePtr->createImage(shadowMapFormat, shadowMapResolution, shadowMapResolution, 1, false, true)),
m_shadowMapDepth(corePtr->createImage(shadowMapDepthFormat, shadowMapResolution, shadowMapResolution, 1, false, false, false, msaa)), m_shadowMapDepth(corePtr->createImage(shadowMapDepthFormat, shadowMapResolution, shadowMapResolution, 1, false, false, false, msaa)),
m_lightInfoBuffer(corePtr->createBuffer<LightInfo>(vkcv::BufferType::UNIFORM, sizeof(glm::vec3))){ m_lightInfoBuffer(corePtr->createBuffer<LightInfo>(vkcv::BufferType::UNIFORM, sizeof(glm::vec3))){
...@@ -162,8 +173,19 @@ ShadowMapping::ShadowMapping(vkcv::Core* corePtr, const vkcv::VertexLayout& vert ...@@ -162,8 +173,19 @@ ShadowMapping::ShadowMapping(vkcv::Core* corePtr, const vkcv::VertexLayout& vert
vkcv::DescriptorWrites depthToMomentDescriptorWrites; vkcv::DescriptorWrites depthToMomentDescriptorWrites;
depthToMomentDescriptorWrites.sampledImageWrites = { vkcv::SampledImageDescriptorWrite(0, m_shadowMapDepth.getHandle()) }; depthToMomentDescriptorWrites.sampledImageWrites = { vkcv::SampledImageDescriptorWrite(0, m_shadowMapDepth.getHandle()) };
depthToMomentDescriptorWrites.samplerWrites = { vkcv::SamplerDescriptorWrite(1, m_shadowSampler) }; depthToMomentDescriptorWrites.samplerWrites = { vkcv::SamplerDescriptorWrite(1, m_shadowSampler) };
depthToMomentDescriptorWrites.storageImageWrites = { vkcv::StorageImageDescriptorWrite(2, m_shadowMap.getHandle()) }; depthToMomentDescriptorWrites.storageImageWrites = { vkcv::StorageImageDescriptorWrite(2, m_shadowMapIntermediate.getHandle()) };
corePtr->writeDescriptorSet(m_depthToMomentsDescriptorSet, depthToMomentDescriptorWrites); corePtr->writeDescriptorSet(m_depthToMomentsDescriptorSet, depthToMomentDescriptorWrites);
// shadow blur
vkcv::ShaderProgram shadowBlurShader = loadShadowBlurShader();
m_shadowBlurDescriptorSet = corePtr->createDescriptorSet(shadowBlurShader.getReflectedDescriptors()[0]);
m_shadowBlurPipe = corePtr->createComputePipeline(shadowBlurShader, { corePtr->getDescriptorSet(m_shadowBlurDescriptorSet).layout });
vkcv::DescriptorWrites shadowBlurDescriptorWrites;
shadowBlurDescriptorWrites.sampledImageWrites = { vkcv::SampledImageDescriptorWrite(0, m_shadowMapIntermediate.getHandle()) };
shadowBlurDescriptorWrites.samplerWrites = { vkcv::SamplerDescriptorWrite(1, m_shadowSampler) };
shadowBlurDescriptorWrites.storageImageWrites = { vkcv::StorageImageDescriptorWrite(2, m_shadowMap.getHandle()) };
corePtr->writeDescriptorSet(m_shadowBlurDescriptorSet, shadowBlurDescriptorWrites);
} }
void ShadowMapping::recordShadowMapRendering( void ShadowMapping::recordShadowMapRendering(
...@@ -172,8 +194,6 @@ void ShadowMapping::recordShadowMapRendering( ...@@ -172,8 +194,6 @@ void ShadowMapping::recordShadowMapRendering(
const glm::vec3& lightColor, const glm::vec3& lightColor,
float lightStrength, float lightStrength,
float maxShadowDistance, float maxShadowDistance,
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,
...@@ -187,8 +207,6 @@ void ShadowMapping::recordShadowMapRendering( ...@@ -187,8 +207,6 @@ 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.exponentialWarpPositive = exponentialWarpPositive;
lightInfo.exponentialWarpNegative = exponentialWarpNegative;
lightInfo.lightMatrix = computeShadowViewProjectionMatrix( lightInfo.lightMatrix = computeShadowViewProjectionMatrix(
lightInfo.direction, lightInfo.direction,
...@@ -226,13 +244,23 @@ void ShadowMapping::recordShadowMapRendering( ...@@ -226,13 +244,23 @@ void ShadowMapping::recordShadowMapRendering(
const uint32_t msaaSampleCount = msaaToSampleCount(msaa); const uint32_t msaaSampleCount = msaaToSampleCount(msaa);
m_corePtr->prepareImageForStorage(cmdStream, m_shadowMap.getHandle()); m_corePtr->prepareImageForStorage(cmdStream, m_shadowMapIntermediate.getHandle());
m_corePtr->recordComputeDispatchToCmdStream( m_corePtr->recordComputeDispatchToCmdStream(
cmdStream, cmdStream,
m_depthToMomentsPipe, m_depthToMomentsPipe,
dispatchCount, dispatchCount,
{ vkcv::DescriptorSetUsage(0, m_corePtr->getDescriptorSet(m_depthToMomentsDescriptorSet).vulkanHandle) }, { vkcv::DescriptorSetUsage(0, m_corePtr->getDescriptorSet(m_depthToMomentsDescriptorSet).vulkanHandle) },
vkcv::PushConstantData((void*)&msaaSampleCount, sizeof(msaaSampleCount))); vkcv::PushConstantData((void*)&msaaSampleCount, sizeof(msaaSampleCount)));
m_corePtr->prepareImageForSampling(cmdStream, m_shadowMapIntermediate.getHandle());
// blur
m_corePtr->prepareImageForStorage(cmdStream, m_shadowMap.getHandle());
m_corePtr->recordComputeDispatchToCmdStream(
cmdStream,
m_shadowBlurPipe,
dispatchCount,
{ vkcv::DescriptorSetUsage(0, m_corePtr->getDescriptorSet(m_shadowBlurDescriptorSet).vulkanHandle) },
vkcv::PushConstantData(nullptr, 0));
m_corePtr->prepareImageForSampling(cmdStream, m_shadowMap.getHandle()); m_corePtr->prepareImageForSampling(cmdStream, m_shadowMap.getHandle());
} }
......
...@@ -13,8 +13,6 @@ struct LightInfo { ...@@ -13,8 +13,6 @@ struct LightInfo {
glm::vec3 sunColor; glm::vec3 sunColor;
float sunStrength; float sunStrength;
glm::mat4 lightMatrix; glm::mat4 lightMatrix;
float exponentialWarpPositive;
float exponentialWarpNegative;
}; };
class ShadowMapping { class ShadowMapping {
...@@ -27,8 +25,6 @@ public: ...@@ -27,8 +25,6 @@ public:
const glm::vec3& lightColor, const glm::vec3& lightColor,
float lightStrength, float lightStrength,
float maxShadowDistance, float maxShadowDistance,
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,
...@@ -43,6 +39,7 @@ private: ...@@ -43,6 +39,7 @@ private:
vkcv::Core* m_corePtr; vkcv::Core* m_corePtr;
vkcv::Image m_shadowMap; vkcv::Image m_shadowMap;
vkcv::Image m_shadowMapIntermediate;
vkcv::Image m_shadowMapDepth; vkcv::Image m_shadowMapDepth;
vkcv::SamplerHandle m_shadowSampler; vkcv::SamplerHandle m_shadowSampler;
vkcv::Buffer<LightInfo> m_lightInfoBuffer; vkcv::Buffer<LightInfo> m_lightInfoBuffer;
...@@ -52,4 +49,7 @@ private: ...@@ -52,4 +49,7 @@ private:
vkcv::PipelineHandle m_depthToMomentsPipe; vkcv::PipelineHandle m_depthToMomentsPipe;
vkcv::DescriptorSetHandle m_depthToMomentsDescriptorSet; vkcv::DescriptorSetHandle m_depthToMomentsDescriptorSet;
vkcv::PipelineHandle m_shadowBlurPipe;
vkcv::DescriptorSetHandle m_shadowBlurDescriptorSet;
}; };
\ No newline at end of file
...@@ -361,8 +361,6 @@ int main(int argc, const char** argv) { ...@@ -361,8 +361,6 @@ int main(int argc, const char** argv) {
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 shadowExponentialWarpPositive = 60.f;
float shadowExponentialWarpNegative = 60.f;
int voxelVisualisationMip = 0; int voxelVisualisationMip = 0;
float voxelizationExtent = 30.f; float voxelizationExtent = 30.f;
...@@ -427,8 +425,6 @@ int main(int argc, const char** argv) { ...@@ -427,8 +425,6 @@ int main(int argc, const char** argv) {
lightColor, lightColor,
lightStrength, lightStrength,
maxShadowDistance, maxShadowDistance,
shadowExponentialWarpPositive,
shadowExponentialWarpNegative,
meshes, meshes,
modelMatrices, modelMatrices,
cameraManager.getActiveCamera(), cameraManager.getActiveCamera(),
...@@ -520,8 +516,6 @@ int main(int argc, const char** argv) { ...@@ -520,8 +516,6 @@ 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 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.
Finish editing this message first!
Please register or to comment