2022-10-26 22:40:04 +08:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
struct DirectionalLightStruct
|
|
|
|
{
|
|
|
|
vec3 direction;
|
|
|
|
uint isActive;
|
|
|
|
uint cullingMask;
|
|
|
|
vec4 diffuseColor;
|
|
|
|
};
|
|
|
|
|
2022-10-27 19:02:55 +08:00
|
|
|
struct AmbientLightStruct
|
|
|
|
{
|
|
|
|
vec4 ambientColor;
|
|
|
|
float strength;
|
|
|
|
uint isActive;
|
|
|
|
uint cullingMask;
|
|
|
|
};
|
|
|
|
|
2022-10-26 22:40:04 +08:00
|
|
|
layout(local_size_x = 16, local_size_y = 16) in;
|
2022-10-27 11:31:11 +08:00
|
|
|
layout(set = 4, binding = 0, rgba32f) uniform image2D positions;
|
|
|
|
layout(set = 4, binding = 1, rgba32f) uniform image2D normals;
|
2022-10-27 02:28:38 +08:00
|
|
|
layout(set = 4, binding = 2, rgba8) uniform image2D albedo;
|
2022-10-27 19:37:05 +08:00
|
|
|
layout(set = 4, binding = 3, r32ui) uniform uimage2D lightLayerData;
|
|
|
|
layout(set = 4, binding = 4, rgba8) uniform image2D targetImage;
|
2022-10-26 22:40:04 +08:00
|
|
|
|
2022-10-27 02:28:38 +08:00
|
|
|
layout(set = 1, binding = 0) uniform LightCounts
|
|
|
|
{
|
|
|
|
uint directionalLights;
|
|
|
|
uint pointLights;
|
|
|
|
uint spotLights;
|
2022-10-27 19:02:55 +08:00
|
|
|
uint ambientLights;
|
2022-10-27 02:28:38 +08:00
|
|
|
|
|
|
|
} lightCounts;
|
|
|
|
|
|
|
|
layout(std430, set = 1, binding = 1) buffer DirectionalLightData
|
2022-10-26 22:40:04 +08:00
|
|
|
{
|
|
|
|
DirectionalLightStruct dLightData[];
|
|
|
|
} DirLightData;
|
|
|
|
|
2022-10-27 19:02:55 +08:00
|
|
|
layout(std430, set = 1, binding = 4) buffer AmbientLightData
|
|
|
|
{
|
|
|
|
AmbientLightStruct aLightData[];
|
|
|
|
} AmbLightData;
|
|
|
|
|
2022-10-26 22:40:04 +08:00
|
|
|
void main()
|
2022-10-27 02:28:38 +08:00
|
|
|
{
|
2022-10-26 22:40:04 +08:00
|
|
|
// convenient variables
|
|
|
|
ivec2 globalThread = ivec2(gl_GlobalInvocationID);
|
|
|
|
|
2022-10-27 02:28:38 +08:00
|
|
|
// Get the diffuse color of the pixel
|
|
|
|
vec3 pixelDiffuse = imageLoad (albedo, globalThread).rgb;
|
|
|
|
|
|
|
|
// Get position of fragment in world space
|
|
|
|
vec3 positionWorld = imageLoad (positions, globalThread).rgb;
|
|
|
|
|
|
|
|
// normal of fragment
|
|
|
|
vec3 normalWorld = imageLoad(normals, globalThread).rgb;
|
|
|
|
|
|
|
|
vec3 fragColor = vec3 (0.0f);
|
|
|
|
|
|
|
|
for (int i = 0; i < lightCounts.directionalLights; ++i)
|
|
|
|
{
|
|
|
|
// get normalized direction of light
|
2022-10-27 11:31:11 +08:00
|
|
|
vec3 dLightNormalized = normalize (DirLightData.dLightData[i].direction);
|
2022-10-27 02:28:38 +08:00
|
|
|
|
|
|
|
// Get diffuse strength
|
|
|
|
float diffuseStrength = max (0, dot (dLightNormalized, normalWorld));
|
|
|
|
|
2022-10-27 11:31:11 +08:00
|
|
|
// Calculate the fragment color
|
2022-10-27 02:28:38 +08:00
|
|
|
fragColor += DirLightData.dLightData[i].diffuseColor.rgb * diffuseStrength.rrr * pixelDiffuse;
|
|
|
|
}
|
2022-10-26 22:40:04 +08:00
|
|
|
|
2022-10-27 19:02:55 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-10-26 22:40:04 +08:00
|
|
|
// store result into result image
|
2022-10-27 02:46:38 +08:00
|
|
|
imageStore(targetImage, ivec2(gl_GlobalInvocationID.xy), vec4(fragColor, 1.0f));
|
2022-10-26 22:40:04 +08:00
|
|
|
|
|
|
|
}
|