Skip to content

Commit 00378d9

Browse files
committed
Added slang shaders for debug utils sample
1 parent 24bc3e3 commit 00378d9

3 files changed

Lines changed: 167 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSInput
8+
{
9+
float4 Pos;
10+
float3 Normal;
11+
float2 UV;
12+
float3 Color;
13+
};
14+
15+
struct VSOutput
16+
{
17+
float4 Pos : SV_POSITION;
18+
float3 Color;
19+
};
20+
21+
struct UBO
22+
{
23+
float4x4 projection;
24+
float4x4 model;
25+
};
26+
ConstantBuffer<UBO> ubo;
27+
28+
[shader("vertex")]
29+
VSOutput vertexMain(VSInput input)
30+
{
31+
VSOutput output;
32+
output.Color = input.Color;
33+
output.Pos = mul(ubo.projection, mul(ubo.model, input.Pos));
34+
return output;
35+
}
36+
37+
[shader("fragment")]
38+
float4 fragmentMain(VSOutput input)
39+
{
40+
return float4(input.Color, 1.0);
41+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSOutput
8+
{
9+
float4 Pos : SV_POSITION;
10+
float2 UV;
11+
};
12+
13+
[[vk::binding(1, 0)]] Sampler2D samplerColor;
14+
15+
[shader("vertex")]
16+
VSOutput vertexMain(uint VertexIndex: SV_VertexID)
17+
{
18+
VSOutput output;
19+
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
20+
output.Pos = float4(output.UV * float2(2.0f, 2.0f) + float2(-1.0f, -1.0f), 0.0f, 1.0f);
21+
return output;
22+
}
23+
24+
[shader("fragment")]
25+
float4 fragmentMain(VSOutput input)
26+
{
27+
// Single pass gauss blur
28+
29+
const float2 texOffset = float2(0.01, 0.01);
30+
31+
float2 tc0 = input.UV + float2(-texOffset.x, -texOffset.y);
32+
float2 tc1 = input.UV + float2( 0.0, -texOffset.y);
33+
float2 tc2 = input.UV + float2(+texOffset.x, -texOffset.y);
34+
float2 tc3 = input.UV + float2(-texOffset.x, 0.0);
35+
float2 tc4 = input.UV + float2( 0.0, 0.0);
36+
float2 tc5 = input.UV + float2(+texOffset.x, 0.0);
37+
float2 tc6 = input.UV + float2(-texOffset.x, +texOffset.y);
38+
float2 tc7 = input.UV + float2( 0.0, +texOffset.y);
39+
float2 tc8 = input.UV + float2(+texOffset.x, +texOffset.y);
40+
41+
float4 col0 = samplerColor.Sample(tc0);
42+
float4 col1 = samplerColor.Sample(tc1);
43+
float4 col2 = samplerColor.Sample(tc2);
44+
float4 col3 = samplerColor.Sample(tc3);
45+
float4 col4 = samplerColor.Sample(tc4);
46+
float4 col5 = samplerColor.Sample(tc5);
47+
float4 col6 = samplerColor.Sample(tc6);
48+
float4 col7 = samplerColor.Sample(tc7);
49+
float4 col8 = samplerColor.Sample(tc8);
50+
51+
float4 sum = (1.0 * col0 + 2.0 * col1 + 1.0 * col2 +
52+
2.0 * col3 + 4.0 * col4 + 2.0 * col5 +
53+
1.0 * col6 + 2.0 * col7 + 1.0 * col8) / 16.0;
54+
return float4(sum.rgb, 1.0);
55+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSInput
8+
{
9+
float3 Pos;
10+
float3 Normal;
11+
float2 UV;
12+
float3 Color;
13+
};
14+
15+
struct VSOutput
16+
{
17+
float4 Pos : SV_POSITION;
18+
float3 Normal;
19+
float3 Color;
20+
float2 UV;
21+
float3 ViewVec;
22+
float3 LightVec;
23+
};
24+
25+
struct UBO
26+
{
27+
float4x4 projection;
28+
float4x4 model;
29+
float4 lightPos;
30+
};
31+
ConstantBuffer<UBO> ubo;
32+
33+
[shader("vertex")]
34+
VSOutput vertexMain(VSInput input)
35+
{
36+
VSOutput output;
37+
output.Color = input.Color;
38+
output.UV = input.UV;
39+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
40+
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
41+
output.Normal = mul((float4x3)ubo.model, input.Normal).xyz;
42+
float3 lPos = mul((float4x3)ubo.model, ubo.lightPos.xyz).xyz;
43+
output.LightVec = lPos - pos.xyz;
44+
output.ViewVec = -pos.xyz;
45+
return output;
46+
}
47+
48+
[shader("fragment")]
49+
float4 fragmentMain(VSOutput input)
50+
{
51+
// Desaturate color
52+
float3 color = float3(lerp(input.Color, dot(float3(0.2126,0.7152,0.0722), input.Color).xxx, 0.65));
53+
54+
// High ambient colors because mesh materials are pretty dark
55+
float3 ambient = color * float3(1.0, 1.0, 1.0);
56+
float3 N = normalize(input.Normal);
57+
float3 L = normalize(input.LightVec);
58+
float3 V = normalize(input.ViewVec);
59+
float3 R = reflect(-L, N);
60+
float3 diffuse = max(dot(N, L), 0.0) * color;
61+
float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.75, 0.75, 0.75);
62+
63+
float intensity = dot(N,L);
64+
float shade = 1.0;
65+
shade = intensity < 0.5 ? 0.75 : shade;
66+
shade = intensity < 0.35 ? 0.6 : shade;
67+
shade = intensity < 0.25 ? 0.5 : shade;
68+
shade = intensity < 0.1 ? 0.25 : shade;
69+
70+
return float4(input.Color * 3.0 * shade, 1);
71+
}

0 commit comments

Comments
 (0)