ForwardShadingCommon.hlsl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Shaders/Functions.hlsl>
  7. #include <AnKi/Shaders/Include/MeshTypes.h>
  8. #include <AnKi/Shaders/Include/MaterialTypes.h>
  9. #include <AnKi/Shaders/Include/GpuSceneTypes.h>
  10. #define FORWARD_SHADING 1
  11. #include <AnKi/Shaders/MaterialShadersCommon.hlsl>
  12. //
  13. // Frag
  14. //
  15. #if ANKI_PIXEL_SHADER
  16. struct PixelOut
  17. {
  18. Vec4 m_color : SV_TARGET0;
  19. };
  20. void packGBuffer(Vec4 color, out PixelOut output)
  21. {
  22. output.m_color = Vec4(color.rgb, color.a);
  23. }
  24. Vec4 readAnimatedTextureRgba(Texture2DArray<Vec4> tex, SamplerState sampl, F32 period, Vec2 uv, F32 time)
  25. {
  26. Vec2 texSize;
  27. F32 layerCount;
  28. F32 mipCount;
  29. tex.GetDimensions(0, texSize.x, texSize.y, layerCount, mipCount);
  30. const F32 layer = fmod(time * layerCount / period, layerCount);
  31. return tex.Sample(sampl, Vec3(uv, layer));
  32. }
  33. // Iterate the clusters to compute the light color
  34. Vec3 computeLightColorHigh(Vec3 diffCol, Vec3 worldPos, Vec4 svPosition)
  35. {
  36. diffCol = diffuseLobe(diffCol);
  37. Vec3 outColor = Vec3(0.0, 0.0, 0.0);
  38. // Find the cluster and then the light counts
  39. Cluster cluster = getClusterFragCoord(g_clusters, g_globalRendererConstants, svPosition.xyz);
  40. // Point lights
  41. U32 idx = 0;
  42. [loop] while((idx = iteratePointLights(cluster)) != kMaxU32)
  43. {
  44. const PointLight light = g_pointLights[idx];
  45. const Vec3 diffC = diffCol * light.m_diffuseColor;
  46. const Vec3 frag2Light = light.m_position - worldPos;
  47. const F32 att = computeAttenuationFactor<F32>(light.m_radius, frag2Light);
  48. F32 shadow = 1.0;
  49. if(light.m_shadowAtlasTileScale >= 0.0)
  50. {
  51. shadow = computeShadowFactorPointLight<F32>(light, frag2Light, g_shadowAtlasTex, g_shadowSampler);
  52. }
  53. outColor += diffC * (att * shadow);
  54. }
  55. // Spot lights
  56. [loop] while((idx = iterateSpotLights(cluster)) != kMaxU32)
  57. {
  58. const SpotLight light = g_spotLights[idx];
  59. const Vec3 diffC = diffCol * light.m_diffuseColor;
  60. const Vec3 frag2Light = light.m_position - worldPos;
  61. const F32 att = computeAttenuationFactor<F32>(light.m_radius, frag2Light);
  62. const Vec3 l = normalize(frag2Light);
  63. const F32 spot = computeSpotFactor<F32>(l, light.m_outerCos, light.m_innerCos, light.m_direction);
  64. F32 shadow = 1.0;
  65. [branch] if(light.m_shadow != 0u)
  66. {
  67. shadow = computeShadowFactorSpotLight<F32>(light, worldPos, g_shadowAtlasTex, g_shadowSampler);
  68. }
  69. outColor += diffC * (att * spot * shadow);
  70. }
  71. return outColor;
  72. }
  73. // Just read the light color from the vol texture
  74. Vec3 computeLightColorLow(Vec3 diffCol, Vec3 worldPos, Vec4 svPosition)
  75. {
  76. ANKI_MAYBE_UNUSED(worldPos);
  77. const Vec2 uv = svPosition.xy / g_globalRendererConstants.m_renderingSize;
  78. const F32 linearDepth = linearizeDepth(svPosition.z, g_globalRendererConstants.m_matrices.m_near, g_globalRendererConstants.m_matrices.m_far);
  79. const F32 w = linearDepth * (F32(g_globalRendererConstants.m_zSplitCount) / F32(g_globalRendererConstants.m_lightVolumeLastZSplit + 1u));
  80. const Vec3 uvw = Vec3(uv, w);
  81. const Vec3 light = g_lightVol.SampleLevel(g_linearAnyClampSampler, uvw, 0.0).rgb;
  82. return diffuseLobe(diffCol) * light;
  83. }
  84. void particleAlpha(Vec4 color, Vec4 scaleColor, Vec4 biasColor, out PixelOut output)
  85. {
  86. packGBuffer(color * scaleColor + biasColor, output);
  87. }
  88. void fog(Vec3 color, F32 fogAlphaScale, F32 fogDistanceOfMaxThikness, F32 zVSpace, Vec2 svPosition, out PixelOut output)
  89. {
  90. const Vec2 screenSize = 1.0 / g_globalRendererConstants.m_renderingSize;
  91. const Vec2 texCoords = svPosition * screenSize;
  92. const F32 depth = g_gbufferDepthTex.Sample(g_linearAnyClampSampler, texCoords, 0.0).r;
  93. F32 zFeatherFactor;
  94. const Vec4 fragPosVspace4 = mul(g_globalRendererConstants.m_matrices.m_invertedProjectionJitter, Vec4(Vec3(uvToNdc(texCoords), depth), 1.0));
  95. const F32 sceneZVspace = fragPosVspace4.z / fragPosVspace4.w;
  96. const F32 diff = max(0.0, zVSpace - sceneZVspace);
  97. zFeatherFactor = min(1.0, diff / fogDistanceOfMaxThikness);
  98. packGBuffer(Vec4(color, zFeatherFactor * fogAlphaScale), output);
  99. }
  100. #endif // ANKI_PIXEL_SHADER