DeclarationsImpl.glsl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //! @file DeclarationsImpl.glsl includes implementation of common functions and properties accessors
  2. #if defined(FRAGMENT_SHADER)
  3. #if defined(OCC_DEPTH_PEEL_OIT)
  4. uniform sampler2D occDepthPeelingDepth;
  5. uniform sampler2D occDepthPeelingFrontColor;
  6. int IsFrontPeelLayer = -1;
  7. bool occFragEarlyReturn()
  8. {
  9. #define THE_DEPTH_CLEAR_VALUE -1e15f
  10. ivec2 aFragCoord = ivec2 (gl_FragCoord.xy);
  11. vec2 aLastDepth = texelFetch (occDepthPeelingDepth, aFragCoord, 0).rg;
  12. occPeelFrontColor = texelFetch (occDepthPeelingFrontColor, aFragCoord, 0);
  13. occPeelDepth.rg = vec2 (THE_DEPTH_CLEAR_VALUE); // depth value always increases, so that MAX blend equation can be used
  14. occPeelBackColor = vec4 (0.0); // back color is blend after each peeling pass
  15. float aNearDepth = -aLastDepth.x;
  16. float aFarDepth = aLastDepth.y;
  17. float aFragDepth = gl_FragCoord.z; // 0 - 1
  18. if (aFragDepth < aNearDepth || aFragDepth > aFarDepth)
  19. {
  20. return true; // skip peeled depth
  21. }
  22. else if (aFragDepth > aNearDepth && aFragDepth < aFarDepth)
  23. {
  24. // to be rendered at next peeling pass
  25. occPeelDepth.rg = vec2 (-aFragDepth, aFragDepth);
  26. return true;
  27. }
  28. IsFrontPeelLayer = (gl_FragCoord.z == aNearDepth) ? 1 : 0;
  29. return false;
  30. }
  31. #else
  32. bool occFragEarlyReturn() { return false; }
  33. #endif
  34. void occSetFragColor (in vec4 theColor)
  35. {
  36. #if defined(OCC_ALPHA_TEST)
  37. if (theColor.a < occAlphaCutoff) discard;
  38. #endif
  39. #if defined(OCC_WRITE_WEIGHT_OIT_COVERAGE)
  40. float aWeight = theColor.a * clamp (1e+2 * pow (1.0 - gl_FragCoord.z * occOitDepthFactor, 3.0), 1e-2, 1e+2);
  41. occFragCoverage.r = theColor.a * aWeight;
  42. occFragColor = vec4 (theColor.rgb * theColor.a * aWeight, theColor.a);
  43. #elif defined(OCC_DEPTH_PEEL_OIT)
  44. if (IsFrontPeelLayer == 1) // front is blended directly
  45. {
  46. vec4 aLastColor = occPeelFrontColor;
  47. float anAlphaMult = 1.0 - aLastColor.a;
  48. occPeelFrontColor.rgb = aLastColor.rgb + theColor.rgb * theColor.a * anAlphaMult;
  49. occPeelFrontColor.a = 1.0 - anAlphaMult * (1.0 - theColor.a);
  50. }
  51. else if (IsFrontPeelLayer == 0) // back is blended afterwards
  52. {
  53. occPeelBackColor = theColor;
  54. }
  55. #else
  56. occFragColor = theColor;
  57. #endif
  58. }
  59. #endif
  60. #if defined(THE_MAX_LIGHTS) && (THE_MAX_LIGHTS > 0)
  61. // arrays of light sources
  62. uniform vec4 occLightSources[THE_MAX_LIGHTS * 4]; //!< packed light sources parameters
  63. uniform THE_PREC_ENUM int occLightSourcesTypes[THE_MAX_LIGHTS]; //!< packed light sources types
  64. #endif
  65. #if defined(THE_IS_PBR)
  66. vec3 occDiffIBLMap (in vec3 theNormal)
  67. {
  68. vec3 aSHCoeffs[9];
  69. for (int i = 0; i < 9; ++i)
  70. {
  71. aSHCoeffs[i] = occTexture2D (occDiffIBLMapSHCoeffs, vec2 ((float(i) + 0.5) / 9.0, 0.0)).rgb;
  72. }
  73. return aSHCoeffs[0]
  74. + aSHCoeffs[1] * theNormal.x
  75. + aSHCoeffs[2] * theNormal.y
  76. + aSHCoeffs[3] * theNormal.z
  77. + aSHCoeffs[4] * theNormal.x * theNormal.z
  78. + aSHCoeffs[5] * theNormal.y * theNormal.z
  79. + aSHCoeffs[6] * theNormal.x * theNormal.y
  80. + aSHCoeffs[7] * (3.0 * theNormal.z * theNormal.z - 1.0)
  81. + aSHCoeffs[8] * (theNormal.x * theNormal.x - theNormal.y * theNormal.y);
  82. }
  83. #endif
  84. // front and back material properties accessors
  85. #if defined(THE_IS_PBR)
  86. uniform vec4 occPbrMaterial[3 * 2];
  87. #define MIN_ROUGHNESS 0.01
  88. float occRoughness (in float theNormalizedRoughness) { return theNormalizedRoughness * (1.0 - MIN_ROUGHNESS) + MIN_ROUGHNESS; }
  89. vec4 occPBRMaterial_Color(in bool theIsFront) { return theIsFront ? occPbrMaterial[0] : occPbrMaterial[3]; }
  90. vec3 occPBRMaterial_Emission(in bool theIsFront) { return theIsFront ? occPbrMaterial[1].rgb : occPbrMaterial[4].rgb; }
  91. float occPBRMaterial_IOR(in bool theIsFront) { return theIsFront ? occPbrMaterial[1].w : occPbrMaterial[4].w; }
  92. float occPBRMaterial_Metallic(in bool theIsFront) { return theIsFront ? occPbrMaterial[2].b : occPbrMaterial[5].b; }
  93. float occPBRMaterial_NormalizedRoughness(in bool theIsFront) { return theIsFront ? occPbrMaterial[2].g : occPbrMaterial[5].g; }
  94. #else
  95. uniform vec4 occCommonMaterial[4 * 2];
  96. vec4 occMaterial_Diffuse(in bool theIsFront) { return theIsFront ? occCommonMaterial[0] : occCommonMaterial[4]; }
  97. vec3 occMaterial_Emission(in bool theIsFront) { return theIsFront ? occCommonMaterial[1].rgb : occCommonMaterial[5].rgb; }
  98. vec3 occMaterial_Specular(in bool theIsFront) { return theIsFront ? occCommonMaterial[2].rgb : occCommonMaterial[6].rgb; }
  99. float occMaterial_Shininess(in bool theIsFront) { return theIsFront ? occCommonMaterial[2].a : occCommonMaterial[6].a; }
  100. vec3 occMaterial_Ambient(in bool theIsFront) { return theIsFront ? occCommonMaterial[3].rgb : occCommonMaterial[7].rgb; }
  101. #endif
  102. // 2D texture coordinates transformation
  103. vec2 occTextureTrsf_Translation(void) { return occTexTrsf2d[0].xy; }
  104. vec2 occTextureTrsf_Scale(void) { return occTexTrsf2d[0].zw; }
  105. float occTextureTrsf_RotationSin(void) { return occTexTrsf2d[1].x; }
  106. float occTextureTrsf_RotationCos(void) { return occTexTrsf2d[1].y; }
  107. //! @endfile DeclarationsImpl.glsl