SSAO, performance sucks and it flickers so tweaking is needed, also more material support

This commit is contained in:
Halbear 2026-09-01 03:33:26 +01:00
parent 5b231d6920
commit 577836a03f
39 changed files with 889 additions and 74 deletions

View file

@ -26,6 +26,7 @@ layout(set = 0, binding = 3) uniform sampler2D pbrSampler;
layout(set = 0, binding = 4) uniform sampler2D emissiveSampler;
layout(set = 0, binding = 5) uniform sampler2D TranslucencySampler;
layout(set = 0, binding = 6) uniform sampler2D OpacitySampler;
layout(set = 0, binding = 7) uniform sampler2D ssaoBlur;
layout(scalar, set = 1, binding = 0) readonly buffer Lights {
Light lights[];
@ -134,6 +135,10 @@ void main() {
vec3 translucency = texture(TranslucencySampler, inTextCoord).rgb;
float emissiveness = emissive.r;
// outFragColor = vec4(vec3(texture(ssaoBlur, inTextCoord).r), 1);
// return;
float ssao = texture(ssaoBlur, inTextCoord).r;
float roughness = pbr.g;
float metallic = pbr.b;
@ -153,8 +158,10 @@ void main() {
Lo += calculatePointLight(light, worldPos, V, N, F0, albedo, metallic, roughness);
}
}
vec3 ambient = sceneInfo.ambientLightColor * albedo * sceneInfo.ambientLightIntensity;
outFragColor = vec4(Lo + ambient + vec3(emissiveness), 1.0f);
vec3 ambient = sceneInfo.ambientLightColor * albedo * sceneInfo.ambientLightIntensity * vec3(ssao,ssao,ssao);;
if(emissive.x > 0 || emissive.y > 0 || emissive.z > 0) ambient = emissive;
outFragColor = vec4(Lo + ambient, 1.0f);
outFragColor = vec4(outFragColor.xyz/2 + (outFragColor.xyz/2) * vec3(ssao,ssao,ssao),1);
// outFragColor = vec4(color, 1.0f);
if (length(normal) < 0.001) {

View file

@ -30,7 +30,8 @@ layout(set = 0, binding = 3) uniform sampler2D pbrSampler;
layout(set = 0, binding = 4) uniform sampler2D emissiveSampler;
layout(set = 0, binding = 5) uniform sampler2D TranslucencySampler;
layout(set = 0, binding = 6) uniform sampler2D OpacitySampler;
layout(set = 0, binding = 7) uniform sampler2DArray shadowSampler;
layout(set = 0, binding = 7) uniform sampler2D ssaoBlur;
layout(set = 0, binding = 8) uniform sampler2DArray shadowSampler;
layout(scalar, set = 1, binding = 0) readonly buffer Lights {
@ -203,6 +204,7 @@ void main() {
float emissiveness = emissive.r;
float translucencyf = translucency.r;
float ssao = texture(ssaoBlur, inTextCoord).r;
// outFragColor = vec4(emissive,1);
// return;
@ -240,9 +242,10 @@ void main() {
Lo += calculatePointLight(light, worldPos, V, N, F0, albedo, metallic, roughness);
}
}
vec3 ambient = sceneInfo.ambientLightColor * albedo * sceneInfo.ambientLightIntensity;
vec3 ambient = sceneInfo.ambientLightColor * albedo * sceneInfo.ambientLightIntensity * vec3(ssao,ssao,ssao);;
if(emissive.x > 0 || emissive.y > 0 || emissive.z > 0) ambient = emissive;
outFragColor = vec4(Lo + ambient, 1.0f);
outFragColor = vec4(outFragColor.xyz/2 + (outFragColor.xyz/2) * vec3(ssao,ssao,ssao),1);
if (DEBUG_SHADOWS == 1) {
switch (cascadeIndex) {

View file

@ -1,9 +0,0 @@
#version 450
layout(location = 0 ) out vec2 outTextCoord;
void main()
{
outTextCoord = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
gl_Position = vec4(outTextCoord.x * 2.0f - 1.0f, outTextCoord.y * -2.0f + 1.0f,0.0f,1.0f);
}

View file

@ -1,6 +1,6 @@
#version 450
const int MAX_TEXTURES = 256;
const int MAX_TEXTURES = 512;
layout(location = 0) in vec4 inPos;
layout(location = 1) in vec3 inNormal;
@ -44,7 +44,12 @@ layout(push_constant) uniform pc{
void main()
{
Material material = matUniform.materials[inMaterialIdx];
int textureIndex = int(material.textureIdx);
if (textureIndex >= MAX_TEXTURES){
outAlbedo = vec4(0.0,0.0,1.0,1.0);
return;
}
Material material = matUniform.materials[push_constants.materialIdx];
if(material.hasTexture == 1){
vec4 texColor = texture(textSampler[material.textureIdx],inTextCoords);

View file

@ -1,6 +1,7 @@
#version 450
const int MAX_TEXTURES = 256;
const int MAX_TEXTURES = 512;
layout(location = 0) in vec4 inPos;
layout(location = 1) in vec3 inNormal;
@ -62,6 +63,16 @@ void main()
outPos = inPos;
Material material = matUniform.materials[push_constants.materialIdx];
int textureIndex = int(material.textureIdx);
if (textureIndex >= MAX_TEXTURES){
outAlbedo = vec4(0.0,0.0,1.0,1.0);
outOpacity = vec4(0.0,0.0,1.0,1.0);
outNormal = vec4(0.0,0.0,1.0,1.0);
outEmissive = vec4(0.0,0.0,1.0,1.0);
outTranslucency = vec4(0.0,0.0,1.0,1.0);
outPBR = vec4(0.0,0.0,1.0,1.0);
return;
}
if (material.hasTexture == 1) {
outAlbedo = texture(textSampler[material.textureIdx], inTextCoords);
} else {

View file

@ -1,6 +1,7 @@
#version 450
const int MAX_TEXTURES = 256;
const int MAX_TEXTURES = 512;
layout(location = 0) in vec4 inPos;
layout(location = 1) in vec3 inNormal;
@ -44,6 +45,11 @@ layout(push_constant) uniform pc{
void main()
{
Material material = matUniform.materials[push_constants.materialIdx];
int textureIndex = int(material.textureIdx);
if (textureIndex >= MAX_TEXTURES){
outAlbedo = vec4(0.0,0.0,1.0,1.0);
return;
}
if(material.hasTexture == 1){
vec4 texColor = texture(textSampler[material.textureIdx],inTextCoords);
outAlbedo = texColor;

View file

@ -1,6 +1,6 @@
#version 450
const int MAX_TEXTURES = 256;
const int MAX_TEXTURES = 512;
layout(location = 0) in vec4 inPos;
layout(location = 1) in vec3 inNormal;
@ -69,6 +69,16 @@ void main()
outPos = inPos;
Material material = matUniform.materials[push_constants.materialIdx];
int textureIndex = int(material.textureIdx);
if (textureIndex >= MAX_TEXTURES){
outAlbedo = vec4(0.0,0.0,1.0,1.0);
outOpacity = vec4(0.0,0.0,1.0,1.0);
outNormal = vec4(0.0,0.0,1.0,1.0);
outEmissive = vec4(0.0,0.0,1.0,1.0);
outTranslucency = vec4(0.0,0.0,1.0,1.0);
outPBR = vec4(0.0,0.0,1.0,1.0);
return;
}
if (material.hasTexture == 1) {
outAlbedo = texture(textSampler[material.textureIdx], inTextCoords);
} else {

View file

@ -1,6 +1,6 @@
#version 450
const int MAX_TEXTURES = 256;
const int MAX_TEXTURES = 512;
layout(location = 0) in vec4 inPos;
layout(location = 1) in vec3 inNormal;
@ -43,6 +43,11 @@ layout(push_constant) uniform pc{
void main()
{
Material material = matUniform.materials[push_constants.materialIdx];
int textureIndex = int(material.textureIdx);
if (textureIndex >= MAX_TEXTURES){
outAlbedo = vec4(0.0,0.0,1.0,1.0);
return;
}
if(material.hasTexture == 1){
vec4 texColor = texture(textSampler[material.textureIdx],inTextCoords);
outAlbedo = texColor;
@ -60,4 +65,5 @@ void main()
if(opacityf >= 0.9){discard;}
outAlbedo = vec4(outAlbedo.rgb, opacityf);
}

View file

@ -1,6 +1,6 @@
#version 450
const int MAX_TEXTURES = 256;
const int MAX_TEXTURES = 512;
layout(location = 0) in vec4 inPos;
layout(location = 1) in vec3 inNormal;
@ -69,8 +69,17 @@ layout(push_constant) uniform pc {
void main()
{
outPos = inPos;
Material material = matUniform.materials[push_constants.materialIdx];
int textureIndex = int(material.textureIdx);
if (textureIndex >= MAX_TEXTURES){
outAlbedo = vec4(0.0,0.0,1.0,1.0);
outOpacity = vec4(0.0,0.0,1.0,1.0);
outNormal = vec4(0.0,0.0,1.0,1.0);
outEmissive = vec4(0.0,0.0,1.0,1.0);
outTranslucency = vec4(0.0,0.0,1.0,1.0);
outPBR = vec4(0.0,0.0,1.0,1.0);
return;
}
if (material.hasTexture == 1) {
outAlbedo = texture(textSampler[material.textureIdx], inTextCoords);
} else {

View file

@ -0,0 +1,104 @@
#version 450
layout(location = 0) in vec2 inTextCoord;
layout(location = 0) out float outAO;
layout(set = 0, binding = 0) uniform sampler2D posSampler;
layout(set = 0, binding = 1) uniform sampler2D normalSampler;
layout(set = 0, binding = 2) uniform sampler2D noiseSampler;
layout(set = 1, binding = 0) readonly buffer SSAOKernel {
vec4 samples[];
} kernel;
layout(set = 2, binding = 0) uniform SSAOInfo {
mat4 projection; //64
mat4 view; //128
vec2 screenSize; // 136
float radius; //140
float bias; //144
int kernelSize; //160
} ssao;
void main() {
vec2 uv = inTextCoord;
vec3 worldPos = texture(posSampler, uv).xyz;
vec3 worldNormal = normalize(texture(normalSampler, uv).xyz);
if (length(worldNormal) < 0.001) {
outAO = 1.0;
return;
}
vec3 fragPos = vec3(ssao.view * vec4(worldPos, 1.0));
vec3 normal = normalize(mat3(ssao.view) * worldNormal);
vec2 noiseScale = ssao.screenSize / 4.0;
vec3 randomVec = normalize(texture(noiseSampler, inTextCoord * noiseScale).xyz);
vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
vec3 bitangent = cross(normal, tangent);
mat3 TBN = mat3(tangent, bitangent, normal);
float occlusion = 0.0;
float validSamples = 0.0;;
for (int i = 0; i < ssao.kernelSize; i++) {
vec3 samplePos = TBN * kernel.samples[i].xyz;
samplePos = fragPos + samplePos * ssao.radius;
vec4 offset = vec4(samplePos, 1.0);
offset = ssao.projection * offset;
offset.xyz /= offset.w;
offset.xyz = offset.xyz * 0.5 + 0.5;
vec2 sampleUV = offset.xy;
sampleUV.y = 1.0 - sampleUV.y;
if (sampleUV.x < 0.0 || sampleUV.x > 1.0 ||
sampleUV.y < 0.0 || sampleUV.y > 1.0) {
continue;
}
vec3 sampleWorldPos = texture(posSampler, sampleUV).xyz;
vec3 sampleWorldNormal = texture(normalSampler, sampleUV).xyz;
if (length(sampleWorldNormal) < 0.001) {
continue;
}
vec3 sampleViewPos = vec3(ssao.view * vec4(sampleWorldPos, 1.0));
float depthDelta = abs(fragPos.z - sampleViewPos.z);
occlusion += abs(fragPos.z)/200.0f;
validSamples += 1.0;
continue;
if (depthDelta > ssao.radius) {
continue;
}
float rangeCheck = 1.0 - depthDelta / ssao.radius;
rangeCheck = rangeCheck * rangeCheck * (3.0 - 2.0 * rangeCheck);
if (sampleViewPos.z >= samplePos.z + ssao.bias) {
occlusion += rangeCheck;
}
validSamples += 1.0;
}
if (validSamples <= 0.0) {
outAO = 1.0;
return;
}
occlusion = 1.0 - occlusion / validSamples;
outAO = clamp(occlusion, 0.0, 1.0);
}

View file

@ -1,7 +1,7 @@
#version 450
// Keep in sync manually with Java code
const int MAX_TEXTURES = 256;
const int MAX_TEXTURES = 512;
layout (location = 0) in vec2 inTextCoords;
layout (location = 1) in flat uint inMaterialIdx;
@ -36,6 +36,11 @@ layout(set = 2, binding = 0) readonly buffer MaterialUniform {
void main()
{
Material material = matUniform.materials[inMaterialIdx];
int textureIndex = int(material.textureIdx);
if (textureIndex >= MAX_TEXTURES){
outFragColor = vec2(0.0,0.0);
return;
}
vec4 albedo;
if (material.hasTexture == 1) {
albedo = texture(textSampler[material.textureIdx], inTextCoords);

View file

@ -0,0 +1,21 @@
#version 450
layout(location = 0) in vec2 inTextCoord;
layout(location = 0) out float outAO;
layout(set = 0, binding = 0) uniform sampler2D ssaoSampler;
void main() {
vec2 texelSize = 1.0 / vec2(textureSize(ssaoSampler, 0));
float result = 0.0;
for (int x = -2; x <= 2; x++) {
for (int y = -2; y <= 2; y++) {
vec2 offset = vec2(float(x), float(y)) * texelSize;
result += texture(ssaoSampler, vec2(inTextCoord.x,inTextCoord.y) + offset).r;
}
}
outAO = result / 25.0;
}

View file

@ -0,0 +1,95 @@
#version 450
layout(location = 0) in vec2 inTextCoord;
layout(location = 0) out float outAO;
layout(set = 0, binding = 0) uniform sampler2D posSampler;
layout(set = 0, binding = 1) uniform sampler2D normalSampler;
layout(set = 0, binding = 2) uniform sampler2D noiseSampler;
layout(set = 1, binding = 0) readonly buffer SSAOKernel {
vec4 samples[];
} kernel;
layout(set = 2, binding = 0) uniform SSAOInfo {
mat4 projection; //64
mat4 view; //128
vec2 screenSize; // 136
float radius; //140
float bias; //144
int kernelSize; //160
} ssao;
void main() {
vec2 uv = inTextCoord;
vec3 worldPos = texture(posSampler, uv).xyz;
vec3 worldNormal = normalize(texture(normalSampler, uv).xyz);
vec2 noiseScale = ssao.screenSize / 4.0;
if (length(worldNormal) < 0.001) {
outAO = 1.0;
return;
}
vec3 fragPos = vec3(ssao.view * vec4(worldPos, 1.0));
vec3 normal = normalize(mat3(ssao.view) * worldNormal);
vec3 randomVec = normalize(texture(noiseSampler, inTextCoord * noiseScale).xyz);
vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
vec3 bitangent = cross(normal, tangent);
mat3 TBN = mat3(tangent, bitangent, normal);
float occlusion = 0.0;
float validSamples = 0.0;;
for (int i = 0; i < ssao.kernelSize; i++) {
vec3 samplePos = TBN * kernel.samples[i].xyz;
samplePos = fragPos + samplePos * ssao.radius;
vec4 offset = vec4(samplePos, 1.0);
offset = ssao.projection * offset;
offset.xyz /= offset.w;
offset.xyz = offset.xyz * 0.5 + 0.5;
vec2 sampleUV = offset.xy;
sampleUV.y = 1 - sampleUV.y;
if (sampleUV.x < 0.0 || sampleUV.x > 1.0 ||
sampleUV.y < 0.0 || sampleUV.y > 1.0) {
continue;
}
vec3 sampleWorldPos = texture(posSampler, sampleUV).xyz;
vec3 sampleWorldNormal = texture(normalSampler, sampleUV).xyz;
if (length(sampleWorldNormal) < 0.001) {
continue;
}
vec3 sampleViewPos = vec3(ssao.view * vec4(sampleWorldPos, 1.0));
float depthDelta = abs(fragPos.z - sampleViewPos.z);
if (depthDelta > ssao.radius) {
continue;
}
float rangeCheck = 1.0 - depthDelta / ssao.radius;
rangeCheck = rangeCheck * rangeCheck * (3.0 - 2.0 * rangeCheck);
if (sampleViewPos.z >= samplePos.z + ssao.bias) {
occlusion += rangeCheck;
}
validSamples += 1;
}
if (validSamples <= 0.0) {
outAO = 1.0;
return;
}
occlusion = 1.0 - occlusion / validSamples;
outAO = occlusion * pow(occlusion, 3.0);
}

View file

@ -1,8 +0,0 @@
#version 450
layout(location = 0) out vec2 outTextCoord;
void main() {
outTextCoord = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
gl_Position = vec4(outTextCoord.x * 2.0f - 1.0f, outTextCoord.y * -2.0f + 1.0f,0.0f,1.0f);
}