Skip to content

Commit

Permalink
Simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
keijiro committed Sep 16, 2018
1 parent 66c19b9 commit 9f41068
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 124 deletions.
50 changes: 12 additions & 38 deletions Assets/Pix2Pix/PostProcessing/Pix2PixEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ sealed class Pix2PixRenderer : PostProcessEffectRenderer<Pix2PixEffect>
{
static class ShaderIDs
{
internal static readonly int EdgeParams = Shader.PropertyToID("_EdgeParams");
internal static readonly int UVRemap = Shader.PropertyToID("_UVRemap");
internal static readonly int PrevUVRemap = Shader.PropertyToID("_PrevUVRemap");
internal static readonly int PrevMoDepth = Shader.PropertyToID("_PrevMoDepth");
internal static readonly int DeltaTime = Shader.PropertyToID("_DeltaTime");
internal static readonly int EdgeParams = Shader.PropertyToID("_EdgeParams");
internal static readonly int RemapTex = Shader.PropertyToID("_RemapTex");
}

Dictionary<string, Tensor> _weightTable;
Expand All @@ -38,12 +35,7 @@ static class ShaderIDs

RenderTexture _source;
RenderTexture _result;

RenderTexture _prevUVRemap;
RenderTexture _prevMoDepth;

float _prevDeltaTime;
int _frameCount;
RenderTexture _remap;

public override void Init()
{
Expand Down Expand Up @@ -85,8 +77,7 @@ public override void Release()
RuntimeUtilities.Destroy(_source);
RuntimeUtilities.Destroy(_result);

if (_prevUVRemap != null) RenderTexture.ReleaseTemporary(_prevUVRemap);
if (_prevMoDepth != null) RenderTexture.ReleaseTemporary(_prevMoDepth);
if (_remap != null) RenderTexture.ReleaseTemporary(_remap);

base.Release();
}
Expand Down Expand Up @@ -130,37 +121,20 @@ public override void Render(PostProcessRenderContext context)
}

// Temporal reprojection pass
if (_prevUVRemap != null) props.SetTexture(ShaderIDs.PrevUVRemap, _prevUVRemap);
if (_prevMoDepth != null) props.SetTexture(ShaderIDs.PrevMoDepth, _prevMoDepth);
if (_remap != null) props.SetTexture(ShaderIDs.RemapTex, _remap);

props.SetVector(
ShaderIDs.DeltaTime,
new Vector2(Time.deltaTime, _prevDeltaTime)
);
var newRemap = RenderTexture.GetTemporary
(context.width, context.height, 0, RenderTextureFormat.RGHalf);

var uvRemap = RenderTexture.GetTemporary
(context.width, context.height, 0, RenderTextureFormat.ARGBHalf);
var moDepth = RenderTexture.GetTemporary
(context.width, context.height, 0, RenderTextureFormat.ARGBHalf);

_mrt[0] = uvRemap.colorBuffer;
_mrt[1] = moDepth.colorBuffer;
_mrt[0] = context.destination;
_mrt[1] = newRemap.colorBuffer;

cmd.BlitFullscreenTriangle
(context.source, _mrt, uvRemap.depthBuffer, sheet, update ? 1 : 2);

// Composition pass
props.SetTexture(ShaderIDs.UVRemap, uvRemap);
cmd.BlitFullscreenTriangle(_result, context.destination, sheet, 3);

// Discard the previous frame state.
if (_prevUVRemap != null) RenderTexture.ReleaseTemporary(_prevUVRemap);
if (_prevMoDepth != null) RenderTexture.ReleaseTemporary(_prevMoDepth);
(_result, _mrt, newRemap.depthBuffer, sheet, update ? 1 : 2);

// Update the internal state.
_prevUVRemap = uvRemap;
_prevMoDepth = moDepth;
_prevDeltaTime = Time.deltaTime;
if (_remap != null) RenderTexture.ReleaseTemporary(_remap);
_remap = newRemap;

cmd.EndSample("Pix2Pix");
}
Expand Down
3 changes: 0 additions & 3 deletions Assets/Pix2Pix/PostProcessing/Resources/EdgeDetection.hlsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include "PostProcessing/Shaders/StdLib.hlsl"
#include "PostProcessing/Shaders/Colors.hlsl"

TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
TEXTURE2D_SAMPLER2D(_EdgeTex, sampler_EdgeTex);
TEXTURE2D_SAMPLER2D(_PrevTex, sampler_PrevTex);

half2 _EdgeParams;

Expand Down
8 changes: 0 additions & 8 deletions Assets/Pix2Pix/PostProcessing/Resources/Pix2Pix.shader
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,5 @@ Shader "Hidden/Pix2Pix/PostProcessing"
#include "TemporalReprojection.hlsl"
ENDHLSL
}
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment FragComposite
#include "TemporalReprojection.hlsl"
ENDHLSL
}
}
}
94 changes: 19 additions & 75 deletions Assets/Pix2Pix/PostProcessing/Resources/TemporalReprojection.hlsl
Original file line number Diff line number Diff line change
@@ -1,104 +1,48 @@
#include "PostProcessing/Shaders/StdLib.hlsl"
#include "PostProcessing/Shaders/Colors.hlsl"

