Advertisement
Guest User

VSSAO2.fx

a guest
Aug 3rd, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.48 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 = 3.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. static const float depthRange = nearZ-farZ;
  54. static const float Zmul = nearZ*farZ;
  55.  
  56.  
  57. texture2D depthTex2D;
  58. sampler depthSampler = sampler_state
  59. {
  60. texture = <depthTex2D>;
  61. MinFilter = POINT;
  62. MagFilter = POINT;
  63. MipFilter = POINT;
  64. AddressU = Mirror;
  65. AddressV = Mirror;
  66. SRGBTexture=FALSE;
  67. };
  68.  
  69. texture2D sampleTex2D;
  70. sampler sampleSampler = sampler_state
  71. {
  72. Texture = <sampleTex2D>;
  73. MinFilter = LINEAR;
  74. MagFilter = LINEAR;
  75. MipFilter = LINEAR;
  76. AddressU = Clamp;
  77. AddressV = Clamp;
  78. SRGBTexture=FALSE;
  79. };
  80.  
  81. texture2D AOTex2D;
  82. sampler AOSampler = sampler_state
  83. {
  84. texture = <AOTex2D>;
  85. MinFilter = POINT;
  86. MagFilter = POINT;
  87. MipFilter = POINT;
  88. AddressU = Clamp;
  89. AddressV = Clamp;
  90. SRGBTexture=FALSE;
  91. };
  92.  
  93. texture2D frameTex2D;
  94. sampler frameSampler = sampler_state
  95. {
  96. texture = <frameTex2D>;
  97. MinFilter = LINEAR;
  98. MagFilter = LINEAR;
  99. MipFilter = LINEAR;
  100. AddressU = Clamp;
  101. AddressV = Clamp;
  102. SRGBTexture=FALSE;
  103. };
  104.  
  105. texture2D prevPassTex2D;
  106. sampler passSampler = sampler_state
  107. {
  108. texture = <prevPassTex2D>;
  109. MinFilter = LINEAR;
  110. MagFilter = LINEAR;
  111. MipFilter = LINEAR;
  112. AddressU = Clamp;
  113. AddressV = Clamp;
  114. SRGBTexture=FALSE;
  115. };
  116.  
  117. struct VSOUT
  118. {
  119. float4 vertPos : POSITION0;
  120. float2 UVCoord : TEXCOORD0;
  121. };
  122.  
  123. struct VSIN
  124. {
  125. float4 vertPos : POSITION0;
  126. float2 UVCoord : TEXCOORD0;
  127. };
  128.  
  129.  
  130. VSOUT FrameVS(VSIN IN)
  131. {
  132. VSOUT OUT;
  133. float4 pos=float4(IN.vertPos.x, IN.vertPos.y, IN.vertPos.z, 1.0f);
  134. OUT.vertPos=pos;
  135. float2 coord=float2(IN.UVCoord.x, IN.UVCoord.y);
  136. OUT.UVCoord=coord;
  137. return OUT;
  138. }
  139.  
  140. static float2 sample_offset[N_SAMPLES] =
  141. {
  142. float2(1.00f, 1.00f),
  143. float2(-1.00f, -1.00f),
  144. float2(-1.00f, 1.00f),
  145. float2(1.00f, -1.00f),
  146.  
  147. float2(1.00f, 0.00f),
  148. float2(-1.00f, 0.00f),
  149. float2(0.00f, 1.00f),
  150. float2(0.00f, -1.00f),
  151.  
  152. float2(1.00f, 0.00f),
  153. float2(-1.00f, 0.00f),
  154. float2(0.00f, 1.00f),
  155. float2(0.00f, -1.00f),
  156.  
  157. float2(1.00f, 1.00f),
  158. float2(-1.00f, -1.00f),
  159. float2(-1.00f, 1.00f),
  160. float2(1.00f, -1.00f),
  161.  
  162. float2(1.00f, 0.00f),
  163. float2(-1.00f, 0.00f),
  164. float2(0.00f, 1.00f),
  165. float2(0.00f, -1.00f),
  166.  
  167. float2(1.00f, 0.00f),
  168. float2(-1.00f, 0.00f),
  169. float2(0.00f, 1.00f),
  170. float2(0.00f, -1.00f),
  171.  
  172. float2(1.00f, 1.00f),
  173. float2(-1.00f, -1.00f),
  174. float2(-1.00f, 1.00f),
  175. float2(1.00f, -1.00f),
  176.  
  177. float2(1.00f, 1.00f),
  178. float2(-1.00f, -1.00f),
  179. float2(-1.00f, 1.00f),
  180. float2(1.00f, -1.00f)
  181. };
  182.  
  183. static float sample_radius[N_SAMPLES] =
  184. {
  185. 0.20f, 0.20f,
  186. 0.20f, 0.20f,
  187. 0.20f, 0.20f,
  188. 0.20f, 0.20f,
  189.  
  190. 0.20f, 0.20f,
  191. 0.20f, 0.20f,
  192. 0.20f, 0.20f,
  193. 0.20f, 0.20f,
  194.  
  195. 0.20f, 0.20f,
  196. 0.20f, 0.20f,
  197. 0.20f, 0.20f,
  198. 0.20f, 0.20f,
  199.  
  200. 0.20f, 0.20f,
  201. 0.20f, 0.20f,
  202. 0.20f, 0.20f,
  203. 0.20f, 0.20f
  204. };
  205.  
  206. float2 rand(in float2 uv : TEXCOORD0) {
  207. float noiseX = (frac(sin(dot(uv, float2(12.9898,78.233)*2.0)) * 43758.5453));
  208. float noiseY = sqrt(1-noiseX*noiseX);
  209. return float2(noiseX, noiseY);
  210. }
  211.  
  212. float readDepth(in float2 coord : TEXCOORD0)
  213. {
  214. //return tex2D(depthSampler, coord).r;
  215. float z = tex2D( depthSampler, coord ).r;
  216. return( ( 2.0 * nearZ ) / ( farZ + nearZ - z * ( farZ - nearZ ) ) );
  217. //float posZ = tex2D(depthSampler, coord).r;
  218. //posZ = Zmul / ((posZ * depthRange) - farZ);
  219. //return posZ;
  220. }
  221.  
  222. float3 getPosition(in float2 uv : TEXCOORD0, in float eye_z : POSITION0) {
  223. uv = (uv * float2(2.0, -2.0) - float2(1.0, -1.0));
  224. float3 pos = float3(uv * g_InvFocalLen * eye_z, eye_z );
  225. return pos;
  226. }
  227.  
  228. float4 ssao_Main(VSOUT IN) : COLOR0
  229. {
  230. clip(1/SCALE-IN.UVCoord.x);
  231. clip(1/SCALE-IN.UVCoord.y);
  232. IN.UVCoord.xy *= SCALE;
  233.  
  234. float depth = readDepth(IN.UVCoord);
  235. float3 pos = getPosition(IN.UVCoord, depth);
  236. float3 dx = ddx(pos);
  237. float3 dy = ddy(pos);
  238. float3 norm = normalize(cross(dx,dy));
  239. norm.y *= -1;
  240.  
  241. float sample_depth;
  242.  
  243. float ao=tex2D(AOSampler, IN.UVCoord);
  244. float s=tex2D(sampleSampler, IN.UVCoord);
  245.  
  246. float2 rand_vec = rand(IN.UVCoord);
  247. float2 sample_vec_divisor = g_InvFocalLen*depth*depthRange/(aoRadiusMultiplier*5000*rcpres);
  248. float2 sample_center = IN.UVCoord + norm.xy/sample_vec_divisor*float2(1.0f,aspect);
  249. float sample_center_depth = depth*depthRange + norm.z*aoRadiusMultiplier*7;
  250.  
  251. for(int i = 0; i < N_SAMPLES; i++)
  252. {
  253. float2 sample_vec = reflect(sample_offset[i], rand_vec);
  254. sample_vec /= sample_vec_divisor;
  255. float2 sample_coords = sample_center + sample_vec*float2(1.0f,aspect);
  256.  
  257. float curr_sample_radius = sample_radius[i]*aoRadiusMultiplier*10;
  258. float curr_sample_depth = depthRange*readDepth(sample_coords);
  259.  
  260. ao += clamp(0,curr_sample_radius+sample_center_depth-curr_sample_depth,2*curr_sample_radius);
  261. ao -= clamp(0,curr_sample_radius+sample_center_depth-curr_sample_depth-ThicknessModel,2*curr_sample_radius);
  262. s += 2.0*curr_sample_radius;
  263. }
  264.  
  265. ao /= s;
  266.  
  267. // adjust for close and far away
  268. if(depth<0.065f) ao = lerp(ao, 0.0f, (0.065f-depth)*13.3);
  269.  
  270. ao = 1.0f-ao*aoStrengthMultiplier;
  271.  
  272. return float4(ao,ao,ao,depth);
  273. }
  274.  
  275. #ifdef BLUR_SHARP
  276. float4 HBlur( VSOUT IN ) : COLOR0 {
  277. float4 sample = tex2D(passSampler, IN.UVCoord);
  278. float blurred = sample.r*2;
  279. float depth = sample.a;
  280. float divide = 2.0;
  281.  
  282. float4 left = tex2D(passSampler, IN.UVCoord - float2(rcpres.x, 0));
  283. float lpower = saturate(ThicknessModel - abs(left.a - depth));
  284. blurred += lpower*left.r;
  285. divide += lpower;
  286.  
  287. float4 right = tex2D(passSampler, IN.UVCoord + float2(rcpres.x, 0));
  288. float rpower = saturate(ThicknessModel - abs(right.a - depth));
  289. blurred += rpower*left.r;
  290. divide += rpower;
  291.  
  292. return blurred/divide;
  293. }
  294. float4 VBlur( VSOUT IN ) : COLOR0 {
  295. float4 sample = tex2D(passSampler, IN.UVCoord);
  296. float blurred = sample.r*2;
  297. float depth = sample.a;
  298. float divide = 2.0;
  299.  
  300. float4 top = tex2D(passSampler, IN.UVCoord - float2(0, rcpres.y));
  301. float tpower = saturate(ThicknessModel - abs(top.a - depth));
  302. blurred += tpower*top.r;
  303. divide += tpower;
  304.  
  305. float4 bottom = tex2D(passSampler, IN.UVCoord + float2(0, rcpres.y));
  306. float bpower = saturate(ThicknessModel - abs(bottom.a - depth));
  307. blurred += bpower*bottom.r;
  308. divide += bpower;
  309.  
  310. return blurred/divide;
  311. }
  312. #else // BLUR_GAUSSIAN
  313. float4 HBlur(VSOUT IN) : COLOR0 {
  314. float color = tex2D(passSampler, IN.UVCoord).r;
  315.  
  316. float blurred = color*0.2270270270;
  317. blurred += tex2D(passSampler, IN.UVCoord + float2(rcpres.x*1.3846153846, 0)).r * 0.3162162162;
  318. blurred += tex2D(passSampler, IN.UVCoord - float2(rcpres.x*1.3846153846, 0)).r * 0.3162162162;
  319. blurred += tex2D(passSampler, IN.UVCoord + float2(rcpres.x*3.2307692308, 0)).r * 0.0702702703;
  320. blurred += tex2D(passSampler, IN.UVCoord - float2(rcpres.x*3.2307692308, 0)).r * 0.0702702703;
  321.  
  322. return blurred;
  323. }
  324.  
  325. float4 VBlur(VSOUT IN) : COLOR0 {
  326. float color = tex2D(passSampler, IN.UVCoord).r;
  327.  
  328. float blurred = color*0.2270270270;
  329. blurred += tex2D(passSampler, IN.UVCoord + float2(0, rcpres.y*1.3846153846)).r * 0.3162162162;
  330. blurred += tex2D(passSampler, IN.UVCoord - float2(0, rcpres.y*1.3846153846)).r * 0.3162162162;
  331. blurred += tex2D(passSampler, IN.UVCoord + float2(0, rcpres.y*3.2307692308)).r * 0.0702702703;
  332. blurred += tex2D(passSampler, IN.UVCoord - float2(0, rcpres.y*3.2307692308)).r * 0.0702702703;
  333.  
  334. return blurred;
  335. }
  336. #endif // blur type
  337.  
  338. float4 Combine( VSOUT IN ) : COLOR0 {
  339. float4 color = tex2D(frameSampler, IN.UVCoord);
  340. float ao = tex2D(passSampler, IN.UVCoord/SCALE).r;
  341. ao = clamp(ao, aoClamp, 1.0);
  342.  
  343. #ifdef LUMINANCE_CONSIDERATION
  344. float luminance = (color.r*0.2125f)+(color.g*0.7154f)+(color.b*0.0721f);
  345. float white = 1.0f;
  346. float black = 0.0f;
  347.  
  348. luminance = clamp(max(black,luminance-luminosity_threshold)+max(black,luminance-luminosity_threshold)+max(black,luminance-luminosity_threshold),0.0,1.0);
  349. ao = lerp(ao,white,luminance);
  350. #endif
  351.  
  352. color.rgb *= ao;
  353.  
  354. return color;
  355. //return float4(ao,ao,ao,color.a);
  356. //return tex2D(frameSampler, IN.UVCoord);
  357. //return tex2D(depthSampler, IN.UVCoord);
  358. }
  359.  
  360. technique t0
  361. {
  362. pass p0
  363. {
  364. VertexShader = compile vs_3_0 FrameVS();
  365. PixelShader = compile ps_3_0 ssao_Main();
  366. ZENABLE = FALSE;
  367. COLORWRITEENABLE = 15;
  368. ALPHATESTENABLE = false;
  369. ALPHABLENDENABLE = false;
  370. CULLMODE = NONE;
  371. STENCILENABLE = false;
  372. }
  373. pass p1
  374. {
  375. VertexShader = compile vs_3_0 FrameVS();
  376. PixelShader = compile ps_3_0 HBlur();
  377. ZENABLE = FALSE;
  378. COLORWRITEENABLE = 15;
  379. ALPHATESTENABLE = false;
  380. ALPHABLENDENABLE = false;
  381. CULLMODE = NONE;
  382. STENCILENABLE = false;
  383. }
  384. pass p2
  385. {
  386. VertexShader = compile vs_3_0 FrameVS();
  387. PixelShader = compile ps_3_0 VBlur();
  388. ZENABLE = FALSE;
  389. COLORWRITEENABLE = 15;
  390. ALPHATESTENABLE = false;
  391. ALPHABLENDENABLE = false;
  392. CULLMODE = NONE;
  393. STENCILENABLE = false;
  394. }
  395. pass p3
  396. {
  397. VertexShader = compile vs_3_0 FrameVS();
  398. PixelShader = compile ps_3_0 Combine();
  399. ZENABLE = FALSE;
  400. COLORWRITEENABLE = 15;
  401. ALPHATESTENABLE = false;
  402. ALPHABLENDENABLE = false;
  403. CULLMODE = NONE;
  404. STENCILENABLE = false;
  405. }
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement