feat: Shadow pass——主光方向光阴影(RenderGraph 双 pass 管线首个验证案例)
- RenderGraph 声明 ShadowMap 虚拟资源(D32,2048²固定分辨率,独立于 swapchain 尺寸),Shadow pass 写深度 → Forward 声明 ShaderRead 采样, barrier/layout(DepthAttachment→ShaderReadOnly)全部由图自动预算 - Shadow pass:空 fragment shader + 深度偏移(斜率缩放)抗 shadow acne - Forward:UBO 增加 lightSpace 矩阵;Set 2 采样阴影图,手动对比 + 斜率缩放偏差;clamp-to-border 白色边界(光视锥外 = 全亮) - 光空间矩阵:主光方向反推 30 单位正交盒;深度重映射 [-1,1]→[0,1] - GetPhysicalView(slot):物理镜像按帧槽位隔离,描述符 Compile 后一次性写入 - 修复 RG 隐蔽 bug:ShaderStage 与 PipelineStage2 位值不同 (eFragment=0x10 vs eFragmentShader=0x80),位值直转会撞出 TESSELLATION_CONTROL stage;改为逐位显式映射 - 修复 Camera 投影深度未重映射到 [0,1] 的潜在裁剪问题 validation 零错误零警告(3 passes / 5 barriers / 4 gpu images)
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
#version 450
|
||||
|
||||
// Shadow pass 片段着色器:深度-only pass,无任何输出。
|
||||
// 注意:不能声明 out 变量——renderingInfo 无颜色附件时写 out 会被 validation 拒绝。
|
||||
|
||||
void main() {
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#version 450
|
||||
|
||||
// Shadow pass 顶点着色器:只关心位置,输出光空间深度。
|
||||
// push_constant 两个字 mat4 = 128 字节,正好在最小保证上限内。
|
||||
|
||||
layout(location = 0) in vec3 inPosition;
|
||||
|
||||
layout(push_constant) uniform PushConstants {
|
||||
mat4 lightVP; // 光空间变换(正交)
|
||||
mat4 model; // 实体世界矩阵
|
||||
} pc;
|
||||
|
||||
void main() {
|
||||
gl_Position = pc.lightVP * pc.model * vec4(inPosition, 1.0);
|
||||
}
|
||||
@@ -10,18 +10,42 @@ struct Light {
|
||||
layout(location = 0) in vec2 fragUV;
|
||||
layout(location = 1) in vec3 fragNormal;
|
||||
layout(location = 2) in vec3 fragWorldPos;
|
||||
layout(location = 3) in vec4 fragLightPos;
|
||||
|
||||
layout(location = 0) out vec4 outColor;
|
||||
|
||||
layout(set = 0, binding = 0) uniform UniformBufferObject {
|
||||
mat4 view;
|
||||
mat4 proj;
|
||||
mat4 lightSpace;
|
||||
vec4 ambientColor;
|
||||
Light lights[MAX_LIGHTS];
|
||||
} ubo;
|
||||
|
||||
layout(set = 1, binding = 0) uniform sampler2D texSampler;
|
||||
|
||||
// 阴影深度图:手动对比(非 comparison sampler,逻辑清晰且免 format feature 依赖)
|
||||
layout(set = 2, binding = 0) uniform sampler2D shadowMap;
|
||||
|
||||
// 主光(lights[0])阴影因子:出光视锥外/未被遮挡 = 1.0
|
||||
float ShadowFactor(vec3 N, vec3 L)
|
||||
{
|
||||
vec3 proj = fragLightPos.xyz / fragLightPos.w;
|
||||
proj = proj * 0.5 + 0.5; // NDC [-1,1] -> UV [0,1]
|
||||
|
||||
if (proj.x < 0.0 || proj.x > 1.0 ||
|
||||
proj.y < 0.0 || proj.y > 1.0 ||
|
||||
proj.z > 1.0) {
|
||||
return 1.0; // 光视锥外:不施加阴影(边界由 border color 兜底)
|
||||
}
|
||||
|
||||
// 斜率缩放偏差,缓解 shadow acne
|
||||
float bias = max(0.004 * (1.0 - dot(N, L)), 0.0008);
|
||||
float closest = texture(shadowMap, proj.xy).r;
|
||||
|
||||
return (proj.z - bias > closest) ? 0.25 : 1.0;
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec3 albedo = texture(texSampler, fragUV).rgb;
|
||||
vec3 N = normalize(fragNormal);
|
||||
@@ -31,8 +55,9 @@ void main() {
|
||||
for (int i = 0; i < MAX_LIGHTS; ++i) {
|
||||
vec3 L = normalize(-ubo.lights[i].direction.xyz);
|
||||
float diff = max(dot(N, L), 0.0);
|
||||
result += ubo.lights[i].color.rgb * ubo.lights[i].color.a * diff * albedo;
|
||||
float shadow = (i == 0) ? ShadowFactor(N, L) : 1.0;
|
||||
result += ubo.lights[i].color.rgb * ubo.lights[i].color.a * diff * albedo * shadow;
|
||||
}
|
||||
|
||||
outColor = vec4(result, 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ layout(location = 2) in vec2 inUV;
|
||||
layout(location = 0) out vec2 fragUV;
|
||||
layout(location = 1) out vec3 fragNormal;
|
||||
layout(location = 2) out vec3 fragWorldPos;
|
||||
layout(location = 3) out vec4 fragLightPos; // 光空间裁剪坐标(阴影采样用)
|
||||
|
||||
layout(push_constant) uniform PushConstants {
|
||||
mat4 model;
|
||||
@@ -22,6 +23,7 @@ layout(push_constant) uniform PushConstants {
|
||||
layout(set = 0, binding = 0) uniform UniformBufferObject {
|
||||
mat4 view;
|
||||
mat4 proj;
|
||||
mat4 lightSpace; // 主光(lights[0])的正交光空间变换
|
||||
vec4 ambientColor;
|
||||
Light lights[MAX_LIGHTS];
|
||||
} ubo;
|
||||
@@ -36,4 +38,6 @@ void main() {
|
||||
fragNormal = normalize(normalMatrix * inNormal);
|
||||
|
||||
fragUV = inUV;
|
||||
}
|
||||
|
||||
fragLightPos = ubo.lightSpace * worldPos;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user