PageRenderTime 39ms CodeModel.GetById 15ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/app_settings/shaders/class3/deferred/giF.glsl

https://bitbucket.org/lindenlab/viewer-beta/
text | 211 lines | 167 code | 44 blank | 0 comment | 0 complexity | e04e664a6ac570dc67d28803e5df671b MD5 | raw file
Possible License(s): LGPL-2.1
  1/** 
  2 * @file giF.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
 30uniform sampler2DRect depthMap;
 31uniform sampler2DRect normalMap;
 32uniform sampler2DRect lightMap;
 33uniform sampler2DRect specularRect;
 34
 35uniform sampler2D noiseMap;
 36
 37uniform sampler2D		diffuseGIMap;
 38uniform sampler2D		specularGIMap;
 39uniform sampler2D		normalGIMap;
 40uniform sampler2D		depthGIMap;
 41
 42uniform sampler2D		lightFunc;
 43
 44// Inputs
 45varying vec2 vary_fragcoord;
 46
 47uniform vec2 screen_res;
 48
 49uniform vec4 sunlight_color;
 50
 51uniform mat4 inv_proj;
 52uniform mat4 gi_mat;  //gPipeline.mGIMatrix - eye space to sun space
 53uniform mat4 gi_mat_proj; //gPipeline.mGIMatrixProj - eye space to projected sun space
 54uniform mat4 gi_norm_mat; //gPipeline.mGINormalMatrix - eye space normal to sun space normal matrix
 55uniform mat4 gi_inv_proj; //gPipeline.mGIInvProj - projected sun space to sun space
 56uniform float gi_sample_width;
 57uniform float gi_noise;
 58uniform float gi_attenuation;
 59uniform float gi_range;
 60
 61vec4 getPosition(vec2 pos_screen)
 62{
 63	float depth = texture2DRect(depthMap, pos_screen.xy).a;
 64	vec2 sc = pos_screen.xy*2.0;
 65	sc /= screen_res;
 66	sc -= vec2(1.0,1.0);
 67	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
 68	vec4 pos = inv_proj * ndc;
 69	pos /= pos.w;
 70	pos.w = 1.0;
 71	return pos;
 72}
 73
 74vec4 getGIPosition(vec2 gi_tc)
 75{
 76	float depth = texture2D(depthGIMap, gi_tc).a;
 77	vec2 sc = gi_tc*2.0;
 78	sc -= vec2(1.0, 1.0);
 79	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
 80	vec4 pos = gi_inv_proj*ndc;
 81	pos.xyz /= pos.w;
 82	pos.w = 1.0;
 83	return pos;
 84}
 85
 86vec3 giAmbient(vec3 pos, vec3 norm)
 87{
 88	vec4 gi_c = gi_mat_proj * vec4(pos, 1.0);
 89	gi_c.xyz /= gi_c.w;
 90
 91	vec4 gi_pos = gi_mat*vec4(pos,1.0);
 92	vec3 gi_norm = (gi_norm_mat*vec4(norm,1.0)).xyz;
 93	gi_norm = normalize(gi_norm);
 94	
 95	vec4 c_spec = texture2DRect(specularRect, vary_fragcoord.xy);
 96	vec3 nz = texture2D(noiseMap, vary_fragcoord.xy/128.0).rgb;
 97	gi_pos.xyz += nz.x*gi_noise*gi_norm.xyz;
 98	vec2 tcx = gi_norm.xy;
 99	vec2 tcy = gi_norm.yx;
100	
101	vec4 eye_pos = gi_mat*vec4(0,0,0,1.0);
102	
103	vec3 eye_dir = normalize(gi_pos.xyz-eye_pos.xyz);
104	vec3 eye_ref = reflect(eye_dir, gi_norm);
105	
106	float da = 0.0; //texture2DRect(lightMap, vary_fragcoord.xy).r*0.5;
107	vec3 fdiff = vec3(da);
108	float fda = da;
109	
110	vec3 rcol = vec3(0,0,0);
111	
112	float fsa = 0.0;
113	
114
115	for (int i = -1; i <= 1; i += 2 )
116	{
117		for (int j = -1; j <= 1; j+= 2)
118		{
119			vec2 tc = vec2(i, j)*0.75+gi_norm.xy*nz.z;
120			tc += nz.xy*2.0;
121			tc *= gi_sample_width*0.25;
122			tc += gi_c.xy;
123			
124			vec3 lnorm = -(texture2D(normalGIMap, tc.xy).xyz*2.0-1.0);
125			vec3 lpos = getGIPosition(tc.xy).xyz;
126							
127			vec3 at = lpos-gi_pos.xyz;
128			float dist = length(at);
129			float dist_atten = clamp(1.0/(gi_attenuation*dist), 0.0, 1.0);
130			
131						
132			if (dist_atten > 0.01)
133			{ //possible contribution of indirect light to this surface
134				vec3 ldir = at;
135				
136				float ld = -dot(ldir, lnorm);
137				
138				if (ld < 0.0)
139				{  					
140					float ang_atten = dot(ldir, gi_norm);
141				
142					if (ang_atten > 0.0)
143					{  
144						vec4 spec = texture2D(specularGIMap, tc.xy);
145						at = normalize(at);
146						vec3 diff;		
147						
148						float da = 0.0;
149												
150						//contribution from indirect source to visible pixel
151						vec3 ha = at;
152						ha.z -= 1.0;
153						ha = normalize(ha);
154						if (spec.a > 0.0)
155						{
156							float sa = dot(ha,lnorm);
157							da = texture2D(lightFunc, vec2(sa, spec.a)).a;
158						}
159						else
160						{
161							da = -lnorm.z;
162						}
163						
164						diff = texture2D(diffuseGIMap, tc.xy).rgb+spec.rgb*spec.a*2.0;
165												
166						if (da > 0.0)
167						{ //contribution from visible pixel to eye
168							vec3 ha = normalize(at-eye_dir);
169							if (c_spec.a > 0.0)
170							{
171								float sa = dot(ha, gi_norm);
172								da = dist_atten*texture2D(lightFunc, vec2(sa, c_spec.a)).a;
173							}
174							else
175							{
176								da = dist_atten*dot(gi_norm, normalize(ldir));
177							}
178							fda += da;
179							fdiff += da*(c_spec.rgb*c_spec.a*2.0+vec3(1,1,1))*diff.rgb;
180						}
181					}
182				}
183			}
184		}
185	}
186
187	fdiff *= sunlight_color.rgb;
188	
189	vec3 ret = fda*fdiff;
190	
191	return clamp(ret,vec3(0.0), vec3(1.0));
192}
193
194void main() 
195{
196	vec2 pos_screen = vary_fragcoord.xy;
197	vec4 pos = getPosition(pos_screen);
198	
199	float rad = gi_range*0.5;
200	
201	vec3 norm = texture2DRect(normalMap, pos_screen).xyz;
202	norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
203	float dist = max(length(pos.xyz)-rad, 0.0);
204	
205	float da = clamp(1.0-dist/rad, 0.0, 1.0);
206	
207	vec3 ambient = da > 0.0 ? giAmbient(pos.xyz, norm) : vec3(0);
208	
209		
210	gl_FragData[0].xyz = mix(vec3(0), ambient, da);
211}