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