yum-mirror/glslang
git clone https://git.yummers.dev/yum-mirror/glslang
284ceb6d
main
1/* 2The MIT License (MIT) 3 4Copyright (c) 2022 Google LLC 5Copyright (c) 2022 Sascha Willems 6 7Permission is hereby granted, free of charge, to any person obtaining a copy 8of this software and associated documentation files (the "Software"), to deal 9in the Software without restriction, including without limitation the rights 10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11copies of the Software, and to permit persons to whom the Software is 12furnished to do so, subject to the following conditions: 13 14The above copyright notice and this permission notice shall be included in all 15copies or substantial portions of the Software. 16 17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23SOFTWARE. 24*/ 25 26Texture2D textureposition : register(t1 ); 27SamplerState samplerposition : register(s1 ); 28Texture2D textureNormal : register(t2 ); 29SamplerState samplerNormal : register(s2 ); 30Texture2D textureAlbedo : register(t3 ); 31SamplerState samplerAlbedo : register(s3 ); 32// Depth from the light's point of view 33//layout (binding = 5) uniform sampler2DShadow samplerShadowMap; 34Texture2DArray textureShadowMap : register(t5 ); 35SamplerState 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 42struct Light 43{ 44float4 position ; 45float4 target ; 46float4 color ; 47float4x4 viewMatrix ; 48}; 49 50struct UBO 51{ 52float4 viewPos ; 53Light lights [LIGHT_COUNT ]; 54int useShadows ; 55int displayDebugTarget ; 56}; 57 58cbuffer ubo : register(b4 ) {UBO ubo ; } 59 60float textureProj (float4 P ,float layer ,float2 offset ) 61{ 62float shadow = 1.0 ; 63float4 shadowCoord = P /P .w ; 64shadowCoord .xy = shadowCoord .xy * 0.5 + 0.5 ; 65 66if (shadowCoord .z > -1.0 && shadowCoord .z < 1.0 ) 67 { 68float dist = textureShadowMap .Sample (samplerShadowMap ,float3 (shadowCoord .xy + offset ,layer )).r ; 69if (shadowCoord .w > 0.0 && dist < shadowCoord .z ) 70 { 71shadow = SHADOW_FACTOR ; 72 } 73 } 74return shadow ; 75} 76 77float filterPCF (float4 sc ,float layer ) 78{ 79int2 texDim ;int elements ;int levels ; 80textureShadowMap .GetDimensions (0 ,texDim .x ,texDim .y ,elements ,levels ); 81float scale = 1.5 ; 82float dx = scale * 1.0 /float (texDim .x ); 83float dy = scale * 1.0 /float (texDim .y ); 84 85float shadowFactor = 0.0 ; 86int count = 0 ; 87int range = 1 ; 88 89for (int x = - range ;x <=range ;x ++ ) 90 { 91for (int y = - range ;y <=range ;y ++ ) 92 { 93shadowFactor += textureProj (sc ,layer ,float2 (dx * x ,dy * y )); 94count ++ ; 95 } 96 97 } 98return shadowFactor /count ; 99} 100 101float3 shadow (float3 fragcolor ,float3 fragPos ) { 102for (int i = 0 ;i < LIGHT_COUNT ;++ i ) 103 { 104float4 shadowClip = mul (ubo .lights [i ].viewMatrix ,float4 (fragPos .xyz ,1.0 )); 105 106float shadowFactor ; 107#ifdef USE_PCF 108shadowFactor = filterPCF (shadowClip ,i ); 109#else 110shadowFactor = textureProj (shadowClip ,i ,float2 (0.0 ,0.0 )); 111#endif 112 113fragcolor *=shadowFactor ; 114 } 115return fragcolor ; 116} 117 118float4 main ([[vk ::location (0 )]]float2 inUV :TEXCOORD0 ) :SV_TARGET 119{ 120// Get G-Buffer values 121float3 fragPos = textureposition .Sample (samplerposition ,inUV ).rgb ; 122float3 normal = textureNormal .Sample (samplerNormal ,inUV ).rgb ; 123float4 albedo = textureAlbedo .Sample (samplerAlbedo ,inUV ); 124 125float3 fragcolor ; 126 127// Debug display 128if (ubo .displayDebugTarget > 0 ) { 129switch (ubo .displayDebugTarget ) { 130case 1 : 131fragcolor .rgb = shadow (float3 (1.0 ,1.0 ,1.0 ),fragPos ); 132break ; 133case 2 : 134fragcolor .rgb = fragPos ; 135break ; 136case 3 : 137fragcolor .rgb = normal ; 138break ; 139case 4 : 140fragcolor .rgb = albedo .rgb ; 141break ; 142case 5 : 143fragcolor .rgb = albedo .aaa ; 144break ; 145 } 146return float4 (fragcolor ,1.0 ); 147 } 148 149// Ambient part 150fragcolor = albedo .rgb * AMBIENT_LIGHT ; 151 152float3 N = normalize (normal ); 153 154for (int i = 0 ;i < LIGHT_COUNT ;++ i ) 155 { 156// Vector to light 157float3 L = ubo .lights [i ].position .xyz - fragPos ; 158// Distance from light to fragment position 159float dist = length (L ); 160L = normalize (L ); 161 162// Viewer to fragment 163float3 V = ubo .viewPos .xyz - fragPos ; 164V = normalize (V ); 165 166float lightCosInnerAngle = cos (radians (15.0 )); 167float lightCosOuterAngle = cos (radians (25.0 )); 168float lightRange = 100.0 ; 169 170// Direction vector from source to target 171float3 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 174float cosDir = dot (L ,dir ); 175float spotEffect = smoothstep (lightCosOuterAngle ,lightCosInnerAngle ,cosDir ); 176float heightAttenuation = smoothstep (lightRange ,0.0f ,dist ); 177 178// Diffuse lighting 179float NdotL = max (0.0 ,dot (N ,L )); 180float3 diff = NdotL .xxx ; 181 182// Specular lighting 183float3 R = reflect (- L ,N ); 184float NdotR = max (0.0 ,dot (R ,V )); 185float3 spec = (pow (NdotR ,16.0 )* albedo .a * 2.5 ).xxx ; 186 187fragcolor += float3 ((diff + spec )* spotEffect * heightAttenuation )* ubo .lights [i ].color .rgb * albedo .rgb ; 188 } 189 190// Shadow calculations in a separate pass 191if (ubo .useShadows > 0 ) 192 { 193fragcolor = shadow (fragcolor ,fragPos ); 194 } 195 196return float4 (fragcolor ,1 ); 197}