| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- 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>
- layout(set = 0, binding = 0) uniform sampler u_nearestAnyClampSampler;
- layout(set = 0, binding = 1) uniform texture2D u_msDepthRt;
- layout(set = 0, binding = 2) uniform sampler u_trilinearRepeatSampler;
- #define CLUSTERED_SHADING_SET 0
- #define CLUSTERED_SHADING_UNIFORMS_BINDING 3
- #define CLUSTERED_SHADING_DECALS_BINDING 4
- #define CLUSTERED_SHADING_CLUSTERS_BINDING 7
- #include <AnKi/Shaders/ClusteredShadingCommon.glsl>
- layout(location = 0) in Vec2 in_uv;
- layout(location = 0) out Vec4 out_color0;
- layout(location = 1) out Vec4 out_color1;
- void main()
- {
- // This code blends the diffuse and the specular+rougness of the decals with GBuffer render targets.
- // Normaly the blending is being done ('D' is the decal diffuse and 'f' is decal blend factor):
- // d=gbuffer.diff
- // 1st decal: d'=d*(1-f)+D*f
- // 2nd decal: d''=d'*(1-f')+D'*f' <=> d''=d*(1-f)*(1-f')+D*f*(1-f')+D'*f'
- // By looking at the trend we will have to multiply the gbuffer.diff with: (1-f)*(1-f') ... (1-f'''')
- Vec4 outDiffuse = Vec4(0.0, 0.0, 0.0, 1.0);
- Vec4 outSpecular = Vec4(0.0, 0.0, 0.0, 1.0);
- // Get worldPos
- const F32 depth = textureLod(u_msDepthRt, u_nearestAnyClampSampler, in_uv, 0.0).r;
- const Vec2 ndc = UV_TO_NDC(in_uv);
- 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);
- // Process decals
- if(cluster.m_decalsMask == ExtendedClusterObjectMask(0))
- {
- discard;
- }
- ANKI_LOOP while(cluster.m_decalsMask != ExtendedClusterObjectMask(0))
- {
- const I32 idx = findLSB2(cluster.m_decalsMask);
- cluster.m_decalsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
- const Decal decal = u_decals2[idx];
- // Project pos to decal space
- const Vec4 texCoords4 = decal.m_textureMatrix * Vec4(worldPos, 1.0);
- if(texCoords4.w <= 0.7)
- {
- // Behind the decal, skip
- continue;
- }
- Vec2 texCoords2 = texCoords4.xy / texCoords4.w;
- // Clamp the tex coords. Expect a border in the texture atlas
- texCoords2 = saturate(texCoords2);
- // Read diffuse
- const Vec2 diffUv = mad(texCoords2, decal.m_diffuseUv.zw, decal.m_diffuseUv.xy);
- const Vec4 decalDiff = texture(u_diffuseDecalTex, u_trilinearRepeatSampler, diffUv);
- // Read roughness
- const Vec2 specUv = mad(texCoords2, decal.m_normRoughnessUv.zw, decal.m_normRoughnessUv.xy);
- const Vec3 spec = texture(u_specularRoughnessDecalTex, u_trilinearRepeatSampler, specUv).rgb;
- // Update diffuse
- F32 f = decalDiff.a * decal.m_blendFactors[0];
- outDiffuse.rgb = outDiffuse.rgb * (1.0 - f) + decalDiff.rgb * f;
- outDiffuse.a *= (1.0 - f);
- // Update specular
- f = decalDiff.a * decal.m_blendFactors[1];
- outSpecular.rgb = outSpecular.rgb * (1.0 - f) + spec.rgb * f;
- outSpecular.a *= (1.0 - f);
- }
- out_color0 = outDiffuse;
- out_color1 = outSpecular;
- }
- #pragma anki end
|