From 00378d97608fe65b79bb3b86b14956cfbd630f8d Mon Sep 17 00:00:00 2001 From: Sascha Willems Date: Tue, 20 May 2025 18:04:54 +0200 Subject: [PATCH] Added slang shaders for debug utils sample --- shaders/slang/debugutils/colorpass.slang | 41 +++++++++++++ shaders/slang/debugutils/postprocess.slang | 55 +++++++++++++++++ shaders/slang/debugutils/toon.slang | 71 ++++++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 shaders/slang/debugutils/colorpass.slang create mode 100644 shaders/slang/debugutils/postprocess.slang create mode 100644 shaders/slang/debugutils/toon.slang diff --git a/shaders/slang/debugutils/colorpass.slang b/shaders/slang/debugutils/colorpass.slang new file mode 100644 index 00000000..7061a628 --- /dev/null +++ b/shaders/slang/debugutils/colorpass.slang @@ -0,0 +1,41 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSInput +{ + float4 Pos; + float3 Normal; + float2 UV; + float3 Color; +}; + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float3 Color; +}; + +struct UBO +{ + float4x4 projection; + float4x4 model; +}; +ConstantBuffer ubo; + +[shader("vertex")] +VSOutput vertexMain(VSInput input) +{ + VSOutput output; + output.Color = input.Color; + output.Pos = mul(ubo.projection, mul(ubo.model, input.Pos)); + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) +{ + return float4(input.Color, 1.0); +} \ No newline at end of file diff --git a/shaders/slang/debugutils/postprocess.slang b/shaders/slang/debugutils/postprocess.slang new file mode 100644 index 00000000..a1bce2f7 --- /dev/null +++ b/shaders/slang/debugutils/postprocess.slang @@ -0,0 +1,55 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float2 UV; +}; + +[[vk::binding(1, 0)]] Sampler2D samplerColor; + +[shader("vertex")] +VSOutput vertexMain(uint VertexIndex: SV_VertexID) +{ + VSOutput output; + output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2); + output.Pos = float4(output.UV * float2(2.0f, 2.0f) + float2(-1.0f, -1.0f), 0.0f, 1.0f); + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) +{ + // Single pass gauss blur + + const float2 texOffset = float2(0.01, 0.01); + + float2 tc0 = input.UV + float2(-texOffset.x, -texOffset.y); + float2 tc1 = input.UV + float2( 0.0, -texOffset.y); + float2 tc2 = input.UV + float2(+texOffset.x, -texOffset.y); + float2 tc3 = input.UV + float2(-texOffset.x, 0.0); + float2 tc4 = input.UV + float2( 0.0, 0.0); + float2 tc5 = input.UV + float2(+texOffset.x, 0.0); + float2 tc6 = input.UV + float2(-texOffset.x, +texOffset.y); + float2 tc7 = input.UV + float2( 0.0, +texOffset.y); + float2 tc8 = input.UV + float2(+texOffset.x, +texOffset.y); + + float4 col0 = samplerColor.Sample(tc0); + float4 col1 = samplerColor.Sample(tc1); + float4 col2 = samplerColor.Sample(tc2); + float4 col3 = samplerColor.Sample(tc3); + float4 col4 = samplerColor.Sample(tc4); + float4 col5 = samplerColor.Sample(tc5); + float4 col6 = samplerColor.Sample(tc6); + float4 col7 = samplerColor.Sample(tc7); + float4 col8 = samplerColor.Sample(tc8); + + float4 sum = (1.0 * col0 + 2.0 * col1 + 1.0 * col2 + + 2.0 * col3 + 4.0 * col4 + 2.0 * col5 + + 1.0 * col6 + 2.0 * col7 + 1.0 * col8) / 16.0; + return float4(sum.rgb, 1.0); +} \ No newline at end of file diff --git a/shaders/slang/debugutils/toon.slang b/shaders/slang/debugutils/toon.slang new file mode 100644 index 00000000..8d69bee0 --- /dev/null +++ b/shaders/slang/debugutils/toon.slang @@ -0,0 +1,71 @@ +/* Copyright (c) 2025, Sascha Willems + * + * SPDX-License-Identifier: MIT + * + */ + +struct VSInput +{ + float3 Pos; + float3 Normal; + float2 UV; + float3 Color; +}; + +struct VSOutput +{ + float4 Pos : SV_POSITION; + float3 Normal; + float3 Color; + float2 UV; + float3 ViewVec; + float3 LightVec; +}; + +struct UBO +{ + float4x4 projection; + float4x4 model; + float4 lightPos; +}; +ConstantBuffer ubo; + +[shader("vertex")] +VSOutput vertexMain(VSInput input) +{ + VSOutput output; + output.Color = input.Color; + output.UV = input.UV; + output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0))); + float4 pos = mul(ubo.model, float4(input.Pos, 1.0)); + output.Normal = mul((float4x3)ubo.model, input.Normal).xyz; + float3 lPos = mul((float4x3)ubo.model, ubo.lightPos.xyz).xyz; + output.LightVec = lPos - pos.xyz; + output.ViewVec = -pos.xyz; + return output; +} + +[shader("fragment")] +float4 fragmentMain(VSOutput input) +{ + // Desaturate color + float3 color = float3(lerp(input.Color, dot(float3(0.2126,0.7152,0.0722), input.Color).xxx, 0.65)); + + // High ambient colors because mesh materials are pretty dark + float3 ambient = color * float3(1.0, 1.0, 1.0); + float3 N = normalize(input.Normal); + float3 L = normalize(input.LightVec); + float3 V = normalize(input.ViewVec); + float3 R = reflect(-L, N); + float3 diffuse = max(dot(N, L), 0.0) * color; + float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.75, 0.75, 0.75); + + float intensity = dot(N,L); + float shade = 1.0; + shade = intensity < 0.5 ? 0.75 : shade; + shade = intensity < 0.35 ? 0.6 : shade; + shade = intensity < 0.25 ? 0.5 : shade; + shade = intensity < 0.1 ? 0.25 : shade; + + return float4(input.Color * 3.0 * shade, 1); +} \ No newline at end of file