76 lines
2.6 KiB
C#
Raw Normal View History

2024-11-22 12:09:31 +08:00
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace X.Rendering.Feature
{
public enum EFrameGeneration
{
AFME,// Adaptive Frame Motion Extrapolation GL_QCOM_frame_extrapolation
HW_INTERPOLATE,// 华为 Graphics Accelerate Kit 内插值
HW_EXTRAPOLATION,// 华为 Graphics Accelerate Kit 外插值
//VGS,// vivo
FSR2,
FSR3,
}
public unsafe class FG
{
static int frameIndex = 0;
static RTHandle[] historys = new RTHandle[2];
static RTHandle presentRt;
static bool presented = false;
public static bool UseFG = true;
public static bool CanDoFG => frameIndex > 0 && frameIndex % 3 == 0 && !presented && UseFG;
[StructLayout(LayoutKind.Sequential)]
struct AFMEParam
{
public IntPtr src;
public IntPtr data;
public IntPtr dst;
}
static IntPtr data = Marshal.AllocHGlobal(sizeof(AFMEParam));
public static bool FrameGenearte(ScriptableRenderContext context, CommandBuffer cmd, out RTHandle dst)
{
#if UNITY_ANDROID && !UNITY_EDITOR
Debug.Log("FrameGenearte" + frameIndex);
dst = presentRt;
var history1 = historys[frameIndex % 2];
var history2 = historys[frameIndex % 2 == 1 ? 0 : 1];
AFMEParam* p = (AFMEParam*)data.ToPointer();
p->src = history1.rt.GetNativeTexturePtr();
p->data = history2.rt.GetNativeTexturePtr();
p->dst = dst.rt.GetNativeTexturePtr();
cmd.IssuePluginEventAndData(RenderingPlugin.GetRenderEventAndDataFunc(), (int)RenderingPlugin.NativeRenderingEvent.DoFGExtrapolation, data);
context.ExecuteCommandBuffer(cmd);
cmd.Clear();
presented = true;
return true;
#else
dst = null;
return false;
#endif
}
public static void RecordFrame(RTHandle source, CommandBuffer cmd)
{
presented = false;
var index = frameIndex++ % 2;
var desc = source.rt.descriptor;
desc.graphicsFormat = UnityEngine.Experimental.Rendering.GraphicsFormat.R8G8B8A8_UNorm;
desc.depthBufferBits = 0;
RenderingUtils.ReAllocateIfNeeded(ref historys[0], desc);
RenderingUtils.ReAllocateIfNeeded(ref historys[1], desc);
RenderingUtils.ReAllocateIfNeeded(ref presentRt, desc);
var history = historys[index];
cmd.Blit(source, history);
}
}
}