Skip to content

Commit

Permalink
TAA: fix projection matrix flip
Browse files Browse the repository at this point in the history
  • Loading branch information
Delt06 committed Oct 13, 2023
1 parent 7d38797 commit f340875
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public struct ToonCameraOverride
private readonly float _zFar;
private readonly Camera _camera;
private bool _overriden;
private readonly ToonCameraRenderTarget _cameraRenderTarget;
private readonly ToonAdditionalCameraData _additionalCameraData;

public ToonCameraOverride(Camera camera, ToonCameraRenderTarget cameraRenderTarget)
public ToonCameraOverride(Camera camera, ToonAdditionalCameraData additionalCameraData)
{
_camera = camera;
_cameraRenderTarget = cameraRenderTarget;
_additionalCameraData = additionalCameraData;
Rect pixelRect = camera.pixelRect;
_aspectRatio = pixelRect.width / pixelRect.height;
_zNear = camera.nearClipPlane;
Expand All @@ -38,14 +38,11 @@ public void OverrideIfEnabled(CommandBuffer cmd, in ToonCameraOverrideSettings s
}

float fieldOfView = settings.FieldOfView;
Matrix4x4 projectionMatrix =
ToonRpUtils.GetGPUProjectionMatrix(
Matrix4x4.Perspective(fieldOfView, _aspectRatio,
_zNear,
_zFar
),
_cameraRenderTarget
);
var matrix = Matrix4x4.Perspective(fieldOfView, _aspectRatio,
_zNear,
_zFar
);
Matrix4x4 projectionMatrix = ToonRpUtils.GetGPUProjectionMatrix(matrix, _camera);
ToonRpUtils.SetViewAndProjectionMatrices(cmd, _camera.worldToCameraMatrix, projectionMatrix, false);
_overriden = true;
}
Expand All @@ -57,7 +54,9 @@ public void RestoreIfEnabled(CommandBuffer cmd)
return;
}

ToonRpUtils.RestoreCameraMatrices(_camera, cmd, _cameraRenderTarget, false);
ToonRpUtils.SetViewAndProjectionMatrices(cmd, _camera.worldToCameraMatrix,
_additionalCameraData.MotionVectorsPersistentData.LastPrimaryProjectionMatrix, false
);
_overriden = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public class ToonInvertedHullOutline : ToonRenderingExtensionBase
private static readonly int NoiseFrequencyId = Shader.PropertyToID("_NoiseFrequency");
private static readonly int NoiseAmplitudeId = Shader.PropertyToID("_NoiseAmplitude");
private readonly List<Material> _materials = new();
private ToonAdditionalCameraData _additionalCameraData;

private Camera _camera;
private ToonCameraRendererSettings _cameraRendererSettings;
private ToonCameraRenderTarget _cameraRenderTarget;
private ScriptableRenderContext _context;
private CullingResults _cullingResults;

Expand All @@ -46,7 +46,7 @@ public override void Render()

CommandBuffer cmd = CommandBufferPool.Get();

var cameraOverride = new ToonCameraOverride(_camera, _cameraRenderTarget);
var cameraOverride = new ToonCameraOverride(_camera, _additionalCameraData);

using (new ProfilingScope(cmd, NamedProfilingSampler.Get(ToonRpPassId.InvertedHullOutlines)))
{
Expand Down Expand Up @@ -161,11 +161,11 @@ public override void Setup(in ToonRenderingExtensionContext context,
IToonRenderingExtensionSettingsStorage settingsStorage)
{
_cameraRendererSettings = context.CameraRendererSettings;
_cameraRenderTarget = context.CameraRenderTarget;
_camera = context.Camera;
_context = context.ScriptableRenderContext;
_cullingResults = context.CullingResults;
_outlineSettings = settingsStorage.GetSettings<ToonInvertedHullOutlineSettings>(this);
_additionalCameraData = context.AdditionalCameraData;
PopulateMaterialsForAllPasses();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace DELTation.ToonRP.Extensions.BuiltIn
public class ToonRenderObjects : ToonRenderingExtensionBase
{
private readonly List<ShaderTagId> _lightModeTags = new();
private ToonAdditionalCameraData _additionalCameraData;
private Camera _camera;
private ToonCameraRendererSettings _cameraRendererSettings;
private ToonCameraRenderTarget _cameraRenderTarget;
private ScriptableRenderContext _context;
private CullingResults _cullingResults;
private ToonRenderObjectsSettings _settings;
Expand All @@ -22,8 +22,8 @@ public override void Setup(in ToonRenderingExtensionContext context,
_context = context.ScriptableRenderContext;
_camera = context.Camera;
_cameraRendererSettings = context.CameraRendererSettings;
_cameraRenderTarget = context.CameraRenderTarget;
_cullingResults = context.CullingResults;
_additionalCameraData = context.AdditionalCameraData;
}

public override void Render()
Expand All @@ -46,7 +46,7 @@ public override void Render()
}
}

var cameraOverride = new ToonCameraOverride(_camera, _cameraRenderTarget);
var cameraOverride = new ToonCameraOverride(_camera, _additionalCameraData);
cameraOverride.OverrideIfEnabled(cmd, _settings.Overrides.Camera);
_context.ExecuteCommandBufferAndClear(cmd);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ public readonly struct ToonRenderingExtensionContext
public readonly ToonCameraRendererSettings CameraRendererSettings;
public readonly CullingResults CullingResults;
public readonly ToonCameraRenderTarget CameraRenderTarget;
public readonly ToonAdditionalCameraData AdditionalCameraData;

public ToonRenderingExtensionContext(ScriptableRenderContext scriptableRenderContext, Camera camera,
ToonCameraRendererSettings cameraRendererSettings, CullingResults cullingResults,
ToonCameraRenderTarget cameraRenderTarget)
ToonCameraRenderTarget cameraRenderTarget, ToonAdditionalCameraData additionalCameraData)
{
ScriptableRenderContext = scriptableRenderContext;
Camera = camera;
CameraRendererSettings = cameraRendererSettings;
CullingResults = cullingResults;
CameraRenderTarget = cameraRenderTarget;
AdditionalCameraData = additionalCameraData;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void CullLights()
CommandBufferPool.Release(cmd);
}

public static void PrepareForOpaqueGeometry(CommandBuffer cmd)
public void PrepareForOpaqueGeometry(CommandBuffer cmd)
{
PrepareForGeometryPass(cmd, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,33 @@ public static class ToonTemporalAAUtils
public static Matrix4x4 CalculateJitterMatrix(in ToonPostProcessingSettings postProcessingSettings,
ToonCameraRenderTarget renderTarget)
{
Matrix4x4 jitterMat = Matrix4x4.identity;
Matrix4x4 jitterMatrix = Matrix4x4.identity;

if (TryGetTaaSettings(postProcessingSettings, out ToonTemporalAASettings taaSettings))
if (!postProcessingSettings.Enabled)
{
int taaFrameIndex = Time.frameCount;
return jitterMatrix;
}

if (!TryGetTaaSettings(postProcessingSettings, out ToonTemporalAASettings taaSettings))
{
return jitterMatrix;
}

// TODO: check whether we should use camera or RT size
float actualWidth = renderTarget.Width;
float actualHeight = renderTarget.Height;
float jitterScale = taaSettings.JitterScale;
int taaFrameIndex = Time.frameCount;

Vector2 jitter = CalculateJitter(taaFrameIndex) * jitterScale;
// TODO: check whether we should use camera or RT size
float actualWidth = renderTarget.Width;
float actualHeight = renderTarget.Height;
float jitterScale = taaSettings.JitterScale;

float offsetX = jitter.x * (2.0f / actualWidth);
float offsetY = jitter.y * (2.0f / actualHeight);
Vector2 jitter = CalculateJitter(taaFrameIndex) * jitterScale;

jitterMat = Matrix4x4.Translate(new Vector3(offsetX, offsetY, 0.0f));
}
float offsetX = jitter.x * (2.0f / actualWidth);
float offsetY = jitter.y * (2.0f / actualHeight);

jitterMatrix = Matrix4x4.Translate(new Vector3(offsetX, offsetY, 0.0f));

return jitterMat;
return jitterMatrix;
}

private static Vector2 CalculateJitter(int frameIndex)
Expand Down
6 changes: 3 additions & 3 deletions Packages/com.deltation.toon-rp/Runtime/ToonCameraData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ public readonly struct ToonCameraData
public readonly float AspectRatio;
public readonly Matrix4x4 ViewMatrix;
public readonly Matrix4x4 ProjectionMatrix;
public readonly Matrix4x4 JitteredProjectionMatrix;
public readonly Matrix4x4 JitterMatrix;

public ToonCameraData(Camera camera, Matrix4x4 jitteredProjectionMatrix)
public ToonCameraData(Camera camera, Matrix4x4 jitterMatrix)
{
Camera = camera;
AspectRatio = camera.aspect;
ViewMatrix = camera.worldToCameraMatrix;
ProjectionMatrix = camera.projectionMatrix;
JitteredProjectionMatrix = jitteredProjectionMatrix;
JitterMatrix = jitterMatrix;
}
}
}
40 changes: 26 additions & 14 deletions Packages/com.deltation.toon-rp/Runtime/ToonCameraRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public sealed partial class ToonCameraRenderer : IDisposable
private ToonAdditionalCameraData _additionalCameraData;

private Camera _camera;
private ToonCameraData _cameraData;

private string _cmdName = DefaultCmdName;
private ScriptableRenderContext _context;
Expand Down Expand Up @@ -162,6 +163,8 @@ public void Render(
{
_extensionsCollection.RenderEvent(ToonRenderingEvent.BeforePrepass);

_context.ExecuteCommandBufferAndClear(cmd);

if (_prePassMode.Includes(PrePassMode.Depth))
{
_depthPrePass.Setup(_context, _cullingResults, _camera, settings, _prePassMode,
Expand Down Expand Up @@ -330,32 +333,36 @@ private void Setup(CommandBuffer cmd, in ToonRampSettings globalRampSettings,

RTHandles.SetReferenceSize(rtWidth, rtHeight);

_context.ExecuteCommandBufferAndClear(cmd);
Matrix4x4 jitterMatrix = ToonTemporalAAUtils.CalculateJitterMatrix(postProcessingSettings, _renderTarget);
_cameraData = new ToonCameraData(_camera, jitterMatrix);

Matrix4x4 originalProjectionMatrix = _camera.projectionMatrix;
Matrix4x4 jitteredProjectionMatrix = _camera.cameraType == CameraType.Game
? ToonTemporalAAUtils.CalculateJitterMatrix(postProcessingSettings, _renderTarget) *
originalProjectionMatrix
: originalProjectionMatrix;
_context.SetupCameraProperties(_camera);

ToonCameraData cameraData = new(_camera, jitteredProjectionMatrix);
Matrix4x4 gpuProjectionMatrix =
ToonRpUtils.GetGPUProjectionMatrix(cameraData.JitteredProjectionMatrix, _renderTarget);
ToonRpUtils.SetViewAndProjectionMatrices(cmd, _camera.worldToCameraMatrix, gpuProjectionMatrix, true);
UpdateProjectionMatrices(cmd);
_context.ExecuteCommandBufferAndClear(cmd);

if (_prePassMode.Includes(PrePassMode.MotionVectors))
{
SupportedRenderingFeatures.active.motionVectors = true;
_additionalCameraData.MotionVectorsPersistentData.Update(cameraData);
_additionalCameraData.MotionVectorsPersistentData.Update(_cameraData);
}

_extensionContext =
new ToonRenderingExtensionContext(_context, _camera, _settings, _cullingResults, _renderTarget);
new ToonRenderingExtensionContext(_context, _camera, _settings, _cullingResults, _renderTarget,
_additionalCameraData
);

_tiledLighting.Setup(_context, _extensionContext);
}

private void UpdateProjectionMatrices(CommandBuffer cmd)
{
Matrix4x4 gpuProjectionMatrix =
ToonRpUtils.GetGPUProjectionMatrix(_cameraData.JitterMatrix * _cameraData.ProjectionMatrix, _camera);
ToonRpUtils.SetViewAndProjectionMatrices(cmd, _camera.worldToCameraMatrix, gpuProjectionMatrix, true);
_additionalCameraData.MotionVectorsPersistentData.LastPrimaryProjectionMatrix = gpuProjectionMatrix;
_context.SetupCameraProperties(_camera);
}

private bool RequireStencil(in ToonRenderingExtensionSettings extensionSettings)
{
if (_settings.Stencil)
Expand Down Expand Up @@ -517,7 +524,10 @@ private void DrawVisibleGeometry(CommandBuffer cmd)
_context.ExecuteCommandBufferAndClear(cmd);

{
ToonTiledLighting.PrepareForOpaqueGeometry(cmd);
_tiledLighting.PrepareForOpaqueGeometry(cmd);

// UpdateProjectionMatrices(cmd, false);
_context.ExecuteCommandBufferAndClear(cmd);

_extensionsCollection.RenderEvent(ToonRenderingEvent.BeforeOpaque);

Expand All @@ -539,8 +549,10 @@ private void DrawVisibleGeometry(CommandBuffer cmd)
{
_tiledLighting.PrepareForTransparentGeometry(cmd);

// UpdateProjectionMatrices(cmd, false);
_extensionsCollection.RenderEvent(ToonRenderingEvent.BeforeTransparent);


using (new ProfilingScope(cmd, NamedProfilingSampler.Get(ToonRpPassId.TransparentGeometry)))
{
_context.ExecuteCommandBufferAndClear(cmd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public sealed class ToonMotionVectorsPersistentData

internal Matrix4x4 PreviousViewProjection { get; private set; } = Matrix4x4.identity;

public Matrix4x4 LastPrimaryProjectionMatrix { get; set; }

public void Update(in ToonCameraData cameraData)
{
bool aspectChanged = !Mathf.Approximately(_prevAspectRatio, cameraData.AspectRatio);
Expand Down
16 changes: 10 additions & 6 deletions Packages/com.deltation.toon-rp/Runtime/ToonRpUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,18 @@ public static void SetViewAndProjectionMatrices(CommandBuffer cmd, Matrix4x4 vie
}

public static Matrix4x4 GetGPUProjectionMatrix(Matrix4x4 projectionMatrix,
ToonCameraRenderTarget cameraRenderTarget) =>
GL.GetGPUProjectionMatrix(projectionMatrix, cameraRenderTarget.RenderToTexture);
Camera camera) =>
GL.GetGPUProjectionMatrix(projectionMatrix, IsCameraProjectionMatrixFlipped(camera));

public static void RestoreCameraMatrices(Camera camera, CommandBuffer cmd,
ToonCameraRenderTarget cameraRenderTarget, bool setInverseMatrices)
private static bool IsCameraProjectionMatrixFlipped(Camera camera)
{
Matrix4x4 gpuProjectionMatrix = GetGPUProjectionMatrix(camera.projectionMatrix, cameraRenderTarget);
SetViewAndProjectionMatrices(cmd, camera.worldToCameraMatrix, gpuProjectionMatrix, setInverseMatrices);
if (!SystemInfo.graphicsUVStartsAtTop)
{
return false;
}

CameraType cameraType = camera.cameraType;
return cameraType is CameraType.SceneView or CameraType.Preview;
}

public static Vector4 BuildRampVectorFromEdges(float edge1, float edge2) =>
Expand Down

0 comments on commit f340875

Please sign in to comment.