From 980414e38b6bb638d1268defb3112f8a84bc470b Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Fri, 9 May 2025 23:05:47 +0200 Subject: [PATCH] Kindof works a little bit --- CMakeLists.txt | 1 - res/shaders/BasePass.slang | 9 +++++---- src/Engine/Graphics/RenderPass/BasePass.cpp | 19 ++++++++++++------- src/Engine/Graphics/RenderPass/ShadowPass.cpp | 12 ++++++------ src/Engine/Graphics/Window.cpp | 2 +- src/Engine/Scene/LightEnvironment.cpp | 2 +- 6 files changed, 25 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a657aa..d1b42d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,7 +57,6 @@ endif() add_library(Engine SHARED "") -target_compile_definitions(Engine PRIVATE GLFW_WINDOWS) target_include_directories(Engine PRIVATE src/Engine) target_include_directories(Engine PRIVATE ${VCPKG_BASE_FOLDER}/include/) target_link_libraries(Engine PUBLIC Vulkan::Vulkan) diff --git a/res/shaders/BasePass.slang b/res/shaders/BasePass.slang index 1b1426a..7942804 100644 --- a/res/shaders/BasePass.slang +++ b/res/shaders/BasePass.slang @@ -26,15 +26,16 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target { float4 lightSpacePos = mul(pLightEnv.directionalLights[i].lightSpaceMatrix, float4(params.position_WS, 1)); float3 projCoords = lightSpacePos.xyz / lightSpacePos.w; - projCoords = projCoords * 0.5 + 0.5; + projCoords.xy = projCoords.xy * 0.5 + 0.5; + projCoords.z /= 2; float closestDepth = pLightEnv.shadowMap.Sample(pLightEnv.shadowSampler, projCoords.xy).r; float currentDepth = projCoords.z; float bias = max(0.05 * (1.0 - dot(brdf.getNormal(), pLightEnv.directionalLights[i].direction.xyz)), 0.005); - float shadow = currentDepth > closestDepth - bias ? 1.0 : 0.0; + float shadow = currentDepth + bias < closestDepth ? 1.0 : 0.0; - result += shadow * pLightEnv.directionalLights[i].illuminate(lightingParams, brdf); + result += float3(currentDepth, 0, 0);//shadow * pLightEnv.directionalLights[i].illuminate(lightingParams, brdf); } - for (uint i = 0; i < pLightEnv.numPo; ++i) + for (uint i = 0; i < lightCount; ++i) { uint lightIndex = pLightCullingData.lightIndexList[startOffset + i]; result += pLightEnv.pointLights[lightIndex].illuminate(lightingParams, brdf); diff --git a/src/Engine/Graphics/RenderPass/BasePass.cpp b/src/Engine/Graphics/RenderPass/BasePass.cpp index 3cb3c9d..93de5bd 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.cpp +++ b/src/Engine/Graphics/RenderPass/BasePass.cpp @@ -147,13 +147,13 @@ void BasePass::render() { query->beginQuery(); timestamps->write(Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT, "BaseBegin"); graphics->beginRenderPass(renderPass); - Array commands; - Gfx::ShaderPermutation permutation = graphics->getShaderCompiler()->getTemplate("BasePass"); - permutation.setDepthCulling(true); // always use the culling info - permutation.setPositionOnly(false); Array transparentData; { graphics->beginDebugRegion("Opaque"); + Array commands; + Gfx::ShaderPermutation permutation = graphics->getShaderCompiler()->getTemplate("BasePass"); + permutation.setDepthCulling(true); // always use the culling info + permutation.setPositionOnly(false); // Base Rendering for (VertexData* vertexData : VertexData::getList()) { transparentData.addAll(vertexData->getTransparentData()); @@ -243,6 +243,7 @@ void BasePass::render() { commands.add(std::move(command)); } } + graphics->executeCommands(std::move(commands)); graphics->endDebugRegion(); } @@ -264,6 +265,8 @@ void BasePass::render() { // Transparent rendering { graphics->beginDebugRegion("Transparent"); + Gfx::ShaderPermutation permutation = graphics->getShaderCompiler()->getTemplate("BasePass"); + permutation.setPositionOnly(false); permutation.setDepthCulling(false); // ignore visibility infos for transparency Map sortedDraws; for (const auto& t : transparentData) { @@ -360,23 +363,25 @@ void BasePass::render() { // } } } - commands.add(std::move(transparentCommand)); + graphics->executeCommands(std::move(transparentCommand)); graphics->endDebugRegion(); } // Debug vertices if (gDebugVertices.size() > 0) { graphics->beginDebugRegion("Debug"); + Gfx::ShaderPermutation permutation = graphics->getShaderCompiler()->getTemplate("BasePass"); + permutation.setDepthCulling(true); // always use the culling info + permutation.setPositionOnly(false); Gfx::ORenderCommand debugCommand = graphics->createRenderCommand("DebugRender"); debugCommand->setViewport(viewport); debugCommand->bindPipeline(debugPipeline); debugCommand->bindDescriptor(viewParamsSet); debugCommand->bindVertexBuffer({debugVertices}); debugCommand->draw((uint32)gDebugVertices.size(), 1, 0, 0); - commands.add(std::move(debugCommand)); + graphics->executeCommands(std::move(debugCommand)); graphics->endDebugRegion(); } - graphics->executeCommands(std::move(commands)); graphics->endRenderPass(); query->endQuery(); timestamps->write(Gfx::SE_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, "BaseEnd"); diff --git a/src/Engine/Graphics/RenderPass/ShadowPass.cpp b/src/Engine/Graphics/RenderPass/ShadowPass.cpp index 91d8d5c..47edacd 100644 --- a/src/Engine/Graphics/RenderPass/ShadowPass.cpp +++ b/src/Engine/Graphics/RenderPass/ShadowPass.cpp @@ -81,8 +81,8 @@ void ShadowPass::render() { "Shadow"); auto light = scene->getLightEnvironment()->getDirectionalLight(shadowIndex); Component::Camera lightCamera = Component::Camera{ - .nearPlane = -20.f, - .farPlane = 10.0f, + .nearPlane = -100.f, + .farPlane = 100.0f, }; Gfx::PDescriptorSet viewParamsSet = createViewParamsSet(lightCamera, scene->getLightEnvironment()->getDirectionalTransform(shadowIndex)); graphics->beginRenderPass(renderPass); @@ -173,10 +173,10 @@ void ShadowPass::publishOutputs() { .offset = {0, 0}, }, .fieldOfView = 0, - .left = -20, - .right = 20, - .top = -20, - .bottom = 20}); + .left = -100, + .right = 100, + .top = 100, + .bottom = -100}); viewport = shadowViewport; } diff --git a/src/Engine/Graphics/Window.cpp b/src/Engine/Graphics/Window.cpp index be15e05..7c8afec 100644 --- a/src/Engine/Graphics/Window.cpp +++ b/src/Engine/Graphics/Window.cpp @@ -26,6 +26,6 @@ Matrix4 Viewport::getProjectionMatrix(float nearPlane, float farPlane) const { Matrix4 projectionMatrix = glm::perspective(fieldOfView, sizeX / static_cast(sizeY), nearPlane, farPlane); return correctionMatrix * projectionMatrix; } else { - return correctionMatrix * glm::ortho(orthoLeft, orthoRight, orthoBottom, orthoTop, farPlane, nearPlane); + return correctionMatrix * glm::ortho(orthoLeft, orthoRight, orthoBottom, orthoTop, nearPlane, farPlane); } } \ No newline at end of file diff --git a/src/Engine/Scene/LightEnvironment.cpp b/src/Engine/Scene/LightEnvironment.cpp index bde9bd1..1a03c2a 100644 --- a/src/Engine/Scene/LightEnvironment.cpp +++ b/src/Engine/Scene/LightEnvironment.cpp @@ -75,7 +75,7 @@ void LightEnvironment::addDirectionalLight(const Component::DirectionalLight& di dirs.add(ShaderDirectionalLight{ .color = Vector4(dirLight.color, dirLight.intensity), .direction = Vector4(transform.getForward(), 0), - .lightSpaceMatrix = correctionMatrix * glm::ortho(-20.f, 20.f, 20.f, -20.f, 10.0f, -20.0f) * cameraMatrix, + .lightSpaceMatrix = correctionMatrix * glm::ortho(-100.0f, 100.0f, -100.0f, 100.0f, -100.0f, 100.0f) * cameraMatrix, }); directionalTransforms.add(transform); if (shadowMaps.size() < dirs.size()) {