From b79525c1a6bed82a24538aef0538ab5f394f947c Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Thu, 9 Mar 2023 17:39:57 +0100 Subject: [PATCH] adding directional light actor --- src/Engine/Actor/CMakeLists.txt | 10 ++++-- src/Engine/Actor/DirectionalLightActor.cpp | 24 ++++++++++++++ src/Engine/Actor/DirectionalLightActor.h | 17 ++++++++++ src/Engine/Actor/PointLightActor.cpp | 24 ++++++++++++++ src/Engine/Actor/PointLightActor.h | 17 ++++++++++ src/Engine/Component/CMakeLists.txt | 4 +++ src/Engine/Component/DirectionalLight.h | 13 ++++++++ src/Engine/Component/PointLight.h | 15 +++++++++ src/Engine/Graphics/RenderPass/BasePass.cpp | 14 +++----- src/Engine/Graphics/RenderPass/BasePass.h | 5 +-- .../Graphics/RenderPass/LightCullingPass.cpp | 5 --- src/Engine/Scene/Scene.cpp | 32 +++++++++---------- src/Engine/Scene/Scene.h | 13 -------- src/Engine/Window/GameView.cpp | 1 + 14 files changed, 144 insertions(+), 50 deletions(-) create mode 100644 src/Engine/Actor/DirectionalLightActor.cpp create mode 100644 src/Engine/Actor/DirectionalLightActor.h create mode 100644 src/Engine/Actor/PointLightActor.cpp create mode 100644 src/Engine/Actor/PointLightActor.h create mode 100644 src/Engine/Component/DirectionalLight.h create mode 100644 src/Engine/Component/PointLight.h diff --git a/src/Engine/Actor/CMakeLists.txt b/src/Engine/Actor/CMakeLists.txt index 810129b..31f4ac0 100644 --- a/src/Engine/Actor/CMakeLists.txt +++ b/src/Engine/Actor/CMakeLists.txt @@ -4,12 +4,18 @@ target_sources(Engine Actor.h CameraActor.cpp CameraActor.h + DirectionalLightActor.cpp + DirectionalLightActor.h Entity.cpp - Entity.h) + Entity.h + PointLightActor.cpp + PointLightActor.h) target_sources(Engine PUBLIC FILE_SET HEADERS FILES Actor.h CameraActor.h - Entity.h) + DirectionalLightActor.h + Entity.h + PointLightActor.h) diff --git a/src/Engine/Actor/DirectionalLightActor.cpp b/src/Engine/Actor/DirectionalLightActor.cpp new file mode 100644 index 0000000..599e1d5 --- /dev/null +++ b/src/Engine/Actor/DirectionalLightActor.cpp @@ -0,0 +1,24 @@ +#include "DirectionalLightActor.h" + +using namespace Seele; + +DirectionalLightActor::DirectionalLightActor(PScene scene) + : Actor(scene) +{ + attachComponent(); +} + +DirectionalLightActor::~DirectionalLightActor() +{ + +} + +Component::DirectionalLight& DirectionalLightActor::getDirectionalLightComponent() +{ + return accessComponent(); +} + +const Component::DirectionalLight& DirectionalLightActor::getDirectionalLightComponent() const +{ + return accessComponent(); +} \ No newline at end of file diff --git a/src/Engine/Actor/DirectionalLightActor.h b/src/Engine/Actor/DirectionalLightActor.h new file mode 100644 index 0000000..c331796 --- /dev/null +++ b/src/Engine/Actor/DirectionalLightActor.h @@ -0,0 +1,17 @@ +#pragma once +#include "Actor.h" +#include "Component/DirectionalLight.h" + +namespace Seele +{ +class DirectionalLightActor : public Actor +{ +public: + DirectionalLightActor(PScene scene); + virtual ~DirectionalLightActor(); + Component::DirectionalLight& getDirectionalLightComponent(); + const Component::DirectionalLight& getDirectionalLightComponent() const; +private: +}; +DEFINE_REF(DirectionalLightActor) +} // namespace Seele diff --git a/src/Engine/Actor/PointLightActor.cpp b/src/Engine/Actor/PointLightActor.cpp new file mode 100644 index 0000000..ab9118b --- /dev/null +++ b/src/Engine/Actor/PointLightActor.cpp @@ -0,0 +1,24 @@ +#include "PointLightActor.h" + +using namespace Seele; + +PointLightActor::PointLightActor(PScene scene) + : Actor(scene) +{ + attachComponent(); +} + +PointLightActor::~PointLightActor() +{ + +} + +Component::PointLight& PointLightActor::getPointLightComponent() +{ + return accessComponent(); +} + +const Component::PointLight& PointLightActor::getPointLightComponent() const +{ + return accessComponent(); +} \ No newline at end of file diff --git a/src/Engine/Actor/PointLightActor.h b/src/Engine/Actor/PointLightActor.h new file mode 100644 index 0000000..fe061a0 --- /dev/null +++ b/src/Engine/Actor/PointLightActor.h @@ -0,0 +1,17 @@ +#pragma once +#include "Actor.h" +#include "Component/PointLight.h" + +namespace Seele +{ +class PointLightActor : public Actor +{ +public: + PointLightActor(PScene scene); + virtual ~PointLightActor(); + Component::PointLight& getPointLightComponent(); + const Component::PointLight& getPointLightComponent() const; +private: +}; +DEFINE_REF(PointLightActor) +} // namespace Seele diff --git a/src/Engine/Component/CMakeLists.txt b/src/Engine/Component/CMakeLists.txt index af2d8dc..1c8b398 100644 --- a/src/Engine/Component/CMakeLists.txt +++ b/src/Engine/Component/CMakeLists.txt @@ -7,8 +7,10 @@ target_sources(Engine Collider.h Collider.cpp Component.h + DirectionalLight.h KeyboardInput.h MeshCollider.h + PointLight.h RigidBody.h ShapeBase.h ShapeBase.cpp @@ -27,8 +29,10 @@ target_sources(Engine Camera.h Collider.h Component.h + DirectionalLight.h KeyboardInput.h MeshCollider.h + PointLight.h RigidBody.h ShapeBase.h Skybox.h diff --git a/src/Engine/Component/DirectionalLight.h b/src/Engine/Component/DirectionalLight.h new file mode 100644 index 0000000..fc89dbc --- /dev/null +++ b/src/Engine/Component/DirectionalLight.h @@ -0,0 +1,13 @@ +#pragma once +#include "Math/Vector.h" +namespace Seele +{ +namespace Component +{ +struct DirectionalLight +{ + Vector4 color; + Vector4 direction; +}; +} // namespace Component +} // namespace Seele diff --git a/src/Engine/Component/PointLight.h b/src/Engine/Component/PointLight.h new file mode 100644 index 0000000..efbba1c --- /dev/null +++ b/src/Engine/Component/PointLight.h @@ -0,0 +1,15 @@ +#pragma once +#include "Math/Vector.h" + +namespace Seele +{ +namespace Component +{ +struct PointLight +{ + Vector4 positionWS; + //Vector4 positionVS; + Vector4 colorRange; +}; +} // namespace Component +} // namespace Seele diff --git a/src/Engine/Graphics/RenderPass/BasePass.cpp b/src/Engine/Graphics/RenderPass/BasePass.cpp index 1918c7d..8298675 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.cpp +++ b/src/Engine/Graphics/RenderPass/BasePass.cpp @@ -154,10 +154,10 @@ void BasePass::render() Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); - descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(0, directLightBuffer); - descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(1, numDirLightBuffer); - descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(2, pointLightBuffer); - descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(3, numPointLightBuffer); + descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(0, passData.lightEnv.directionalLights); + descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(1, passData.lightEnv.numDirectional); + descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(2, passData.lightEnv.pointLights); + descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(3, passData.lightEnv.numPoints); descriptorSets[INDEX_LIGHT_ENV]->updateBuffer(4, oLightIndexList); descriptorSets[INDEX_LIGHT_ENV]->updateTexture(5, oLightGrid); descriptorSets[INDEX_LIGHT_ENV]->writeChanges(); @@ -186,11 +186,7 @@ void BasePass::publishOutputs() } void BasePass::createRenderPass() -{ - directLightBuffer = resources->requestBuffer("DIRECTIONAL_LIGHTS"); - pointLightBuffer = resources->requestBuffer("POINT_LIGHTS"); - numDirLightBuffer = resources->requestUniform("NUM_DIRECTIONAL_LIGHTS"); - numPointLightBuffer = resources->requestUniform("NUM_POINT_LIGHTS"); +{ Gfx::PRenderTargetAttachment depthAttachment = resources->requestRenderTarget("DEPTHPREPASS_DEPTH"); depthAttachment->loadOp = Gfx::SE_ATTACHMENT_LOAD_OP_LOAD; colorAttachment->loadOp = Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR; diff --git a/src/Engine/Graphics/RenderPass/BasePass.h b/src/Engine/Graphics/RenderPass/BasePass.h index 976e076..84f04ad 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.h +++ b/src/Engine/Graphics/RenderPass/BasePass.h @@ -30,6 +30,7 @@ struct BasePassData { Array staticDrawList; Gfx::PStructuredBuffer sceneDataBuffer; + LightEnv lightEnv; }; class BasePass : public RenderPass { @@ -54,10 +55,6 @@ private: Gfx::PPipelineLayout basePassLayout; // Set 0: Light environment static constexpr uint32 INDEX_LIGHT_ENV = 0; - Gfx::PStructuredBuffer directLightBuffer; - Gfx::PUniformBuffer numDirLightBuffer; - Gfx::PStructuredBuffer pointLightBuffer; - Gfx::PUniformBuffer numPointLightBuffer; Gfx::PStructuredBuffer oLightIndexList; Gfx::PTexture oLightGrid; Gfx::PDescriptorLayout lightLayout; diff --git a/src/Engine/Graphics/RenderPass/LightCullingPass.cpp b/src/Engine/Graphics/RenderPass/LightCullingPass.cpp index c9ed683..86efcce 100644 --- a/src/Engine/Graphics/RenderPass/LightCullingPass.cpp +++ b/src/Engine/Graphics/RenderPass/LightCullingPass.cpp @@ -188,11 +188,6 @@ void LightCullingPass::publishOutputs() resources->registerBufferOutput("LIGHTCULLING_OLIGHTLIST", oLightIndexList); resources->registerBufferOutput("LIGHTCULLING_TLIGHTLIST", tLightIndexList); - resources->registerBufferOutput("DIRECTIONAL_LIGHTS", passData.lightEnv.directionalLights); - resources->registerUniformOutput("NUM_DIRECTIONAL_LIGHTS", passData.lightEnv.numDirectional); - resources->registerBufferOutput("POINT_LIGHTS", passData.lightEnv.pointLights); - resources->registerUniformOutput("NUM_POINT_LIGHTS", passData.lightEnv.numPoints); - TextureCreateInfo textureInfo = { .width = dispatchParams.numThreadGroups.x, .height = dispatchParams.numThreadGroups.y, diff --git a/src/Engine/Scene/Scene.cpp b/src/Engine/Scene/Scene.cpp index 41f629c..e94011d 100644 --- a/src/Engine/Scene/Scene.cpp +++ b/src/Engine/Scene/Scene.cpp @@ -6,14 +6,12 @@ #include "Asset/AssetRegistry.h" #include "Asset/TextureAsset.h" #include "Asset/MaterialAsset.h" +#include "Component/PointLight.h" +#include "Component/DirectionalLight.h" +#include "Actor/PointLightActor.h" using namespace Seele; -inline float frand() -{ - return (float)rand()/RAND_MAX; -} - Scene::Scene(Gfx::PGraphics graphics) : graphics(graphics) , physics(registry) @@ -21,14 +19,15 @@ Scene::Scene(Gfx::PGraphics graphics) StructuredBufferCreateInfo structInfo = { .resourceData = { - .size = sizeof(DirectionalLight) * MAX_DIRECTIONAL_LIGHTS, + .size = sizeof(Component::DirectionalLight) * MAX_DIRECTIONAL_LIGHTS, .data = nullptr, }, - .stride = sizeof(DirectionalLight), + .stride = sizeof(Component::DirectionalLight), .bDynamic = true, }; lightEnv.directionalLights = graphics->createStructuredBuffer(structInfo); - structInfo.resourceData.size = sizeof(PointLight) * MAX_POINT_LIGHTS; + structInfo.resourceData.size = sizeof(Component::PointLight) * MAX_POINT_LIGHTS; + structInfo.stride = sizeof(Component::PointLight); lightEnv.pointLights = graphics->createStructuredBuffer(structInfo); UniformBufferCreateInfo uniformInfo = { @@ -40,7 +39,6 @@ Scene::Scene(Gfx::PGraphics graphics) }; lightEnv.numDirectional = graphics->createUniformBuffer(uniformInfo); lightEnv.numPoints = graphics->createUniformBuffer(uniformInfo); - } Scene::~Scene() @@ -107,28 +105,28 @@ Array Scene::getStaticMeshes() LightEnv Scene::getLightBuffer() { - StaticArray dirLights; - uint32 numDirLights; - for(auto&& [entity, light] : registry.view().each()) + StaticArray dirLights; + uint32 numDirLights = 0; + for(auto&& [entity, light] : registry.view().each()) { dirLights[numDirLights++] = light; } lightEnv.directionalLights->updateContents({ - .size = sizeof(DirectionalLight) * numDirLights, + .size = sizeof(Component::DirectionalLight) * numDirLights, .data = (uint8*)dirLights.data(), }); lightEnv.numDirectional->updateContents({ .size = sizeof(uint32), .data = (uint8*)&numDirLights, }); - StaticArray pointLights; - uint32 numPointLights; - for(auto&& [entity, light] : registry.view().each()) + StaticArray pointLights; + uint32 numPointLights = 0; + for(auto&& [entity, light] : registry.view().each()) { pointLights[numPointLights++] = light; } lightEnv.pointLights->updateContents({ - .size = sizeof(PointLight) * numPointLights, + .size = sizeof(Component::PointLight) * numPointLights, .data = (uint8*)pointLights.data(), }); lightEnv.numPoints->updateContents({ diff --git a/src/Engine/Scene/Scene.h b/src/Engine/Scene/Scene.h index 731fc00..bb5e646 100644 --- a/src/Engine/Scene/Scene.h +++ b/src/Engine/Scene/Scene.h @@ -11,19 +11,6 @@ namespace Seele DECLARE_REF(Material) DECLARE_REF(Entity) -struct DirectionalLight -{ - Vector4 color; - Vector4 direction; -}; - -struct PointLight -{ - Vector4 positionWS; - //Vector4 positionVS; - Vector4 colorRange; -}; - #define MAX_DIRECTIONAL_LIGHTS 4 #define MAX_POINT_LIGHTS 256 struct LightEnv diff --git a/src/Engine/Window/GameView.cpp b/src/Engine/Window/GameView.cpp index d66c4f1..d3801a7 100644 --- a/src/Engine/Window/GameView.cpp +++ b/src/Engine/Window/GameView.cpp @@ -54,6 +54,7 @@ void GameView::commitUpdate() lightCullingData.lightEnv = scene->getLightBuffer(); basePassData.staticDrawList = scene->getStaticMeshes(); basePassData.sceneDataBuffer = scene->getSceneDataBuffer(); + basePassData.lightEnv = scene->getLightBuffer(); skyboxData.skybox = scene->getSkybox(); }