From 09c5843cdb2492686e692f5f2237ed73f50c057f Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Thu, 27 Oct 2022 19:02:55 +0800 Subject: [PATCH] Ambient lighting added --- SHADE_Application/src/Scenes/SBTestScene.cpp | 10 +++++++ .../MiddleEnd/Lights/SHLightComponent.cpp | 15 ++++++++-- .../MiddleEnd/Lights/SHLightComponent.h | 4 ++- .../Graphics/MiddleEnd/Lights/SHLightData.cpp | 2 +- .../Graphics/MiddleEnd/Lights/SHLightData.h | 6 +++- .../MiddleEnd/Lights/SHLightingSubSystem.cpp | 26 ++++++++++++++---- .../MiddleEnd/Lights/SHLightingSubSystem.h | 20 ++++++++++++++ TempShaderFolder/DeferredCompositeCs.glsl | 21 ++++++++++++++ TempShaderFolder/DeferredCompositeCs.spv | Bin 3460 -> 4532 bytes 9 files changed, 94 insertions(+), 10 deletions(-) diff --git a/SHADE_Application/src/Scenes/SBTestScene.cpp b/SHADE_Application/src/Scenes/SBTestScene.cpp index a06e68c2..eb6149ad 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.cpp +++ b/SHADE_Application/src/Scenes/SBTestScene.cpp @@ -162,12 +162,22 @@ namespace Sandbox SHComponentManager::AddComponent(0); SHComponentManager::RemoveComponent (0); SHComponentManager::RemoveComponent (0); + + auto ambientLight = SHEntityManager::CreateEntity(); + SHComponentManager::GetComponent(ambientLight)->SetColor(SHVec4(1.0f, 1.0f, 1.0f, 1.0f)); + SHComponentManager::GetComponent(ambientLight)->SetStrength(0.25f); + SHComponentManager::GetComponent(ambientLight)->SetType(SH_LIGHT_TYPE::AMBIENT); } void SBTestScene::Update(float dt) { static float rotation = 0.0f; + SHVec3 direction{0.0f, 0.0f, 1.0f}; + direction = SHVec3::RotateY(direction, rotation); + auto* lightComp =SHComponentManager::GetComponent(0); + lightComp->SetDirection (direction); + rotation += 0.005f; //auto& transform = *SHADE::SHComponentManager::GetComponent_s(testObj); //transform.SetWorldPosition({1.0f, 1.0f, -1.0f}); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.cpp index fd122334..6800e955 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.cpp @@ -41,9 +41,9 @@ namespace SHADE } - void SHLightComponent::SetDiffuseColor(SHVec4 diffuseColor) noexcept + void SHLightComponent::SetColor(SHVec4 color) noexcept { - lightData.diffuseColor = diffuseColor; + lightData.color = color; MakeDirty(); } @@ -100,6 +100,12 @@ namespace SHADE } + void SHLightComponent::SetStrength(float value) noexcept + { + lightData.strength = value; + MakeDirty(); + } + SHLightData const& SHLightComponent::GetLightData(void) const noexcept { return lightData; @@ -115,6 +121,11 @@ namespace SHADE return bound; } + bool SHLightComponent::GetActive(void) const noexcept + { + return active; + } + uint32_t SHLightComponent::GetIndexInBuffer(void) const noexcept { return indexInBuffer; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.h b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.h index 20ae3892..d267d2d4 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightComponent.h @@ -40,7 +40,7 @@ namespace SHADE void SetPosition (SHVec3 position) noexcept; void SetType (SH_LIGHT_TYPE type) noexcept; void SetDirection (SHVec3 direction) noexcept; - void SetDiffuseColor (SHVec4 diffuseColor) noexcept; + void SetColor (SHVec4 color) noexcept; void ModifyLayer (uint8_t layerIndex, bool value) noexcept; void SetAllLayers (void) noexcept; void ClearAllLayers (void) noexcept; @@ -49,11 +49,13 @@ namespace SHADE void Unbind (void) noexcept; void SetBound (uint32_t inIndexInBuffer) noexcept; void SetActive (bool flag) noexcept; + void SetStrength (float value) noexcept; SHLightData const& GetLightData (void) const noexcept; bool IsDirty (void) const noexcept; bool GetBound (void) const noexcept; + bool GetActive (void) const noexcept; uint32_t GetIndexInBuffer (void) const noexcept; }; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightData.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightData.cpp index ba910408..8e8f0783 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightData.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightData.cpp @@ -15,7 +15,7 @@ namespace SHADE direction = SHVec3::Forward; // Diffuse color set to 1 - diffuseColor = SHVec4::One; + color = SHVec4::One; } } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightData.h b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightData.h index 607978a4..fccc2856 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightData.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightData.h @@ -10,6 +10,7 @@ namespace SHADE DIRECTIONAL = 0, POINT, SPOT, + AMBIENT, NUM_TYPES }; @@ -40,7 +41,10 @@ namespace SHADE uint32_t cullingMask; //! Diffuse color emitted by the light - SHVec4 diffuseColor; + SHVec4 color; + + //! Strength of the light + float strength; void Reset (void) noexcept; //! TODO: diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp index 6e8dd916..97d8b24f 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp @@ -42,13 +42,23 @@ namespace SHADE lightPtr->cullingMask = lightData.cullingMask; lightPtr->direction = lightData.direction; - lightPtr->diffuseColor = lightData.diffuseColor; + lightPtr->diffuseColor = lightData.color; + lightPtr->active = lightComp->GetActive(); break; } case SH_LIGHT_TYPE::POINT: break; case SH_LIGHT_TYPE::SPOT: break; + case SH_LIGHT_TYPE::AMBIENT: + { + SHAmbientLightData* lightPtr = reinterpret_cast(address); + lightPtr->ambientColor = lightData.color; + lightPtr->strength = lightData.strength; + lightPtr->cullingMask = lightData.cullingMask; + lightPtr->active = lightComp->GetActive (); + break; + } case SH_LIGHT_TYPE::NUM_TYPES: break; default: @@ -176,6 +186,9 @@ namespace SHADE case SH_LIGHT_TYPE::SPOT: // TOOD: Change after creating spot light struct return 4; + case SH_LIGHT_TYPE::AMBIENT: + return sizeof(SHAmbientLightData); + return 4; case SH_LIGHT_TYPE::NUM_TYPES: default: return 4; @@ -406,8 +419,8 @@ namespace SHADE { perTypeData[enumValue].AddLight(logicalDevice, &light, expanded); - // add to light count - ++lightCountsData[enumValue]; + //// add to light count + //++lightCountsData[enumValue]; } // if there was modification to the light data @@ -427,6 +440,11 @@ namespace SHADE data.WriteToGPU(frameIndex); } + for (uint32_t i = 0; i < NUM_LIGHT_TYPES; ++i) + { + lightCountsData[i] = perTypeData[i].GetNumLights(); + } + lightCountsBuffer->WriteToMemory(lightCountsData.data(), static_cast(lightCountsData.size()) * sizeof (uint32_t), 0, lightCountsAlignedSize * frameIndex); // If any of the buffers got expanded, the descriptor set is invalid because the expanded buffer @@ -444,8 +462,6 @@ namespace SHADE // so we do it anyway. #NoteToSelf: if at any point it affects performance, do a check before computing. ComputeDynamicOffsets(); - //cmdBuffer->ForceSetPipelineLayout() - // Bind descriptor set (We bind at an offset because the buffer holds NUM_FRAME_BUFFERS sets of data). cmdBuffer->BindDescriptorSet(lightingDataDescSet, SH_PIPELINE_TYPE::COMPUTE, SHGraphicsConstants::DescriptorSetIndex::DYNAMIC_GLOBALS, {dynamicOffsets[frameIndex]}); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h index efc6ddf6..dfb956ec 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h @@ -35,6 +35,26 @@ namespace SHADE }; + // Represents how the data will be interpreted in GPU. we want to copy to a container of these before passing to GPU. + struct SHAmbientLightData + { + //! Diffuse color emitted by the light + SHVec4 ambientColor; + + //! Strength of the ambient light + float strength; + + //! Represents if the light is active or not + uint32_t active; + + //! Each bit in this 32 bit field will represent a layer. If the bit is set, + //! when a fragment is being evaluated, the shader will use the fragment's + //! layer value to AND with the light's. If result is 1, do lighting calculations. + uint32_t cullingMask; + + + }; + class SH_API SHLightingSubSystem { private: diff --git a/TempShaderFolder/DeferredCompositeCs.glsl b/TempShaderFolder/DeferredCompositeCs.glsl index c707901f..bd482b1a 100644 --- a/TempShaderFolder/DeferredCompositeCs.glsl +++ b/TempShaderFolder/DeferredCompositeCs.glsl @@ -8,6 +8,14 @@ struct DirectionalLightStruct vec4 diffuseColor; }; +struct AmbientLightStruct +{ + vec4 ambientColor; + float strength; + uint isActive; + uint cullingMask; +}; + layout(local_size_x = 16, local_size_y = 16) in; layout(set = 4, binding = 0, rgba32f) uniform image2D positions; layout(set = 4, binding = 1, rgba32f) uniform image2D normals; @@ -19,6 +27,7 @@ layout(set = 1, binding = 0) uniform LightCounts uint directionalLights; uint pointLights; uint spotLights; + uint ambientLights; } lightCounts; @@ -27,6 +36,11 @@ layout(std430, set = 1, binding = 1) buffer DirectionalLightData DirectionalLightStruct dLightData[]; } DirLightData; +layout(std430, set = 1, binding = 4) buffer AmbientLightData +{ + AmbientLightStruct aLightData[]; +} AmbLightData; + void main() { // convenient variables @@ -55,6 +69,13 @@ void main() fragColor += DirLightData.dLightData[i].diffuseColor.rgb * diffuseStrength.rrr * pixelDiffuse; } + for (int i = 0; i < lightCounts.ambientLights; ++i) + { + // Just do some add + //fragColor += pixelDiffuse.rgb * AmbLightData.aLightData[i].ambientColor.rgb * vec3 (0.5f); + fragColor += pixelDiffuse.rgb * AmbLightData.aLightData[i].ambientColor.rgb * vec3 (AmbLightData.aLightData[i].strength); + } + // store result into result image imageStore(targetImage, ivec2(gl_GlobalInvocationID.xy), vec4(fragColor, 1.0f)); diff --git a/TempShaderFolder/DeferredCompositeCs.spv b/TempShaderFolder/DeferredCompositeCs.spv index 455ab5483f63c9c8c37d786c435e1d7ca6138b50..4ec7ac0f659951dcc4c1a5dcbdfd79bf13eaee50 100644 GIT binary patch delta 1472 zcmYk5%W6|m6oz;1Z8V^u)C(=qj6@N{iBQrK6zf1~MRepOO=FriO*A>FR$EW1X}#22 z8yq?C6_hv=pTUt2(2;K-UJ(4hlfC7f@U!;%*V=3Sd#|1D(NDQdESgIh6ESI%F};tO zKanFzlQQGRn3zypnyr@W-sNg#-fNkDGMbI}F*Bz~RY)lFwrnAkb}^ux3?Sn>chGN!SXqdgTUV0 z5&4_4^ih8AU1~VLMZk0>PbvogM&K@;zpZsdI3=xnWOoE|z;BP!n3$guHM)umj;s z1Ku|ulvI#Y7uZPPw+i9k5)eQ`)-*gQ+Ym;qkASv-fP(%?H)NLuZka<7!?Ksj-4%#W zn<3Tmjk#w@0(@K_nJqTWNx!LOmL- z;Lo=qbgj>WdLm?mX{H9Z^KpB;gOuzufw()&bFC44RbsfA7e>zwlFyMZwT7dR h<6c=VDm$an%<;9L0)~9zTS7^V5lmy^e^uq2@DG4#r_cZZ delta 407 zcmX|-OG*P#5Jhi)l4whV10{+?(CA2U90(#AITCdZCjNhkQ$+{vpr=CCAni=tf%|Zx zD-gkR+in{U^={Rzs`vV({kV`uVY?v(N|fs6IsFdum2feLYU*dL_%-{C-&eO4t1oF8 zVyN^I=cv@9{I34ywrrcfOXY1 zMI3=WW|)0Vj`OFa6W?#rK1XT*UTJOn5u~uIMzQ|5%sKDRqi^x@2Xq3iyLXm&3LCJm vT5;9$GDrB&UHs*Cib0j}vNZagdyH-oufPM`;JV*^z#?DRi(UU=N(X)c55_6?