Skip to content

Commit 24bc3e3

Browse files
committed
Added slang shaders for additional samples
1 parent 834ee9e commit 24bc3e3

5 files changed

Lines changed: 361 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSInput
8+
{
9+
float3 Pos;
10+
float2 UV;
11+
float3 Normal;
12+
float4 Tangent;
13+
};
14+
15+
struct VSOutput
16+
{
17+
float4 Pos : SV_POSITION;
18+
float2 UV;
19+
float3 LightVec;
20+
float3 LightVecB;
21+
float3 LightDir;
22+
float3 ViewVec;
23+
};
24+
25+
struct UBO
26+
{
27+
float4x4 projection;
28+
float4x4 model;
29+
float4x4 normal;
30+
float4 lightPos;
31+
};
32+
ConstantBuffer<UBO> ubo;
33+
34+
Sampler2D samplerColorMap;
35+
Sampler2D samplerNormalHeightMap;
36+
37+
#define lightRadius 45.0
38+
39+
[shader("vertex")]
40+
VSOutput vertexMain(VSInput input)
41+
{
42+
VSOutput output;
43+
float3 vertexPosition = mul(ubo.model, float4(input.Pos, 1.0)).xyz;
44+
output.LightDir = normalize(ubo.lightPos.xyz - vertexPosition);
45+
46+
float3 biTangent = cross(input.Normal, input.Tangent.xyz);
47+
48+
// Setup (t)angent-(b)inormal-(n)ormal matrix for converting
49+
// object coordinates into tangent space
50+
float3x3 tbnMatrix;
51+
tbnMatrix[0] = mul((float3x3)ubo.normal, input.Tangent.xyz);
52+
tbnMatrix[1] = mul((float3x3)ubo.normal, biTangent);
53+
tbnMatrix[2] = mul((float3x3)ubo.normal, input.Normal);
54+
55+
output.LightVec.xyz = mul(float3(ubo.lightPos.xyz - vertexPosition), tbnMatrix);
56+
57+
float3 lightDist = ubo.lightPos.xyz - input.Pos;
58+
output.LightVecB.x = dot(input.Tangent.xyz, lightDist);
59+
output.LightVecB.y = dot(biTangent, lightDist);
60+
output.LightVecB.z = dot(input.Normal, lightDist);
61+
62+
output.ViewVec.x = dot(input.Tangent.xyz, input.Pos);
63+
output.ViewVec.y = dot(biTangent, input.Pos);
64+
output.ViewVec.z = dot(input.Normal, input.Pos);
65+
66+
output.UV = input.UV;
67+
68+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos, 1.0)));
69+
return output;
70+
}
71+
72+
[shader("fragment")]
73+
float4 fragmentMain(VSOutput input)
74+
{
75+
float3 specularColor = float3(0.85, 0.5, 0.0);
76+
77+
float invRadius = 1.0/lightRadius;
78+
float ambient = 0.25;
79+
80+
float3 rgb, normal;
81+
82+
rgb = samplerColorMap.Sample(input.UV).rgb;
83+
normal = normalize((samplerNormalHeightMap.Sample(input.UV).rgb - 0.5) * 2.0);
84+
85+
float distSqr = dot(input.LightVecB, input.LightVecB);
86+
float3 lVec = input.LightVecB * rsqrt(distSqr);
87+
88+
float atten = max(clamp(1.0 - invRadius * sqrt(distSqr), 0.0, 1.0), ambient);
89+
float diffuse = clamp(dot(lVec, normal), 0.0, 1.0);
90+
91+
float3 light = normalize(-input.LightVec);
92+
float3 view = normalize(input.ViewVec);
93+
float3 reflectDir = reflect(-light, normal);
94+
95+
float specular = pow(max(dot(view, reflectDir), 0.0), 4.0);
96+
97+
return float4((rgb * atten + (diffuse * rgb + 0.5 * specular * specularColor.rgb)) * atten, 1.0);
98+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSInput
8+
{
9+
float4 Pos;
10+
float4 Color;
11+
float Alpha;
12+
float Size;
13+
float Rotation;
14+
int Type;
15+
};
16+
17+
struct VSOutput
18+
{
19+
float4 Pos : SV_POSITION;
20+
float PSize : SV_PointSize;
21+
float4 Color;
22+
float Alpha;
23+
int Type;
24+
float Rotation;
25+
float2 CenterPos;
26+
float PointSize;
27+
};
28+
29+
struct UBO
30+
{
31+
float4x4 projection;
32+
float4x4 modelview;
33+
float2 viewportDim;
34+
float pointSize;
35+
};
36+
ConstantBuffer<UBO> ubo;
37+
38+
Sampler2D samplerSmoke;
39+
Sampler2D samplerFire;
40+
41+
[shader("vertex")]
42+
VSOutput vertexMain(VSInput input)
43+
{
44+
VSOutput output;
45+
output.Color = input.Color;
46+
output.Alpha = input.Alpha;
47+
output.Type = input.Type;
48+
output.Rotation = input.Rotation;
49+
50+
output.Pos = mul(ubo.projection, mul(ubo.modelview, float4(input.Pos.xyz, 1.0)));
51+
52+
// Base size of the point sprites
53+
float spriteSize = 8.0 * input.Size;
54+
55+
// Scale particle size depending on camera projection
56+
float4 eyePos = mul(ubo.modelview, float4(input.Pos.xyz, 1.0));
57+
float4 projectedCorner = mul(ubo.projection, float4(0.5 * spriteSize, 0.5 * spriteSize, eyePos.z, eyePos.w));
58+
output.PointSize = output.PSize = ubo.viewportDim.x * projectedCorner.x / projectedCorner.w;
59+
output.CenterPos = ((output.Pos.xy / output.Pos.w) + 1.0) * 0.5 * ubo.viewportDim;
60+
return output;
61+
}
62+
63+
[shader("fragment")]
64+
float4 fragmentMain(VSOutput input)
65+
{
66+
float4 color;
67+
float alpha = (input.Alpha <= 1.0) ? input.Alpha : 2.0 - input.Alpha;
68+
69+
// Rotate texture coordinates
70+
// Rotate UV
71+
float rotCenter = 0.5;
72+
float rotCos = cos(input.Rotation);
73+
float rotSin = sin(input.Rotation);
74+
75+
float2 PointCoord = (input.Pos.xy - input.CenterPos.xy) / input.PointSize + 0.5;
76+
77+
float2 rotUV = float2(
78+
rotCos * (PointCoord.x - rotCenter) + rotSin * (PointCoord.y - rotCenter) + rotCenter,
79+
rotCos * (PointCoord.y - rotCenter) - rotSin * (PointCoord.x - rotCenter) + rotCenter);
80+
81+
float4 outFragColor;
82+
if (input.Type == 0)
83+
{
84+
// Flame
85+
color = samplerFire.Sample(rotUV);
86+
outFragColor.a = 0.0;
87+
}
88+
else
89+
{
90+
// Smoke
91+
color = samplerSmoke.Sample(rotUV);
92+
outFragColor.a = color.a * alpha;
93+
}
94+
95+
outFragColor.rgb = color.rgb * input.Color.rgb * alpha;
96+
return outFragColor;
97+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Copyright (c) 2025, Sascha Willems
2+
*
3+
* SPDX-License-Identifier: MIT
4+
*
5+
*/
6+
7+
struct VSInput
8+
{
9+
float3 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 model;
25+
float gradientPos;
26+
};
27+
ConstantBuffer<UBO> ubo;
28+
29+
Sampler2D samplerGradientRamp;
30+
31+
[shader("vertex")]
32+
VSOutput vertexMain(VSInput input)
33+
{
34+
VSOutput output;
35+
output.Color = input.Color;
36+
output.UV = float2(ubo.gradientPos, 0.0f);
37+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos, 1.0)));
38+
return output;
39+
}
40+
41+
[shader("fragment")]
42+
float4 fragmentMain(VSOutput input)
43+
{
44+
// Use max. color channel value to detect bright glow emitters
45+
if ((input.Color.r >= 0.9) || (input.Color.g >= 0.9) || (input.Color.b >= 0.9))
46+
{
47+
return float4(samplerGradientRamp.Sample(input.UV).rgb, 1);
48+
} else {
49+
return float4(input.Color, 1);
50+
}
51+
}
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+
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+
float3 Color;
20+
float3 EyePos;
21+
float3 LightVec;
22+
float2 UV;
23+
};
24+
25+
struct UBO
26+
{
27+
float4x4 projection;
28+
float4x4 model;
29+
float gradientPos;
30+
};
31+
ConstantBuffer<UBO> ubo;
32+
33+
Sampler2D samplerGradientRamp;
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 = float2(ubo.gradientPos, 0.0);
42+
output.Pos = mul(ubo.projection, mul(ubo.model, input.Pos));
43+
output.EyePos = mul(ubo.model, input.Pos).xyz;
44+
float4 lightPos = float4(0.0, 0.0, -5.0, 1.0); // * ubo.model;
45+
output.LightVec = normalize(lightPos.xyz - input.Pos.xyz);
46+
return output;
47+
}
48+
49+
[shader("fragment")]
50+
float4 fragmentMain(VSOutput input)
51+
{
52+
// No light calculations for glow color
53+
// Use max. color channel value
54+
// to detect bright glow emitters
55+
if ((input.Color.r >= 0.9) || (input.Color.g >= 0.9) || (input.Color.b >= 0.9))
56+
{
57+
return float4(samplerGradientRamp.Sample(input.UV).rgb, 1);
58+
} else {
59+
float3 Eye = normalize(-input.EyePos);
60+
float3 Reflected = normalize(reflect(-input.LightVec, input.Normal));
61+
float4 IAmbient = float4(0.2, 0.2, 0.2, 1.0);
62+
float4 IDiffuse = float4(0.5, 0.5, 0.5, 0.5) * max(dot(input.Normal, input.LightVec), 0.0);
63+
float specular = 0.25;
64+
float4 ISpecular = float4(0.5, 0.5, 0.5, 1.0) * pow(max(dot(Reflected, Eye), 0.0), 4.0) * specular;
65+
return float4((IAmbient + IDiffuse) * float4(input.Color, 1.0) + ISpecular);
66+
}
67+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 radialBlurScale;
16+
float radialBlurStrength;
17+
float2 radialOrigin;
18+
};
19+
ConstantBuffer<UBO> ubo;
20+
21+
Sampler2D samplerColor;
22+
23+
[shader("vertex")]
24+
VSOutput vertexMain(uint VertexIndex: SV_VertexID)
25+
{
26+
VSOutput output;
27+
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
28+
output.Pos = float4(output.UV * 2.0f - 1.0f, 0.0f, 1.0f);
29+
return output;
30+
}
31+
32+
[shader("fragment")]
33+
float4 fragmentMain(VSOutput input)
34+
{
35+
int2 texDim;
36+
samplerColor.GetDimensions(texDim.x, texDim.y);
37+
float2 radialSize = float2(1.0 / texDim.x, 1.0 / texDim.y);
38+
float2 UV = input.UV;
39+
float4 color = float4(0.0, 0.0, 0.0, 0.0);
40+
UV += radialSize * 0.5 - ubo.radialOrigin;
41+
#define samples 32
42+
for (int i = 0; i < samples; i++)
43+
{
44+
float scale = 1.0 - ubo.radialBlurScale * (float(i) / float(samples - 1));
45+
color += samplerColor.Sample(UV * scale + ubo.radialOrigin);
46+
}
47+
return (color / samples) * ubo.radialBlurStrength;
48+
}

0 commit comments

Comments
 (0)