Advertisement
Guest User

ReShade.fx

a guest
Jul 1st, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.08 KB | None | 0 0
  1. /** @file ReShade.fx
  2.  *  
  3.  *  @brief This shader is based on physiological characteristics of the human eye and it is
  4.  *         designed to provide a natural look of dark scenes or low light environments.
  5.  *         It is also a good prevention of the abuse of the gamma and brightness settings
  6.  *         without restricting these settings them selfs.
  7.  *  
  8.  *  For further information goto:
  9.  *  https://feedback.bistudio.com/T87715
  10.  *  
  11.  *  
  12.  *  Installation and use:
  13.  *  
  14.  *  IMPORTANT NOTE: ReShade hooks into the renderer and thus it might modify the memory
  15.  *  of the running game. This could be classified as a sign of cheating/hacking and because
  16.  *  of this it is recommended to test the shader in singleplayer/editor with disconnected
  17.  *  networking. After testing and before reconnecting you should uninstall it by deleting
  18.  *  the two files you copied during the installation. That might sound a bit paranoid,
  19.  *  but better safe than sorry... :)
  20.  *  
  21.  *  
  22.  *  During the installation you need to rename two files and change their file extensions too.
  23.  *  For this you need to activate/show the file extensions in the windows explorer, if you
  24.  *  haven't done it already:
  25.  *  http://windows.microsoft.com/en-us/windows/show-hide-file-name-extensions
  26.  *  
  27.  *  1. Download ReShade
  28.  *     During the testing Reshade is needed for hooking into the renderer and
  29.  *     loading/starting/toggling the shader. It is available at http://reshade.me/
  30.  *  
  31.  *  2. Copy ReShade32.dll from the downloaded archive into ArmAs main directory
  32.  *     (the same directory like the arma2.exe/ArmA2OA.exe/arma3.exe)
  33.  *  
  34.  *  3. For ArmA 2 (OA/CO) rename the copied ReShade32.dll to d3d9.dll, for ArmA 3
  35.  *     rename it to dxgi.dll
  36.  *  
  37.  *  4. If the shader file (the one you are reading now) isn't named ReShade.fx
  38.  *     rename it ReShade.fx
  39.  *  
  40.  *  5. Copy this ReShade.fx into ArmAs main directory too.
  41.  *  
  42.  *  
  43.  *  That's it. If you now start ArmA, ReShade should load the shader.
  44.  *  - Press [Insert] to toggle the whole shader
  45.  *  - Press [Delete] to toggle the blur
  46.  *  - Press [End]    to toggle the rod saturation
  47.  *  - Press [PgDwn]  to toggle the noise
  48.  *  - Press [PgUp]   to toggle the noise type
  49.  *  - Press [Home]   to toggle gamma
  50.  *  
  51.  *  It might be helpful to note that ReShade is not compatible with software like screen
  52.  *  capturing or overlays and you would need to deactivate them in order to test the shader.
  53.  *  To take screenshots nevertheless ReShade brings its own screenshot functionality:
  54.  *  - Press [Print]  to take a screenshot
  55.  *  The screenshots are saved to the same directory where you put the shader files.
  56.  *  
  57.  */
  58.  
  59.  
  60. /** @brief The (maximum) filter radius used for the static and the adaptive blurring
  61.  *  
  62.  *  It is defined as th ratio of filter radius and screen width.
  63.  *  The default is MAX_RADIUS=0.011; this means the radius will be 1.1% of the screen width.
  64.  *  
  65.  *  Range: [0.005 to 0.05]
  66.  */
  67. #define MAX_RADIUS 0.011
  68.  
  69.  
  70. /** @brief Value to configure the mapping of the luminance to the amount of the blur
  71.  *  
  72.  *  To calculate the amount of blur, noise and other values applied to each pixel
  73.  *  based on its luminance or the luminance of its neighborhood the function
  74.  *  f(x)=1/((2^A*x)^B+1) is used.
  75.  *  
  76.  *  In this case is A = BLUR_CONST_A
  77.  *  
  78.  *  Range: [3.5 to 7.0]
  79.  */
  80. #define BLUR_CONST_A 6.0
  81.  
  82.  
  83. /** @brief Value to configure the mapping of the luminance to the amount of the blur
  84.  *  
  85.  *  To calculate the amount of blur, noise and other values applied to each pixel
  86.  *  based on its luminance or the luminance of its neighborhood the function
  87.  *  f(x)=1/((2^A*x)^B+1) is used.
  88.  *  
  89.  *  In this case is B = BLUR_CONST_B
  90.  *  
  91.  *  Range: [2.0 to 5.0]
  92.  */
  93. #define BLUR_CONST_B 4.0
  94.  
  95.  
  96. /** @brief Value to configure the mapping of the luminance to the amount of the noise
  97.  *  
  98.  *  @see BLUR_CONST_A
  99.  *  
  100.  *  Range: [3.5 to 7.0]
  101.  */
  102. #define NOISE_CONST_A 4.5
  103.  
  104.  
  105. /** @brief Value to configure the mapping of the luminance to the amount of the noise
  106.  *  
  107.  *  @see BLUR_CONST_B
  108.  *  
  109.  *  Range: [2.0 to 5.0]
  110.  */
  111. #define NOISE_CONST_B 3.0
  112.  
  113.  
  114. /** @brief Value to configure the mapping of the luminance to the amount of visible saturation
  115.  *  
  116.  *  @see BLUR_CONST_A
  117.  *  
  118.  *  Range: [3.5 to 7.0]
  119.  */
  120. #define SATURATION_CONST_A 4.0
  121.  
  122.  
  123. /** @brief Value to configure the mapping of the luminance to the amount of visible saturation
  124.  *  
  125.  *  @see BLUR_CONST_B
  126.  *  
  127.  *  Range: [2.0 to 5.0]
  128.  */
  129. #define SATURATION_CONST_B 2.0
  130.  
  131.  
  132. /** @brief Time constant in milliseconds used for the calculation of the saturation of the
  133.  *         rod cells
  134.  *  
  135.  *  Range: [50.0 to 500.0]
  136.  */
  137. #define ROD_SATURATION_TAU 200.0
  138.  
  139.  
  140. /** @brief Time constant in milliseconds used for the calculation of the desaturation of the
  141.  *         rod cells
  142.  *  
  143.  *  Range: [500.0 to 5000.0]
  144.  */
  145. #define ROD_DESATURATION_TAU 5000.0
  146.  
  147.  
  148. /** @brief Value to configure the blurring of pixels in the neighborhood of bright pixels
  149.  *  
  150.  *  @see BLUR_CONST_A
  151.  *  
  152.  *  Range: [3.5 to 7.0]
  153.  */
  154. #define DEBLOOM_CONST_A 6.0
  155.  
  156.  
  157. /** @brief Value to configure the blurring of pixels in the neighborhood of bright pixels
  158.  *  
  159.  *  @see BLUR_CONST_B
  160.  *  
  161.  *  Range: [2.0 to 5.0]
  162.  */
  163. #define DEBLOOM_CONST_B 4.0
  164.  
  165. #define KERNEL_RADIUS 8
  166.  
  167.  
  168. #pragma message "\n"
  169. #pragma message "Press [Insert] to toggle the whole shader\n"
  170. #pragma message "Press [Delete] to toggle the blur\n"
  171. #pragma message "Press [End]    to toggle the rod saturation\n"
  172. #pragma message "Press [PgDwn]  to toggle the noise\n"
  173. #pragma message "Press [PgUp]   to toggle the noise type\n"
  174. #pragma message "Press [Home]   to toggle gamma\n"
  175. #pragma message "Press [Print]  to take a screenshot\n"
  176.  
  177. #pragma reshade showstatistics
  178. #pragma reshade screenshot_format png
  179.  
  180.  
  181. /** @brief Total frame count since the start of the game
  182.  *  
  183.  */
  184. uniform int framecount < source = "framecount"; >;
  185.  
  186. /** @brief The time passed since the previous frame in milliseconds
  187.  *  
  188.  */
  189. uniform float frametime < source = "frametime"; >;
  190.  
  191. /** @brief Uniform to toggle the blur
  192.  *  
  193.  *  Key: [DEL]
  194.  */
  195. uniform bool blurOn < source = "key"; keycode = 0x2E; toggle = true; >;
  196.  
  197. /** @brief Uniform to toggle the saturation
  198.  *  
  199.  *  Key: [END]
  200.  */
  201. uniform bool saturationOn < source = "key"; keycode = 0x23; toggle = true; >;
  202.  
  203. /** @brief Uniform to toggle the noise type
  204.  *  
  205.  *  Key: [PgUp]
  206.  */
  207. uniform bool noiseType < source = "key"; keycode = 0x21; toggle = true; >;
  208.  
  209. /** @brief Uniform to toggle the noise
  210.  *  
  211.  *  Key: [PgDwn]
  212.  */
  213. uniform bool noiseOn < source = "key"; keycode = 0x22; toggle = true; >;
  214.  
  215. static const float3 LUMCOEFF = float3(0.2126, 0.7152, 0.0722);
  216.  
  217. /** @brief Input
  218.  *  
  219.  */
  220. texture texColorBuffer : COLOR;
  221.  
  222. texture texBlurredLuminance
  223. {
  224.   Width = BUFFER_WIDTH;
  225.   Height = BUFFER_HEIGHT;
  226.   MipLevels = 1;
  227.   Format = R8;
  228. };
  229.  
  230. texture texRodSaturation
  231. {
  232.   Width = BUFFER_WIDTH;
  233.   Height = BUFFER_HEIGHT;
  234.   MipLevels = 1;
  235.   Format = R32F;
  236. };
  237.  
  238. texture texBlurred
  239. {
  240.   Width = BUFFER_WIDTH;
  241.   Height = BUFFER_HEIGHT;
  242.   MipLevels = 1;
  243.   Format = RGBA8;
  244. };
  245.  
  246. texture texR8Temp
  247. {
  248.   Width = BUFFER_WIDTH;
  249.   Height = BUFFER_HEIGHT;
  250.   MipLevels = 1;
  251.   Format = R8;
  252. };
  253.  
  254. texture texRGBA8Temp
  255. {
  256.   Width = BUFFER_WIDTH;
  257.   Height = BUFFER_HEIGHT;
  258.   MipLevels = 1;
  259.   Format = RGBA8;
  260. };
  261.  
  262. texture texR32FTemp
  263. {
  264.   Width = BUFFER_WIDTH;
  265.   Height = BUFFER_HEIGHT;
  266.   MipLevels = 1;
  267.   Format = R32F;
  268. };
  269.  
  270.  
  271. sampler samplerColor
  272. {
  273.   Texture = texColorBuffer;
  274.   AddressU = CLAMP;
  275.   AddressV = CLAMP;
  276.   AddressW = CLAMP;
  277.   MinFilter = LINEAR;
  278.   MagFilter = LINEAR;
  279.   MipFilter = LINEAR;
  280.   MipLODBias = 0.0f;
  281.   MinLOD = -1000.0f;
  282.   MaxLOD = 1000.0f;
  283.   MaxAnisotropy = 1;
  284.   SRGBTexture = FALSE;
  285. };
  286.  
  287. sampler samplerBlurredLuminance
  288. {
  289.   Texture = texBlurredLuminance;
  290. };
  291.  
  292. sampler samplerRodSaturation
  293. {
  294.   Texture = texRodSaturation;
  295. };
  296.  
  297. sampler samplerBlurred
  298. {
  299.   Texture = texBlurred;
  300. };
  301.  
  302. sampler samplerR8Temp
  303. {
  304.   Texture = texR8Temp;
  305. };
  306.  
  307. sampler samplerRGBA8Temp
  308. {
  309.   Texture = texRGBA8Temp;
  310. };
  311.  
  312. sampler samplerR32FTemp
  313. {
  314.   Texture = texR32FTemp;
  315. };
  316.  
  317.  
  318. /** @brief Simple 3D value noise
  319.  *  
  320.  */
  321. float sNoise(float3 p)
  322. {
  323.   return frac(sin(dot(p, float3(12.9898, 78.233, 41.5435))) * 43758.5453);
  324. }
  325.  
  326. /** @brief Sum of simple 3D value noise at 4 different frequencies
  327.  *  
  328.  *  frequencies = {1.0, 2.0, 4.0, 8.0}*f0
  329.  */
  330. float fNoise(float3 p)
  331. {
  332.   return (sNoise(floor(p)) + sNoise(floor(p*2.0)) + sNoise(floor(p*4.0)) + sNoise(floor(p*8.0))) / 4.0;
  333. }
  334.  
  335. /** @brief Trilinearly interpolated simple 3D value noise
  336.  *  
  337.  */
  338. float iNoise(float3 p)
  339. {
  340.   float3 floorP = floor(p);
  341.   float3 fractP = p-floorP;
  342.  
  343.   float v0 = sNoise(floorP+float3(0.0, 0.0, 0.0));
  344.   float v1 = sNoise(floorP+float3(0.0, 0.0, 1.0));
  345.   float v2 = sNoise(floorP+float3(0.0, 1.0, 0.0));
  346.   float v3 = sNoise(floorP+float3(0.0, 1.0, 1.0));
  347.   float v4 = sNoise(floorP+float3(1.0, 0.0, 0.0));
  348.   float v5 = sNoise(floorP+float3(1.0, 0.0, 1.0));
  349.   float v6 = sNoise(floorP+float3(1.0, 1.0, 0.0));
  350.   float v7 = sNoise(floorP+float3(1.0, 1.0, 1.0));
  351.  
  352.   v0 = lerp(v0, v1, fractP.z);
  353.   v2 = lerp(v2, v3, fractP.z);
  354.   v4 = lerp(v4, v5, fractP.z);
  355.   v6 = lerp(v6, v7, fractP.z);
  356.  
  357.   v0 = lerp(v0, v2, fractP.y);
  358.   v4 = lerp(v4, v6, fractP.y);
  359.  
  360.   v0 = lerp(v0, v4, fractP.x);
  361.  
  362.   return v0;
  363. }
  364.  
  365. /** @brief Sum of trilinearly interpolated 3D value noise at 4 different frequencies
  366.  *  
  367.  *  frequencies = {1.0, 2.0, 4.0, 8.0}*f0
  368.  */
  369. float fiNoise(float3 p)
  370. {
  371.   return (iNoise(p) + iNoise(p*2.0) + iNoise(p*4.0) + iNoise(p*8.0)) / 4.0;
  372. }
  373.  
  374.  
  375. /** @brief Vertex shader to generate a simple fullscreen triangle with the three vertices
  376.  *         provided by ReShade
  377.  *  
  378.  *  See http://redd.it/2j17wk for additional information.
  379.  */
  380. void PostProcessingVS(
  381.   in uint id : SV_VertexID,
  382.   out float4 position : SV_Position,
  383.   out float2 texcoord : TEXCOORD0)
  384. {
  385.   texcoord = float2((id << 1) & 2, id & 2);
  386.   position = float4(texcoord * float2(2, -2) + float2(-1, 1), 0, 1);
  387. }
  388.  
  389.  
  390. /** @brief Simulates the (de)saturation of the rod cells
  391.  *  
  392.  *  - The saturation makes the value converge to 1.0; the rate of convergence is dependent
  393.  *    on the luminance.
  394.  *  - The desaturation makes the value converge to 0.0; the rate of convergence is fixed.
  395.  */
  396. void CalcRodSaturationPS(
  397.   in float4 pos : SV_Position,
  398.   in float2 texcoord : TEXCOORD0,
  399.   out float newSat : SV_Target)
  400. {
  401.   float oldSat = tex2D(samplerRodSaturation, texcoord).r;
  402.   float lum = tex2D(samplerBlurredLuminance, texcoord).r;
  403.  
  404.   //saturation
  405.   float scalar = exp(-frametime*lum*lum*2.0/ROD_SATURATION_TAU);
  406.   newSat = lerp(1.0, oldSat, scalar);
  407.  
  408.   //desaturation
  409.   newSat *= exp(-frametime/ROD_DESATURATION_TAU);
  410.  
  411.   saturate(newSat);
  412. }
  413.  
  414.  
  415. /** @brief Is used to copy the result of CalcRodSaturationPS to texRodSaturation
  416.  *  
  417.  *  This is necessary because the render target can not be written and read at the same time.
  418.  */
  419. void CopyRodSaturationPS(
  420.   in float4 pos : SV_Position,
  421.   in float2 texcoord : TEXCOORD0,
  422.   out float sat : SV_Target)
  423. {
  424.   sat = tex2D(samplerR32FTemp, texcoord).r;
  425. }
  426.  
  427.  
  428. /** @brief Is used in combination with BlurLuminanceVerticalPS to calculate the blurred luminance
  429.  *  
  430.  *  The luminance is calculated in this step.
  431.  */
  432. void BlurLuminanceHorizontalPS(
  433.   in float4 pos : SV_Position,
  434.   in float2 texcoord : TEXCOORD0,
  435.   out float lum : SV_Target)
  436. {
  437.   static const float gauss[17] = {0.011421, 0.019353, 0.030565, 0.044996, 0.061744,
  438.                                   0.078971, 0.094148, 0.104620, 0.108363, 0.104620,
  439.                                   0.094148, 0.078971, 0.061744, 0.044996, 0.030565,
  440.                                   0.019353, 0.011421};
  441.  
  442.   float blurStepsizeX = MAX_RADIUS/KERNEL_RADIUS;
  443.   float3 sum = float3(0.0, 0.0, 0.0);
  444.   for (int x=-KERNEL_RADIUS; x<=KERNEL_RADIUS; x++)
  445.     sum += gauss[x+KERNEL_RADIUS] * tex2D(samplerColor, float2(texcoord + float2(blurStepsizeX*x, 0.0))).rgb;
  446.  
  447.   lum = dot(sum, LUMCOEFF);
  448. }
  449.  
  450.  
  451. /** @brief Is used in combination with BlurLuminanceHorizontalPS to calculate the blurred luminance
  452.  *  
  453.  *  The result is the MAXIMUM of the gaussian-weighted average luminance of the neighborhood and
  454.  *  the original luminance. You need to take the maximum to preserve small, bright objects like
  455.  *  stars or other distant lightsources on a dark background. These pixels may contain important
  456.  *  information, but they have too little influence on the weighted average luminance of their
  457.  *  neighborhood and would get "blurred away" otherwise.
  458.  */
  459. void BlurLuminanceVerticalPS(
  460.   in float4 pos : SV_Position,
  461.   in float2 texcoord : TEXCOORD0,
  462.   out float lum : SV_Target)
  463. {
  464.   static const float gauss[17] = {0.011421, 0.019353, 0.030565, 0.044996, 0.061744,
  465.                                   0.078971, 0.094148, 0.104620, 0.108363, 0.104620,
  466.                                   0.094148, 0.078971, 0.061744, 0.044996, 0.030565,
  467.                                   0.019353, 0.011421};
  468.  
  469.   float blurStepsizeY = MAX_RADIUS/KERNEL_RADIUS * BUFFER_WIDTH/BUFFER_HEIGHT;
  470.   float sum = 0.0;
  471.   for (int y=-KERNEL_RADIUS; y<=KERNEL_RADIUS; y++)
  472.     sum += gauss[y+KERNEL_RADIUS] * tex2D(samplerR8Temp, float2(texcoord + float2(0.0, blurStepsizeY*y))).r;
  473.  
  474.   lum = max(sum, dot(tex2D(samplerColor, texcoord).rgb, LUMCOEFF));
  475. }
  476.  
  477.  
  478. /** @brief Is used in combination with BlurFrameVerticalPS to blur the current frame
  479.  *         according to the blurred luminance
  480.  *
  481.  *  To avoid the bloom the blurring would generate on dark areas around bright areas the
  482.  *  algorithm is slightly modified; the more the luminance of a pixel differs from the
  483.  *  luminance of the original pixel the less it influences the result.
  484.  */
  485. void BlurFrameHorizontalPS(
  486.   in float4 pos : SV_Position,
  487.   in float2 texcoord : TEXCOORD0,
  488.   out float4 color : SV_Target)
  489. {
  490.   static const float gauss[17] = {0.011421, 0.019353, 0.030565, 0.044996, 0.061744,
  491.                                   0.078971, 0.094148, 0.104620, 0.108363, 0.104620,
  492.                                   0.094148, 0.078971, 0.061744, 0.044996, 0.030565,
  493.                                   0.019353, 0.011421};
  494.  
  495.   color = tex2D(samplerColor, texcoord);
  496.   float lumColor = dot(color.rgb, LUMCOEFF);
  497.  
  498.   float blur = 1.0/(pow(exp2(BLUR_CONST_A)*tex2D(samplerBlurredLuminance, texcoord).r, BLUR_CONST_B)+1.0);
  499.  
  500.   float blurStepsizeX = MAX_RADIUS/KERNEL_RADIUS * blur;
  501.   float3 sum = float3(0.0, 0.0, 0.0);
  502.   for (int x=-KERNEL_RADIUS; x<=KERNEL_RADIUS; x++)
  503.   {
  504.     float3 color2 = tex2D( samplerColor, texcoord+float2(blurStepsizeX*x, 0.0) ).rgb;
  505.     float lumColor2 = dot(color2, LUMCOEFF);
  506.    
  507.     float debloomScalar = 1.0/(pow( exp2(DEBLOOM_CONST_A)*abs(lumColor-lumColor2), DEBLOOM_CONST_B ) + 1.0);
  508.     sum += gauss[x+KERNEL_RADIUS] * lerp(color.rgb, color2, debloomScalar);
  509.   }
  510.   color.rgb = sum;
  511. }
  512.  
  513.  
  514. /** @brief Is used in combination with BlurFrameHorizontalPS to blur the current frame
  515.  *         according to the blurred luminance
  516.  *  
  517.  *  @see BlurFrameHorizontalPS
  518.  */
  519. void BlurFrameVerticalPS(
  520.   in float4 pos : SV_Position,
  521.   in float2 texcoord : TEXCOORD0,
  522.   out float4 color : SV_Target)
  523. {
  524.   static const float gauss[17] = {0.011421, 0.019353, 0.030565, 0.044996, 0.061744,
  525.                                   0.078971, 0.094148, 0.104620, 0.108363, 0.104620,
  526.                                   0.094148, 0.078971, 0.061744, 0.044996, 0.030565,
  527.                                   0.019353, 0.011421};
  528.  
  529.   color = tex2D(samplerRGBA8Temp, texcoord);
  530.   float lumColor = dot(color.rgb, LUMCOEFF);
  531.  
  532.   float blur = 1.0/(pow(exp2(BLUR_CONST_A)*tex2D(samplerBlurredLuminance, texcoord).r, BLUR_CONST_B)+1.0);
  533.  
  534.   float blurStepsizeY = MAX_RADIUS/KERNEL_RADIUS * BUFFER_WIDTH/BUFFER_HEIGHT * blur;
  535.   float3 sum = float3(0.0, 0.0, 0.0);
  536.   for (int y=-KERNEL_RADIUS; y<=KERNEL_RADIUS; y++)
  537.   {
  538.     float3 color2 = tex2D( samplerRGBA8Temp, texcoord+float2(0.0, blurStepsizeY*y) ).rgb;
  539.     float lumColor2 = dot(color2, LUMCOEFF);
  540.    
  541.     float debloomScalar = 1.0/(pow( exp2(DEBLOOM_CONST_A)*abs(lumColor-lumColor2), DEBLOOM_CONST_B ) + 1.0);
  542.     sum += gauss[y+KERNEL_RADIUS] * lerp(color.rgb, color2, debloomScalar);
  543.   }
  544.  
  545.   if (blurOn)
  546.     color.rgb = sum;
  547.   else
  548.     color = tex2D(samplerColor, texcoord);
  549.  
  550.   saturate(color);
  551. }
  552.  
  553.  
  554. /** @brief Simulates the effects of the saturation of the rod cells and the slightly noisy
  555.  *         look of dark environments
  556.  *  
  557.  */
  558. void ApplyNoiseAndSaturationPS(
  559.   in float4 pos : SV_Position,
  560.   in float2 texcoord : TEXCOORD0,
  561.   out float4 color : SV_Target)
  562. {
  563.   color = tex2D(samplerBlurred, texcoord);
  564.  
  565.   float lum = dot(tex2D(samplerColor, texcoord).rgb, LUMCOEFF);
  566.  
  567.   if (noiseOn)
  568.   {
  569.     float noise = (noiseType) ? fNoise(float3(texcoord*128.0, framecount))//"digital" noise
  570.                               : fiNoise(float3(texcoord*128.0, framecount*0.1));//smooth noise
  571.     noise += 0.5;
  572.     float noiseAmt = 1.0/(pow( exp2(NOISE_CONST_A)*lum, NOISE_CONST_B ) + 1.0);
  573.     color.rgb *= lerp(1.0, noise, noiseAmt);
  574.   }
  575.  
  576.   if (saturationOn)
  577.   {
  578.     float sat = tex2D(samplerRodSaturation, texcoord).r;
  579.     float saturationAmt = 1.0/(pow( exp2(SATURATION_CONST_A)*lum, SATURATION_CONST_B ) + 1.0);
  580.     color.rgb *= 1.0-sat*saturationAmt;
  581.     color.rgb += saturationAmt*sat*sat*float3(0.04, 0.05, 0.06);
  582.   }
  583.  
  584.   saturate(color);
  585. }
  586.  
  587.  
  588. /** @brief Gamma correction with gamma=0.5 to increase the brightness
  589.  *  
  590.  */
  591. void GammaCorrectionPS(
  592.   in float4 pos : SV_Position,
  593.   in float2 texcoord : TEXCOORD0,
  594.   out float4 color : SV_Target)
  595. {
  596.   color = tex2D(samplerColor, texcoord);
  597.   float lum = dot(color.rgb, LUMCOEFF);
  598.   color.rgb *= pow(lum, 0.5)/lum;
  599.  
  600.   saturate(color);
  601. }
  602.  
  603.  
  604. /** @brief Definition of the complete effect/shader.
  605.  *  
  606.  *  Press [Insert] to toggle the shader on/off
  607.  */
  608. technique NightShader < toggle = 0x2D; >
  609. {
  610.   pass BlurLuminanceHorizontal
  611.   {
  612.     VertexShader = PostProcessingVS;
  613.     PixelShader = BlurLuminanceHorizontalPS;
  614.     RenderTarget = texR8Temp;
  615.   }
  616.  
  617.   pass BlurLuminanceVertical
  618.   {
  619.     VertexShader = PostProcessingVS;
  620.     PixelShader = BlurLuminanceVerticalPS;
  621.     RenderTarget = texBlurredLuminance;
  622.   }
  623.  
  624.   pass CalcRodSaturation
  625.   {
  626.     VertexShader = PostProcessingVS;
  627.     PixelShader = CalcRodSaturationPS;
  628.     RenderTarget = texR32FTemp;
  629.   }
  630.  
  631.   pass CopyRodSaturation
  632.   {
  633.     VertexShader = PostProcessingVS;
  634.     PixelShader = CopyRodSaturationPS;
  635.     RenderTarget = texRodSaturation;
  636.   }
  637.  
  638.   pass BlurFrameHorizontal
  639.   {
  640.     VertexShader = PostProcessingVS;
  641.     PixelShader = BlurFrameHorizontalPS;
  642.     RenderTarget = texRGBA8Temp;
  643.   }
  644.  
  645.   pass BlurFrameVertical
  646.   {
  647.     VertexShader = PostProcessingVS;
  648.     PixelShader = BlurFrameVerticalPS;
  649.     RenderTarget = texBlurred;
  650.   }
  651.  
  652.   pass ApplyNoiseAndSaturation
  653.   {
  654.     VertexShader = PostProcessingVS;
  655.     PixelShader = ApplyNoiseAndSaturationPS;
  656.   }
  657.  
  658. }
  659.  
  660.  
  661. /** @brief Definition of the gamma correction.
  662.  *  
  663.  *  Press [Home] to toggle gamma
  664.  */
  665. technique Gamma < toggle = 0x24; >
  666. {
  667.   pass GammaCorrection
  668.   {
  669.     VertexShader = PostProcessingVS;
  670.     PixelShader = GammaCorrectionPS;
  671.   }
  672.  
  673. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement