PageRenderTime 303ms CodeModel.GetById 181ms app.highlight 1ms RepoModel.GetById 119ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
text | 126 lines | 105 code | 21 blank | 0 comment | 0 complexity | 06d1b2750a25e87a033de9cc8c3b928e MD5 | raw file
  1/** 
  2 * @file pointLightF.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
 42varying vec4 vary_light;
 43
 44varying vec4 vary_fragcoord;
 45uniform vec2 screen_res;
 46
 47uniform mat4 inv_proj;
 48uniform vec4 viewport;
 49
 50vec4 getPosition(ivec2 pos_screen, int sample)
 51{
 52	float depth = texelFetch(depthMap, pos_screen, sample).r;
 53	vec2 sc = (vec2(pos_screen.xy)-viewport.xy)*2.0;
 54	sc /= viewport.zw;
 55	sc -= vec2(1.0,1.0);
 56	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
 57	vec4 pos = inv_proj * ndc;
 58	pos /= pos.w;
 59	pos.w = 1.0;
 60	return pos;
 61}
 62
 63void main() 
 64{
 65	vec4 frag = vary_fragcoord;
 66	frag.xyz /= frag.w;
 67	frag.xyz = frag.xyz*0.5+0.5;
 68	frag.xy *= screen_res;
 69	
 70	ivec2 itc = ivec2(frag.xy);
 71
 72	int wght = 0;
 73	vec3 fcol = vec3(0,0,0);
 74
 75	for (int s = 0; s < samples; ++s)
 76	{
 77		vec3 pos = getPosition(itc, s).xyz;
 78		vec3 lv = vary_light.xyz-pos;
 79		float dist2 = dot(lv,lv);
 80		dist2 /= vary_light.w;
 81		if (dist2 <= 1.0)
 82		{
 83			vec3 norm = texelFetch(normalMap, itc, s).xyz;
 84			norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
 85			float da = dot(norm, lv);
 86			if (da >= 0.0)
 87			{
 88				norm = normalize(norm);
 89				lv = normalize(lv);
 90				da = dot(norm, lv);
 91	
 92				float noise = texture2D(noiseMap, frag.xy/128.0).b;
 93	
 94				vec3 col = texelFetch(diffuseRect, itc, s).rgb;
 95				float fa = gl_Color.a+1.0;
 96				float dist_atten = clamp(1.0-(dist2-1.0*(1.0-fa))/fa, 0.0, 1.0);
 97				float lit = da * dist_atten * noise;
 98	
 99				col = gl_Color.rgb*lit*col;
100
101				vec4 spec = texelFetch(specularRect, itc, s);
102				if (spec.a > 0.0)
103				{
104					float sa = dot(normalize(lv-normalize(pos)),norm);
105					if (sa > 0.0)
106					{
107						sa = texture2D(lightFunc, vec2(sa, spec.a)).a * min(dist_atten*4.0, 1.0);
108						sa *= noise;
109						col += da*sa*gl_Color.rgb*spec.rgb;
110					}
111				}
112
113				fcol += col;
114				++wght;
115			}
116		}
117	}
118	
119	if (wght <= 0)
120	{
121		discard;
122	}
123		
124	gl_FragColor.rgb = fcol/samples;	
125	gl_FragColor.a = 0.0;
126}