Skip to content

Commit 7c115af

Browse files
committed
Add slang shaders for additional samples
1 parent b3c032e commit 7c115af

8 files changed

Lines changed: 529 additions & 0 deletions

File tree

shaders/slang/imgui/scene.slang

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
float3 Color;
12+
};
13+
14+
struct VSOutput
15+
{
16+
float4 Pos : SV_POSITION;
17+
float3 Normal;
18+
float3 Color;
19+
float3 ViewVec;
20+
float3 LightVec;
21+
};
22+
23+
struct UBO
24+
{
25+
float4x4 projection;
26+
float4x4 model;
27+
float4 lightPos;
28+
};
29+
ConstantBuffer<UBO> ubo;
30+
31+
[shader("vertex")]
32+
VSOutput vertexMain(VSInput input)
33+
{
34+
VSOutput output;
35+
output.Normal = input.Normal;
36+
output.Color = input.Color;
37+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
38+
39+
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
40+
output.Normal = mul((float4x3)ubo.model, input.Normal).xyz;
41+
float3 lPos = mul((float4x3)ubo.model, ubo.lightPos.xyz).xyz;
42+
output.LightVec = lPos - pos.xyz;
43+
output.ViewVec = -pos.xyz;
44+
return output;
45+
}
46+
47+
[shader("fragment")]
48+
float4 fragmentMain(VSOutput input)
49+
{
50+
float3 N = normalize(input.Normal);
51+
float3 L = normalize(input.LightVec);
52+
float3 V = normalize(input.ViewVec);
53+
float3 R = reflect(-L, N);
54+
float diffuse = max(dot(N, L), 0.0);
55+
float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.75, 0.75, 0.75);
56+
return float4(diffuse * input.Color + specular, 1.0);
57+
}

