Skip to content

Commit c2e3b49

Browse files
committed
Add slang shaders for additional samples
1 parent cb1f443 commit c2e3b49

4 files changed

Lines changed: 266 additions & 0 deletions

File tree

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
};
13+
14+
struct VSOutput
15+
{
16+
float4 Pos : SV_POSITION;
17+
float3 Normal;
18+
float2 UV;
19+
};
20+
21+
struct HSOutput
22+
{
23+
float4 Pos : SV_POSITION;
24+
float3 Normal;
25+
float2 UV;
26+
};
27+
28+
struct DSOutput
29+
{
30+
float4 Pos : SV_POSITION;
31+
float3 Normal;
32+
float2 UV;
33+
float3 EyePos;
34+
float3 LightVec;
35+
};
36+
37+
struct UBO
38+
{
39+
float4x4 projection;
40+
float4x4 model;
41+
float4 lightPos;
42+
float tessAlpha;
43+
float tessStrength;
44+
float tessLevel;
45+
};
46+
ConstantBuffer<UBO> ubo;
47+
48+
Sampler2D samplerColorAndDisplacementMap;
49+
50+
struct ConstantsHSOutput
51+
{
52+
float TessLevelOuter[3] : SV_TessFactor;
53+
float TessLevelInner[2] : SV_InsideTessFactor;
54+
};
55+
56+
ConstantsHSOutput ConstantsHS()
57+
{
58+
ConstantsHSOutput output;
59+
output.TessLevelInner[0] = ubo.tessLevel;
60+
output.TessLevelInner[1] = ubo.tessLevel;
61+
output.TessLevelOuter[0] = ubo.tessLevel;
62+
output.TessLevelOuter[1] = ubo.tessLevel;
63+
output.TessLevelOuter[2] = ubo.tessLevel;
64+
return output;
65+
}
66+
67+
[shader("vertex")]
68+
VSOutput vertexMain(VSInput input)
69+
{
70+
VSOutput output;
71+
output.Pos = float4(input.Pos.xyz, 1.0);
72+
output.UV = input.UV;
73+
output.Normal = input.Normal;
74+
return output;
75+
}
76+
77+
[shader("hull")]
78+
[domain("tri")]
79+
[partitioning("integer")]
80+
[outputtopology("triangle_cw")]
81+
[outputcontrolpoints(3)]
82+
[patchconstantfunc("ConstantsHS")]
83+
[maxtessfactor(20.0f)]
84+
HSOutput hullMain(InputPatch<VSOutput, 3> patch, uint InvocationID: SV_OutputControlPointID)
85+
{
86+
HSOutput output;
87+
output.Pos = patch[InvocationID].Pos;
88+
output.Normal = patch[InvocationID].Normal;
89+
output.UV = patch[InvocationID].UV;
90+
return output;
91+
}
92+
93+
[shader("domain")]
94+
[domain("tri")]
95+
DSOutput domainMain(ConstantsHSOutput input, float3 TessCoord: SV_DomainLocation, const OutputPatch<HSOutput, 3> patch)
96+
{
97+
DSOutput output;
98+
output.Pos = (TessCoord.x * patch[0].Pos) + (TessCoord.y * patch[1].Pos) + (TessCoord.z * patch[2].Pos);
99+
output.UV = (TessCoord.x * patch[0].UV) + (TessCoord.y * patch[1].UV) + (TessCoord.z * patch[2].UV);
100+
output.Normal = TessCoord.x * patch[0].Normal + TessCoord.y * patch[1].Normal + TessCoord.z * patch[2].Normal;
101+
output.Pos.xyz += normalize(output.Normal) * (max(samplerColorAndDisplacementMap.SampleLevel(output.UV.xy, 0).a, 0.0) * ubo.tessStrength);
102+
output.EyePos = output.Pos.xyz;
103+
output.LightVec = normalize(ubo.lightPos.xyz - output.EyePos);
104+
output.Pos = mul(ubo.projection, mul(ubo.model, output.Pos));
105+
return output;
106+
}
107+
108+
[shader("fragment")]
109+
float4 fragmentMain(DSOutput input)
110+
{
111+
float3 N = normalize(input.Normal);
112+
float3 L = normalize(float3(1.0, 1.0, 1.0));
113+
float3 Eye = normalize(-input.EyePos);
114+
float3 Reflected = normalize(reflect(-input.LightVec, input.Normal));
115+
float4 IAmbient = float4(0.0, 0.0, 0.0, 1.0);
116+
float4 IDiffuse = float4(1.0, 1.0, 1.0, 1.0) * max(dot(input.Normal, input.LightVec), 0.0);
117+
return float4((IAmbient + IDiffuse) * float4(samplerColorAndDisplacementMap.Sample(input.UV).rgb, 1.0));
118+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
float Visible;
20+
float3 ViewVec;
21+
float3 LightVec;
22+
};
23+
24+
struct UBO
25+
{
26+
float4x4 projection;
27+
float4x4 view;
28+
float4x4 model;
29+
float4 color;
30+
float4 lightPos;
31+
float visible;
32+
};
33+
ConstantBuffer<UBO> ubo;
34+
35+
[shader("vertex")]
36+
VSOutput vertexMain(VSInput input)
37+
{
38+
VSOutput output;
39+
output.Color = input.Color * ubo.color.rgb;
40+
output.Visible = ubo.visible;
41+
float4x4 modelView = mul(ubo.view, ubo.model);
42+
output.Pos = mul(ubo.projection, mul(modelView, float4(input.Pos.xyz, 1.0)));
43+
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
44+
output.Normal = mul((float3x3)ubo.model, input.Normal);
45+
output.LightVec = ubo.lightPos.xyz - pos.xyz;
46+
output.ViewVec = -pos.xyz;
47+
return output;
48+
}
49+
50+
[shader("fragment")]
51+
float4 fragmentMain(VSOutput input)
52+
{
53+
if (input.Visible > 0.0)
54+
{
55+
float3 N = normalize(input.Normal);
56+
float3 L = normalize(input.LightVec);
57+
float3 V = normalize(input.ViewVec);
58+
float3 R = reflect(-L, N);
59+
float3 diffuse = max(dot(N, L), 0.25) * input.Color;
60+
float3 specular = pow(max(dot(R, V), 0.0), 8.0) * float3(0.75);
61+
return float4(diffuse + specular, 1.0);
62+
}
63+
else
64+
{
65+
return float4(float3(0.1, 0.1, 0.1), 1.0);
66+
}
67+
}
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+
float3 Pos;
10+
float3 Normal;
11+
float3 Color;
12+
};
13+
14+
struct VSOutput
15+
{
16+
float4 Pos : SV_POSITION;
17+
float3 Color;
18+
};
19+
20+
struct UBO
21+
{
22+
float4x4 projection;
23+
float4x4 view;
24+
float4x4 model;
25+
float4 color;
26+
};
27+
ConstantBuffer<UBO> ubo;
28+
29+
[shader("vertex")]
30+
VSOutput vertexMain(VSInput input)
31+
{
32+
VSOutput output;
33+
output.Color = input.Color * ubo.color.rgb;
34+
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, float4(input.Pos.xyz, 1.0))));
35+
return output;
36+
}
37+
38+
[shader("fragment")]
39+
float4 fragmentMain(VSOutput input)
40+
{
41+
return float4(input.Color, 0.5);
42+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSInput
8+
{
9+
float4 Pos;
10+
float3 Color;
11+
};
12+
13+
struct VSOutput
14+
{
15+
float4 Pos : SV_POSITION;
16+
float3 Color;
17+
};
18+
19+
struct UBO
20+
{
21+
float4x4 projection;
22+
float4x4 view;
23+
float4x4 model;
24+
};
25+
ConstantBuffer<UBO> ubo;
26+
27+
[shader("vertex")]
28+
VSOutput vertexMain(VSInput input)
29+
{
30+
VSOutput output;
31+
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, float4(input.Pos.xyz, 1.0))));
32+
return output;
33+
}
34+
35+
[shader("fragment")]
36+
float4 fragmentMain()
37+
{
38+
return float4(1.0, 1.0, 1.0, 1.0);
39+
}

0 commit comments

Comments
 (0)