PageRenderTime 17ms CodeModel.GetById 8ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
text | 155 lines | 124 code | 31 blank | 0 comment | 0 complexity | ba416baeb4daadc0ec1ff9613e09cff7 MD5 | raw file
  1/** 
  2 * @file multiPointLightF.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 depthMap;
 32uniform sampler2DMS diffuseRect;
 33uniform sampler2DMS specularRect;
 34uniform sampler2DMS normalMap;
 35uniform sampler2D noiseMap;
 36uniform sampler2D lightFunc;
 37
 38
 39uniform vec3 env_mat[3];
 40uniform float sun_wash;
 41
 42uniform int light_count;
 43
 44#define MAX_LIGHT_COUNT		16
 45uniform vec4 light[MAX_LIGHT_COUNT];
 46uniform vec4 light_col[MAX_LIGHT_COUNT];
 47
 48varying vec4 vary_fragcoord;
 49uniform vec2 screen_res;
 50
 51uniform float far_z;
 52
 53uniform mat4 inv_proj;
 54
 55vec4 getPosition(ivec2 pos_screen, int sample)
 56{
 57	float depth = texelFetch(depthMap, pos_screen, sample).r;
 58	vec2 sc = vec2(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
 68void main() 
 69{
 70	vec2 frag = (vary_fragcoord.xy*0.5+0.5)*screen_res;
 71	ivec2 itc = ivec2(frag);
 72
 73	int wght = 0;
 74	vec3 fcol = vec3(0,0,0);
 75
 76	for (int s = 0; s < samples; ++s)
 77	{
 78		vec3 pos = getPosition(itc, s).xyz;
 79		if (pos.z >= far_z)
 80		{
 81			vec3 norm = texelFetch(normalMap, itc, s).xyz;
 82			norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
 83			norm = normalize(norm);
 84			vec4 spec = texelFetch(specularRect, itc, s);
 85			vec3 diff = texelFetch(diffuseRect, itc, s).rgb;
 86			float noise = texture2D(noiseMap, frag.xy/128.0).b;
 87			vec3 out_col = vec3(0,0,0);
 88			vec3 npos = normalize(-pos);
 89
 90			// As of OSX 10.6.7 ATI Apple's crash when using a variable size loop
 91			for (int i = 0; i < MAX_LIGHT_COUNT; ++i)
 92			{
 93				bool light_contrib = (i < light_count);
 94		
 95				vec3 lv = light[i].xyz-pos;
 96				float dist2 = dot(lv,lv);
 97				dist2 /= light[i].w;
 98				if (dist2 > 1.0)
 99				{
100					light_contrib = false;
101				}
102		
103				float da = dot(norm, lv);
104				if (da < 0.0)
105				{
106					light_contrib = false;
107				}
108		
109				if (light_contrib)
110				{
111					lv = normalize(lv);
112					da = dot(norm, lv);
113					
114					float fa = light_col[i].a+1.0;
115					float dist_atten = clamp(1.0-(dist2-1.0*(1.0-fa))/fa, 0.0, 1.0);
116					dist_atten *= noise;
117
118					float lit = da * dist_atten;
119			
120					vec3 col = light_col[i].rgb*lit*diff;
121					//vec3 col = vec3(dist2, light_col[i].a, lit);
122			
123					if (spec.a > 0.0)
124					{
125						//vec3 ref = dot(pos+lv, norm);
126				
127						float sa = dot(normalize(lv+npos),norm);
128				
129						if (sa > 0.0)
130						{
131							sa = texture2D(lightFunc,vec2(sa, spec.a)).a * min(dist_atten*4.0, 1.0);
132							sa *= noise;
133							col += da*sa*light_col[i].rgb*spec.rgb;
134						}
135					}
136			
137					out_col += col;
138				}
139			}
140	
141			fcol += out_col;
142			++wght;
143		}
144	}
145
146	if (wght <= 0)
147	{
148		discard;
149	}
150
151	gl_FragColor.rgb = fcol/samples;
152	gl_FragColor.a = 0.0;
153
154	
155}