SHADOWS WORK

This commit is contained in:
Dynamitos
2025-05-10 22:52:46 +02:00
parent 980414e38b
commit 08a4dbdfdf
6 changed files with 63 additions and 32 deletions
+19 -13
View File
@@ -11,6 +11,12 @@ struct LightCullingData
}; };
ParameterBlock<LightCullingData> pLightCullingData; ParameterBlock<LightCullingData> pLightCullingData;
static const float4x4 biasMat = float4x4(
0.5, 0.0, 0.0, 0.5,
0.0, 0.5, 0.0, 0.5,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
[shader("pixel")] [shader("pixel")]
float4 fragmentMain(in FragmentParameter params) : SV_Target float4 fragmentMain(in FragmentParameter params) : SV_Target
{ {
@@ -24,22 +30,22 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target
float3 result = float3(0, 0, 0); float3 result = float3(0, 0, 0);
for(int i = 0; i < pLightEnv.numDirectionalLights; ++i) for(int i = 0; i < pLightEnv.numDirectionalLights; ++i)
{ {
float4 lightSpacePos = mul(pLightEnv.directionalLights[i].lightSpaceMatrix, float4(params.position_WS, 1)); float4 lightSpacePos = mul(biasMat, mul(pLightEnv.directionalLights[i].lightSpaceMatrix, float4(params.position_WS, 1)));
float3 projCoords = lightSpacePos.xyz / lightSpacePos.w; float4 shadowCoord = lightSpacePos / lightSpacePos.w;
projCoords.xy = projCoords.xy * 0.5 + 0.5; float shadow = 1.0f;
projCoords.z /= 2; float dist = pLightEnv.shadowMap.Sample(pLightEnv.shadowSampler, shadowCoord.xy).r;
float closestDepth = pLightEnv.shadowMap.Sample(pLightEnv.shadowSampler, projCoords.xy).r; if (shadowCoord.w > 0 && dist > shadowCoord.z)
float currentDepth = projCoords.z; {
float bias = max(0.05 * (1.0 - dot(brdf.getNormal(), pLightEnv.directionalLights[i].direction.xyz)), 0.005); shadow = 0;
float shadow = currentDepth + bias < closestDepth ? 1.0 : 0.0; }
result += float3(currentDepth, 0, 0);//shadow * pLightEnv.directionalLights[i].illuminate(lightingParams, brdf); result += shadow * pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
} }
for (uint i = 0; i < lightCount; ++i) for (uint i = 0; i < pLightEnv.numPointLights; ++i)
{ {
uint lightIndex = pLightCullingData.lightIndexList[startOffset + i]; //uint lightIndex = pLightCullingData.lightIndexList[startOffset + i];
result += pLightEnv.pointLights[lightIndex].illuminate(lightingParams, brdf); result += pLightEnv.pointLights[i].illuminate(lightingParams, brdf);
} }
result += brdf.evaluateAmbient(lightingParams.viewDir_WS); //result += brdf.evaluateAmbient(lightingParams.viewDir_WS);
return float4(result, brdf.getAlpha()); return float4(result, brdf.getAlpha());
} }
+1 -1
View File
@@ -113,5 +113,5 @@ float4 toneMapping(float2 uv : UV) : SV_Target
value = agxLook(value); value = agxLook(value);
value = agxEotf(value); value = agxEotf(value);
return float4(value, 1); return float4(hdrValue, 1);
} }
+2 -2
View File
@@ -6,8 +6,8 @@
namespace Seele { namespace Seele {
namespace Component { namespace Component {
struct Camera { struct Camera {
float nearPlane = 0.001f; float nearPlane = 0.1f;
float farPlane = 10000.0f; float farPlane = 1000.0f;
bool mainCamera = false; bool mainCamera = false;
}; };
} // namespace Component } // namespace Component
@@ -94,6 +94,8 @@ void ShadowPass::render() {
command->setViewport(shadowViewport); command->setViewport(shadowViewport);
const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(id); const Gfx::ShaderCollection* collection = graphics->getShaderCompiler()->findShaders(id);
constexpr float depthBiasConstant = -1.25f;
constexpr float depthBiasSlope = -1.75f;
if (graphics->supportMeshShading()) { if (graphics->supportMeshShading()) {
Gfx::MeshPipelineCreateInfo pipelineInfo = { Gfx::MeshPipelineCreateInfo pipelineInfo = {
.taskShader = collection->taskShader, .taskShader = collection->taskShader,
@@ -104,10 +106,9 @@ void ShadowPass::render() {
.rasterizationState = .rasterizationState =
{ {
.cullMode = Gfx::SE_CULL_MODE_NONE, .cullMode = Gfx::SE_CULL_MODE_NONE,
}, .depthBiasEnable = true,
.colorBlend = .depthBiasConstantFactor = depthBiasConstant,
{ .depthBiasSlopeFactor = depthBiasSlope,
.attachmentCount = 1,
}, },
}; };
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo)); Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
@@ -121,10 +122,9 @@ void ShadowPass::render() {
.rasterizationState = .rasterizationState =
{ {
.cullMode = Gfx::SE_CULL_MODE_NONE, .cullMode = Gfx::SE_CULL_MODE_NONE,
}, .depthBiasEnable = true,
.colorBlend = .depthBiasConstantFactor = depthBiasConstant,
{ .depthBiasSlopeFactor = depthBiasSlope,
.attachmentCount = 1,
}, },
}; };
Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo)); Gfx::PGraphicsPipeline pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo));
+29 -4
View File
@@ -10,8 +10,7 @@ Window::~Window() {}
Viewport::Viewport(PWindow owner, const ViewportCreateInfo& viewportInfo) Viewport::Viewport(PWindow owner, const ViewportCreateInfo& viewportInfo)
: sizeX(viewportInfo.dimensions.size.x), sizeY(viewportInfo.dimensions.size.y), offsetX(viewportInfo.dimensions.offset.x), : sizeX(viewportInfo.dimensions.size.x), sizeY(viewportInfo.dimensions.size.y), offsetX(viewportInfo.dimensions.offset.x),
offsetY(viewportInfo.dimensions.offset.y), fieldOfView(viewportInfo.fieldOfView), orthoLeft(viewportInfo.left), offsetY(viewportInfo.dimensions.offset.y), fieldOfView(viewportInfo.fieldOfView), orthoLeft(viewportInfo.left),
orthoRight(viewportInfo.right), orthoTop(viewportInfo.top), orthoBottom(viewportInfo.bottom), orthoRight(viewportInfo.right), orthoTop(viewportInfo.top), orthoBottom(viewportInfo.bottom), owner(owner) {
owner(owner) {
if (owner != nullptr) { if (owner != nullptr) {
sizeX = std::min(owner->getFramebufferWidth(), sizeX); sizeX = std::min(owner->getFramebufferWidth(), sizeX);
sizeY = std::min(owner->getFramebufferHeight(), sizeY); sizeY = std::min(owner->getFramebufferHeight(), sizeY);
@@ -23,8 +22,34 @@ Viewport::~Viewport() {}
Matrix4 Viewport::getProjectionMatrix(float nearPlane, float farPlane) const { Matrix4 Viewport::getProjectionMatrix(float nearPlane, float farPlane) const {
Matrix4 correctionMatrix = Matrix4(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1 / 2.f, 0, 0, 0, 1 / 2.f, 1); Matrix4 correctionMatrix = Matrix4(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1 / 2.f, 0, 0, 0, 1 / 2.f, 1);
if (fieldOfView > 0.0f) { if (fieldOfView > 0.0f) {
Matrix4 projectionMatrix = glm::perspective(fieldOfView, sizeX / static_cast<float>(sizeY), nearPlane, farPlane); const float aspect = static_cast<float>(sizeX) / sizeY;
return correctionMatrix * projectionMatrix; const float e = 1.0f / std::tan(fieldOfView * 0.5f);
return {
{
e / aspect,
0.0f,
0.0f,
0.0f,
},
{
0.0f,
-e,
0.0f,
0.0f,
},
{
0.0f,
0.0f,
0.5f * (farPlane + nearPlane) / (nearPlane - farPlane),
-1.0f,
},
{
0.0f,
0.0f,
(farPlane * nearPlane) / (nearPlane - farPlane),
0.0f,
},
};
} else { } else {
return correctionMatrix * glm::ortho(orthoLeft, orthoRight, orthoBottom, orthoTop, nearPlane, farPlane); return correctionMatrix * glm::ortho(orthoLeft, orthoRight, orthoBottom, orthoTop, nearPlane, farPlane);
} }
+4 -4
View File
@@ -75,7 +75,7 @@ void LightEnvironment::addDirectionalLight(const Component::DirectionalLight& di
dirs.add(ShaderDirectionalLight{ dirs.add(ShaderDirectionalLight{
.color = Vector4(dirLight.color, dirLight.intensity), .color = Vector4(dirLight.color, dirLight.intensity),
.direction = Vector4(transform.getForward(), 0), .direction = Vector4(transform.getForward(), 0),
.lightSpaceMatrix = correctionMatrix * glm::ortho(-100.0f, 100.0f, -100.0f, 100.0f, -100.0f, 100.0f) * cameraMatrix, .lightSpaceMatrix = correctionMatrix * glm::ortho(-100.0f, 100.0f, -100.0f, 100.0f, 100.0f, -100.0f) * cameraMatrix,
}); });
directionalTransforms.add(transform); directionalTransforms.add(transform);
if (shadowMaps.size() < dirs.size()) { if (shadowMaps.size() < dirs.size()) {
@@ -90,9 +90,9 @@ void LightEnvironment::addDirectionalLight(const Component::DirectionalLight& di
shadowMaps.back()->changeLayout(Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, Gfx::SE_ACCESS_NONE, Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT, shadowMaps.back()->changeLayout(Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, Gfx::SE_ACCESS_NONE, Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT); Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT);
shadowSamplers.add(graphics->createSampler(SamplerCreateInfo{ shadowSamplers.add(graphics->createSampler(SamplerCreateInfo{
.addressModeU = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER, .addressModeU = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.addressModeV = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER, .addressModeV = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.addressModeW = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER, .addressModeW = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.name = "ShadowSampler", .name = "ShadowSampler",
})); }));
} }