PageRenderTime 70ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
text | 126 lines | 105 code | 21 blank | 0 comment | 0 complexity | 06d1b2750a25e87a033de9cc8c3b928e MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file pointLightF.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 diffuseRect;
  29. uniform sampler2DMS specularRect;
  30. uniform sampler2DMS normalMap;
  31. uniform sampler2D noiseMap;
  32. uniform sampler2D lightFunc;
  33. uniform vec3 env_mat[3];
  34. uniform float sun_wash;
  35. varying vec4 vary_light;
  36. varying vec4 vary_fragcoord;
  37. uniform vec2 screen_res;
  38. uniform mat4 inv_proj;
  39. uniform vec4 viewport;
  40. vec4 getPosition(ivec2 pos_screen, int sample)
  41. {
  42. float depth = texelFetch(depthMap, pos_screen, sample).r;
  43. vec2 sc = (vec2(pos_screen.xy)-viewport.xy)*2.0;
  44. sc /= viewport.zw;
  45. sc -= vec2(1.0,1.0);
  46. vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
  47. vec4 pos = inv_proj * ndc;
  48. pos /= pos.w;
  49. pos.w = 1.0;
  50. return pos;
  51. }
  52. void main()
  53. {
  54. vec4 frag = vary_fragcoord;
  55. frag.xyz /= frag.w;
  56. frag.xyz = frag.xyz*0.5+0.5;
  57. frag.xy *= screen_res;
  58. ivec2 itc = ivec2(frag.xy);
  59. int wght = 0;
  60. vec3 fcol = vec3(0,0,0);
  61. for (int s = 0; s < samples; ++s)
  62. {
  63. vec3 pos = getPosition(itc, s).xyz;
  64. vec3 lv = vary_light.xyz-pos;
  65. float dist2 = dot(lv,lv);
  66. dist2 /= vary_light.w;
  67. if (dist2 <= 1.0)
  68. {
  69. vec3 norm = texelFetch(normalMap, itc, s).xyz;
  70. norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
  71. float da = dot(norm, lv);
  72. if (da >= 0.0)
  73. {
  74. norm = normalize(norm);
  75. lv = normalize(lv);
  76. da = dot(norm, lv);
  77. float noise = texture2D(noiseMap, frag.xy/128.0).b;
  78. vec3 col = texelFetch(diffuseRect, itc, s).rgb;
  79. float fa = gl_Color.a+1.0;
  80. float dist_atten = clamp(1.0-(dist2-1.0*(1.0-fa))/fa, 0.0, 1.0);
  81. float lit = da * dist_atten * noise;
  82. col = gl_Color.rgb*lit*col;
  83. vec4 spec = texelFetch(specularRect, itc, s);
  84. if (spec.a > 0.0)
  85. {
  86. float sa = dot(normalize(lv-normalize(pos)),norm);
  87. if (sa > 0.0)
  88. {
  89. sa = texture2D(lightFunc, vec2(sa, spec.a)).a * min(dist_atten*4.0, 1.0);
  90. sa *= noise;
  91. col += da*sa*gl_Color.rgb*spec.rgb;
  92. }
  93. }
  94. fcol += col;
  95. ++wght;
  96. }
  97. }
  98. }
  99. if (wght <= 0)
  100. {
  101. discard;
  102. }
  103. gl_FragColor.rgb = fcol/samples;
  104. gl_FragColor.a = 0.0;
  105. }