Advertisement
Guest User

VSSAO2.fx

a guest
Aug 3rd, 2014
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.21 KB | None | 0 0
  1. /*
  2. -Volumetric SSAO-
  3. Implemented by Tomerk for OBGE
  4. Adapted and tweaked for Dark Souls by Durante
  5. Modified by Asmodean, for accurate occlusion distance, and dynamic positional depth offsets, for Dark Souls
  6. Modified further for inclusion in DSfix 2.2 (depth-dependent thickness model) and adapted for Dark Souls 2 by Durante. Will it ever stop?
  7. */
  8.  
  9. /***User-controlled variables***/
  10. #define N_SAMPLES 32 //number of samples, currently do not change.
  11.  
  12. extern float aoRadiusMultiplier = 0.2; //Linearly multiplies the radius of the AO Sampling
  13. extern float ThicknessModel = 6; //units in space the AO assumes objects' thicknesses are
  14. extern float FOV = 85; //Field of View in Degrees
  15. extern float luminosity_threshold = 0.5;
  16.  
  17. #ifndef SCALE
  18. #define SCALE 1.0
  19. #endif
  20.  
  21. #ifndef SSAO_STRENGTH_LOW
  22. #ifndef SSAO_STRENGTH_MEDIUM
  23. #ifndef SSAO_STRENGTH_HIGH
  24. #define SSAO_STRENGTH_MEDIUM 1
  25. #endif
  26. #endif
  27. #endif
  28.  
  29. #ifdef SSAO_STRENGTH_LOW
  30. extern float aoClamp = 0.6;
  31. extern float aoStrengthMultiplier = 0.9;
  32. #endif
  33.  
  34. #ifdef SSAO_STRENGTH_MEDIUM
  35. extern float aoClamp = 0.3;
  36. extern float aoStrengthMultiplier = 1.5;
  37. #endif
  38.  
  39. #ifdef SSAO_STRENGTH_HIGH
  40. extern float aoClamp = 0.1;
  41. extern float aoStrengthMultiplier = 2.0;
  42. #endif
  43.  
  44.  
  45. #define LUMINANCE_CONSIDERATION //comment this line to not take pixel brightness into account
  46.  
  47. /***End Of User-controlled Variables***/
  48. static float2 rcpres = PIXEL_SIZE;
  49. static float aspect = rcpres.y/rcpres.x;
  50. static const float nearZ = 0.4;
  51. static const float farZ = 10000.0;
  52. static const float2 g_InvFocalLen = { tan(0.5f*radians(FOV)) / rcpres.y * rcpres.x, tan(0.5f*radians(FOV)) };
  53.  
  54.  
  55. texture2D depthTex2D;
  56. sampler depthSampler = sampler_state
  57. {
  58. texture = <depthTex2D>;
  59. MinFilter = LINEAR;
  60. MagFilter = LINEAR;
  61. MipFilter = LINEAR;
  62. AddressU = Clamp;
  63. AddressV = Clamp;
  64. SRGBTexture=FALSE;
  65. };
  66.  
  67. texture2D sampleTex2D;
  68. sampler sampleSampler = sampler_state
  69. {
  70. Texture = <sampleTex2D>;
  71. MinFilter = LINEAR;
  72. MagFilter = LINEAR;
  73. MipFilter = LINEAR;
  74. AddressU = Clamp;
  75. AddressV = Clamp;
  76. SRGBTexture=FALSE;
  77. };
  78.  
  79. texture2D AOTex2D;
  80. sampler AOSampler = sampler_state
  81. {
  82. texture = <AOTex2D>;
  83. MinFilter = POINT;
  84. MagFilter = POINT;
  85. MipFilter = POINT;
  86. AddressU = Clamp;
  87. AddressV = Clamp;
  88. SRGBTexture=FALSE;
  89. };
  90.  
  91. texture2D frameTex2D;
  92. sampler frameSampler = sampler_state
  93. {
  94. texture = <frameTex2D>;
  95. MinFilter = LINEAR;
  96. MagFilter = LINEAR;
  97. MipFilter = LINEAR;
  98. AddressU = Clamp;
  99. AddressV = Clamp;
  100. SRGBTexture=FALSE;
  101. };
  102.  
  103. texture2D prevPassTex2D;
  104. sampler passSampler = sampler_state
  105. {
  106. texture = <prevPassTex2D>;
  107. MinFilter = LINEAR;
  108. MagFilter = LINEAR;
  109. MipFilter = LINEAR;
  110. AddressU = Clamp;
  111. AddressV = Clamp;
  112. SRGBTexture=FALSE;
  113. };
  114.  
  115. struct VSOUT
  116. {
  117. float4 vertPos : POSITION0;
  118. float2 UVCoord : TEXCOORD0;
  119. };
  120.  
  121. struct VSIN
  122. {
  123. float4 vertPos : POSITION0;
  124. float2 UVCoord : TEXCOORD0;
  125. };
  126.  
  127.  
  128. VSOUT FrameVS(VSIN IN)
  129. {
  130. VSOUT OUT;
  131. float4 pos=float4(IN.vertPos.x, IN.vertPos.y, IN.vertPos.z, 1.0f);
  132. OUT.vertPos=pos;
  133. float2 coord=float2(IN.UVCoord.x, IN.UVCoord.y);
  134. OUT.UVCoord=coord;
  135. return OUT;
  136. }
  137.  
  138. static float2 sample_offset[N_SAMPLES] =
  139. {
  140. float2(1.00f, 1.00f),
  141. float2(-1.00f, -1.00f),
  142. float2(-1.00f, 1.00f),
  143. float2(1.00f, -1.00f),
  144.  
  145. float2(1.00f, 0.00f),
  146. float2(-1.00f, 0.00f),
  147. float2(0.00f, 1.00f),
  148. float2(0.00f, -1.00f),
  149.  
  150. float2(1.00f, 0.00f),
  151. float2(-1.00f, 0.00f),
  152. float2(0.00f, 1.00f),
  153. float2(0.00f, -1.00f),
  154.  
  155. float2(1.00f, 1.00f),
  156. float2(-1.00f, -1.00f),
  157. float2(-1.00f, 1.00f),
  158. float2(1.00f, -1.00f),
  159.  
  160. float2(1.00f, 0.00f),
  161. float2(-1.00f, 0.00f),
  162. float2(0.00f, 1.00f),
  163. float2(0.00f, -1.00f),
  164.  
  165. float2(1.00f, 0.00f),
  166. float2(-1.00f, 0.00f),
  167. float2(0.00f, 1.00f),
  168. float2(0.00f, -1.00f),
  169.  
  170. float2(1.00f, 1.00f),
  171. float2(-1.00f, -1.00f),
  172. float2(-1.00f, 1.00f),
  173. float2(1.00f, -1.00f),
  174.  
  175. float2(1.00f, 1.00f),
  176. float2(-1.00f, -1.00f),
  177. float2(-1.00f, 1.00f),
  178. float2(1.00f, -1.00f)
  179. };
  180.  
  181. static float sample_radius[N_SAMPLES] =
  182. {
  183. 0.20f, 0.20f,
  184. 0.20f, 0.20f,
  185. 0.20f, 0.20f,
  186. 0.20f, 0.20f,
  187.  
  188. 0.20f, 0.20f,
  189. 0.20f, 0.20f,
  190. 0.20f, 0.20f,
  191. 0.20f, 0.20f,
  192.  
  193. 0.20f, 0.20f,
  194. 0.20f, 0.20f,
  195. 0.20f, 0.20f,
  196. 0.20f, 0.20f,
  197.  
  198. 0.20f, 0.20f,
  199. 0.20f, 0.20f,
  200. 0.20f, 0.20f,
  201. 0.20f, 0.20f
  202. };
  203.  
  204. float2 rand(in float2 uv : TEXCOORD0) {
  205. float noiseX = (frac(sin(dot(uv, float2(12.9898,78.233)*2.0)) * 43758.5453));
  206. float noiseY = sqrt(1-noiseX*noiseX);
  207. return float2(noiseX, noiseY);
  208. }
  209.  
  210. float readDepth(in float2 coord : TEXCOORD0)
  211. {
  212. float z = tex2D( depthSampler, coord ).r;
  213. float result = z/(max(1.0-z, 0.000000001));
  214. return result;
  215. }
  216.  
  217. float3 getPosition(in float2 uv : TEXCOORD0, in float eye_z : POSITION0) {
  218. uv = (uv * float2(2.0, -2.0) - float2(1.0, -1.0));
  219. float3 pos = float3(uv * g_InvFocalLen * eye_z, eye_z );
  220. return pos;
  221. }
  222.  
  223. float4 ssao_Main(VSOUT IN) : COLOR0
  224. {
  225. clip(1/SCALE-IN.UVCoord.x);
  226. clip(1/SCALE-IN.UVCoord.y);
  227. IN.UVCoord.xy *= SCALE;
  228.  
  229. float depth = readDepth(IN.UVCoord);
  230. float3 pos = getPosition(IN.UVCoord, depth);
  231. float3 dx = ddx(pos);
  232. float3 dy = ddy(pos);
  233. float3 norm = normalize(cross(dx,dy));
  234. norm.y *= -1;
  235.  
  236. float sample_depth;
  237.  
  238. float ao=tex2D(AOSampler, IN.UVCoord);
  239. float s=tex2D(sampleSampler, IN.UVCoord);
  240.  
  241. float2 rand_vec = rand(IN.UVCoord);
  242. float2 sample_vec_divisor = g_InvFocalLen*depth/(aoRadiusMultiplier*5000*rcpres);
  243. float2 sample_center = IN.UVCoord + norm.xy/sample_vec_divisor*float2(1.0f,aspect);
  244. float sample_center_depth = depth + norm.z*aoRadiusMultiplier*7;
  245.  
  246. for(int i = 0; i < N_SAMPLES; i++)
  247. {
  248. float2 sample_vec = reflect(sample_offset[i], rand_vec);
  249. sample_vec /= sample_vec_divisor;
  250. float2 sample_coords = sample_center + sample_vec*float2(1.0f,aspect);
  251.  
  252. float curr_sample_radius = sample_radius[i]*aoRadiusMultiplier*10;
  253. float curr_sample_depth = readDepth(sample_coords);
  254.  
  255. ao += clamp(0,curr_sample_radius+sample_center_depth-curr_sample_depth,2*curr_sample_radius);
  256. ao -= clamp(0,curr_sample_radius+sample_center_depth-curr_sample_depth-ThicknessModel,2*curr_sample_radius);
  257. s += 2.0*curr_sample_radius;
  258. }
  259.  
  260. ao /= s;
  261.  
  262. // adjust for close and far away
  263. if(depth<0.065f) ao = lerp(ao, 0.0f, (0.065f-depth)*13.3);
  264.  
  265. ao = 1.0f-ao*aoStrengthMultiplier;
  266.  
  267. return float4(ao,ao,ao,depth);
  268. }
  269.  
  270. #ifdef BLUR_SHARP
  271. float4 HBlur( VSOUT IN ) : COLOR0 {
  272. float4 sample = tex2D(passSampler, IN.UVCoord);
  273. float blurred = sample.r*2;
  274. float depth = sample.a;
  275. float divide = 2.0;
  276.  
  277. float4 left = tex2D(passSampler, IN.UVCoord - float2(rcpres.x, 0));
  278. float lpower = saturate(ThicknessModel - abs(left.a - depth));
  279. blurred += lpower*left.r;
  280. divide += lpower;
  281.  
  282. float4 right = tex2D(passSampler, IN.UVCoord + float2(rcpres.x, 0));
  283. float rpower = saturate(ThicknessModel - abs(right.a - depth));
  284. blurred += rpower*left.r;
  285. divide += rpower;
  286.  
  287. return blurred/divide;
  288. }
  289. float4 VBlur( VSOUT IN ) : COLOR0 {
  290. float4 sample = tex2D(passSampler, IN.UVCoord);
  291. float blurred = sample.r*2;
  292. float depth = sample.a;
  293. float divide = 2.0;
  294.  
  295. float4 top = tex2D(passSampler, IN.UVCoord - float2(0, rcpres.y));
  296. float tpower = saturate(ThicknessModel - abs(top.a - depth));
  297. blurred += tpower*top.r;
  298. divide += tpower;
  299.  
  300. float4 bottom = tex2D(passSampler, IN.UVCoord + float2(0, rcpres.y));
  301. float bpower = saturate(ThicknessModel - abs(bottom.a - depth));
  302. blurred += bpower*bottom.r;
  303. divide += bpower;
  304.  
  305. return blurred/divide;
  306. }
  307. #else // BLUR_GAUSSIAN
  308. float4 HBlur(VSOUT IN) : COLOR0 {
  309. float color = tex2D(passSampler, IN.UVCoord).r;
  310.  
  311. float blurred = color*0.2270270270;
  312. blurred += tex2D(passSampler, IN.UVCoord + float2(rcpres.x*1.3846153846, 0)).r * 0.3162162162;
  313. blurred += tex2D(passSampler, IN.UVCoord - float2(rcpres.x*1.3846153846, 0)).r * 0.3162162162;
  314. blurred += tex2D(passSampler, IN.UVCoord + float2(rcpres.x*3.2307692308, 0)).r * 0.0702702703;
  315. blurred += tex2D(passSampler, IN.UVCoord - float2(rcpres.x*3.2307692308, 0)).r * 0.0702702703;
  316.  
  317. return blurred;
  318. }
  319.  
  320. float4 VBlur(VSOUT IN) : COLOR0 {
  321. float color = tex2D(passSampler, IN.UVCoord).r;
  322.  
  323. float blurred = color*0.2270270270;
  324. blurred += tex2D(passSampler, IN.UVCoord + float2(0, rcpres.y*1.3846153846)).r * 0.3162162162;
  325. blurred += tex2D(passSampler, IN.UVCoord - float2(0, rcpres.y*1.3846153846)).r * 0.3162162162;
  326. blurred += tex2D(passSampler, IN.UVCoord + float2(0, rcpres.y*3.2307692308)).r * 0.0702702703;
  327. blurred += tex2D(passSampler, IN.UVCoord - float2(0, rcpres.y*3.2307692308)).r * 0.0702702703;
  328.  
  329. return blurred;
  330. }
  331. #endif // blur type
  332.  
  333. float4 Combine( VSOUT IN ) : COLOR0 {
  334. float4 color = tex2D(frameSampler, IN.UVCoord);
  335. float ao = tex2D(passSampler, IN.UVCoord/SCALE).r;
  336. ao = clamp(ao, aoClamp, 1.0);
  337.  
  338. #ifdef LUMINANCE_CONSIDERATION
  339. float luminance = (color.r*0.2125f)+(color.g*0.7154f)+(color.b*0.0721f);
  340. float white = 1.0f;
  341. float black = 0.0f;
  342.  
  343. luminance = clamp(max(black,luminance-luminosity_threshold)+max(black,luminance-luminosity_threshold)+max(black,luminance-luminosity_threshold),0.0,1.0);
  344. ao = lerp(ao,white,luminance);
  345. #endif
  346.  
  347. color.rgb *= ao;
  348.  
  349. return color;
  350. //return float4(ao,ao,ao,color.a);
  351. //return tex2D(frameSampler, IN.UVCoord);
  352. //return tex2D(depthSampler, IN.UVCoord);
  353. }
  354.  
  355. technique t0
  356. {
  357. pass p0
  358. {
  359. VertexShader = compile vs_3_0 FrameVS();
  360. PixelShader = compile ps_3_0 ssao_Main();
  361. ZENABLE = FALSE;
  362. COLORWRITEENABLE = 15;
  363. ALPHATESTENABLE = false;
  364. ALPHABLENDENABLE = false;
  365. CULLMODE = NONE;
  366. STENCILENABLE = false;
  367. }
  368. pass p1
  369. {
  370. VertexShader = compile vs_3_0 FrameVS();
  371. PixelShader = compile ps_3_0 HBlur();
  372. ZENABLE = FALSE;
  373. COLORWRITEENABLE = 15;
  374. ALPHATESTENABLE = false;
  375. ALPHABLENDENABLE = false;
  376. CULLMODE = NONE;
  377. STENCILENABLE = false;
  378. }
  379. pass p2
  380. {
  381. VertexShader = compile vs_3_0 FrameVS();
  382. PixelShader = compile ps_3_0 VBlur();
  383. ZENABLE = FALSE;
  384. COLORWRITEENABLE = 15;
  385. ALPHATESTENABLE = false;
  386. ALPHABLENDENABLE = false;
  387. CULLMODE = NONE;
  388. STENCILENABLE = false;
  389. }
  390. pass p3
  391. {
  392. VertexShader = compile vs_3_0 FrameVS();
  393. PixelShader = compile ps_3_0 Combine();
  394. ZENABLE = FALSE;
  395. COLORWRITEENABLE = 15;
  396. ALPHATESTENABLE = false;
  397. ALPHABLENDENABLE = false;
  398. CULLMODE = NONE;
  399. STENCILENABLE = false;
  400. }
  401. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement