PageRenderTime 293ms CodeModel.GetById 160ms app.highlight 2ms RepoModel.GetById 129ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 188 lines | 153 code | 35 blank | 0 comment | 0 complexity | 1d2948605284e8843b1078ca676d8254 MD5 | raw file
  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#extension GL_ARB_texture_rectangle : enable
 27
 28#ifdef DEFINE_GL_FRAGCOLOR
 29out vec4 gl_FragColor;
 30#endif
 31
 32uniform sampler2DRect depthMap;
 33uniform sampler2DRect normalMap;
 34uniform sampler2D noiseMap;
 35
 36uniform sampler2D		diffuseGIMap;
 37uniform sampler2D		normalGIMap;
 38uniform sampler2D		depthGIMap;
 39
 40uniform sampler2D		lightFunc;
 41
 42// Inputs
 43VARYING vec2 vary_fragcoord;
 44
 45uniform vec2 screen_res;
 46
 47uniform mat4 inv_proj;
 48uniform mat4 gi_mat;  //gPipeline.mGIMatrix - eye space to sun space
 49uniform mat4 gi_mat_proj; //gPipeline.mGIMatrixProj - eye space to projected sun space
 50uniform mat4 gi_norm_mat; //gPipeline.mGINormalMatrix - eye space normal to sun space normal matrix
 51uniform mat4 gi_inv_proj; //gPipeline.mGIInvProj - projected sun space to sun space
 52uniform float gi_radius;
 53uniform float gi_intensity;
 54uniform int gi_samples;
 55uniform vec2 gi_kern[25];
 56uniform vec2 gi_scale;
 57uniform vec3 gi_quad;
 58uniform vec3 gi_spec;
 59uniform float gi_direction_weight;
 60uniform float gi_light_offset;
 61
 62vec4 getPosition(vec2 pos_screen)
 63{
 64	float depth = texture2DRect(depthMap, pos_screen.xy).a;
 65	vec2 sc = pos_screen.xy*2.0;
 66	sc /= screen_res;
 67	sc -= vec2(1.0,1.0);
 68	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
 69	vec4 pos = inv_proj * ndc;
 70	pos /= pos.w;
 71	pos.w = 1.0;
 72	return pos;
 73}
 74
 75vec4 getGIPosition(vec2 gi_tc)
 76{
 77	float depth = texture2D(depthGIMap, gi_tc).a;
 78	vec2 sc = gi_tc*2.0;
 79	sc -= vec2(1.0, 1.0);
 80	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
 81	vec4 pos = gi_inv_proj*ndc;
 82	pos.xyz /= pos.w;
 83	pos.w = 1.0;
 84	return pos;
 85}
 86
 87vec3 giAmbient(vec3 pos, vec3 norm)
 88{
 89	vec4 gi_c = gi_mat_proj * vec4(pos, 1.0);
 90	gi_c.xyz /= gi_c.w;
 91
 92	vec4 gi_pos = gi_mat*vec4(pos,1.0);
 93	vec3 gi_norm = (gi_norm_mat*vec4(norm,1.0)).xyz;
 94	gi_norm = normalize(gi_norm);
 95	
 96	vec2 tcx = gi_norm.xy;
 97	vec2 tcy = gi_norm.yx;
 98	
 99	vec4 eye_pos = gi_mat*vec4(0,0,0,1.0);
100	
101	vec3 eye_dir = normalize(gi_pos.xyz-eye_pos.xyz/eye_pos.w);
102	
103	//vec3 eye_dir = vec3(0,0,-1);
104	//eye_dir = (gi_norm_mat*vec4(eye_dir, 1.0)).xyz;
105	//eye_dir = normalize(eye_dir);
106	
107	//float round_x = gi_scale.x;
108	//float round_y = gi_scale.y;
109	
110	vec3 debug = texture2D(normalGIMap, gi_c.xy).rgb*0.5+0.5;
111	debug.xz = vec2(0.0,0.0);
112	//debug = fract(debug);
113	
114	float round_x = 1.0/64.0;
115	float round_y = 1.0/64.0;
116	
117	//gi_c.x = floor(gi_c.x/round_x+0.5)*round_x;
118	//gi_c.y = floor(gi_c.y/round_y+0.5)*round_y;
119	
120	float fda = 0.0;
121	vec3 fdiff = vec3(0,0,0);
122	
123	vec3 rcol = vec3(0,0,0);
124	
125	float fsa = 0.0;
126	
127	for (int i = -1; i < 2; i+=2 )
128	{
129		for (int j = -1; j < 2; j+=2)
130		{
131			vec2 tc = vec2(i, j)*0.75;
132			vec3 nz = texture2D(noiseMap, vary_fragcoord.xy/128.0+tc*0.5).xyz;
133			//tc += gi_norm.xy*nz.z;
134			tc += nz.xy*2.0;
135			tc /= gi_samples;
136			tc += gi_c.xy;
137			
138			vec3 lnorm = -normalize(texture2D(normalGIMap, tc.xy).xyz*2.0-1.0);
139			vec3 lpos = getGIPosition(tc.xy).xyz;
140							
141			vec3 at = lpos-gi_pos.xyz;
142			float dist = dot(at,at);
143			float da = clamp(1.0/(gi_spec.x*dist), 0.0, 1.0);
144			
145			if (da > 0.0)
146			{
147				//add angular attenuation
148				vec3 ldir = at;
149				float ang_atten = clamp(dot(ldir, gi_norm), 0.0, 1.0);
150			
151				float ld = -dot(ldir, lnorm);
152				
153				if (ang_atten > 0.0 && ld < 0.0)
154				{
155					vec3 diff = texture2D(diffuseGIMap, tc.xy).xyz;
156					da = da*ang_atten;
157					fda += da;
158					fdiff += diff*da;
159				}
160			}
161		}
162	}
163
164	fdiff /= max(gi_spec.y*fda, gi_quad.z);
165	fdiff = clamp(fdiff, vec3(0), vec3(1));
166	
167	vec3 ret = fda*fdiff;
168	//ret = ret*ret*gi_quad.x+ret*gi_quad.y+gi_quad.z;			
169
170	//fda *= nz.z;
171	
172	//rcol.rgb *= gi_intensity;
173	//return rcol.rgb+vary_AmblitColor.rgb*0.25;
174	//return vec4(debug, 0.0);
175	//return vec4(fda*fdiff, 0.0);
176	return clamp(ret,vec3(0.0), vec3(1.0));
177	//return debug.xyz;
178}
179
180void main() 
181{
182	vec2 pos_screen = vary_fragcoord.xy;
183	vec4 pos = getPosition(pos_screen);
184	vec3 norm = texture2DRect(normalMap, pos_screen).xyz;
185	norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
186	
187	gl_FragColor.xyz = giAmbient(pos, norm);
188}