4 Copyright (c) 2022 Google LLC
5 Copyright (c) 2022 Sascha Willems
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 Texture2D textureposition : register(t1);
27 SamplerState samplerposition : register(s1);
28 Texture2D textureNormal : register(t2);
29 SamplerState samplerNormal : register(s2);
30 Texture2D textureAlbedo : register(t3);
31 SamplerState samplerAlbedo : register(s3);
32 // Depth from the light's point of view
33 //layout (binding = 5) uniform sampler2DShadow samplerShadowMap;
34 Texture2DArray textureShadowMap : register(t5);
35 SamplerState samplerShadowMap : register(s5);
38 #define SHADOW_FACTOR 0.25
39 #define AMBIENT_LIGHT 0.1
53 Light lights[LIGHT_COUNT];
55 int displayDebugTarget;
58 cbuffer ubo : register(b4) { UBO ubo; }
60 float textureProj(float4 P, float layer, float2 offset)
63 float4 shadowCoord = P / P.w;
64 shadowCoord.xy = shadowCoord.xy * 0.5 + 0.5;
66 if (shadowCoord.z > -1.0 && shadowCoord.z < 1.0)
68 float dist = textureShadowMap.Sample(samplerShadowMap, float3(shadowCoord.xy + offset, layer)).r;
69 if (shadowCoord.w > 0.0 && dist < shadowCoord.z)
71 shadow = SHADOW_FACTOR;
77 float filterPCF(float4 sc, float layer)
79 int2 texDim; int elements; int levels;
80 textureShadowMap.GetDimensions(0, texDim.x, texDim.y, elements, levels);
82 float dx = scale * 1.0 / float(texDim.x);
83 float dy = scale * 1.0 / float(texDim.y);
85 float shadowFactor = 0.0;
89 for (int x = -range; x <= range; x++)
91 for (int y = -range; y <= range; y++)
93 shadowFactor += textureProj(sc, layer, float2(dx*x, dy*y));
98 return shadowFactor / count;
101 float3 shadow(float3 fragcolor, float3 fragPos) {
102 for (int i = 0; i < LIGHT_COUNT; ++i)
104 float4 shadowClip = mul(ubo.lights[i].viewMatrix, float4(fragPos.xyz, 1.0));
108 shadowFactor= filterPCF(shadowClip, i);
110 shadowFactor = textureProj(shadowClip, i, float2(0.0, 0.0));
113 fragcolor *= shadowFactor;
118 float4 main([[vk::location(0)]] float2 inUV : TEXCOORD0) : SV_TARGET
120 // Get G-Buffer values
121 float3 fragPos = textureposition.Sample(samplerposition, inUV).rgb;
122 float3 normal = textureNormal.Sample(samplerNormal, inUV).rgb;
123 float4 albedo = textureAlbedo.Sample(samplerAlbedo, inUV);
128 if (ubo.displayDebugTarget > 0) {
129 switch (ubo.displayDebugTarget) {
131 fragcolor.rgb = shadow(float3(1.0, 1.0, 1.0), fragPos);
134 fragcolor.rgb = fragPos;
137 fragcolor.rgb = normal;
140 fragcolor.rgb = albedo.rgb;
143 fragcolor.rgb = albedo.aaa;
146 return float4(fragcolor, 1.0);
150 fragcolor = albedo.rgb * AMBIENT_LIGHT;
152 float3 N = normalize(normal);
154 for(int i = 0; i < LIGHT_COUNT; ++i)
157 float3 L = ubo.lights[i].position.xyz - fragPos;
158 // Distance from light to fragment position
159 float dist = length(L);
162 // Viewer to fragment
163 float3 V = ubo.viewPos.xyz - fragPos;
166 float lightCosInnerAngle = cos(radians(15.0));
167 float lightCosOuterAngle = cos(radians(25.0));
168 float lightRange = 100.0;
170 // Direction vector from source to target
171 float3 dir = normalize(ubo.lights[i].position.xyz - ubo.lights[i].target.xyz);
173 // Dual cone spot light with smooth transition between inner and outer angle
174 float cosDir = dot(L, dir);
175 float spotEffect = smoothstep(lightCosOuterAngle, lightCosInnerAngle, cosDir);
176 float heightAttenuation = smoothstep(lightRange, 0.0f, dist);
179 float NdotL = max(0.0, dot(N, L));
180 float3 diff = NdotL.xxx;
183 float3 R = reflect(-L, N);
184 float NdotR = max(0.0, dot(R, V));
185 float3 spec = (pow(NdotR, 16.0) * albedo.a * 2.5).xxx;
187 fragcolor += float3((diff + spec) * spotEffect * heightAttenuation) * ubo.lights[i].color.rgb * albedo.rgb;
190 // Shadow calculations in a separate pass
191 if (ubo.useShadows > 0)
193 fragcolor = shadow(fragcolor, fragPos);
196 return float4(fragcolor, 1);