Advertisement
Guest User

Perfect Perspective ver. 2.3.3+MOD

a guest
Sep 25th, 2018
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. /*
  2. Copyright (c) 2018 Jacob Maximilian Fober
  3.  
  4. This work is licensed under the Creative Commons
  5. Attribution-ShareAlike 4.0 International License.
  6. To view a copy of this license, visit
  7. http://creativecommons.org/licenses/by-sa/4.0/.
  8. */
  9.  
  10. // Perfect Perspective PS ver. 2.3.3+MOD
  11.  
  12. // Modifications by u/carlo_ren79:
  13. //
  14. // - Added supersampling (up to 8x8) to remove aliasing.
  15. // - Speedup by removing border blend options (always black color) and using BORDER texture addressing.
  16. // - Minor speedup by moving initialization of SqrTanFOVq to VS.
  17.  
  18. ////////////////////
  19. /////// MENU ///////
  20. ////////////////////
  21.  
  22. #ifndef ShaderAnalyzer
  23. uniform int FOV <
  24. ui_label = "Field of View";
  25. ui_tooltip = "Match in-game Field of View";
  26. ui_type = "drag";
  27. ui_min = 45; ui_max = 120;
  28. ui_category = "Distortion";
  29. > = 90;
  30.  
  31. uniform float Vertical <
  32. ui_label = "Vertical Amount";
  33. ui_tooltip = "0.0 - cylindrical projection \n"
  34. "1.0 - spherical projection";
  35. ui_type = "drag";
  36. ui_min = 0.0; ui_max = 1.0;
  37. ui_category = "Distortion";
  38. > = 0.618;
  39.  
  40. uniform int Type <
  41. ui_label = "Type of FOV";
  42. ui_tooltip = "If the image bulges in movement (too high FOV), change it to 'Diagonal' \n"
  43. "When proportions are distorted at the periphery (too low FOV), choose 'Vertical'";
  44. ui_type = "combo";
  45. ui_items = "Horizontal FOV\0Diagonal FOV\0Vertical FOV\0";
  46. ui_category = "Distortion";
  47. > = 0;
  48.  
  49. uniform float Zooming <
  50. ui_label = "Border Scale";
  51. ui_type = "drag";
  52. ui_min = 0.0; ui_max = 3.0; ui_step = 0.001;
  53. ui_category = "Borders";
  54. > = 1.0;
  55.  
  56. uniform bool Debug <
  57. ui_label = "Display Resolution Map";
  58. ui_tooltip = "Color map of the Resolution Scale \n"
  59. " Red - Undersampling \n"
  60. " Green - Supersampling \n"
  61. " Blue - Neutral sampling";
  62. ui_category = "Debug Tools";
  63. > = false;
  64.  
  65. uniform float ResScale <
  66. ui_label = "DSR scale factor";
  67. ui_tooltip = "(DSR) Dynamic Super Resolution... \n"
  68. "Simulate application running beyond-native screen resolution";
  69. ui_type = "drag";
  70. ui_min = 1.0; ui_max = 8.0; ui_step = 0.02;
  71. ui_category = "Debug Tools";
  72. > = 1.0;
  73.  
  74. uniform int SupersamplingChoice <
  75. ui_label = "Supersampling level";
  76. ui_category = "Quality";
  77. ui_type = "combo";
  78. ui_items = " 1x1\0 2x2\0 3x3\0 4x4\0 5x5\0 6x6\0 7x7\0 8x8\0";
  79. > = 0;
  80.  
  81. #endif
  82.  
  83.  
  84. //////////////////////
  85. /////// SHADER ///////
  86. //////////////////////
  87.  
  88. #include "ReShade.fxh"
  89.  
  90. sampler SamplerColor
  91. {
  92. Texture = ReShade::BackBufferTex;
  93. SRGBTexture = true;
  94. AddressU = BORDER;
  95. AddressV = BORDER;
  96. };
  97.  
  98. // Stereographic-Gnomonic lookup function by Jacob Max Fober
  99. // Input data:
  100. // FOV >> Camera Field of View in degrees
  101. // Coordinates >> UV coordinates (from -1, to 1), where (0,0) is at the center of the screen
  102. float Formula(float2 Coordinates, float SqrTanFOVq)
  103. {
  104. return (1.0 - SqrTanFOVq) / (1.0 - SqrTanFOVq * dot(Coordinates, Coordinates));
  105. }
  106.  
  107. // Shader pass
  108. float3 PerfectPerspectivePS(float4 vois : SV_Position, float4 texcoord : TexCoord) : SV_Target
  109. {
  110. // Get Aspect Ratio
  111. float AspectR = BUFFER_HEIGHT * BUFFER_RCP_WIDTH;
  112.  
  113. // Convert FOV type..
  114. float FovType = (Type == 1) ? sqrt(AspectR * AspectR + 1.0) : Type == 2 ? AspectR : 1.0;
  115.  
  116. const float Supersampling = SupersamplingChoice + 1.0;
  117.  
  118. const float dtx = BUFFER_RCP_WIDTH / Supersampling;
  119. const float dty = BUFFER_RCP_HEIGHT / Supersampling;
  120.  
  121. // Sample display image
  122. float3 Display = float3(0.0, 0.0, 0.0);
  123. float2 SphCoord;
  124.  
  125. for(float y = 0; y < Supersampling; ++y)
  126. {
  127. for(float x = 0; x < Supersampling; ++x)
  128. {
  129. float2 offset = float2(x * dtx, y * dty);
  130.  
  131. // Convert UV to Radial Coordinates
  132. SphCoord = (texcoord.xy + offset) * 2.0 - 1.0;
  133.  
  134. // Aspect Ratio correction
  135. SphCoord.y *= AspectR;
  136.  
  137. // Zoom in image and adjust FOV type (pass 1 of 2)
  138. SphCoord *= Zooming / FovType;
  139.  
  140. // Stereographic-Gnomonic lookup, vertical distortion amount and FOV type (pass 2 of 2)
  141. SphCoord *= Formula(float2(SphCoord.x, sqrt(Vertical) * SphCoord.y), texcoord.z) * FovType;
  142.  
  143. // Aspect Ratio back to square
  144. SphCoord.y /= AspectR;
  145.  
  146. // Back to UV Coordinates
  147. SphCoord = SphCoord * 0.5 + 0.5;
  148.  
  149. Display += tex2D(SamplerColor, SphCoord).rgb;
  150. }
  151. }
  152.  
  153. Display /= Supersampling * Supersampling;
  154.  
  155. // Output type choice
  156. if (Debug)
  157. {
  158. // Calculate radial screen coordinates before and after perspective transformation
  159. float4 RadialCoord = float4(texcoord.xy, SphCoord) * 2 - 1;
  160. // Correct vertical aspect ratio
  161. RadialCoord.yw *= AspectR;
  162.  
  163. // Define Mapping color
  164. float3 UnderSmpl = float3(1, 0, 0.2); // Red
  165. float3 SuperSmpl = float3(0, 1, 0.5); // Green
  166. float3 NeutralSmpl = float3(0, 0.5, 1); // Blue
  167.  
  168. // Calculate Pixel Size difference...
  169. float PixelScale = fwidth( length(RadialCoord.xy) );
  170. // ...and simulate Dynamic Super Resolution (DSR) scalar
  171. PixelScale /= ResScale * fwidth( length(RadialCoord.zw) );
  172. PixelScale -= 1;
  173.  
  174. // Generate supersampled-undersampled color map
  175. float3 ResMap = lerp(
  176. SuperSmpl,
  177. UnderSmpl,
  178. saturate(ceil(PixelScale))
  179. );
  180.  
  181. // Create black-white gradient mask of scale-neutral pixels
  182. PixelScale = 1 - abs(PixelScale);
  183. PixelScale = saturate(PixelScale * 4 - 3); // Clamp to more representative values
  184.  
  185. // Color neutral scale pixels
  186. ResMap = lerp(ResMap, NeutralSmpl, PixelScale);
  187.  
  188. // Blend color map with display image
  189. Display = normalize(ResMap) * (0.8 * max( max(Display.r, Display.g), Display.b ) + 0.2);
  190. }
  191.  
  192. return Display;
  193. }
  194.  
  195. // Vertex shader generating a triangle covering the entire screen
  196. void PerfectPerspectiveVS(in uint id : SV_VertexID, out float4 position : SV_Position, out float3 texcoord : TEXCOORD)
  197. {
  198. texcoord.x = (id == 2) ? 2.0 : 0.0;
  199. texcoord.y = (id == 1) ? 2.0 : 0.0;
  200.  
  201. // Convert 1/4 FOV to radians and calc tangent squared
  202. texcoord.z = tan(radians(float(FOV) * 0.25));
  203. texcoord.z *= texcoord.z;
  204.  
  205. position = float4(texcoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
  206. }
  207. technique PerfectPerspective
  208. {
  209. pass
  210. {
  211. VertexShader = PerfectPerspectiveVS;
  212. PixelShader = PerfectPerspectivePS;
  213. SRGBWriteEnable = true;
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement