Commit 7ae49adf authored by Lionel Landwerlin's avatar Lionel Landwerlin

import Sascha Willems' shaders

parent 7f506e21
[require]
GLSL >= 4.5
[fragment shader]
#version 450 core
layout (location = 0) in vec2 inUV;
layout (binding = 0) uniform sampler2D samplerFont;
layout (location = 0) out vec4 outFragColor;
void main(void)
{
float color = texture(samplerFont, inUV).r;
outFragColor = vec4(vec3(color), 1.0);
}
[vertex shader]
#version 450 core
layout (location = 0) in vec2 inPos;
layout (location = 1) in vec2 inUV;
layout (location = 0) out vec2 outUV;
out gl_PerVertex
{
vec4 gl_Position;
};
void main(void)
{
gl_Position = vec4(inPos, 0.0, 1.0);
outUV = inUV;
}
[require]
GLSL >= 4.5
[fragment shader]
#version 450
layout (binding = 0) uniform sampler2D fontSampler;
layout (location = 0) in vec2 inUV;
layout (location = 1) in vec4 inColor;
layout (location = 0) out vec4 outColor;
void main()
{
outColor = inColor * texture(fontSampler, inUV);
}
[vertex shader]
#version 450
layout (location = 0) in vec2 inPos;
layout (location = 1) in vec2 inUV;
layout (location = 2) in vec4 inColor;
layout (push_constant) uniform PushConstants {
vec2 scale;
vec2 translate;
} pushConstants;
layout (location = 0) out vec2 outUV;
layout (location = 1) out vec4 outColor;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
outUV = inUV;
outColor = inColor;
gl_Position = vec4(inPos * pushConstants.scale + pushConstants.translate, 0.0, 1.0);
}
[require]
GLSL >= 4.5
[fragment shader]
#version 450
layout (binding = 1) uniform sampler2D samplerColor;
layout (binding = 0) uniform UBO
{
float blurScale;
float blurStrength;
} ubo;
layout (constant_id = 0) const int blurdirection = 0;
layout (location = 0) in vec2 inUV;
layout (location = 0) out vec4 outFragColor;
void main()
{
float weight[5];
weight[0] = 0.227027;
weight[1] = 0.1945946;
weight[2] = 0.1216216;
weight[3] = 0.054054;
weight[4] = 0.016216;
vec2 tex_offset = 1.0 / textureSize(samplerColor, 0) * ubo.blurScale; // gets size of single texel
vec3 result = texture(samplerColor, inUV).rgb * weight[0]; // current fragment's contribution
for(int i = 1; i < 5; ++i)
{
if (blurdirection == 1)
{
// H
result += texture(samplerColor, inUV + vec2(tex_offset.x * i, 0.0)).rgb * weight[i] * ubo.blurStrength;
result += texture(samplerColor, inUV - vec2(tex_offset.x * i, 0.0)).rgb * weight[i] * ubo.blurStrength;
}
else
{
// V
result += texture(samplerColor, inUV + vec2(0.0, tex_offset.y * i)).rgb * weight[i] * ubo.blurStrength;
result += texture(samplerColor, inUV - vec2(0.0, tex_offset.y * i)).rgb * weight[i] * ubo.blurStrength;
}
}
outFragColor = vec4(result, 1.0);
}
[vertex shader]
#version 450
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);
}
[require]
GLSL >= 4.5
[fragment shader]
#version 450
layout (binding = 1) uniform sampler2D samplerGradientRamp;
layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec3 inColor;
layout (location = 2) in vec3 inEyePos;
layout (location = 3) in vec3 inLightVec;
layout (location = 4) in vec2 inUV;
layout (location = 0) out vec4 outFragColor;
void main()
{
// No light calculations for glow color
// Use max. color channel value
// to detect bright glow emitters
if ((inColor.r >= 0.9) || (inColor.g >= 0.9) || (inColor.b >= 0.9))
{
outFragColor.rgb = texture(samplerGradientRamp, inUV).rgb;
}
else
{
vec3 Eye = normalize(-inEyePos);
vec3 Reflected = normalize(reflect(-inLightVec, inNormal));
vec4 IAmbient = vec4(0.2, 0.2, 0.2, 1.0);
vec4 IDiffuse = vec4(0.5, 0.5, 0.5, 0.5) * max(dot(inNormal, inLightVec), 0.0);
float specular = 0.25;
vec4 ISpecular = vec4(0.5, 0.5, 0.5, 1.0) * pow(max(dot(Reflected, Eye), 0.0), 4.0) * specular;
outFragColor = vec4((IAmbient + IDiffuse) * vec4(inColor, 1.0) + ISpecular);
}
}
[vertex shader]
#version 450
layout (location = 0) in vec4 pos;
layout (location = 2) in vec3 inColor;
layout (location = 3) in vec3 inNormal;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
float gradientPos;
} ubo;
layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec3 outColor;
layout (location = 2) out vec3 outEyePos;
layout (location = 3) out vec3 outLightVec;
layout (location = 4) out vec2 outUV;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
outNormal = inNormal;
outColor = inColor;
outUV = vec2(ubo.gradientPos, 0.0);
gl_Position = ubo.projection * ubo.model * pos;
outEyePos = vec3(ubo.model * pos);
vec4 lightPos = vec4(0.0, 0.0, -5.0, 1.0);// * ubo.model;
outLightVec = normalize(lightPos.xyz - pos.xyz);
}
[require]
GLSL >= 4.5
[fragment shader]
#version 450
layout (binding = 1) uniform sampler2D samplerColor;
layout (location = 0) in vec2 inUV;
layout (location = 1) in vec3 inNormal;
layout (location = 2) in vec3 inViewVec;
layout (location = 3) in vec3 inLightVec;
layout (location = 0) out vec4 outFragColor;
void main ()
{
vec3 color = texture(samplerColor, inUV).rgb;
vec3 N = normalize(inNormal);
vec3 L = normalize(inLightVec);
vec3 V = normalize(inViewVec);
vec3 R = reflect(-L, N);
vec3 diffuse = max(dot(N, L), 0.15) * vec3(1.0);
vec3 specular = pow(max(dot(R, V), 0.0), 8.0) * vec3(0.2);
outFragColor = vec4(diffuse * color.rgb + specular, 1.0);
}
[vertex shader]
#version 450
layout (location = 0) in vec3 inPos;
layout (location = 1) in vec2 inUV;
layout (location = 2) in vec3 inNormal;
layout (location = 0) out vec2 outUV;
layout (location = 1) out vec3 outNormal;
layout (location = 2) out vec3 outViewVec;
layout (location = 3) out vec3 outLightVec;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 modelview;
vec4 lightPos;
} ubo;
out gl_PerVertex
{
vec4 gl_Position;
};
void main ()
{
outUV = inUV;
outNormal = inNormal.xyz;
vec4 eyePos = ubo.modelview * vec4(inPos.x, inPos.y, inPos.z, 1.0);
gl_Position = ubo.projection * eyePos;
vec4 pos = vec4(inPos, 1.0);
vec3 lPos = ubo.lightPos.xyz;
outLightVec = lPos - pos.xyz;
outViewVec = -pos.xyz;
}
[compute shader]
#version 450
struct Particle {
vec4 pos;
vec4 vel;
vec4 uv;
vec4 normal;
float pinned;
};
layout(std430, binding = 0) buffer ParticleIn {
Particle particleIn[ ];
};
layout(std430, binding = 1) buffer ParticleOut {
Particle particleOut[ ];
};
// todo: use shared memory to speed up calculation
layout (local_size_x = 10, local_size_y = 10) in;
layout (binding = 2) uniform UBO
{
float deltaT;
float particleMass;
float springStiffness;
float damping;
float restDistH;
float restDistV;
float restDistD;
float sphereRadius;
vec4 spherePos;
vec4 gravity;
ivec2 particleCount;
} params;
layout (push_constant) uniform PushConsts {
uint calculateNormals;
} pushConsts;
vec3 springForce(vec3 p0, vec3 p1, float restDist)
{
vec3 dist = p0 - p1;
return normalize(dist) * params.springStiffness * (length(dist) - restDist);
}
void main()
{
uvec3 id = gl_GlobalInvocationID;
uint index = id.y * params.particleCount.x + id.x;
if (index > params.particleCount.x * params.particleCount.y)
return;
// Pinned?
if (particleIn[index].pinned == 1.0) {
particleOut[index].pos = particleOut[index].pos;
particleOut[index].vel = vec4(0.0);
return;
}
// Initial force from gravity
vec3 force = params.gravity.xyz * params.particleMass;
vec3 pos = particleIn[index].pos.xyz;
vec3 vel = particleIn[index].vel.xyz;
// Spring forces from neighboring particles
// left
if (id.x > 0) {
force += springForce(particleIn[index-1].pos.xyz, pos, params.restDistH);
}
// right
if (id.x < params.particleCount.x - 1) {
force += springForce(particleIn[index + 1].pos.xyz, pos, params.restDistH);
}
// upper
if (id.y < params.particleCount.y - 1) {
force += springForce(particleIn[index + params.particleCount.x].pos.xyz, pos, params.restDistV);
}
// lower
if (id.y > 0) {
force += springForce(particleIn[index - params.particleCount.x].pos.xyz, pos, params.restDistV);
}
// upper-left
if ((id.x > 0) && (id.y < params.particleCount.y - 1)) {
force += springForce(particleIn[index + params.particleCount.x - 1].pos.xyz, pos, params.restDistD);
}
// lower-left
if ((id.x > 0) && (id.y > 0)) {
force += springForce(particleIn[index - params.particleCount.x - 1].pos.xyz, pos, params.restDistD);
}
// upper-right
if ((id.x < params.particleCount.x - 1) && (id.y < params.particleCount.y - 1)) {
force += springForce(particleIn[index + params.particleCount.x + 1].pos.xyz, pos, params.restDistD);
}
// lower-right
if ((id.x < params.particleCount.x - 1) && (id.y > 0)) {
force += springForce(particleIn[index - params.particleCount.x + 1].pos.xyz, pos, params.restDistD);
}
force += (-params.damping * vel);
// Integrate
vec3 f = force * (1.0 / params.particleMass);
particleOut[index].pos = vec4(pos + vel * params.deltaT + 0.5 * f * params.deltaT * params.deltaT, 1.0);
particleOut[index].vel = vec4(vel + f * params.deltaT, 0.0);
// Sphere collision
vec3 sphereDist = particleOut[index].pos.xyz - params.spherePos.xyz;
if (length(sphereDist) < params.sphereRadius + 0.01) {
// If the particle is inside the sphere, push it to the outer radius
particleOut[index].pos.xyz = params.spherePos.xyz + normalize(sphereDist) * (params.sphereRadius + 0.01);
// Cancel out velocity
particleOut[index].vel = vec4(0.0);
}
// Normals
if (pushConsts.calculateNormals == 1) {
vec3 normal = vec3(0.0);
vec3 a, b, c;
if (id.y > 0) {
if (id.x > 0) {
a = particleIn[index - 1].pos.xyz - pos;
b = particleIn[index - params.particleCount.x - 1].pos.xyz - pos;
c = particleIn[index - params.particleCount.x].pos.xyz - pos;
normal += cross(a,b) + cross(b,c);
}
if (id.x < params.particleCount.x - 1) {
a = particleIn[index - params.particleCount.x].pos.xyz - pos;
b = particleIn[index - params.particleCount.x + 1].pos.xyz - pos;
c = particleIn[index + 1].pos.xyz - pos;
normal += cross(a,b) + cross(b,c);
}
}
if (id.y < params.particleCount.y - 1) {
if (id.x > 0) {
a = particleIn[index + params.particleCount.x].pos.xyz - pos;
b = particleIn[index + params.particleCount.x - 1].pos.xyz - pos;
c = particleIn[index - 1].pos.xyz - pos;
normal += cross(a,b) + cross(b,c);
}
if (id.x < params.particleCount.x - 1) {
a = particleIn[index + 1].pos.xyz - pos;
b = particleIn[index + params.particleCount.x + 1].pos.xyz - pos;
c = particleIn[index + params.particleCount.x].pos.xyz - pos;
normal += cross(a,b) + cross(b,c);
}
}
particleOut[index].normal = vec4(normalize(normal), 0.0f);
}
}
[require]
GLSL >= 4.5
[fragment shader]
#version 450
layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec3 inViewVec;
layout (location = 2) in vec3 inLightVec;
layout (location = 0) out vec4 outFragColor;
void main ()
{
vec3 color = vec3(0.5);
vec3 N = normalize(inNormal);
vec3 L = normalize(inLightVec);
vec3 V = normalize(inViewVec);
vec3 R = reflect(-L, N);
vec3 diffuse = max(dot(N, L), 0.15) * vec3(1.0);
vec3 specular = pow(max(dot(R, V), 0.0), 32.0) * vec3(1.0);
outFragColor = vec4(diffuse * color.rgb + specular, 1.0);
}
[vertex shader]
#version 450
layout (location = 0) in vec3 inPos;
layout (location = 2) in vec3 inNormal;
layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec3 outViewVec;
layout (location = 2) out vec3 outLightVec;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 modelview;
vec4 lightPos;
} ubo;
out gl_PerVertex
{
vec4 gl_Position;
};
void main ()
{
vec4 eyePos = ubo.modelview * vec4(inPos.x, inPos.y, inPos.z, 1.0);
gl_Position = ubo.projection * eyePos;
vec4 pos = vec4(inPos, 1.0);
vec3 lPos = ubo.lightPos.xyz;
outLightVec = lPos - pos.xyz;
outViewVec = -pos.xyz;
outNormal = inNormal;
}
[require]
GLSL >= 4.5
[compute shader]
#version 450
layout (constant_id = 0) const int MAX_LOD_LEVEL = 5;
struct InstanceData
{
vec3 pos;
float scale;
};
// Binding 0: Instance input data for culling
layout (binding = 0, std140) buffer Instances
{
InstanceData instances[ ];
};
// Same layout as VkDrawIndexedIndirectCommand
struct IndexedIndirectCommand
{
uint indexCount;
uint instanceCount;
uint firstIndex;
uint vertexOffset;
uint firstInstance;
};
// Binding 1: Multi draw output
layout (binding = 1, std430) writeonly buffer IndirectDraws
{
IndexedIndirectCommand indirectDraws[ ];
};
// Binding 2: Uniform block object with matrices
layout (binding = 2) uniform UBO
{
mat4 projection;
mat4 modelview;
vec4 cameraPos;
vec4 frustumPlanes[6];
} ubo;
// Binding 3: Indirect draw stats
layout (binding = 3) buffer UBOOut
{
uint drawCount;
uint lodCount[MAX_LOD_LEVEL + 1];
} uboOut;
// Binding 4: level-of-detail information
struct LOD
{
uint firstIndex;
uint indexCount;
float distance;
float _pad0;
};
layout (binding = 4) readonly buffer LODs
{
LOD lods[ ];
};
layout (local_size_x = 16) in;
bool frustumCheck(vec4 pos, float radius)
{
// Check sphere against frustum planes
for (int i = 0; i < 6; i++)
{
if (dot(pos, ubo.frustumPlanes[i]) + radius < 0.0)
{
return false;
}
}
return true;
}
layout (local_size_x = 16) in;
void main()
{
uint idx = gl_GlobalInvocationID.x + gl_GlobalInvocationID.y * gl_NumWorkGroups.x * gl_WorkGroupSize.x;
// Clear stats on first invocation
if (idx == 0)
{
atomicExchange(uboOut.drawCount, 0);
for (uint i = 0; i < MAX_LOD_LEVEL + 1; i++)
{
atomicExchange(uboOut.lodCount[i], 0);
}
}
vec4 pos = vec4(instances[idx].pos.xyz, 1.0);
// Check if object is within current viewing frustum
if (frustumCheck(pos, 1.0))
{
indirectDraws[idx].instanceCount = 1;
// Increase number of indirect draw counts
atomicAdd(uboOut.drawCount, 1);
// Select appropriate LOD level based on distance to camera
uint lodLevel = MAX_LOD_LEVEL;
for (uint i = 0; i < MAX_LOD_LEVEL; i++)
{
if (distance(instances[idx].pos.xyz, ubo.cameraPos.xyz) < lods[i].distance)
{
lodLevel = i;
break;
}
}
indirectDraws[idx].firstIndex = lods[lodLevel].firstIndex;
indirectDraws[idx].indexCount = lods[lodLevel].indexCount;
// Update stats
atomicAdd(uboOut.lodCount[lodLevel], 1);
}
else
{
indirectDraws[idx].instanceCount = 0;
}
}
[require]
GLSL >= 4.5
[compute shader]
#version 450
layout(binding = 0) buffer Pos {
uint values[ ];
};
layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout (constant_id = 0) const uint BUFFER_ELEMENTS = 32;
uint fibonacci(uint n) {
if(n <= 1){
return n;
}
uint curr = 1;
uint prev = 1;
for(uint i = 2; i < n; ++i) {
uint temp = curr;
curr += prev;
prev = temp;
}
return curr;
}
void main()
{
uint index = gl_GlobalInvocationID.x;
if (index >= BUFFER_ELEMENTS)
return;
values[index] = fibonacci(values[index]);
}
[require]
GLSL >= 4.5
[compute shader]
#version 450
struct Particle
{
vec4 pos;
vec4 vel;
};
// Binding 0 : Position storage buffer
layout(std140, binding = 0) buffer Pos
{
Particle particles[ ];
};
layout (local_size_x = 256) in;
layout (binding = 1) uniform UBO
{
float deltaT;
float destX;
float destY;
int particleCount;
} ubo;
layout (constant_id = 0) const int SHARED_DATA_SIZE = 512;