Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @file ReShade.fx
- *
- * @brief 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 them selfs.
- *
- * For further information goto:
- * https://feedback.bistudio.com/T87715
- *
- *
- * Installation and use:
- *
- * IMPORTANT NOTE: ReShade 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/toggling 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 whole shader
- * - Press [Delete] to toggle the blur
- * - Press [End] to toggle the rod saturation
- * - Press [PgDwn] to toggle the noise
- * - Press [PgUp] to toggle the noise type
- * - Press [Home] to toggle gamma
- *
- * It might be helpful 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
- * The screenshots are saved to the same directory where you put the shader files.
- *
- */
- /** @brief The (maximum) filter radius used for the static and the adaptive blurring
- *
- * It is defined as th ratio of filter radius and screen width.
- * The default is MAX_RADIUS=0.011; this means the radius will be 1.1% of the screen width.
- *
- * Range: [0.005 to 0.05]
- */
- #define MAX_RADIUS 0.011
- /** @brief Value to configure the mapping of the luminance to the amount of the blur
- *
- * To calculate the amount of blur, noise and other values applied to each pixel
- * based on its luminance or the luminance of its neighborhood the function
- * f(x)=1/((2^A*x)^B+1) is used.
- *
- * In this case is A = BLUR_CONST_A
- *
- * Range: [3.5 to 7.0]
- */
- #define BLUR_CONST_A 6.0
- /** @brief Value to configure the mapping of the luminance to the amount of the blur
- *
- * To calculate the amount of blur, noise and other values applied to each pixel
- * based on its luminance or the luminance of its neighborhood the function
- * f(x)=1/((2^A*x)^B+1) is used.
- *
- * In this case is B = BLUR_CONST_B
- *
- * Range: [2.0 to 5.0]
- */
- #define BLUR_CONST_B 4.0
- /** @brief Value to configure the mapping of the luminance to the amount of the noise
- *
- * @see BLUR_CONST_A
- *
- * Range: [3.5 to 7.0]
- */
- #define NOISE_CONST_A 4.5
- /** @brief Value to configure the mapping of the luminance to the amount of the noise
- *
- * @see BLUR_CONST_B
- *
- * Range: [2.0 to 5.0]
- */
- #define NOISE_CONST_B 3.0
- /** @brief Value to configure the mapping of the luminance to the amount of visible saturation
- *
- * @see BLUR_CONST_A
- *
- * Range: [3.5 to 7.0]
- */
- #define SATURATION_CONST_A 4.0
- /** @brief Value to configure the mapping of the luminance to the amount of visible saturation
- *
- * @see BLUR_CONST_B
- *
- * Range: [2.0 to 5.0]
- */
- #define SATURATION_CONST_B 2.0
- /** @brief Time constant in milliseconds used for the calculation of the saturation of the
- * rod cells
- *
- * Range: [50.0 to 500.0]
- */
- #define ROD_SATURATION_TAU 200.0
- /** @brief Time constant in milliseconds used for the calculation of the desaturation of the
- * rod cells
- *
- * Range: [500.0 to 5000.0]
- */
- #define ROD_DESATURATION_TAU 5000.0
- /** @brief Value to configure the blurring of pixels in the neighborhood of bright pixels
- *
- * @see BLUR_CONST_A
- *
- * Range: [3.5 to 7.0]
- */
- #define DEBLOOM_CONST_A 6.0
- /** @brief Value to configure the blurring of pixels in the neighborhood of bright pixels
- *
- * @see BLUR_CONST_B
- *
- * Range: [2.0 to 5.0]
- */
- #define DEBLOOM_CONST_B 4.0
- #define KERNEL_RADIUS 8
- #pragma message "\n"
- #pragma message "Press [Insert] to toggle the whole shader\n"
- #pragma message "Press [Delete] to toggle the blur\n"
- #pragma message "Press [End] to toggle the rod saturation\n"
- #pragma message "Press [PgDwn] to toggle the noise\n"
- #pragma message "Press [PgUp] to toggle the noise type\n"
- #pragma message "Press [Home] to toggle gamma\n"
- #pragma message "Press [Print] to take a screenshot\n"
- #pragma reshade showstatistics
- #pragma reshade screenshot_format png
- /** @brief Total frame count since the start of the game
- *
- */
- uniform int framecount < source = "framecount"; >;
- /** @brief The time passed since the previous frame in milliseconds
- *
- */
- uniform float frametime < source = "frametime"; >;
- /** @brief Uniform to toggle the blur
- *
- * Key: [DEL]
- */
- uniform bool blurOn < source = "key"; keycode = 0x2E; toggle = true; >;
- /** @brief Uniform to toggle the saturation
- *
- * Key: [END]
- */
- uniform bool saturationOn < source = "key"; keycode = 0x23; toggle = true; >;
- /** @brief Uniform to toggle the noise type
- *
- * Key: [PgUp]
- */
- uniform bool noiseType < source = "key"; keycode = 0x21; toggle = true; >;
- /** @brief Uniform to toggle the noise
- *
- * Key: [PgDwn]
- */
- uniform bool noiseOn < source = "key"; keycode = 0x22; toggle = true; >;
- static const float3 LUMCOEFF = float3(0.2126, 0.7152, 0.0722);
- /** @brief Input
- *
- */
- texture texColorBuffer : COLOR;
- texture texBlurredLuminance
- {
- Width = BUFFER_WIDTH;
- Height = BUFFER_HEIGHT;
- MipLevels = 1;
- Format = R8;
- };
- texture texRodSaturation
- {
- Width = BUFFER_WIDTH;
- Height = BUFFER_HEIGHT;
- MipLevels = 1;
- Format = R32F;
- };
- texture texBlurred
- {
- Width = BUFFER_WIDTH;
- Height = BUFFER_HEIGHT;
- MipLevels = 1;
- Format = RGBA8;
- };
- texture texR8Temp
- {
- Width = BUFFER_WIDTH;
- Height = BUFFER_HEIGHT;
- MipLevels = 1;
- Format = R8;
- };
- texture texRGBA8Temp
- {
- Width = BUFFER_WIDTH;
- Height = BUFFER_HEIGHT;
- MipLevels = 1;
- Format = RGBA8;
- };
- texture texR32FTemp
- {
- Width = BUFFER_WIDTH;
- Height = BUFFER_HEIGHT;
- MipLevels = 1;
- Format = R32F;
- };
- 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 samplerBlurredLuminance
- {
- Texture = texBlurredLuminance;
- };
- sampler samplerRodSaturation
- {
- Texture = texRodSaturation;
- };
- sampler samplerBlurred
- {
- Texture = texBlurred;
- };
- sampler samplerR8Temp
- {
- Texture = texR8Temp;
- };
- sampler samplerRGBA8Temp
- {
- Texture = texRGBA8Temp;
- };
- sampler samplerR32FTemp
- {
- Texture = texR32FTemp;
- };
- /** @brief Simple 3D value noise
- *
- */
- float sNoise(float3 p)
- {
- return frac(sin(dot(p, float3(12.9898, 78.233, 41.5435))) * 43758.5453);
- }
- /** @brief Sum of simple 3D value noise at 4 different frequencies
- *
- * frequencies = {1.0, 2.0, 4.0, 8.0}*f0
- */
- float fNoise(float3 p)
- {
- return (sNoise(floor(p)) + sNoise(floor(p*2.0)) + sNoise(floor(p*4.0)) + sNoise(floor(p*8.0))) / 4.0;
- }
- /** @brief Trilinearly interpolated simple 3D value noise
- *
- */
- float iNoise(float3 p)
- {
- float3 floorP = floor(p);
- float3 fractP = p-floorP;
- float v0 = sNoise(floorP+float3(0.0, 0.0, 0.0));
- float v1 = sNoise(floorP+float3(0.0, 0.0, 1.0));
- float v2 = sNoise(floorP+float3(0.0, 1.0, 0.0));
- float v3 = sNoise(floorP+float3(0.0, 1.0, 1.0));
- float v4 = sNoise(floorP+float3(1.0, 0.0, 0.0));
- float v5 = sNoise(floorP+float3(1.0, 0.0, 1.0));
- float v6 = sNoise(floorP+float3(1.0, 1.0, 0.0));
- float v7 = sNoise(floorP+float3(1.0, 1.0, 1.0));
- v0 = lerp(v0, v1, fractP.z);
- v2 = lerp(v2, v3, fractP.z);
- v4 = lerp(v4, v5, fractP.z);
- v6 = lerp(v6, v7, fractP.z);
- v0 = lerp(v0, v2, fractP.y);
- v4 = lerp(v4, v6, fractP.y);
- v0 = lerp(v0, v4, fractP.x);
- return v0;
- }
- /** @brief Sum of trilinearly interpolated 3D value noise at 4 different frequencies
- *
- * frequencies = {1.0, 2.0, 4.0, 8.0}*f0
- */
- float fiNoise(float3 p)
- {
- return (iNoise(p) + iNoise(p*2.0) + iNoise(p*4.0) + iNoise(p*8.0)) / 4.0;
- }
- /** @brief Vertex shader to generate a simple fullscreen triangle with the three vertices
- * provided by ReShade
- *
- * See http://redd.it/2j17wk for additional information.
- */
- 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);
- }
- /** @brief Simulates the (de)saturation of the rod cells
- *
- * - The saturation makes the value converge to 1.0; the rate of convergence is dependent
- * on the luminance.
- * - The desaturation makes the value converge to 0.0; the rate of convergence is fixed.
- */
- void CalcRodSaturationPS(
- in float4 pos : SV_Position,
- in float2 texcoord : TEXCOORD0,
- out float newSat : SV_Target)
- {
- float oldSat = tex2D(samplerRodSaturation, texcoord).r;
- float lum = tex2D(samplerBlurredLuminance, texcoord).r;
- //saturation
- float scalar = exp(-frametime*lum*lum*2.0/ROD_SATURATION_TAU);
- newSat = lerp(1.0, oldSat, scalar);
- //desaturation
- newSat *= exp(-frametime/ROD_DESATURATION_TAU);
- saturate(newSat);
- }
- /** @brief Is used to copy the result of CalcRodSaturationPS to texRodSaturation
- *
- * This is necessary because the render target can not be written and read at the same time.
- */
- void CopyRodSaturationPS(
- in float4 pos : SV_Position,
- in float2 texcoord : TEXCOORD0,
- out float sat : SV_Target)
- {
- sat = tex2D(samplerR32FTemp, texcoord).r;
- }
- /** @brief Is used in combination with BlurLuminanceVerticalPS to calculate the blurred luminance
- *
- * The luminance is calculated in this step.
- */
- void BlurLuminanceHorizontalPS(
- in float4 pos : SV_Position,
- in float2 texcoord : TEXCOORD0,
- out float lum : SV_Target)
- {
- static const float gauss[17] = {0.011421, 0.019353, 0.030565, 0.044996, 0.061744,
- 0.078971, 0.094148, 0.104620, 0.108363, 0.104620,
- 0.094148, 0.078971, 0.061744, 0.044996, 0.030565,
- 0.019353, 0.011421};
- float blurStepsizeX = MAX_RADIUS/KERNEL_RADIUS;
- float3 sum = float3(0.0, 0.0, 0.0);
- for (int x=-KERNEL_RADIUS; x<=KERNEL_RADIUS; x++)
- sum += gauss[x+KERNEL_RADIUS] * tex2D(samplerColor, float2(texcoord + float2(blurStepsizeX*x, 0.0))).rgb;
- lum = dot(sum, LUMCOEFF);
- }
- /** @brief Is used in combination with BlurLuminanceHorizontalPS to calculate the blurred luminance
- *
- * The result is the MAXIMUM of the gaussian-weighted average luminance of the neighborhood and
- * the original luminance. You need to take the maximum to preserve small, bright objects like
- * stars or other distant lightsources on a dark background. These pixels may contain important
- * information, but they have too little influence on the weighted average luminance of their
- * neighborhood and would get "blurred away" otherwise.
- */
- void BlurLuminanceVerticalPS(
- in float4 pos : SV_Position,
- in float2 texcoord : TEXCOORD0,
- out float lum : SV_Target)
- {
- static const float gauss[17] = {0.011421, 0.019353, 0.030565, 0.044996, 0.061744,
- 0.078971, 0.094148, 0.104620, 0.108363, 0.104620,
- 0.094148, 0.078971, 0.061744, 0.044996, 0.030565,
- 0.019353, 0.011421};
- float blurStepsizeY = MAX_RADIUS/KERNEL_RADIUS * BUFFER_WIDTH/BUFFER_HEIGHT;
- float sum = 0.0;
- for (int y=-KERNEL_RADIUS; y<=KERNEL_RADIUS; y++)
- sum += gauss[y+KERNEL_RADIUS] * tex2D(samplerR8Temp, float2(texcoord + float2(0.0, blurStepsizeY*y))).r;
- lum = max(sum, dot(tex2D(samplerColor, texcoord).rgb, LUMCOEFF));
- }
- /** @brief Is used in combination with BlurFrameVerticalPS to blur the current frame
- * according to the blurred luminance
- *
- * To avoid the bloom the blurring would generate on dark areas around bright areas the
- * algorithm is slightly modified; the more the luminance of a pixel differs from the
- * luminance of the original pixel the less it influences the result.
- */
- void BlurFrameHorizontalPS(
- in float4 pos : SV_Position,
- in float2 texcoord : TEXCOORD0,
- out float4 color : SV_Target)
- {
- static const float gauss[17] = {0.011421, 0.019353, 0.030565, 0.044996, 0.061744,
- 0.078971, 0.094148, 0.104620, 0.108363, 0.104620,
- 0.094148, 0.078971, 0.061744, 0.044996, 0.030565,
- 0.019353, 0.011421};
- color = tex2D(samplerColor, texcoord);
- float lumColor = dot(color.rgb, LUMCOEFF);
- float blur = 1.0/(pow(exp2(BLUR_CONST_A)*tex2D(samplerBlurredLuminance, texcoord).r, BLUR_CONST_B)+1.0);
- float blurStepsizeX = MAX_RADIUS/KERNEL_RADIUS * blur;
- float3 sum = float3(0.0, 0.0, 0.0);
- for (int x=-KERNEL_RADIUS; x<=KERNEL_RADIUS; x++)
- {
- float3 color2 = tex2D( samplerColor, texcoord+float2(blurStepsizeX*x, 0.0) ).rgb;
- float lumColor2 = dot(color2, LUMCOEFF);
- float debloomScalar = 1.0/(pow( exp2(DEBLOOM_CONST_A)*abs(lumColor-lumColor2), DEBLOOM_CONST_B ) + 1.0);
- sum += gauss[x+KERNEL_RADIUS] * lerp(color.rgb, color2, debloomScalar);
- }
- color.rgb = sum;
- }
- /** @brief Is used in combination with BlurFrameHorizontalPS to blur the current frame
- * according to the blurred luminance
- *
- * @see BlurFrameHorizontalPS
- */
- void BlurFrameVerticalPS(
- in float4 pos : SV_Position,
- in float2 texcoord : TEXCOORD0,
- out float4 color : SV_Target)
- {
- static const float gauss[17] = {0.011421, 0.019353, 0.030565, 0.044996, 0.061744,
- 0.078971, 0.094148, 0.104620, 0.108363, 0.104620,
- 0.094148, 0.078971, 0.061744, 0.044996, 0.030565,
- 0.019353, 0.011421};
- color = tex2D(samplerRGBA8Temp, texcoord);
- float lumColor = dot(color.rgb, LUMCOEFF);
- float blur = 1.0/(pow(exp2(BLUR_CONST_A)*tex2D(samplerBlurredLuminance, texcoord).r, BLUR_CONST_B)+1.0);
- float blurStepsizeY = MAX_RADIUS/KERNEL_RADIUS * BUFFER_WIDTH/BUFFER_HEIGHT * blur;
- float3 sum = float3(0.0, 0.0, 0.0);
- for (int y=-KERNEL_RADIUS; y<=KERNEL_RADIUS; y++)
- {
- float3 color2 = tex2D( samplerRGBA8Temp, texcoord+float2(0.0, blurStepsizeY*y) ).rgb;
- float lumColor2 = dot(color2, LUMCOEFF);
- float debloomScalar = 1.0/(pow( exp2(DEBLOOM_CONST_A)*abs(lumColor-lumColor2), DEBLOOM_CONST_B ) + 1.0);
- sum += gauss[y+KERNEL_RADIUS] * lerp(color.rgb, color2, debloomScalar);
- }
- if (blurOn)
- color.rgb = sum;
- else
- color = tex2D(samplerColor, texcoord);
- saturate(color);
- }
- /** @brief Simulates the effects of the saturation of the rod cells and the slightly noisy
- * look of dark environments
- *
- */
- void ApplyNoiseAndSaturationPS(
- in float4 pos : SV_Position,
- in float2 texcoord : TEXCOORD0,
- out float4 color : SV_Target)
- {
- color = tex2D(samplerBlurred, texcoord);
- float lum = dot(tex2D(samplerColor, texcoord).rgb, LUMCOEFF);
- if (noiseOn)
- {
- float noise = (noiseType) ? fNoise(float3(texcoord*128.0, framecount))//"digital" noise
- : fiNoise(float3(texcoord*128.0, framecount*0.1));//smooth noise
- noise += 0.5;
- float noiseAmt = 1.0/(pow( exp2(NOISE_CONST_A)*lum, NOISE_CONST_B ) + 1.0);
- color.rgb *= lerp(1.0, noise, noiseAmt);
- }
- if (saturationOn)
- {
- float sat = tex2D(samplerRodSaturation, texcoord).r;
- float saturationAmt = 1.0/(pow( exp2(SATURATION_CONST_A)*lum, SATURATION_CONST_B ) + 1.0);
- color.rgb *= 1.0-sat*saturationAmt;
- color.rgb += saturationAmt*sat*sat*float3(0.04, 0.05, 0.06);
- }
- saturate(color);
- }
- /** @brief 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);
- }
- /** @brief Definition of the complete effect/shader.
- *
- * Press [Insert] to toggle the shader on/off
- */
- technique NightShader < toggle = 0x2D; >
- {
- pass BlurLuminanceHorizontal
- {
- VertexShader = PostProcessingVS;
- PixelShader = BlurLuminanceHorizontalPS;
- RenderTarget = texR8Temp;
- }
- pass BlurLuminanceVertical
- {
- VertexShader = PostProcessingVS;
- PixelShader = BlurLuminanceVerticalPS;
- RenderTarget = texBlurredLuminance;
- }
- pass CalcRodSaturation
- {
- VertexShader = PostProcessingVS;
- PixelShader = CalcRodSaturationPS;
- RenderTarget = texR32FTemp;
- }
- pass CopyRodSaturation
- {
- VertexShader = PostProcessingVS;
- PixelShader = CopyRodSaturationPS;
- RenderTarget = texRodSaturation;
- }
- pass BlurFrameHorizontal
- {
- VertexShader = PostProcessingVS;
- PixelShader = BlurFrameHorizontalPS;
- RenderTarget = texRGBA8Temp;
- }
- pass BlurFrameVertical
- {
- VertexShader = PostProcessingVS;
- PixelShader = BlurFrameVerticalPS;
- RenderTarget = texBlurred;
- }
- pass ApplyNoiseAndSaturation
- {
- VertexShader = PostProcessingVS;
- PixelShader = ApplyNoiseAndSaturationPS;
- }
- }
- /** @brief Definition of the gamma correction.
- *
- * Press [Home] to toggle gamma
- */
- technique Gamma < toggle = 0x24; >
- {
- pass GammaCorrection
- {
- VertexShader = PostProcessingVS;
- PixelShader = GammaCorrectionPS;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement