PageRenderTime 34ms CodeModel.GetById 30ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/app_settings/shaders/class2/deferred/sunLightF.glsl

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 216 lines | 176 code | 40 blank | 0 comment | 0 complexity | f9c38ba6c0ffe772944db30de4b2579b MD5 | raw file
  1/** 
  2 * @file sunLightF.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
 32//class 2, shadows, no SSAO
 33
 34uniform sampler2DRect depthMap;
 35uniform sampler2DRect normalMap;
 36uniform sampler2DRectShadow shadowMap0;
 37uniform sampler2DRectShadow shadowMap1;
 38uniform sampler2DRectShadow shadowMap2;
 39uniform sampler2DRectShadow shadowMap3;
 40uniform sampler2DShadow shadowMap4;
 41uniform sampler2DShadow shadowMap5;
 42
 43
 44// Inputs
 45uniform mat4 shadow_matrix[6];
 46uniform vec4 shadow_clip;
 47uniform float ssao_radius;
 48uniform float ssao_max_radius;
 49uniform float ssao_factor;
 50uniform float ssao_factor_inv;
 51
 52VARYING vec2 vary_fragcoord;
 53
 54uniform mat4 inv_proj;
 55uniform vec2 screen_res;
 56uniform vec2 shadow_res;
 57uniform vec2 proj_shadow_res;
 58uniform vec3 sun_dir;
 59
 60uniform float shadow_bias;
 61uniform float shadow_offset;
 62
 63uniform float spot_shadow_bias;
 64uniform float spot_shadow_offset;
 65
 66vec4 getPosition(vec2 pos_screen)
 67{
 68	float depth = texture2DRect(depthMap, pos_screen.xy).r;
 69	vec2 sc = pos_screen.xy*2.0;
 70	sc /= screen_res;
 71	sc -= vec2(1.0,1.0);
 72	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
 73	vec4 pos = inv_proj * ndc;
 74	pos /= pos.w;
 75	pos.w = 1.0;
 76	return pos;
 77}
 78
 79float pcfShadow(sampler2DRectShadow shadowMap, vec4 stc, float scl)
 80{
 81	stc.xyz /= stc.w;
 82	stc.z += shadow_bias*scl;
 83	
 84	float cs = shadow2DRect(shadowMap, stc.xyz).x;
 85	float shadow = cs;
 86
 87	shadow += max(shadow2DRect(shadowMap, stc.xyz+vec3(1.5, 1.5, 0.0)).x, cs);
 88	shadow += max(shadow2DRect(shadowMap, stc.xyz+vec3(1.5, -1.5, 0.0)).x, cs);
 89	shadow += max(shadow2DRect(shadowMap, stc.xyz+vec3(-1.5, 1.5, 0.0)).x, cs);
 90	shadow += max(shadow2DRect(shadowMap, stc.xyz+vec3(-1.5, -1.5, 0.0)).x, cs);
 91			
 92	return shadow/5.0;
 93	
 94	//return shadow;
 95}
 96
 97float pcfShadow(sampler2DShadow shadowMap, vec4 stc, float scl)
 98{
 99	stc.xyz /= stc.w;
100	stc.z += spot_shadow_bias*scl;
101	
102	float cs = shadow2D(shadowMap, stc.xyz).x;
103	float shadow = cs;
104
105	vec2 off = 1.5/proj_shadow_res;
106	
107	shadow += max(shadow2D(shadowMap, stc.xyz+vec3(off.x, off.y, 0.0)).x, cs);
108	shadow += max(shadow2D(shadowMap, stc.xyz+vec3(off.x, -off.y, 0.0)).x, cs);
109	shadow += max(shadow2D(shadowMap, stc.xyz+vec3(-off.x, off.y, 0.0)).x, cs);
110	shadow += max(shadow2D(shadowMap, stc.xyz+vec3(-off.x, -off.y, 0.0)).x, cs);
111				
112	return shadow/5.0;
113	
114	//return shadow;
115}
116
117void main() 
118{
119	vec2 pos_screen = vary_fragcoord.xy;
120	
121	//try doing an unproject here
122	
123	vec4 pos = getPosition(pos_screen);
124	
125	vec4 nmap4 = texture2DRect(normalMap, pos_screen);
126	nmap4 = vec4((nmap4.xy-0.5)*2.0,nmap4.z,nmap4.w); // unpack norm
127	float displace = nmap4.w;
128	vec3 norm = nmap4.xyz;
129	
130	/*if (pos.z == 0.0) // do nothing for sky *FIX: REMOVE THIS IF/WHEN THE POSITION MAP IS BEING USED AS A STENCIL
131	{
132		gl_FragColor = vec4(0.0); // doesn't matter
133		return;
134	}*/
135	
136	float shadow = 1.0;
137	float dp_directional_light = max(0.0, dot(norm, sun_dir.xyz));
138
139	vec3 shadow_pos = pos.xyz + displace*norm;
140	vec3 offset = sun_dir.xyz * (1.0-dp_directional_light);
141	
142	vec4 spos = vec4(shadow_pos+offset*shadow_offset, 1.0);
143	
144	if (spos.z > -shadow_clip.w)
145	{	
146		if (dp_directional_light == 0.0)
147		{
148			// if we know this point is facing away from the sun then we know it's in shadow without having to do a squirrelly shadow-map lookup
149			shadow = 0.0;
150		}
151		else
152		{
153			vec4 lpos;
154			
155			if (spos.z < -shadow_clip.z)
156			{
157				lpos = shadow_matrix[3]*spos;
158				lpos.xy *= shadow_res;
159				shadow = pcfShadow(shadowMap3, lpos, 0.25);
160				shadow += max((pos.z+shadow_clip.z)/(shadow_clip.z-shadow_clip.w)*2.0-1.0, 0.0);
161			}
162			else if (spos.z < -shadow_clip.y)
163			{
164				lpos = shadow_matrix[2]*spos;
165				lpos.xy *= shadow_res;
166				shadow = pcfShadow(shadowMap2, lpos, 0.5);
167			}
168			else if (spos.z < -shadow_clip.x)
169			{
170				lpos = shadow_matrix[1]*spos;
171				lpos.xy *= shadow_res;
172				shadow = pcfShadow(shadowMap1, lpos, 0.75);
173			}
174			else
175			{
176				lpos = shadow_matrix[0]*spos;
177				lpos.xy *= shadow_res;
178				shadow = pcfShadow(shadowMap0, lpos, 1.0);
179			}
180		
181			// take the most-shadowed value out of these two:
182			//  * the blurred sun shadow in the light (shadow) map
183			//  * an unblurred dot product between the sun and this norm
184			// the goal is to err on the side of most-shadow to fill-in shadow holes and reduce artifacting
185			shadow = min(shadow, dp_directional_light);
186			
187			//lpos.xy /= lpos.w*32.0;
188			//if (fract(lpos.x) < 0.1 || fract(lpos.y) < 0.1)
189			//{
190			//	shadow = 0.0;
191			//}
192			
193		}
194	}
195	else
196	{
197		// more distant than the shadow map covers
198		shadow = 1.0;
199	}
200	
201	gl_FragColor[0] = shadow;
202	gl_FragColor[1] = 1.0;
203	
204	spos = vec4(shadow_pos+norm*spot_shadow_offset, 1.0);
205	
206	//spotlight shadow 1
207	vec4 lpos = shadow_matrix[4]*spos;
208	gl_FragColor[2] = pcfShadow(shadowMap4, lpos, 0.8); 
209	
210	//spotlight shadow 2
211	lpos = shadow_matrix[5]*spos;
212	gl_FragColor[3] = pcfShadow(shadowMap5, lpos, 0.8); 
213
214	//gl_FragColor.rgb = pos.xyz;
215	//gl_FragColor.b = shadow;
216}