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 455ab548..4ec7ac0f 100644 Binary files a/TempShaderFolder/DeferredCompositeCs.spv and b/TempShaderFolder/DeferredCompositeCs.spv differ