Merge pull request #3031 from rg3igalia/sub-group-size-arb-flat-fix
[platform/upstream/glslang.git] / Test / spv.debuginfo.hlsl.frag
1 /*
2 The MIT License (MIT)
3
4 Copyright (c) 2022 Google LLC
5 Copyright (c) 2022 Sascha Willems
6
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:
13
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16
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
23 SOFTWARE.
24 */
25
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);
36
37 #define LIGHT_COUNT 3
38 #define SHADOW_FACTOR 0.25
39 #define AMBIENT_LIGHT 0.1
40 #define USE_PCF
41
42 struct Light
43 {
44         float4 position;
45         float4 target;
46         float4 color;
47         float4x4 viewMatrix;
48 };
49
50 struct UBO
51 {
52         float4 viewPos;
53         Light lights[LIGHT_COUNT];
54         int useShadows;
55         int displayDebugTarget;
56 };
57
58 cbuffer ubo : register(b4) { UBO ubo; }
59
60 float textureProj(float4 P, float layer, float2 offset)
61 {
62         float shadow = 1.0;
63         float4 shadowCoord = P / P.w;
64         shadowCoord.xy = shadowCoord.xy * 0.5 + 0.5;
65
66         if (shadowCoord.z > -1.0 && shadowCoord.z < 1.0)
67         {
68                 float dist = textureShadowMap.Sample(samplerShadowMap, float3(shadowCoord.xy + offset, layer)).r;
69                 if (shadowCoord.w > 0.0 && dist < shadowCoord.z)
70                 {
71                         shadow = SHADOW_FACTOR;
72                 }
73         }
74         return shadow;
75 }
76
77 float filterPCF(float4 sc, float layer)
78 {
79         int2 texDim; int elements; int levels;
80         textureShadowMap.GetDimensions(0, texDim.x, texDim.y, elements, levels);
81         float scale = 1.5;
82         float dx = scale * 1.0 / float(texDim.x);
83         float dy = scale * 1.0 / float(texDim.y);
84
85         float shadowFactor = 0.0;
86         int count = 0;
87         int range = 1;
88
89         for (int x = -range; x <= range; x++)
90         {
91                 for (int y = -range; y <= range; y++)
92                 {
93                         shadowFactor += textureProj(sc, layer, float2(dx*x, dy*y));
94                         count++;
95                 }
96
97         }
98         return shadowFactor / count;
99 }
100
101 float3 shadow(float3 fragcolor, float3 fragPos) {
102         for (int i = 0; i < LIGHT_COUNT; ++i)
103         {
104                 float4 shadowClip = mul(ubo.lights[i].viewMatrix, float4(fragPos.xyz, 1.0));
105
106                 float shadowFactor;
107                 #ifdef USE_PCF
108                         shadowFactor= filterPCF(shadowClip, i);
109                 #else
110                         shadowFactor = textureProj(shadowClip, i, float2(0.0, 0.0));
111                 #endif
112
113                 fragcolor *= shadowFactor;
114         }
115         return fragcolor;
116 }
117
118 float4 main([[vk::location(0)]] float2 inUV : TEXCOORD0) : SV_TARGET
119 {
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);
124
125         float3 fragcolor;
126
127         // Debug display
128         if (ubo.displayDebugTarget > 0) {
129                 switch (ubo.displayDebugTarget) {
130                         case 1: 
131                                 fragcolor.rgb = shadow(float3(1.0, 1.0, 1.0), fragPos);
132                                 break;
133                         case 2: 
134                                 fragcolor.rgb = fragPos;
135                                 break;
136                         case 3: 
137                                 fragcolor.rgb = normal;
138                                 break;
139                         case 4: 
140                                 fragcolor.rgb = albedo.rgb;
141                                 break;
142                         case 5: 
143                                 fragcolor.rgb = albedo.aaa;
144                                 break;
145                 }               
146                 return float4(fragcolor, 1.0);
147         }
148
149         // Ambient part
150         fragcolor  = albedo.rgb * AMBIENT_LIGHT;
151
152         float3 N = normalize(normal);
153
154         for(int i = 0; i < LIGHT_COUNT; ++i)
155         {
156                 // Vector to light
157                 float3 L = ubo.lights[i].position.xyz - fragPos;
158                 // Distance from light to fragment position
159                 float dist = length(L);
160                 L = normalize(L);
161
162                 // Viewer to fragment
163                 float3 V = ubo.viewPos.xyz - fragPos;
164                 V = normalize(V);
165
166                 float lightCosInnerAngle = cos(radians(15.0));
167                 float lightCosOuterAngle = cos(radians(25.0));
168                 float lightRange = 100.0;
169
170                 // Direction vector from source to target
171                 float3 dir = normalize(ubo.lights[i].position.xyz - ubo.lights[i].target.xyz);
172
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);
177
178                 // Diffuse lighting
179                 float NdotL = max(0.0, dot(N, L));
180                 float3 diff = NdotL.xxx;
181
182                 // Specular lighting
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;
186
187                 fragcolor += float3((diff + spec) * spotEffect * heightAttenuation) * ubo.lights[i].color.rgb * albedo.rgb;
188         }
189
190         // Shadow calculations in a separate pass
191         if (ubo.useShadows > 0)
192         {
193                 fragcolor = shadow(fragcolor, fragPos);
194         }
195
196         return float4(fragcolor, 1);
197 }