EDIT: Forward-Z cascaded shadow map failed...

This commit is contained in:
2025-10-20 14:26:08 +09:00
parent b419cc1148
commit 4dec20b60f
5 changed files with 17 additions and 13 deletions

View File

@@ -19,20 +19,20 @@ float hash12(vec2 p)
}
const vec2 POISSON_16[16] = vec2[16](
vec2(0.2852, -0.1883), vec2(-0.1464, 0.2591),
vec2(-0.3651, -0.0974), vec2(0.0901, 0.3807),
vec2(0.4740, 0.0679), vec2(-0.0512, -0.4466),
vec2(-0.4497, 0.1673), vec2(0.3347, 0.3211),
vec2(0.1948, -0.4196), vec2(-0.2919, -0.3291),
vec2(-0.0763, 0.4661), vec2(0.4421, -0.2217),
vec2(0.0281, -0.2468), vec2(-0.2104, 0.0573),
vec2(0.1197, 0.0779), vec2(-0.0905, -0.1203)
vec2(0.2852, -0.1883), vec2(-0.1464, 0.2591),
vec2(-0.3651, -0.0974), vec2(0.0901, 0.3807),
vec2(0.4740, 0.0679), vec2(-0.0512, -0.4466),
vec2(-0.4497, 0.1673), vec2(0.3347, 0.3211),
vec2(0.1948, -0.4196), vec2(-0.2919, -0.3291),
vec2(-0.0763, 0.4661), vec2(0.4421, -0.2217),
vec2(0.0281, -0.2468), vec2(-0.2104, 0.0573),
vec2(0.1197, 0.0779), vec2(-0.0905, -0.1203)
);
float calcShadowVisibility(vec3 worldPos, vec3 N, vec3 L)
{
// Choose cascade based on view-space depth
float viewDepth = - (sceneData.view * vec4(worldPos, 1.0)).z; // positive
float viewDepth = - (sceneData.view * vec4(worldPos, 1.0)).z;// positive
int ci = 0;
if (viewDepth > sceneData.cascadeSplitsView.x) ci = 1;
if (viewDepth > sceneData.cascadeSplitsView.y) ci = 2;

View File

@@ -12,7 +12,7 @@ inline constexpr int kShadowCascadeCount = 4;
// Maximum shadow distance for CSM in view-space units
inline constexpr float kShadowCSMFar = 50.0f;
// Shadow map resolution used for stabilization (texel snapping). Must match actual image size.
inline constexpr float kShadowMapResolution = 2048.0f;
inline constexpr float kShadowMapResolution = 4096.0f;
// Extra XY expansion for cascade footprint (safety against FOV/aspect changes)
inline constexpr float kShadowCascadeRadiusScale = 1.15f;
// Additive XY margin in world units (light-space) beyond scaled radius

View File

@@ -35,6 +35,9 @@ void SamplerManager::init(DeviceManager *deviceManager)
sh.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
sh.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
sh.compareEnable = VK_FALSE; // manual PCF
// Depth shadow maps are single-level; keep base LOD only and avoid mip filtering.
sh.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
sh.maxLod = 0.0f;
sh.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
vkCreateSampler(_deviceManager->device(), &sh, nullptr, &_shadowLinearClamp);
}

View File

@@ -76,7 +76,6 @@ struct GPUSceneData {
glm::vec4 sunlightDirection; // w for sun power
glm::vec4 sunlightColor;
// CSM data (unused by current shaders until wired)
glm::mat4 lightViewProjCascades[4];
glm::vec4 cascadeSplitsView;
};

View File

@@ -122,7 +122,7 @@ void SceneManager::update_scene()
glm::vec3 up = glm::normalize(glm::cross(L, right));
const float csmFar = kShadowCSMFar; // configurable shadow distance
const float lambda = 0.8f; // split weighting
const float lambda = 0.5f; // split weighting
const int cascades = kShadowCascadeCount;
float splits[4] = {0, 0, 0, 0};
@@ -213,7 +213,9 @@ void SceneManager::update_scene()
float bottom = centerLS.y - radius;
float top = centerLS.y + radius;
mat4 projLight = orthoRH_ZO(left, rightE, bottom, top, nearLS, farLS);
mat4 projLight = orthoRH_ZO(-40.f, 40.f, -40.f, 40.f, nearLS, farLS);
// projLight[1][1] *= -1.0f;
return projLight * viewLight;
};