Advertisement
Guest User

shadow.h

a guest
Jan 3rd, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 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. /*
  60. Modded by cj ayho (dez0wave team). 29.08.2010. added soft shadows
  61. version 1.1 by cj ayho (dez0wave team) 13.04.2011 - optimization
  62.  
  63. <mod>
  64. */
  65.  
  66. half4 s0;
  67.  
  68. int n = 3;
  69.  
  70. for( int i = 0; i < n; i++ )
  71. {
  72.  
  73. float3 a1 = float3( 0, +i, -i );
  74. float3 a2 = float3( 0, +i + 1, -i - 1 );
  75. float3 a3 = float3( 0, +i + 2, -i - 2 );
  76.  
  77. s0.x += sample_hw_pcf ( tc, a1.zzxx );
  78. s0.y += sample_hw_pcf ( tc, a1.yzxx );
  79. s0.z += sample_hw_pcf ( tc, a1.zyxx );
  80. s0.w += sample_hw_pcf ( tc, a1.yyxx );
  81.  
  82. s0.x += sample_hw_pcf ( tc, a1.xyxx );
  83. s0.y += sample_hw_pcf ( tc, a1.xzxx );
  84. s0.z += sample_hw_pcf ( tc, a1.yxxx );
  85. s0.w += sample_hw_pcf ( tc, a1.zxxx );
  86.  
  87. s0.x += sample_hw_pcf ( tc, a2.zzxx );
  88. s0.y += sample_hw_pcf ( tc, a2.yzxx );
  89. s0.z += sample_hw_pcf ( tc, a2.zyxx );
  90. s0.w += sample_hw_pcf ( tc, a2.yyxx );
  91.  
  92. s0.x += sample_hw_pcf ( tc, a2.xyxx );
  93. s0.y += sample_hw_pcf ( tc, a2.xzxx );
  94. s0.z += sample_hw_pcf ( tc, a2.yxxx );
  95. s0.w += sample_hw_pcf ( tc, a2.zxxx );
  96.  
  97. s0.x += sample_hw_pcf ( tc, a3.zzxx );
  98. s0.y += sample_hw_pcf ( tc, a3.yzxx );
  99. s0.z += sample_hw_pcf ( tc, a3.zyxx );
  100. s0.w += sample_hw_pcf ( tc, a3.yyxx );
  101.  
  102. s0.x += sample_hw_pcf ( tc, a3.xyxx );
  103. s0.y += sample_hw_pcf ( tc, a3.xzxx );
  104. s0.z += sample_hw_pcf ( tc, a3.yxxx );
  105. s0.w += sample_hw_pcf ( tc, a3.zxxx );
  106.  
  107. }
  108.  
  109. return dot ( s0, 1.f / ( 24 * n ) );
  110. /*
  111. </mod>
  112. */
  113. }
  114.  
  115. //////////////////////////////////////////////////////////////////////////////////////////
  116. // hardware (ATI) + DF24/Fetch4
  117. //////////////////////////////////////////////////////////////////////////////////////////
  118.  
  119. /*
  120. half sample_hw_f4 (float4 tc,float4 shift){
  121. static const float ts = KERNEL / float(SMAP_size);
  122. float4 D4 = tex2Dproj (s_smap,tc + tc.w*shift*ts);
  123. float4 dcmp = tc.z/tc.w ;
  124. float4 cmp = dcmp<D4 ;
  125. return dot (cmp,1.h/4.h);
  126. }
  127. */
  128.  
  129. half sample_hw_f4 (float4 tc,float4 shift){
  130. static const float ts = KERNEL / float(SMAP_size);
  131. float4 T4 = tc/tc.w ;
  132. T4.xy += shift.xy*ts ;
  133.  
  134. float4 D4 = tex2D (s_smap, T4);
  135. float4 compare = T4.z<D4 ;
  136.  
  137. float texsize = SMAP_size ;
  138. float2 fr = frac (T4.xy * texsize);
  139. half2 ifr = half2 (1,1) - fr;
  140. half4 fr4 = half4 (ifr.x*ifr.y, ifr.x*fr.y, fr.x*ifr.y, fr.x*fr.y);
  141. half4 fr4s = fr4.zywx ;
  142.  
  143. return dot (compare, fr4s) ;
  144. // return dot (compare, 1.h/4.h) ;
  145. }
  146.  
  147.  
  148. half shadow_hw_f4 (float4 tc) {
  149. half s0 = sample_hw_f4 (tc,float4(-1,-1,0,0));
  150. half s1 = sample_hw_f4 (tc,float4(+1,-1,0,0));
  151. half s2 = sample_hw_f4 (tc,float4(-1,+1,0,0));
  152. half s3 = sample_hw_f4 (tc,float4(+1,+1,0,0));
  153. return (s0+s1+s2+s3)/4.h;
  154. }
  155.  
  156.  
  157.  
  158. //////////////////////////////////////////////////////////////////////////////////////////
  159. // testbed
  160.  
  161. uniform sampler2D jitter0;
  162. uniform sampler2D jitter1;
  163. uniform sampler2D jitter2;
  164. uniform sampler2D jitter3;
  165. uniform half4 jitterS;
  166. half4 test (float4 tc, half2 offset)
  167. {
  168. float4 tcx = float4 (tc.xy + tc.w*offset, tc.zw);
  169. return tex2Dproj (s_smap,tcx);
  170. }
  171.  
  172. half shadowtest (float4 tc, float4 tcJ) // jittered sampling
  173. {
  174. half4 r;
  175.  
  176. const float scale = (2.7f/float(SMAP_size));
  177. half4 J0 = tex2Dproj (jitter0,tcJ)*scale;
  178. half4 J1 = tex2Dproj (jitter1,tcJ)*scale;
  179.  
  180. r.x = test (tc,J0.xy).x;
  181. r.y = test (tc,J0.wz).y;
  182. r.z = test (tc,J1.xy).z;
  183. r.w = test (tc,J1.wz).x;
  184.  
  185. return dot(r,1.h/4.h);
  186. }
  187.  
  188. half shadowtest_sun (float4 tc, float4 tcJ) // jittered sampling
  189. {
  190. half4 r;
  191.  
  192. // const float scale = (2.0f/float(SMAP_size));
  193. const float scale = (0.7f/float(SMAP_size));
  194.  
  195.  
  196. float2 tc_J = frac(tc.xy/tc.w*SMAP_size/4.0f )*.5f;
  197. half4 J0 = tex2D (jitter0,tc_J)*scale;
  198. //half4 J1 = tex2D (jitter1,tc_J)*scale;
  199.  
  200. const float k = .5f/float(SMAP_size);
  201. r.x = test (tc, J0.xy+half2(-k,-k)).x;
  202. r.y = test (tc, J0.wz+half2( k,-k)).y;
  203. r.z = test (tc,-J0.xy+half2(-k, k)).z;
  204. r.w = test (tc,-J0.wz+half2( k, k)).x;
  205.  
  206. return dot(r,1.h/4.h);
  207. }
  208.  
  209. half shadow_high (float4 tc) // jittered sampling
  210. {
  211.  
  212. const float scale = (0.5f/float(SMAP_size));
  213.  
  214. float2 tc_J = frac(tc.xy/tc.w*SMAP_size/4.0f )*.5f;
  215. half4 J0 = tex2D (jitter0,tc_J)*scale;
  216.  
  217. const float k = 1.f/float(SMAP_size);
  218. half4 r;
  219. r.x = test (tc,J0.xy+half2(-k,-k)).x;
  220. r.y = test (tc,J0.wz+half2( k,-k)).y;
  221.  
  222. r.z = test (tc,J0.xy+half2(-k, k)).z;
  223. r.w = test (tc,J0.wz+half2( k, k)).x;
  224.  
  225.  
  226. const float k1 = 1.3f/float(SMAP_size);
  227. half4 r1;
  228. r1.x = test (tc,-J0.xy+half2(-k1,0)).x;
  229. r1.y = test (tc,-J0.wz+half2( 0,-k1)).y;
  230.  
  231. r1.z = test (tc,-2*J0.xy+half2( k1, 0)).z;
  232. r1.w = test (tc,-2*J0.wz+half2( 0, k1)).x;
  233.  
  234. return ( r.x + r.y + r.z + r.w + r1.x + r1.y + r1.z + r1.w )*1.h/8.h;
  235. }
  236.  
  237. //////////////////////////////////////////////////////////////////////////////////////////
  238. // select hardware or software shadowmaps
  239. //////////////////////////////////////////////////////////////////////////////////////////
  240. #ifdef USE_HWSMAP_PCF
  241. // D24X8+PCF
  242. half shadow (float4 tc) { return shadow_hw (tc); }
  243. #else
  244. #ifdef USE_FETCH4
  245. // DF24+Fetch4
  246. half shadow (float4 tc) { return shadow_hw_f4(tc); }
  247. #else
  248. // FP32
  249. half shadow (float4 tc) { return shadow_sw (tc); }
  250. #endif
  251. #endif
  252.  
  253.  
  254. #ifdef USE_HWSMAP_PCF
  255. // D24X8+PCF
  256. half shadow_volumetric (float4 tc) { return sample_hw_pcf ( tc, float4(0,0,0,0) ); }
  257. #else
  258. #ifdef USE_FETCH4
  259. // DF24+Fetch4
  260. half shadow_volumetric (float4 tc) { return sample_hw_f4 (tc, float4(0,0,0,0)); }
  261. #else
  262. // FP32
  263. half shadow_volumetric (float4 tc) { return sample_sw (tc.xy / tc.w,float2(0,0),tc.z); }
  264. #endif
  265. #endif
  266.  
  267. //////////////////////////////////////////////////////////////////////////////////////////
  268. #ifdef USE_SUNMASK
  269. uniform float3x4 m_sunmask ; // ortho-projection
  270. half sunmask (float4 P) { //
  271. float2 tc = mul (m_sunmask, P); //
  272. return tex2D (s_lmap,tc).w; // A8
  273.  
  274. }
  275. #else
  276. half sunmask (float4 P) { return 1.h; } //
  277. #endif
  278.  
  279. //////////////////////////////////////////////////////////////////////////////////////////
  280. uniform float4x4 m_shadow;
  281.  
  282. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement