PageRenderTime 235ms CodeModel.GetById 110ms app.highlight 2ms RepoModel.GetById 119ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
text | 151 lines | 117 code | 34 blank | 0 comment | 0 complexity | ef76363e9b7e24aa8f81f120eb30e1f5 MD5 | raw file
  1/** 
  2 * @file postDeferredF.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 diffuseRect;
 32uniform sampler2DMS edgeMap;
 33uniform sampler2DMS depthMap;
 34uniform sampler2DMS normalMap;
 35uniform sampler2D bloomMap;
 36
 37uniform float depth_cutoff;
 38uniform float norm_cutoff;
 39uniform float focal_distance;
 40uniform float blur_constant;
 41uniform float tan_pixel_angle;
 42uniform float magnification;
 43
 44uniform mat4 inv_proj;
 45uniform vec2 screen_res;
 46
 47varying vec2 vary_fragcoord;
 48
 49vec4 texture2DMS(sampler2DMS tex, ivec2 tc)
 50{
 51	vec4 ret = vec4(0,0,0,0);
 52	for (int i = 0; i < samples; ++i)
 53	{
 54		ret += texelFetch(tex, tc, i);
 55	}
 56
 57	return ret/samples;
 58}
 59
 60float getDepth(ivec2 pos_screen)
 61{
 62	float z = texture2DMS(depthMap, pos_screen.xy).r;
 63	z = z*2.0-1.0;
 64	vec4 ndc = vec4(0.0, 0.0, z, 1.0);
 65	vec4 p = inv_proj*ndc;
 66	return p.z/p.w;
 67}
 68
 69float calc_cof(float depth)
 70{
 71	float sc = abs(depth-focal_distance)/-depth*blur_constant;
 72		
 73	sc /= magnification;
 74	
 75	// tan_pixel_angle = pixel_length/-depth;
 76	float pixel_length =  tan_pixel_angle*-focal_distance;
 77	
 78	sc = sc/pixel_length;
 79	sc *= 1.414;
 80	
 81	return sc;
 82}
 83
 84void dofSample(inout vec4 diff, inout float w, float min_sc, float cur_depth, ivec2 tc)
 85{
 86	float d = getDepth(tc);
 87	
 88	float sc = calc_cof(d);
 89	
 90	if (sc > min_sc //sampled pixel is more "out of focus" than current sample radius
 91	   || d < cur_depth) //sampled pixel is further away than current pixel
 92	{
 93		float wg = 0.25;
 94		
 95		vec4 s = texture2DMS(diffuseRect, tc);
 96		// de-weight dull areas to make highlights 'pop'
 97		wg += s.r+s.g+s.b;
 98	
 99		diff += wg*s;
100		
101		w += wg;
102	}
103}
104
105
106void main() 
107{
108	ivec2 itc = ivec2(vary_fragcoord.xy);
109
110	vec3 norm = texture2DMS(normalMap, itc).xyz;
111	norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
112		
113	float depth = getDepth(itc);
114	
115	vec4 diff = texture2DMS(diffuseRect, itc);
116	
117	{ 
118		float w = 1.0;
119		
120		float sc = calc_cof(depth);
121		sc = min(abs(sc), 10.0);
122		
123		float fd = depth*0.5f;
124		
125		float PI = 3.14159265358979323846264;
126
127		int isc = int(sc);
128		
129		// sample quite uniformly spaced points within a circle, for a circular 'bokeh'		
130		//if (depth < focal_distance)
131		{
132			for (int x = -isc; x <= isc; x+=2)
133			{
134				for (int y = -isc; y <= isc; y+=2)
135				{
136					ivec2 cur_samp = ivec2(x,y);
137					float cur_sc = length(vec2(cur_samp));
138					if (cur_sc < sc)
139					{
140						dofSample(diff, w, cur_sc, depth, itc+cur_samp);
141					}
142				}
143			}
144		}
145		
146		diff /= w;
147	}
148		
149	vec4 bloom = texture2D(bloomMap, vary_fragcoord.xy/screen_res);
150	gl_FragColor = diff + bloom;
151}