Use shader storage buffer to pass lights to shader
This commit is contained in:
parent
a467d94159
commit
1a635f16ab
5 changed files with 53 additions and 95 deletions
|
|
@ -1,50 +1,47 @@
|
|||
#version 450
|
||||
|
||||
layout (input_attachment_index = 0, binding = 0) uniform subpassInput samplerposition;
|
||||
layout (input_attachment_index = 1, binding = 1) uniform subpassInput samplerNormal;
|
||||
layout (input_attachment_index = 2, binding = 2) uniform subpassInput samplerAlbedo;
|
||||
layout (input_attachment_index = 0, binding = 0) uniform subpassInput inputPosition;
|
||||
layout (input_attachment_index = 1, binding = 1) uniform subpassInput inputNormal;
|
||||
layout (input_attachment_index = 2, binding = 2) uniform subpassInput inputAlbedo;
|
||||
|
||||
layout (location = 0) in vec2 inUV;
|
||||
|
||||
layout (location = 0) out vec4 outColor;
|
||||
|
||||
layout (constant_id = 0) const int NUM_LIGHTS = 64;
|
||||
|
||||
struct Light {
|
||||
vec4 position;
|
||||
vec3 color;
|
||||
float radius;
|
||||
};
|
||||
|
||||
layout (binding = 3) uniform UBO
|
||||
layout (std140, binding = 3) buffer LightsBuffer
|
||||
{
|
||||
Light lights[NUM_LIGHTS];
|
||||
} ubo;
|
||||
|
||||
Light lights[];
|
||||
};
|
||||
|
||||
void main()
|
||||
{
|
||||
// Read G-Buffer values from previous sub pass
|
||||
vec3 fragPos = subpassLoad(samplerposition).rgb;
|
||||
vec3 normal = subpassLoad(samplerNormal).rgb;
|
||||
vec4 albedo = subpassLoad(samplerAlbedo);
|
||||
vec3 fragPos = subpassLoad(inputPosition).rgb;
|
||||
vec3 normal = subpassLoad(inputNormal).rgb;
|
||||
vec4 albedo = subpassLoad(inputAlbedo);
|
||||
|
||||
#define ambient 0.05
|
||||
|
||||
// Ambient part
|
||||
vec3 fragcolor = albedo.rgb * ambient;
|
||||
|
||||
for(int i = 0; i < NUM_LIGHTS; ++i)
|
||||
for(int i = 0; i < lights.length(); ++i)
|
||||
{
|
||||
vec3 L = ubo.lights[i].position.xyz - fragPos;
|
||||
vec3 L = lights[i].position.xyz - fragPos;
|
||||
float dist = length(L);
|
||||
|
||||
L = normalize(L);
|
||||
float atten = ubo.lights[i].radius / (pow(dist, 3.0) + 1.0);
|
||||
float atten = lights[i].radius / (pow(dist, 3.0) + 1.0);
|
||||
|
||||
vec3 N = normalize(normal);
|
||||
float NdotL = max(0.0, dot(N, L));
|
||||
vec3 diff = ubo.lights[i].color * albedo.rgb * NdotL * atten;
|
||||
vec3 diff = lights[i].color * albedo.rgb * NdotL * atten;
|
||||
|
||||
fragcolor += diff;
|
||||
}
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue