PageRenderTime 364ms CodeModel.GetById 181ms app.highlight 1ms RepoModel.GetById 180ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
text | 131 lines | 109 code | 22 blank | 0 comment | 0 complexity | 69bc7d35796a2ce293fad05db1d9b252 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
 27
 28#extension GL_ARB_texture_rectangle : enable
 29#extension GL_ARB_texture_multisample : enable
 30
 31uniform sampler2DMS depthMap;
 32uniform sampler2DMS normalMap;
 33uniform sampler2DRect lightMap;
 34
 35uniform float dist_factor;
 36uniform float blur_size;
 37uniform vec2 delta;
 38uniform vec3 kern[4];
 39uniform float kern_scale;
 40
 41varying vec2 vary_fragcoord;
 42
 43uniform mat4 inv_proj;
 44uniform vec2 screen_res;
 45
 46vec3 texture2DMS3(sampler2DMS tex, ivec2 tc)
 47{
 48	vec3 ret = vec3(0,0,0);
 49	for (int i = 0; i < samples; i++)
 50	{
 51		ret += texelFetch(tex, tc, i).rgb;
 52	}
 53
 54	return ret/samples;
 55}
 56
 57float texture2DMS1(sampler2DMS tex, ivec2 tc)
 58{
 59	float ret = 0;
 60	for (int i = 0; i < samples; i++)
 61	{
 62		ret += texelFetch(tex, tc, i).r;
 63	}
 64
 65	return ret/samples;
 66}
 67
 68vec4 getPosition(ivec2 pos_screen)
 69{
 70	float depth = texture2DMS1(depthMap, pos_screen.xy);
 71	vec2 sc = pos_screen.xy*2.0;
 72	sc /= screen_res;
 73	sc -= vec2(1.0,1.0);
 74	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
 75	vec4 pos = inv_proj * ndc;
 76	pos /= pos.w;
 77	pos.w = 1.0;
 78	return pos;
 79}
 80
 81void main() 
 82{
 83    vec2 tc = vary_fragcoord.xy;
 84	ivec2 itc = ivec2(tc);
 85
 86	vec3 norm = texture2DMS3(normalMap, itc).xyz;
 87	norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
 88	vec3 pos = getPosition(itc).xyz;
 89	vec4 ccol = texture2DRect(lightMap, tc).rgba;
 90	
 91	vec2 dlt = kern_scale * delta / (1.0+norm.xy*norm.xy);
 92	dlt /= max(-pos.z*dist_factor, 1.0);
 93	
 94	vec2 defined_weight = kern[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'
 95	vec4 col = defined_weight.xyxx * ccol;
 96
 97	// 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
 98	float pointplanedist_tolerance_pow2 = pos.z*pos.z*0.00005;
 99
100	// perturb sampling origin slightly in screen-space to hide edge-ghosting artifacts where smoothing radius is quite large
101	tc += ( (mod(tc.x+tc.y,2) - 0.5) * kern[1].z * dlt * 0.5 );
102
103	for (int i = 1; i < 4; i++)
104	{
105		vec2 samptc = tc + kern[i].z*dlt;
106		vec3 samppos = getPosition(ivec2(samptc)).xyz; 
107		float d = dot(norm.xyz, samppos.xyz-pos.xyz);// dist from plane
108		if (d*d <= pointplanedist_tolerance_pow2)
109		{
110			col += texture2DRect(lightMap, samptc)*kern[i].xyxx;
111			defined_weight += kern[i].xy;
112		}
113	}
114	for (int i = 1; i < 4; i++)
115	{
116		vec2 samptc = vec2(tc - kern[i].z*dlt);
117		vec3 samppos = getPosition(ivec2(samptc)).xyz; 
118		float d = dot(norm.xyz, samppos.xyz-pos.xyz);// dist from plane
119		if (d*d <= pointplanedist_tolerance_pow2)
120		{
121			col += texture2DRect(lightMap, samptc)*kern[i].xyxx;
122			defined_weight += kern[i].xy;
123		}
124	}
125
126	col /= defined_weight.xyxx;
127	col.y *= col.y;
128
129	gl_FragColor = col;
130}
131