PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

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

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 116 lines | 99 code | 17 blank | 0 comment | 0 complexity | 8e71d9c02ed2067f1dd5a71d5bd3d374 MD5 | raw file
  1/** 
  2 * @file blurLightF.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
 26#extension GL_ARB_texture_rectangle : enable
 27
 28#ifdef DEFINE_GL_FRAGCOLOR
 29out vec4 gl_FragColor;
 30#endif
 31
 32uniform sampler2DRect depthMap;
 33uniform sampler2DRect normalMap;
 34uniform sampler2DRect lightMap;
 35
 36uniform float dist_factor;
 37uniform float blur_size;
 38uniform vec2 delta;
 39uniform vec3 kern[4];
 40uniform float kern_scale;
 41
 42VARYING vec2 vary_fragcoord;
 43
 44uniform mat4 inv_proj;
 45uniform vec2 screen_res;
 46
 47vec3 getKern(int i)
 48{
 49	return kern[i];
 50}
 51
 52vec4 getPosition(vec2 pos_screen)
 53{
 54	float depth = texture2DRect(depthMap, pos_screen.xy).r;
 55	vec2 sc = pos_screen.xy*2.0;
 56	sc /= screen_res;
 57	sc -= vec2(1.0,1.0);
 58	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
 59	vec4 pos = inv_proj * ndc;
 60	pos /= pos.w;
 61	pos.w = 1.0;
 62	return pos;
 63}
 64
 65void main() 
 66{
 67    vec2 tc = vary_fragcoord.xy;
 68	vec3 norm = texture2DRect(normalMap, tc).xyz;
 69	norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
 70	vec3 pos = getPosition(tc).xyz;
 71	vec4 ccol = texture2DRect(lightMap, tc).rgba;
 72	
 73	vec2 dlt = kern_scale * delta / (1.0+norm.xy*norm.xy);
 74	dlt /= max(-pos.z*dist_factor, 1.0);
 75	
 76	vec2 defined_weight = getKern(0).xy; // special case the first (centre) sample's weight in the blur; we have to sample it anyway so we get it for 'free'
 77	vec4 col = defined_weight.xyxx * ccol;
 78
 79	// relax tolerance according to distance to avoid speckling artifacts, as angles and distances are a lot more abrupt within a small screen area at larger distances
 80	float pointplanedist_tolerance_pow2 = pos.z*pos.z*0.00005;
 81
 82	// perturb sampling origin slightly in screen-space to hide edge-ghosting artifacts where smoothing radius is quite large
 83	float tc_mod = 0.5*(tc.x + tc.y); // mod(tc.x+tc.y,2)
 84	tc_mod -= floor(tc_mod);
 85	tc_mod *= 2.0;
 86	tc += ( (tc_mod - 0.5) * getKern(1).z * dlt * 0.5 );
 87
 88	for (int i = 1; i < 4; i++)
 89	{
 90		vec2 samptc = tc + getKern(i).z*dlt;
 91	        vec3 samppos = getPosition(samptc).xyz; 
 92		float d = dot(norm.xyz, samppos.xyz-pos.xyz);// dist from plane
 93		if (d*d <= pointplanedist_tolerance_pow2)
 94		{
 95			col += texture2DRect(lightMap, samptc)*getKern(i).xyxx;
 96			defined_weight += getKern(i).xy;
 97		}
 98	}
 99	for (int i = 1; i < 4; i++)
100	{
101		vec2 samptc = tc - getKern(i).z*dlt;
102	        vec3 samppos = getPosition(samptc).xyz; 
103		float d = dot(norm.xyz, samppos.xyz-pos.xyz);// dist from plane
104		if (d*d <= pointplanedist_tolerance_pow2)
105		{
106			col += texture2DRect(lightMap, samptc)*getKern(i).xyxx;
107			defined_weight += getKern(i).xy;
108		}
109	}
110
111	col /= defined_weight.xyxx;
112	col.y *= col.y;
113	
114	gl_FragColor = col;
115}
116