PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
text | 258 lines | 206 code | 52 blank | 0 comment | 0 complexity | c9ceb804a7aaccf3b19fe840b4ff63e9 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file sunLightSSAOF.glsl
  3. * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  4. * Second Life Viewer Source Code
  5. * Copyright (C) 2007, Linden Research, Inc.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation;
  10. * version 2.1 of the License only.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  22. * $/LicenseInfo$
  23. */
  24. #extension GL_ARB_texture_rectangle : enable
  25. #extension GL_ARB_texture_multisample : enable
  26. //class 2 -- shadows and SSAO
  27. uniform sampler2DMS depthMap;
  28. uniform sampler2DMS normalMap;
  29. uniform sampler2DRectShadow shadowMap0;
  30. uniform sampler2DRectShadow shadowMap1;
  31. uniform sampler2DRectShadow shadowMap2;
  32. uniform sampler2DRectShadow shadowMap3;
  33. uniform sampler2DShadow shadowMap4;
  34. uniform sampler2DShadow shadowMap5;
  35. uniform sampler2D noiseMap;
  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, 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. //calculate decreases in ambient lighting when crowded out (SSAO)
  66. float calcAmbientOcclusion(vec4 pos, vec3 norm, int sample)
  67. {
  68. float ret = 1.0;
  69. vec2 kern[8];
  70. // exponentially (^2) distant occlusion samples spread around origin
  71. kern[0] = vec2(-1.0, 0.0) * 0.125*0.125;
  72. kern[1] = vec2(1.0, 0.0) * 0.250*0.250;
  73. kern[2] = vec2(0.0, 1.0) * 0.375*0.375;
  74. kern[3] = vec2(0.0, -1.0) * 0.500*0.500;
  75. kern[4] = vec2(0.7071, 0.7071) * 0.625*0.625;
  76. kern[5] = vec2(-0.7071, -0.7071) * 0.750*0.750;
  77. kern[6] = vec2(-0.7071, 0.7071) * 0.875*0.875;
  78. kern[7] = vec2(0.7071, -0.7071) * 1.000*1.000;
  79. vec2 pos_screen = vary_fragcoord.xy;
  80. vec3 pos_world = pos.xyz;
  81. vec2 noise_reflect = texture2D(noiseMap, vary_fragcoord.xy/128.0).xy;
  82. float angle_hidden = 0.0;
  83. int points = 0;
  84. float scale = min(ssao_radius / -pos_world.z, ssao_max_radius);
  85. // it was found that keeping # of samples a constant was the fastest, probably due to compiler optimizations (unrolling?)
  86. for (int i = 0; i < 8; i++)
  87. {
  88. ivec2 samppos_screen = ivec2(pos_screen + scale * reflect(kern[i], noise_reflect));
  89. vec3 samppos_world = getPosition(samppos_screen, sample).xyz;
  90. vec3 diff = pos_world - samppos_world;
  91. float dist2 = dot(diff, diff);
  92. // assume each sample corresponds to an occluding sphere with constant radius, constant x-sectional area
  93. // --> solid angle shrinking by the square of distance
  94. //radius is somewhat arbitrary, can approx with just some constant k * 1 / dist^2
  95. //(k should vary inversely with # of samples, but this is taken care of later)
  96. angle_hidden = angle_hidden + float(dot((samppos_world - 0.05*norm - pos_world), norm) > 0.0) * min(1.0/dist2, ssao_factor_inv);
  97. // 'blocked' samples (significantly closer to camera relative to pos_world) are "no data", not "no occlusion"
  98. points = points + int(diff.z > -1.0);
  99. }
  100. angle_hidden = min(ssao_factor*angle_hidden/float(points), 1.0);
  101. ret = (1.0 - (float(points != 0) * angle_hidden));
  102. return min(ret, 1.0);
  103. }
  104. float pcfShadow(sampler2DRectShadow shadowMap, vec4 stc, float scl)
  105. {
  106. stc.xyz /= stc.w;
  107. stc.z += shadow_bias*scl;
  108. float cs = shadow2DRect(shadowMap, stc.xyz).x;
  109. float shadow = cs;
  110. shadow += max(shadow2DRect(shadowMap, stc.xyz+vec3(1.5, 1.5, 0.0)).x, cs);
  111. shadow += max(shadow2DRect(shadowMap, stc.xyz+vec3(1.5, -1.5, 0.0)).x, cs);
  112. shadow += max(shadow2DRect(shadowMap, stc.xyz+vec3(-1.5, 1.5, 0.0)).x, cs);
  113. shadow += max(shadow2DRect(shadowMap, stc.xyz+vec3(-1.5, -1.5, 0.0)).x, cs);
  114. return shadow/5.0;
  115. //return shadow;
  116. }
  117. float pcfShadow(sampler2DShadow shadowMap, vec4 stc, float scl)
  118. {
  119. stc.xyz /= stc.w;
  120. stc.z += spot_shadow_bias*scl;
  121. float cs = shadow2D(shadowMap, stc.xyz).x;
  122. float shadow = cs;
  123. vec2 off = 1.5/proj_shadow_res;
  124. shadow += max(shadow2D(shadowMap, stc.xyz+vec3(off.x, off.y, 0.0)).x, cs);
  125. shadow += max(shadow2D(shadowMap, stc.xyz+vec3(off.x, -off.y, 0.0)).x, cs);
  126. shadow += max(shadow2D(shadowMap, stc.xyz+vec3(-off.x, off.y, 0.0)).x, cs);
  127. shadow += max(shadow2D(shadowMap, stc.xyz+vec3(-off.x, -off.y, 0.0)).x, cs);
  128. return shadow/5.0;
  129. //return shadow;
  130. }
  131. void main()
  132. {
  133. vec2 pos_screen = vary_fragcoord.xy;
  134. ivec2 itc = ivec2(pos_screen);
  135. vec4 fcol = vec4(0,0,0,0);
  136. for (int i = 0; i < samples; i++)
  137. {
  138. vec4 pos = getPosition(itc, i);
  139. vec4 nmap4 = texelFetch(normalMap, itc, i);
  140. nmap4 = vec4((nmap4.xy-0.5)*2.0,nmap4.z,nmap4.w); // unpack norm
  141. float displace = nmap4.w;
  142. vec3 norm = nmap4.xyz;
  143. float shadow = 1.0;
  144. float dp_directional_light = max(0.0, dot(norm, vary_light.xyz));
  145. vec3 shadow_pos = pos.xyz + displace*norm;
  146. vec3 offset = vary_light.xyz * (1.0-dp_directional_light);
  147. vec4 spos = vec4(shadow_pos+offset*shadow_offset, 1.0);
  148. if (spos.z > -shadow_clip.w)
  149. {
  150. if (dp_directional_light == 0.0)
  151. {
  152. // 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
  153. shadow = 0.0;
  154. }
  155. else
  156. {
  157. vec4 lpos;
  158. if (spos.z < -shadow_clip.z)
  159. {
  160. lpos = shadow_matrix[3]*spos;
  161. lpos.xy *= shadow_res;
  162. shadow = pcfShadow(shadowMap3, lpos, 0.25);
  163. shadow += max((pos.z+shadow_clip.z)/(shadow_clip.z-shadow_clip.w)*2.0-1.0, 0.0);
  164. }
  165. else if (spos.z < -shadow_clip.y)
  166. {
  167. lpos = shadow_matrix[2]*spos;
  168. lpos.xy *= shadow_res;
  169. shadow = pcfShadow(shadowMap2, lpos, 0.5);
  170. }
  171. else if (spos.z < -shadow_clip.x)
  172. {
  173. lpos = shadow_matrix[1]*spos;
  174. lpos.xy *= shadow_res;
  175. shadow = pcfShadow(shadowMap1, lpos, 0.75);
  176. }
  177. else
  178. {
  179. lpos = shadow_matrix[0]*spos;
  180. lpos.xy *= shadow_res;
  181. shadow = pcfShadow(shadowMap0, lpos, 1.0);
  182. }
  183. // take the most-shadowed value out of these two:
  184. // * the blurred sun shadow in the light (shadow) map
  185. // * an unblurred dot product between the sun and this norm
  186. // the goal is to err on the side of most-shadow to fill-in shadow holes and reduce artifacting
  187. shadow = min(shadow, dp_directional_light);
  188. }
  189. }
  190. else
  191. {
  192. // more distant than the shadow map covers
  193. shadow = 1.0;
  194. }
  195. fcol[0] += shadow;
  196. fcol[1] += calcAmbientOcclusion(pos, norm, i);
  197. spos.xyz = shadow_pos+offset*spot_shadow_offset;
  198. //spotlight shadow 1
  199. vec4 lpos = shadow_matrix[4]*spos;
  200. fcol[2] += pcfShadow(shadowMap4, lpos, 0.8);
  201. //spotlight shadow 2
  202. lpos = shadow_matrix[5]*spos;
  203. fcol[3] += pcfShadow(shadowMap5, lpos, 0.8);
  204. }
  205. gl_FragColor = fcol / samples;
  206. }