RaytraceSmooth.fs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //! Input ray-traced image.
  2. uniform sampler2D uFSAAInputTexture;
  3. //! Number of accumulated FSAA samples.
  4. uniform int uSamples;
  5. //! Output pixel color.
  6. out vec4 OutColor;
  7. #define LUM_DIFFERENCE 0.085f
  8. // =======================================================================
  9. // function : main
  10. // purpose :
  11. // =======================================================================
  12. void main (void)
  13. {
  14. #ifndef PATH_TRACING
  15. int aPixelX = int (gl_FragCoord.x);
  16. int aPixelY = int (gl_FragCoord.y);
  17. // Adjust FLIPTRI pattern used for adaptive FSAA
  18. float anOffsetX = mix (uFsaaOffset.x, -uFsaaOffset.x, float (aPixelX % 2));
  19. float anOffsetY = mix (uFsaaOffset.y, -uFsaaOffset.y, float (aPixelY % 2));
  20. vec4 aClr0 = texelFetch (uFSAAInputTexture, ivec2 (aPixelX + 0, aPixelY + 0), 0);
  21. vec4 aClr1 = texelFetch (uFSAAInputTexture, ivec2 (aPixelX + 0, aPixelY - 1), 0);
  22. vec4 aClr2 = texelFetch (uFSAAInputTexture, ivec2 (aPixelX + 0, aPixelY + 1), 0);
  23. vec4 aClr3 = texelFetch (uFSAAInputTexture, ivec2 (aPixelX + 1, aPixelY + 0), 0);
  24. vec4 aClr4 = texelFetch (uFSAAInputTexture, ivec2 (aPixelX + 1, aPixelY - 1), 0);
  25. vec4 aClr5 = texelFetch (uFSAAInputTexture, ivec2 (aPixelX + 1, aPixelY + 1), 0);
  26. vec4 aClr6 = texelFetch (uFSAAInputTexture, ivec2 (aPixelX - 1, aPixelY + 0), 0);
  27. vec4 aClr7 = texelFetch (uFSAAInputTexture, ivec2 (aPixelX - 1, aPixelY - 1), 0);
  28. vec4 aClr8 = texelFetch (uFSAAInputTexture, ivec2 (aPixelX - 1, aPixelY + 1), 0);
  29. float aLum = dot (LUMA, aClr0.xyz);
  30. bool aRender = abs (aClr1.w - aClr0.w) > LUM_DIFFERENCE ||
  31. abs (aClr2.w - aClr0.w) > LUM_DIFFERENCE ||
  32. abs (aClr3.w - aClr0.w) > LUM_DIFFERENCE ||
  33. abs (aClr4.w - aClr0.w) > LUM_DIFFERENCE ||
  34. abs (aClr5.w - aClr0.w) > LUM_DIFFERENCE ||
  35. abs (aClr6.w - aClr0.w) > LUM_DIFFERENCE ||
  36. abs (aClr7.w - aClr0.w) > LUM_DIFFERENCE ||
  37. abs (aClr8.w - aClr0.w) > LUM_DIFFERENCE;
  38. if (!aRender)
  39. {
  40. aRender = abs (dot (LUMA, aClr1.xyz) - aLum) > LUM_DIFFERENCE ||
  41. abs (dot (LUMA, aClr2.xyz) - aLum) > LUM_DIFFERENCE ||
  42. abs (dot (LUMA, aClr3.xyz) - aLum) > LUM_DIFFERENCE ||
  43. abs (dot (LUMA, aClr4.xyz) - aLum) > LUM_DIFFERENCE ||
  44. abs (dot (LUMA, aClr5.xyz) - aLum) > LUM_DIFFERENCE ||
  45. abs (dot (LUMA, aClr6.xyz) - aLum) > LUM_DIFFERENCE ||
  46. abs (dot (LUMA, aClr7.xyz) - aLum) > LUM_DIFFERENCE ||
  47. abs (dot (LUMA, aClr8.xyz) - aLum) > LUM_DIFFERENCE;
  48. }
  49. vec4 aColor = aClr0;
  50. if (aRender)
  51. {
  52. SRay aRay = GenerateRay (vPixel + vec2 (anOffsetX, anOffsetY));
  53. vec3 aInvDirect = 1.f / max (abs (aRay.Direct), SMALL);
  54. aInvDirect = vec3 (aRay.Direct.x < 0.f ? -aInvDirect.x : aInvDirect.x,
  55. aRay.Direct.y < 0.f ? -aInvDirect.y : aInvDirect.y,
  56. aRay.Direct.z < 0.f ? -aInvDirect.z : aInvDirect.z);
  57. aColor = mix (aClr0, clamp (Radiance (aRay, aInvDirect), 0.0, 1.0), 1.0 / float(uSamples));
  58. }
  59. OutColor = aColor;
  60. #endif
  61. }