2024-12-26 14:17:41 +08:00

122 lines
4.0 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using NVStreamline;
namespace NVStreamline
{
[AddComponentMenu("NVIDIA/Streamline DLSS-G HDRP")]
public class StreamlineDLSSGHDRP : MonoBehaviour
{
private Camera m_camera;
private uint m_id;
public bool DLSSGFeatureAvailable { get; private set; } = false;
#if false
//Put 5x3Compute shader in test project and drag onto this in editor
public ComputeShader TESTcs5x3 = null;
public ComputeShader GetFont5x3Shader()
{
return TESTcs5x3;
}
#endif
public void SetupDepthAndMVecTextureTags(CommandBuffer cmd, int frameCount)
{
uint frameIndex = (uint)frameCount;
if (StreamlineDLSSGCore.DLSSGEnabled)
{
RenderTexture targetDepth = StreamlineDLSSGCore.GetTargetDepthTexture();
RenderTexture targetMVecs = StreamlineDLSSGCore.GetTargetMVecsTexture();
StreamlineCore.SetTag(cmd, frameIndex, m_id, targetDepth, StreamlineCore.BufferType.eBufferTypeDepth, 0);
StreamlineCore.SetTag(cmd, frameIndex, m_id, targetMVecs, StreamlineCore.BufferType.eBufferTypeMVec, 0);
}
else
{
StreamlineCore.SetTag(cmd, frameIndex, m_id, null, StreamlineCore.BufferType.eBufferTypeDepth, 0);
StreamlineCore.SetTag(cmd, frameIndex, m_id, null, StreamlineCore.BufferType.eBufferTypeMVec, 0);
}
}
public void SetupHudlessColorTextureTags(CommandBuffer cmd, int frameCount)
{
uint frameIndex = (uint)frameCount;
if (StreamlineDLSSGCore.DLSSGEnabled && StreamlineDLSSGCore.UseHudlessColor)
{
RenderTexture targetHudlessColor = StreamlineDLSSGCore.GetTargetHudlessColorTexture();
StreamlineCore.SetTag(cmd, frameIndex, m_id, targetHudlessColor, StreamlineCore.BufferType.eBufferTypeHUDLessColor, 0);
}
else
{
StreamlineCore.SetTag(cmd, frameIndex, m_id, null, StreamlineCore.BufferType.eBufferTypeHUDLessColor, 0);
}
}
public void HDRPSetupDLSSGFeatureConstants(CommandBuffer cmdBuffer, Camera camera, int frameCount)
{
StreamlineDLSSGCore.SetupDLSSGFeatureConstants(cmdBuffer, camera, m_id, (uint)frameCount);
}
void Awake()
{
m_camera = GetComponent<Camera>();
DLSSGFeatureAvailable = false;
if (!StreamlineCore.IsFeatureSupported(StreamlineCore.Feature.eFeatureDLSS_G))
{
Debug.Log("DLSSG is not supported");
return;
}
int id = StreamlineCore.CreateFeature(StreamlineCore.Feature.eFeatureDLSS_G);
if (id != 0) //CreateFeature must return 0 for DLSSG
{
Debug.LogError("DLSSG Awake FAILED to get feature id");
return;
}
m_id = (uint)id;
DLSSGFeatureAvailable = true;
}
void OnDestroy()
{
if (DLSSGFeatureAvailable && m_id == 0)
{
StreamlineCore.DestroyFeature((int)m_id);
}
}
void OnEnable()
{
}
void OnDisable()
{
}
// Update is called once per frame
void LateUpdate()
{
if (DLSSGFeatureAvailable)
{
StreamlineDLSSGCore.ProcessDisposals();
if (StreamlineDLSSGCore.UpdateDrawRects(m_camera))
{
//reallocate tagged textures
StreamlineDLSSGCore.TaggedTextureDimensions tdd = StreamlineDLSSGCore.GetTaggedTextureDimensionsRef();
StreamlineDLSSGCore.AllocateTextures(tdd.m_currentDrawRect, tdd.m_currentViewRect, false, tdd.m_desktopScale, true);
}
}
}
}
}