shaders/slang/imgui/ui.slang

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSInput
8+
{
9+
float2 Pos;
10+
float2 UV;
11+
float4 Color;
12+
};
13+
14+
struct VSOutput
15+
{
16+
float4 Pos : SV_POSITION;
17+
float2 UV;
18+
float4 Color;
19+
};
20+
21+
Sampler2D fontSampler;
22+
23+
[shader("vertex")]
24+
VSOutput vertexMain(VSInput input, uniform float2 scale, uniform float2 translate)
25+
{
26+
VSOutput output;
27+
output.UV = input.UV;
28+
output.Color = input.Color;
29+
output.Pos = float4(input.Pos * scale + translate, 0.0, 1.0);
30+
return output;
31+
}
32+
33+
[shader("fragment")]
34+
float4 fragmentMain(VSOutput input)
35+
{
36+
return input.Color * fontSampler.Sample(input.UV);
37+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
float2 UV;
19+
};
20+
21+
struct UBO
22+
{
23+
float4x4 projection;
24+
float4x4 modelview;
25+
};
26+
ConstantBuffer<UBO> ubo;
27+
28+
[[vk::binding(2,0)]] Sampler2D samplerColor;
29+
30+
[shader("vertex")]
31+
VSOutput vertexMain(VSInput input)
32+
{
33+
VSOutput output;
34+
output.UV = input.UV * 32.0;
35+
output.Pos = mul(ubo.projection, mul(ubo.modelview, float4(input.Pos.xyz, 1.0)));
36+
return output;
37+
}
38+
39+
[shader("fragment")]
40+
float4 fragmentMain(VSOutput input)
41+
{
42+
float4 color = samplerColor.Sample(input.UV);
43+
return float4(color.rgb, 1.0);
44+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
float3 instancePos;
14+
float3 instanceRot;
15+
float instanceScale;
16+
int instanceTexIndex;
17+
};
18+
19+
struct VSOutput
20+
{
21+
float4 Pos : SV_POSITION;
22+
float3 Normal;
23+
float3 Color;
24+
float3 UV;
25+
float3 ViewVec;
26+
float3 LightVec;
27+
};
28+
29+
struct UBO
30+
{
31+
float4x4 projection;
32+
float4x4 modelview;
33+
};
34+
ConstantBuffer<UBO> ubo;
35+
36+
Sampler2DArray samplerArray;
37+
38+
[shader("vertex")]
39+
VSOutput vertexMain(VSInput input)
40+
{
41+
VSOutput output;
42+
output.Color = input.Color;
43+
output.UV = float3(input.UV, input.instanceTexIndex);
44+
45+
float4x4 mx, my, mz;
46+
47+
// rotate around x
48+
float s = sin(input.instanceRot.x);
49+
float c = cos(input.instanceRot.x);
50+
51+
mx[0] = float4(c, s, 0.0, 0.0);
52+
mx[1] = float4(-s, c, 0.0, 0.0);
53+
mx[2] = float4(0.0, 0.0, 1.0, 0.0);
54+
mx[3] = float4(0.0, 0.0, 0.0, 1.0);
55+
56+
// rotate around y
57+
s = sin(input.instanceRot.y);
58+
c = cos(input.instanceRot.y);
59+
60+
my[0] = float4(c, 0.0, s, 0.0);
61+
my[1] = float4(0.0, 1.0, 0.0, 0.0);
62+
my[2] = float4(-s, 0.0, c, 0.0);
63+
my[3] = float4(0.0, 0.0, 0.0, 1.0);
64+
65+
// rot around z
66+
s = sin(input.instanceRot.z);
67+
c = cos(input.instanceRot.z);
68+
69+
mz[0] = float4(1.0, 0.0, 0.0, 0.0);
70+
mz[1] = float4(0.0, c, s, 0.0);
71+
mz[2] = float4(0.0, -s, c, 0.0);
72+
mz[3] = float4(0.0, 0.0, 0.0, 1.0);
73+
74+
float4x4 rotMat = mul(mz, mul(my, mx));
75+
76+
output.Normal = mul((float4x3)rotMat, input.Normal).xyz;
77+
78+
float4 pos = mul(rotMat, float4((input.Pos.xyz * input.instanceScale) + input.instancePos, 1.0));
79+
80+
output.Pos = mul(ubo.projection, mul(ubo.modelview, pos));
81+
82+
float4 wPos = mul(ubo.modelview, float4(pos.xyz, 1.0));
83+
float4 lPos = float4(0.0, -5.0, 0.0, 1.0);
84+
output.LightVec = lPos.xyz - pos.xyz;
85+
output.ViewVec = -pos.xyz;
86+
return output;
87+
}
88+
89+
[shader("fragment")]
90+
float4 fragmentMain(VSOutput input)
91+
{
92+
float4 color = samplerArray.Sample(input.UV);
93+
94+
if (color.a < 0.5)
95+
{
96+
clip(-1);
97+
}
98+
99+
float3 N = normalize(input.Normal);
100+
float3 L = normalize(input.LightVec);
101+
float3 ambient = float3(0.65, 0.65, 0.65);
102+
float3 diffuse = max(dot(N, L), 0.0) * input.Color;
103+
return float4((ambient + diffuse) * color.rgb, 1.0);
104+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
};
12+
13+
struct VSOutput
14+
{
15+
float4 Pos : SV_POSITION;
16+
float2 UV;
17+
};
18+
19+
struct UBO
20+
{
21+
float4x4 projection;
22+
float4x4 modelview;
23+
};
24+
ConstantBuffer<UBO> ubo;
25+
26+
[shader("vertex")]
27+
VSOutput vertexMain(VSInput input)
28+
{
29+
VSOutput output;
30+
output.UV = input.UV;
31+
// Skysphere always at center, only use rotation part of modelview matrix
32+
output.Pos = mul(ubo.projection, float4(mul((float3x3)ubo.modelview, input.Pos.xyz), 1));
33+
return output;
34+
}
35+
36+
[shader("fragment")]
37+
float4 fragmentMain(VSOutput input)
38+
{
39+
const float4 gradientStart = float4(0.93, 0.9, 0.81, 1.0);
40+
const float4 gradientEnd = float4(0.35, 0.5, 1.0, 1.0);
41+
return lerp(gradientStart, gradientEnd, min(0.5 - (input.UV.y + 0.05), 0.5)/0.15 - 0.5);
42+
}

0 commit comments

Comments
 (0)