PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/indra/newview/app_settings/shaders/class2/deferred/sunLightMSF.glsl

https://bitbucket.org/lindenlab/viewer-beta/
text | 220 lines | 178 code | 42 blank | 0 comment | 0 complexity | e409d131e1de48e45c224bbd35067f19 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file sunLightMSF.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. //class 2, shadows, no SSAO
  28. uniform sampler2DMS depthMap;
  29. uniform sampler2DMS normalMap;
  30. uniform sampler2DRectShadow shadowMap0;
  31. uniform sampler2DRectShadow shadowMap1;
  32. uniform sampler2DRectShadow shadowMap2;
  33. uniform sampler2DRectShadow shadowMap3;
  34. uniform sampler2DShadow shadowMap4;
  35. uniform sampler2DShadow shadowMap5;
  36. // Inputs
  37. uniform mat4 shadow_matrix[6];
  38. uniform vec4 shadow_clip;
  39. uniform float ssao_radius;
  40. uniform float ssao_max_radius;
  41. uniform float ssao_factor;
  42. uniform float ssao_factor_inv;
  43. varying vec2 vary_fragcoord;
  44. varying vec4 vary_light;
  45. uniform mat4 inv_proj;
  46. uniform vec2 screen_res;
  47. uniform vec2 shadow_res;
  48. uniform vec2 proj_shadow_res;
  49. uniform float shadow_bias;
  50. uniform float shadow_offset;
  51. uniform float spot_shadow_bias;
  52. uniform float spot_shadow_offset;
  53. vec4 getPosition(ivec2 pos_screen, int sample)
  54. {
  55. float depth = texelFetch(depthMap, pos_screen.xy, sample).r;
  56. vec2 sc = vec2(pos_screen.xy)*2.0;
  57. sc /= screen_res;
  58. sc -= vec2(1.0,1.0);
  59. vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
  60. vec4 pos = inv_proj * ndc;
  61. pos /= pos.w;
  62. pos.w = 1.0;
  63. return pos;
  64. }
  65. float pcfShadow(sampler2DRectShadow shadowMap, vec4 stc, float scl)
  66. {
  67. stc.xyz /= stc.w;
  68. stc.z += shadow_bias*scl;
  69. float cs = shadow2DRect(shadowMap, stc.xyz).x;
  70. float shadow = cs;
  71. shadow += max(shadow2DRect(shadowMap, stc.xyz+vec3(1.5, 1.5, 0.0)).x, cs);
  72. shadow += max(shadow2DRect(shadowMap, stc.xyz+vec3(1.5, -1.5, 0.0)).x, cs);
  73. shadow += max(shadow2DRect(shadowMap, stc.xyz+vec3(-1.5, 1.5, 0.0)).x, cs);
  74. shadow += max(shadow2DRect(shadowMap, stc.xyz+vec3(-1.5, -1.5, 0.0)).x, cs);
  75. return shadow/5.0;
  76. //return shadow;
  77. }
  78. float pcfShadow(sampler2DShadow shadowMap, vec4 stc, float scl)
  79. {
  80. stc.xyz /= stc.w;
  81. stc.z += spot_shadow_bias*scl;
  82. float cs = shadow2D(shadowMap, stc.xyz).x;
  83. float shadow = cs;
  84. vec2 off = 1.5/proj_shadow_res;
  85. shadow += max(shadow2D(shadowMap, stc.xyz+vec3(off.x, off.y, 0.0)).x, cs);
  86. shadow += max(shadow2D(shadowMap, stc.xyz+vec3(off.x, -off.y, 0.0)).x, cs);
  87. shadow += max(shadow2D(shadowMap, stc.xyz+vec3(-off.x, off.y, 0.0)).x, cs);
  88. shadow += max(shadow2D(shadowMap, stc.xyz+vec3(-off.x, -off.y, 0.0)).x, cs);
  89. return shadow/5.0;
  90. //return shadow;
  91. }
  92. void main()
  93. {
  94. vec2 pos_screen = vary_fragcoord.xy;
  95. ivec2 itc = ivec2(pos_screen);
  96. //try doing an unproject here
  97. vec4 fcol = vec4(0,0,0,0);
  98. for (int i = 0; i < samples; i++)
  99. {
  100. vec4 pos = getPosition(itc, i);
  101. vec4 nmap4 = texelFetch(normalMap, itc, i);
  102. nmap4 = vec4((nmap4.xy-0.5)*2.0,nmap4.z,nmap4.w); // unpack norm
  103. float displace = nmap4.w;
  104. vec3 norm = nmap4.xyz;
  105. /*if (pos.z == 0.0) // do nothing for sky *FIX: REMOVE THIS IF/WHEN THE POSITION MAP IS BEING USED AS A STENCIL
  106. {
  107. gl_FragColor = vec4(0.0); // doesn't matter
  108. return;
  109. }*/
  110. float shadow = 1.0;
  111. float dp_directional_light = max(0.0, dot(norm, vary_light.xyz));
  112. vec3 shadow_pos = pos.xyz + displace*norm;
  113. vec3 offset = vary_light.xyz * (1.0-dp_directional_light);
  114. vec4 spos = vec4(shadow_pos+offset*shadow_offset, 1.0);
  115. if (spos.z > -shadow_clip.w)
  116. {
  117. if (dp_directional_light == 0.0)
  118. {
  119. // if we know this point is facing away from the sun then we know it's in shadow without having to do a squirrelly shadow-map lookup
  120. shadow = 0.0;
  121. }
  122. else
  123. {
  124. vec4 lpos;
  125. if (spos.z < -shadow_clip.z)
  126. {
  127. lpos = shadow_matrix[3]*spos;
  128. lpos.xy *= shadow_res;
  129. shadow = pcfShadow(shadowMap3, lpos, 0.25);
  130. shadow += max((pos.z+shadow_clip.z)/(shadow_clip.z-shadow_clip.w)*2.0-1.0, 0.0);
  131. }
  132. else if (spos.z < -shadow_clip.y)
  133. {
  134. lpos = shadow_matrix[2]*spos;
  135. lpos.xy *= shadow_res;
  136. shadow = pcfShadow(shadowMap2, lpos, 0.5);
  137. }
  138. else if (spos.z < -shadow_clip.x)
  139. {
  140. lpos = shadow_matrix[1]*spos;
  141. lpos.xy *= shadow_res;
  142. shadow = pcfShadow(shadowMap1, lpos, 0.75);
  143. }
  144. else
  145. {
  146. lpos = shadow_matrix[0]*spos;
  147. lpos.xy *= shadow_res;
  148. shadow = pcfShadow(shadowMap0, lpos, 1.0);
  149. }
  150. // take the most-shadowed value out of these two:
  151. // * the blurred sun shadow in the light (shadow) map
  152. // * an unblurred dot product between the sun and this norm
  153. // the goal is to err on the side of most-shadow to fill-in shadow holes and reduce artifacting
  154. shadow = min(shadow, dp_directional_light);
  155. //lpos.xy /= lpos.w*32.0;
  156. //if (fract(lpos.x) < 0.1 || fract(lpos.y) < 0.1)
  157. //{
  158. // shadow = 0.0;
  159. //}
  160. }
  161. }
  162. else
  163. {
  164. // more distant than the shadow map covers
  165. shadow = 1.0;
  166. }
  167. fcol[0] += shadow;
  168. fcol[1] += 1.0;
  169. spos = vec4(shadow_pos+norm*spot_shadow_offset, 1.0);
  170. //spotlight shadow 1
  171. vec4 lpos = shadow_matrix[4]*spos;
  172. fcol[2] += pcfShadow(shadowMap4, lpos, 0.8);
  173. //spotlight shadow 2
  174. lpos = shadow_matrix[5]*spos;
  175. fcol[3] += pcfShadow(shadowMap5, lpos, 0.8);
  176. }
  177. gl_FragColor = fcol/samples;
  178. }