PageRenderTime 50ms CodeModel.GetById 43ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 126 lines | 102 code | 24 blank | 0 comment | 0 complexity | 63adcb35d96ce4be87e5bffdd076a973 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#extension GL_ARB_texture_rectangle : enable
 27
 28#ifdef DEFINE_GL_FRAGCOLOR
 29out vec4 gl_FragColor;
 30#endif
 31
 32uniform sampler2DRect diffuseRect;
 33
 34uniform mat4 inv_proj;
 35uniform vec2 screen_res;
 36uniform float max_cof;
 37uniform float res_scale;
 38
 39VARYING vec2 vary_fragcoord;
 40
 41void dofSample(inout vec4 diff, inout float w, float min_sc, vec2 tc)
 42{
 43	vec4 s = texture2DRect(diffuseRect, tc);
 44
 45	float sc = abs(s.a*2.0-1.0)*max_cof;
 46
 47	if (sc > min_sc) //sampled pixel is more "out of focus" than current sample radius
 48	{
 49		float wg = 0.25;
 50		
 51		// de-weight dull areas to make highlights 'pop'
 52		wg += s.r+s.g+s.b;
 53	
 54		diff += wg*s;
 55		
 56		w += wg;
 57	}
 58}
 59
 60void dofSampleNear(inout vec4 diff, inout float w, float min_sc, vec2 tc)
 61{
 62	vec4 s = texture2DRect(diffuseRect, tc);
 63
 64	float wg = 0.25;
 65
 66	// de-weight dull areas to make highlights 'pop'
 67	wg += s.r+s.g+s.b;
 68
 69	diff += wg*s;
 70		
 71	w += wg;
 72}
 73
 74void main() 
 75{
 76	vec2 tc = vary_fragcoord.xy;
 77	
 78	vec4 diff = texture2DRect(diffuseRect, vary_fragcoord.xy);
 79	
 80	{ 
 81		float w = 1.0;
 82		
 83		float sc = (diff.a*2.0-1.0)*max_cof;
 84			
 85		float PI = 3.14159265358979323846264;
 86
 87		// sample quite uniformly spaced points within a circle, for a circular 'bokeh'		
 88		if (sc > 0.5)
 89		{
 90			while (sc > 0.5)
 91			{
 92				int its = int(max(1.0,(sc*3.7)));
 93				for (int i=0; i<its; ++i)
 94				{
 95					float ang = sc+i*2*PI/its; // sc is added for rotary perturbance
 96					float samp_x = sc*sin(ang);
 97					float samp_y = sc*cos(ang);
 98					// you could test sample coords against an interesting non-circular aperture shape here, if desired.
 99					dofSampleNear(diff, w, sc, vary_fragcoord.xy + vec2(samp_x,samp_y));
100				}
101				sc -= 1.0;
102			}
103		}
104		else if (sc < -0.5)
105		{
106			sc = abs(sc);
107			while (sc > 0.5)
108			{
109				int its = int(max(1.0,(sc*3.7)));
110				for (int i=0; i<its; ++i)
111				{
112					float ang = sc+i*2*PI/its; // sc is added for rotary perturbance
113					float samp_x = sc*sin(ang);
114					float samp_y = sc*cos(ang);
115					// you could test sample coords against an interesting non-circular aperture shape here, if desired.
116					dofSample(diff, w, sc, vary_fragcoord.xy + vec2(samp_x,samp_y));
117				}
118				sc -= 1.0;
119			}
120		}
121
122		diff /= w;
123	}
124		
125	gl_FragColor = diff;
126}