summaryrefslogtreecommitdiff
path: root/indra/newview/app_settings/shaders/class3/deferred/giF.glsl
blob: 95913a502c4a852e3fc9ee7d1e3a18dad108cfd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/** 
 * @file giF.glsl
 *
 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2007, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */
 


#extension GL_ARB_texture_rectangle : enable

uniform sampler2DRect depthMap;
uniform sampler2DRect normalMap;
uniform sampler2DRect lightMap;
uniform sampler2DRect specularRect;

uniform sampler2D noiseMap;

uniform sampler2D		diffuseGIMap;
uniform sampler2D		specularGIMap;
uniform sampler2D		normalGIMap;
uniform sampler2D		depthGIMap;

uniform sampler2D		lightFunc;

// Inputs
varying vec2 vary_fragcoord;

uniform vec2 screen_res;

uniform vec4 sunlight_color;

uniform mat4 inv_proj;
uniform mat4 gi_mat;  //gPipeline.mGIMatrix - eye space to sun space
uniform mat4 gi_mat_proj; //gPipeline.mGIMatrixProj - eye space to projected sun space
uniform mat4 gi_norm_mat; //gPipeline.mGINormalMatrix - eye space normal to sun space normal matrix
uniform mat4 gi_inv_proj; //gPipeline.mGIInvProj - projected sun space to sun space
uniform float gi_sample_width;
uniform float gi_noise;
uniform float gi_attenuation;
uniform float gi_range;

vec4 getPosition(vec2 pos_screen)
{
	float depth = texture2DRect(depthMap, pos_screen.xy).a;
	vec2 sc = pos_screen.xy*2.0;
	sc /= screen_res;
	sc -= vec2(1.0,1.0);
	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
	vec4 pos = inv_proj * ndc;
	pos /= pos.w;
	pos.w = 1.0;
	return pos;
}

vec4 getGIPosition(vec2 gi_tc)
{
	float depth = texture2D(depthGIMap, gi_tc).a;
	vec2 sc = gi_tc*2.0;
	sc -= vec2(1.0, 1.0);
	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
	vec4 pos = gi_inv_proj*ndc;
	pos.xyz /= pos.w;
	pos.w = 1.0;
	return pos;
}

vec3 giAmbient(vec3 pos, vec3 norm)
{
	vec4 gi_c = gi_mat_proj * vec4(pos, 1.0);
	gi_c.xyz /= gi_c.w;

	vec4 gi_pos = gi_mat*vec4(pos,1.0);
	vec3 gi_norm = (gi_norm_mat*vec4(norm,1.0)).xyz;
	gi_norm = normalize(gi_norm);
	
	vec4 c_spec = texture2DRect(specularRect, vary_fragcoord.xy);
	vec3 nz = texture2D(noiseMap, vary_fragcoord.xy/128.0).rgb;
	gi_pos.xyz += nz.x*gi_noise*gi_norm.xyz;
	vec2 tcx = gi_norm.xy;
	vec2 tcy = gi_norm.yx;
	
	vec4 eye_pos = gi_mat*vec4(0,0,0,1.0);
	
	vec3 eye_dir = normalize(gi_pos.xyz-eye_pos.xyz);
	vec3 eye_ref = reflect(eye_dir, gi_norm);
	
	float da = 0.0; //texture2DRect(lightMap, vary_fragcoord.xy).r*0.5;
	vec3 fdiff = vec3(da);
	float fda = da;
	
	vec3 rcol = vec3(0,0,0);
	
	float fsa = 0.0;
	

	for (int i = -1; i <= 1; i += 2 )
	{
		for (int j = -1; j <= 1; j+= 2)
		{
			vec2 tc = vec2(i, j)*0.75+gi_norm.xy*nz.z;
			tc += nz.xy*2.0;
			tc *= gi_sample_width*0.25;
			tc += gi_c.xy;
			
			vec3 lnorm = -(texture2D(normalGIMap, tc.xy).xyz*2.0-1.0);
			vec3 lpos = getGIPosition(tc.xy).xyz;
							
			vec3 at = lpos-gi_pos.xyz;
			float dist = length(at);
			float dist_atten = clamp(1.0/(gi_attenuation*dist), 0.0, 1.0);
			
						
			if (dist_atten > 0.01)
			{ //possible contribution of indirect light to this surface
				vec3 ldir = at;
				
				float ld = -dot(ldir, lnorm);
				
				if (ld < 0.0)
				{  					
					float ang_atten = dot(ldir, gi_norm);
				
					if (ang_atten > 0.0)
					{  
						vec4 spec = texture2D(specularGIMap, tc.xy);
						at = normalize(at);
						vec3 diff;		
						
						float da = 0.0;
												
						//contribution from indirect source to visible pixel
						vec3 ha = at;
						ha.z -= 1.0;
						ha = normalize(ha);
						if (spec.a > 0.0)
						{
							float sa = dot(ha,lnorm);
							da = texture2D(lightFunc, vec2(sa, spec.a)).a;
						}
						else
						{
							da = -lnorm.z;
						}
						
						diff = texture2D(diffuseGIMap, tc.xy).rgb+spec.rgb*spec.a*2.0;
												
						if (da > 0.0)
						{ //contribution from visible pixel to eye
							vec3 ha = normalize(at-eye_dir);
							if (c_spec.a > 0.0)
							{
								float sa = dot(ha, gi_norm);
								da = dist_atten*texture2D(lightFunc, vec2(sa, c_spec.a)).a;
							}
							else
							{
								da = dist_atten*dot(gi_norm, normalize(ldir));
							}
							fda += da;
							fdiff += da*(c_spec.rgb*c_spec.a*2.0+vec3(1,1,1))*diff.rgb;
						}
					}
				}
			}
		}
	}

	fdiff *= sunlight_color.rgb;
	
	vec3 ret = fda*fdiff;
	
	return clamp(ret,vec3(0.0), vec3(1.0));
}

void main() 
{
	vec2 pos_screen = vary_fragcoord.xy;
	vec4 pos = getPosition(pos_screen);
	
	float rad = gi_range*0.5;
	
	vec3 norm = texture2DRect(normalMap, pos_screen).xyz;
	norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
	float dist = max(length(pos.xyz)-rad, 0.0);
	
	float da = clamp(1.0-dist/rad, 0.0, 1.0);
	
	vec3 ambient = da > 0.0 ? giAmbient(pos.xyz, norm) : vec3(0);
	
		
	gl_FragData[0].xyz = mix(vec3(0), ambient, da);
}