Problem with an old SweetFX Grain shader
- BONKERS
- Topic Author
Less
More
6 years 2 months ago - 6 years 2 months ago #1
by BONKERS
Problem with an old SweetFX Grain shader was created by BONKERS
Hi there, I realize this may not be appropriate since it's not strictly Reshade related but I am pretty desperate for help.
So I am very fond of Boulotaur2024's custom injector for SweetFX he made several years ago
forums.guru3d.com/threads/sweetfx-shader...page-27#post-4581604
xp-dev.com/sc/174865/HEAD/%2Fd3d9DLL
Because of one thing, the film grain shader that he ported from Jorge Jimenez's SSS demo on github to DX10/11 and DX9.
When paired with a custom grain texture it looks gorgeous imo and blends really well. And it really works well in easing distraction from compression artifacts in FMVs.
The only problem is, after the shader has been running for approximately 30 minutes the output turns into garbage
This happens with Jorge's original texture, and several custom textures i've tried.
Default abload.de/img/rerev_2018_01_04_18_4lgs1y.png (Exaggerated for effect so it's more visible)
After about 30 minutes abload.de/img/rerev_2018_01_04_20_2rzsb2.png
I played through a few games over the last few years with this, dealing with the problem and restarting the game after it starts to annoy me.
abload.de/img/222480_20180101233614pvslk.png
abload.de/img/222480_20180103001331qtsnk.png
Examples when set up and working
So i'm just curious if anyone can look at the shader code here and could think of any reason that might jump out at you as to why this happens.
In the source repository linked above there is some files for the grain shader which isn't visible in the sweetFX shader visible to the user. So I don't know if that points to anything either.
If anyone could take a glance and offer any ideas, that'd be stupendous. Thanks a lot.
I haven't tried Reshade's grain in a while but last I recall it didn't work correctly. I do remember using the grain in the 1.0/2.0 releases that was ported from the Grain shader in GeDoSaTo and enjoying that quite a bit. (Which is my next favorite to use). But the real reason I don't want to use Reshade is because I prefer to force AA at the driver level (I manage the HBAO+/AA threads on Guru3D forums.guru3d.com/forums/videocards-nvid...-drivers-section.21/ ), and all I really need is some basic sharpening most of the time after AA. The other reason is that ReShade 3 interferes with forcing AA (Likely due to surface formats), often requiring extra compatibility to work and it also can alter the efficacy of shaders and also can catastrophically drop performance when forcing AA.
Or Perhaps would anyone be able to port this shader to reshade as is, and test to see if it does the same thing??
So I am very fond of Boulotaur2024's custom injector for SweetFX he made several years ago
forums.guru3d.com/threads/sweetfx-shader...page-27#post-4581604
xp-dev.com/sc/174865/HEAD/%2Fd3d9DLL
Because of one thing, the film grain shader that he ported from Jorge Jimenez's SSS demo on github to DX10/11 and DX9.
When paired with a custom grain texture it looks gorgeous imo and blends really well. And it really works well in easing distraction from compression artifacts in FMVs.
The only problem is, after the shader has been running for approximately 30 minutes the output turns into garbage
This happens with Jorge's original texture, and several custom textures i've tried.
Default abload.de/img/rerev_2018_01_04_18_4lgs1y.png (Exaggerated for effect so it's more visible)
After about 30 minutes abload.de/img/rerev_2018_01_04_20_2rzsb2.png
I played through a few games over the last few years with this, dealing with the problem and restarting the game after it starts to annoy me.
abload.de/img/222480_20180101233614pvslk.png
abload.de/img/222480_20180103001331qtsnk.png
Examples when set up and working
So i'm just curious if anyone can look at the shader code here and could think of any reason that might jump out at you as to why this happens.
Warning: Spoiler!
/**
* Copyright (C) 2012 Jorge Jimenez (jorge@iryoku.com). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
* IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holders.
*/
#include "..\..\SweetFX_preset.txt"
cbuffer UpdatedOncePerFrame {
float2 pixelSize;
float noiseIntensity;
float exposure;
float t;
}
Texture2D srcTex;
Texture3D noiseTex;
sampler2D LinearSampler {
Texture = <srcTex>;
AddressU = Clamp;
AddressV = Clamp;
MipFilter = point; MinFilter = Linear; MagFilter = Linear;
SRGBTexture = false;
};
sampler2D LinearSamplerWrap {
Texture = <noiseTex>;
AddressU = Wrap;
AddressV = Wrap;
AddressW = Wrap;
MipFilter = point; MinFilter = Linear; MagFilter = Linear;
SRGBTexture = false;
};
void PassVS(float4 position : POSITION,
out float4 svposition : SV_POSITION,
inout float2 texcoord : TEXCOORD0) {
svposition = position;
}
float3 Overlay(float3 a, float3 b) {
return pow(abs(b), 2.2) < 0.5? 2 * a * b : 1.0 - 2 * (1.0 - a) * (1.0 - b);
}
float3 AddNoise(float3 color, float2 texcoord) {
float2 coord = texcoord * FilmGrainSize;
coord.x *= pixelSize.y / pixelSize.x;
//float noise = noiseTex.Sample(LinearSamplerWrap, float3(coord, t)).r;
float noise = tex2D(LinearSamplerWrap, coord + float2(t, t *4)).r;
float exposureFactor = exposure / 2.0;
exposureFactor = sqrt(exposureFactor);
float t = lerp(3.5 * noiseIntensity, 1.13 * noiseIntensity, exposureFactor);
return Overlay(color, lerp(0.5, noise, t));
}
float4 FilmGrainPS(float4 position : SV_POSITION,
float2 texcoord : TEXCOORD0) : COLOR {
float3 color = tex2D(LinearSampler, texcoord).rgb;
color = AddNoise(color, texcoord);
return float4(color, 1.0);
}
technique FilmGrain {
pass FilmGrain {
VertexShader = compile vs_3_0 PassVS();
PixelShader = compile ps_3_0 FilmGrainPS();
SRGBWriteEnable = false;
AlphaBlendEnable = false;
StencilEnable = false;
}
}
* Copyright (C) 2012 Jorge Jimenez (jorge@iryoku.com). All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
* IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holders.
*/
#include "..\..\SweetFX_preset.txt"
cbuffer UpdatedOncePerFrame {
float2 pixelSize;
float noiseIntensity;
float exposure;
float t;
}
Texture2D srcTex;
Texture3D noiseTex;
sampler2D LinearSampler {
Texture = <srcTex>;
AddressU = Clamp;
AddressV = Clamp;
MipFilter = point; MinFilter = Linear; MagFilter = Linear;
SRGBTexture = false;
};
sampler2D LinearSamplerWrap {
Texture = <noiseTex>;
AddressU = Wrap;
AddressV = Wrap;
AddressW = Wrap;
MipFilter = point; MinFilter = Linear; MagFilter = Linear;
SRGBTexture = false;
};
void PassVS(float4 position : POSITION,
out float4 svposition : SV_POSITION,
inout float2 texcoord : TEXCOORD0) {
svposition = position;
}
float3 Overlay(float3 a, float3 b) {
return pow(abs(b), 2.2) < 0.5? 2 * a * b : 1.0 - 2 * (1.0 - a) * (1.0 - b);
}
float3 AddNoise(float3 color, float2 texcoord) {
float2 coord = texcoord * FilmGrainSize;
coord.x *= pixelSize.y / pixelSize.x;
//float noise = noiseTex.Sample(LinearSamplerWrap, float3(coord, t)).r;
float noise = tex2D(LinearSamplerWrap, coord + float2(t, t *4)).r;
float exposureFactor = exposure / 2.0;
exposureFactor = sqrt(exposureFactor);
float t = lerp(3.5 * noiseIntensity, 1.13 * noiseIntensity, exposureFactor);
return Overlay(color, lerp(0.5, noise, t));
}
float4 FilmGrainPS(float4 position : SV_POSITION,
float2 texcoord : TEXCOORD0) : COLOR {
float3 color = tex2D(LinearSampler, texcoord).rgb;
color = AddNoise(color, texcoord);
return float4(color, 1.0);
}
technique FilmGrain {
pass FilmGrain {
VertexShader = compile vs_3_0 PassVS();
PixelShader = compile ps_3_0 FilmGrainPS();
SRGBWriteEnable = false;
AlphaBlendEnable = false;
StencilEnable = false;
}
}
In the source repository linked above there is some files for the grain shader which isn't visible in the sweetFX shader visible to the user. So I don't know if that points to anything either.
If anyone could take a glance and offer any ideas, that'd be stupendous. Thanks a lot.
I haven't tried Reshade's grain in a while but last I recall it didn't work correctly. I do remember using the grain in the 1.0/2.0 releases that was ported from the Grain shader in GeDoSaTo and enjoying that quite a bit. (Which is my next favorite to use). But the real reason I don't want to use Reshade is because I prefer to force AA at the driver level (I manage the HBAO+/AA threads on Guru3D forums.guru3d.com/forums/videocards-nvid...-drivers-section.21/ ), and all I really need is some basic sharpening most of the time after AA. The other reason is that ReShade 3 interferes with forcing AA (Likely due to surface formats), often requiring extra compatibility to work and it also can alter the efficacy of shaders and also can catastrophically drop performance when forcing AA.
Or Perhaps would anyone be able to port this shader to reshade as is, and test to see if it does the same thing??
Last edit: 6 years 2 months ago by BONKERS.
Please Log in or Create an account to join the conversation.
6 years 3 weeks ago - 6 years 3 weeks ago #2
by Fu-Bama
Replied by Fu-Bama on topic Problem with an old SweetFX Grain shader
I think the degradation over time has something to do with random number generator, where the seed is the frame number.
When there's no sine in the random function or seed exceeds certain number, some glitch happens.
I recently made film grain fx as the ReShade default dropped for me 10FPS in every game. Maybe you can try this one and see if you like it:
EDIT*
I posted the updated version in presentation topics reshade.me/forum/shader-presentation/4035-simple-grain
When there's no sine in the random function or seed exceeds certain number, some glitch happens.
I recently made film grain fx as the ReShade default dropped for me 10FPS in every game. Maybe you can try this one and see if you like it:
Warning: Spoiler!
Just copy and save as SimpleGrain.fx
/*
Simple Grain PS v1.0.1 (c) 2018 Jacob Maximilian Fober
This work is licensed under the Creative Commons
Attribution-ShareAlike 4.0 International License.
To view a copy of this license, visit
http://creativecommons.org/licenses/by-sa/4.0/.
*/
////////////////////
/////// MENU ///////
////////////////////
uniform float Intensity <
ui_label = "Noise intensity";
ui_type = "drag";
ui_min = 0.0; ui_max = 1.0; ui_step = 0.002;
> = 0.4;
uniform int Coefficient <
ui_label = "Luma coefficient";
ui_tooltip = "For digital connection use BT.709, for analog (like VGA) use BT.601";
ui_type = "combo";
ui_items = "BT.709\0BT.601\0";
> = 0;
uniform int Framerate <
ui_label = "Noise framerate";
ui_tooltip = "Zero will match in-game framerate";
ui_type = "drag";
ui_min = 0; ui_max = 120; ui_step = 1;
> = 12;
//////////////////////
/////// SHADER ///////
//////////////////////
uniform float Timer < source = "timer"; >;
uniform int FrameCount < source = "framecount"; >;
// Overlay blending mode
float Overlay(float LayerA, float LayerB)
{
float MinA = min(LayerA, 0.5);
float MinB = min(LayerB, 0.5);
float MaxA = max(LayerA, 0.5);
float MaxB = max(LayerB, 0.5);
return 2 * (MinA * MinB + MaxA + MaxB - MaxA * MaxB) - 1.5;
}
// Convert RGB to YUV.luma
float Luma(float3 Source, float3 Coefficients)
{
float3 Result = Source * Coefficients;
return Result.r + Result.g + Result.b;
}
// Noise generator
float noise(float p)
{
return frac(sin(dot(p, float2(12.9898, 78.233))) * 43758.5453);
}
#include "ReShade.fxh"
// Shader pass
void SimpleGrainPS(float4 vois : SV_Position, float2 TexCoord : TEXCOORD, out float3 Image : SV_Target)
{
// Choose luma coefficient, if True BT.709 Luma, else BT.601 Luma
const float3 LumaCoefficient = (Coefficient == 0) ?
float3( 0.2126, 0.7152, 0.0722) : float3( 0.299, 0.587, 0.114)
;
// Sample image
Image = tex2D(ReShade::BackBuffer, TexCoord).rgb;
// Mask out bright pixels gamma: (sqrt(5)+1)/2
float Mask = pow(1 - Luma(Image, LumaCoefficient), 1.6180339887498948482);
// Calculate seed change
float Seed = Framerate == 0 ? FrameCount : floor(Timer * 0.001 * Framerate);
// Generate noise * (sqrt(5) + 1) / 4 (to remain brightness)
float Noise = saturate(noise(Seed * TexCoord.x * TexCoord.y) * 0.8090169943749474241);
Noise = lerp(0.5, Noise, Intensity * 0.1 * Mask);
// Blend noise with image
Image = float3(
Overlay(Image.r, Noise),
Overlay(Image.g, Noise),
Overlay(Image.b, Noise)
);
}
technique SimpleGrain
{
pass
{
VertexShader = PostProcessVS;
PixelShader = SimpleGrainPS;
}
}
EDIT*
I posted the updated version in presentation topics reshade.me/forum/shader-presentation/4035-simple-grain
Last edit: 6 years 3 weeks ago by Fu-Bama.
The following user(s) said Thank You: sajittarius
Please Log in or Create an account to join the conversation.
- Marty McFly
Less
More
6 years 3 weeks ago - 6 years 3 weeks ago #3
by Marty McFly
Replied by Marty McFly on topic Problem with an old SweetFX Grain shader
One can fix such RNG's that degrade with high timer values by just lowering the value periodically by timervalue mod largevalue.
Fu-Bama: is there a special reason for the golden ratio in the mask or not?
Also: float Luma(float3 Source, float3 Coefficients) {return dot(Source, Coefficients);}
Fu-Bama: is there a special reason for the golden ratio in the mask or not?
Also: float Luma(float3 Source, float3 Coefficients) {return dot(Source, Coefficients);}
Last edit: 6 years 3 weeks ago by Marty McFly.
Please Log in or Create an account to join the conversation.
6 years 3 weeks ago - 6 years 3 weeks ago #4
by Fu-Bama
Replied by Fu-Bama on topic Problem with an old SweetFX Grain shader
I've set the noise mask to inverted luminance value, but there was too much noise in the gray areas. So I played with some values to get sweet spot. 2 was too big, 1.5 too small. Something around 1.6 gamma was alright so I went with golden ratio.
And thanks for the mod and dot function.
And thanks for the mod and dot function.
Last edit: 6 years 3 weeks ago by Fu-Bama.
Please Log in or Create an account to join the conversation.
6 years 3 weeks ago - 6 years 3 weeks ago #5
by Fu-Bama
Replied by Fu-Bama on topic Problem with an old SweetFX Grain shader
I tested it out and it creates periodic flashing of no-noise.Marty McFly wrote: (...) by just lowering the value periodically by timervalue mod largevalue. (...)
Simulated 8h gameplay by adding 1728000 to the FrameCount.
*EDIT
I found something that works.
Seed = frac(Seed * 0.0001) * 10000;
Last edit: 6 years 3 weeks ago by Fu-Bama. Reason: added solution
Please Log in or Create an account to join the conversation.
- BONKERS
- Topic Author
Less
More
6 years 2 weeks ago - 6 years 2 weeks ago #6
by BONKERS
Replied by BONKERS on topic Problem with an old SweetFX Grain shader
Ah, I will try that shader. Thanks for the replies.
That simple grain shader is pretty nice for the performance. Seems pretty similar the one I posted above looks wise.
Though the old one used to have the effect of slightly changing the exposure of the whites. I think this was influenced by the noise texture used however. As the texture I made for it was different than the default and I don't think that one did the same.
I will probably use this in the future in addition to the other ReShade Grain shaders. (I still keep an old version of Reshade around that has the properly working GrainShader ported from GeDoSaTo. As I like that one too. But performance is a bit costlier.)
That simple grain shader is pretty nice for the performance. Seems pretty similar the one I posted above looks wise.
Though the old one used to have the effect of slightly changing the exposure of the whites. I think this was influenced by the noise texture used however. As the texture I made for it was different than the default and I don't think that one did the same.
I will probably use this in the future in addition to the other ReShade Grain shaders. (I still keep an old version of Reshade around that has the properly working GrainShader ported from GeDoSaTo. As I like that one too. But performance is a bit costlier.)
Last edit: 6 years 2 weeks ago by BONKERS.
Please Log in or Create an account to join the conversation.