using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

namespace URP
{

    /// <summary>
    /// FinalBlitPass 扩展
    /// </summary>

    public class FinalBlitPassEx
    {
        public static bool ms_renderGTexture = false;
        public static bool ms_realtimeRenderGTexture = false;
        public static RenderTexture ms_GRenderTarget = null;

        public static Action<ScriptableRenderContext ,Camera, CommandBuffer, RenderTexture> GTextureRenderEndCallback = null;

        /// <summary>
        /// 获取GTexture
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cam"></param>
        /// <param name="cmd"></param>
        /// <param name="source"></param>
        /// <param name="blitMaterl"></param>
        public static void FinalBlitGTexture(ScriptableRenderContext context, Camera cam, CommandBuffer cmd, RenderTargetIdentifier source, Material blitMaterl)
        {
            if (!ms_renderGTexture)
			{
				if(ms_GRenderTarget != null)
				{
					RenderTexture.ReleaseTemporary(ms_GRenderTarget);
					ms_GRenderTarget.Release();
					ms_GRenderTarget = null;
				}				
				return;
			}
                

            if (ms_GRenderTarget == null)
            {
                ms_GRenderTarget = RenderTexture.GetTemporary(cam.scaledPixelWidth, cam.scaledPixelHeight, 0);
                ms_GRenderTarget.name = "GTexture RT";
            }
            cmd.Blit(source, ms_GRenderTarget);
            context.ExecuteCommandBuffer(cmd);

            ms_renderGTexture = ms_realtimeRenderGTexture;

            GTextureRenderEndCallback?.Invoke(context, cam, cmd, ms_GRenderTarget);
        }
    }
}