Sub pass G-Buffer compositing example (wip)

This commit is contained in:
saschawillems 2016-10-14 20:35:41 +02:00
parent 10b3d0b53c
commit 66682abe8c
12 changed files with 1276 additions and 0 deletions

View file

@ -0,0 +1,81 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (input_attachment_index = 1, binding = 0) uniform subpassInput samplerposition;
layout (input_attachment_index = 2, binding = 1) uniform subpassInput samplerNormal;
layout (input_attachment_index = 3, binding = 2) uniform subpassInput samplerAlbedo;
layout (location = 0) in vec2 inUV;
layout (location = 0) out vec4 outFragcolor;
layout (location = 1) out vec4 outPosition;
layout (location = 2) out vec4 outNormal;
layout (location = 3) out vec4 outAlbedo;
layout (constant_id = 0) const int NUM_LIGHTS = 32;
struct Light {
vec4 position;
vec3 color;
float radius;
};
layout (binding = 3) uniform UBO
{
vec4 viewPos;
Light lights[NUM_LIGHTS];
} ubo;
void main()
{
// Read G-Buffer values from previous sub pass
vec3 fragPos = subpassLoad(samplerposition).rgb;
vec3 normal = subpassLoad(samplerNormal).rgb;
vec4 albedo = subpassLoad(samplerAlbedo);
#define ambient 0.15
// Ambient part
vec3 fragcolor = albedo.rgb * ambient;
for(int i = 0; i < NUM_LIGHTS; ++i)
{
// Vector to light
vec3 L = ubo.lights[i].position.xyz - fragPos;
// Distance from light to fragment position
float dist = length(L);
// Viewer to fragment
vec3 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
vec3 N = normalize(normal);
float NdotL = max(0.0, dot(N, L));
vec3 diff = ubo.lights[i].color * albedo.rgb * NdotL * atten;
// Specular part
// Specular map values are stored in alpha of albedo mrt
vec3 R = reflect(-L, N);
float NdotR = max(0.0, dot(R, V));
vec3 spec = ubo.lights[i].color * albedo.a * pow(NdotR, 32.0) * atten;
fragcolor += diff + spec;
}
outFragcolor = vec4(fragcolor, 1.0);
// Write G-Buffer attachments to avoid undefined behaviour (validation error)
outPosition = vec4(0.0);
outNormal = vec4(0.0);
outAlbedo = vec4(0.0);
}

Binary file not shown.

View file

@ -0,0 +1,17 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (location = 0) out vec2 outUV;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
gl_Position = vec4(outUV * 2.0f - 1.0f, 0.0f, 1.0f);
}

Binary file not shown.

View file

@ -0,0 +1,30 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 1) uniform sampler2D samplerColor;
layout (binding = 2) uniform sampler2D samplerNormalMap;
layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec3 inColor;
layout (location = 2) in vec3 inWorldPos;
layout (location = 0) out vec4 outColor;
layout (location = 1) out vec4 outPosition;
layout (location = 2) out vec4 outNormal;
layout (location = 3) out vec4 outAlbedo;
void main()
{
outPosition = vec4(inWorldPos, 1.0);
vec3 N = normalize(inNormal);
N.y = -N.y;
outNormal = vec4(N, 1.0);
outAlbedo = vec4(inColor, 1.0);
// Write color attachments to avoid undefined behaviour (validation error)
outColor = vec4(0.0);
}

Binary file not shown.

View file

@ -0,0 +1,42 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (location = 0) in vec4 inPos;
layout (location = 1) in vec3 inColor;
layout (location = 2) in vec3 inNormal;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
mat4 view;
} ubo;
layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec3 outColor;
layout (location = 2) out vec3 outWorldPos;
layout (location = 3) out vec3 outTangent;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
gl_Position = ubo.projection * ubo.view * ubo.model * inPos;
// Vertex position in world space
outWorldPos = vec3(ubo.model * inPos);
// GL to Vulkan coord space
outWorldPos.y = -outWorldPos.y;
// Normal in world space
mat3 mNormal = transpose(inverse(mat3(ubo.model)));
outNormal = mNormal * normalize(inNormal);
// Currently just vertex color
outColor = inColor;
}

Binary file not shown.