Merge pull request #3031 from rg3igalia/sub-group-size-arb-flat-fix
[platform/upstream/glslang.git] / Test / spv.debuginfo.glsl.frag
1 /*
2 The MIT License (MIT)
3
4 Copyright (c) 2022 Sascha Willems
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 #version 450
26
27 layout (binding = 1) uniform sampler2D samplerposition;
28 layout (binding = 2) uniform sampler2D samplerNormal;
29 layout (binding = 3) uniform sampler2D samplerAlbedo;
30 layout (binding = 5) uniform sampler2DArray samplerShadowMap;
31
32 layout (location = 0) in vec2 inUV;
33
34 layout (location = 0) out vec4 outFragColor;
35
36 #define LIGHT_COUNT 3
37 #define SHADOW_FACTOR 0.25
38 #define AMBIENT_LIGHT 0.1
39 #define USE_PCF
40
41 struct Light 
42 {
43         vec4 position;
44         vec4 target;
45         vec4 color;
46         mat4 viewMatrix;
47 };
48
49 layout (binding = 4) uniform UBO 
50 {
51         vec4 viewPos;
52         Light lights[LIGHT_COUNT];
53         int useShadows;
54         int debugDisplayTarget;
55 } ubo;
56
57 float textureProj(vec4 P, float layer, vec2 offset)
58 {
59         float shadow = 1.0;
60         vec4 shadowCoord = P / P.w;
61         shadowCoord.st = shadowCoord.st * 0.5 + 0.5;
62         
63         if (shadowCoord.z > -1.0 && shadowCoord.z < 1.0) 
64         {
65                 float dist = texture(samplerShadowMap, vec3(shadowCoord.st + offset, layer)).r;
66                 if (shadowCoord.w > 0.0 && dist < shadowCoord.z) 
67                 {
68                         shadow = SHADOW_FACTOR;
69                 }
70         }
71         return shadow;
72 }
73
74 float filterPCF(vec4 sc, float layer)
75 {
76         ivec2 texDim = textureSize(samplerShadowMap, 0).xy;
77         float scale = 1.5;
78         float dx = scale * 1.0 / float(texDim.x);
79         float dy = scale * 1.0 / float(texDim.y);
80
81         float shadowFactor = 0.0;
82         int count = 0;
83         int range = 1;
84         
85         for (int x = -range; x <= range; x++)
86         {
87                 for (int y = -range; y <= range; y++)
88                 {
89                         shadowFactor += textureProj(sc, layer, vec2(dx*x, dy*y));
90                         count++;
91                 }
92         
93         }
94         return shadowFactor / count;
95 }
96
97 vec3 shadow(vec3 fragcolor, vec3 fragpos) {
98         for(int i = 0; i < LIGHT_COUNT; ++i)
99         {
100                 vec4 shadowClip = ubo.lights[i].viewMatrix * vec4(fragpos, 1.0);
101
102                 float shadowFactor;
103                 #ifdef USE_PCF
104                         shadowFactor= filterPCF(shadowClip, i);
105                 #else
106                         shadowFactor = textureProj(shadowClip, i, vec2(0.0));
107                 #endif
108
109                 fragcolor *= shadowFactor;
110         }
111         return fragcolor;
112 }
113
114 void main() 
115 {
116         // Get G-Buffer values
117         vec3 fragPos = texture(samplerposition, inUV).rgb;
118         vec3 normal = texture(samplerNormal, inUV).rgb;
119         vec4 albedo = texture(samplerAlbedo, inUV);
120
121         // Debug display
122         if (ubo.debugDisplayTarget > 0) {
123                 switch (ubo.debugDisplayTarget) {
124                         case 1: 
125                                 outFragColor.rgb = shadow(vec3(1.0), fragPos).rgb;
126                                 break;
127                         case 2: 
128                                 outFragColor.rgb = fragPos;
129                                 break;
130                         case 3: 
131                                 outFragColor.rgb = normal;
132                                 break;
133                         case 4: 
134                                 outFragColor.rgb = albedo.rgb;
135                                 break;
136                         case 5: 
137                                 outFragColor.rgb = albedo.aaa;
138                                 break;
139                 }               
140                 outFragColor.a = 1.0;
141                 return;
142         }
143
144         // Ambient part
145         vec3 fragcolor  = albedo.rgb * AMBIENT_LIGHT;
146
147         vec3 N = normalize(normal);
148                 
149         for(int i = 0; i < LIGHT_COUNT; ++i)
150         {
151                 // Vector to light
152                 vec3 L = ubo.lights[i].position.xyz - fragPos;
153                 // Distance from light to fragment position
154                 float dist = length(L);
155                 L = normalize(L);
156
157                 // Viewer to fragment
158                 vec3 V = ubo.viewPos.xyz - fragPos;
159                 V = normalize(V);
160
161                 float lightCosInnerAngle = cos(radians(15.0));
162                 float lightCosOuterAngle = cos(radians(25.0));
163                 float lightRange = 100.0;
164
165                 // Direction vector from source to target
166                 vec3 dir = normalize(ubo.lights[i].position.xyz - ubo.lights[i].target.xyz);
167
168                 // Dual cone spot light with smooth transition between inner and outer angle
169                 float cosDir = dot(L, dir);
170                 float spotEffect = smoothstep(lightCosOuterAngle, lightCosInnerAngle, cosDir);
171                 float heightAttenuation = smoothstep(lightRange, 0.0f, dist);
172
173                 // Diffuse lighting
174                 float NdotL = max(0.0, dot(N, L));
175                 vec3 diff = vec3(NdotL);
176
177                 // Specular lighting
178                 vec3 R = reflect(-L, N);
179                 float NdotR = max(0.0, dot(R, V));
180                 vec3 spec = vec3(pow(NdotR, 16.0) * albedo.a * 2.5);
181
182                 fragcolor += vec3((diff + spec) * spotEffect * heightAttenuation) * ubo.lights[i].color.rgb * albedo.rgb;
183         }       
184
185         // Shadow calculations in a separate pass
186         if (ubo.useShadows > 0)
187         {
188                 fragcolor = shadow(fragcolor, fragPos);
189         }
190
191         outFragColor = vec4(fragcolor, 1.0);
192 }