Advertisement
luluco250

Light DoF

Sep 12th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.86 KB | None | 0 0
  1. /*
  2.     Light Depth of Field by luluco250
  3.    
  4.     Poisson disk blur by spite ported from: https://github.com/spite/Wagner/blob/master/fragment-shaders/poisson-disc-blur-fs.glsl
  5.    
  6.     Why "light"?
  7.     Because I wanted a DoF shader that didn't tank my GPU and didn't take rocket science to configure.
  8.     Also with delayed auto focus like in ENB.
  9.    
  10.     License: https://creativecommons.org/licenses/by-sa/4.0/
  11.     CC BY-SA 4.0
  12.    
  13.     You are free to:
  14.  
  15.     Share — copy and redistribute the material in any medium or format
  16.        
  17.     Adapt — remix, transform, and build upon the material
  18.     for any purpose, even commercially.
  19.  
  20.     The licensor cannot revoke these freedoms as long as you follow the license terms.
  21.        
  22.     Under the following terms:
  23.  
  24.     Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made.
  25.     You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  26.  
  27.     ShareAlike — If you remix, transform, or build upon the material,
  28.     you must distribute your contributions under the same license as the original.
  29.  
  30.     No additional restrictions — You may not apply legal terms or technological measures
  31.     that legally restrict others from doing anything the license permits.
  32. */
  33.  
  34. #include "ReShade.fxh"
  35.  
  36. //user variables//////////////////////////////////////////////////////////////////////////////////
  37.  
  38. uniform float fLightDoF_Width <
  39.     ui_label = "Bokeh Width [Light DoF]";
  40.     ui_type = "drag";
  41.     ui_min = 1.0;
  42.     ui_max = 25.0;
  43. > = 5.0;
  44.  
  45. uniform float fLightDoF_Amount <
  46.     ui_label = "DoF Amount [Light DoF]";
  47.     ui_type = "drag";
  48.     ui_min = 0.0;
  49.     ui_max = 10.0;
  50. > = 10.0;
  51.  
  52. uniform bool bLightDoF_UseCA <
  53.     ui_label = "Use Chromatic Aberration [Light DoF]";
  54.     ui_tooltip = "Use color channel shifting.";
  55. > = false;
  56.  
  57. uniform float2 f2LightDoF_CA <
  58.     ui_label = "Chromatic Aberration [Light DoF]";
  59.     ui_tooltip = "Shifts color channels.\nFirst value controls far CA, second controls near CA.";
  60.     ui_type = "drag";
  61.     ui_min = 0.0;
  62.     ui_max = 1.0;
  63. > = float2(0.0, 1.0);
  64.  
  65. uniform bool bLightDoF_AutoFocus <
  66.     ui_label = "Use Auto Focus [Light DoF]";
  67. > = true;
  68.  
  69. uniform float fLightDoF_AutoFocusSpeed <
  70.     ui_label = "Auto Focus Speed [Light DoF]";
  71.     ui_type = "drag";
  72.     ui_min = 0.001;
  73.     ui_max = 1.0;
  74. > = 0.1;
  75.  
  76. uniform bool bLightDoF_UseMouseFocus <
  77.     ui_label = "Use Mouse for Auto Focus Center [Light DoF]";
  78.     ui_tooltip = "Use the mouse position as the auto focus center";
  79. > = false;
  80.  
  81. uniform float2 f2Bokeh_AutoFocusCenter <
  82.     ui_label = "Auto Focus Center [Light DoF]";
  83.     ui_tooltip = "Target for auto focus.\nFirst value is horizontal: Left<->Right\nSecond value is vertical: Up<->Down";
  84.     ui_type = "drag";
  85.     ui_min = 0.0;
  86.     ui_max = 1.0;
  87. > = float2(0.5, 0.5);
  88.  
  89. uniform float fLightDoF_ManualFocus <
  90.     ui_label = "Manual Focus [Light DoF]";
  91.     ui_type = "drag";
  92.     ui_min = 0.0;
  93.     ui_max = 1.0;
  94. > = 0.0;
  95.  
  96. //system variables////////////////////////////////////////////////////////////////////////////////
  97.  
  98. //internal variable that holds the current mouse position
  99. uniform float2 f2LightDoF_MouseCoord <source="mousepoint";>;
  100.  
  101. //textures////////////////////////////////////////////////////////////////////////////////////////
  102.  
  103. /*
  104.     For those curious...
  105.    
  106.     Two textures are needed in order to delay focus.
  107.     I just lerp between them by a speed.
  108. */
  109.  
  110. //texture for saving the current frame's focus
  111. texture tFocus { Format=R16F; };
  112. //texture for saving the last frame's focus
  113. texture tLastFocus { Format=R16F; };
  114.  
  115. //samplers////////////////////////////////////////////////////////////////////////////////////////
  116.  
  117. //sampler for the current frame's focus
  118. sampler sFocus { Texture=tFocus; };
  119. //sampler for the last frame's focus
  120. sampler sLastFocus { Texture=tLastFocus; };
  121.  
  122. //functions///////////////////////////////////////////////////////////////////////////////////////
  123.  
  124. //interpret the focus textures and separate far/near focuses
  125. float getFocus(float2 coord, bool farOrNear) {
  126.     float depth = ReShade::GetLinearizedDepth(coord);
  127.    
  128.     depth -= bLightDoF_AutoFocus ? tex2D(sFocus, 0).x : fLightDoF_ManualFocus;
  129.    
  130.     if (farOrNear) {
  131.         depth = saturate(-depth * fLightDoF_Amount);
  132.     }
  133.     else {
  134.         depth = saturate(depth * fLightDoF_Amount);
  135.     }
  136.    
  137.     return depth;
  138. }
  139.  
  140. //helper function for poisson-disk blur
  141. float2 rot2D(float2 pos, float angle) {
  142.     float2 source = float2(sin(angle), cos(angle));
  143.     return float2(dot(pos, float2(source.y, -source.x)), dot(pos, source));
  144. }
  145.  
  146. //poisson-disk blur
  147. float3 poisson(sampler sp, float2 uv, float farOrNear, float CA) {
  148.     float2 poisson[12];
  149.     poisson[0]  = float2(-.326,-.406);
  150.     poisson[1]  = float2(-.840,-.074);
  151.     poisson[2]  = float2(-.696, .457);
  152.     poisson[3]  = float2(-.203, .621);
  153.     poisson[4]  = float2( .962,-.195);
  154.     poisson[5]  = float2( .473,-.480);
  155.     poisson[6]  = float2( .519, .767);
  156.     poisson[7]  = float2( .185,-.893);
  157.     poisson[8]  = float2( .507, .064);
  158.     poisson[9]  = float2( .896, .412);
  159.     poisson[10] = float2(-.322,-.933);
  160.     poisson[11] = float2(-.792,-.598);
  161.    
  162.     float3 col = 0;
  163.     float random = frac(sin(dot(uv, float2(12.9898, 78.233))) * 43758.5453);
  164.     float4 basis = float4(rot2D(float2(1, 0), random), rot2D(float2(0, 1), random));
  165.     [unroll]
  166.     for (int i = 0; i < 12; ++i) {
  167.         float2 offset = poisson[i];
  168.         offset = float2(dot(offset, basis.xz), dot(offset, basis.yw));
  169.        
  170.         if (bLightDoF_UseCA) {
  171.             float2 rCoord = uv + offset * ReShade::PixelSize * fLightDoF_Width * (1.0 + CA);
  172.             float2 gCoord = uv + offset * ReShade::PixelSize * fLightDoF_Width * (1.0 + CA * 0.5);
  173.             float2 bCoord = uv + offset * ReShade::PixelSize * fLightDoF_Width;
  174.            
  175.             rCoord = lerp(uv, rCoord, getFocus(rCoord, farOrNear));
  176.             gCoord = lerp(uv, gCoord, getFocus(gCoord, farOrNear));
  177.             bCoord = lerp(uv, bCoord, getFocus(bCoord, farOrNear));
  178.            
  179.             col +=  float3(
  180.                         tex2Dlod(sp, float4(rCoord, 0, 0)).r,
  181.                         tex2Dlod(sp, float4(gCoord, 0, 0)).g,
  182.                         tex2Dlod(sp, float4(bCoord, 0, 0)).b
  183.                     );
  184.         }
  185.         else {
  186.             float2 coord = uv + offset * ReShade::PixelSize * fLightDoF_Width;
  187.             coord = lerp(uv, coord, getFocus(coord, farOrNear));
  188.             col += tex2Dlod(sp, float4(coord, 0, 0)).rgb;
  189.         }
  190.        
  191.     }
  192.     return col * 0.083;
  193. }
  194.  
  195. //shaders/////////////////////////////////////////////////////////////////////////////////////////
  196.  
  197. //far blur shader
  198. float3 Far(float4 pos : SV_Position, float2 uv : TEXCOORD0) : SV_Target {
  199.     return poisson(ReShade::BackBuffer, uv, false, f2LightDoF_CA.x);
  200. }
  201.  
  202. //near blur shader
  203. float3 Near(float4 pos : SV_Position, float2 uv : TEXCOORD0) : SV_Target {
  204.     return poisson(ReShade::BackBuffer, uv, true, f2LightDoF_CA.y);
  205. }
  206.  
  207. //shader to get the focus, kinda like center of confusion but less complicated
  208. float GetFocus(float4 pos : SV_Position, float2 uv : TEXCOORD0) : SV_Target {
  209.     float2 linearMouse = f2LightDoF_MouseCoord * ReShade::PixelSize; //linearize the mouse position
  210.     float2 focus = bLightDoF_UseMouseFocus ? linearMouse : f2Bokeh_AutoFocusCenter;
  211.     return lerp(tex2D(sLastFocus, 0).x, ReShade::GetLinearizedDepth(focus), fLightDoF_AutoFocusSpeed);
  212. }
  213.  
  214. //shader for saving this frame's focus to lerp with the next one's
  215. float SaveFocus(float4 pos : SV_Position, float2 uv : TEXCOORD0) : SV_Target {
  216.     return tex2D(sFocus, 0).x;
  217. }
  218.  
  219.  
  220. //techniques//////////////////////////////////////////////////////////////////////////////////////
  221.  
  222. //this technique is dedicated for auto focus, so you don't need it if you're not using auto-focus :)
  223. technique LightDoF_AutoFocus {
  224.     pass GetFocus {
  225.         VertexShader=PostProcessVS;
  226.         PixelShader=GetFocus;
  227.         RenderTarget=tFocus;
  228.     }
  229.     pass SaveFocus {
  230.         VertexShader=PostProcessVS;
  231.         PixelShader=SaveFocus;
  232.         RenderTarget=tLastFocus;
  233.     }
  234. }
  235.  
  236. //technique for far blur
  237. technique LightDoF_Far {
  238.     pass Far {
  239.         VertexShader=PostProcessVS;
  240.         PixelShader=Far;
  241.     }
  242. }
  243.  
  244. //technique for near blur
  245. technique LightDoF_Near {
  246.     pass Near {
  247.         VertexShader=PostProcessVS;
  248.         PixelShader=Near;
  249.     }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement