GBufferPost.ankiprog 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. ANKI_SPECIALIZATION_CONSTANT_UVEC2(TILE_COUNTS, 0u);
  6. ANKI_SPECIALIZATION_CONSTANT_U32(Z_SPLIT_COUNT, 2u);
  7. ANKI_SPECIALIZATION_CONSTANT_U32(TILE_SIZE, 3u);
  8. #pragma anki start vert
  9. #include <AnKi/Shaders/QuadVert.glsl>
  10. #pragma anki end
  11. #pragma anki start frag
  12. #include <AnKi/Shaders/PackFunctions.glsl>
  13. #include <AnKi/Shaders/Functions.glsl>
  14. layout(set = 0, binding = 0) uniform sampler u_nearestAnyClampSampler;
  15. layout(set = 0, binding = 1) uniform texture2D u_msDepthRt;
  16. layout(set = 0, binding = 2) uniform sampler u_trilinearRepeatSampler;
  17. #define CLUSTERED_SHADING_SET 0
  18. #define CLUSTERED_SHADING_UNIFORMS_BINDING 3
  19. #define CLUSTERED_SHADING_DECALS_BINDING 4
  20. #define CLUSTERED_SHADING_CLUSTERS_BINDING 7
  21. #include <AnKi/Shaders/ClusteredShadingCommon.glsl>
  22. layout(location = 0) in Vec2 in_uv;
  23. layout(location = 0) out Vec4 out_color0;
  24. layout(location = 1) out Vec4 out_color1;
  25. void main()
  26. {
  27. // This code blends the diffuse and the specular+rougness of the decals with GBuffer render targets.
  28. // Normaly the blending is being done ('D' is the decal diffuse and 'f' is decal blend factor):
  29. // d=gbuffer.diff
  30. // 1st decal: d'=d*(1-f)+D*f
  31. // 2nd decal: d''=d'*(1-f')+D'*f' <=> d''=d*(1-f)*(1-f')+D*f*(1-f')+D'*f'
  32. // By looking at the trend we will have to multiply the gbuffer.diff with: (1-f)*(1-f') ... (1-f'''')
  33. Vec4 outDiffuse = Vec4(0.0, 0.0, 0.0, 1.0);
  34. Vec4 outSpecular = Vec4(0.0, 0.0, 0.0, 1.0);
  35. // Get worldPos
  36. const F32 depth = textureLod(u_msDepthRt, u_nearestAnyClampSampler, in_uv, 0.0).r;
  37. const Vec2 ndc = UV_TO_NDC(in_uv);
  38. const Vec4 worldPos4 = u_clusteredShading.m_matrices.m_invertedViewProjectionJitter * Vec4(ndc, depth, 1.0);
  39. const Vec3 worldPos = worldPos4.xyz / worldPos4.w;
  40. // Get the cluster
  41. Cluster cluster = getClusterFragCoord(Vec3(gl_FragCoord.xy, depth), TILE_SIZE, TILE_COUNTS, Z_SPLIT_COUNT,
  42. u_clusteredShading.m_zSplitMagic.x, u_clusteredShading.m_zSplitMagic.y);
  43. // Process decals
  44. if(cluster.m_decalsMask == ExtendedClusterObjectMask(0))
  45. {
  46. discard;
  47. }
  48. ANKI_LOOP while(cluster.m_decalsMask != ExtendedClusterObjectMask(0))
  49. {
  50. const I32 idx = findLSB2(cluster.m_decalsMask);
  51. cluster.m_decalsMask &= ~(ExtendedClusterObjectMask(1) << ExtendedClusterObjectMask(idx));
  52. const Decal decal = u_decals2[idx];
  53. // Project pos to decal space
  54. const Vec4 texCoords4 = decal.m_textureMatrix * Vec4(worldPos, 1.0);
  55. if(texCoords4.w <= 0.7)
  56. {
  57. // Behind the decal, skip
  58. continue;
  59. }
  60. Vec2 texCoords2 = texCoords4.xy / texCoords4.w;
  61. // Clamp the tex coords. Expect a border in the texture atlas
  62. texCoords2 = saturate(texCoords2);
  63. // Read diffuse
  64. const Vec2 diffUv = mad(texCoords2, decal.m_diffuseUv.zw, decal.m_diffuseUv.xy);
  65. const Vec4 decalDiff = texture(u_diffuseDecalTex, u_trilinearRepeatSampler, diffUv);
  66. // Read roughness
  67. const Vec2 specUv = mad(texCoords2, decal.m_normRoughnessUv.zw, decal.m_normRoughnessUv.xy);
  68. const Vec3 spec = texture(u_specularRoughnessDecalTex, u_trilinearRepeatSampler, specUv).rgb;
  69. // Update diffuse
  70. F32 f = decalDiff.a * decal.m_blendFactors[0];
  71. outDiffuse.rgb = outDiffuse.rgb * (1.0 - f) + decalDiff.rgb * f;
  72. outDiffuse.a *= (1.0 - f);
  73. // Update specular
  74. f = decalDiff.a * decal.m_blendFactors[1];
  75. outSpecular.rgb = outSpecular.rgb * (1.0 - f) + spec.rgb * f;
  76. outSpecular.a *= (1.0 - f);
  77. }
  78. out_color0 = outDiffuse;
  79. out_color1 = outSpecular;
  80. }
  81. #pragma anki end