Advertisement
Guest User

McFly DOF 1.3B Simple Tilt Shift Toggle Modification

a guest
Dec 12th, 2016
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.35 KB | None | 0 0
  1. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. // ENBSeries Skyrim SE dx11 effect file
  3. // visit facebook.com/MartyMcModding for news/updates
  4. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  5. // Advanced Depth of Field 1.3b by Marty McFly
  6. // Do not redistribute without credits!
  7. // Beta version for testing
  8. // Copyright � 2008-2016 Marty McFly
  9. // Simple Tilt Shift toggle functionality added by TreyM
  10. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  11.  
  12. //Non UI-able vars (actually possible but requires dynamic branching which wastes resources even when effects are not used)
  13. #define bADOF_ShapeWeightEnable     1   //[0 or 1] Enables bokeh shape weight bias and shifts color to the shape borders.
  14. #define bADOF_ShapeChromaEnable     1   //[0 or 1] Enables chromatic aberration at bokeh shape borders. This means 3 times more samples = less performance.
  15. #define iADOF_ShapeChromaMode       1   //[1 to 6] Switches through the possible R G B shifts.
  16.  
  17. //UI vars
  18. //Marty McFly (gp65cj04 modified) Focusing
  19. bool    bADOF_AutofocusEnable       < string UIName="DOF: Enable Autofocus";                                                            > = {true};
  20. float2  fADOF_AutofocusCenter       < string UIName="DOF: Autofocus sample center";     string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.00;   float UIMax=1.00;   > = {0.5,0.5};
  21. int iADOF_AutofocusSamples      < string UIName="DOF: Autofocus sample count";      string UIWidget="spinner";              int UIMin=0;        int UIMax=10;       > = {6};
  22. float   fADOF_AutofocusRadius       < string UIName="DOF: Autofocus sample radius";     string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.00;   float UIMax=0.50;   > = {0.05};
  23. float   fADOF_ManualfocusDepth      < string UIName="DOF: Manual focus depth";      string UIWidget="Spinner";  float UIStep=0.001; float UIMin=0.00;   float UIMax=1.0;    > = {0.05};
  24. float   fADOF_NearBlurCurve     < string UIName="DOF: Near blur curve";         string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.00;   float UIMax=20.0;   > = {1.0};
  25. float   fADOF_FarBlurCurve      < string UIName="DOF: Far blur curve";          string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.00;   float UIMax=20.0;   > = {1.0};
  26. float   fADOF_NearBlurMult      < string UIName="DOF: Near blur mult";          string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.00;   float UIMax=1.0;    > = {1.0};
  27. float   fADOF_FarBlurMult       < string UIName="DOF: Far blur mult";           string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.00;   float UIMax=1.0;    > = {1.0};
  28. float   fADOF_InfiniteFocus     < string UIName="DOF: Infinite depth distance";     string UIWidget="Spinner";  float UIStep=0.001; float UIMin=0.00;   float UIMax=1.0;    > = {1.0};
  29. float   fADOF_BokehCurve        < string UIName="DOF: Bokeh Intensity";         string UIWidget="Spinner";  float UIStep=0.1;   float UIMin=0.1;    float UIMax=10.0;   > = {4.0};
  30. float   fADOF_ShapeRadius       < string UIName="DOF: Bokeh shape max size";        string UIWidget="Spinner";  float UIStep=0.1;   float UIMin=0.0;    float UIMax=50.0;   > = {12.0};
  31. int iADOF_ShapeVertices     < string UIName="DOF: Bokeh shape vertices";        string UIWidget="spinner";              int UIMin=3;        int UIMax=9;        > = {6};
  32. int iADOF_ShapeQuality      < string UIName="DOF: Bokeh shape quality";     string UIWidget="spinner";              int UIMin=2;        int UIMax=25;       > = {9};
  33. float   fADOF_ShapeCurvatureAmount  < string UIName="DOF: Bokeh shape curvature";       string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.1;    float UIMax=1.0;    > = {1.0};
  34. float   fADOF_ShapeRotation     < string UIName="DOF: Bokeh shape rotation (\xB0)"; string UIWidget="Spinner";  float UIStep=1;     float UIMin=0;      float UIMax=360;    > = {15};
  35. float   fADOF_ShapeAnamorphRatio    < string UIName="DOF: Bokeh shape aspect ratio";    string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.0;    float UIMax=1.0;    > = {1.0};
  36.  
  37. #if(bADOF_ShapeWeightEnable != 0)
  38.  float  fADOF_ShapeWeightCurve      < string UIName="DOF: Bokeh shape weight curve";    string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.50;   float UIMax=8.0;    > = {1.0};
  39.  float  fADOF_ShapeWeightAmount     < string UIName="DOF: Bokeh shape weight amount";   string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.00;   float UIMax=1.0;    > = {1.0};
  40. #endif
  41. #if(bADOF_ShapeChromaEnable != 0)
  42.  float  fADOF_ShapeChromaAmount     < string UIName="DOF: Shape chroma amount";     string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.00;   float UIMax=0.50;   > = {0.15};
  43. #endif
  44. float   fADOF_RenderResolutionMult  < string UIName="DOF: Blur render res mult";        string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.5;    float UIMax=1.0;    > = {0.7};
  45. float   fADOF_SmootheningAmount     < string UIName="DOF: Gaussian postblur width";     string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.1;    float UIMax=5.0;    > = {1.0};
  46.  
  47. //Marty McFly Tilt Shift
  48. float   DOF_EMPTY       < string UIName="------------------------------------"; string UIWidget="Spinner";  float UIStep=0;     float UIMin=0;      float UIMax=0;  > = {0};
  49. float   DOF_EMPTY2      < string UIName="Some settings above are ignored";  string UIWidget="Spinner";  float UIStep=0;     float UIMin=0;      float UIMax=0;  > = {0};
  50. float   DOF_EMPTY3      < string UIName="in Tilt Shift mode.";  string UIWidget="Spinner";  float UIStep=0;     float UIMin=0;      float UIMax=0;  > = {0};
  51. float   DOF_EMPTY4      < string UIName="------------------------------------ ";    string UIWidget="Spinner";  float UIStep=0;     float UIMin=0;      float UIMax=0;  > = {0};
  52. bool    iADOF_TilShift      < string UIName="DOF: Tilt Shift Mode";                                                         > = {false};
  53. float   fADOF_TiltShiftPosition     < string UIName="DOF: Tilt Shift Axis Position";    string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=-1.0;   float UIMax=1.0;    > = {0.0};
  54. float   fADOF_TiltShiftWidth        < string UIName="DOF: Tilt Shift Focus Width";      string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.0;    float UIMax=1.0;    > = {0.0};
  55. float   fADOF_TiltShiftRotation     < string UIName="DOF: Tilt Shift Axis Rotation (\xB0)"; string UIWidget="Spinner";  float UIStep=1.00;  float UIMin=0.0;    float UIMax=180.0;  > = {0.0};
  56. float   fADOF_TiltShiftBlurCurve    < string UIName="DOF: Tilt Shift Blur Curve";       string UIWidget="Spinner";  float UIStep=0.01;  float UIMin=0.01;   float UIMax=10.0;   > = {2.0};
  57.  
  58. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  59. //external enb parameters, do not modify
  60. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  61.  
  62. float4  Timer;          //x = generic timer in range 0..1, period of 16777216 ms (4.6 hours), y = average fps, w = frame time elapsed (in seconds)
  63. float4  ScreenSize;         //x = Width, y = 1/Width, z = aspect, w = 1/aspect, aspect is Width/Height
  64. float   AdaptiveQuality;    //changes in range 0..1, 0 means full quality, 1 lowest dynamic quality (0.33, 0.66 are limits for quality levels)
  65. float4  Weather;        //x = current weather index, y = outgoing weather index, z = weather transition, w = time of the day in 24 standart hours. Weather index is value from weather ini file, for example WEATHER002 means index==2, but index==0 means that weather not captured.
  66. float4  TimeOfDay1;     //x = dawn, y = sunrise, z = day, w = sunset. Interpolators range from 0..1
  67. float4  TimeOfDay2;     //x = dusk, y = night. Interpolators range from 0..1
  68. float   ENightDayFactor;    //changes in range 0..1, 0 means that night time, 1 - day time
  69. float   EInteriorFactor;    //changes 0 or 1. 0 means that exterior, 1 - interior
  70. #define PixelSize       float2(ScreenSize.y,ScreenSize.y*ScreenSize.z)
  71.  
  72. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  73. //external enb debugging parameters for shader programmers, do not modify
  74. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  75.  
  76. float4  tempF1;         //0,1,2,3
  77. float4  tempF2;         //5,6,7,8
  78. float4  tempF3;         //9,0
  79. float4  tempInfo1;      //float4(cursorpos.xy 0~1,isshaderwindowopen, mouse buttons)
  80. float4  tempInfo2;      //float4(cursorpos.xy prev left mouse button click, cursorpos.xy prev right mouse button click)
  81.  
  82. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  83. //mod parameters, do not modify
  84. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  85.  
  86. float4          DofParameters;      //z = ApertureTime multiplied by time elapsed, w = FocusingTime multiplied by time elapsed
  87. Texture2D       TextureCurrent;     //current frame focus depth or aperture. unused in dof computation
  88. Texture2D       TexturePrevious;    //previous frame focus depth or aperture. unused in dof computation
  89. Texture2D       TextureOriginal;    //color R16B16G16A16 64 bit hdr format
  90. Texture2D       TextureColor;       //color which is output of previous technique (except when drawed to temporary render target), R16B16G16A16 64 bit hdr format
  91. Texture2D       TextureDepth;       //scene depth R32F 32 bit hdr format
  92. Texture2D       TextureFocus;       //this frame focus 1*1 R32F hdr red channel only. computed in PS_Focus
  93. Texture2D       TextureAperture;    //this frame aperture 1*1 R32F hdr red channel only. computed in PS_Aperture
  94. Texture2D       TextureAdaptation;  //previous frame vanilla or enb adaptation 1*1 R32F hdr red channel only. adaptation computed after depth of field and it's kinda "average" brightness of screen!!!
  95. //temporary textures which can be set as render target for techniques via annotations like <string RenderTarget="RenderTargetRGBA32";>
  96. Texture2D       RenderTargetRGBA32;     //R8G8B8A8 32 bit ldr format
  97. Texture2D       RenderTargetRGBA64;     //R16B16G16A16 64 bit ldr format
  98. Texture2D       RenderTargetRGBA64F;    //R16B16G16A16F 64 bit hdr format
  99. Texture2D       RenderTargetR16F;   //R16F 16 bit hdr format with red channel only
  100. Texture2D       RenderTargetR32F;   //R32F 32 bit hdr format with red channel only
  101. Texture2D       RenderTargetRGB32F;     //32 bit hdr format without alpha
  102.  
  103. SamplerState        Sampler0
  104. {
  105.     Filter = MIN_MAG_MIP_POINT;
  106.     AddressU = Clamp;
  107.     AddressV = Clamp;
  108. };
  109. SamplerState        Sampler1
  110. {
  111.     Filter = MIN_MAG_MIP_LINEAR;
  112.     AddressU = Clamp;
  113.     AddressV = Clamp;
  114. };
  115.  
  116. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  117. // Vertex Shader
  118. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  119.  
  120. void VS_DOF(in float3 inpos : POSITION, inout float2 txcoord0 : TEXCOORD0, out float4 outpos : SV_POSITION)
  121. {
  122.     outpos = float4(inpos.xyz,1.0);
  123. }
  124.  
  125. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  126. // Functions
  127. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  128.  
  129. float GetLinearDepth(float2 coords)
  130. {
  131.     float depth = TextureDepth.SampleLevel(Sampler1, coords.xy,0).x;
  132.     static const float N = 1.0;
  133.     static const float F = 3000.0;
  134.  
  135.     depth /= F - depth * (F - N);
  136.     return depth;
  137. }
  138.  
  139. float GetCoC(float2 texcoord)
  140. {
  141.         float   scenecoc    = 0.0;
  142.  
  143.         if(iADOF_TilShift == false)
  144.           {
  145.                 float   scenedepth  = GetLinearDepth(texcoord.xy);
  146.             float   scenefocus  = TextureFocus.Sample(Sampler0, texcoord.xy).x;
  147.  
  148.                 scenefocus = smoothstep(0.0,fADOF_InfiniteFocus,scenefocus);
  149.             scenedepth = smoothstep(0.0,fADOF_InfiniteFocus,scenedepth);
  150.  
  151.             float farBlurDepth = scenefocus*pow(4.0,fADOF_FarBlurCurve);
  152.  
  153.             if(scenedepth < scenefocus)
  154.              {
  155.                 scenecoc = (scenedepth - scenefocus) / scenefocus;
  156.                 scenecoc *= fADOF_NearBlurMult;
  157.              }
  158.             else
  159.              {
  160.                 scenecoc=(scenedepth - scenefocus)/(farBlurDepth - scenefocus);
  161.                 scenecoc *= fADOF_FarBlurMult;
  162.                 scenecoc=saturate(scenecoc);
  163.              }
  164.             scenecoc = (scenedepth < 0.00000001) ? 0.0 : scenecoc; //first person models, that epsilon is handpicked, do not change
  165.           }
  166.         else
  167.         {
  168.                 float2 ncoord = texcoord.xy * 2.0 - 1.0;
  169.                 ncoord.x *= ScreenSize.z;
  170.                 ncoord.y += fADOF_TiltShiftPosition;
  171.  
  172.                 float2 matrixVector;
  173.                 sincos(fADOF_TiltShiftRotation*0.0174533,matrixVector.x,matrixVector.y);
  174.                 float2x2 axisrot = float2x2(matrixVector.y,-matrixVector.x,matrixVector.xy);
  175.  
  176.                 ncoord.xy = mul(ncoord.xy,axisrot);
  177.  
  178.                 scenecoc = abs(ncoord.y);
  179.                 scenecoc = saturate((scenecoc - fADOF_TiltShiftWidth) / (1.00001 - fADOF_TiltShiftWidth));
  180.                 scenecoc = pow(scenecoc,fADOF_TiltShiftBlurCurve);
  181.         }
  182.  
  183.     scenecoc = saturate(scenecoc * 0.5 + 0.5);
  184.     return scenecoc;
  185. }
  186.  
  187. float4 colortexSampleChroma(Texture2D colortex, float2 sourcecoord, float2 offsetcoord, float3 chromaoffsets) //new function
  188. {
  189.     float4 res      = 0.0;
  190.     float2 Rsample      = colortex.SampleLevel(Sampler1, sourcecoord.xy + offsetcoord.xy * chromaoffsets.x,0).xw;
  191.     float2 Gsample      = colortex.SampleLevel(Sampler1, sourcecoord.xy + offsetcoord.xy    * chromaoffsets.y,0).yw;
  192.     float2 Bsample      = colortex.SampleLevel(Sampler1, sourcecoord.xy + offsetcoord.xy * chromaoffsets.z,0).zw;
  193.     res.xyz         = float3(Rsample.x,Gsample.x,Bsample.x);
  194.     res.w           = min(min(Rsample.y,Gsample.y),Bsample.y); //best for masking
  195.     return res;
  196. }
  197.  
  198. float3 BokehBlur(Texture2D colortex, float2 coords, float discRadius, float centerDepth)
  199. {
  200.     float4 res          = float4(TextureColor.Sample(Sampler1, coords.xy).xyz, 1.0);
  201.     int ringCount           = round(lerp(1.0,(float)iADOF_ShapeQuality,discRadius/fADOF_ShapeRadius));
  202.     float ringIncrement     = 1.0 / ringCount;
  203.     float2 discRadiusInPixels   = discRadius*PixelSize.xy;
  204.     float Alpha = 6.2831853 / iADOF_ShapeVertices;
  205.  
  206.     discRadiusInPixels.x *= fADOF_ShapeAnamorphRatio;
  207.  
  208.     float2 currentVertex;
  209.     float2 matrixVector;
  210.     sincos(fADOF_ShapeRotation*0.0174533,currentVertex.y,currentVertex.x);
  211.     sincos(Alpha,matrixVector.x,matrixVector.y);
  212.     float2x2 rotMatrix = float2x2(matrixVector.y,-matrixVector.x,matrixVector.x,matrixVector.y);
  213.  
  214.     #if(bADOF_ShapeWeightEnable != 0)
  215.     res.w = lerp(1.0,0.0,fADOF_ShapeWeightAmount*saturate(ringCount));
  216.     #endif
  217.  
  218.     res.xyz = pow(res.xyz,fADOF_BokehCurve)*res.w;
  219.  
  220.     #if(bADOF_ShapeChromaEnable != 0)
  221.     float3 chromaoffsets = float3(1.0 - fADOF_ShapeChromaAmount, 1.0, 1.0 + fADOF_ShapeChromaAmount);
  222.  
  223.      #if(iADOF_ShapeChromaMode == 2)
  224.         chromaoffsets.xyz = chromaoffsets.zxy;
  225.      #elif(iADOF_ShapeChromaMode == 3)
  226.         chromaoffsets.xyz = chromaoffsets.yzx;
  227.      #elif(iADOF_ShapeChromaMode == 4)
  228.         chromaoffsets.xyz = chromaoffsets.xzy;
  229.      #elif(iADOF_ShapeChromaMode == 5)
  230.         chromaoffsets.xyz = chromaoffsets.yxz;
  231.      #elif(iADOF_ShapeChromaMode == 6)
  232.         chromaoffsets.xyz = chromaoffsets.zyx;
  233.      #else
  234.         chromaoffsets.xyz = chromaoffsets.xyz;
  235.      #endif
  236.     #endif
  237.  
  238.     [fastopt]
  239.     for(float i = 1; i <= ringCount && i <= 25; i++)
  240.     {
  241.         float radiusCoeff = i*ringIncrement;
  242.  
  243.         [fastopt]
  244.         for (int j = 1; j <= iADOF_ShapeVertices && j <= 9; j++)
  245.         {
  246.             float2 nextVertex = mul(currentVertex.xy, rotMatrix);
  247.  
  248.             [fastopt]
  249.             for (float k = 0; k < i && k < 25; k++)
  250.             {
  251.                 float2 sampleOffset = lerp(currentVertex,nextVertex,k/i);
  252.                 sampleOffset = lerp(sampleOffset,normalize(sampleOffset),fADOF_ShapeCurvatureAmount);
  253.  
  254.                 #if(bADOF_ShapeChromaEnable != 0)
  255.                 float4 tap = colortexSampleChroma(colortex, coords.xy, sampleOffset.xy * discRadiusInPixels * radiusCoeff, chromaoffsets);
  256.                 #else
  257.                 float4 tap = colortex.SampleLevel(Sampler1, coords.xy + sampleOffset.xy * discRadiusInPixels * radiusCoeff,0);
  258.                 #endif
  259.  
  260.               if(iADOF_TilShift == false)
  261.                 {
  262.                         float tapcoc = tap.w * 2.0 - 1.0;
  263.                         tap.w = (tap.w >= centerDepth * 0.99) ? 1.0 : tapcoc*tapcoc*tapcoc*tapcoc;
  264.                 }
  265.               else
  266.                 {
  267.                 tap.w = 1.0;
  268.                 }
  269.  
  270.                 #if(bADOF_ShapeWeightEnable != 0)
  271.                 tap.w *= lerp(1.0,pow(radiusCoeff,fADOF_ShapeWeightCurve),fADOF_ShapeWeightAmount);
  272.                 #endif
  273.  
  274.                 res.xyz += pow(tap.xyz,fADOF_BokehCurve)*tap.w;
  275.                 res.w += tap.w;
  276.             }
  277.  
  278.             currentVertex = nextVertex;
  279.         }
  280.     }
  281.     res.xyz = max(res.xyz/res.w,0.0); //compiler...
  282.     return pow(res.xyz,1.0/fADOF_BokehCurve);
  283. }
  284.  
  285. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  286. // Pixel Shaders
  287. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  288. //? -> 1x1 R32F
  289. float4  PS_Aperture(float2 texcoord : TEXCOORD0) : SV_Target
  290. {
  291.     //as I don't use aperture and deleting the technique causes weird things to happen, don't waste resources :v
  292.     return 1;
  293. }
  294.  
  295. //fullres -> 16x16 R32F
  296. float4  PS_ReadFocus(float2 texcoord : TEXCOORD0) : SV_Target
  297. {
  298. if(iADOF_TilShift == false)
  299. {
  300.     float scenefocus    = fADOF_ManualfocusDepth;
  301.     float2 coords       = 0.0;
  302.  
  303.     if(bADOF_AutofocusEnable != 0)
  304.     {
  305.         scenefocus = GetLinearDepth(fADOF_AutofocusCenter.xy);
  306.         float2 offsetVector = float2(1.0,0.0) * fADOF_AutofocusRadius;
  307.         float Alpha = 6.2831853 / iADOF_AutofocusSamples;
  308.         float2x2 rotMatrix = float2x2(cos(Alpha),-sin(Alpha),sin(Alpha),cos(Alpha));
  309.  
  310.         for(int i=0; i<iADOF_AutofocusSamples; i++)
  311.         {
  312.             float2 currentOffset = fADOF_AutofocusCenter + offsetVector.xy;
  313.             scenefocus += GetLinearDepth(currentOffset);
  314.             offsetVector = mul(offsetVector,rotMatrix);
  315.         }
  316.  
  317.         scenefocus /= iADOF_AutofocusSamples;
  318.     }
  319.  
  320.     scenefocus = saturate(scenefocus);
  321.     return scenefocus;
  322. }
  323.         return 0.0;
  324. }
  325.  
  326. //16x16 -> 1x1 R32F
  327. float4  PS_Focus(float2 texcoord : TEXCOORD0) : SV_Target
  328. {
  329.     float prevFocus = TexturePrevious.Sample(Sampler1, texcoord.xy).x;
  330.     float currFocus = TextureCurrent.Sample(Sampler1, texcoord.xy).x;
  331.  
  332.     float res = lerp(prevFocus, currFocus, DofParameters.w);
  333.     res = saturate(res);
  334.     return res;
  335. }
  336.  
  337. float4  PS_CoC(float2 texcoord : TEXCOORD0) : SV_Target
  338. {
  339.     float4 res      = TextureColor.Sample(Sampler1, texcoord.xy);
  340.     float scenecoc      = GetCoC(texcoord.xy);
  341.     res.w = scenecoc;
  342.     return res;
  343. }
  344.  
  345. float4  PS_DoF_Main(float2 texcoord : TEXCOORD0) : SV_Target
  346. {
  347.     float2 scaledcoord = texcoord.xy / fADOF_RenderResolutionMult;
  348.  
  349.     if(!all(saturate(-scaledcoord * scaledcoord + scaledcoord))) discard;
  350.     float4 scenecolor = TextureColor.Sample(Sampler1, scaledcoord.xy);
  351.  
  352.     float centerDepth = scenecolor.w;
  353.     float blurAmount = abs(centerDepth * 2.0 - 1.0);
  354.     float discRadius = blurAmount * fADOF_ShapeRadius;
  355.  
  356.         if(iADOF_TilShift == false)
  357.         {
  358.     discRadius*=(centerDepth < 0.5) ? (1.0 / max(fADOF_NearBlurCurve * 2.0, 1.0)) : 1.0;
  359.         }
  360.  
  361.     if(discRadius < 1.0) discard;
  362.  
  363.     scenecolor.xyz = BokehBlur(TextureColor,scaledcoord.xy, discRadius, centerDepth);
  364.     scenecolor.w = centerDepth;
  365.  
  366.     return scenecolor;
  367. }
  368.  
  369. float4  PS_DoF_Combine(float2 texcoord : TEXCOORD0) : SV_Target
  370. {
  371.     float4 blurredcolor         = TextureColor.Sample(Sampler1, texcoord.xy*fADOF_RenderResolutionMult);
  372.     float4 unblurredcolor       = TextureOriginal.Sample(Sampler1, texcoord.xy);
  373.     float4 scenecolor       = 0.0;
  374.     float centerDepth       = GetCoC(texcoord.xy);
  375.  
  376.     float discRadius = abs(centerDepth * 2.0 - 1.0) * fADOF_ShapeRadius;
  377.         if(iADOF_TilShift == false)
  378.         {
  379.     discRadius*=(centerDepth < 0.5) ? (1.0 / max(fADOF_NearBlurCurve * 2.0, 1.0)) : 1.0;
  380.         }
  381.     //1.0 + 0.05 epsilon because discard at 1.0 in PS_DoF_Main
  382.     scenecolor.xyz = lerp(blurredcolor.xyz, unblurredcolor.xyz,smoothstep(4.0,1.05,discRadius));
  383.  
  384.     scenecolor.w = centerDepth;
  385.     return scenecolor;
  386. }
  387.  
  388. float4  PS_DoF_Smoothen(float2 texcoord : TEXCOORD0) : SV_Target
  389. {
  390.     float4 scenecolor       = TextureColor.Sample(Sampler1, texcoord.xy);
  391.  
  392.     float centerDepth = scenecolor.w;
  393.     float blurAmount = abs(centerDepth * 2.0 - 1.0);
  394.     float blurFactor = smoothstep(0.0,0.15,blurAmount)*fADOF_SmootheningAmount;
  395.  
  396.     scenecolor = 0.0;
  397.  
  398.  
  399.     float offsets[5] = {-3.2307692308, -1.3846153846, 0.0, 1.3846153846, 3.2307692308};
  400.     float weights[3] = {0.2270270270, 0.3162162162, 0.0702702703};
  401.  
  402.     float chromaamount = 1.3;
  403.  
  404.     for(int x=-2; x<=2; x++)
  405.     for(int y=-2; y<=2; y++)
  406.     {
  407.         float2 coord = float2(x,y);
  408.         float2 actualoffset = float2(offsets[x+2],offsets[abs(y+2)])*blurFactor*PixelSize.xy;
  409.         float weight = weights[abs(x)] * weights[abs(y)];
  410.  
  411.                 float4 tap = TextureColor.SampleLevel(Sampler1, texcoord.xy + actualoffset,0);
  412.                 float tapblurAmount = abs(tap.w * 2.0 - 1.0);
  413.                 //weight *= (tapblurAmount >= blurAmount*0.2) ? 1.0 : 0.0;
  414.                 weight *= smoothstep(-0.0001,0.0,tapblurAmount-blurAmount);
  415.  
  416.                 scenecolor.xyz += tap.xyz * weight;
  417.                 scenecolor.w += weight;
  418.     }
  419.     scenecolor.xyz /= scenecolor.w;
  420.     return scenecolor;
  421.  
  422. }
  423.  
  424. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  425. // Techniques
  426. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  427.  
  428. //write aperture with time factor, this is always first technique
  429. technique11 Aperture
  430. {
  431.     pass p0
  432.     {
  433.         SetVertexShader(CompileShader(vs_5_0, VS_DOF()));
  434.         SetPixelShader(CompileShader(ps_5_0, PS_Aperture()));
  435.     }
  436. }
  437.  
  438. //compute focus from depth of screen and may be brightness, this is always second technique
  439. technique11 ReadFocus
  440. {
  441.     pass p0
  442.     {
  443.         SetVertexShader(CompileShader(vs_5_0, VS_DOF()));
  444.         SetPixelShader(CompileShader(ps_5_0, PS_ReadFocus()));
  445.     }
  446. }
  447.  
  448. //write focus with time factor, this is always third technique
  449. technique11 Focus
  450. {
  451.     pass p0
  452.     {
  453.         SetVertexShader(CompileShader(vs_5_0, VS_DOF()));
  454.         SetPixelShader(CompileShader(ps_5_0, PS_Focus()));
  455.     }
  456. }
  457.  
  458. technique11 DOF <string UIName="Marty McFly's ADOF";>
  459. {
  460.     pass p0
  461.     {
  462.         SetVertexShader(CompileShader(vs_5_0, VS_DOF()));
  463.         SetPixelShader(CompileShader(ps_5_0, PS_CoC()));
  464.     }
  465. }
  466.  
  467. technique11 DOF1
  468. {
  469.     pass p0
  470.     {
  471.         SetVertexShader(CompileShader(vs_5_0, VS_DOF()));
  472.         SetPixelShader(CompileShader(ps_5_0, PS_DoF_Main()));
  473.     }
  474. }
  475.  
  476. technique11 DOF2
  477. {
  478.     pass p0
  479.     {
  480.         SetVertexShader(CompileShader(vs_5_0, VS_DOF()));
  481.         SetPixelShader(CompileShader(ps_5_0, PS_DoF_Combine()));
  482.     }
  483. }
  484.  
  485. technique11 DOF3
  486. {
  487.     pass p0
  488.     {
  489.         SetVertexShader(CompileShader(vs_5_0, VS_DOF()));
  490.         SetPixelShader(CompileShader(ps_5_0, PS_DoF_Smoothen()));
  491.     }
  492. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement