Skip to content

Commit 4df49db

Browse files
committed
Add slang shaders for additional samples
1 parent afbc3a5 commit 4df49db

5 files changed

Lines changed: 266 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSInput
8+
{
9+
float4 Pos;
10+
float2 UV;
11+
float3 Color;
12+
};
13+
14+
struct VSOutput
15+
{
16+
float4 Pos : SV_POSITION;
17+
float3 Color;
18+
float2 UV;
19+
};
20+
21+
struct UBO
22+
{
23+
float4x4 projection;
24+
float4x4 view;
25+
float4x4 model;
26+
};
27+
ConstantBuffer<UBO> ubo;
28+
29+
Sampler2D colorMapSampler;
30+
31+
[shader("vertex")]
32+
VSOutput vertexMain(VSInput input)
33+
{
34+
VSOutput output;
35+
output.UV = input.UV;
36+
output.Color = input.Color;
37+
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, input.Pos)));
38+
return output;
39+
}
40+
41+
[shader("fragment")]
42+
float4 fragmentMain(VSOutput input)
43+
{
44+
return float4(input.Color, 1);
45+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
struct UBO
14+
{
15+
float blurScale;
16+
float blurStrength;
17+
};
18+
ConstantBuffer<UBO> ubo;
19+
20+
Sampler2D samplerColor;
21+
22+
[[SpecializationConstant]] const int blurdirection = 0;
23+
24+
[shader("vertex")]
25+
VSOutput vertexMain(uint VertexIndex: SV_VertexID)
26+
{
27+
VSOutput output;
28+
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
29+
output.Pos = float4(output.UV * 2.0f - 1.0f, 0.0f, 1.0f);
30+
return output;
31+
}
32+
33+
[shader("fragment")]
34+
float4 fragmentMain(VSOutput input)
35+
{
36+
float weight[5];
37+
weight[0] = 0.227027;
38+
weight[1] = 0.1945946;
39+
weight[2] = 0.1216216;
40+
weight[3] = 0.054054;
41+
weight[4] = 0.016216;
42+
43+
float2 textureSize;
44+
samplerColor.GetDimensions(textureSize.x, textureSize.y);
45+
float2 tex_offset = 1.0 / textureSize * ubo.blurScale; // gets size of single texel
46+
float3 result = samplerColor.Sample(input.UV).rgb * weight[0]; // current fragment's contribution
47+
for(int i = 1; i < 5; ++i)
48+
{
49+
if (blurdirection == 1)
50+
{
51+
// H
52+
result += samplerColor.Sample(input.UV + float2(tex_offset.x * i, 0.0)).rgb * weight[i] * ubo.blurScale;
53+
result += samplerColor.Sample(input.UV - float2(tex_offset.x * i, 0.0)).rgb * weight[i] * ubo.blurScale;
54+
}
55+
else
56+
{
57+
// V
58+
result += samplerColor.Sample(input.UV + float2(0.0, tex_offset.y * i)).rgb * weight[i] * ubo.blurScale;
59+
result += samplerColor.Sample(input.UV - float2(0.0, tex_offset.y * i)).rgb * weight[i] * ubo.blurScale;
60+
}
61+
}
62+
return float4(result, 1.0);
63+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSInput
8+
{
9+
float4 Pos;
10+
float2 UV;
11+
float3 Color;
12+
float3 Normal;
13+
};
14+
15+
struct VSOutput
16+
{
17+
float4 Pos : SV_POSITION;
18+
float3 Normal;
19+
float2 UV;
20+
float3 Color;
21+
float3 ViewVec;
22+
float3 LightVec;
23+
};
24+
25+
struct UBO
26+
{
27+
float4x4 projection;
28+
float4x4 view;
29+
float4x4 model;
30+
};
31+
ConstantBuffer<UBO> ubo;
32+
33+
Sampler2D colorMapSampler;
34+
35+
[shader("vertex")]
36+
VSOutput vertexMain(VSInput input)
37+
{
38+
VSOutput output;
39+
output.Normal = input.Normal;
40+
output.Color = input.Color;
41+
output.UV = input.UV;
42+
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, input.Pos)));
43+
44+
float3 lightPos = float3(-5.0, -5.0, 0.0);
45+
float4 pos = mul(ubo.view, mul(ubo.model, input.Pos));
46+
output.Normal = mul((float4x3)mul(ubo.view, ubo.model), input.Normal).xyz;
47+
output.LightVec = lightPos - pos.xyz;
48+
output.ViewVec = -pos.xyz;
49+
return output;
50+
}
51+
52+
[shader("fragment")]
53+
float4 fragmentMain(VSOutput input)
54+
{
55+
float3 ambient = float3(0.0f, 0.0f, 0.0f);
56+
57+
// Adjust light calculations for glow color
58+
if ((input.Color.r >= 0.9) || (input.Color.g >= 0.9) || (input.Color.b >= 0.9))
59+
{
60+
ambient = input.Color * 0.25;
61+
}
62+
63+
float3 N = normalize(input.Normal);
64+
float3 L = normalize(input.LightVec);
65+
float3 V = normalize(input.ViewVec);
66+
float3 R = reflect(-L, N);
67+
float3 diffuse = max(dot(N, L), 0.0) * input.Color;
68+
float3 specular = pow(max(dot(R, V), 0.0), 8.0) * float3(0.75f, 0.75f, 0.75f);
69+
return float4(ambient + diffuse + specular, 1.0);
70+
}

shaders/slang/bloom/skybox.slang

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+
float3 Pos;
10+
}
11+
12+
struct VSOutput
13+
{
14+
float4 Pos : SV_POSITION;
15+
float3 UVW;
16+
};
17+
18+
struct UBO
19+
{
20+
float4x4 projection;
21+
float4x4 view;
22+
float4x4 model;
23+
};
24+
ConstantBuffer<UBO> ubo;
25+
26+
SamplerCube samplerCubeMap;
27+
28+
[shader("vertex")]
29+
VSOutput vertexMain(VSInput input)
30+
{
31+
VSOutput output;
32+
output.UVW = input.Pos;
33+
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, float4(input.Pos.xyz, 1.0))));
34+
return output;
35+
}
36+
37+
[shader("fragment")]
38+
float4 fragmentMain(VSOutput input)
39+
{
40+
return samplerCubeMap.Sample(input.UVW);
41+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSInput
8+
{
9+
float3 Pos;
10+
float3 Color;
11+
};
12+
13+
struct VSOutput
14+
{
15+
float4 Pos : SV_POSITION;
16+
float3 Color;
17+
};
18+
19+
struct UboView
20+
{
21+
float4x4 projection;
22+
float4x4 view;
23+
};
24+
ConstantBuffer<UboView> uboView;
25+
26+
struct UboInstance
27+
{
28+
float4x4 model;
29+
};
30+
ConstantBuffer<UboInstance> uboInstance;
31+
32+
[shader("vertex")]
33+
VSOutput vertexMain(VSInput input)
34+
{
35+
VSOutput output;
36+
output.Color = input.Color;
37+
float4x4 modelView = mul(uboView.view, uboInstance.model);
38+
float3 worldPos = mul(modelView, float4(input.Pos, 1.0)).xyz;
39+
output.Pos = mul(uboView.projection, mul(modelView, float4(input.Pos.xyz, 1.0)));
40+
return output;
41+
}
42+
43+
[shader("fragment")]
44+
float4 fragmentMain(VSOutput input)
45+
{
46+
return float4(input.Color, 1.0);
47+
}

0 commit comments

Comments
 (0)