Added final missing slang shaders

Requires a very recent version of slang
This commit is contained in:
Sascha Willems 2025-05-31 13:43:45 +02:00
parent 0d5becd8ea
commit 487fd21d44
5 changed files with 431 additions and 0 deletions

View file

@ -0,0 +1,66 @@
/* Copyright (c) 2025, Sascha Willems
*
* SPDX-License-Identifier: MIT
*
*/
struct VSOutput
{
float4 Pos : SV_POSITION;
float2 UV;
};
[[vk::input_attachment_index(0)]] SubpassInput inputPosition;
[[vk::input_attachment_index(1)]] SubpassInput inputNormal;
[[vk::input_attachment_index(2)]] SubpassInput inputAlbedo;
struct Light {
float4 position;
float3 color;
float radius;
};
RWStructuredBuffer<Light> lights;
[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()
{
// Read G-Buffer values from previous sub pass
float3 fragPos = inputPosition.SubpassLoad().rgb;
float3 normal = inputNormal.SubpassLoad().rgb;
float4 albedo = inputAlbedo.SubpassLoad();
#define ambient 0.05
// Ambient part
float3 fragcolor = albedo.rgb * ambient;
uint lightsLength;
uint lightsStride;
lights.GetDimensions(lightsLength, lightsStride);
for(int i = 0; i < lightsLength; ++i)
{
float3 L = lights[i].position.xyz - fragPos;
float dist = length(L);
L = normalize(L);
float atten = lights[i].radius / (pow(dist, 3.0) + 1.0);
float3 N = normalize(normal);
float NdotL = max(0.0, dot(N, L));
float3 diff = lights[i].color * albedo.rgb * NdotL * atten;
fragcolor += diff;
}
return float4(fragcolor, 1.0);
}

View file

@ -0,0 +1,78 @@
/* Copyright (c) 2025, Sascha Willems
*
* SPDX-License-Identifier: MIT
*
*/
struct VSInput
{
float4 Pos;
float3 Color;
float3 Normal;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
float3 Normal;
float3 Color;
float3 WorldPos;
float3 Tangent;
};
struct FSOutput
{
float4 Color : SV_TARGET0;
float4 Position : SV_TARGET1;
float4 Normal : SV_TARGET2;
float4 Albedo : SV_TARGET3;
};
struct UBO
{
float4x4 projection;
float4x4 model;
float4x4 view;
};
ConstantBuffer<UBO> ubo;
[[SpecializationConstant]] const float NEAR_PLANE = 0.1;
[[SpecializationConstant]] const float FAR_PLANE = 256.0;
float linearDepth(float depth)
{
float z = depth * 2.0f - 1.0f;
return (2.0f * NEAR_PLANE * FAR_PLANE) / (FAR_PLANE + NEAR_PLANE - z * (FAR_PLANE - NEAR_PLANE));
}
[shader("vertex")]
VSOutput vertexMain(VSInput input)
{
VSOutput output;
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, input.Pos)));
// Vertex position in world space
output.WorldPos = mul(ubo.model, input.Pos).xyz;
// GL to Vulkan coord space
output.WorldPos.y = -output.WorldPos.y;
// Normal in world space
output.Normal = mul((float3x3)ubo.model, normalize(input.Normal));
// Currently just vertex color
output.Color = input.Color;
return output;
}
[shader("fragment")]
FSOutput fragmentMain(VSOutput input)
{
FSOutput output;
output.Position = float4(input.WorldPos, 1.0);
float3 N = normalize(input.Normal);
N.y = -N.y;
output.Normal = float4(N, 1.0);
output.Albedo.rgb = input.Color;
// Store linearized depth in alpha component
output.Position.a = linearDepth(input.Pos.z);
// Write color attachments to avoid undefined behaviour (validation error)
output.Color = float4(0.0);
return output;
}

View file

@ -0,0 +1,64 @@
/* Copyright (c) 2025, Sascha Willems
*
* SPDX-License-Identifier: MIT
*
*/
struct VSInput
{
float4 Pos;
float3 Color;
float3 Normal;
float2 UV;
};
struct VSOutput
{
float4 Pos : SV_POSITION;
float3 Color;
float2 UV;
};
struct UBO
{
float4x4 projection;
float4x4 model;
float4x4 view;
};
ConstantBuffer<UBO> ubo;
[[vk::input_attachment_index(0)]] SubpassInput samplerPositionDepth;
Sampler2D samplerTexture;
[[SpecializationConstant]] const float NEAR_PLANE = 0.1f;
[[SpecializationConstant]] const float FAR_PLANE = 256.0f;
float linearDepth(float depth)
{
float z = depth * 2.0f - 1.0f;
return (2.0f * NEAR_PLANE * FAR_PLANE) / (FAR_PLANE + NEAR_PLANE - z * (FAR_PLANE - NEAR_PLANE));
}
[shader("vertex")]
VSOutput vertexMain(VSInput input)
{
VSOutput output;
output.Color = input.Color;
output.UV = input.UV;
output.Pos = mul(ubo.projection, mul(ubo.view, mul(ubo.model, float4(input.Pos.xyz, 1.0))));
return output;
}
[shader("fragment")]
float4 fragmentMain (VSOutput input)
{
// Sample depth from deferred depth buffer and discard if obscured
float depth = samplerPositionDepth.SubpassLoad().a;
if ((depth != 0.0) && (linearDepth(input.Pos.z) > depth))
{
clip(-1);
};
return samplerTexture.Sample(input.UV);
}