#define SAMPLE_TEX2D(name, uv) SAMPLE_TEXTURE2D(name, sampler##name, uv)
#define SAMPLE_DEPTH(name, uv) SAMPLE_DEPTH_TEXTURE(name, sampler##name, uv).x

TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
TEXTURE2D_SAMPLER2D(_RemapTex, sampler_RemapTex);
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
TEXTURE2D_SAMPLER2D(_CameraMotionVectorsTexture, sampler_CameraMotionVectorsTexture);
TEXTURE2D_SAMPLER2D(_UVRemap, sampler_UVRemap);
TEXTURE2D_SAMPLER2D(_PrevUVRemap, sampler_PrevUVRemap);
TEXTURE2D_SAMPLER2D(_PrevMoDepth, sampler_PrevMoDepth);

float4 _CameraDepthTexture_TexelSize;

float _DepthWeight;
float _MotionWeight;
float2 _DeltaTime;

struct FragmentOutput
{
half4 uvRemap : SV_Target0;
half4 moDepth : SV_Target1;
half4 color : SV_Target0;
half2 remap : SV_Target1;
};

// Converts a raw depth value to a linear 0-1 depth value.
float LinearizeDepth(float z)
{
// A little bit complicated to take the current projection mode
// (perspective/orthographic) into account.
float isOrtho = unity_OrthoParams.w;
float isPers = 1.0 - unity_OrthoParams.w;
z *= _ZBufferParams.x;
return (1.0 - isOrtho * z) / (isPers * z + _ZBufferParams.y);
}

float3 CompareDepth(float3 min_uvd, float2 uv)
bool DepthMask(float depth)
{
float d = SAMPLE_DEPTH(_CameraDepthTexture, uv);
return d < min_uvd ? float3(uv, d) : min_uvd;
}

float2 SearchClosest(float2 uv)
{
float4 duv = _CameraDepthTexture_TexelSize.xyxy * float4(1, 1, -1, 0);

float3 min_uvd = float3(uv, SAMPLE_DEPTH(_CameraDepthTexture, uv));

min_uvd = CompareDepth(min_uvd, uv - duv.xy);
min_uvd = CompareDepth(min_uvd, uv - duv.wy);
min_uvd = CompareDepth(min_uvd, uv - duv.zy);

min_uvd = CompareDepth(min_uvd, uv + duv.zw);
min_uvd = CompareDepth(min_uvd, uv + duv.xw);

min_uvd = CompareDepth(min_uvd, uv + duv.zy);
min_uvd = CompareDepth(min_uvd, uv + duv.wy);
min_uvd = CompareDepth(min_uvd, uv + duv.xy);

return min_uvd.xy;
const float epsilon = 1e-5;
#if defined(UNITY_REVERSED_Z)
return depth > epsilon;
#else
return depth < 1 - epsilon;
#endif
}

FragmentOutput FragInitialize(VaryingsDefault i)
{
half2 m = SAMPLE_TEX2D(_CameraMotionVectorsTexture, i.texcoord).xy;
half d = LinearizeDepth(SAMPLE_DEPTH(_CameraDepthTexture, i.texcoord));

FragmentOutput o;
o.uvRemap = half4(i.texcoord, d < 0.9, 1);
o.moDepth = half4(m, d, 0);
o.color = SAMPLE_TEX2D(_MainTex, i.texcoord);
o.remap = i.texcoord;
return o;
}

FragmentOutput FragUpdate(VaryingsDefault i)
{
float2 uv1 = i.texcoord;
half2 m1 = SAMPLE_TEX2D(_CameraMotionVectorsTexture, uv1).xy;
half d1 = LinearizeDepth(SAMPLE_DEPTH(_CameraDepthTexture, uv1));

float2 uv0 = uv1 - m1;
half3 md0 = SAMPLE_TEX2D(_PrevMoDepth, uv0).xyz;
half4 c0 = SAMPLE_TEX2D(_PrevUVRemap, uv0);
half2 mv = SAMPLE_TEX2D(_CameraMotionVectorsTexture, i.texcoord).xy;
half2 remap = SAMPLE_TEX2D(_RemapTex, i.texcoord - mv).xy;

// Disocclusion test
float docc = abs(1 - d1 / md0.z) * 20;//_DepthWeight;

// Out of screen test
float oscr = any(uv0 < 0) + any(uv0 > 1);

float alpha = 1 - saturate(docc + oscr);
half4 c1 = SAMPLE_TEX2D(_MainTex, i.texcoord);
half4 c2 = SAMPLE_TEX2D(_MainTex, remap);
bool mask = DepthMask(SAMPLE_DEPTH(_CameraDepthTexture, i.texcoord));

FragmentOutput o;
o.uvRemap = half4(c0.xy, d1 < 0.9, min(c0.a, alpha));
o.moDepth = half4(m1, d1, 0);
o.color = lerp(c1, c2, mask);
o.remap = remap;
return o;
}

half4 FragComposite(VaryingsDefault i) : SV_Target
{
half4 remap = SAMPLE_TEX2D(_UVRemap, i.texcoord);
half4 c = SAMPLE_TEX2D(_MainTex, remap.xy);
c = lerp(SAMPLE_TEX2D(_MainTex,i.texcoord), c, remap.z);
return c;
}

0 comments on commit 9f41068

Please sign in to comment.