Added slang shaders for additional samples

This commit is contained in:
Sascha Willems 2025-05-19 21:42:08 +02:00
parent 829118736f
commit 834ee9ed83
5 changed files with 271 additions and 0 deletions

View file

@ -0,0 +1,68 @@
/* Copyright (c) 2025, Sascha Willems
*
* SPDX-License-Identifier: MIT
*
*/
struct VSInput
{
float3 Pos;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
float4 ProjCoord;
};
struct UBO
{
float4x4 projection;
float4x4 view;
float4x4 model;
};
ConstantBuffer<UBO> ubo;
Sampler2D samplerColor;
[shader("vertex")]
VSOutput vertexMain(VSInput input)
{
VSOutput output;
output.ProjCoord = mul(ubo.projection, mul(ubo.view, mul(ubo.model, float4(input.Pos.xyz, 1.0))));
output.Pos = output.ProjCoord;
return output;
}
[shader("fragment")]
float4 fragmentMain(VSOutput input, bool FrontFacing : SV_IsFrontFace)
{
float4 tmp = (1.0 / input.ProjCoord.w).xxxx;
float4 projCoord = input.ProjCoord * tmp;
// Scale and bias
projCoord += float4(1.0, 1.0, 1.0, 1.0);
projCoord *= float4(0.5, 0.5, 0.5, 0.5);
// Slow single pass blur
// For demonstration purposes only
const float blurSize = 1.0 / 512.0;
float4 color = float4(float3(0.0, 0.0, 0.0), 1.);
if (FrontFacing)
{
// Only render mirrored scene on front facing (upper) side of mirror surface
float4 reflection = float4(0.0, 0.0, 0.0, 0.0);
for (int x = -3; x <= 3; x++)
{
for (int y = -3; y <= 3; y++)
{
reflection += samplerColor.Sample(float2(projCoord.x + x * blurSize, projCoord.y + y * blurSize)) / 49.0;
}
}
color += reflection;
}
return color;
}

View file

@ -0,0 +1,63 @@
/* Copyright (c) 2025, Sascha Willems
*
* SPDX-License-Identifier: MIT
*
*/
struct VSInput
{
float3 Pos;
float3 Color;
float3 Normal;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
float ClipDistance : SV_ClipDistance0;
float3 Normal;
float3 Color;
float3 EyePos;
float3 LightVec;
};
struct UBO
{
float4x4 projection;
float4x4 view;
float4x4 model;
float4 lightPos;
};
ConstantBuffer<UBO> ubo;
[shader("vertex")]
VSOutput vertexMain(VSInput input)
{
VSOutput output;
output.Normal = input.Normal;
output.Color = input.Color;
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, float4(input.Pos, 1.0))));
output.EyePos = mul(ubo.view, mul(ubo.model, float4(input.Pos, 1.0))).xyz;
output.LightVec = normalize(ubo.lightPos.xyz - output.EyePos);
// Clip against reflection plane
float4 clipPlane = float4(0.0, 0.0, 0.0, 0.0);
output.ClipDistance = dot(float4(input.Pos, 1.0), clipPlane);
return output;
}
[shader("fragment")]
float4 fragmentMain(VSOutput input)
{
float3 Eye = normalize(-input.EyePos);
float3 Reflected = normalize(reflect(-input.LightVec, input.Normal));
float4 IAmbient = float4(0.1, 0.1, 0.1, 1.0);
float4 IDiffuse = max(dot(input.Normal, input.LightVec), 0.0).xxxx;
float specular = 0.75;
float4 ISpecular = float4(0.0, 0.0, 0.0, 0.0);
if (dot(input.EyePos, input.Normal) < 0.0)
{
ISpecular = float4(0.5, 0.5, 0.5, 1.0) * pow(max(dot(Reflected, Eye), 0.0), 16.0) * specular;
}
return float4((IAmbient + IDiffuse) * float4(input.Color, 1.0) + ISpecular);
}

View file

@ -0,0 +1,28 @@
/* Copyright (c) 2025, Sascha Willems
*
* SPDX-License-Identifier: MIT
*
*/
struct VSOutput
{
float4 Pos : SV_POSITION;
float2 UV;
};
[[vk::binding(1, 0)]] Sampler2D samplerColor;
[shader("vertex")]
VSOutput vertexMain(uint VertexIndex: SV_VertexID)
{
VSOutput output;
output.UV = float2((VertexIndex << 1) & 2, VertexIndex & 2);
output.Pos = float4(output.UV * 2.0f - 1.0f, 0.0f, 1.0f);
return output;
}
[shader("fragment")]
float4 fragmentMain(VSOutput input)
{
return samplerColor.Sample(input.UV);
}