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

https://bitbucket.org/lindenlab/viewer-beta/ · GLSL · 116 lines · 73 code · 17 blank · 26 comment · 4 complexity · 8e71d9c02ed2067f1dd5a71d5bd3d374 MD5 · raw file

  1. /**
  2. * @file blurLightF.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 depthMap;
  30. uniform sampler2DRect normalMap;
  31. uniform sampler2DRect lightMap;
  32. uniform float dist_factor;
  33. uniform float blur_size;
  34. uniform vec2 delta;
  35. uniform vec3 kern[4];
  36. uniform float kern_scale;
  37. VARYING vec2 vary_fragcoord;
  38. uniform mat4 inv_proj;
  39. uniform vec2 screen_res;
  40. vec3 getKern(int i)
  41. {
  42. return kern[i];
  43. }
  44. vec4 getPosition(vec2 pos_screen)
  45. {
  46. float depth = texture2DRect(depthMap, pos_screen.xy).r;
  47. vec2 sc = pos_screen.xy*2.0;
  48. sc /= screen_res;
  49. sc -= vec2(1.0,1.0);
  50. vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
  51. vec4 pos = inv_proj * ndc;
  52. pos /= pos.w;
  53. pos.w = 1.0;
  54. return pos;
  55. }
  56. void main()
  57. {
  58. vec2 tc = vary_fragcoord.xy;
  59. vec3 norm = texture2DRect(normalMap, tc).xyz;
  60. norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
  61. vec3 pos = getPosition(tc).xyz;
  62. vec4 ccol = texture2DRect(lightMap, tc).rgba;
  63. vec2 dlt = kern_scale * delta / (1.0+norm.xy*norm.xy);
  64. dlt /= max(-pos.z*dist_factor, 1.0);
  65. vec2 defined_weight = getKern(0).xy; // special case the first (centre) sample's weight in the blur; we have to sample it anyway so we get it for 'free'
  66. vec4 col = defined_weight.xyxx * ccol;
  67. // relax tolerance according to distance to avoid speckling artifacts, as angles and distances are a lot more abrupt within a small screen area at larger distances
  68. float pointplanedist_tolerance_pow2 = pos.z*pos.z*0.00005;
  69. // perturb sampling origin slightly in screen-space to hide edge-ghosting artifacts where smoothing radius is quite large
  70. float tc_mod = 0.5*(tc.x + tc.y); // mod(tc.x+tc.y,2)
  71. tc_mod -= floor(tc_mod);
  72. tc_mod *= 2.0;
  73. tc += ( (tc_mod - 0.5) * getKern(1).z * dlt * 0.5 );
  74. for (int i = 1; i < 4; i++)
  75. {
  76. vec2 samptc = tc + getKern(i).z*dlt;
  77. vec3 samppos = getPosition(samptc).xyz;
  78. float d = dot(norm.xyz, samppos.xyz-pos.xyz);// dist from plane
  79. if (d*d <= pointplanedist_tolerance_pow2)
  80. {
  81. col += texture2DRect(lightMap, samptc)*getKern(i).xyxx;
  82. defined_weight += getKern(i).xy;
  83. }
  84. }
  85. for (int i = 1; i < 4; i++)
  86. {
  87. vec2 samptc = tc - getKern(i).z*dlt;
  88. vec3 samppos = getPosition(samptc).xyz;
  89. float d = dot(norm.xyz, samppos.xyz-pos.xyz);// dist from plane
  90. if (d*d <= pointplanedist_tolerance_pow2)
  91. {
  92. col += texture2DRect(lightMap, samptc)*getKern(i).xyxx;
  93. defined_weight += getKern(i).xy;
  94. }
  95. }
  96. col /= defined_weight.xyxx;
  97. col.y *= col.y;
  98. gl_FragColor = col;
  99. }