Advertisement
Guest User

shadow

a guest
Aug 31st, 2014
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. #ifndef SHADOW_H
  2. #define SHADOW_H
  3.  
  4. #include "common.h"
  5.  
  6. uniform sampler s_smap : register(ps,s0); // 2D/cube shadowmap
  7.  
  8. #define KERNEL .6f
  9. //#define USE_SJITTER
  10. //////////////////////////////////////////////////////////////////////////////////////////
  11. // software
  12. //////////////////////////////////////////////////////////////////////////////////////////
  13. half sample_sw (float2 tc, float2 shift, float depth_cmp)
  14. {
  15. static const float ts = KERNEL / float(SMAP_size);
  16. tc += shift*ts;
  17.  
  18. float texsize = SMAP_size;
  19. float offset = 0.5f/texsize;
  20. float2 Tex00 = tc + float2(-offset, -offset);
  21. float2 Tex01 = tc + float2(-offset, offset);
  22. float2 Tex10 = tc + float2( offset, -offset);
  23. float2 Tex11 = tc + float2( offset, offset);
  24. float4 depth = float4(
  25. depth_cmp-tex2D (s_smap, Tex00).x,
  26. depth_cmp-tex2D (s_smap, Tex01).x,
  27. depth_cmp-tex2D (s_smap, Tex10).x,
  28. depth_cmp-tex2D (s_smap, Tex11).x);
  29. half4 compare = step (depth,0);
  30. float2 fr = frac (Tex00*texsize);
  31. half2 ifr = half2 (1,1) - fr;
  32. half4 fr4 = half4 (ifr.x*ifr.y, ifr.x*fr.y, fr.x*ifr.y, fr.x*fr.y);
  33. return dot (compare, fr4);
  34. }
  35. half shadow_sw (float4 tc) {
  36. float2 tc_dw = tc.xy / tc.w;
  37. half4 s;
  38. s.x = sample_sw (tc_dw,float2(-1,-1),tc.z);
  39. s.y = sample_sw (tc_dw,float2(+1,-1),tc.z);
  40. s.z = sample_sw (tc_dw,float2(-1,+1),tc.z);
  41. s.w = sample_sw (tc_dw,float2(+1,+1),tc.z);
  42. return dot (s, 1.h/4.h);
  43. }
  44.  
  45. //////////////////////////////////////////////////////////////////////////////////////////
  46. // hardware + PCF
  47. //////////////////////////////////////////////////////////////////////////////////////////
  48. half sample_hw_pcf (float4 tc,float4 shift){
  49. static const float ts = KERNEL / float(SMAP_size);
  50. #ifndef SUNSHAFTS_DYNAMIC
  51. return tex2Dproj (s_smap,tc + tc.w*shift*ts).x;
  52. #else // SUNSHAFTS_DYNAMIC
  53. float4 tc2 = tc / tc.w + shift * ts;
  54. tc2.w = 0;
  55. return tex2Dlod(s_smap, tc2);
  56. #endif // SUNSHAFTS_DYNAMIC
  57. }
  58. half shadow_hw (float4 tc) {
  59. half s0 = sample_hw_pcf (tc,float4(-1,-1,0,0));
  60. half s1 = sample_hw_pcf (tc,float4(+1,-1,0,0));
  61. half s2 = sample_hw_pcf (tc,float4(-1,+1,0,0));
  62. half s3 = sample_hw_pcf (tc,float4(+1,+1,0,0));
  63.  
  64. return (s0+s1+s2+s3)/(4.h);
  65. }
  66.  
  67. //////////////////////////////////////////////////////////////////////////////////////////
  68. // hardware (ATI) + DF24/Fetch4
  69. //////////////////////////////////////////////////////////////////////////////////////////
  70.  
  71. /*
  72. half sample_hw_f4 (float4 tc,float4 shift){
  73. static const float ts = KERNEL / float(SMAP_size);
  74. float4 D4 = tex2Dproj (s_smap,tc + tc.w*shift*ts);
  75. float4 dcmp = tc.z/tc.w ;
  76. float4 cmp = dcmp<D4 ;
  77. return dot (cmp,1.h/4.h);
  78. }
  79. */
  80.  
  81. half sample_hw_f4 (float4 tc,float4 shift){
  82. static const float ts = KERNEL / float(SMAP_size);
  83. float4 T4 = tc/tc.w ;
  84. T4.xy += shift.xy*ts ;
  85.  
  86. float4 D4 = tex2D (s_smap, T4);
  87. float4 compare = T4.z<D4 ;
  88.  
  89. float texsize = SMAP_size ;
  90. float2 fr = frac (T4.xy * texsize);
  91. half2 ifr = half2 (1,1) - fr;
  92. half4 fr4 = half4 (ifr.x*ifr.y, ifr.x*fr.y, fr.x*ifr.y, fr.x*fr.y);
  93. half4 fr4s = fr4.zywx ;
  94.  
  95. return dot (compare, fr4s) ;
  96. // return dot (compare, 1.h/4.h) ;
  97. }
  98.  
  99.  
  100. half shadow_hw_f4 (float4 tc) {
  101. half s0 = sample_hw_f4 (tc,float4(-1,-1,0,0));
  102. half s1 = sample_hw_f4 (tc,float4(+1,-1,0,0));
  103. half s2 = sample_hw_f4 (tc,float4(-1,+1,0,0));
  104. half s3 = sample_hw_f4 (tc,float4(+1,+1,0,0));
  105. return (s0+s1+s2+s3)/4.h;
  106. }
  107.  
  108.  
  109.  
  110. //////////////////////////////////////////////////////////////////////////////////////////
  111. // testbed
  112.  
  113. uniform sampler2D jitter0;
  114. uniform sampler2D jitter1;
  115. uniform sampler2D jitter2;
  116. uniform sampler2D jitter3;
  117. uniform half4 jitterS;
  118. half4 test (float4 tc, half2 offset)
  119. {
  120. float4 tcx = float4 (tc.xy + tc.w*offset, tc.zw);
  121. return tex2Dproj (s_smap,tcx);
  122. }
  123.  
  124. half shadowtest (float4 tc, float4 tcJ) // jittered sampling
  125. {
  126. half4 r;
  127.  
  128. const float scale = (2.7f/float(SMAP_size));
  129. half4 J0 = tex2Dproj (jitter0,tcJ)*scale;
  130. half4 J1 = tex2Dproj (jitter1,tcJ)*scale;
  131.  
  132. r.x = test (tc,J0.xy).x;
  133. r.y = test (tc,J0.wz).y;
  134. r.z = test (tc,J1.xy).z;
  135. r.w = test (tc,J1.wz).x;
  136.  
  137. return dot(r,1.h/4.h);
  138. }
  139.  
  140. half shadowtest_sun (float4 tc, float4 tcJ) // jittered sampling
  141. {
  142. half4 r;
  143.  
  144. // const float scale = (2.0f/float(SMAP_size));
  145. const float scale = (0.7f/float(SMAP_size));
  146.  
  147.  
  148. float2 tc_J = frac(tc.xy/tc.w*SMAP_size/4.0f )*.5f;
  149. half4 J0 = tex2D (jitter0,tc_J)*scale;
  150. //half4 J1 = tex2D (jitter1,tc_J)*scale;
  151.  
  152. const float k = .5f/float(SMAP_size);
  153. r.x = test (tc, J0.xy+half2(-k,-k)).x;
  154. r.y = test (tc, J0.wz+half2( k,-k)).y;
  155. r.z = test (tc,-J0.xy+half2(-k, k)).z;
  156. r.w = test (tc,-J0.wz+half2( k, k)).x;
  157.  
  158. return dot(r,1.h/4.h);
  159. }
  160.  
  161. half shadow_high (float4 tc) // jittered sampling
  162. {
  163.  
  164. const float scale = (0.5f/float(SMAP_size));
  165.  
  166. float2 tc_J = frac(tc.xy/tc.w*SMAP_size/4.0f )*.5f;
  167. half4 J0 = tex2D (jitter0,tc_J)*scale;
  168.  
  169. const float k = 1.f/float(SMAP_size);
  170. half4 r;
  171. r.x = test (tc,J0.xy+half2(-k,-k)).x;
  172. r.y = test (tc,J0.wz+half2( k,-k)).y;
  173.  
  174. r.z = test (tc,J0.xy+half2(-k, k)).z;
  175. r.w = test (tc,J0.wz+half2( k, k)).x;
  176.  
  177.  
  178. const float k1 = 1.3f/float(SMAP_size);
  179. half4 r1;
  180. r1.x = test (tc,-J0.xy+half2(-k1,0)).x;
  181. r1.y = test (tc,-J0.wz+half2( 0,-k1)).y;
  182.  
  183. r1.z = test (tc,-2*J0.xy+half2( k1, 0)).z;
  184. r1.w = test (tc,-2*J0.wz+half2( 0, k1)).x;
  185.  
  186. return ( r.x + r.y + r.z + r.w + r1.x + r1.y + r1.z + r1.w )*1.h/8.h;
  187. }
  188.  
  189. //////////////////////////////////////////////////////////////////////////////////////////
  190. // select hardware or software shadowmaps
  191. //////////////////////////////////////////////////////////////////////////////////////////
  192. #ifdef USE_HWSMAP_PCF
  193. // D24X8+PCF
  194. half shadow (float4 tc) { return shadow_hw (tc); }
  195. #else
  196. #ifdef USE_FETCH4
  197. // DF24+Fetch4
  198. half shadow (float4 tc) { return shadow_hw_f4(tc); }
  199. #else
  200. // FP32
  201. half shadow (float4 tc) { return shadow_sw (tc); }
  202. #endif
  203. #endif
  204.  
  205.  
  206. #ifdef USE_HWSMAP_PCF
  207. // D24X8+PCF
  208. half shadow_volumetric (float4 tc) { return sample_hw_pcf ( tc, float4(0,0,0,0) ); }
  209. #else
  210. #ifdef USE_FETCH4
  211. // DF24+Fetch4
  212. half shadow_volumetric (float4 tc) { return sample_hw_f4 (tc, float4(0,0,0,0)); }
  213. #else
  214. // FP32
  215. half shadow_volumetric (float4 tc) { return sample_sw (tc.xy / tc.w,float2(0,0),tc.z); }
  216. #endif
  217. #endif
  218.  
  219. //////////////////////////////////////////////////////////////////////////////////////////
  220. #ifdef USE_SUNMASK
  221. uniform float3x4 m_sunmask ; // ortho-projection
  222. half sunmask (float4 P) { //
  223. float2 tc = mul (m_sunmask, P); //
  224. return tex2D (s_lmap,tc).w; // A8
  225.  
  226. }
  227. #else
  228. half sunmask (float4 P) { return 1.h; } //
  229. #endif
  230.  
  231. //////////////////////////////////////////////////////////////////////////////////////////
  232. uniform float4x4 m_shadow;
  233.  
  234. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement