using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

public class BlendPass : ScriptableRenderPass
{
    private GlitchFeature.Settings _settings;
    private RenderTargetIdentifier _src;
    private RenderTargetIdentifier _dest;

    private int _temporaryRTID = Shader.PropertyToID("_TempRT");
    private RenderTargetIdentifier _temp;

    public BlendPass(GlitchFeature.Settings settings)
    {
        _settings = settings;
        _src = new RenderTargetIdentifier(_settings.stencilTex);
        _dest = new RenderTargetIdentifier(_settings.blendTex);
        _temp = new RenderTargetIdentifier(_temporaryRTID);
    }

    public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    {
        var cmd = CommandBufferPool.Get();
        
        cmd.GetTemporaryRT(_temporaryRTID, _settings.blendTex.descriptor);
        
        // set properties on material
        var blend = _settings.blendMat;
        blend.SetFloat("_Sample", _settings.sample * Time.deltaTime);
        blend.SetTexture("_BlendTex", _settings.blendTex);
        // blend.SetFloat("_DeltaTime", Time.deltaTime);
        
        // blend.SetTexture("_GlitchTex", Settings.glitchTex);
        // blend.SetTexture("_BlendTex", Settings.blendTex);
        
        cmd.Blit(_src,_temporaryRTID,blend);
        cmd.Blit(_temporaryRTID, _dest);
        
        context.ExecuteCommandBuffer(cmd);
    }

    public override void FrameCleanup(CommandBuffer cmd)
    {
        cmd.ReleaseTemporaryRT(_temporaryRTID);
    }
}
