PageRenderTime 17ms CodeModel.GetById 10ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 135 lines | 106 code | 29 blank | 0 comment | 0 complexity | 22e81bf6235dc37a18fb04776f80f230 MD5 | raw file
  1/** 
  2 * @file sunLightSSAOF.glsl
  3 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  4 * Second Life Viewer Source Code
  5 * Copyright (C) 2007, Linden Research, Inc.
  6 * 
  7 * This library is free software; you can redistribute it and/or
  8 * modify it under the terms of the GNU Lesser General Public
  9 * License as published by the Free Software Foundation;
 10 * version 2.1 of the License only.
 11 * 
 12 * This library is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15 * Lesser General Public License for more details.
 16 * 
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this library; if not, write to the Free Software
 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 20 * 
 21 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 22 * $/LicenseInfo$
 23 */
 24 
 25
 26#extension GL_ARB_texture_rectangle : enable
 27
 28#ifdef DEFINE_GL_FRAGCOLOR
 29out vec4 gl_FragColor;
 30#endif
 31
 32//class 1 -- no shadow, SSAO only
 33
 34uniform sampler2DRect depthMap;
 35uniform sampler2DRect normalMap;
 36uniform sampler2D noiseMap;
 37
 38
 39// Inputs
 40uniform mat4 shadow_matrix[6];
 41uniform vec4 shadow_clip;
 42uniform float ssao_radius;
 43uniform float ssao_max_radius;
 44uniform float ssao_factor;
 45uniform float ssao_factor_inv;
 46
 47VARYING vec2 vary_fragcoord;
 48
 49uniform mat4 inv_proj;
 50uniform vec2 screen_res;
 51
 52uniform float shadow_bias;
 53uniform float shadow_offset;
 54
 55vec4 getPosition(vec2 pos_screen)
 56{
 57	float depth = texture2DRect(depthMap, pos_screen.xy).r;
 58	vec2 sc = pos_screen.xy*2.0;
 59	sc /= screen_res;
 60	sc -= vec2(1.0,1.0);
 61	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
 62	vec4 pos = inv_proj * ndc;
 63	pos /= pos.w;
 64	pos.w = 1.0;
 65	return pos;
 66}
 67
 68//calculate decreases in ambient lighting when crowded out (SSAO)
 69float calcAmbientOcclusion(vec4 pos, vec3 norm)
 70{
 71	float ret = 1.0;
 72	
 73	vec2 kern[8];
 74	// exponentially (^2) distant occlusion samples spread around origin
 75	kern[0] = vec2(-1.0, 0.0) * 0.125*0.125;
 76	kern[1] = vec2(1.0, 0.0) * 0.250*0.250;
 77	kern[2] = vec2(0.0, 1.0) * 0.375*0.375;
 78	kern[3] = vec2(0.0, -1.0) * 0.500*0.500;
 79	kern[4] = vec2(0.7071, 0.7071) * 0.625*0.625;
 80	kern[5] = vec2(-0.7071, -0.7071) * 0.750*0.750;
 81	kern[6] = vec2(-0.7071, 0.7071) * 0.875*0.875;
 82	kern[7] = vec2(0.7071, -0.7071) * 1.000*1.000;
 83
 84	vec2 pos_screen = vary_fragcoord.xy;
 85	vec3 pos_world = pos.xyz;
 86	vec2 noise_reflect = texture2D(noiseMap, vary_fragcoord.xy/128.0).xy;
 87		
 88	float angle_hidden = 0.0;
 89	int points = 0;
 90		
 91	float scale = min(ssao_radius / -pos_world.z, ssao_max_radius);
 92		
 93	// it was found that keeping # of samples a constant was the fastest, probably due to compiler optimizations unrolling?)
 94	for (int i = 0; i < 8; i++)
 95	{
 96		vec2 samppos_screen = pos_screen + scale * reflect(kern[i], noise_reflect);
 97		vec3 samppos_world = getPosition(samppos_screen).xyz; 
 98			
 99		vec3 diff = pos_world - samppos_world;
100		float dist2 = dot(diff, diff);
101			
102		// assume each sample corresponds to an occluding sphere with constant radius, constant x-sectional area
103		// --> solid angle shrinking by the square of distance
104		//radius is somewhat arbitrary, can approx with just some constant k * 1 / dist^2
105		//(k should vary inversely with # of samples, but this is taken care of later)
106			
107		angle_hidden = angle_hidden + float(dot((samppos_world - 0.05*norm - pos_world), norm) > 0.0) * min(1.0/dist2, ssao_factor_inv);
108			
109		// 'blocked' samples (significantly closer to camera relative to pos_world) are "no data", not "no occlusion" 
110		points = points + int(diff.z > -1.0);
111	}
112		
113	angle_hidden = min(ssao_factor*angle_hidden/float(points), 1.0);
114		
115	ret = (1.0 - (float(points != 0) * angle_hidden));
116	
117	return min(ret, 1.0);
118}
119
120void main() 
121{
122	vec2 pos_screen = vary_fragcoord.xy;
123	
124	//try doing an unproject here
125	
126	vec4 pos = getPosition(pos_screen);
127	
128	vec3 norm = texture2DRect(normalMap, pos_screen).xyz;
129	norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
130		
131	gl_FragColor[0] = 1.0;
132	gl_FragColor[1] = calcAmbientOcclusion(pos, norm);
133	gl_FragColor[2] = 1.0; 
134	gl_FragColor[3] = 1.0;
135}