Advertisement
Guest User

e

a guest
Nov 3rd, 2016
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.67 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.0b by Marty McFly
  6. // Port of Advanced Depth of Field 4.3 for ReShade
  7. // Do not redistribute without credits!
  8. // Beta version for testing
  9. // Copyright © 2008-2016 Marty McFly
  10. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  11.  
  12. #define bADOF_AutofocusEnable 1 //[0 or 1] Enables automated focus recognition based on samples around autofocus center.
  13. #define fADOF_AutofocusCenter float2(0.5, 0.5) //[0.0 to 1.0] X and Y coordinates of autofocus center. Axes start from upper left screen corner.
  14. #define iADOF_AutofocusSamples 6 //[3 to 10] Amount of samples around the focus point for smoother focal plane detection.
  15. #define fADOF_AutofocusRadius 0.02 //[0.02 to 0.2] Radius of samples around the focus point.
  16. #define fADOF_NearBlurCurve 1.00 //[0.5 to 5.0] Curve of blur closer than focal plane. Higher means less blur.
  17. #define fADOF_FarBlurCurve 1.00 //[0.5 to 5.0] Curve of blur behind focal plane. Higher means less blur.
  18. #define fADOF_NearBlurMult 1.0
  19. #define fADOF_FarBlurMult 1.0
  20. #define fADOF_ManualfocusDepth 0.02 //[0.0 to 1.0] Depth of focal plane when autofocus is off. 0.0 means camera, 1.0 means infinite distance.
  21. #define fADOF_InfiniteFocus 1.00 //[0.01 to 1.0] Distance at which depth is considered as infinite. 1.0 is standard. Low values only produce out of focus blur when focus object is very close to the camera. Recommended for gaming.
  22. #define fADOF_ShapeRadius 22.0
  23. #define iADOF_ShapeQuality 12
  24. #define iADOF_ShapeVertices 6
  25. #define fADOF_BokehCurve 4.0
  26. #define fADOF_ShapeRotation 15.0
  27.  
  28. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  29. //external enb parameters, do not modify
  30. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  31.  
  32. 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)
  33. float4 ScreenSize; //x = Width, y = 1/Width, z = aspect, w = 1/aspect, aspect is Width/Height
  34. float AdaptiveQuality; //changes in range 0..1, 0 means full quality, 1 lowest dynamic quality (0.33, 0.66 are limits for quality levels)
  35. 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.
  36. float4 TimeOfDay1; //x = dawn, y = sunrise, z = day, w = sunset. Interpolators range from 0..1
  37. float4 TimeOfDay2; //x = dusk, y = night. Interpolators range from 0..1
  38. float ENightDayFactor; //changes in range 0..1, 0 means that night time, 1 - day time
  39. float EInteriorFactor; //changes 0 or 1. 0 means that exterior, 1 - interior
  40. #define PixelSize float2(ScreenSize.y,ScreenSize.y*ScreenSize.z)
  41.  
  42. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  43. //external enb debugging parameters for shader programmers, do not modify
  44. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  45.  
  46. float4 tempF1; //0,1,2,3
  47. float4 tempF2; //5,6,7,8
  48. float4 tempF3; //9,0
  49. float4 tempInfo1; //float4(cursorpos.xy 0~1,isshaderwindowopen, mouse buttons)
  50. float4 tempInfo2; //float4(cursorpos.xy prev left mouse button click, cursorpos.xy prev right mouse button click)
  51.  
  52. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  53. //mod parameters, do not modify
  54. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  55.  
  56. float4 DofParameters; //z = ApertureTime multiplied by time elapsed, w = FocusingTime multiplied by time elapsed
  57. Texture2D TextureCurrent; //current frame focus depth or aperture. unused in dof computation
  58. Texture2D TexturePrevious; //previous frame focus depth or aperture. unused in dof computation
  59. Texture2D TextureOriginal; //color R16B16G16A16 64 bit hdr format
  60. Texture2D TextureColor; //color which is output of previous technique (except when drawed to temporary render target), R16B16G16A16 64 bit hdr format
  61. Texture2D TextureDepth; //scene depth R32F 32 bit hdr format
  62. Texture2D TextureFocus; //this frame focus 1*1 R32F hdr red channel only. computed in PS_Focus
  63. Texture2D TextureAperture; //this frame aperture 1*1 R32F hdr red channel only. computed in PS_Aperture
  64. 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!!!
  65. //temporary textures which can be set as render target for techniques via annotations like <string RenderTarget="RenderTargetRGBA32";>
  66. Texture2D RenderTargetRGBA32; //R8G8B8A8 32 bit ldr format
  67. Texture2D RenderTargetRGBA64; //R16B16G16A16 64 bit ldr format
  68. Texture2D RenderTargetRGBA64F; //R16B16G16A16F 64 bit hdr format
  69. Texture2D RenderTargetR16F; //R16F 16 bit hdr format with red channel only
  70. Texture2D RenderTargetR32F; //R32F 32 bit hdr format with red channel only
  71. Texture2D RenderTargetRGB32F; //32 bit hdr format without alpha
  72.  
  73. SamplerState Sampler0
  74. {
  75. Filter = MIN_MAG_MIP_POINT;
  76. AddressU = Clamp;
  77. AddressV = Clamp;
  78. };
  79. SamplerState Sampler1
  80. {
  81. Filter = MIN_MAG_MIP_LINEAR;
  82. AddressU = Clamp;
  83. AddressV = Clamp;
  84. };
  85.  
  86. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  87. //
  88. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  89.  
  90. void VS_DOF(in float3 inpos : POSITION, inout float2 txcoord0 : TEXCOORD0, out float4 outpos : SV_POSITION)
  91. {
  92. outpos = float4(inpos.xyz,1.0);
  93. }
  94.  
  95. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  96. //
  97. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  98.  
  99. float GetLinearDepth(float2 coords)
  100. {
  101. float depth = TextureDepth.SampleLevel(Sampler0, coords.xy,0).x;
  102. static const float N = 1.0;
  103. static const float F = 3000.0;
  104.  
  105. depth /= F - depth * (F - N);
  106. return depth;
  107. }
  108.  
  109. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  110. //
  111. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  112.  
  113. //? -> 1x1 R32F
  114. float4 PS_Aperture(float2 texcoord : TEXCOORD0) : SV_Target
  115. {
  116. return 1; //as I have no clue what this aperture thing does, don't waste resources :v
  117. }
  118.  
  119. //fullres -> 16x16 R32F
  120. float4 PS_ReadFocus(float2 texcoord : TEXCOORD0) : SV_Target
  121. {
  122. float scenefocus = fADOF_ManualfocusDepth;
  123. float2 coords = 0.0;
  124.  
  125. if(bADOF_AutofocusEnable != 0)
  126. {
  127. scenefocus = 0.0;
  128. float2 offsetVector = float2(1.0,0.0) * fADOF_AutofocusRadius;
  129. float Alpha = 6.2831853 / iADOF_AutofocusSamples;
  130. float2x2 rotMatrix = float2x2(cos(Alpha),-sin(Alpha),sin(Alpha),cos(Alpha));
  131.  
  132. for(int i=0; i<iADOF_AutofocusSamples; i++)
  133. {
  134. float2 currentOffset = fADOF_AutofocusCenter + offsetVector.xy;
  135. scenefocus += GetLinearDepth(currentOffset);
  136. offsetVector = mul(offsetVector,rotMatrix);
  137. }
  138.  
  139. scenefocus /= iADOF_AutofocusSamples;
  140. }
  141.  
  142. scenefocus = saturate(scenefocus);
  143. return scenefocus;
  144. }
  145.  
  146. //16x16 -> 1x1 R32F
  147. float4 PS_Focus(float2 texcoord : TEXCOORD0) : SV_Target
  148. {
  149. float prevFocus = TexturePrevious.Sample(Sampler0, texcoord.xy).x;
  150. float currFocus = TextureCurrent.Sample(Sampler0, texcoord.xy).x;
  151.  
  152. float res = lerp(prevFocus, currFocus, DofParameters.w);
  153. res = saturate(res);
  154. return res;
  155. }
  156.  
  157. //various -> fullres RGBA16F
  158. float4 PS_CoC(float2 texcoord : TEXCOORD0) : SV_Target
  159. {
  160. float4 res = TextureColor.Sample(Sampler1, texcoord.xy);
  161. float scenedepth = GetLinearDepth(texcoord.xy);
  162. float scenefocus = TextureFocus.Sample(Sampler0, texcoord.xy).x;
  163. float scenecoc = 0.0;
  164.  
  165. scenefocus = smoothstep(0.0,fADOF_InfiniteFocus,scenefocus);
  166. scenedepth = smoothstep(0.0,fADOF_InfiniteFocus,scenedepth);
  167.  
  168. float farBlurDepth = scenefocus*pow(4.0,fADOF_FarBlurCurve);
  169.  
  170. if(scenedepth < scenefocus)
  171. {
  172. scenecoc = (scenedepth - scenefocus) / scenefocus;
  173. scenecoc *= fADOF_NearBlurMult;
  174. }
  175. else
  176. {
  177. scenecoc=(scenedepth - scenefocus)/(farBlurDepth - scenefocus);
  178. scenecoc *= fADOF_FarBlurMult;
  179. scenecoc=saturate(scenecoc);
  180. }
  181.  
  182. scenecoc = saturate(scenecoc * 0.5 + 0.5);
  183. res.w = scenecoc;
  184. return res;
  185. }
  186.  
  187. float3 BokehBlur(Texture2D colortex, float2 coords, float discRadius, float centerDepth)
  188. {
  189. float4 res = float4(TextureColor.Sample(Sampler1, coords.xy).xyz, 1.0);
  190. int ringCount = round(lerp(0.0,(float)iADOF_ShapeQuality,discRadius/fADOF_ShapeRadius));
  191. float2 discRadiusInPixels = discRadius*PixelSize.xy;
  192.  
  193. float2 edgeVertices[iADOF_ShapeVertices+1];
  194.  
  195. res.xyz = pow(res.xyz,fADOF_BokehCurve)*res.w;
  196.  
  197. float2 currentVertex;
  198. sincos(fADOF_ShapeRotation*0.0174533,currentVertex.y,currentVertex.x);
  199. float Alpha = 6.2831853 / iADOF_ShapeVertices;
  200. float2x2 rotMatrix = float2x2(cos(Alpha),-sin(Alpha),sin(Alpha),cos(Alpha));
  201.  
  202. [unroll]
  203. for(int z = 0; z <= iADOF_ShapeVertices; z++)
  204. {
  205. edgeVertices[z].xy = currentVertex;
  206. currentVertex.xy = mul(currentVertex.xy, rotMatrix); //yes I like rotation matrices. NOW HATE ME
  207. }
  208.  
  209. [fastopt]
  210. for(float i = 1; i <= ringCount; i++)
  211. {
  212. [fastopt]
  213. for (int j = 1; j <= iADOF_ShapeVertices; j++)
  214. {
  215. float radiusCoeff = i/ringCount;
  216. float blursamples = i;
  217.  
  218. [fastopt]
  219. for (float k = 0; k < blursamples; k++)
  220. {
  221. float2 sampleOffset = lerp(edgeVertices[j-1].xy,edgeVertices[j].xy,k/blursamples) * radiusCoeff;
  222.  
  223. float4 tap = colortex.SampleLevel(Sampler1, coords.xy + sampleOffset.xy * discRadiusInPixels,0);
  224. tap.w = (tap.w >= centerDepth*0.99) ? 1.0 : pow(abs(tap.w * 2.0 - 1.0),4.0);
  225.  
  226. res.xyz += pow(tap.xyz,fADOF_BokehCurve)*tap.w;
  227. res.w += tap.w;
  228. }
  229. }
  230. }
  231. res.xyz = max(res.xyz/res.w,0.0);
  232. return pow(res.xyz,1.0/fADOF_BokehCurve);
  233. }
  234.  
  235. float4 PS_DoF(float2 texcoord : TEXCOORD0) : SV_Target
  236. {
  237. float4 scenecolor = TextureColor.Sample(Sampler1, texcoord.xy);
  238.  
  239. float centerDepth = scenecolor.w;
  240. float blurAmount = abs(centerDepth * 2.0 - 1.0);
  241. float discRadius = blurAmount * fADOF_ShapeRadius;
  242.  
  243. discRadius*=(centerDepth < 0.5) ? (1.0 / max(fADOF_NearBlurCurve * 2.0, 1.0)) : 1.0;
  244.  
  245. scenecolor.xyz = BokehBlur(TextureColor,texcoord.xy, discRadius, centerDepth);
  246. //scenecolor.xyz = (discRadius > 1.2) ? BokehBlur(TextureColor,texcoord.xy, discRadius, centerDepth) : scenecolor.xyz;
  247. scenecolor.w = 1.0;
  248. //return GetLinearDepth(texcoord.xy);
  249. return scenecolor;
  250. }
  251.  
  252. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  253. //
  254. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  255.  
  256. //write aperture with time factor, this is always first technique
  257. technique11 Aperture
  258. {
  259. pass p0
  260. {
  261. SetVertexShader(CompileShader(vs_5_0, VS_DOF()));
  262. SetPixelShader(CompileShader(ps_5_0, PS_Aperture()));
  263. }
  264. }
  265.  
  266. //compute focus from depth of screen and may be brightness, this is always second technique
  267. technique11 ReadFocus
  268. {
  269. pass p0
  270. {
  271. SetVertexShader(CompileShader(vs_5_0, VS_DOF()));
  272. SetPixelShader(CompileShader(ps_5_0, PS_ReadFocus()));
  273. }
  274. }
  275.  
  276. //write focus with time factor, this is always third technique
  277. technique11 Focus
  278. {
  279. pass p0
  280. {
  281. SetVertexShader(CompileShader(vs_5_0, VS_DOF()));
  282. SetPixelShader(CompileShader(ps_5_0, PS_Focus()));
  283. }
  284. }
  285.  
  286. technique11 DOF <string UIName="DOF";>
  287. {
  288. pass p0
  289. {
  290. SetVertexShader(CompileShader(vs_5_0, VS_DOF()));
  291. SetPixelShader(CompileShader(ps_5_0, PS_CoC()));
  292. }
  293. }
  294.  
  295. technique11 DOF1 <string UIName="DOF";>
  296. {
  297. pass p0
  298. {
  299. SetVertexShader(CompileShader(vs_5_0, VS_DOF()));
  300. SetPixelShader(CompileShader(ps_5_0, PS_DoF()));
  301. }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement