Display.fs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #ifdef ADAPTIVE_SAMPLING
  2. #extension GL_ARB_shader_image_load_store : require
  3. #extension GL_ARB_shader_image_size : enable
  4. //! OpenGL image used for accumulating rendering result.
  5. volatile restrict layout(r32f) uniform image2D uRenderImage;
  6. //! OpenGL image storing variance of sampled pixels blocks.
  7. volatile restrict layout(r32i) uniform iimage2D uVarianceImage;
  8. //! Scale factor used to quantize visual error (float) into signed integer.
  9. uniform float uVarianceScaleFactor;
  10. //! Screen space tile size.
  11. uniform ivec2 uTileSize;
  12. #else // ADAPTIVE_SAMPLING
  13. //! Input image.
  14. uniform sampler2D uInputTexture;
  15. //! Ray tracing depth image.
  16. uniform sampler2D uDepthTexture;
  17. #endif // ADAPTIVE_SAMPLING
  18. //! Number of accumulated frames.
  19. uniform int uAccumFrames;
  20. //! Is debug mode enabled for importance screen sampling.
  21. uniform int uDebugAdaptive;
  22. //! Exposure value for tone mapping.
  23. uniform float uExposure;
  24. #ifdef TONE_MAPPING_FILMIC
  25. //! White point value for filmic tone mapping.
  26. uniform float uWhitePoint;
  27. #endif // TONE_MAPPING
  28. //! Output pixel color.
  29. out vec4 OutColor;
  30. //! RGB weight factors to calculate luminance.
  31. #define LUMA vec3 (0.2126f, 0.7152f, 0.0722f)
  32. // =======================================================================
  33. // function : ToneMappingFilmic
  34. // purpose :
  35. // =======================================================================
  36. vec4 ToneMappingFilmic(vec4 theColor, float theWhitePoint)
  37. {
  38. vec4 aPackColor = vec4 (theColor.rgb, theWhitePoint);
  39. vec4 aFilmicCurve = 1.425f * aPackColor + vec4 (0.05f);
  40. vec4 aResultColor = (aPackColor * aFilmicCurve + vec4 (0.004f)) / (aPackColor * (aFilmicCurve + vec4 (0.55f)) + vec4 (0.0491f)) - vec4 (0.0821f);
  41. return vec4 (aResultColor.rgb / aResultColor.www, 1.0);
  42. }
  43. // =======================================================================
  44. // function : main
  45. // purpose :
  46. // =======================================================================
  47. void main (void)
  48. {
  49. #ifndef ADAPTIVE_SAMPLING
  50. vec4 aColor = texelFetch (uInputTexture, ivec2 (gl_FragCoord.xy), 0);
  51. #ifdef PATH_TRACING
  52. float aDepth = aColor.w; // path tracing uses averaged depth
  53. #else
  54. float aDepth = texelFetch (uDepthTexture, ivec2 (gl_FragCoord.xy), 0).r;
  55. #endif
  56. gl_FragDepth = aDepth;
  57. #else // ADAPTIVE_SAMPLING
  58. ivec2 aPixel = ivec2 (gl_FragCoord.xy);
  59. vec4 aColor = vec4 (0.0);
  60. // fetch accumulated color and total number of samples
  61. aColor.x = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 0,
  62. 2 * aPixel.y + 0)).x;
  63. aColor.y = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 1,
  64. 2 * aPixel.y + 0)).x;
  65. aColor.z = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 1,
  66. 2 * aPixel.y + 1)).x;
  67. aColor.w = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 0,
  68. 2 * aPixel.y + 1)).x;
  69. // calculate normalization factor
  70. float aSampleWeight = 1.f / max (1.0, aColor.w);
  71. // calculate averaged depth value
  72. gl_FragDepth = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 2,
  73. 2 * aPixel.y + 1)).x * aSampleWeight;
  74. // calculate averaged radiance for all samples and even samples only
  75. float aHalfRad = imageLoad (uRenderImage, ivec2 (3 * aPixel.x + 2,
  76. 2 * aPixel.y + 0)).x * aSampleWeight * 2.f;
  77. float aAverRad = dot (aColor.rgb, LUMA) * aSampleWeight;
  78. // apply our 'tone mapping' operator (gamma correction and clamping)
  79. aHalfRad = min (1.f, sqrt (aHalfRad));
  80. aAverRad = min (1.f, sqrt (aAverRad));
  81. // calculate visual error
  82. float anError = (aAverRad - aHalfRad) * (aAverRad - aHalfRad);
  83. // accumulate visual error to current block; estimated error is written only
  84. // after the first 40 samples and path length has reached 10 bounces or more
  85. imageAtomicAdd (uVarianceImage, aPixel / uTileSize,
  86. int (mix (uVarianceScaleFactor, anError * uVarianceScaleFactor, aColor.w > 40.f)));
  87. if (uDebugAdaptive == 0) // normal rendering
  88. {
  89. aColor = vec4 (aColor.rgb * aSampleWeight, 1.0);
  90. }
  91. else // showing number of samples
  92. {
  93. vec2 aRatio = vec2 (1.f, 1.f);
  94. #ifdef GL_ARB_shader_image_size
  95. aRatio = vec2 (imageSize (uRenderImage)) / vec2 (3.f * 512.f, 2.f * 512.f);
  96. #endif
  97. aColor = vec4 (0.5f * aColor.rgb * aSampleWeight + vec3 (0.f, sqrt (aRatio.x * aRatio.y) * aColor.w / uAccumFrames * 0.35f, 0.f), 1.0);
  98. }
  99. #endif // ADAPTIVE_SAMPLING
  100. #ifdef PATH_TRACING
  101. aColor *= pow (2.0, uExposure);
  102. #ifdef TONE_MAPPING_FILMIC
  103. aColor = ToneMappingFilmic (aColor, uWhitePoint);
  104. #endif // TONE_MAPPING
  105. #ifdef THE_SHIFT_sRGB
  106. // apply gamma correction (we use gamma = 2)
  107. OutColor = vec4 (sqrt (aColor.rgb), 0.f);
  108. #else
  109. OutColor = vec4 (aColor.rgb, 0.f);
  110. #endif
  111. #else // not PATH_TRACING
  112. OutColor = aColor;
  113. #endif
  114. }