This commit is contained in:
xsl
2025-09-04 10:54:47 +08:00
commit 6bc8f61b18
1808 changed files with 208268 additions and 0 deletions
@@ -0,0 +1,67 @@
/* Copyright (c) 2025, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
[[vk::input_attachment_index(0)]] SubpassInput positionDepthAttachment;
[[vk::input_attachment_index(1)]] SubpassInput normalAttachment;
[[vk::input_attachment_index(2)]] SubpassInput albedoAttachment;
struct VSOutput
{
float4 Pos : SV_POSITION;
float2 UV : TEXCOORD0;
};
struct Light
{
float4 position;
float3 color;
float radius;
};
// Binding 0 : Position storage buffer
StructuredBuffer<Light> lights;
float4 main(VSOutput input) : SV_TARGET
{
// Read G-Buffer values from previous sub pass
float3 fragPos = positionDepthAttachment.SubpassLoad().rgb;
float3 normal = normalAttachment.SubpassLoad().rgb;
float4 albedo = albedoAttachment.SubpassLoad();
#define ambient 0.005
// 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);
float attenuation = lights[i].radius / (pow(dist, 8.0) + 1.0);
float NdotL = max(0.0, dot(normalize(normal), normalize(L)));
float3 diffuse = lights[i].color * albedo.rgb * NdotL * attenuation;
fragcolor += diffuse;
}
return float4(fragcolor, 1.0);
}
@@ -0,0 +1,30 @@
/* Copyright (c) 2025, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
struct VSOutput
{
float4 Pos : SV_POSITION;
float2 UV : TEXCOORD0;
};
VSOutput main(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;
}
@@ -0,0 +1,56 @@
/* Copyright (c) 2025, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
struct VSOutput
{
float4 Pos : SV_POSITION;
float3 Normal : NORMAL0;
float4 Color : COLOR0;
float3 WorldPos : POS0;
};
[[vk::constant_id(0)]] const float NEAR_PLANE = 0.1;
[[vk::constant_id(1)]] 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));
}
struct FSOutput
{
float4 Color : SV_TARGET0;
float4 PositionDepth : SV_TARGET1;
float4 Normal : SV_TARGET2;
float4 Albedo : SV_TARGET3;
};
FSOutput main(VSOutput input)
{
FSOutput output;
float3 N = normalize(input.Normal);
N.y = -N.y;
output.Normal = float4(N, 1.0);
output.Albedo = input.Color;
// Store linearized depth in alpha component
output.PositionDepth.rgb = input.WorldPos;
output.PositionDepth.a = linearDepth(input.Pos.z);
// Write color attachments to avoid undefined behaviour (validation error)
output.Color = (1.0).rrrr;
return output;
}
@@ -0,0 +1,59 @@
/* Copyright (c) 2025, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
struct VSInput
{
float3 Pos : POSITION0;
float3 Normal : NORMAL0;
};
struct UBO
{
float4x4 projection;
float4x4 model;
float4x4 view;
float4x4 inverseTranspose;
};
ConstantBuffer<UBO> ubo;
struct VSOutput
{
float4 Pos : SV_POSITION;
float3 Normal : NORMAL0;
float4 Color : COLOR0;
float3 WorldPos : POS0;
};
struct PushConsts {
float4x4 Matrix;
float4 Color;
};
[[vk::push_constant]] PushConsts pushConsts;
VSOutput main(VSInput input)
{
float4x4 nodeMat = mul(ubo.model, pushConsts.Matrix);
VSOutput output;
output.Pos = mul(ubo.projection, mul(ubo.view, mul(pushConsts.Matrix, float4(input.Pos, 1.0))));
// Vertex position in world space
output.WorldPos = mul(pushConsts.Matrix, float4(input.Pos, 1.0)).xyz;
// GL to Vulkan coord space
output.WorldPos.y = -output.WorldPos.y;
output.Normal = mul((float3x3)nodeMat, input.Normal);
output.Color = pushConsts.Color;
return output;
}
@@ -0,0 +1,54 @@
/* Copyright (c) 2025, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
[[vk::input_attachment_index(0)]] SubpassInput positionDepthAttachment;
Texture2D texture : register(t2);
SamplerState samplerTexture : register(s2);
struct VSOutput
{
float4 Pos : SV_POSITION;
float3 Color : COLOR0;
float2 UV : TEXCOORD0;
};
[[vk::constant_id(0)]] const float NEAR_PLANE = 0.1;
[[vk::constant_id(1)]] 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));
}
float4 main(VSOutput input) : SV_TARGET
{
// Sample depth from deferred depth buffer and discard if obscured
float depth = positionDepthAttachment.SubpassLoad().a;
// Save the sampled texture color before discarding.
// This is to avoid implicit derivatives in non-uniform control flow.
// @todo: reversed depth
float4 sampledColor = texture.Sample(samplerTexture, input.UV);
if ((depth != 0.0) && (linearDepth(input.Pos.z) < depth))
{
// discard;
};
return sampledColor;
}
@@ -0,0 +1,53 @@
/* Copyright (c) 2025, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
struct VSInput
{
float3 Pos : POSITION0;
float3 Normal : NORMAL0;
float2 UV : TEXCOORD;
};
struct UBO
{
float4x4 projection;
float4x4 model;
float4x4 view;
};
[[vk::binding(1,0)]] ConstantBuffer<UBO> ubo;
struct VSOutput
{
float4 Pos : SV_POSITION;
float3 Color : COLOR0;
float2 UV : TEXCOORD0;
};
struct PushConsts {
float4x4 Matrix;
float4 Color;
};
[[vk::push_constant]] PushConsts pushConsts;
VSOutput main(VSInput input)
{
VSOutput output;
output.Color = pushConsts.Color.rgb;
output.Pos = mul(ubo.projection, mul(ubo.view, mul(pushConsts.Matrix, float4(input.Pos, 1.0))));
output.UV = input.UV;
return output;
}