PageRenderTime 25ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
text | 342 lines | 280 code | 62 blank | 0 comment | 0 complexity | 0321459a6eeede4a8737d6ae2c098902 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file softenLightF.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 diffuseRect;
  28. uniform sampler2DMS specularRect;
  29. uniform sampler2DMS normalMap;
  30. uniform sampler2DMS depthMap;
  31. uniform sampler2D noiseMap;
  32. uniform samplerCube environmentMap;
  33. uniform sampler2D lightFunc;
  34. uniform float blur_size;
  35. uniform float blur_fidelity;
  36. // Inputs
  37. uniform vec4 morphFactor;
  38. uniform vec3 camPosLocal;
  39. //uniform vec4 camPosWorld;
  40. uniform vec4 gamma;
  41. uniform vec4 lightnorm;
  42. uniform vec4 sunlight_color;
  43. uniform vec4 ambient;
  44. uniform vec4 blue_horizon;
  45. uniform vec4 blue_density;
  46. uniform vec4 haze_horizon;
  47. uniform vec4 haze_density;
  48. uniform vec4 cloud_shadow;
  49. uniform vec4 density_multiplier;
  50. uniform vec4 distance_multiplier;
  51. uniform vec4 max_y;
  52. uniform vec4 glow;
  53. uniform float scene_light_strength;
  54. uniform vec3 env_mat[3];
  55. //uniform mat4 shadow_matrix[3];
  56. //uniform vec4 shadow_clip;
  57. uniform mat3 ssao_effect_mat;
  58. varying vec4 vary_light;
  59. varying vec2 vary_fragcoord;
  60. vec3 vary_PositionEye;
  61. vec3 vary_SunlitColor;
  62. vec3 vary_AmblitColor;
  63. vec3 vary_AdditiveColor;
  64. vec3 vary_AtmosAttenuation;
  65. uniform mat4 inv_proj;
  66. uniform vec2 screen_res;
  67. vec4 getPosition_d(vec2 pos_screen, float depth)
  68. {
  69. vec2 sc = pos_screen.xy*2.0;
  70. sc /= screen_res;
  71. sc -= vec2(1.0,1.0);
  72. vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
  73. vec4 pos = inv_proj * ndc;
  74. pos /= pos.w;
  75. pos.w = 1.0;
  76. return pos;
  77. }
  78. vec3 getPositionEye()
  79. {
  80. return vary_PositionEye;
  81. }
  82. vec3 getSunlitColor()
  83. {
  84. return vary_SunlitColor;
  85. }
  86. vec3 getAmblitColor()
  87. {
  88. return vary_AmblitColor;
  89. }
  90. vec3 getAdditiveColor()
  91. {
  92. return vary_AdditiveColor;
  93. }
  94. vec3 getAtmosAttenuation()
  95. {
  96. return vary_AtmosAttenuation;
  97. }
  98. void setPositionEye(vec3 v)
  99. {
  100. vary_PositionEye = v;
  101. }
  102. void setSunlitColor(vec3 v)
  103. {
  104. vary_SunlitColor = v;
  105. }
  106. void setAmblitColor(vec3 v)
  107. {
  108. vary_AmblitColor = v;
  109. }
  110. void setAdditiveColor(vec3 v)
  111. {
  112. vary_AdditiveColor = v;
  113. }
  114. void setAtmosAttenuation(vec3 v)
  115. {
  116. vary_AtmosAttenuation = v;
  117. }
  118. void calcAtmospherics(vec3 inPositionEye, float ambFactor) {
  119. vec3 P = inPositionEye;
  120. setPositionEye(P);
  121. //(TERRAIN) limit altitude
  122. if (P.y > max_y.x) P *= (max_y.x / P.y);
  123. if (P.y < -max_y.x) P *= (-max_y.x / P.y);
  124. vec3 tmpLightnorm = lightnorm.xyz;
  125. vec3 Pn = normalize(P);
  126. float Plen = length(P);
  127. vec4 temp1 = vec4(0);
  128. vec3 temp2 = vec3(0);
  129. vec4 blue_weight;
  130. vec4 haze_weight;
  131. vec4 sunlight = sunlight_color;
  132. vec4 light_atten;
  133. //sunlight attenuation effect (hue and brightness) due to atmosphere
  134. //this is used later for sunlight modulation at various altitudes
  135. light_atten = (blue_density * 1.0 + vec4(haze_density.r) * 0.25) * (density_multiplier.x * max_y.x);
  136. //I had thought blue_density and haze_density should have equal weighting,
  137. //but attenuation due to haze_density tends to seem too strong
  138. temp1 = blue_density + vec4(haze_density.r);
  139. blue_weight = blue_density / temp1;
  140. haze_weight = vec4(haze_density.r) / temp1;
  141. //(TERRAIN) compute sunlight from lightnorm only (for short rays like terrain)
  142. temp2.y = max(0.0, tmpLightnorm.y);
  143. temp2.y = 1. / temp2.y;
  144. sunlight *= exp( - light_atten * temp2.y);
  145. // main atmospheric scattering line integral
  146. temp2.z = Plen * density_multiplier.x;
  147. // Transparency (-> temp1)
  148. // ATI Bugfix -- can't store temp1*temp2.z*distance_multiplier.x in a variable because the ati
  149. // compiler gets confused.
  150. temp1 = exp(-temp1 * temp2.z * distance_multiplier.x);
  151. //final atmosphere attenuation factor
  152. setAtmosAttenuation(temp1.rgb);
  153. //compute haze glow
  154. //(can use temp2.x as temp because we haven't used it yet)
  155. temp2.x = dot(Pn, tmpLightnorm.xyz);
  156. temp2.x = 1. - temp2.x;
  157. //temp2.x is 0 at the sun and increases away from sun
  158. temp2.x = max(temp2.x, .03); //was glow.y
  159. //set a minimum "angle" (smaller glow.y allows tighter, brighter hotspot)
  160. temp2.x *= glow.x;
  161. //higher glow.x gives dimmer glow (because next step is 1 / "angle")
  162. temp2.x = pow(temp2.x, glow.z);
  163. //glow.z should be negative, so we're doing a sort of (1 / "angle") function
  164. //add "minimum anti-solar illumination"
  165. temp2.x += .25;
  166. //increase ambient when there are more clouds
  167. vec4 tmpAmbient = ambient + (vec4(1.) - ambient) * cloud_shadow.x * 0.5;
  168. /* decrease value and saturation (that in HSV, not HSL) for occluded areas
  169. * // for HSV color/geometry used here, see http://gimp-savvy.com/BOOK/index.html?node52.html
  170. * // The following line of code performs the equivalent of:
  171. * float ambAlpha = tmpAmbient.a;
  172. * float ambValue = dot(vec3(tmpAmbient), vec3(0.577)); // projection onto <1/rt(3), 1/rt(3), 1/rt(3)>, the neutral white-black axis
  173. * vec3 ambHueSat = vec3(tmpAmbient) - vec3(ambValue);
  174. * tmpAmbient = vec4(RenderSSAOEffect.valueFactor * vec3(ambValue) + RenderSSAOEffect.saturationFactor *(1.0 - ambFactor) * ambHueSat, ambAlpha);
  175. */
  176. tmpAmbient = vec4(mix(ssao_effect_mat * tmpAmbient.rgb, tmpAmbient.rgb, ambFactor), tmpAmbient.a);
  177. //haze color
  178. setAdditiveColor(
  179. vec3(blue_horizon * blue_weight * (sunlight*(1.-cloud_shadow.x) + tmpAmbient)
  180. + (haze_horizon.r * haze_weight) * (sunlight*(1.-cloud_shadow.x) * temp2.x
  181. + tmpAmbient)));
  182. //brightness of surface both sunlight and ambient
  183. setSunlitColor(vec3(sunlight * .5));
  184. setAmblitColor(vec3(tmpAmbient * .25));
  185. setAdditiveColor(getAdditiveColor() * vec3(1.0 - temp1));
  186. }
  187. vec3 atmosLighting(vec3 light)
  188. {
  189. light *= getAtmosAttenuation().r;
  190. light += getAdditiveColor();
  191. return (2.0 * light);
  192. }
  193. vec3 atmosTransport(vec3 light) {
  194. light *= getAtmosAttenuation().r;
  195. light += getAdditiveColor() * 2.0;
  196. return light;
  197. }
  198. vec3 atmosGetDiffuseSunlightColor()
  199. {
  200. return getSunlitColor();
  201. }
  202. vec3 scaleDownLight(vec3 light)
  203. {
  204. return (light / scene_light_strength );
  205. }
  206. vec3 scaleUpLight(vec3 light)
  207. {
  208. return (light * scene_light_strength);
  209. }
  210. vec3 atmosAmbient(vec3 light)
  211. {
  212. return getAmblitColor() + light / 2.0;
  213. }
  214. vec3 atmosAffectDirectionalLight(float lightIntensity)
  215. {
  216. return getSunlitColor() * lightIntensity;
  217. }
  218. vec3 scaleSoftClip(vec3 light)
  219. {
  220. //soft clip effect:
  221. light = 1. - clamp(light, vec3(0.), vec3(1.));
  222. light = 1. - pow(light, gamma.xxx);
  223. return light;
  224. }
  225. vec4 texture2DMS(sampler2DMS tex, ivec2 tc)
  226. {
  227. vec4 ret = vec4(0,0,0,0);
  228. for (int i = 0; i < samples; ++i)
  229. {
  230. ret += texelFetch(tex,tc,i);
  231. }
  232. return ret/samples;
  233. }
  234. void main()
  235. {
  236. vec2 tc = vary_fragcoord.xy;
  237. ivec2 itc = ivec2(tc);
  238. vec4 fcol = vec4(0,0,0,0);
  239. for (int i = 0; i < samples; ++i)
  240. {
  241. float depth = texelFetch(depthMap, itc, i).r;
  242. vec3 pos = getPosition_d(tc, depth).xyz;
  243. vec3 norm = texelFetch(normalMap, itc, i).xyz;
  244. norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
  245. //vec3 nz = texture2D(noiseMap, vary_fragcoord.xy/128.0).xyz;
  246. float da = max(dot(norm.xyz, vary_light.xyz), 0.0);
  247. vec4 diffuse = texelFetch(diffuseRect, itc, i);
  248. vec3 col;
  249. float bloom = 0.0;
  250. if (diffuse.a < 0.9)
  251. {
  252. vec4 spec = texelFetch(specularRect, itc, i);
  253. calcAtmospherics(pos.xyz, 1.0);
  254. col = atmosAmbient(vec3(0));
  255. col += atmosAffectDirectionalLight(max(min(da, 1.0), diffuse.a));
  256. col *= diffuse.rgb;
  257. if (spec.a > 0.0) // specular reflection
  258. {
  259. // the old infinite-sky shiny reflection
  260. //
  261. vec3 refnormpersp = normalize(reflect(pos.xyz, norm.xyz));
  262. float sa = dot(refnormpersp, vary_light.xyz);
  263. vec3 dumbshiny = vary_SunlitColor*texture2D(lightFunc, vec2(sa, spec.a)).a;
  264. // add the two types of shiny together
  265. vec3 spec_contrib = dumbshiny * spec.rgb;
  266. bloom = dot(spec_contrib, spec_contrib);
  267. col += spec_contrib;
  268. }
  269. col = atmosLighting(col);
  270. col = scaleSoftClip(col);
  271. col = mix(col, diffuse.rgb, diffuse.a);
  272. }
  273. else
  274. {
  275. col = diffuse.rgb;
  276. }
  277. fcol += vec4(col, bloom);
  278. }
  279. gl_FragColor = fcol/samples;
  280. }