Ambient lighting added

This commit is contained in:
Brandon Mak 2022-10-27 19:02:55 +08:00
parent b7b3ef08ab
commit 09c5843cdb
9 changed files with 94 additions and 10 deletions

View File

@ -162,12 +162,22 @@ namespace Sandbox
SHComponentManager::AddComponent<SHLightComponent>(0);
SHComponentManager::RemoveComponent <SHRigidBodyComponent>(0);
SHComponentManager::RemoveComponent <SHColliderComponent>(0);
auto ambientLight = SHEntityManager::CreateEntity<SHLightComponent>();
SHComponentManager::GetComponent<SHLightComponent>(ambientLight)->SetColor(SHVec4(1.0f, 1.0f, 1.0f, 1.0f));
SHComponentManager::GetComponent<SHLightComponent>(ambientLight)->SetStrength(0.25f);
SHComponentManager::GetComponent<SHLightComponent>(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<SHLightComponent>(0);
lightComp->SetDirection (direction);
rotation += 0.005f;
//auto& transform = *SHADE::SHComponentManager::GetComponent_s<SHADE::SHTransformComponent>(testObj);
//transform.SetWorldPosition({1.0f, 1.0f, -1.0f});

View File

@ -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;

View File

@ -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;
};

View File

@ -15,7 +15,7 @@ namespace SHADE
direction = SHVec3::Forward;
// Diffuse color set to 1
diffuseColor = SHVec4::One;
color = SHVec4::One;
}
}

View File

@ -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:

View File

@ -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<SHAmbientLightData*>(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<uint32_t>(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]});

View File

@ -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:

View File

@ -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));