Pass depth range to depth map visualization shader

Fixes #895
This commit is contained in:
Sascha Willems 2022-05-08 11:43:06 +02:00
parent 8e98d10f74
commit 8c376121c3
16 changed files with 46 additions and 16 deletions

View file

@ -6,10 +6,21 @@ layout (location = 0) in vec2 inUV;
layout (location = 0) out vec4 outFragColor;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 view;
mat4 model;
mat4 lightSpace;
vec4 lightPos;
float zNear;
float zFar;
} ubo;
float LinearizeDepth(float depth)
{
float n = 1.0; // camera z near
float f = 128.0; // camera z far
float n = ubo.zNear;
float f = ubo.zFar;
float z = depth;
return (2.0 * n) / (f + n - z * (f - n));
}

View file

@ -11,7 +11,9 @@ layout (binding = 0) uniform UBO
mat4 view;
mat4 model;
mat4 lightSpace;
vec3 lightPos;
vec4 lightPos;
float zNear;
float zFar;
} ubo;
layout (location = 0) out vec3 outNormal;
@ -20,11 +22,6 @@ layout (location = 2) out vec3 outViewVec;
layout (location = 3) out vec3 outLightVec;
layout (location = 4) out vec4 outShadowCoord;
out gl_PerVertex
{
vec4 gl_Position;
};
const mat4 biasMat = mat4(
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
@ -40,7 +37,7 @@ void main()
vec4 pos = ubo.model * vec4(inPos, 1.0);
outNormal = mat3(ubo.model) * inNormal;
outLightVec = normalize(ubo.lightPos - inPos);
outLightVec = normalize(ubo.lightPos.xyz - inPos);
outViewVec = -pos.xyz;
outShadowCoord = ( biasMat * ubo.lightSpace * ubo.model ) * vec4(inPos, 1.0);

View file

@ -3,10 +3,23 @@
Texture2D textureColor : register(t1);
SamplerState samplerColor : register(s1);
struct UBO
{
float4x4 projection;
float4x4 view;
float4x4 model;
float4x4 lightSpace;
float4 lightPos;
float zNear;
float zFar;
};
cbuffer ubo : register(b0) { UBO ubo; }
float LinearizeDepth(float depth)
{
float n = 1.0; // camera z near
float f = 128.0; // camera z far
float n = ubo.zNear;
float f = ubo.zFar;
float z = depth;
return (2.0 * n) / (f + n - z * (f - n));
}

View file

@ -14,7 +14,9 @@ struct UBO
float4x4 view;
float4x4 model;
float4x4 lightSpace;
float3 lightPos;
float4 lightPos;
float zNear;
float zFar;
};
cbuffer ubo : register(b0) { UBO ubo; }
@ -45,7 +47,7 @@ VSOutput main(VSInput input)
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
output.Normal = mul((float3x3)ubo.model, input.Normal);
output.LightVec = normalize(ubo.lightPos - input.Pos);
output.LightVec = normalize(ubo.lightPos.xyz - input.Pos);
output.ViewVec = -pos.xyz;
output.ShadowCoord = mul(biasMat, mul(ubo.lightSpace, mul(ubo.model, float4(input.Pos, 1.0))));

View file

@ -56,7 +56,10 @@ public:
glm::mat4 view;
glm::mat4 model;
glm::mat4 depthBiasMVP;
glm::vec3 lightPos;
glm::vec4 lightPos;
// Used for depth map visualization
float zNear;
float zFar;
} uboVSscene;
struct {
@ -384,7 +387,7 @@ public:
// Shared pipeline layout for all pipelines used in this sample
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
// Binding 0 : Vertex shader uniform buffer
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0),
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0),
// Binding 1 : Fragment shader image sampler (shadow map)
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 1)
};
@ -409,6 +412,8 @@ public:
VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.debug));
writeDescriptorSets = {
// Binding 0 : Parameters uniform buffer
vks::initializers::writeDescriptorSet(descriptorSets.debug, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.scene.descriptor),
// Binding 1 : Fragment shader texture sampler
vks::initializers::writeDescriptorSet(descriptorSets.debug, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &shadowMapDescriptor)
};
@ -542,8 +547,10 @@ public:
uboVSscene.projection = camera.matrices.perspective;
uboVSscene.view = camera.matrices.view;
uboVSscene.model = glm::mat4(1.0f);
uboVSscene.lightPos = lightPos;
uboVSscene.lightPos = glm::vec4(lightPos, 1.0f);
uboVSscene.depthBiasMVP = uboOffscreenVS.depthMVP;
uboVSscene.zNear = zNear;
uboVSscene.zFar = zFar;
memcpy(uniformBuffers.scene.mapped, &uboVSscene, sizeof(uboVSscene));
}