SHARE
TWEET

HBAO for Dark Souls dsfix 1.1 (replace VSSAO.fx)

a guest Sep 9th, 2012 453 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Cosine Weighted HBAO
  2. //Originally based off of NVIDIA's HBAO, implemented and heavily modified by Tomerk
  3. //SSAO ported to Dark Souls by Durante. This HBAO shader shoved in over that by roxahris.
  4.  
  5. /***User-controlled variables***/
  6. #define N_DIRECTIONS 8 //number of directions to sample in, do not change.
  7. #define N_STEPS 6 //number of steps to raymarch, go ahead and change
  8. // The higher this is, the higher the quality and the lower the performance. Default is 6. I recommend 2.
  9. // Note that lowering this makes the occlusion lighter.
  10.  
  11. extern float scale = 1.0; //downsampling scale, 1 is highest quality but slowest
  12.  
  13. extern float aoRadiusMultiplier = 0.8; //Linearly multiplies the radius of the AO Sampling
  14. extern float Attenuation_Factor = 0.01; //Affects units in space the AO will extend to 0.01
  15.  
  16. extern float FOV = 75; //Field of View in Degrees
  17.  
  18. extern float luminosity_threshold = 0.3;
  19.  
  20. #ifndef SSAO_STRENGTH_LOW
  21. #ifndef SSAO_STRENGTH_MEDIUM
  22. #ifndef SSAO_STRENGTH_HIGH
  23. #define SSAO_STRENGTH_MEDIUM 1
  24. #endif
  25. #endif
  26. #endif
  27.  
  28. #ifdef SSAO_STRENGTH_LOW
  29. extern float aoClamp = 0.4;
  30. extern float aoStrengthMultiplier = 1.0;
  31. #endif
  32.  
  33. #ifdef SSAO_STRENGTH_MEDIUM
  34. extern float aoClamp = 0.4;
  35. extern float aoStrengthMultiplier = 2;
  36. #endif
  37.  
  38. #ifdef SSAO_STRENGTH_HIGH
  39. extern float aoClamp = 0.1;
  40. extern float aoStrengthMultiplier = 2.0;
  41. #endif
  42.  
  43. #define LUMINANCE_CONSIDERATION //comment this line to not take pixel brightness into account
  44. //#define RAW_SSAO //uncomment this line to show the raw ssao
  45.  
  46. /***End Of User-controlled Variables***/
  47. #ifndef PIXEL_SIZE
  48. float PIXEL_SIZE = 1.0;
  49. #endif
  50.  
  51. static float2 rcpres = PIXEL_SIZE;
  52. static float aspect = rcpres.y/rcpres.x;
  53.  
  54. static const float nearZ = 10.0;
  55. static const float farZ = 5000.0;
  56.  
  57. static const float2 g_InvFocalLen = { tan(0.5f*radians(FOV)) / rcpres.y * rcpres.x, tan(0.5f*radians(FOV)) };
  58. static const float depthRange = nearZ-farZ;
  59.  
  60.  
  61. texture depthTex2D;
  62. texture frameTex2D;
  63. texture prevPassTex2D;
  64.  
  65. sampler depthSampler = sampler_state
  66. {
  67.         texture = <depthTex2D>;
  68.         AddressU = CLAMP;
  69.         AddressV = CLAMP;
  70.         MINFILTER = LINEAR;
  71.         MAGFILTER = LINEAR;
  72. };
  73.  
  74. sampler frameSampler = sampler_state
  75. {
  76.         texture = <frameTex2D>;
  77.         AddressU = CLAMP;
  78.         AddressV = CLAMP;
  79.         MINFILTER = LINEAR;
  80.         MAGFILTER = LINEAR;
  81. };
  82.  
  83. sampler passSampler = sampler_state
  84. {
  85.         texture = <prevPassTex2D>;
  86.         AddressU = CLAMP;
  87.         AddressV = CLAMP;
  88.         MINFILTER = LINEAR;
  89.         MAGFILTER = LINEAR;
  90. };
  91.  
  92. struct VSOUT
  93. {
  94.         float4 vertPos : POSITION;
  95.         float2 UVCoord : TEXCOORD0;
  96. };
  97.  
  98. struct VSIN
  99. {
  100.         float4 vertPos : POSITION0;
  101.         float2 UVCoord : TEXCOORD0;
  102. };
  103.  
  104. VSOUT FrameVS(VSIN IN)
  105. {
  106.         VSOUT OUT = (VSOUT)0.0f;        // initialize to zero, avoid complaints.
  107.         OUT.vertPos = IN.vertPos;
  108.         OUT.UVCoord = IN.UVCoord;
  109.         return OUT;
  110. }
  111.  
  112. static float2 sample_offset[N_DIRECTIONS] =
  113. {
  114. //#if N_DIRECTIONS >= 9
  115.         float2(1, 0),
  116.         float2(0.7071f, 0.7071f),
  117.         float2(0, 1),
  118.         float2(-0.7071f, 0.7071f),
  119.         float2(-1, 0),
  120.         float2(-0.7071f, -0.7071f),
  121.         float2(0, -1),
  122.         float2(0.7071f, -0.7071f)
  123. //#endif
  124. };
  125.  
  126. float2 rand(in float2 uv) {
  127.         float noiseX = (frac(sin(dot(uv, float2(12.9898,78.233)*2.0)) * 43758.5453));
  128.         //float noiseY = sqrt(1-noiseX*noiseX);
  129.         float noiseY = (frac(sin(dot(uv, float2(12.9898,78.233))) * 43758.5453));
  130.         return float2(noiseX, noiseY);
  131. }
  132.  
  133.  
  134. float readDepth(in float2 coord : TEXCOORD0) {
  135.         float4 col = tex2D(depthSampler, coord);
  136.         float posZ = clamp((((1.0-col.z)/(256.0) + (1.0-col.y) + (1.0-col.x)*256))/8.0, 0.0, 1.0);
  137.         //float posZ = ((1.0-col.y) + 256.0*(1.0-col.x))/(5.0);
  138.         return ((2.0f * nearZ) / (nearZ + farZ - posZ * (farZ - nearZ)))*20.0;
  139.         //return log(pow(exp(((2.0f * nearZ) / (nearZ + farZ - posZ * (farZ - nearZ)))),20));
  140.         //return posZ / (depthRange - (depthRange - 1.0)*posZ);
  141.         //float posZ = ((1.0-col.y) + 256.0*(1.0-col.x));
  142.         //return posZ;
  143. }
  144.  
  145.  
  146. float3 getPosition(in float2 uv, in float eye_z) {
  147.    uv = (uv * float2(2.0, -2.0) - float2(1.0, -1.0));
  148.    float3 pos = float3(uv * g_InvFocalLen * eye_z, eye_z );
  149.    return 1.0 - pos;
  150. }
  151.  
  152. float4 ssao_Main( VSOUT IN ) : COLOR0
  153. {
  154.         clip(1/scale-IN.UVCoord.x);
  155.         clip(1/scale-IN.UVCoord.y);    
  156.         IN.UVCoord.xy *= scale;
  157.  
  158.         float3 sample = tex2D(passSampler, IN.UVCoord).rgb;
  159.         float depth = readDepth(IN.UVCoord);
  160.          
  161.         [branch]
  162. //      if (depth >= 0.99)
  163. //      if (depth <= 0.045)
  164.         if (depth <= 0.082)
  165.         //      return float4(sample, 1.0);
  166.                 return float4(1.0, 1.0, 1.0, 1.0);
  167.                                
  168.         float3 pos = getPosition(IN.UVCoord, depth);
  169.         float3 dx = ddx(pos);
  170.         float3 dy = ddy(pos);
  171.         float3 norm = normalize(cross(dx,dy));
  172.  
  173.         float sample_depth;
  174.         float3 sample_pos;
  175.  
  176.         float ao = 0;
  177.         float s = 0.0;
  178.  
  179.         float2 rand_vec = rand(IN.UVCoord);
  180.         float2 sample_vec_divisor = g_InvFocalLen*depth*depthRange/(aoRadiusMultiplier*5000*rcpres);
  181.         float2 sample_center = IN.UVCoord;
  182.  
  183.         [loop]
  184.         for (int i = 0; i < N_DIRECTIONS; i++) {
  185.                 float theta = 0;
  186.                 float temp_theta = 0;
  187.  
  188.                 float temp_ao = 0;
  189.                 float curr_ao = 0;
  190.  
  191.                 float3 occlusion_vector = float3(0, 0, 0);
  192.  
  193.                 float2 sample_vec = reflect(sample_offset[i], rand_vec);
  194.                 sample_vec /= sample_vec_divisor;
  195.                 float2 sample_coords = (sample_vec * float2(1, aspect)) / N_STEPS;
  196.  
  197.                 [loop]
  198.                 for (int k = 1; k <= N_STEPS; k++) {
  199.                         sample_depth = readDepth(sample_center + sample_coords * (k - 0.5 * (i % 2)));
  200.                         sample_pos = getPosition(sample_center + sample_coords * (k - 0.5 * (i % 2)), sample_depth);
  201.                         occlusion_vector = sample_pos - pos;
  202.                         temp_theta = dot(norm, normalize(occlusion_vector));
  203.  
  204.                         if (temp_theta > theta) {
  205.                                 theta = temp_theta;
  206.                                 temp_ao = 1 - sqrt(1 - theta * theta);
  207.                                 ao += (1 / (1 + Attenuation_Factor * pow(length(occlusion_vector) / aoRadiusMultiplier * depthRange, 2))) * (temp_ao - curr_ao);
  208.                                 curr_ao = temp_ao;
  209.                         }
  210.                 }
  211.  
  212.                 s += 1;
  213.         }
  214.  
  215.         ao /= s;
  216.  
  217.         ao = 1.0 - ao * aoStrengthMultiplier;
  218.        
  219.         // if(depth<0.08) ao = lerp(ao, 0.0, (0.08-depth)*25);
  220.        
  221.         #define fadeout 0.11
  222.         if(depth<fadeout) ao = lerp(ao, 1.0, (fadeout-depth)*25);
  223.        
  224.         // return float4(sample, clamp(ao, aoClamp, 1));
  225.        
  226.         return float4(clamp(ao,aoClamp,1),1,1,1);
  227.        
  228.         // return float4(ao,ao,ao,1);
  229. }
  230.  
  231.  
  232. float4 Blur( VSOUT IN ) : COLOR0 {
  233.         float color = tex2D(passSampler, IN.UVCoord).r*4;
  234.  
  235.         color += tex2D(passSampler, IN.UVCoord + float2(rcpres.x,0)).r*2;
  236.         color += tex2D(passSampler, IN.UVCoord + float2(-rcpres.x,0)).r*2;
  237.         color += tex2D(passSampler, IN.UVCoord + float2(0,rcpres.y)).r*2;
  238.         color += tex2D(passSampler, IN.UVCoord + float2(0,-rcpres.y)).r*2;
  239.  
  240.         color += tex2D(passSampler, IN.UVCoord + rcpres).r;
  241.         color += tex2D(passSampler, IN.UVCoord - rcpres).r;
  242.         color += tex2D(passSampler, IN.UVCoord + rcpres * float2(1,-1)).r;
  243.         color += tex2D(passSampler, IN.UVCoord - rcpres * float2(1,-1)).r;
  244.  
  245.         color /= 16;
  246.  
  247.         return float4(color,1,1,1);
  248.         //return tex2D(passSampler, IN.UVCoord).r;
  249. }
  250.  
  251. static const float sampleWeights[3] = { 0.2270270270, 0.3162162162, 0.0702702703 };
  252. static const float sampleOffsets[3] = { 0.0, 1.3846153846, 3.2307692308 };
  253.  
  254. float4 Combine( VSOUT IN ) : COLOR0 {
  255.         float3 color = tex2D(frameSampler, IN.UVCoord).rgb;
  256.         float ao = tex2D(passSampler, IN.UVCoord/scale).r;
  257.         ao = clamp(ao, aoClamp, 1.0);
  258.  
  259.         #ifdef LUMINANCE_CONSIDERATION
  260.         float luminance = color.r*0.3+color.g*0.59+color.b*0.11;
  261.         float white = 1.0;
  262.         float black = 0;
  263.  
  264.         luminance = clamp(max(black,luminance-luminosity_threshold)+max(black,luminance-luminosity_threshold)+max(black,luminance-luminosity_threshold),0.0,1.0);
  265.         ao = lerp(ao,white,luminance);
  266.         #endif
  267.  
  268.         color *= ao;
  269. #ifdef RAW_SSAO
  270.         return float4(ao, ao, ao,1);
  271. #endif
  272.         return float4(color,1);
  273. }
  274.  
  275. technique t0
  276. {
  277.         pass p0
  278.         {
  279.                 VertexShader = compile vs_3_0 FrameVS();
  280.                 PixelShader = compile ps_3_0 ssao_Main();
  281.         }
  282.         pass p1
  283.         {
  284.                 VertexShader = compile vs_3_0 FrameVS();
  285.                 PixelShader = compile ps_3_0 Blur();
  286.         }
  287.         pass p2
  288.         {
  289.                 VertexShader = compile vs_1_1 FrameVS();
  290.                 PixelShader = compile ps_2_0 Combine();
  291.         }
  292. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top