Browse Source

Reimplemented FSR 2.1 Unity plugin wrapper, making use of a temporary command buffer to satisfy its need. Ugly, but it works.

master
Nico de Poel 2 years ago
parent
commit
c51b788e98
  1. 110
      com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/Upscalers/FSR2WrapperUpscaler.cs
  2. 3
      com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/Upscalers/UpscalerPlugin.cs

110
com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/Upscalers/FSR2WrapperUpscaler.cs

@ -1,6 +1,6 @@
using System;
#if UNITY_STANDALONE_WIN && false
#if UNITY_STANDALONE_WIN
namespace UnityEngine.Rendering.HighDefinition.AMD.FSR2Wrapper
{
/// <summary>
@ -9,7 +9,7 @@ namespace UnityEngine.Rendering.HighDefinition.AMD.FSR2Wrapper
/// </summary>
public class FSR2WrapperUpscaler: UpscalerPlugin
{
public override string name => "FSR 2.1";
public override string name => "FSR 2.1 (Unity)";
public override bool isSupported => UnityEngine.AMD.AMDUnityPlugin.IsLoaded();
@ -28,12 +28,14 @@ namespace UnityEngine.Rendering.HighDefinition.AMD.FSR2Wrapper
public override UpscalerContext CreateContext(in FSR2CommandInitializationData initSettings)
{
throw new NotImplementedException();
var context = new FSR2WrappedContext(in initSettings);
context.Init();
return context;
}
public override void DestroyContext(UpscalerContext context)
{
throw new NotImplementedException();
((FSR2WrappedContext)context).Destroy();
}
public override bool GetRenderResolutionFromQualityMode(FSR2Quality qualityMode, uint displayWidth, uint displayHeight, out uint renderWidth, out uint renderHeight)
@ -46,81 +48,67 @@ namespace UnityEngine.Rendering.HighDefinition.AMD.FSR2Wrapper
return UnityEngine.AMD.GraphicsDevice.device.GetUpscaleRatioFromQualityMode((UnityEngine.AMD.FSR2Quality)qualityMode);
}
}
public class FSR2WrappedGraphicsDevice: GraphicsDevice
{
private readonly UnityEngine.AMD.GraphicsDevice _wrappedDevice;
internal FSR2WrappedGraphicsDevice(UnityEngine.AMD.GraphicsDevice wrappedDevice)
{
_wrappedDevice = wrappedDevice;
}
public override FSR2Context CreateFeature(CommandBuffer cmd, in FSR2CommandInitializationData initSettings)
{
var wrappedInitSettings = initSettings.ToWrapped();
return new FSR2WrappedContext(_wrappedDevice.CreateFeature(cmd, in wrappedInitSettings), in initSettings);
}
public override void DestroyFeature(CommandBuffer cmd, FSR2Context fsrContext)
{
_wrappedDevice.DestroyFeature(cmd, ((FSR2WrappedContext)fsrContext).WrappedContext);
}
public override void ExecuteFSR2(CommandBuffer cmd, FSR2Context fsr2Context, in FSR2TextureTable textures)
{
((FSR2WrappedContext)fsr2Context).SyncExecuteData();
var wrappedTextures = textures.ToWrapped();
_wrappedDevice.ExecuteFSR2(cmd, ((FSR2WrappedContext)fsr2Context).WrappedContext, in wrappedTextures);
}
public override bool GetRenderResolutionFromQualityMode(FSR2Quality qualityMode, uint displayWidth, uint displayHeight, out uint renderWidth, out uint renderHeight)
{
return _wrappedDevice.GetRenderResolutionFromQualityMode((UnityEngine.AMD.FSR2Quality)qualityMode, displayWidth, displayHeight, out renderWidth, out renderHeight);
}
public override float GetUpscaleRatioFromQualityMode(FSR2Quality qualityMode)
{
return _wrappedDevice.GetUpscaleRatioFromQualityMode((UnityEngine.AMD.FSR2Quality)qualityMode);
}
}
public class FSR2WrappedContext : UpscalerContext
{
private readonly FSR2CommandInitializationData _initData;
private UnityEngine.AMD.FSR2Context _wrappedContext;
private CommandBuffer _tempCmd;
internal FSR2WrappedContext(in FSR2CommandInitializationData initData)
{
_initData = initData;
}
internal void Init(UnityEngine.AMD.GraphicsDevice graphicsDevice)
internal void Init()
{
_wrappedContext = graphicsDevice.CreateFeature()
_tempCmd ??= new CommandBuffer { name = "FSR2Wrapper" };
_tempCmd.Clear();
var initSettings = _initData.ToWrapped();
_wrappedContext = UnityEngine.AMD.GraphicsDevice.device.CreateFeature(_tempCmd, in initSettings);
Graphics.ExecuteCommandBuffer(_tempCmd);
}
internal void SyncExecuteData()
internal void Destroy()
{
WrappedContext.executeData.jitterOffsetX = _executeData.jitterOffsetX;
WrappedContext.executeData.jitterOffsetY = _executeData.jitterOffsetY;
WrappedContext.executeData.MVScaleX = _executeData.MVScaleX;
WrappedContext.executeData.MVScaleY = _executeData.MVScaleY;
WrappedContext.executeData.renderSizeWidth = _executeData.renderSizeWidth;
WrappedContext.executeData.renderSizeHeight = _executeData.renderSizeHeight;
WrappedContext.executeData.enableSharpening = _executeData.enableSharpening;
WrappedContext.executeData.sharpness = _executeData.sharpness;
WrappedContext.executeData.frameTimeDelta = _executeData.frameTimeDelta;
WrappedContext.executeData.preExposure = _executeData.preExposure;
WrappedContext.executeData.reset = _executeData.reset;
WrappedContext.executeData.cameraNear = _executeData.cameraNear;
WrappedContext.executeData.cameraFar = _executeData.cameraFar;
WrappedContext.executeData.cameraFovAngleVertical = _executeData.cameraFovAngleVertical;
if (_wrappedContext == null)
return;
_tempCmd.Clear();
UnityEngine.AMD.GraphicsDevice.device.DestroyFeature(_tempCmd, _wrappedContext);
Graphics.ExecuteCommandBuffer(_tempCmd);
_wrappedContext = null;
_tempCmd.Release();
_tempCmd = null;
}
public override void Execute(CommandBuffer cmd, in FSR2CommandExecutionData executeData, in FSR2TextureTable textures)
{
throw new NotImplementedException();
SyncExecuteData(in executeData);
var tex = textures.ToWrapped();
UnityEngine.AMD.GraphicsDevice.device.ExecuteFSR2(cmd, _wrappedContext, in tex);
}
private void SyncExecuteData(in FSR2CommandExecutionData executeData)
{
_wrappedContext.executeData.jitterOffsetX = executeData.jitterOffsetX;
_wrappedContext.executeData.jitterOffsetY = executeData.jitterOffsetY;
_wrappedContext.executeData.MVScaleX = executeData.MVScaleX;
_wrappedContext.executeData.MVScaleY = executeData.MVScaleY;
_wrappedContext.executeData.renderSizeWidth = executeData.renderSizeWidth;
_wrappedContext.executeData.renderSizeHeight = executeData.renderSizeHeight;
_wrappedContext.executeData.enableSharpening = executeData.enableSharpening;
_wrappedContext.executeData.sharpness = executeData.sharpness;
_wrappedContext.executeData.frameTimeDelta = executeData.frameTimeDelta;
_wrappedContext.executeData.preExposure = executeData.preExposure;
_wrappedContext.executeData.reset = executeData.reset;
_wrappedContext.executeData.cameraNear = executeData.cameraNear;
_wrappedContext.executeData.cameraFar = executeData.cameraFar;
_wrappedContext.executeData.cameraFovAngleVertical = executeData.cameraFovAngleVertical;
}
}

3
com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/Upscalers/UpscalerPlugin.cs

@ -11,6 +11,9 @@ namespace UnityEngine.Rendering.HighDefinition.AMD
{
new FSR2.FSR2UpscalerPlugin(),
new FSR3.FSR3UpscalerPlugin(),
#if UNITY_STANDALONE_WIN
new FSR2Wrapper.FSR2WrapperUpscaler(),
#endif
};
private static UpscalerPlugin _activePlugin = AvailablePlugins[0];

Loading…
Cancel
Save