Added slang shaders for ssao sample
This commit is contained in:
parent
8d50885e92
commit
80ff4a41d2
6 changed files with 261 additions and 0 deletions
30
shaders/slang/ssao/blur.slang
Normal file
30
shaders/slang/ssao/blur.slang
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* Copyright (c) 2025, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
import types;
|
||||
|
||||
Sampler2D samplerSSAO;
|
||||
|
||||
[shader("fragment")]
|
||||
float4 fragmentMain(VSOutput input)
|
||||
{
|
||||
const int blurRange = 2;
|
||||
int n = 0;
|
||||
int2 texDim;
|
||||
samplerSSAO.GetDimensions(texDim.x, texDim.y);
|
||||
float2 texelSize = 1.0 / (float2)texDim;
|
||||
float result = 0.0;
|
||||
for (int x = -blurRange; x <= blurRange; x++)
|
||||
{
|
||||
for (int y = -blurRange; y <= blurRange; y++)
|
||||
{
|
||||
float2 offset = float2(float(x), float(y)) * texelSize;
|
||||
result += samplerSSAO.Sample(input.UV + offset).r;
|
||||
n++;
|
||||
}
|
||||
}
|
||||
return result / (float(n));
|
||||
}
|
||||
59
shaders/slang/ssao/composition.slang
Normal file
59
shaders/slang/ssao/composition.slang
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/* Copyright (c) 2025, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
import types;
|
||||
|
||||
Sampler2D samplerposition;
|
||||
Sampler2D samplerNormal;
|
||||
Sampler2D samplerAlbedo;
|
||||
Sampler2D samplerSSAO;
|
||||
Sampler2D samplerSSAOBlur;
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 _dummy;
|
||||
int ssao;
|
||||
int ssaoOnly;
|
||||
int ssaoBlur;
|
||||
};
|
||||
ConstantBuffer<UBO> uboParams;
|
||||
|
||||
[shader("fragment")]
|
||||
float4 fragmentMain(VSOutput input)
|
||||
{
|
||||
float3 fragPos = samplerposition.Sample(input.UV).rgb;
|
||||
float3 normal = normalize(samplerNormal.Sample(input.UV).rgb * 2.0 - 1.0);
|
||||
float4 albedo = samplerAlbedo.Sample(input.UV);
|
||||
|
||||
float ssao = (uboParams.ssaoBlur == 1) ? samplerSSAOBlur.Sample(input.UV).r : samplerSSAO.Sample(input.UV).r;
|
||||
|
||||
float3 lightPos = float3(0.0, 0.0, 0.0);
|
||||
float3 L = normalize(lightPos - fragPos);
|
||||
float NdotL = max(0.5, dot(normal, L));
|
||||
|
||||
float4 outFragColor;
|
||||
if (uboParams.ssaoOnly == 1)
|
||||
{
|
||||
outFragColor.rgb = ssao.rrr;
|
||||
}
|
||||
else
|
||||
{
|
||||
float3 baseColor = albedo.rgb * NdotL;
|
||||
|
||||
if (uboParams.ssao == 1)
|
||||
{
|
||||
outFragColor.rgb = ssao.rrr;
|
||||
|
||||
if (uboParams.ssaoOnly != 1)
|
||||
outFragColor.rgb *= baseColor;
|
||||
}
|
||||
else
|
||||
{
|
||||
outFragColor.rgb = baseColor;
|
||||
}
|
||||
}
|
||||
return outFragColor;
|
||||
}
|
||||
16
shaders/slang/ssao/fullscreen.slang
Normal file
16
shaders/slang/ssao/fullscreen.slang
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/* Copyright (c) 2025, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
import types;
|
||||
|
||||
[shader("vertex")]
|
||||
VSOutput vertexMain(uint VertexIndex : SV_VertexID)
|
||||
{
|
||||
VSOutput output;
|
||||
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
|
||||
output.Pos = float4(output.UV * 2.0f - 1.0f, 0.0f, 1.0f);
|
||||
return output;
|
||||
}
|
||||
73
shaders/slang/ssao/gbuffer.slang
Normal file
73
shaders/slang/ssao/gbuffer.slang
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/* Copyright (c) 2025, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
struct VSInput
|
||||
{
|
||||
float4 Pos;
|
||||
float2 UV;
|
||||
float3 Color;
|
||||
float3 Normal;
|
||||
};
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
float3 Normal;
|
||||
float2 UV;
|
||||
float3 Color;
|
||||
float3 WorldPos;
|
||||
};
|
||||
|
||||
struct FSOutput
|
||||
{
|
||||
float4 Position : SV_TARGET0;
|
||||
float4 Normal : SV_TARGET1;
|
||||
float4 Albedo : SV_TARGET2;
|
||||
};
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
float4x4 model;
|
||||
float4x4 view;
|
||||
float nearPlane;
|
||||
float farPlane;
|
||||
};
|
||||
ConstantBuffer<UBO> ubo;
|
||||
|
||||
[[vk::binding(0, 1)]] Texture2D textureColorMap;
|
||||
[[vk::binding(0, 1)]] SamplerState samplerColorMap;
|
||||
|
||||
[shader("vertex")]
|
||||
VSOutput vertexMain(VSInput input)
|
||||
{
|
||||
VSOutput output;
|
||||
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, input.Pos)));
|
||||
output.UV = input.UV;
|
||||
// Vertex position in view space
|
||||
output.WorldPos = mul(ubo.view, mul(ubo.model, input.Pos)).xyz;
|
||||
// Normal in view space
|
||||
float3x3 normalMatrix = (float3x3)mul(ubo.view, ubo.model);
|
||||
output.Normal = mul(normalMatrix, input.Normal);
|
||||
output.Color = input.Color;
|
||||
return output;
|
||||
}
|
||||
|
||||
float linearDepth(float depth)
|
||||
{
|
||||
float z = depth * 2.0f - 1.0f;
|
||||
return (2.0f * ubo.nearPlane * ubo.farPlane) / (ubo.farPlane + ubo.nearPlane - z * (ubo.farPlane - ubo.nearPlane));
|
||||
}
|
||||
|
||||
[shader("fragment")]
|
||||
FSOutput fragmentMain(VSOutput input)
|
||||
{
|
||||
FSOutput output;
|
||||
output.Position = float4(input.WorldPos, linearDepth(input.Pos.z));
|
||||
output.Normal = float4(normalize(input.Normal) * 0.5 + 0.5, 1.0);
|
||||
output.Albedo = textureColorMap.Sample(samplerColorMap, input.UV) * float4(input.Color, 1.0);
|
||||
return output;
|
||||
}
|
||||
70
shaders/slang/ssao/ssao.slang
Normal file
70
shaders/slang/ssao/ssao.slang
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
/* Copyright (c) 2025, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
import types;
|
||||
|
||||
Sampler2D samplerPositionDepth;
|
||||
Sampler2D samplerNormal;
|
||||
Sampler2D ssaoNoiseSampler;
|
||||
|
||||
struct UBOSSAOKernel
|
||||
{
|
||||
float4 samples[64];
|
||||
};
|
||||
ConstantBuffer<UBOSSAOKernel> uboSSAOKernel;
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
};
|
||||
ConstantBuffer<UBO> ubo;
|
||||
|
||||
[[SpecializationConstant]] const int SSAO_KERNEL_SIZE = 64;
|
||||
[[SpecializationConstant]] const float SSAO_RADIUS = 0.5;
|
||||
|
||||
[shader("fragment")]
|
||||
float fragmentMain(VSOutput input)
|
||||
{
|
||||
// Get G-Buffer values
|
||||
float3 fragPos = samplerPositionDepth.Sample(input.UV).rgb;
|
||||
float3 normal = normalize(samplerNormal.Sample(input.UV).rgb * 2.0 - 1.0);
|
||||
|
||||
// Get a random vector using a noise lookup
|
||||
int2 texDim;
|
||||
samplerPositionDepth.GetDimensions(texDim.x, texDim.y);
|
||||
int2 noiseDim;
|
||||
ssaoNoiseSampler.GetDimensions(noiseDim.x, noiseDim.y);
|
||||
const float2 noiseUV = float2(float(texDim.x) / float(noiseDim.x), float(texDim.y) / (noiseDim.y)) * input.UV;
|
||||
float3 randomVec = ssaoNoiseSampler.Sample(noiseUV).xyz * 2.0 - 1.0;
|
||||
|
||||
// Create TBN matrix
|
||||
float3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
|
||||
float3 bitangent = cross(tangent, normal);
|
||||
float3x3 TBN = transpose(float3x3(tangent, bitangent, normal));
|
||||
|
||||
// Calculate occlusion value
|
||||
float occlusion = 0.0f;
|
||||
for(int i = 0; i < SSAO_KERNEL_SIZE; i++)
|
||||
{
|
||||
float3 samplePos = mul(TBN, uboSSAOKernel.samples[i].xyz);
|
||||
samplePos = fragPos + samplePos * SSAO_RADIUS;
|
||||
|
||||
// project
|
||||
float4 offset = float4(samplePos, 1.0f);
|
||||
offset = mul(ubo.projection, offset);
|
||||
offset.xyz /= offset.w;
|
||||
offset.xyz = offset.xyz * 0.5f + 0.5f;
|
||||
|
||||
float sampleDepth = -samplerPositionDepth.Sample(offset.xy).w;
|
||||
|
||||
float rangeCheck = smoothstep(0.0f, 1.0f, SSAO_RADIUS / abs(fragPos.z - sampleDepth));
|
||||
occlusion += (sampleDepth >= samplePos.z ? 1.0f : 0.0f) * rangeCheck;
|
||||
}
|
||||
occlusion = 1.0 - (occlusion / float(SSAO_KERNEL_SIZE));
|
||||
|
||||
return occlusion;
|
||||
}
|
||||
|
||||
13
shaders/slang/ssao/types.slang
Normal file
13
shaders/slang/ssao/types.slang
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/* Copyright (c) 2025, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
module types;
|
||||
|
||||
public struct VSOutput
|
||||
{
|
||||
public float4 Pos : SV_POSITION;
|
||||
public float2 UV;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue