PageRenderTime 30ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/app_settings/shaders/class1/deferred/postDeferredF.glsl

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 126 lines | 102 code | 24 blank | 0 comment | 0 complexity | 63adcb35d96ce4be87e5bffdd076a973 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file postDeferredF.glsl
  3. *
  4. * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  5. * Second Life Viewer Source Code
  6. * Copyright (C) 2007, Linden Research, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License only.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  23. * $/LicenseInfo$
  24. */
  25. #extension GL_ARB_texture_rectangle : enable
  26. #ifdef DEFINE_GL_FRAGCOLOR
  27. out vec4 gl_FragColor;
  28. #endif
  29. uniform sampler2DRect diffuseRect;
  30. uniform mat4 inv_proj;
  31. uniform vec2 screen_res;
  32. uniform float max_cof;
  33. uniform float res_scale;
  34. VARYING vec2 vary_fragcoord;
  35. void dofSample(inout vec4 diff, inout float w, float min_sc, vec2 tc)
  36. {
  37. vec4 s = texture2DRect(diffuseRect, tc);
  38. float sc = abs(s.a*2.0-1.0)*max_cof;
  39. if (sc > min_sc) //sampled pixel is more "out of focus" than current sample radius
  40. {
  41. float wg = 0.25;
  42. // de-weight dull areas to make highlights 'pop'
  43. wg += s.r+s.g+s.b;
  44. diff += wg*s;
  45. w += wg;
  46. }
  47. }
  48. void dofSampleNear(inout vec4 diff, inout float w, float min_sc, vec2 tc)
  49. {
  50. vec4 s = texture2DRect(diffuseRect, tc);
  51. float wg = 0.25;
  52. // de-weight dull areas to make highlights 'pop'
  53. wg += s.r+s.g+s.b;
  54. diff += wg*s;
  55. w += wg;
  56. }
  57. void main()
  58. {
  59. vec2 tc = vary_fragcoord.xy;
  60. vec4 diff = texture2DRect(diffuseRect, vary_fragcoord.xy);
  61. {
  62. float w = 1.0;
  63. float sc = (diff.a*2.0-1.0)*max_cof;
  64. float PI = 3.14159265358979323846264;
  65. // sample quite uniformly spaced points within a circle, for a circular 'bokeh'
  66. if (sc > 0.5)
  67. {
  68. while (sc > 0.5)
  69. {
  70. int its = int(max(1.0,(sc*3.7)));
  71. for (int i=0; i<its; ++i)
  72. {
  73. float ang = sc+i*2*PI/its; // sc is added for rotary perturbance
  74. float samp_x = sc*sin(ang);
  75. float samp_y = sc*cos(ang);
  76. // you could test sample coords against an interesting non-circular aperture shape here, if desired.
  77. dofSampleNear(diff, w, sc, vary_fragcoord.xy + vec2(samp_x,samp_y));
  78. }
  79. sc -= 1.0;
  80. }
  81. }
  82. else if (sc < -0.5)
  83. {
  84. sc = abs(sc);
  85. while (sc > 0.5)
  86. {
  87. int its = int(max(1.0,(sc*3.7)));
  88. for (int i=0; i<its; ++i)
  89. {
  90. float ang = sc+i*2*PI/its; // sc is added for rotary perturbance
  91. float samp_x = sc*sin(ang);
  92. float samp_y = sc*cos(ang);
  93. // you could test sample coords against an interesting non-circular aperture shape here, if desired.
  94. dofSample(diff, w, sc, vary_fragcoord.xy + vec2(samp_x,samp_y));
  95. }
  96. sc -= 1.0;
  97. }
  98. }
  99. diff /= w;
  100. }
  101. gl_FragColor = diff;
  102. }