SSAO updated and certifiably working

This commit is contained in:
Peter Li 2025-05-11 22:16:50 -07:00
parent 0e5ffc847d
commit c62ab1ec84
16 changed files with 208 additions and 143 deletions

View File

@ -188,7 +188,8 @@ PixelOutput main(
float3 WorldPos: TEXCOORD1,
float3 Normal: TEXCOORD2,
float4 DirectionalShadowFragPos: TEXCOORD3,
uint Instance: TEXCOORD4
uint Instance: TEXCOORD4,
float3 ScreenNormal: TEXCOORD5
) {
float4 s;
@ -254,7 +255,7 @@ PixelOutput main(
PixelOutput o;
o.Position = float4(WorldPos, 1.0);
o.Normal = float4(Normal, 1.0);
o.Normal = float4(ScreenNormal, 1.0);
o.Color = float4(c2, alpha);
o.Emissive = float4(0.0, 0.0, 0.0, 1.0);

View File

@ -6,7 +6,7 @@
}
],
"types" : {
"_22" : {
"_23" : {
"name" : "type.Uniforms",
"members" : [
{
@ -46,7 +46,7 @@
}
]
},
"_25" : {
"_26" : {
"name" : "Scene",
"members" : [
{
@ -78,12 +78,12 @@
}
]
},
"_24" : {
"_25" : {
"name" : "type.StructuredBuffer.Scene",
"members" : [
{
"name" : "_m0",
"type" : "_25",
"type" : "_26",
"array" : [
0
],
@ -121,6 +121,11 @@
"type" : "uint",
"name" : "in.var.TEXCOORD4",
"location" : 4
},
{
"type" : "vec3",
"name" : "in.var.TEXCOORD5",
"location" : 5
}
],
"outputs" : [
@ -199,7 +204,7 @@
],
"ssbos" : [
{
"type" : "_24",
"type" : "_25",
"name" : "scene",
"readonly" : true,
"block_size" : 0,
@ -209,7 +214,7 @@
],
"ubos" : [
{
"type" : "_22",
"type" : "_23",
"name" : "type.Uniforms",
"block_size" : 80,
"set" : 3,

View File

@ -19,6 +19,7 @@ struct Output
float3 Normal: TEXCOORD2;
float4 DirectionalShadowFragPos: TEXCOORD3;
uint Instance: TEXCOORD4;
float3 ScreenNormal: TEXCOORD5;
float4 Position : SV_Position;
};
@ -40,6 +41,7 @@ StructuredBuffer<Scene> scene: register(t0, space0);
cbuffer Uniforms : register(b0, space1)
{
float4x4 ViewProjection;
float4x4 NoTranslateView;
float4x4 ShadowMapProjection;
float time;
};
@ -82,6 +84,10 @@ Output main(Input input)
float4x4 noTranslate = ExtractRotation(scene[input.Instance].Model);
output.Normal = normalize(mul(noTranslate, float4(input.Normal, 1.0))).xyz;
float4x4 noTranslateScreen = mul(NoTranslateView, noTranslate);
output.ScreenNormal = mul(noTranslateScreen, float4(input.Normal, 1.0)).xyz;
output.DirectionalShadowFragPos = mul(ShadowMapProjection, WorldPos);
output.Instance = input.Instance;

View File

@ -6,7 +6,7 @@
}
],
"types" : {
"_14" : {
"_15" : {
"name" : "Scene",
"members" : [
{
@ -38,12 +38,12 @@
}
]
},
"_13" : {
"_14" : {
"name" : "type.StructuredBuffer.Scene",
"members" : [
{
"name" : "_m0",
"type" : "_14",
"type" : "_15",
"array" : [
0
],
@ -55,7 +55,7 @@
}
]
},
"_16" : {
"_17" : {
"name" : "type.Uniforms",
"members" : [
{
@ -66,16 +66,23 @@
"row_major" : true
},
{
"name" : "ShadowMapProjection",
"name" : "NoTranslateView",
"type" : "mat4",
"offset" : 64,
"matrix_stride" : 16,
"row_major" : true
},
{
"name" : "ShadowMapProjection",
"type" : "mat4",
"offset" : 128,
"matrix_stride" : 16,
"row_major" : true
},
{
"name" : "time",
"type" : "float",
"offset" : 128
"offset" : 192
}
]
}
@ -122,11 +129,16 @@
"type" : "uint",
"name" : "out.var.TEXCOORD4",
"location" : 4
},
{
"type" : "vec3",
"name" : "out.var.TEXCOORD5",
"location" : 5
}
],
"ssbos" : [
{
"type" : "_13",
"type" : "_14",
"name" : "scene",
"readonly" : true,
"block_size" : 0,
@ -136,9 +148,9 @@
],
"ubos" : [
{
"type" : "_16",
"type" : "_17",
"name" : "type.Uniforms",
"block_size" : 132,
"block_size" : 196,
"set" : 1,
"binding" : 0
}

View File

@ -130,10 +130,15 @@ float4 main(float2 UV : TEXCOORD0) : SV_Target0
// c = float3(uv.x, uv.y, 0.0);
// c = SsaoTexture.Sample(Sampler2, uv).x;
// float ssao = SsaoTexture.Sample(Sampler2, uv).x;
// float4 ssao = SsaoTexture.Sample(Sampler2, uv);
float ssao = blurSSAO(uv);
c = c * ssao;
// float4 x = SsaoTexture.Sample(Sampler2, uv);
// c = x;
// c = ssao.xyz;
c.x *= 1.0 + 0.000001 * EmissiveTexture.Sample(Sampler1, uv).x;
c.x *= 1.0 + 0.000001 * ColorTexture.Sample(Sampler0, uv).x;

View File

@ -34,46 +34,66 @@ float4 main(float2 UV: TEXCOORD0) : SV_Target0
{
return 1.0;
}
float3 normal = normalize(mul(View, NormalTexture.Sample(Sampler1, uv)).xyz);
float3 normal = NormalTexture.Sample(Sampler1, uv).xyz;
float3 randomVec = normalize(NoiseTexture.Sample(Sampler2, uv * scale).xyz);
// randomVec = normalize(float3(1.0, 1.0, 0.0) + randomVec * 0.00001);
float3 x = normal;
float3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
float3 bitangent = cross(normal, tangent);
float3x3 TBN = float3x3(tangent, bitangent, normal);
float3x3 TBN = transpose(float3x3(tangent, bitangent, normal));
x = tangent;
float occlusion = 0.0;
x = float3(0.0,0.0,0.0);
for(int i = 0; i < numSamples; i += 1)
{
float3 samplePos = mul(TBN, SamplingKernel[i]); // from tangent to view-space
float3 samplePos = mul(TBN, SamplingKernel[i] + float3(0.0, 0.0, 0.01)); //float3(0.0, 0.0, 1.0)); // from tangent to view-space
samplePos = position + samplePos * radius;
// project sample position (to sample texture) (to get position on screen/texture)
float4 offset = float4(samplePos, 1.0);
// c = offset - float4(position, 1.0);
offset = mul(Projection, offset); // from view to clip-space
offset.xyz /= offset.w; // perspective divide
offset.xyz = offset.xyz * 0.5 + 0.5; // transform to range 0.0 - 1.0
offset.y *= -1;
offset.y += 1.0;
// x += (float3(offset.xy - uv, 0.0) * 0.5 + 0.5) / numSamples;
// get sample depth
float sampleDepth = mul(View, PositionTexture.Sample(Sampler0, offset.xy)).z; // texture(gPosition, offset.xy).z; // get depth value of kernel sample
x += float3(sampleDepth, sampleDepth, sampleDepth) / numSamples;
x -= float3(samplePos.z, samplePos.z, samplePos.z) / numSamples;
// c = float4(sampleDepth, sampleDepth, sampleDepth, 1.0) / 100;
// c = float4(offset) * 2.0 - 0.6 ;
// range check & accumulate
float rangeCheck = smoothstep(0.0, 1.0, radius / abs(position.z - sampleDepth));
occlusion += (sampleDepth >= samplePos.z + bias ? 1.0 : 0.0) * rangeCheck;
occlusion += (sampleDepth <= samplePos.z - bias ? 1.0 : 0.0) * rangeCheck;
if (samplePos.z > 30)
{
occlusion *= max(0.0, 1.0 - ((samplePos.z - 30) / 300));
}
}
x.z = 0;
occlusion = 1.0 - (occlusion / numSamples);
// occlusion = 1.0 - occlusion;
// c += PositionTexture.Sample(Sampler0, uv).x * 0.00001;
// c += NormalTexture.Sample(Sampler1, uv).x * 0.00001;
// c += NoiseTexture.Sample(Sampler2, uv).x * 0.00001;
// float x = length(c);
// c = float4(x, x, x, 1.0);
c = occlusion;
//c = float4(x, 1.0);
return c + occlusion * 0.00001;
}

View File

@ -585,6 +585,7 @@ pub const Renderer = struct {
const ptr: *meshes_vert.Uniforms = @ptrCast(@alignCast(&data));
if (self.activeCamera) |camera| {
ptr.ViewProjection = @bitCast(camera.final);
ptr.NoTranslateView = @bitCast(camera.noTranslate);
}
ptr.ShadowMapProjection = @bitCast(self.shadowMapProjection);
// ptr.ViewProjection = @bitCast(self.shadowMapProjection);

View File

@ -28,8 +28,8 @@ struct type_StructuredBuffer_Scene
Scene _m0[1];
};
constant float4 _90 = {};
constant float3 _92 = {};
constant float4 _91 = {};
constant float3 _93 = {};
struct main0_out
{
@ -46,152 +46,153 @@ struct main0_in
float3 in_var_TEXCOORD2 [[user(locn2)]];
float4 in_var_TEXCOORD3 [[user(locn3)]];
uint in_var_TEXCOORD4 [[user(locn4)]];
float3 in_var_TEXCOORD5 [[user(locn5)]];
};
fragment main0_out main0(main0_in in [[stage_in]], constant type_Uniforms& Uniforms [[buffer(0)]], const device type_StructuredBuffer_Scene& scene [[buffer(1)]], texture2d<float> Texture0 [[texture(0)]], texture2d<float> Texture1 [[texture(1)]], texture2d<float> Texture2 [[texture(2)]], texture2d<float> DirectionalShadowDepthMap [[texture(3)]], sampler Sampler0 [[sampler(0)]], sampler Sampler1 [[sampler(1)]], sampler Sampler2 [[sampler(2)]], sampler DirectionalShadowSampler [[sampler(3)]])
{
main0_out out = {};
int _101 = int(scene._m0[in.in_var_TEXCOORD4].textureMode);
int _102 = _101 & 240;
float4 _136;
if (_102 == 16)
int _103 = int(scene._m0[in.in_var_TEXCOORD4].textureMode);
int _104 = _103 & 240;
float4 _138;
if (_104 == 16)
{
float3 _123 = float3(Texture0.sample(Sampler0, in.in_var_TEXCOORD0).x, Texture1.sample(Sampler1, in.in_var_TEXCOORD0).x, Texture2.sample(Sampler2, in.in_var_TEXCOORD0).x) + float3(-0.0625, -0.5, -0.5);
_136 = float4(dot(_123, float3(1.164000034332275390625, 0.0, 1.7929999828338623046875)), dot(_123, float3(1.164000034332275390625, -0.212999999523162841796875, -0.53299999237060546875)), dot(_123, float3(1.164000034332275390625, 2.111999988555908203125, 0.0)), 1.0);
float3 _125 = float3(Texture0.sample(Sampler0, in.in_var_TEXCOORD0).x, Texture1.sample(Sampler1, in.in_var_TEXCOORD0).x, Texture2.sample(Sampler2, in.in_var_TEXCOORD0).x) + float3(-0.0625, -0.5, -0.5);
_138 = float4(dot(_125, float3(1.164000034332275390625, 0.0, 1.7929999828338623046875)), dot(_125, float3(1.164000034332275390625, -0.212999999523162841796875, -0.53299999237060546875)), dot(_125, float3(1.164000034332275390625, 2.111999988555908203125, 0.0)), 1.0);
}
else
{
float4 _135;
if (_102 == 0)
float4 _137;
if (_104 == 0)
{
_135 = Texture0.sample(Sampler0, in.in_var_TEXCOORD0);
_137 = Texture0.sample(Sampler0, in.in_var_TEXCOORD0);
}
else
{
_135 = _90;
_137 = _91;
}
_136 = _135;
_138 = _137;
}
float4 _142;
if ((_101 & 1) == 1)
float4 _144;
if ((_103 & 1) == 1)
{
_142 = powr(_136, float4(2.2000000476837158203125));
_144 = powr(_138, float4(2.2000000476837158203125));
}
else
{
_142 = _136;
_144 = _138;
}
if (_142.w < 0.00999999977648258209228515625)
if (_144.w < 0.00999999977648258209228515625)
{
discard_fragment();
}
float3 _152 = float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * Uniforms.lightPower;
float3 _199;
float3 _154 = float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * Uniforms.lightPower;
float3 _201;
do
{
float3 _156 = Uniforms.lightPosition.xyz - in.in_var_TEXCOORD1;
float _157 = length(_156);
float _158 = _157 * _157;
float _164;
if (_157 > 2.0)
float3 _158 = Uniforms.lightPosition.xyz - in.in_var_TEXCOORD1;
float _159 = length(_158);
float _160 = _159 * _159;
float _166;
if (_159 > 2.0)
{
_164 = 0.5 / _158;
_166 = 0.5 / _160;
}
else
{
_164 = 1.0 / _158;
_166 = 1.0 / _160;
}
float _169;
if (_157 > 4.0)
float _171;
if (_159 > 4.0)
{
_169 = _164 * 0.5;
_171 = _166 * 0.5;
}
else
{
_169 = _164;
_171 = _166;
}
float _171 = (_157 > 15.0) ? 0.0 : _169;
float _173 = (_171 > 1.0) ? 1.0 : _171;
float _173 = (_159 > 15.0) ? 0.0 : _171;
float _175 = (_173 > 1.0) ? 1.0 : _173;
if (length(in.in_var_TEXCOORD2) < 0.100000001490116119384765625)
{
_199 = (in.in_var_TEXCOORD1 * _152) * _173;
_201 = (in.in_var_TEXCOORD1 * _154) * _175;
break;
}
float3 _180 = fast::normalize(_156);
_199 = ((_152 * precise::max(dot(_180, in.in_var_TEXCOORD2), 0.0)) * _173) + (((_152 * powr(precise::max(dot(in.in_var_TEXCOORD2, fast::normalize(_180 + fast::normalize(in.in_var_TEXCOORD1 - Uniforms.viewPos.xyz))), 0.0), 30.0)) * 2.0) * _173);
float3 _182 = fast::normalize(_158);
_201 = ((_154 * precise::max(dot(_182, in.in_var_TEXCOORD2), 0.0)) * _175) + (((_154 * powr(precise::max(dot(in.in_var_TEXCOORD2, fast::normalize(_182 + fast::normalize(in.in_var_TEXCOORD1 - Uniforms.viewPos.xyz))), 0.0), 30.0)) * 2.0) * _175);
break;
} while(false);
float3 _216 = ((in.in_var_TEXCOORD3.xyz / float3(in.in_var_TEXCOORD3.w)) * 0.5) + float3(0.5);
float3 _218;
_218.x = _216.x;
float2 _219 = _218.xy;
_219.y = -_216.y;
float _226;
int _229;
float3 _218 = ((in.in_var_TEXCOORD3.xyz / float3(in.in_var_TEXCOORD3.w)) * 0.5) + float3(0.5);
float3 _220;
_220.x = _218.x;
float2 _221 = _220.xy;
_221.y = -_218.y;
float _228;
int _231;
_226 = 0.0;
_229 = 0;
_231 = -4;
float _227;
int _230;
for (; _231 <= 4; _226 = _227, _229 = _230, _231++)
int _233;
_228 = 0.0;
_231 = 0;
_233 = -4;
float _229;
int _232;
for (; _233 <= 4; _228 = _229, _231 = _232, _233++)
{
_230 = _229;
_227 = _226;
for (int _240 = -4; _240 <= 4; )
_232 = _231;
_229 = _228;
for (int _242 = -4; _242 <= 4; )
{
_230++;
_227 += float((in.in_var_TEXCOORD3.z - 0.004999999888241291046142578125) > DirectionalShadowDepthMap.sample(DirectionalShadowSampler, (_219 + float2(float(_240) * 0.000244140625, float(_231) * 0.000244140625))).x);
_240++;
_232++;
_229 += float((in.in_var_TEXCOORD3.z - 0.004999999888241291046142578125) > DirectionalShadowDepthMap.sample(DirectionalShadowSampler, (_221 + float2(float(_242) * 0.000244140625, float(_233) * 0.000244140625))).x);
_242++;
continue;
}
}
float3 _269;
if ((_101 & 2) > 0)
float3 _271;
if ((_103 & 2) > 0)
{
_269 = _142.xyz * 2.0;
_271 = _144.xyz * 2.0;
}
else
{
_269 = _142.xyz * ((float3(0.100000001490116119384765625) + _199) + ((Uniforms.directionalLightColor.xyz * precise::max(dot(Uniforms.directionalLight.xyz, in.in_var_TEXCOORD2), 0.0)) * (1.0 - (_226 / float(_229)))));
_271 = _144.xyz * ((float3(0.100000001490116119384765625) + _201) + ((Uniforms.directionalLightColor.xyz * precise::max(dot(Uniforms.directionalLight.xyz, in.in_var_TEXCOORD2), 0.0)) * (1.0 - (_228 / float(_231)))));
}
float4 _286;
if (_269.x > 1.0)
float4 _288;
if (_271.x > 1.0)
{
float4 _285 = float4(0.0, 0.0, 0.0, 1.0);
_285.x = _269.x;
_286 = _285;
float4 _287 = float4(0.0, 0.0, 0.0, 1.0);
_287.x = _271.x;
_288 = _287;
}
else
{
_286 = float4(0.0, 0.0, 0.0, 1.0);
_288 = float4(0.0, 0.0, 0.0, 1.0);
}
float4 _291;
if (_269.y > 1.0)
float4 _293;
if (_271.y > 1.0)
{
float4 _290 = _286;
_290.y = _269.y;
_291 = _290;
float4 _292 = _288;
_292.y = _271.y;
_293 = _292;
}
else
{
_291 = _286;
_293 = _288;
}
float4 _296;
if (_269.z > 1.0)
float4 _298;
if (_271.z > 1.0)
{
float4 _295 = _291;
_295.z = _269.z;
_296 = _295;
float4 _297 = _293;
_297.z = _271.z;
_298 = _297;
}
else
{
_296 = _291;
_298 = _293;
}
out.out_var_SV_Target0 = float4(_269, _142.w);
out.out_var_SV_Target1 = _296;
out.out_var_SV_Target0 = float4(_271, _144.w);
out.out_var_SV_Target1 = _298;
out.out_var_SV_Target2 = float4(in.in_var_TEXCOORD1, 1.0);
out.out_var_SV_Target3 = float4(in.in_var_TEXCOORD2, 1.0);
out.out_var_SV_Target3 = float4(in.in_var_TEXCOORD5, 1.0);
return out;
}

View File

@ -20,6 +20,7 @@ struct type_StructuredBuffer_Scene
struct type_Uniforms
{
float4x4 ViewProjection;
float4x4 NoTranslateView;
float4x4 ShadowMapProjection;
float time;
};
@ -31,6 +32,7 @@ struct main0_out
float3 out_var_TEXCOORD2 [[user(locn2)]];
float4 out_var_TEXCOORD3 [[user(locn3)]];
uint out_var_TEXCOORD4 [[user(locn4)]];
float3 out_var_TEXCOORD5 [[user(locn5)]];
float4 gl_Position [[position]];
};
@ -44,14 +46,17 @@ struct main0_in
vertex main0_out main0(main0_in in [[stage_in]], constant type_Uniforms& Uniforms [[buffer(0)]], const device type_StructuredBuffer_Scene& scene [[buffer(1)]], uint gl_InstanceIndex [[instance_id]])
{
main0_out out = {};
float4 _51 = float4(in.in_var_TEXCOORD0, 1.0);
float4 _54 = scene._m0[gl_InstanceIndex].Model * _51;
float4 _53 = float4(in.in_var_TEXCOORD0, 1.0);
float4 _56 = scene._m0[gl_InstanceIndex].Model * _53;
float4x4 _91 = float4x4(float4(fast::normalize(float3(scene._m0[gl_InstanceIndex].Model[0][0], scene._m0[gl_InstanceIndex].Model[1][0], scene._m0[gl_InstanceIndex].Model[2][0])), 0.0), float4(fast::normalize(float3(scene._m0[gl_InstanceIndex].Model[0][1], scene._m0[gl_InstanceIndex].Model[1][1], scene._m0[gl_InstanceIndex].Model[2][1])), 0.0), float4(fast::normalize(float3(scene._m0[gl_InstanceIndex].Model[0][2], scene._m0[gl_InstanceIndex].Model[1][2], scene._m0[gl_InstanceIndex].Model[2][2])), 0.0), float4(0.0, 0.0, 0.0, 1.0));
float4 _95 = float4(in.in_var_TEXCOORD1, 1.0);
out.out_var_TEXCOORD0 = in.in_var_TEXCOORD3;
out.out_var_TEXCOORD1 = _54.xyz;
out.out_var_TEXCOORD2 = fast::normalize(float4(in.in_var_TEXCOORD1, 1.0) * float4x4(float4(fast::normalize(float3(scene._m0[gl_InstanceIndex].Model[0][0], scene._m0[gl_InstanceIndex].Model[1][0], scene._m0[gl_InstanceIndex].Model[2][0])), 0.0), float4(fast::normalize(float3(scene._m0[gl_InstanceIndex].Model[0][1], scene._m0[gl_InstanceIndex].Model[1][1], scene._m0[gl_InstanceIndex].Model[2][1])), 0.0), float4(fast::normalize(float3(scene._m0[gl_InstanceIndex].Model[0][2], scene._m0[gl_InstanceIndex].Model[1][2], scene._m0[gl_InstanceIndex].Model[2][2])), 0.0), float4(0.0, 0.0, 0.0, 1.0))).xyz;
out.out_var_TEXCOORD3 = Uniforms.ShadowMapProjection * _54;
out.out_var_TEXCOORD1 = _56.xyz;
out.out_var_TEXCOORD2 = fast::normalize(_95 * _91).xyz;
out.out_var_TEXCOORD3 = Uniforms.ShadowMapProjection * _56;
out.out_var_TEXCOORD4 = gl_InstanceIndex;
out.gl_Position = Uniforms.ViewProjection * (scene._m0[gl_InstanceIndex].Model * _51);
out.out_var_TEXCOORD5 = (_95 * (_91 * transpose(Uniforms.NoTranslateView))).xyz;
out.gl_Position = Uniforms.ViewProjection * (scene._m0[gl_InstanceIndex].Model * _53);
return out;
}

File diff suppressed because one or more lines are too long