From 36aec0fa0649c3712722c513be36a50782aa3e48 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Thu, 1 Feb 2024 17:07:51 +0100 Subject: [PATCH] Removing limit for point lights --- res/shaders/ComputeFrustums.slang | 22 +++++++++- res/shaders/LightCulling.slang | 4 +- src/Engine/Actor/DirectionalLightActor.cpp | 6 +++ src/Engine/Actor/DirectionalLightActor.h | 1 + src/Engine/Actor/PointLightActor.cpp | 6 +++ src/Engine/Actor/PointLightActor.h | 1 + src/Engine/Scene/LightEnvironment.cpp | 48 +++++++++++++++++----- src/Engine/Scene/LightEnvironment.h | 4 +- 8 files changed, 76 insertions(+), 16 deletions(-) diff --git a/res/shaders/ComputeFrustums.slang b/res/shaders/ComputeFrustums.slang index 10dffaa..2ade0a0 100644 --- a/res/shaders/ComputeFrustums.slang +++ b/res/shaders/ComputeFrustums.slang @@ -13,9 +13,29 @@ struct ComputeShaderInput [shader("compute")] void computeFrustums(ComputeShaderInput in) { + const float3 topLeft = float3(-1, -1, -1); + const float3 topRight = float3(1, -1, -1); + const float3 bottomLeft = float3(-1, 1, -1); + const float3 origin = float3(0, 0, 0); + float3 xStep = (topRight - topLeft) / pDispatchParams.numThreads.x; + float3 yStep = (bottomLeft - topLeft) / pDispatchParams.numThreads.y; + float3 corners[4] = { + topLeft + (in.dispatchThreadID.x + 0) * xStep + (in.dispatchThreadID.y + 0) * yStep, + topLeft + (in.dispatchThreadID.x + 1) * xStep + (in.dispatchThreadID.y + 0) * yStep, + topLeft + (in.dispatchThreadID.x + 0) * xStep + (in.dispatchThreadID.y + 1) * yStep, + topLeft + (in.dispatchThreadID.x + 1) * xStep + (in.dispatchThreadID.y + 1) * yStep + }; + Frustum frustum; + frustum.basePlane.n = float3(0, 0, -1); + frustum.basePlane.d = 0; + + frustum.sides[0] = computePlane(origin, corners[0], corners[2]); + frustum.sides[1] = computePlane(origin, corners[3], corners[1]); + frustum.sides[2] = computePlane(origin, corners[1], corners[0]); + frustum.sides[3] = computePlane(origin, corners[2], corners[3]); if(in.dispatchThreadID.x < pDispatchParams.numThreads.x && in.dispatchThreadID.y < pDispatchParams.numThreads.y) { uint index = in.dispatchThreadID.x + (in.dispatchThreadID.y * pDispatchParams.numThreads.x); - //pDispatchParams.frustums[index] = frustum; + pDispatchParams.frustums[index] = frustum; } } diff --git a/res/shaders/LightCulling.slang b/res/shaders/LightCulling.slang index 85414e8..c234675 100644 --- a/res/shaders/LightCulling.slang +++ b/res/shaders/LightCulling.slang @@ -99,7 +99,7 @@ void cullLights(ComputeShaderInput in) { PointLight light = pLightEnv.pointLights[i]; //TODO: why doesn't this check go through? - //if(light.insideFrustum(groupFrustum, nearClipVS, maxDepthVS)) + if(light.insideFrustum(groupFrustum, nearClipVS, maxDepthVS)) { tAppendLight(i); if(!light.insidePlane(minPlane)) @@ -131,6 +131,4 @@ void cullLights(ComputeShaderInput in) { pCullingParams.tLightIndexList[tLightIndexStartOffset + k] = tLightList[k]; } - - } diff --git a/src/Engine/Actor/DirectionalLightActor.cpp b/src/Engine/Actor/DirectionalLightActor.cpp index 599e1d5..9fd9360 100644 --- a/src/Engine/Actor/DirectionalLightActor.cpp +++ b/src/Engine/Actor/DirectionalLightActor.cpp @@ -8,6 +8,12 @@ DirectionalLightActor::DirectionalLightActor(PScene scene) attachComponent(); } +DirectionalLightActor::DirectionalLightActor(PScene scene, Vector color, Vector direction) + : Actor(scene) +{ + attachComponent(Vector4(color, 0), Vector4(direction, 0)); +} + DirectionalLightActor::~DirectionalLightActor() { diff --git a/src/Engine/Actor/DirectionalLightActor.h b/src/Engine/Actor/DirectionalLightActor.h index c331796..20dc0eb 100644 --- a/src/Engine/Actor/DirectionalLightActor.h +++ b/src/Engine/Actor/DirectionalLightActor.h @@ -8,6 +8,7 @@ class DirectionalLightActor : public Actor { public: DirectionalLightActor(PScene scene); + DirectionalLightActor(PScene scene, Vector color, Vector direction); virtual ~DirectionalLightActor(); Component::DirectionalLight& getDirectionalLightComponent(); const Component::DirectionalLight& getDirectionalLightComponent() const; diff --git a/src/Engine/Actor/PointLightActor.cpp b/src/Engine/Actor/PointLightActor.cpp index ab9118b..b35cf91 100644 --- a/src/Engine/Actor/PointLightActor.cpp +++ b/src/Engine/Actor/PointLightActor.cpp @@ -8,6 +8,12 @@ PointLightActor::PointLightActor(PScene scene) attachComponent(); } +PointLightActor::PointLightActor(PScene scene, Vector position, Vector color, float attenuation) + : Actor(scene) +{ + attachComponent(Vector4(position, 1), Vector4(color, attenuation)); +} + PointLightActor::~PointLightActor() { diff --git a/src/Engine/Actor/PointLightActor.h b/src/Engine/Actor/PointLightActor.h index fe061a0..c6e9677 100644 --- a/src/Engine/Actor/PointLightActor.h +++ b/src/Engine/Actor/PointLightActor.h @@ -8,6 +8,7 @@ class PointLightActor : public Actor { public: PointLightActor(PScene scene); + PointLightActor(PScene scene, Vector position, Vector color, float attenuation); virtual ~PointLightActor(); Component::PointLight& getPointLightComponent(); const Component::PointLight& getPointLightComponent() const; diff --git a/src/Engine/Scene/LightEnvironment.cpp b/src/Engine/Scene/LightEnvironment.cpp index f2cec2c..d5b0205 100644 --- a/src/Engine/Scene/LightEnvironment.cpp +++ b/src/Engine/Scene/LightEnvironment.cpp @@ -20,18 +20,18 @@ LightEnvironment::LightEnvironment(Gfx::PGraphics graphics) }); directionalLights = graphics->createShaderBuffer(ShaderBufferCreateInfo{ .sourceData = { - .size = sizeof(Component::DirectionalLight) * MAX_DIRECTIONAL_LIGHTS, + .size = sizeof(Component::DirectionalLight) * INIT_DIRECTIONAL_LIGHTS, .data = nullptr, }, - .numElements = MAX_DIRECTIONAL_LIGHTS, + .numElements = INIT_DIRECTIONAL_LIGHTS, .dynamic = true, }); pointLights = graphics->createShaderBuffer(ShaderBufferCreateInfo{ .sourceData = { - .size = sizeof(Component::PointLight) * MAX_POINT_LIGHTS, + .size = sizeof(Component::PointLight) * INIT_POINT_LIGHTS, .data = nullptr, }, - .numElements = MAX_POINT_LIGHTS, + .numElements = INIT_POINT_LIGHTS, .dynamic = true, }); } @@ -66,14 +66,42 @@ void LightEnvironment::commit() .size = sizeof(LightEnv), .data = (uint8*) & lightEnv, }); - directionalLights->updateContents(DataSource{ - .size = sizeof(Component::DirectionalLight) * dirs.size(), - .data = (uint8*)dirs.data(), + if(directionalLights->getNumElements() < dirs.size()) + { + directionalLights = graphics->createShaderBuffer({ + .sourceData = { + .size = sizeof(Component::DirectionalLight) * dirs.size(), + .data = (uint8*)dirs.data(), + }, + .numElements = dirs.size(), + .dynamic = true, }); - pointLights->updateContents(DataSource{ - .size = sizeof(Component::PointLight) * points.size(), - .data = (uint8*)points.data(), + } + else + { + directionalLights->updateContents(DataSource{ + .size = sizeof(Component::DirectionalLight) * dirs.size(), + .data = (uint8*)dirs.data(), + }); + } + if(pointLights->getNumElements() < points.size()) + { + pointLights = graphics->createShaderBuffer({ + .sourceData = { + .size = sizeof(Component::PointLight) * points.size(), + .data = (uint8*)points.data() + }, + .numElements = points.size(), + .dynamic = true }); + } + else + { + pointLights->updateContents(DataSource{ + .size = sizeof(Component::PointLight) * points.size(), + .data = (uint8*)points.data(), + }); + } set->updateBuffer(0, lightEnvBuffer); set->updateBuffer(1, directionalLights); set->updateBuffer(2, pointLights); diff --git a/src/Engine/Scene/LightEnvironment.h b/src/Engine/Scene/LightEnvironment.h index 759d0ac..3f32a45 100644 --- a/src/Engine/Scene/LightEnvironment.h +++ b/src/Engine/Scene/LightEnvironment.h @@ -18,8 +18,8 @@ public: const Gfx::PDescriptorLayout getDescriptorLayout() const; Gfx::PDescriptorSet getDescriptorSet(); private: - #define MAX_DIRECTIONAL_LIGHTS 4 - #define MAX_POINT_LIGHTS 256 + #define INIT_DIRECTIONAL_LIGHTS 4 + #define INIT_POINT_LIGHTS 256 struct LightEnv { uint32 numDirectionalLights;