Shadows, broke OpenGL, broke graphics settings, Exception raised when closing software
This commit is contained in:
parent
f7127e0878
commit
0d41f3e15b
36 changed files with 1187 additions and 219 deletions
|
|
@ -0,0 +1,233 @@
|
|||
#version 450
|
||||
#extension GL_EXT_scalar_block_layout: require
|
||||
|
||||
// CREDITS: Most of the functions here have been obtained from this link: https://github.com/SaschaWillems/Vulkan
|
||||
// developed by Sascha Willems, https://twitter.com/JoeyDeVriez, and licensed under the terms of the MIT License (MIT)
|
||||
|
||||
layout (constant_id = 0) const int SHADOW_MAP_CASCADE_COUNT = 6;
|
||||
layout (constant_id = 1) const int DEBUG_SHADOWS = 0;
|
||||
const float PI = 3.14159265359;
|
||||
|
||||
struct Light {
|
||||
vec3 position;
|
||||
uint directional;
|
||||
float intensity;
|
||||
vec3 color;
|
||||
};
|
||||
struct CascadeShadow {
|
||||
mat4 projViewMatrix;
|
||||
vec4 splitDistance;
|
||||
};
|
||||
|
||||
layout(location = 0) in vec2 inTextCoord;
|
||||
|
||||
layout(location = 0) out vec4 outFragColor;
|
||||
|
||||
layout(set = 0, binding = 0) uniform sampler2D posSampler;
|
||||
layout(set = 0, binding = 1) uniform sampler2D albedoSampler;
|
||||
layout(set = 0, binding = 2) uniform sampler2D normalsSampler;
|
||||
layout(set = 0, binding = 3) uniform sampler2D pbrSampler;
|
||||
layout(set = 0, binding = 4) uniform sampler2DArray shadowSampler;
|
||||
|
||||
layout(scalar, set = 1, binding = 0) readonly buffer Lights {
|
||||
Light lights[];
|
||||
} lights;
|
||||
layout(set = 2, binding = 0) readonly buffer Shadows {
|
||||
CascadeShadow cascadeshadows[];
|
||||
} shadows;
|
||||
layout(scalar, set = 3, binding = 0) uniform SceneInfo {
|
||||
vec3 camPos;
|
||||
float ambientLightIntensity;
|
||||
vec3 ambientLightColor;
|
||||
uint numLights;
|
||||
mat4 viewMatrix;
|
||||
} sceneInfo;
|
||||
|
||||
float chebyshevUpperBound(vec2 moments, float t) {
|
||||
// Surface is fully lit if the current fragment is before the light occluder
|
||||
if (t <= moments.x)
|
||||
return 1.0;
|
||||
|
||||
// Compute variance
|
||||
float variance = moments.y - (moments.x * moments.x);
|
||||
variance = max(variance, 0.00002); // Small epsilon to avoid divide by zero
|
||||
|
||||
// Compute probabilistic upper bound
|
||||
float d = t - moments.x;
|
||||
float p_max = variance / (variance + d * d);
|
||||
|
||||
// Reduce light bleeding
|
||||
p_max = smoothstep(0.2, 1.0, p_max);
|
||||
|
||||
return p_max;
|
||||
}
|
||||
|
||||
float calcVisibility(vec4 worldPosition, uint cascadeIndex) {
|
||||
vec4 shadowMapPosition = shadows.cascadeshadows[cascadeIndex].projViewMatrix * worldPosition;
|
||||
|
||||
shadowMapPosition.xyz /= shadowMapPosition.w;
|
||||
|
||||
vec2 uv = vec2(
|
||||
shadowMapPosition.x * 0.5 + 0.5,
|
||||
shadowMapPosition.y * -0.5 + 0.5
|
||||
);
|
||||
|
||||
float depth = shadowMapPosition.z;
|
||||
|
||||
if (uv.x < 0.0 || uv.x > 1.0 ||
|
||||
uv.y < 0.0 || uv.y > 1.0 ||
|
||||
depth < 0.0 || depth > 1.0) {
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
vec2 moments = texture(shadowSampler, vec3(uv, cascadeIndex)).rg;
|
||||
|
||||
float visibility = chebyshevUpperBound(moments, depth);
|
||||
return visibility;
|
||||
}
|
||||
|
||||
float distributionGGX(vec3 N, vec3 H, float roughness) {
|
||||
float a = roughness * roughness;
|
||||
float a2 = a * a;
|
||||
float NdotH = max(dot(N, H), 0.0);
|
||||
float NdotH2 = NdotH * NdotH;
|
||||
|
||||
float nom = a2;
|
||||
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
|
||||
denom = PI * denom * denom;
|
||||
|
||||
return nom / denom;
|
||||
}
|
||||
|
||||
float geometrySchlickGGX(float NdotV, float roughness) {
|
||||
float r = (roughness + 1.0);
|
||||
float k = (r * r) / 8.0;
|
||||
|
||||
float nom = NdotV;
|
||||
float denom = NdotV * (1.0 - k) + k;
|
||||
|
||||
return nom / denom;
|
||||
}
|
||||
|
||||
float geometrySmith(vec3 N, vec3 V, vec3 L, float roughness) {
|
||||
float NdotV = max(dot(N, V), 0.0);
|
||||
float NdotL = max(dot(N, L), 0.0);
|
||||
float ggx2 = geometrySchlickGGX(NdotV, roughness);
|
||||
float ggx1 = geometrySchlickGGX(NdotL, roughness);
|
||||
|
||||
return ggx1 * ggx2;
|
||||
}
|
||||
|
||||
vec3 fresnelSchlick(float cosTheta, vec3 F0) {
|
||||
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
|
||||
}
|
||||
|
||||
vec3 calculatePointLight(Light light, vec3 worldPos, vec3 V, vec3 N, vec3 F0, vec3 albedo, float metallic, float roughness) {
|
||||
vec3 tmpSub = light.position - worldPos;
|
||||
vec3 L = normalize(tmpSub);
|
||||
vec3 H = normalize(V + L);
|
||||
|
||||
// Calculate distance and attenuation
|
||||
float distance = length(tmpSub);
|
||||
float attenuation = 1.0 / (distance * distance);
|
||||
float intensity = 10.0f;
|
||||
vec3 radiance = light.color * light.intensity * attenuation;
|
||||
|
||||
// Cook-Torrance BRDF
|
||||
float NDF = distributionGGX(N, H, roughness);
|
||||
float G = geometrySmith(N, V, L, roughness);
|
||||
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
|
||||
|
||||
vec3 numerator = NDF * G * F;
|
||||
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
|
||||
vec3 specular = numerator / denominator;
|
||||
|
||||
vec3 kS = F;
|
||||
vec3 kD = vec3(1.0) - kS;
|
||||
kD *= 1.0 - metallic;
|
||||
|
||||
float NdotL = max(dot(N, L), 0.0);
|
||||
return (kD * albedo / PI + specular) * radiance * NdotL;
|
||||
}
|
||||
|
||||
vec3 calculateDirectionalLight(Light light, vec3 V, vec3 N, vec3 F0, vec3 albedo, float metallic, float roughness) {
|
||||
vec3 L = normalize(-light.position);
|
||||
vec3 H = normalize(V + L);
|
||||
|
||||
vec3 radiance = light.color * light.intensity;
|
||||
|
||||
// Cook-Torrance BRDF
|
||||
float NDF = distributionGGX(N, H, roughness);
|
||||
float G = geometrySmith(N, V, L, roughness);
|
||||
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
|
||||
|
||||
vec3 numerator = NDF * G * F;
|
||||
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
|
||||
vec3 specular = numerator / denominator;
|
||||
|
||||
vec3 kS = F;
|
||||
vec3 kD = vec3(1.0) - kS;
|
||||
kD *= 1.0 - metallic;
|
||||
|
||||
float NdotL = max(dot(N, L), 0.0);
|
||||
return (kD * albedo / PI + specular) * radiance * NdotL;
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec3 albedo = texture(albedoSampler, inTextCoord).rgb;
|
||||
vec3 normal = texture(normalsSampler, inTextCoord).rgb;
|
||||
vec4 worldPosW = texture(posSampler, inTextCoord);
|
||||
vec3 worldPos = worldPosW.xyz;
|
||||
vec3 pbr = texture(pbrSampler, inTextCoord).rgb;
|
||||
|
||||
float roughness = pbr.g;
|
||||
float metallic = pbr.b;
|
||||
|
||||
vec3 N = normalize(normal);
|
||||
vec3 V = normalize(sceneInfo.camPos - worldPos);
|
||||
|
||||
vec3 F0 = vec3(0.04);
|
||||
F0 = mix(F0, albedo, metallic);
|
||||
|
||||
uint cascadeIndex = 0;
|
||||
vec4 viewPos = sceneInfo.viewMatrix * worldPosW;
|
||||
for (uint i = 0; i < SHADOW_MAP_CASCADE_COUNT - 1; ++i) {
|
||||
if (viewPos.z < shadows.cascadeshadows[i].splitDistance.x) {
|
||||
cascadeIndex = i + 1;
|
||||
}
|
||||
}
|
||||
float shadow = calcVisibility(vec4(worldPos, 1), cascadeIndex);
|
||||
|
||||
vec3 Lo = vec3(0.0);
|
||||
for (uint i = 0; i < sceneInfo.numLights; i++) {
|
||||
Light light = lights.lights[i];
|
||||
if (light.directional == 1) {
|
||||
Lo += calculateDirectionalLight(light, V, N, F0, albedo, metallic, roughness) * shadow;
|
||||
} else {
|
||||
Lo += calculatePointLight(light, worldPos, V, N, F0, albedo, metallic, roughness);
|
||||
}
|
||||
}
|
||||
vec3 ambient = sceneInfo.ambientLightColor * albedo * sceneInfo.ambientLightIntensity;
|
||||
outFragColor = vec4(Lo + ambient, 1.0f);
|
||||
|
||||
if (DEBUG_SHADOWS == 1) {
|
||||
switch (cascadeIndex) {
|
||||
case 0:
|
||||
outFragColor.rgb *= vec3(1.0f, 0.25f, 0.25f);
|
||||
break;
|
||||
case 1:
|
||||
outFragColor.rgb *= vec3(0.25f, 1.0f, 0.25f);
|
||||
break;
|
||||
case 2:
|
||||
outFragColor.rgb *= vec3(0.25f, 0.25f, 1.0f);
|
||||
break;
|
||||
default:
|
||||
outFragColor.rgb *= vec3(1.0f, 1.0f, 0.25f);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (length(normal) < 0.001) {
|
||||
outFragColor = vec4(albedo,1.0f);
|
||||
return;
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
|
@ -1,5 +1,51 @@
|
|||
#version 450
|
||||
|
||||
void main() {
|
||||
// Keep in sync manually with Java code
|
||||
const int MAX_TEXTURES = 256;
|
||||
|
||||
}
|
||||
layout (location = 0) in vec2 inTextCoords;
|
||||
layout (location = 1) in flat uint inMaterialIdx;
|
||||
|
||||
layout(location = 0) out vec2 outFragColor;
|
||||
|
||||
struct Material {
|
||||
vec4 diffuseColor;
|
||||
uint hasTexture;
|
||||
uint textureIdx;
|
||||
uint hasNormalMap;
|
||||
uint normalMapIdx;
|
||||
uint hasRoughMap;
|
||||
uint roughMapIdx;
|
||||
float roughnessFactor;
|
||||
float metallicFactor;
|
||||
};
|
||||
|
||||
layout(set = 1, binding = 0) uniform sampler2D textSampler[MAX_TEXTURES];
|
||||
layout(set = 2, binding = 0) readonly buffer MaterialUniform {
|
||||
Material materials[];
|
||||
} matUniform;
|
||||
|
||||
void main()
|
||||
{
|
||||
Material material = matUniform.materials[inMaterialIdx];
|
||||
vec4 albedo;
|
||||
if (material.hasTexture == 1) {
|
||||
albedo = texture(textSampler[material.textureIdx], inTextCoords);
|
||||
} else {
|
||||
albedo = material.diffuseColor;
|
||||
}
|
||||
if (albedo.a < 0.5) {
|
||||
discard;
|
||||
}
|
||||
|
||||
float depth = gl_FragCoord.z;
|
||||
float moment1 = depth;
|
||||
float moment2 = depth * depth;
|
||||
|
||||
// Adjust moments to avoid light bleeding
|
||||
float dx = dFdx(depth);
|
||||
float dy = dFdy(depth);
|
||||
moment2 += 0.25 * (dx * dx + dy * dy);
|
||||
|
||||
outFragColor = vec2(moment1, moment2);
|
||||
}
|
||||
|
|
@ -1,5 +1,29 @@
|
|||
#version 450
|
||||
|
||||
void main() {
|
||||
#define SHADOW_MAP_CASCADE_COUNT 6
|
||||
|
||||
layout (triangles, invocations = SHADOW_MAP_CASCADE_COUNT) in;
|
||||
layout (triangle_strip, max_vertices = 3) out;
|
||||
|
||||
layout (location = 0) in vec2 inTextCoords[];
|
||||
layout (location = 1) in flat uint inMaterialIdx[];
|
||||
|
||||
layout (location = 0) out vec2 outTextCoords;
|
||||
layout (location = 1) out flat uint outMaterialIdx;
|
||||
|
||||
layout(set = 0, binding = 0) uniform ProjUniforms {
|
||||
mat4 projViewMatrices[SHADOW_MAP_CASCADE_COUNT];
|
||||
} projUniforms;
|
||||
|
||||
void main()
|
||||
{
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
outTextCoords = inTextCoords[i];
|
||||
outMaterialIdx = inMaterialIdx[i];
|
||||
gl_Layer = gl_InvocationID;
|
||||
gl_Position = projUniforms.projViewMatrices[gl_InvocationID] * gl_in[i].gl_Position;
|
||||
EmitVertex();
|
||||
}
|
||||
EndPrimitive();
|
||||
}
|
||||
|
|
@ -1,5 +1,23 @@
|
|||
#version 450
|
||||
|
||||
void main() {
|
||||
layout(location = 0) in vec3 entityPos;
|
||||
layout(location = 1) in vec3 entityNormal;
|
||||
layout(location = 2) in vec3 entityTangent;
|
||||
layout(location = 3) in vec3 entityBitangent;
|
||||
layout(location = 4) in vec2 entityTextCoords;
|
||||
|
||||
}
|
||||
layout(push_constant) uniform matrices {
|
||||
mat4 modelMatrix;
|
||||
uint materialIdx;
|
||||
} push_constants;
|
||||
|
||||
layout (location = 0) out vec2 outTextCoord;
|
||||
layout (location = 1) out flat uint outMaterialIdx;
|
||||
|
||||
void main()
|
||||
{
|
||||
outTextCoord = entityTextCoords;
|
||||
outMaterialIdx = push_constants.materialIdx;
|
||||
|
||||
gl_Position = push_constants.modelMatrix * vec4(entityPos, 1.0f);
|
||||
}
|
||||
|
|
@ -7,7 +7,5 @@ layout(location = 1) out vec4 outColor;
|
|||
layout(set = 2, binding = 0) uniform samplerCube skyboxSampler;
|
||||
|
||||
void main() {
|
||||
vec3 textureIn = texture(skyboxSampler, outTexCoords).rgb;
|
||||
vec3 adjusted =vec3(0.0,textureIn.y/200.0,textureIn.z/150.0);
|
||||
outColor = vec4(adjusted, 1.0);
|
||||
outColor = texture(skyboxSampler, outTexCoords);// * vec4(0.0,0.05,0.1,1);
|
||||
}
|
||||
Binary file not shown.
|
|
@ -14,5 +14,5 @@ void main() {
|
|||
outTexCoords = inPosition;
|
||||
mat4 skyView = mat4(mat3(uboView.view));
|
||||
vec4 pos = uboProj.proj * skyView * vec4(inPosition, 1.0);
|
||||
gl_Position = vec4(pos.xy, 0.0, pos.w);
|
||||
gl_Position = vec4(pos.xy, pos.w, pos.w);
|
||||
}
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue