init
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
#version 450
|
||||
/* Copyright (c) 2024-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.
|
||||
*/
|
||||
|
||||
layout (input_attachment_index = 0, binding = 0) uniform subpassInput positionDepthAttachment;
|
||||
layout (input_attachment_index = 1, binding = 1) uniform subpassInput normalAttachment;
|
||||
layout (input_attachment_index = 2, binding = 2) uniform subpassInput albedoAttachment;
|
||||
|
||||
layout (location = 0) in vec2 inUV;
|
||||
|
||||
layout (location = 0) out vec4 outColor;
|
||||
|
||||
struct Light {
|
||||
vec4 position;
|
||||
vec3 color;
|
||||
float radius;
|
||||
};
|
||||
|
||||
layout(std140, binding = 3) readonly buffer LightsBuffer
|
||||
{
|
||||
Light lights[ ];
|
||||
};
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
// Read G-Buffer values from previous sub pass
|
||||
vec3 fragPos = subpassLoad(positionDepthAttachment).rgb;
|
||||
vec3 normal = subpassLoad(normalAttachment).rgb;
|
||||
vec4 albedo = subpassLoad(albedoAttachment);
|
||||
|
||||
#define ambient 0.005
|
||||
|
||||
// Ambient part
|
||||
vec3 fragcolor = albedo.rgb * ambient;
|
||||
|
||||
for(int i = 0; i < lights.length(); ++i)
|
||||
{
|
||||
vec3 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)));
|
||||
vec3 diffuse = lights[i].color * albedo.rgb * NdotL * attenuation;
|
||||
|
||||
fragcolor += diffuse;
|
||||
}
|
||||
|
||||
outColor = vec4(fragcolor, 1.0);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
#version 450
|
||||
/* Copyright (c) 2024-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.
|
||||
*/
|
||||
|
||||
layout (location = 0) out vec2 outUV;
|
||||
|
||||
void main()
|
||||
{
|
||||
outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
|
||||
gl_Position = vec4(outUV * 2.0f - 1.0f, 0.0f, 1.0f);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,52 @@
|
||||
#version 450
|
||||
/* Copyright (c) 2024-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.
|
||||
*/
|
||||
|
||||
layout (location = 0) in vec3 inNormal;
|
||||
layout (location = 1) in vec4 inColor;
|
||||
layout (location = 2) in vec3 inWorldPos;
|
||||
|
||||
layout (location = 0) out vec4 outColor;
|
||||
layout (location = 1) out vec4 outPositionDepth;
|
||||
layout (location = 2) out vec4 outNormal;
|
||||
layout (location = 3) out vec4 outAlbedo;
|
||||
|
||||
layout (constant_id = 0) const float NEAR_PLANE = 0.1f;
|
||||
layout (constant_id = 1) 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));
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
outPositionDepth = vec4(inWorldPos, 1.0);
|
||||
|
||||
vec3 N = normalize(inNormal);
|
||||
N.y = -N.y;
|
||||
outNormal = vec4(N, 1.0);
|
||||
|
||||
outAlbedo = inColor;
|
||||
|
||||
// Store linearized depth in alpha component
|
||||
outPositionDepth.a = linearDepth(gl_FragCoord.z);
|
||||
|
||||
// Write color attachments to avoid undefined behaviour (validation error)
|
||||
outColor = vec4(0.0);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,54 @@
|
||||
#version 450
|
||||
/* Copyright (c) 2024-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.
|
||||
*/
|
||||
|
||||
layout (location = 0) in vec3 inPos;
|
||||
layout (location = 1) in vec3 inNormal;
|
||||
|
||||
layout (binding = 0) uniform UBO
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 model;
|
||||
mat4 view;
|
||||
} ubo;
|
||||
|
||||
layout (location = 0) out vec3 outNormal;
|
||||
layout (location = 1) out vec4 outColor;
|
||||
layout (location = 2) out vec3 outWorldPos;
|
||||
|
||||
layout(push_constant) uniform SceneNode {
|
||||
mat4 matrix;
|
||||
vec4 color;
|
||||
} sceneNode;
|
||||
|
||||
void main()
|
||||
{
|
||||
mat4 nodeMat = ubo.model * sceneNode.matrix;
|
||||
|
||||
gl_Position = ubo.projection * ubo.view * nodeMat * vec4(inPos, 1.0);
|
||||
|
||||
// Vertex position in world space
|
||||
outWorldPos = vec3(sceneNode.matrix * vec4(inPos, 1.0));
|
||||
// GL to Vulkan coord space
|
||||
outWorldPos.y = -outWorldPos.y;
|
||||
|
||||
// Normal in world space
|
||||
mat3 mNormal = transpose(inverse(mat3(mat3(nodeMat))));
|
||||
outNormal = normalize(mNormal * inNormal);
|
||||
|
||||
outColor = sceneNode.color;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,53 @@
|
||||
#version 450
|
||||
/* Copyright (c) 2024-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.
|
||||
*/
|
||||
|
||||
// Should be zero, also rename in other shader
|
||||
layout (input_attachment_index = 0, binding = 0) uniform subpassInput positionDepthAttachment;
|
||||
|
||||
layout (binding = 2) uniform sampler2D samplerTexture;
|
||||
|
||||
layout (location = 0) in vec4 inColor;
|
||||
layout (location = 1) in vec2 inUV;
|
||||
|
||||
layout (location = 0) out vec4 outColor;
|
||||
|
||||
layout (constant_id = 0) const float NEAR_PLANE = 0.1f;
|
||||
layout (constant_id = 1) 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));
|
||||
}
|
||||
|
||||
void main ()
|
||||
{
|
||||
// Sample depth from deferred depth buffer and discard if obscured
|
||||
float depth = subpassLoad(positionDepthAttachment).a;
|
||||
|
||||
// Save the sampled texture color before discarding.
|
||||
// This is to avoid implicit derivatives in non-uniform control flow.
|
||||
// @todo: reversed depth
|
||||
vec4 sampledColor = texture(samplerTexture, inUV);
|
||||
if ((depth != 0.0) && (linearDepth(gl_FragCoord.z) < depth))
|
||||
{
|
||||
// discard;
|
||||
};
|
||||
|
||||
outColor = sampledColor;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,45 @@
|
||||
#version 450
|
||||
/* Copyright (c) 2024-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.
|
||||
*/
|
||||
|
||||
layout (location = 0) in vec3 inPos;
|
||||
layout (location = 1) in vec3 inNormal;
|
||||
layout (location = 2) in vec2 inUV;
|
||||
|
||||
layout (binding = 1) uniform UBO
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 model;
|
||||
mat4 view;
|
||||
} ubo;
|
||||
|
||||
layout (location = 0) out vec4 outColor;
|
||||
layout (location = 1) out vec2 outUV;
|
||||
|
||||
layout(push_constant) uniform SceneNode {
|
||||
mat4 matrix;
|
||||
vec4 color;
|
||||
} sceneNode;
|
||||
|
||||
|
||||
void main ()
|
||||
{
|
||||
outColor = vec4(1.0);
|
||||
outUV = inUV;
|
||||
|
||||
gl_Position = ubo.projection * ubo.view * ubo.model * sceneNode.matrix * vec4(inPos.xyz, 1.0);
|
||||
}
|
||||
Binary file not shown.
@@ -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);
|
||||
}
|
||||
Binary file not shown.
@@ -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;
|
||||
}
|
||||
Binary file not shown.
@@ -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;
|
||||
}
|
||||
Binary file not shown.
@@ -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;
|
||||
}
|
||||
Binary file not shown.
@@ -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;
|
||||
}
|
||||
Binary file not shown.
@@ -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;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,68 @@
|
||||
/* 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;
|
||||
};
|
||||
|
||||
struct Light
|
||||
{
|
||||
float4 position;
|
||||
float3 color;
|
||||
float radius;
|
||||
};
|
||||
// Binding 0 : Position storage buffer
|
||||
StructuredBuffer<Light> lights;
|
||||
|
||||
[shader("fragment")]
|
||||
float4 main(VSOutput input)
|
||||
{
|
||||
// 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);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,31 @@
|
||||
/* 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;
|
||||
};
|
||||
|
||||
[shader("vertex")]
|
||||
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;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,57 @@
|
||||
/* 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;
|
||||
float4 Color;
|
||||
float3 WorldPos;
|
||||
};
|
||||
|
||||
[[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));
|
||||
}
|
||||
|
||||
struct FSOutput
|
||||
{
|
||||
float4 Color;
|
||||
float4 PositionDepth;
|
||||
float4 Normal;
|
||||
float4 Albedo;
|
||||
};
|
||||
|
||||
[shader("fragment")]
|
||||
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;
|
||||
}
|
||||
Binary file not shown.
@@ -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.
|
||||
*/
|
||||
|
||||
struct VSInput
|
||||
{
|
||||
float3 Pos;
|
||||
float3 Normal;
|
||||
};
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
float4x4 model;
|
||||
float4x4 view;
|
||||
float4x4 inverseTranspose;
|
||||
};
|
||||
ConstantBuffer<UBO> ubo;
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
float3 Normal;
|
||||
float4 Color;
|
||||
float3 WorldPos;
|
||||
};
|
||||
|
||||
[shader("vertex")]
|
||||
VSOutput main(VSInput input, uniform float4x4 pushMatrix, uniform float4 pushColor)
|
||||
{
|
||||
float4x4 nodeMat = mul(ubo.model, pushMatrix);
|
||||
VSOutput output;
|
||||
output.Pos = mul(ubo.projection, mul(ubo.view, mul(pushMatrix, float4(input.Pos, 1.0))));
|
||||
// Vertex position in world space
|
||||
output.WorldPos = mul(pushMatrix, 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 = pushColor;
|
||||
return output;
|
||||
}
|
||||
Binary file not shown.
@@ -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;
|
||||
|
||||
[[vk::binding(2, 0)]] Sampler2D samplerTexture;
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
float3 Color;
|
||||
float2 UV;
|
||||
};
|
||||
|
||||
[[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("fragment")]
|
||||
float4 main(VSOutput input)
|
||||
{
|
||||
// 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 = samplerTexture.Sample(input.UV);
|
||||
if ((depth != 0.0) && (linearDepth(input.Pos.z) < depth))
|
||||
{
|
||||
// discard;
|
||||
};
|
||||
|
||||
return sampledColor;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,50 @@
|
||||
/* 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;
|
||||
float3 Normal;
|
||||
float2 UV;
|
||||
};
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
float4x4 model;
|
||||
float4x4 view;
|
||||
};
|
||||
[[vk::binding(1,0)]] ConstantBuffer<UBO> ubo;
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
float4 Color;
|
||||
float2 UV;
|
||||
};
|
||||
|
||||
[shader("vertex")]
|
||||
VSOutput main(VSInput input, uniform float4x4 pushMatrix, uniform float4 pushColor)
|
||||
{
|
||||
VSOutput output;
|
||||
output.Color = pushColor;
|
||||
// @todo
|
||||
// outColor = vec4(1.0);
|
||||
output.Pos = mul(ubo.projection, mul(ubo.view, mul(pushMatrix, float4(input.Pos, 1.0))));
|
||||
output.UV = input.UV;
|
||||
return output;
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user