Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This shader is based on physiological characteristics of the human eye and it is designed to
- * provide a natural look of dark scenes or low light environments. It is also a good prevention
- * of the abuse of the gamma and brightness settings without restricting these settings themselfs.
- *
- * For further information goto:
- * https://feedback.dayz.com/view.php?id=1294
- *
- *
- * Installation and use:
- *
- * IMPORTANT NOTE: The shader hooks into the renderer and thus it might modify the memory
- * of the running game. This could be classified as a sign of cheating/hacking and because
- * of this it is recommended to test the shader in singleplayer/editor with disconnected
- * networking. After testing and before reconnecting you should uninstall it by deleting
- * the two files you copied during the installation. That might sound a bit paranoid,
- * but better safe than sorry... :)
- *
- *
- * During the installation you need to rename two files and change their file extensions too.
- * For this you need to activate/show the file extensions in the windows explorer, if you
- * haven't done it already:
- * http://windows.microsoft.com/en-us/windows/show-hide-file-name-extensions
- *
- * 1. Download ReShade
- * During the testing Reshade is needed for hooking into the renderer and
- * loading/starting/toggeling the shader. It is available at http://reshade.me/
- *
- * 2. Copy ReShade32.dll from the downloaded archive into ArmAs main directory
- * (the same directory like the arma2.exe/ArmA2OA.exe/arma3.exe)
- *
- * 3. For ArmA 2 (OA/CO) rename the copied ReShade32.dll to d3d9.dll, for ArmA 3
- * rename it to dxgi.dll
- *
- * 4. If the shader file (the one you are reading now) isn't named ReShade.fx
- * rename it ReShade.fx
- *
- * 5. Copy this ReShade.fx into ArmAs main directory too.
- *
- *
- * That's it. If you now start ArmA, ReShade should load the shader.
- * Press [Insert] to toggle the shader on/off
- * Press [Home] to toggle gamma 1.0/2.0
- *
- * It might be helpfull to note that ReShade is not compatible with software like screen
- * capturing or overlays and you would need to deactivate them in order to test the shader.
- * To take screenshots nevertheless ReShade brings its own screenshot functionality:
- * Press [Print] to take a screenshot (they are saved to the same directory where you put
- * the shader files)
- *
- */
- #define max_radius 0.011 // [0.005 to 0.05] Ratio of filter radius and screen width. The default is max_radius=0.011 and
- // this means the max. blur radius will be 1.1% of the screen width.
- // The bigger the radius, the stronger the blur. But at some point the quality of
- // the blur gets worse and you might need to increase the kernel radius too.
- #define kernel_radius 8 // [2|3|4|5|6|7|8] Size of the gaussian kernel: (2n+1)x(2n+1)-kernel. If you set kernel_radius=3
- // you will get a 7x7-kernel (gaussian blur)
- // The performance of the shader is mainly determined by the kernel radius.
- // A higher kernel radius leads to a smoother blur but a lower performance.
- #define blur_const_a 6.0 // [3.5 to 7.0] To calculate the amount of blur applied to each pixel based on its luminance a
- #define blur_const_b 4.0 // [2.0 to 5.0] function similar to 1/x is used. A good starting point for understanding the
- // function is to plot it with the given constants. This can be done with the
- // help of e.g. wolframalpha.com. You would just need to put
- // plot 1/((2^blur_const_a*x)^blur_const_b+1), x=0 to x=1, y=0 to y=1
- // into the input field and replace the constants names with the chosen values and
- // submit the input.
- // The x-axis defines the given luminance and the y-axis the according normalized
- // blur radius. This needs to be multiplied with the max_radius defined above to
- // get the actual blur radius.
- #define debloom 1 // [0|1] Debloom reduces the bloom applied around small, bright objects and the blur in
- // semi-dark areas. That comes at the expense of some performance but might be
- // interesting for players who are prone to gaming sickness or who want the gfx to
- // be as clean as possible.
- #define debloom_const_a 6.0 // [3.5 to 7.0] It uses the same function like above but with the absolute difference of the
- #define debloom_const_b 4.0 // [2.0 to 5.0] luminances of two instead of the ablolute luminance of one pixel.
- // The constants can but don't need to be the same like the ones above.
- //
- #define debloom_interpol 1 // [0|1] To improve the quality even further, debloom can be used with linear
- // interpolation but that costs a bit more performance too.
- #define integrate_lum_pass 0 // [0|1] If activated the calculation of the luminance is integrated into the bluring of
- // these values. This reduces the number of passes but might increase the overall
- // number of instructions. (The ideal setting might be different for each gfx-card
- // vendor, gfx-card series or even driver version.)
- #define integrate_bm_pass 0 // [0|1] If activated the calculation of the blurmap is integrated into the bluring of
- // the actual frame. This reduces the number of passes but might increase the
- // overall number of instructions. (The ideal setting might be different for each
- // gfx-card vendor, gfx-card series or even driver version.)
- #if kernel_radius < 2
- #define kernel_radius 2
- #elif 8 < kernel_radius
- #define kernel_radius 8
- #endif
- #pragma message "\n"
- #pragma message "Press [Insert] to toggle the shader on/off\n"
- #pragma message "Press [Home] to toggle gamma 1.0/2.0\n"
- #pragma message "Press [Print] to take a screenshot\n"
- #pragma reshade showstatistics
- #pragma reshade screenshot_format png
- static const float3 lumcoeff = float3(0.2126, 0.7152, 0.0722);
- texture texColorBuffer : COLOR;
- texture texTarget
- {
- Width = BUFFER_WIDTH;
- Height = BUFFER_HEIGHT;
- MipLevels = 1;
- Format = R8;
- };
- texture texTarget2
- {
- Width = BUFFER_WIDTH;
- Height = BUFFER_HEIGHT;
- MipLevels = 1;
- Format = R8;
- };
- texture texTarget3
- {
- Width = BUFFER_WIDTH;
- Height = BUFFER_HEIGHT;
- MipLevels = 1;
- Format = RGBA8;
- };
- sampler samplerColor
- {
- Texture = texColorBuffer;
- AddressU = CLAMP;
- AddressV = CLAMP;
- AddressW = CLAMP;
- MinFilter = LINEAR;
- MagFilter = LINEAR;
- MipFilter = LINEAR;
- MipLODBias = 0.0f;
- MinLOD = -1000.0f;
- MaxLOD = 1000.0f;
- MaxAnisotropy = 1;
- SRGBTexture = FALSE;
- };
- sampler samplerTarget
- {
- Texture = texTarget;
- };
- sampler samplerTarget2
- {
- Texture = texTarget2;
- };
- sampler samplerTarget3
- {
- Texture = texTarget3;
- };
- /**
- * Vertex shader to generate a simple fullscreen triangle with the three vertices
- * provided by ReShade (http://redd.it/2j17wk):
- */
- void PostProcessingVS(
- in uint id : SV_VertexID,
- out float4 position : SV_Position,
- out float2 texcoord : TEXCOORD0)
- {
- texcoord = float2((id << 1) & 2, id & 2);
- position = float4(texcoord * float2(2, -2) + float2(-1, 1), 0, 1);
- }
- /**
- * Pixel shader to calculate the luminance.
- * It can be integrated into BlurLuminanceHorizontalPS.
- */
- void CalcLuminancePS(
- in float4 pos : SV_Position,
- in float2 texcoord : TEXCOORD0,
- out float lum : SV_Target)
- {
- lum = dot( tex2D(samplerColor, texcoord).rgb, lumcoeff );
- }
- /**
- * BlurLuminanceHorizontalPS and BlurLuminanceVerticalPS blur the calculated luminance values
- * to get the gaussian-weighted average luminance of the neighborhood of each pixel.
- */
- void BlurLuminanceHorizontalPS(
- in float4 pos : SV_Position,
- in float2 texcoord : TEXCOORD0,
- out float lum : SV_Target)
- {
- #if kernel_radius == 2
- static const float gauss[5] = {0.044844, 0.242423, 0.425466, 0.242423, 0.044844};
- #elif kernel_radius == 3
- static const float gauss[7] = {0.030078, 0.104984, 0.22225, 0.285375, 0.22225, 0.104984, 0.030078};
- #elif kernel_radius == 4
- static const float gauss[9] = {0.022656, 0.060631, 0.122478, 0.186757, 0.214956, 0.186757, 0.122478, 0.060631, 0.022656};
- #elif kernel_radius == 5
- static const float gauss[11] = {0.01818, 0.040867, 0.076732, 0.120339, 0.15764, 0.172486, 0.15764, 0.120339, 0.076732,
- 0.040867, 0.01818};
- #elif kernel_radius == 6
- static const float gauss[13] = {0.015183, 0.030196, 0.052995, 0.08208, 0.11219, 0.135328, 0.144055, 0.135328, 0.11219,
- 0.08208, 0.052995, 0.030196, 0.015183};
- #elif kernel_radius == 7
- static const float gauss[15] = {0.013036, 0.023681, 0.039242, 0.059325, 0.081814, 0.102929, 0.118132, 0.123682, 0.118132,
- 0.102929, 0.081814, 0.059325, 0.039242, 0.023681, 0.013036};
- #elif kernel_radius == 8
- static const float gauss[17] = {0.011421, 0.019353, 0.030565, 0.044996, 0.061744, 0.078971, 0.094148, 0.10462, 0.108363,
- 0.10462, 0.094148, 0.078971, 0.061744, 0.044996, 0.030565, 0.019353, 0.011421};
- #endif
- #if integrate_lum_pass == 1
- float4 color = tex2D(samplerColor, texcoord);
- float blur_stepsize_x = max_radius/kernel_radius;
- float3 sum = float3(0.0, 0.0, 0.0);
- for (int db_x1=-kernel_radius; db_x1<=kernel_radius; db_x1++)
- sum += gauss[db_x1+kernel_radius] * tex2D(samplerColor, float2(texcoord + float2(blur_stepsize_x*db_x1, 0.0))).rgb;
- float lum_sum = dot(sum, lumcoeff);
- float lum_color = dot(color.rgb, lumcoeff);
- // This "max(..." is needed to preserve small, bright objects like stars or other distant lightsources on a dark background.
- // These pixels contain important information but they have too little influence on the weighted average luminance
- // of their neighborhood and would get "blured away" otherwise. In adition it reduces the bloom around these objects comparable,
- // but not as effective as the dedicated "debloom"
- lum = max(lum_sum, lum_color);
- #else
- float blur_stepsize_x = max_radius/kernel_radius;
- float sum = 0.0;
- for (int db_x1=-kernel_radius; db_x1<=kernel_radius; db_x1++)
- sum += gauss[db_x1+kernel_radius] * tex2D(samplerTarget, float2(texcoord + float2(blur_stepsize_x*db_x1, 0.0))).r;
- // This "max(..." is needed to preserve small, bright objects like stars or other distant lightsources on a dark background.
- // These pixels contain important information but they have too little influence on the weighted average luminance
- // of their neighborhood and would get "blured away" otherwise. In adition it reduces the bloom around these objects comparable,
- // but not as effective as the dedicated "debloom"
- lum = max(sum, tex2D(samplerTarget, texcoord).r);
- #endif
- }
- /**
- * BlurLuminanceHorizontalPS and BlurLuminanceVerticalPS blur the calculated luminance values
- * to get the gaussian-weighted average luminance of the neighborhood of each pixel.
- */
- void BlurLuminanceVerticalPS(
- in float4 pos : SV_Position,
- in float2 texcoord : TEXCOORD0,
- out float lum : SV_Target)
- {
- #if kernel_radius == 2
- static const float gauss[5] = {0.044844, 0.242423, 0.425466, 0.242423, 0.044844};
- #elif kernel_radius == 3
- static const float gauss[7] = {0.030078, 0.104984, 0.22225, 0.285375, 0.22225, 0.104984, 0.030078};
- #elif kernel_radius == 4
- static const float gauss[9] = {0.022656, 0.060631, 0.122478, 0.186757, 0.214956, 0.186757, 0.122478, 0.060631, 0.022656};
- #elif kernel_radius == 5
- static const float gauss[11] = {0.01818, 0.040867, 0.076732, 0.120339, 0.15764, 0.172486, 0.15764, 0.120339, 0.076732,
- 0.040867, 0.01818};
- #elif kernel_radius == 6
- static const float gauss[13] = {0.015183, 0.030196, 0.052995, 0.08208, 0.11219, 0.135328, 0.144055, 0.135328, 0.11219,
- 0.08208, 0.052995, 0.030196, 0.015183};
- #elif kernel_radius == 7
- static const float gauss[15] = {0.013036, 0.023681, 0.039242, 0.059325, 0.081814, 0.102929, 0.118132, 0.123682, 0.118132,
- 0.102929, 0.081814, 0.059325, 0.039242, 0.023681, 0.013036};
- #elif kernel_radius == 8
- static const float gauss[17] = {0.011421, 0.019353, 0.030565, 0.044996, 0.061744, 0.078971, 0.094148, 0.10462, 0.108363,
- 0.10462, 0.094148, 0.078971, 0.061744, 0.044996, 0.030565, 0.019353, 0.011421};
- #endif
- float blur_stepsize_y = max_radius/kernel_radius * BUFFER_WIDTH/BUFFER_HEIGHT;
- float sum = 0.0;
- for (int db_y1=-kernel_radius; db_y1<=kernel_radius; db_y1++)
- sum += gauss[db_y1+kernel_radius] * tex2D(samplerTarget2, float2(texcoord + float2(0.0, blur_stepsize_y*db_y1))).r;
- // This "max(..." is needed to preserve small, bright objects like stars or other distant lightsources on a dark background.
- // These pixels contain important information but they have too little influence on the weighted average luminance
- // of their neighborhood and would get "blured away" otherwise. In adition it reduces the bloom around these objects comparable,
- // but not as effective as the dedicated "debloom"
- lum = max(sum, tex2D(samplerTarget2, texcoord).r);
- }
- /**
- * This PS calculates the amount of blur which needs to be applied to each pixel based
- * on its luminance. ("blurmap")
- * It can be integrated into BlurFrameHorizontalPS and BlurFrameVerticalPS.
- */
- void CalcBlurMapPS(
- in float4 pos : SV_Position,
- in float2 texcoord : TEXCOORD0,
- out float blur : SV_Target)
- {
- blur = 1.0/(pow( exp2(blur_const_a) * tex2D(samplerTarget, texcoord).r, blur_const_b ) + 1.0);
- }
- /**
- * BlurFrameHorizontalPS and BlurFrameVerticalPS blur the actual frame according to the
- * calculated blurmap.
- */
- void BlurFrameHorizontalPS(
- in float4 pos : SV_Position,
- in float2 texcoord : TEXCOORD0,
- out float4 color : SV_Target)
- {
- #if kernel_radius == 2
- static const float gauss[5] = {0.044844, 0.242423, 0.425466, 0.242423, 0.044844};
- #elif kernel_radius == 3
- static const float gauss[7] = {0.030078, 0.104984, 0.22225, 0.285375, 0.22225, 0.104984, 0.030078};
- #elif kernel_radius == 4
- static const float gauss[9] = {0.022656, 0.060631, 0.122478, 0.186757, 0.214956, 0.186757, 0.122478, 0.060631, 0.022656};
- #elif kernel_radius == 5
- static const float gauss[11] = {0.01818, 0.040867, 0.076732, 0.120339, 0.15764, 0.172486, 0.15764, 0.120339, 0.076732,
- 0.040867, 0.01818};
- #elif kernel_radius == 6
- static const float gauss[13] = {0.015183, 0.030196, 0.052995, 0.08208, 0.11219, 0.135328, 0.144055, 0.135328, 0.11219,
- 0.08208, 0.052995, 0.030196, 0.015183};
- #elif kernel_radius == 7
- static const float gauss[15] = {0.013036, 0.023681, 0.039242, 0.059325, 0.081814, 0.102929, 0.118132, 0.123682, 0.118132,
- 0.102929, 0.081814, 0.059325, 0.039242, 0.023681, 0.013036};
- #elif kernel_radius == 8
- static const float gauss[17] = {0.011421, 0.019353, 0.030565, 0.044996, 0.061744, 0.078971, 0.094148, 0.10462, 0.108363,
- 0.10462, 0.094148, 0.078971, 0.061744, 0.044996, 0.030565, 0.019353, 0.011421};
- #endif
- #if integrate_bm_pass == 1
- float blur = 1.0/(pow( exp2(blur_const_a)*tex2D(samplerTarget, texcoord).r, blur_const_b ) + 1.0);
- #else
- float blur = tex2D(samplerTarget2, texcoord).r;
- #endif
- color = tex2D(samplerColor, texcoord);
- float lum_color = dot(color.rgb, lumcoeff);
- float blur_stepsize_x = max_radius/kernel_radius * blur;
- float3 sum = float3(0.0, 0.0, 0.0);
- float3 color2;
- float lum_color2;
- float debloom_val;
- for (int db_x2=-kernel_radius; db_x2<=kernel_radius; db_x2++)
- {
- color2 = tex2D( samplerColor, texcoord+float2(blur_stepsize_x*db_x2, 0.0) ).rgb;
- // Debloom reduces the influence bright pixels in the neighborhood have on dark pixels.
- #if debloom == 1
- lum_color2 = dot(color2, lumcoeff);
- // This is done by calculating a scalar based on the difference of the luminances of the original pixel and the neighbor pixel...
- debloom_val = 1.0/(pow( exp2(debloom_const_a)*abs(lum_color-lum_color2), debloom_const_b ) + 1.0);
- #if debloom_interpol == 0
- // ...then multiply the neighbor pixels value by the scalar in addition to the gaussian weighting.
- sum += gauss[db_x2+kernel_radius] * color2 * debloom_val;
- #else
- // If interpolation is activated use a linear interpolation of the original pixel and the neighbor pixel instead of the scaled
- // neighbor pixel alone. The more the two pixels differ in luminance the more scale in favor of the original pixel.
- sum += gauss[db_x2+kernel_radius] * lerp(color.rgb, color2, debloom_val);
- #endif
- #else
- // If debloom is deactivated a simple gaussian blur is used.
- sum += gauss[db_x2+kernel_radius] * color2;
- #endif
- }
- color.rgb = sum;
- }
- /**
- * BlurFrameHorizontalPS and BlurFrameVerticalPS blur the actual frame according to the
- * calculated blurmap.
- */
- void BlurFrameVerticalPS(
- in float4 pos : SV_Position,
- in float2 texcoord : TEXCOORD0,
- out float4 color : SV_Target)
- {
- #if kernel_radius == 2
- static const float gauss[5] = {0.044844, 0.242423, 0.425466, 0.242423, 0.044844};
- #elif kernel_radius == 3
- static const float gauss[7] = {0.030078, 0.104984, 0.22225, 0.285375, 0.22225, 0.104984, 0.030078};
- #elif kernel_radius == 4
- static const float gauss[9] = {0.022656, 0.060631, 0.122478, 0.186757, 0.214956, 0.186757, 0.122478, 0.060631, 0.022656};
- #elif kernel_radius == 5
- static const float gauss[11] = {0.01818, 0.040867, 0.076732, 0.120339, 0.15764, 0.172486, 0.15764, 0.120339, 0.076732,
- 0.040867, 0.01818};
- #elif kernel_radius == 6
- static const float gauss[13] = {0.015183, 0.030196, 0.052995, 0.08208, 0.11219, 0.135328, 0.144055, 0.135328, 0.11219,
- 0.08208, 0.052995, 0.030196, 0.015183};
- #elif kernel_radius == 7
- static const float gauss[15] = {0.013036, 0.023681, 0.039242, 0.059325, 0.081814, 0.102929, 0.118132, 0.123682, 0.118132,
- 0.102929, 0.081814, 0.059325, 0.039242, 0.023681, 0.013036};
- #elif kernel_radius == 8
- static const float gauss[17] = {0.011421, 0.019353, 0.030565, 0.044996, 0.061744, 0.078971, 0.094148, 0.10462, 0.108363,
- 0.10462, 0.094148, 0.078971, 0.061744, 0.044996, 0.030565, 0.019353, 0.011421};
- #endif
- #if integrate_bm_pass == 1
- float blur = 1.0/(pow( exp2(blur_const_a)*tex2D(samplerTarget, texcoord).r, blur_const_b ) + 1.0);
- #else
- float blur = tex2D(samplerTarget2, texcoord).r;
- #endif
- color = tex2D(samplerTarget3, texcoord);
- float lum_color = dot(color.rgb, lumcoeff);
- float blur_stepsize_y = max_radius/kernel_radius * BUFFER_WIDTH/BUFFER_HEIGHT * blur;
- float3 sum = float3(0.0, 0.0, 0.0);
- float3 color2;
- float lum_color2;
- float debloom_val;
- for (int db_y2=-kernel_radius; db_y2<=kernel_radius; db_y2++)
- {
- color2 = tex2D( samplerTarget3, texcoord+float2(0.0, blur_stepsize_y*db_y2) ).rgb;
- // Debloom reduces the influence bright pixels in the neighborhood have on dark pixels.
- #if debloom == 1
- lum_color2 = dot(color2, lumcoeff);
- // This is done by calculating a scalar based on the difference of the luminances of the original pixel and the neighbor pixel...
- debloom_val = 1.0/(pow( exp2(debloom_const_a)*abs(lum_color-lum_color2), debloom_const_b ) + 1.0);
- #if debloom_interpol == 0
- // ...then multiply the neighbor pixels value by the scalar in addition to the gaussian weighting.
- sum += gauss[db_y2+kernel_radius] * color2 * debloom_val;
- #else
- // If interpolation is activated use a linear interpolation of the original pixel and the neighbor pixel instead of the scaled
- // neighbor pixel alone. The more the two pixels differ in luminance the more scale in favor of the original pixel.
- sum += gauss[db_y2+kernel_radius] * lerp(color.rgb, color2, debloom_val);
- #endif
- #else
- // If debloom is deactivated a simple gaussian blur is used.
- sum += gauss[db_y2+kernel_radius] * color2;
- #endif
- }
- color.rgb = sum;
- }
- /**
- * Gamma correction with gamma=0.5 to increase the brightness.
- */
- void GammaCorrectionPS(
- in float4 pos : SV_Position,
- in float2 texcoord : TEXCOORD0,
- out float4 color : SV_Target)
- {
- color = tex2D(samplerColor, texcoord);
- float lum = dot(color.rgb, lumcoeff);
- color.rgb *= ( pow(lum, 0.5)/lum );
- saturate(color);
- }
- /**
- * Definition of the complete effect/shader.
- * Press [Insert] to toggle the shader on/off
- */
- technique Blur < toggle = 0x2D; >
- {
- #if integrate_lum_pass == 0
- pass CalcLuminance
- {
- VertexShader = PostProcessingVS;
- PixelShader = CalcLuminancePS;
- RenderTarget = texTarget;
- }
- #endif
- pass BlurLuminanceHorizontal
- {
- VertexShader = PostProcessingVS;
- PixelShader = BlurLuminanceHorizontalPS;
- RenderTarget = texTarget2;
- }
- pass BlurLuminanceVertical
- {
- VertexShader = PostProcessingVS;
- PixelShader = BlurLuminanceVerticalPS;
- RenderTarget = texTarget;
- }
- #if integrate_bm_pass == 0
- pass CalcBlurMap
- {
- VertexShader = PostProcessingVS;
- PixelShader = CalcBlurMapPS;
- RenderTarget = texTarget2;
- }
- #endif
- pass BlurFrameHorizontal
- {
- VertexShader = PostProcessingVS;
- PixelShader = BlurFrameHorizontalPS;
- RenderTarget = texTarget3;
- }
- pass BlurFrameVertical
- {
- VertexShader = PostProcessingVS;
- PixelShader = BlurFrameVerticalPS;
- }
- }
- /**
- * Definition of the gamma correction.
- * Press [Home] to toggle gamma 1.0/2.0
- */
- technique Gamma < toggle = 0x24; >
- {
- pass GammaCorrection
- {
- VertexShader = PostProcessingVS;
- PixelShader = GammaCorrectionPS;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement