Issues with temporal transition smoothing in fog shader.

  • Zentient
  • Topic Author
More
5 months 1 week ago #1 by Zentient Issues with temporal transition smoothing in fog shader. was created by Zentient
I'm trying to make a relatively simple depth fog shader that chooses a fog color based on the average color of the screen (This is accomplished with mipmaps) But any time I try to incorporate data from the previous frame (in the texture "prevFrame") with itself it returns as black. Note that I can use the data from the previous frame unmodified just fine, but when I modify it at all it changes to a value of float3(0); I've tried pulling the data from a separate texture than the rendertarget, but this caused the texture to show as black regardless of modification.

#include "Tools.fxh"
    
    
    
texture2D texDepth : DEPTH;
texture2D screenAVG {Width = BUFFER_WIDTH / 4; Height = BUFFER_HEIGHT / 4; MipLevels = 9; };
texture2D prevFrame {Width = 1; Height = 1;};


sampler2D prevMerge{Texture = prevFrame; };        
sampler2D screenData {Texture = screenAVG; };


uniform float Fog_Strength <
    ui_type = "slider";    
    ui_min = 0.0;
    ui_max = 1.0;
    ui_tooltip = "Fog + Depth Buffer Blend.";
> = 0.5;

uniform float Fog_Temporal <
    ui_type = "slider";
    ui_min = 0.0;
    ui_max = 1.0;
    ui_tooltip = "Fog Color Frame Persistence.";
> = 0.9;

uniform float Frametime < source = "frametime";>;        
    
#include "ReShade.fxh"


//Duplicates the Backbuffer to a separate texture for downsampling
float3 Screen(float4 vpos : SV_Position, float2 texcoord : TexCoord) : SV_Target
{
    float3 depth = tex2D(ReShade::DepthBuffer, texcoord).r;
    float3 blur = depth * tex2D(ReShade::BackBuffer, texcoord).rgb;
    
    return blur;    
}

//Downsamples the current frame to calculate a single value and blends it with the previous frame
float3 Temporal(float4 vpos : SV_Position, float2 texcoord : TexCoord) : SV_Target
{
    float3 frameColor = tex2Dlod(screenData, float4(texcoord, 8, 8)).rgb;
    float3 avgColor = tex2D(prevMerge, 1).rgb;
    float3 temp = frameColor * (1.0 - Fog_Temporal) + avgColor * Fog_Temporal;
    
    return temp;
}


//Unrefined fog things
float3 ZN_Fog_FXmain(float4 vpos : SV_Position, float2 texcoord : TexCoord) : SV_Target
{
    float3 frameColor = tex2Dlod(screenData, float4(texcoord, 8, 8)).rgb;
    float3 avgColor = tex2D(prevMerge, 1).rgb;
    float3 input = tex2D(ReShade::BackBuffer, texcoord).rgb;
    float depth = tex2D(ReShade::DepthBuffer, texcoord).r;
    depth = pow(depth, 20.0);
    float3 fog = depth * avgColor;
    input = lerp(input, lerp(input, avgColor, depth), Fog_Strength);

    return input;
}
    

technique Zentient_Fog
{
    pass
    {
        //Duplicates the buffer
        VertexShader = PostProcessVS;
        PixelShader = Screen;
        RenderTarget = screenAVG;
    }
    pass
    {
        //Modifies the 
        VertexShader = PostProcessVS;
        PixelShader = Temporal;
        RenderTarget = prevFrame;
    }
    pass
    {
        VertexShader = PostProcessVS;
        PixelShader = ZN_Fog_FXmain;
    }
    
    
    
}

 

Please Log in or Create an account to join the conversation.

  • crosire
More
5 months 1 week ago #2 by crosire Replied by crosire on topic Issues with temporal transition smoothing in fog shader.
One cannot read and write to the same texture in a graphics pass (which your "Temporal" pass does with the "prevFrame" texture).
You'll either have to double buffer the texture (add an additional pass that copies the data to a second texture which can then be read from) or use blending pass states to modify existing data instead of reading it in the shader (though looking and what the "Temporal" pixel shader does, I don't think it can be achieved with blending).
The following user(s) said Thank You: Zentient

Please Log in or Create an account to join the conversation.

We use cookies
We use cookies on our website. Some of them are essential for the operation of the forum. You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.