PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
text | 155 lines | 124 code | 31 blank | 0 comment | 0 complexity | ba416baeb4daadc0ec1ff9613e09cff7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file multiPointLightF.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. uniform int light_count;
  36. #define MAX_LIGHT_COUNT 16
  37. uniform vec4 light[MAX_LIGHT_COUNT];
  38. uniform vec4 light_col[MAX_LIGHT_COUNT];
  39. varying vec4 vary_fragcoord;
  40. uniform vec2 screen_res;
  41. uniform float far_z;
  42. uniform mat4 inv_proj;
  43. vec4 getPosition(ivec2 pos_screen, int sample)
  44. {
  45. float depth = texelFetch(depthMap, pos_screen, sample).r;
  46. vec2 sc = vec2(pos_screen.xy)*2.0;
  47. sc /= screen_res;
  48. sc -= vec2(1.0,1.0);
  49. vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
  50. vec4 pos = inv_proj * ndc;
  51. pos /= pos.w;
  52. pos.w = 1.0;
  53. return pos;
  54. }
  55. void main()
  56. {
  57. vec2 frag = (vary_fragcoord.xy*0.5+0.5)*screen_res;
  58. ivec2 itc = ivec2(frag);
  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. if (pos.z >= far_z)
  65. {
  66. vec3 norm = texelFetch(normalMap, itc, s).xyz;
  67. norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
  68. norm = normalize(norm);
  69. vec4 spec = texelFetch(specularRect, itc, s);
  70. vec3 diff = texelFetch(diffuseRect, itc, s).rgb;
  71. float noise = texture2D(noiseMap, frag.xy/128.0).b;
  72. vec3 out_col = vec3(0,0,0);
  73. vec3 npos = normalize(-pos);
  74. // As of OSX 10.6.7 ATI Apple's crash when using a variable size loop
  75. for (int i = 0; i < MAX_LIGHT_COUNT; ++i)
  76. {
  77. bool light_contrib = (i < light_count);
  78. vec3 lv = light[i].xyz-pos;
  79. float dist2 = dot(lv,lv);
  80. dist2 /= light[i].w;
  81. if (dist2 > 1.0)
  82. {
  83. light_contrib = false;
  84. }
  85. float da = dot(norm, lv);
  86. if (da < 0.0)
  87. {
  88. light_contrib = false;
  89. }
  90. if (light_contrib)
  91. {
  92. lv = normalize(lv);
  93. da = dot(norm, lv);
  94. float fa = light_col[i].a+1.0;
  95. float dist_atten = clamp(1.0-(dist2-1.0*(1.0-fa))/fa, 0.0, 1.0);
  96. dist_atten *= noise;
  97. float lit = da * dist_atten;
  98. vec3 col = light_col[i].rgb*lit*diff;
  99. //vec3 col = vec3(dist2, light_col[i].a, lit);
  100. if (spec.a > 0.0)
  101. {
  102. //vec3 ref = dot(pos+lv, norm);
  103. float sa = dot(normalize(lv+npos),norm);
  104. if (sa > 0.0)
  105. {
  106. sa = texture2D(lightFunc,vec2(sa, spec.a)).a * min(dist_atten*4.0, 1.0);
  107. sa *= noise;
  108. col += da*sa*light_col[i].rgb*spec.rgb;
  109. }
  110. }
  111. out_col += col;
  112. }
  113. }
  114. fcol += out_col;
  115. ++wght;
  116. }
  117. }
  118. if (wght <= 0)
  119. {
  120. discard;
  121. }
  122. gl_FragColor.rgb = fcol/samples;
  123. gl_FragColor.a = 0.0;
  124. }