/**********************************************************************************
* Blueprint Reality Inc. CONFIDENTIAL
* 2020 Blueprint Reality Inc.
* All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains, the property of
* Blueprint Reality Inc. and its suppliers, if any.  The intellectual and
* technical concepts contained herein are proprietary to Blueprint Reality Inc.
* and its suppliers and may be covered by Patents, pending patents, and are
* protected by trade secret or copyright law.
*
* Dissemination of this information or reproduction of this material is strictly
* forbidden unless prior written permission is obtained from Blueprint Reality Inc.
***********************************************************************************/

#if MIXCAST_LWRP || MIXCAST_URP
using UnityEngine;
using UnityEngine.Rendering;
#if MIXCAST_LWRP
using UnityEngine.Rendering.LWRP;
#elif MIXCAST_URP
using UnityEngine.Rendering.Universal;
#endif

namespace BlueprintReality.MixCast
{
    public static class MixCastScriptableRenderPasses
    {
        public class ApplyCutoffPass : ScriptableRenderPass
        {
            ExpCameraBehaviour cam;
            public ApplyCutoffPass(ExpCameraBehaviour cam)
            {
                this.cam = cam;
                renderPassEvent = RenderPassEvent.BeforeRenderingPrepasses;
            }
            public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
            {
                Camera camera = renderingData.cameraData.camera;

                var cmd = CommandBufferPool.Get();
                cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
                cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, cam.ApplyCutoffMat);
                cmd.SetViewProjectionMatrices(camera.worldToCameraMatrix, camera.projectionMatrix);
                context.ExecuteCommandBuffer(cmd);
                CommandBufferPool.Release(cmd);
            }
        }

        public class SaveAlphaPass : ScriptableRenderPass
        {
            ExpCameraBehaviour cam;
            RenderTargetIdentifier colorTarget;
            public SaveAlphaPass(ExpCameraBehaviour cam)
            {
                this.cam = cam;
                renderPassEvent = RenderPassEvent.AfterRenderingTransparents;
            }
            public void Prepare(RenderTargetIdentifier colorTarget)
            {
                this.colorTarget = colorTarget;
            }
            public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
            {
                var cmd = CommandBufferPool.Get();
                Blit(cmd, colorTarget, cam.CleanForegroundTarget, cam.TransferAlphaMat);
                context.ExecuteCommandBuffer(cmd);
                CommandBufferPool.Release(cmd);
            }
        }
    }
}
#endif
