From b959d4abc02d17e72d0615e3e83d9aeddaab0a8a Mon Sep 17 00:00:00 2001 From: antopilo Date: Sun, 13 Apr 2025 12:39:03 -0400 Subject: [PATCH] Added blur + clamping to ssao --- Data/Shaders/Vulkan/blur.frag | 127 ++++++++++++++++++++++++++++++++++ Data/Shaders/Vulkan/blur.vert | 110 +++++++++++++++++++++++++++++ Data/Shaders/Vulkan/ssao.frag | 3 + 3 files changed, 240 insertions(+) create mode 100644 Data/Shaders/Vulkan/blur.frag create mode 100644 Data/Shaders/Vulkan/blur.vert diff --git a/Data/Shaders/Vulkan/blur.frag b/Data/Shaders/Vulkan/blur.frag new file mode 100644 index 00000000..168f4fdd --- /dev/null +++ b/Data/Shaders/Vulkan/blur.frag @@ -0,0 +1,127 @@ +// Transforms +struct ModelData +{ + float4x4 model; +}; +[[vk::binding(0, 0)]] +StructuredBuffer model : register(t1); + +// Vertex +struct Vertex +{ + float3 position; + float uv_x; + float3 normal; + float uv_y; + float3 tangent; + float3 bitangent; +}; + +[[vk::binding(0, 1)]] +StructuredBuffer vertexBuffer : register(t2); + +// Samplers +[[vk::binding(0, 2)]] +SamplerState mySampler : register(s0); + +// Materials +struct Material +{ + bool hasAlbedo; + float3 albedo; + bool hasNormal; + bool hasMetalness; + bool hasRoughness; + bool hasAO; + float metalnessValue; + float roughnessValue; + float aoValue; + int albedoTextureId; + int normalTextureId; + int metalnessTextureId; + int roughnessTextureId; + int aoTextureId; +}; +[[vk::binding(0, 3)]] +StructuredBuffer material; + +// Textures +[[vk::binding(0, 4)]] +Texture2D textures[]; + +// Lights +struct Light +{ + float3 position; + int type; + float4 color; + float3 direction; + float outerConeAngle; + float innerConeAngle; + bool castShadow; + int shadowMapTextureId[4]; + int transformId[4]; +}; + +[[vk::binding(0, 5)]] +StructuredBuffer lights; + +// Cameras +struct CameraView { + float4x4 View; + float4x4 Projection; + float4x4 ViewProjection; + float4x4 InverseView; + float4x4 InverseProjection; + float3 Position; + float Near; + float Far; +}; +[[vk::binding(0, 6)]] +StructuredBuffer cameras; + +[[vk::binding(0, 7)]] +StructuredBuffer ssaoKernels; + +struct PSInput { + float4 Position : SV_Position; + float2 UV : TEXCOORD0; +}; + +struct PSOutput { + float4 oColor0 : SV_TARGET; +}; + +struct BlurConstant +{ + int blurSourceID; + float2 sourceSize; +}; + +[[vk::push_constant]] +BlurConstant pushConstants; + +float3 SampleTexture(int textureId, float2 uv) +{ + return textures[textureId].Sample(mySampler, uv).rgb; +} + +PSOutput main(PSInput input) +{ + float2 texelSize = 1.0 / pushConstants.sourceSize; + float3 result = 0.0; + for (int x = -2; x < 2; x++) + { + for (int y = -2; y < 2; y++) + { + float2 offset = float2(x, y) * texelSize; + result += SampleTexture(pushConstants.blurSourceID, input.UV + offset); + } + } + + result = result / (4.0 * 4.0); + + PSOutput output; + output.oColor0 = float4(result.rgb, 1.0f); + return output; +} \ No newline at end of file diff --git a/Data/Shaders/Vulkan/blur.vert b/Data/Shaders/Vulkan/blur.vert new file mode 100644 index 00000000..8ccbbe49 --- /dev/null +++ b/Data/Shaders/Vulkan/blur.vert @@ -0,0 +1,110 @@ +// Transforms +struct ModelData +{ + float4x4 model; +}; +[[vk::binding(0, 0)]] +StructuredBuffer model : register(t1); + +// Vertex +struct Vertex +{ + float3 position; + float uv_x; + float3 normal; + float uv_y; + float3 tangent; + float3 bitangent; +}; + +[[vk::binding(0, 1)]] +StructuredBuffer vertexBuffer : register(t2); + +// Samplers +[[vk::binding(0, 2)]] +SamplerState mySampler : register(s0); + +// Materials +struct Material +{ + bool hasAlbedo; + float3 albedo; + bool hasNormal; + bool hasMetalness; + bool hasRoughness; + bool hasAO; + float metalnessValue; + float roughnessValue; + float aoValue; + int albedoTextureId; + int normalTextureId; + int metalnessTextureId; + int roughnessTextureId; + int aoTextureId; +}; +[[vk::binding(0, 3)]] +StructuredBuffer material; + +// Textures +[[vk::binding(0, 4)]] +Texture2D textures[]; + +// Lights +struct Light +{ + float3 position; + int type; + float4 color; + float3 direction; + float outerConeAngle; + float innerConeAngle; + bool castShadow; + int shadowMapTextureId[4]; + int transformId[4]; +}; + +[[vk::binding(0, 5)]] +StructuredBuffer lights; + +// Cameras +struct CameraView { + float4x4 View; + float4x4 Projection; + float4x4 ViewProjection; + float4x4 InverseView; + float4x4 InverseProjection; + float3 Position; + float Near; + float Far; +}; +[[vk::binding(0, 6)]] +StructuredBuffer cameras; + +[[vk::binding(0, 7)]] +StructuredBuffer ssaoKernels; + +struct BlurConstant +{ + int blurSourceID; +}; + +[[vk::push_constant]] +BlurConstant pushConstants; + +// Outputs +struct VSOutput { + float4 Position : SV_Position; + float2 UV : TEXCOORD0; +}; + +// Main vertex shader +VSOutput main(uint vertexIndex : SV_VertexID) +{ + VSOutput output; + + Vertex v = vertexBuffer[vertexIndex]; + output.UV = float2(v.uv_x, v.uv_y); + output.Position = float4(v.position, 1.0f); + + return output; +} \ No newline at end of file diff --git a/Data/Shaders/Vulkan/ssao.frag b/Data/Shaders/Vulkan/ssao.frag index 52d47140..2b7aa1b8 100644 --- a/Data/Shaders/Vulkan/ssao.frag +++ b/Data/Shaders/Vulkan/ssao.frag @@ -206,6 +206,9 @@ PSOutput main(PSInput input) offset.xyz /= offset.w; offset.xyz = offset.xyz * 0.5 + 0.5; + offset.x = clamp(offset.x, 0.00001, 0.999); + offset.y = clamp(offset.y, 0.00001, 0.999); + float sampleDepth = ViewPosFromDepth(SampleDepth(offset.xy), offset.xy, camera.InverseProjection).z; float rangeCheck = smoothstep(0.0, 1.0, pushConstants.radius / abs(sampleDepth - fragPos.z)); occlusion += (sampleDepth - pushConstants.bias >= samplePos.z ? 1.0 : 0.0) * rangeCheck;