PageRenderTime 37ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
text | 151 lines | 117 code | 34 blank | 0 comment | 0 complexity | ef76363e9b7e24aa8f81f120eb30e1f5 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. #extension GL_ARB_texture_multisample : enable
  27. uniform sampler2DMS diffuseRect;
  28. uniform sampler2DMS edgeMap;
  29. uniform sampler2DMS depthMap;
  30. uniform sampler2DMS normalMap;
  31. uniform sampler2D bloomMap;
  32. uniform float depth_cutoff;
  33. uniform float norm_cutoff;
  34. uniform float focal_distance;
  35. uniform float blur_constant;
  36. uniform float tan_pixel_angle;
  37. uniform float magnification;
  38. uniform mat4 inv_proj;
  39. uniform vec2 screen_res;
  40. varying vec2 vary_fragcoord;
  41. vec4 texture2DMS(sampler2DMS tex, ivec2 tc)
  42. {
  43. vec4 ret = vec4(0,0,0,0);
  44. for (int i = 0; i < samples; ++i)
  45. {
  46. ret += texelFetch(tex, tc, i);
  47. }
  48. return ret/samples;
  49. }
  50. float getDepth(ivec2 pos_screen)
  51. {
  52. float z = texture2DMS(depthMap, pos_screen.xy).r;
  53. z = z*2.0-1.0;
  54. vec4 ndc = vec4(0.0, 0.0, z, 1.0);
  55. vec4 p = inv_proj*ndc;
  56. return p.z/p.w;
  57. }
  58. float calc_cof(float depth)
  59. {
  60. float sc = abs(depth-focal_distance)/-depth*blur_constant;
  61. sc /= magnification;
  62. // tan_pixel_angle = pixel_length/-depth;
  63. float pixel_length = tan_pixel_angle*-focal_distance;
  64. sc = sc/pixel_length;
  65. sc *= 1.414;
  66. return sc;
  67. }
  68. void dofSample(inout vec4 diff, inout float w, float min_sc, float cur_depth, ivec2 tc)
  69. {
  70. float d = getDepth(tc);
  71. float sc = calc_cof(d);
  72. if (sc > min_sc //sampled pixel is more "out of focus" than current sample radius
  73. || d < cur_depth) //sampled pixel is further away than current pixel
  74. {
  75. float wg = 0.25;
  76. vec4 s = texture2DMS(diffuseRect, tc);
  77. // de-weight dull areas to make highlights 'pop'
  78. wg += s.r+s.g+s.b;
  79. diff += wg*s;
  80. w += wg;
  81. }
  82. }
  83. void main()
  84. {
  85. ivec2 itc = ivec2(vary_fragcoord.xy);
  86. vec3 norm = texture2DMS(normalMap, itc).xyz;
  87. norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
  88. float depth = getDepth(itc);
  89. vec4 diff = texture2DMS(diffuseRect, itc);
  90. {
  91. float w = 1.0;
  92. float sc = calc_cof(depth);
  93. sc = min(abs(sc), 10.0);
  94. float fd = depth*0.5f;
  95. float PI = 3.14159265358979323846264;
  96. int isc = int(sc);
  97. // sample quite uniformly spaced points within a circle, for a circular 'bokeh'
  98. //if (depth < focal_distance)
  99. {
  100. for (int x = -isc; x <= isc; x+=2)
  101. {
  102. for (int y = -isc; y <= isc; y+=2)
  103. {
  104. ivec2 cur_samp = ivec2(x,y);
  105. float cur_sc = length(vec2(cur_samp));
  106. if (cur_sc < sc)
  107. {
  108. dofSample(diff, w, cur_sc, depth, itc+cur_samp);
  109. }
  110. }
  111. }
  112. }
  113. diff /= w;
  114. }
  115. vec4 bloom = texture2D(bloomMap, vary_fragcoord.xy/screen_res);
  116. gl_FragColor = diff + bloom;
  117. }