PageRenderTime 36ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
text | 131 lines | 109 code | 22 blank | 0 comment | 0 complexity | 69bc7d35796a2ce293fad05db1d9b252 MD5 | raw file
Possible License(s): LGPL-2.1
  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. #extension GL_ARB_texture_multisample : enable
  27. uniform sampler2DMS depthMap;
  28. uniform sampler2DMS normalMap;
  29. uniform sampler2DRect lightMap;
  30. uniform float dist_factor;
  31. uniform float blur_size;
  32. uniform vec2 delta;
  33. uniform vec3 kern[4];
  34. uniform float kern_scale;
  35. varying vec2 vary_fragcoord;
  36. uniform mat4 inv_proj;
  37. uniform vec2 screen_res;
  38. vec3 texture2DMS3(sampler2DMS tex, ivec2 tc)
  39. {
  40. vec3 ret = vec3(0,0,0);
  41. for (int i = 0; i < samples; i++)
  42. {
  43. ret += texelFetch(tex, tc, i).rgb;
  44. }
  45. return ret/samples;
  46. }
  47. float texture2DMS1(sampler2DMS tex, ivec2 tc)
  48. {
  49. float ret = 0;
  50. for (int i = 0; i < samples; i++)
  51. {
  52. ret += texelFetch(tex, tc, i).r;
  53. }
  54. return ret/samples;
  55. }
  56. vec4 getPosition(ivec2 pos_screen)
  57. {
  58. float depth = texture2DMS1(depthMap, pos_screen.xy);
  59. vec2 sc = pos_screen.xy*2.0;
  60. sc /= screen_res;
  61. sc -= vec2(1.0,1.0);
  62. vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
  63. vec4 pos = inv_proj * ndc;
  64. pos /= pos.w;
  65. pos.w = 1.0;
  66. return pos;
  67. }
  68. void main()
  69. {
  70. vec2 tc = vary_fragcoord.xy;
  71. ivec2 itc = ivec2(tc);
  72. vec3 norm = texture2DMS3(normalMap, itc).xyz;
  73. norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
  74. vec3 pos = getPosition(itc).xyz;
  75. vec4 ccol = texture2DRect(lightMap, tc).rgba;
  76. vec2 dlt = kern_scale * delta / (1.0+norm.xy*norm.xy);
  77. dlt /= max(-pos.z*dist_factor, 1.0);
  78. vec2 defined_weight = kern[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'
  79. vec4 col = defined_weight.xyxx * ccol;
  80. // 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
  81. float pointplanedist_tolerance_pow2 = pos.z*pos.z*0.00005;
  82. // perturb sampling origin slightly in screen-space to hide edge-ghosting artifacts where smoothing radius is quite large
  83. tc += ( (mod(tc.x+tc.y,2) - 0.5) * kern[1].z * dlt * 0.5 );
  84. for (int i = 1; i < 4; i++)
  85. {
  86. vec2 samptc = tc + kern[i].z*dlt;
  87. vec3 samppos = getPosition(ivec2(samptc)).xyz;
  88. float d = dot(norm.xyz, samppos.xyz-pos.xyz);// dist from plane
  89. if (d*d <= pointplanedist_tolerance_pow2)
  90. {
  91. col += texture2DRect(lightMap, samptc)*kern[i].xyxx;
  92. defined_weight += kern[i].xy;
  93. }
  94. }
  95. for (int i = 1; i < 4; i++)
  96. {
  97. vec2 samptc = vec2(tc - kern[i].z*dlt);
  98. vec3 samppos = getPosition(ivec2(samptc)).xyz;
  99. float d = dot(norm.xyz, samppos.xyz-pos.xyz);// dist from plane
  100. if (d*d <= pointplanedist_tolerance_pow2)
  101. {
  102. col += texture2DRect(lightMap, samptc)*kern[i].xyxx;
  103. defined_weight += kern[i].xy;
  104. }
  105. }
  106. col /= defined_weight.xyxx;
  107. col.y *= col.y;
  108. gl_FragColor = col;
  109. }