Skip to content

Commit 5484d51

Browse files
committed
Add slang shader for texturing samples
1 parent b6b4fcc commit 5484d51

8 files changed

Lines changed: 485 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
};
13+
14+
struct VSOutput
15+
{
16+
float4 Pos : SV_POSITION;
17+
float2 UV;
18+
float LodBias;
19+
float3 Normal;
20+
float3 ViewVec;
21+
float3 LightVec;
22+
};
23+
24+
struct UBO
25+
{
26+
float4x4 projection;
27+
float4x4 model;
28+
float4 viewPos;
29+
float lodBias;
30+
};
31+
ConstantBuffer<UBO> ubo;
32+
33+
Sampler2D samplerColor;
34+
35+
[shader("vertex")]
36+
VSOutput vertexMain(VSInput input)
37+
{
38+
VSOutput output;
39+
output.UV = input.UV;
40+
output.LodBias = ubo.lodBias;
41+
42+
float3 worldPos = mul(ubo.model, float4(input.Pos, 1.0)).xyz;
43+
44+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
45+
46+
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
47+
output.Normal = mul((float3x3)ubo.model, input.Normal);
48+
float3 lightPos = float3(0.0, 0.0, 0.0);
49+
float3 lPos = mul((float3x3)ubo.model, lightPos.xyz);
50+
output.LightVec = lPos - pos.xyz;
51+
output.ViewVec = ubo.viewPos.xyz - pos.xyz;
52+
return output;
53+
}
54+
55+
[shader("fragment")]
56+
float4 fragmentMain(VSOutput input)
57+
{
58+
float4 color = samplerColor.SampleLevel(input.UV, input.LodBias);
59+
60+
float3 N = normalize(input.Normal);
61+
float3 L = normalize(input.LightVec);
62+
float3 V = normalize(input.ViewVec);
63+
float3 R = reflect(-L, N);
64+
float3 diffuse = max(dot(N, L), 0.0) * float3(1.0, 1.0, 1.0);
65+
float specular = pow(max(dot(R, V), 0.0), 16.0) * color.a;
66+
67+
return float4(diffuse * color.rgb + specular, 1.0);
68+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
};
13+
14+
struct VSOutput
15+
{
16+
float4 Pos : SV_POSITION;
17+
float3 UV;
18+
float3 Normal;
19+
float3 ViewVec;
20+
float3 LightVec;
21+
};
22+
23+
struct UBO
24+
{
25+
float4x4 projection;
26+
float4x4 model;
27+
float4 viewPos;
28+
float depth;
29+
};
30+
ConstantBuffer<UBO> ubo;
31+
32+
Sampler3D samplerColor;
33+
34+
[shader("vertex")]
35+
VSOutput vertexMain(VSInput input)
36+
{
37+
VSOutput output;
38+
output.UV = float3(input.UV, ubo.depth);
39+
40+
float3 worldPos = mul(ubo.model, float4(input.Pos, 1.0)).xyz;
41+
42+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
43+
44+
float4 pos = mul(ubo.model, float4(input.Pos, 1.0));
45+
output.Normal = mul((float3x3)ubo.model, input.Normal);
46+
float3 lightPos = float3(0.0, 0.0, 0.0);
47+
float3 lPos = mul((float3x3)ubo.model, lightPos.xyz);
48+
output.LightVec = lPos - pos.xyz;
49+
output.ViewVec = ubo.viewPos.xyz - pos.xyz;
50+
return output;
51+
}
52+
53+
[shader("fragment")]
54+
float4 fragmentMain(VSOutput input)
55+
{
56+
float4 color = samplerColor.Sample(input.UV);
57+
58+
float3 N = normalize(input.Normal);
59+
float3 L = normalize(input.LightVec);
60+
float3 V = normalize(input.ViewVec);
61+
float3 R = reflect(-L, N);
62+
float3 diffuse = max(dot(N, L), 0.0) * float3(1.0, 1.0, 1.0);
63+
float specular = pow(max(dot(R, V), 0.0), 16.0) * color.r;
64+
65+
return float4(diffuse * color.r + specular, 1.0);
66+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
};
12+
13+
struct VSOutput
14+
{
15+
float4 Pos : SV_POSITION;
16+
float3 UV;
17+
};
18+
19+
struct Instance
20+
{
21+
float4x4 model;
22+
float arrayIndex;
23+
};
24+
25+
struct UBO
26+
{
27+
float4x4 projection;
28+
float4x4 view;
29+
Instance instance[8];
30+
};
31+
ConstantBuffer<UBO> ubo;
32+
33+
Sampler2DArray samplerArray;
34+
35+
[shader("vertex")]
36+
VSOutput vertexMain(VSInput input, uint InstanceIndex: SV_InstanceID)
37+
{
38+
VSOutput output;
39+
output.UV = float3(input.UV, ubo.instance[InstanceIndex].arrayIndex);
40+
float4x4 modelView = mul(ubo.view, ubo.instance[InstanceIndex].model);
41+
output.Pos = mul(ubo.projection, mul(modelView, float4(input.Pos, 1.0)));
42+
return output;
43+
}
44+
45+
[shader("fragment")]
46+
float4 fragmentMain(VSOutput input)
47+
{
48+
return samplerArray.Sample(input.UV);
49+
}
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+
float3 Pos;
10+
float3 Normal;
11+
};
12+
13+
struct VSOutput
14+
{
15+
float4 Pos : SV_POSITION;
16+
float3 WorldPos;
17+
float3 Normal;
18+
float LodBias;
19+
float3 ViewVec;
20+
float3 LightVec;
21+
};
22+
23+
struct UBO
24+
{
25+
float4x4 projection;
26+
float4x4 model;
27+
float4x4 invModel;
28+
float lodBias;
29+
};
30+
ConstantBuffer<UBO> ubo;
31+
32+
SamplerCube samplerColor;
33+
34+
[shader("vertex")]
35+
VSOutput vertexMain(VSInput input)
36+
{
37+
VSOutput output;
38+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
39+
40+
output.WorldPos = mul(ubo.model, float4(input.Pos, 1.0)).xyz;
41+
output.Normal = mul((float3x3)ubo.model, input.Normal);
42+
output.LodBias = ubo.lodBias;
43+
44+
float3 lightPos = float3(0.0f, -5.0f, 5.0f);
45+
output.LightVec = lightPos.xyz - output.WorldPos.xyz;
46+
output.ViewVec = -output.WorldPos;
47+
return output;
48+
}
49+
50+
[shader("fragment")]
51+
float4 fragmentMain(VSOutput input) : SV_TARGET
52+
{
53+
float3 cI = normalize (input.ViewVec);
54+
float3 cR = reflect (cI, normalize(input.Normal));
55+
56+
cR = mul(ubo.invModel, float4(cR, 0.0)).xyz;
57+
// Convert cubemap coordinates into Vulkan coordinate space
58+
cR.z *= -1.0;
59+
60+
float4 color = samplerColor.SampleLevel(cR, input.LodBias);
61+
62+
float3 N = normalize(input.Normal);
63+
float3 L = normalize(input.LightVec);
64+
float3 V = normalize(input.ViewVec);
65+
float3 R = reflect(-L, N);
66+
float3 ambient = float3(0.5, 0.5, 0.5) * color.rgb;
67+
float3 diffuse = max(dot(N, L), 0.0) * float3(1.0, 1.0, 1.0);
68+
float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.5, 0.5, 0.5);
69+
return float4(ambient + diffuse * color.rgb + specular, 1.0);
70+
}
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+
};
11+
12+
struct VSOutput
13+
{
14+
float4 Pos : SV_POSITION;
15+
float3 UVW;
16+
};
17+
18+
struct UBO
19+
{
20+
float4x4 projection;
21+
float4x4 model;
22+
};
23+
ConstantBuffer<UBO> ubo;
24+
25+
SamplerCube samplerCubeMap;
26+
27+
[shader("vertex")]
28+
VSOutput vertexMain(VSInput input)
29+
{
30+
VSOutput output;
31+
output.UVW = input.Pos;
32+
// Convert cubemap coordinates into Vulkan coordinate space
33+
output.UVW.xy *= -1.0;
34+
// Remove translation from view matrix
35+
float4x4 viewMat = ubo.model;
36+
viewMat[0][3] = 0.0;
37+
viewMat[1][3] = 0.0;
38+
viewMat[2][3] = 0.0;
39+
output.Pos = mul(ubo.projection, mul(viewMat, float4(input.Pos.xyz, 1.0)));
40+
return output;
41+
}
42+
43+
[shader("fragment")]
44+
float4 fragmentMain(VSOutput input)
45+
{
46+
return samplerCubeMap.Sample(input.UVW);
47+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
};
12+
13+
struct VSOutput
14+
{
15+
float4 Pos : SV_POSITION;
16+
float3 WorldPos;
17+
float3 Normal;
18+
float3 ViewVec;
19+
float3 LightVec;
20+
};
21+
22+
struct UBO
23+
{
24+
float4x4 projection;
25+
float4x4 model;
26+
float4x4 invModel;
27+
float lodBias;
28+
int cubeMapIndex;
29+
};
30+
ConstantBuffer<UBO> ubo;
31+
32+
SamplerCubeArray samplerCubeMapArray;
33+
34+
[shader("vertex")]
35+
VSOutput vertexMain(VSInput input)
36+
{
37+
VSOutput output;
38+
output.Pos = mul(ubo.projection, mul(ubo.model, float4(input.Pos.xyz, 1.0)));
39+
40+
output.WorldPos = mul(ubo.model, float4(input.Pos, 1.0)).xyz;
41+
output.Normal = mul((float3x3)ubo.model, input.Normal);
42+
43+
float3 lightPos = float3(0.0f, -5.0f, 5.0f);
44+
output.LightVec = lightPos.xyz - output.WorldPos.xyz;
45+
output.ViewVec = -output.WorldPos;
46+
return output;
47+
}
48+
49+
[shader("fragment")]
50+
float4 fragmentMain(VSOutput input)
51+
{
52+
float3 cI = normalize(input.WorldPos.xyz);
53+
float3 cR = reflect(cI, normalize(input.Normal));
54+
55+
cR = mul(ubo.invModel, float4(cR, 0.0)).xyz;
56+
cR.yz *= -1.0;
57+
58+
float4 color = samplerCubeMapArray.SampleLevel(float4(cR, ubo.cubeMapIndex), ubo.lodBias);
59+
60+
float3 N = normalize(input.Normal);
61+
float3 L = normalize(input.LightVec);
62+
float3 V = normalize(input.ViewVec);
63+
float3 R = reflect(-L, N);
64+
float3 ambient = float3(0.5, 0.5, 0.5) * color.rgb;
65+
float3 diffuse = max(dot(N, L), 0.0) * float3(1.0, 1.0, 1.0);
66+
float3 specular = pow(max(dot(R, V), 0.0), 16.0) * float3(0.5, 0.5, 0.5);
67+
return float4(ambient + diffuse * color.rgb + specular, 1.0);
68+
}

0 commit comments

Comments
 (0)