| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #pragma anki mutator USE_SHADOW_LAYERS 0 1
- ANKI_SPECIALIZATION_CONSTANT_UVEC2(TILE_COUNTS, 0u);
- ANKI_SPECIALIZATION_CONSTANT_U32(Z_SPLIT_COUNT, 2u);
- ANKI_SPECIALIZATION_CONSTANT_U32(TILE_SIZE, 3u);
- #pragma anki start vert
- #include <AnKi/Shaders/QuadVert.glsl>
- #pragma anki end
- #pragma anki start frag
- #include <AnKi/Shaders/PackFunctions.glsl>
- #include <AnKi/Shaders/Functions.glsl>
- #include <AnKi/Shaders/RtShadows.glsl>
- #define CLUSTERED_SHADING_SET 0u
- #define CLUSTERED_SHADING_UNIFORMS_BINDING 0u
- #define CLUSTERED_SHADING_LIGHTS_BINDING 1u
- #define CLUSTERED_SHADING_CLUSTERS_BINDING 4u
- #include <AnKi/Shaders/ClusteredShadingCommon.glsl>
- layout(set = 0, binding = 5) uniform sampler u_nearestAnyClampSampler;
- layout(set = 0, binding = 6) uniform sampler u_trilinearClampSampler;
- layout(set = 0, binding = 7) uniform ANKI_RP texture2D u_gbuffer0Tex;
- layout(set = 0, binding = 8) uniform ANKI_RP texture2D u_gbuffer1Tex;
- layout(set = 0, binding = 9) uniform ANKI_RP texture2D u_gbuffer2Tex;
- layout(set = 0, binding = 10) uniform texture2D u_msDepthRt;
- #if USE_SHADOW_LAYERS
- layout(set = 0, binding = 11) uniform utexture2D u_shadowLayersTex;
- #else
- layout(set = 0, binding = 12) uniform ANKI_RP texture2D u_resolvedSm;
- #endif
- layout(location = 0) in Vec2 in_uv;
- layout(location = 0) out ANKI_RP Vec3 out_color;
- // Common code for lighting
- #define LIGHTING_COMMON_BRDF() \
- const ANKI_RP Vec3 frag2Light = light.m_position - worldPos; \
- const ANKI_RP Vec3 l = normalize(frag2Light); \
- const ANKI_RP Vec3 specC = specularIsotropicLobe(gbuffer, viewDir, l); \
- const ANKI_RP Vec3 diffC = diffuseLobe(gbuffer.m_diffuse); \
- const ANKI_RP F32 att = computeAttenuationFactor(light.m_squareRadiusOverOne, frag2Light); \
- ANKI_RP F32 lambert = max(0.0, dot(gbuffer.m_normal, l));
- void main()
- {
- const F32 depth = textureLod(u_msDepthRt, u_nearestAnyClampSampler, in_uv, 0.0).r;
- const Vec2 ndc = UV_TO_NDC(in_uv);
- if(depth == 1.0)
- {
- out_color = Vec3(0.0);
- return;
- }
- // Get world position
- const Vec4 worldPos4 = u_clusteredShading.m_matrices.m_invertedViewProjectionJitter * Vec4(ndc, depth, 1.0);
- const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
- // Get the cluster
- Cluster cluster = getClusterFragCoord(Vec3(gl_FragCoord.xy, depth), TILE_SIZE, TILE_COUNTS, Z_SPLIT_COUNT,
- u_clusteredShading.m_zSplitMagic.x, u_clusteredShading.m_zSplitMagic.y);
- // out_color = clusterHeatmap(cluster, 1u << CLUSTER_OBJECT_TYPE_POINT_LIGHT); return;
- // Decode GBuffer
- GbufferInfo gbuffer;
- unpackGBufferNoVelocity(textureLod(u_gbuffer0Tex, u_nearestAnyClampSampler, in_uv, 0.0),
- textureLod(u_gbuffer1Tex, u_nearestAnyClampSampler, in_uv, 0.0),
- textureLod(u_gbuffer2Tex, u_nearestAnyClampSampler, in_uv, 0.0), gbuffer);
- gbuffer.m_subsurface = max(gbuffer.m_subsurface, SUBSURFACE_MIN);
- // SM
- #if USE_SHADOW_LAYERS
- ANKI_RP F32 resolvedSm[MAX_RT_SHADOW_LAYERS];
- unpackRtShadows(textureLod(u_shadowLayersTex, u_nearestAnyClampSampler, in_uv, 0.0), resolvedSm);
- #else
- ANKI_RP Vec4 resolvedSm = textureLod(u_resolvedSm, u_trilinearClampSampler, in_uv, 0.0);
- U32 resolvedSmIdx = 0u;
- #endif
- // Ambient and emissive color
- out_color = gbuffer.m_emission;
- // Dir light
- const ANKI_RP Vec3 viewDir = normalize(u_clusteredShading.m_cameraPosition - worldPos);
- const DirectionalLight dirLight = u_clusteredShading.m_directionalLight;
- if(dirLight.m_active != 0u)
- {
- ANKI_RP F32 shadowFactor;
- if(dirLight.m_cascadeCount > 0u)
- {
- #if USE_SHADOW_LAYERS
- shadowFactor = resolvedSm[dirLight.m_shadowLayer];
- #else
- shadowFactor = resolvedSm[0];
- ++resolvedSmIdx;
- #endif
- }
- else
- {
- shadowFactor = 1.0;
- }
- const ANKI_RP Vec3 l = -dirLight.m_direction;
- const ANKI_RP F32 lambert = max(gbuffer.m_subsurface, dot(l, gbuffer.m_normal));
- const ANKI_RP Vec3 diffC = diffuseLobe(gbuffer.m_diffuse);
- const ANKI_RP Vec3 specC = specularIsotropicLobe(gbuffer, viewDir, l);
- out_color += (diffC + specC) * dirLight.m_diffuseColor * (shadowFactor * lambert);
- }
- // Point lights
- ANKI_LOOP while(cluster.m_pointLightsMask != ExtendedClusterObjectMask(0))
- {
- const I32 idx = findLSB2(cluster.m_pointLightsMask);
- cluster.m_pointLightsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
- const PointLight light = u_pointLights2[idx];
- LIGHTING_COMMON_BRDF();
- ANKI_BRANCH if(light.m_shadowAtlasTileScale >= 0.0)
- {
- #if USE_SHADOW_LAYERS
- const ANKI_RP F32 shadow = resolvedSm[light.m_shadowLayer];
- #else
- const ANKI_RP F32 shadow = resolvedSm[resolvedSmIdx++];
- #endif
- lambert *= shadow;
- }
- out_color += (diffC + specC) * light.m_diffuseColor * (att * max(gbuffer.m_subsurface, lambert));
- }
- // Spot lights
- ANKI_LOOP while(cluster.m_spotLightsMask != ExtendedClusterObjectMask(0))
- {
- const I32 idx = findLSB2(cluster.m_spotLightsMask);
- cluster.m_spotLightsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
- const SpotLight light = u_spotLights[idx];
- LIGHTING_COMMON_BRDF();
- const F32 spot = computeSpotFactor(l, light.m_outerCos, light.m_innerCos, light.m_direction);
- ANKI_BRANCH if(light.m_shadowLayer != MAX_U32)
- {
- #if USE_SHADOW_LAYERS
- const ANKI_RP F32 shadow = resolvedSm[light.m_shadowLayer];
- #else
- const ANKI_RP F32 shadow = resolvedSm[resolvedSmIdx++];
- #endif
- lambert *= shadow;
- }
- out_color += (diffC + specC) * light.m_diffuseColor * (att * spot * max(gbuffer.m_subsurface, lambert));
- }
- out_color = saturateRp(out_color);
- }
- #pragma anki end
|