Use shader storage buffer to pass lights to shader

This commit is contained in:
Sascha Willems 2023-12-13 18:31:49 +01:00
parent a467d94159
commit 1a635f16ab
5 changed files with 53 additions and 95 deletions

View file

@ -1,11 +1,8 @@
// Copyright 2020 Google LLC
[[vk::input_attachment_index(0)]][[vk::binding(0)]] SubpassInput samplerposition;
[[vk::input_attachment_index(1)]][[vk::binding(1)]] SubpassInput samplerNormal;
[[vk::input_attachment_index(2)]][[vk::binding(2)]] SubpassInput samplerAlbedo;
#define MAX_NUM_LIGHTS 64
[[vk::constant_id(0)]] const int NUM_LIGHTS = 64;
[[vk::input_attachment_index(0)]][[vk::binding(0)]] SubpassInput inputPosition;
[[vk::input_attachment_index(1)]][[vk::binding(1)]] SubpassInput inputNormal;
[[vk::input_attachment_index(2)]][[vk::binding(2)]] SubpassInput inputAlbedo;
struct Light {
float4 position;
@ -13,56 +10,37 @@ struct Light {
float radius;
};
struct UBO
{
float4 viewPos;
Light lights[MAX_NUM_LIGHTS];
};
cbuffer ubo : register(b3) { UBO ubo; }
RWStructuredBuffer<Light> lights: register(u3);
float4 main([[vk::location(0)]] float2 inUV : TEXCOORD) : SV_TARGET
{
// Read G-Buffer values from previous sub pass
float3 fragPos = samplerposition.SubpassLoad().rgb;
float3 normal = samplerNormal.SubpassLoad().rgb;
float4 albedo = samplerAlbedo.SubpassLoad();
float3 fragPos = inputPosition.SubpassLoad().rgb;
float3 normal = inputNormal.SubpassLoad().rgb;
float4 albedo = inputAlbedo.SubpassLoad();
#define ambient 0.15
#define ambient 0.05
// Ambient part
float3 fragcolor = albedo.rgb * ambient;
for(int i = 0; i < NUM_LIGHTS; ++i)
uint lightsLength;
uint lightsStride;
lights.GetDimensions(lightsLength, lightsStride);
for(int i = 0; i < lightsLength; ++i)
{
// Vector to light
float3 L = ubo.lights[i].position.xyz - fragPos;
// Distance from light to fragment position
float3 L = lights[i].position.xyz - fragPos;
float dist = length(L);
// Viewer to fragment
float3 V = ubo.viewPos.xyz - fragPos;
V = normalize(V);
// Light to fragment
L = normalize(L);
// Attenuation
float atten = ubo.lights[i].radius / (pow(dist, 2.0) + 1.0);
// Diffuse part
float atten = lights[i].radius / (pow(dist, 3.0) + 1.0);
float3 N = normalize(normal);
float NdotL = max(0.0, dot(N, L));
float3 diff = ubo.lights[i].color * albedo.rgb * NdotL * atten;
float3 diff = lights[i].color * albedo.rgb * NdotL * atten;
// Specular part
// Specular map values are stored in alpha of albedo mrt
float3 R = reflect(-L, N);
float NdotR = max(0.0, dot(R, V));
//float3 spec = ubo.lights[i].color * albedo.a * pow(NdotR, 32.0) * atten;
fragcolor += diff;// + spec;
fragcolor += diff;
}
return float4(fragcolor, 1.0);