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:
2026-09-22 14:30:50 +08:00
parent 394bfd3d0c
commit d55f644b68
8 changed files with 316 additions and 16 deletions
+27 -2
View File
@@ -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);
}
}