PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

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