2022-10-30 00:00:17 +08:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
struct DirectionalLightStruct
|
|
|
|
{
|
|
|
|
vec3 direction;
|
|
|
|
uint isActive;
|
|
|
|
uint cullingMask;
|
|
|
|
vec4 diffuseColor;
|
2023-01-07 17:45:49 +08:00
|
|
|
mat4 pvMatrix;
|
2023-01-09 11:06:10 +08:00
|
|
|
uint shadowData;
|
2022-10-30 00:00:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AmbientLightStruct
|
|
|
|
{
|
|
|
|
vec4 ambientColor;
|
|
|
|
float strength;
|
|
|
|
uint isActive;
|
|
|
|
uint cullingMask;
|
|
|
|
};
|
|
|
|
|
|
|
|
layout(local_size_x = 16, local_size_y = 16) in;
|
2022-12-28 20:47:20 +08:00
|
|
|
layout(set = 3, binding = 0, rgba32f) uniform image2D positions;
|
|
|
|
layout(set = 3, binding = 1, rgba32f) uniform image2D normals;
|
|
|
|
layout(set = 3, binding = 2, rgba8) uniform image2D albedo;
|
|
|
|
layout(set = 3, binding = 3, r32ui) uniform uimage2D lightLayerData;
|
|
|
|
layout(set = 3, binding = 4, r8) uniform image2D ssaoBlurredImage;
|
|
|
|
layout(set = 3, binding = 5, rgba8) uniform image2D targetImage;
|
2022-10-30 00:00:17 +08:00
|
|
|
|
2023-01-07 17:45:49 +08:00
|
|
|
layout (set = 4, binding = 0) uniform sampler2D shadowMaps[]; // for textures (global)
|
|
|
|
|
2022-10-30 00:00:17 +08:00
|
|
|
layout(set = 1, binding = 0) uniform LightCounts
|
|
|
|
{
|
|
|
|
uint directionalLights;
|
|
|
|
uint pointLights;
|
|
|
|
uint spotLights;
|
|
|
|
uint ambientLights;
|
|
|
|
|
|
|
|
} lightCounts;
|
|
|
|
|
|
|
|
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
|
|
|
|
ivec2 globalThread = ivec2(gl_GlobalInvocationID);
|
|
|
|
|
|
|
|
// Get the diffuse color of the pixel
|
|
|
|
vec3 pixelDiffuse = imageLoad (albedo, globalThread).rgb;
|
|
|
|
|
|
|
|
// Get position of fragment in world space
|
2022-10-30 19:21:02 +08:00
|
|
|
vec3 positionView = imageLoad (positions, globalThread).rgb;
|
2022-10-30 00:00:17 +08:00
|
|
|
|
|
|
|
// normal of fragment
|
2022-11-01 20:10:59 +08:00
|
|
|
vec3 normalView = imageLoad(normals, globalThread).rgb;
|
2022-10-30 00:00:17 +08:00
|
|
|
|
2022-11-02 14:21:27 +08:00
|
|
|
// light layer index
|
|
|
|
uint lightLayer = imageLoad (lightLayerData, globalThread).r;
|
|
|
|
|
2022-10-30 00:00:17 +08:00
|
|
|
vec3 fragColor = vec3 (0.0f);
|
|
|
|
|
2023-01-09 11:06:10 +08:00
|
|
|
vec4 shadowMapColor = vec4 (1.0f);
|
|
|
|
|
2022-10-30 00:00:17 +08:00
|
|
|
for (int i = 0; i < lightCounts.directionalLights; ++i)
|
|
|
|
{
|
2022-11-02 14:21:27 +08:00
|
|
|
if ((lightLayer & DirLightData.dLightData[i].cullingMask) != 0)
|
|
|
|
{
|
|
|
|
// get normalized direction of light
|
|
|
|
vec3 dLightNormalized = normalize (DirLightData.dLightData[i].direction);
|
2022-10-30 00:00:17 +08:00
|
|
|
|
2022-11-02 14:21:27 +08:00
|
|
|
// Get diffuse strength
|
|
|
|
float diffuseStrength = max (0, dot (dLightNormalized, normalView));
|
2022-10-30 00:00:17 +08:00
|
|
|
|
2022-11-02 14:21:27 +08:00
|
|
|
// Calculate the fragment color
|
|
|
|
fragColor += DirLightData.dLightData[i].diffuseColor.rgb * diffuseStrength.rrr * pixelDiffuse;
|
2023-01-09 11:06:10 +08:00
|
|
|
|
|
|
|
if ((DirLightData.dLightData[i].shadowData & uint(1)) == 1)
|
|
|
|
{
|
|
|
|
// calculate shadow map here
|
|
|
|
vec2 texCoord = vec2 (gl_GlobalInvocationID.xy) / vec2 (imageSize (targetImage));
|
|
|
|
shadowMapColor = texture (shadowMaps[0], texCoord).xxxx;
|
|
|
|
}
|
2022-11-02 14:21:27 +08:00
|
|
|
}
|
2022-10-30 00:00:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < lightCounts.ambientLights; ++i)
|
|
|
|
{
|
2022-11-02 14:21:27 +08:00
|
|
|
if ((lightLayer & AmbLightData.aLightData[i].cullingMask) != 0)
|
|
|
|
{
|
|
|
|
// 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-30 00:00:17 +08:00
|
|
|
}
|
|
|
|
|
2022-11-01 20:10:59 +08:00
|
|
|
float ssaoVal = imageLoad (ssaoBlurredImage, globalThread).r;
|
|
|
|
fragColor *= ssaoVal;
|
|
|
|
|
2022-10-30 00:00:17 +08:00
|
|
|
// store result into result image
|
2022-11-01 20:10:59 +08:00
|
|
|
imageStore(targetImage, ivec2(gl_GlobalInvocationID.xy), vec4(fragColor.rgb, 1.0f));
|
2023-01-09 11:06:10 +08:00
|
|
|
|
|
|
|
imageStore(targetImage, ivec2(gl_GlobalInvocationID.xy), shadowMapColor);
|
2022-11-01 20:10:59 +08:00
|
|
|
//imageStore(targetImage, ivec2(gl_GlobalInvocationID.xy), vec4(ssaoVal.rrr, 1.0f));
|
2022-10-30 00:00:17 +08:00
|
|
|
|
|
|
|
}
|