PageRenderTime 239ms CodeModel.GetById 111ms app.highlight 2ms RepoModel.GetById 124ms app.codeStats 0ms

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

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