浏览代码

Reproject SSR with motion vectors

Added HiZ cell mip diff debug
/main
Frédéric Vauchelles 7 年前
当前提交
27865707
共有 8 个文件被更改,包括 91 次插入28 次删除
  1. 3
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Camera/HDCameraFrameHistoryType.cs
  2. 1
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.cs
  3. 1
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/LightingDebug.cs
  4. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/LightingDebug.cs.hlsl
  5. 75
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs
  6. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs
  7. 15
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl
  8. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderVariables.hlsl

3
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Camera/HDCameraFrameHistoryType.cs


public enum HDCameraFrameHistoryType
{
DepthPyramid,
ColorPyramid
ColorPyramid,
MotionVectors
}
}

1
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.cs


new DebugUI.Value { displayName = "Position SS", getter = () => new Vector2(screenSpaceTracingDebugData.iterationPositionSS.x, screenSpaceTracingDebugData.iterationPositionSS.y) },
new DebugUI.Value { displayName = "Depth", getter = () => 1f / screenSpaceTracingDebugData.iterationPositionSS.z },
new DebugUI.Value { displayName = "Depth Buffer Min/Min + Thickness/Max", getter = () => string.Format("{0}/{1}/{2}", screenSpaceTracingDebugData.iterationLinearDepthBufferMin, screenSpaceTracingDebugData.iterationLinearDepthBufferMinThickness, screenSpaceTracingDebugData.iterationLinearDepthBufferMax) },
new DebugUI.Value { displayName = "Depth Buffer Diff (Max - Min)", getter = () => (screenSpaceTracingDebugData.iterationLinearDepthBufferMax - screenSpaceTracingDebugData.iterationLinearDepthBufferMin).ToString("F6") },
new DebugUI.Value { displayName = "Intersect Depth Buffer", getter = () => screenSpaceTracingDebugData.intersectDepthBuffer },
new DebugUI.Value { displayName = "Mip Level", getter = () => screenSpaceTracingDebugData.iterationMipLevel },
new DebugUI.Value { displayName = "Cell Id", getter = () => screenSpaceTracingDebugData.iterationCellId },

1
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/LightingDebug.cs


HiZIntersectionKind,
HiZHitWeight,
HiZSampledColor,
HiZDiff,
LinearPositionNDC,
LinearRayDirNDC,
LinearIterationCount,

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/LightingDebug.cs.hlsl


#define DEBUGSCREENSPACETRACING_HI_ZINTERSECTION_KIND (10)
#define DEBUGSCREENSPACETRACING_HI_ZHIT_WEIGHT (11)
#define DEBUGSCREENSPACETRACING_HI_ZSAMPLED_COLOR (12)
#define DEBUGSCREENSPACETRACING_LINEAR_POSITION_NDC (13)
#define DEBUGSCREENSPACETRACING_LINEAR_RAY_DIR_NDC (14)
#define DEBUGSCREENSPACETRACING_LINEAR_ITERATION_COUNT (15)
#define DEBUGSCREENSPACETRACING_LINEAR_HIT_WEIGHT (16)
#define DEBUGSCREENSPACETRACING_LINEAR_SAMPLED_COLOR (17)
#define DEBUGSCREENSPACETRACING_HI_ZDIFF (13)
#define DEBUGSCREENSPACETRACING_LINEAR_POSITION_NDC (14)
#define DEBUGSCREENSPACETRACING_LINEAR_RAY_DIR_NDC (15)
#define DEBUGSCREENSPACETRACING_LINEAR_ITERATION_COUNT (16)
#define DEBUGSCREENSPACETRACING_LINEAR_HIT_WEIGHT (17)
#define DEBUGSCREENSPACETRACING_LINEAR_SAMPLED_COLOR (18)
#endif

75
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs


RTHandleSystem.RTHandle m_CameraDepthBufferCopy;
RTHandleSystem.RTHandle m_CameraStencilBufferCopy;
RTHandleSystem.RTHandle m_VelocityBuffer;
RTHandleSystem.RTHandle m_DeferredShadowBuffer;
RTHandleSystem.RTHandle m_AmbientOcclusionBuffer;
RTHandleSystem.RTHandle m_DistortionBuffer;

m_AmbientOcclusionBuffer = RTHandles.Alloc(Vector2.one, filterMode: FilterMode.Bilinear, colorFormat: RenderTextureFormat.R8, sRGB: false, enableRandomWrite: true, name: "AmbientOcclusion");
}
if (m_Asset.renderPipelineSettings.supportMotionVectors)
{
m_VelocityBuffer = RTHandles.Alloc(Vector2.one, filterMode: FilterMode.Point, colorFormat: Builtin.GetVelocityBufferFormat(), sRGB: Builtin.GetVelocityBufferSRGBFlag(), enableMSAA: true, name: "Velocity");
}
m_DistortionBuffer = RTHandles.Alloc(Vector2.one, filterMode: FilterMode.Point, colorFormat: Builtin.GetDistortionBufferFormat(), sRGB: Builtin.GetDistortionBufferSRGBFlag(), name: "Distortion");
// TODO: For MSAA, we'll need to add a Draw path in order to support MSAA properly

RTHandles.Release(m_CameraStencilBufferCopy);
RTHandles.Release(m_AmbientOcclusionBuffer);
RTHandles.Release(m_VelocityBuffer);
RTHandles.Release(m_DistortionBuffer);
RTHandles.Release(m_DeferredShadowBuffer);

cmd.SetGlobalTexture(HDShaderIDs._ColorPyramidTexture, Texture2D.blackTexture);
cmd.SetGlobalVector(HDShaderIDs._ColorPyramidSize, Vector4.one);
cmd.SetGlobalVector(HDShaderIDs._ColorPyramidScale, Vector4.one);
}
}
var previousMotionVectorsRT = hdCamera.GetPreviousFrameRT((int)HDCameraFrameHistoryType.MotionVectors);
if (previousMotionVectorsRT != null)
{
cmd.SetGlobalTexture(HDShaderIDs._CameraMotionVectorsTexture, previousMotionVectorsRT);
cmd.SetGlobalVector(HDShaderIDs._CameraMotionVectorsSize, new Vector4(
previousMotionVectorsRT.referenceSize.x,
previousMotionVectorsRT.referenceSize.y,
1f / previousMotionVectorsRT.referenceSize.x,
1f / previousMotionVectorsRT.referenceSize.y
));
cmd.SetGlobalVector(HDShaderIDs._CameraMotionVectorsScale, new Vector4(
previousMotionVectorsRT.referenceSize.x / (float)previousMotionVectorsRT.rt.width,
previousMotionVectorsRT.referenceSize.y / (float)previousMotionVectorsRT.rt.height,
1, 0.0f
));
}
else
{
cmd.SetGlobalTexture(HDShaderIDs._CameraMotionVectorsTexture, Texture2D.blackTexture);
cmd.SetGlobalVector(HDShaderIDs._CameraMotionVectorsSize, Vector4.one);
cmd.SetGlobalVector(HDShaderIDs._CameraMotionVectorsScale, Vector4.one);
}
}
}

// If the flag hasn't been set yet on this camera, motion vectors will skip a frame.
hdcamera.camera.depthTextureMode |= DepthTextureMode.MotionVectors | DepthTextureMode.Depth;
HDUtils.SetRenderTarget(cmd, hdcamera, m_VelocityBuffer, m_CameraDepthStencilBuffer);
var velocityBufferRT = hdcamera.GetCurrentFrameRT((int)HDCameraFrameHistoryType.MotionVectors)
?? hdcamera.AllocHistoryFrameRT((int)HDCameraFrameHistoryType.MotionVectors, AllocMotionVectorRT);
HDUtils.SetRenderTarget(cmd, hdcamera, velocityBufferRT, m_CameraDepthStencilBuffer);
RenderOpaqueRenderList(cullResults, hdcamera.camera, renderContext, cmd, HDShaderPassNames.s_MotionVectorsName, RendererConfiguration.PerObjectMotionVectors);
}
}

// If the flag hasn't been set yet on this camera, motion vectors will skip a frame.
hdcamera.camera.depthTextureMode |= DepthTextureMode.MotionVectors | DepthTextureMode.Depth;
HDUtils.DrawFullScreen(cmd, hdcamera, m_CameraMotionVectorsMaterial, m_VelocityBuffer, m_CameraDepthStencilBuffer, null, 0);
PushFullScreenDebugTexture(cmd, m_VelocityBuffer, hdcamera, FullScreenDebugMode.MotionVectors);
var velocityBufferRT = hdcamera.GetCurrentFrameRT((int)HDCameraFrameHistoryType.MotionVectors)
?? hdcamera.AllocHistoryFrameRT((int)HDCameraFrameHistoryType.MotionVectors, AllocMotionVectorRT);
HDUtils.DrawFullScreen(cmd, hdcamera, m_CameraMotionVectorsMaterial, velocityBufferRT, m_CameraDepthStencilBuffer, null, 0);
PushFullScreenDebugTexture(cmd, velocityBufferRT, hdcamera, FullScreenDebugMode.MotionVectors);
var scale = new Vector2((float)hdcamera.actualWidth / velocityBufferRT.rt.width, (float)hdcamera.actualHeight / velocityBufferRT.rt.height);
cmd.SetGlobalTexture(HDShaderIDs._CameraMotionVectorsTexture, velocityBufferRT);
cmd.SetGlobalVector(HDShaderIDs._CameraMotionVectorsSize, new Vector4(
hdcamera.actualWidth, hdcamera.actualHeight,
1f / hdcamera.actualWidth, 1f / hdcamera.actualHeight));
cmd.SetGlobalVector(HDShaderIDs._CameraMotionVectorsScale, new Vector4(scale.x, scale.y, 1, 0.0f));
}
}

PushFullScreenDebugTextureMip(cmd, cameraRT, m_BufferPyramid.GetPyramidLodCount(new Vector2Int(hdCamera.actualWidth, hdCamera.actualHeight)), new Vector4(pyramidScale.x, pyramidScale.y, 0.0f, 0.0f), hdCamera, debugMode);
}
static readonly int _CameraMotionVectorsTexture_PostHack = Shader.PropertyToID("_CameraMotionVectorsTexture_PostHack");
void RenderPostProcess(HDCamera hdcamera, CommandBuffer cmd, PostProcessLayer layer)
{
using (new ProfilingSample(cmd, "Post-processing", CustomSamplerId.PostProcessing.GetSampler()))

// Since we don't render to the full render textures, we need to feed the post processing stack with the right scale/bias.
// This feature not being implemented yet, we'll just copy the relevant buffers into an appropriately sized RT.
cmd.ReleaseTemporaryRT(HDShaderIDs._CameraDepthTexture);
cmd.ReleaseTemporaryRT(HDShaderIDs._CameraMotionVectorsTexture);
cmd.ReleaseTemporaryRT(_CameraMotionVectorsTexture_PostHack);
if (m_VelocityBuffer != null)
var velocityBufferRT = hdcamera.GetCurrentFrameRT((int)HDCameraFrameHistoryType.MotionVectors);
if (velocityBufferRT != null)
cmd.GetTemporaryRT(HDShaderIDs._CameraMotionVectorsTexture, hdcamera.actualWidth, hdcamera.actualHeight, 0, FilterMode.Point, m_VelocityBuffer.rt.format);
HDUtils.BlitCameraTexture(cmd, hdcamera, m_VelocityBuffer, HDShaderIDs._CameraMotionVectorsTexture);
cmd.GetTemporaryRT(_CameraMotionVectorsTexture_PostHack, hdcamera.actualWidth, hdcamera.actualHeight, 0, FilterMode.Point, velocityBufferRT.rt.format);
HDUtils.BlitCameraTexture(cmd, hdcamera, velocityBufferRT, _CameraMotionVectorsTexture_PostHack);
cmd.SetGlobalTexture(HDShaderIDs._CameraMotionVectorsTexture, _CameraMotionVectorsTexture_PostHack);
}
cmd.GetTemporaryRT(HDShaderIDs._CameraColorTexture, hdcamera.actualWidth, hdcamera.actualHeight, 0, FilterMode.Point, m_CameraColorBuffer.rt.format);
HDUtils.BlitCameraTexture(cmd, hdcamera, m_CameraColorBuffer, HDShaderIDs._CameraColorTexture);

{
// Note: Here we don't use GetDepthTexture() to get the depth texture but m_CameraDepthStencilBuffer as the Forward transparent pass can
// write extra data to deal with DOF/MB
var velocityBufferRT = hdcamera.GetCurrentFrameRT((int)HDCameraFrameHistoryType.MotionVectors);
cmd.SetGlobalTexture(HDShaderIDs._CameraMotionVectorsTexture, m_VelocityBuffer);
cmd.SetGlobalTexture(HDShaderIDs._CameraMotionVectorsTexture, velocityBufferRT);
}
var context = hdcamera.postprocessRenderContext;

{
if (m_FrameSettings.enableStereo)
renderContext.StopMultiEye(cam);
}
RTHandleSystem.RTHandle AllocMotionVectorRT(string id, int frameIndex, RTHandleSystem rtHandleSystem)
{
return rtHandleSystem.Alloc(
Vector2.one,
filterMode: FilterMode.Point,
colorFormat: Builtin.GetVelocityBufferFormat(),
sRGB: Builtin.GetVelocityBufferSRGBFlag(),
enableMSAA: true,
name: string.Format("Velocity-{0}-{1}", id, frameIndex)
);
}
}
}

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs


public static readonly int _CameraDepthTexture = Shader.PropertyToID("_CameraDepthTexture");
public static readonly int _CameraMotionVectorsTexture = Shader.PropertyToID("_CameraMotionVectorsTexture");
public static readonly int _CameraMotionVectorsSize = Shader.PropertyToID("_CameraMotionVectorsSize");
public static readonly int _CameraMotionVectorsScale = Shader.PropertyToID("_CameraMotionVectorsScale");
public static readonly int _FullScreenDebugMode = Shader.PropertyToID("_FullScreenDebugMode");
public static readonly int _InputCubemap = Shader.PropertyToID("_InputCubemap");

15
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl


{
rayOriginWS = posInput.positionWS;
rayDirWS = preLightData.iblR;
mipLevel = PerceptualRoughnessToMipmapLevel(preLightData.iblPerceptualRoughness);
mipLevel = PositivePow(preLightData.iblPerceptualRoughness, 0.8) * uint(max(_ColorPyramidScale.z - 1, 0));
invScreenWeightDistance = _SSReflectionInvScreenWeightDistance;
#if DEBUG_DISPLAY
debugMode = DEBUGLIGHTINGMODE_SCREEN_SPACE_TRACING_REFLECTION;

UpdateLightingHierarchyWeights(hierarchyWeight, weight); // Shouldn't be needed, but safer in case we decide to change hierarchy priority
// Reproject color pyramid
float4 velocityBuffer = SAMPLE_TEXTURE2D_LOD(
_CameraMotionVectorsTexture,
s_linear_clamp_sampler,
hit.positionNDC * _CameraMotionVectorsScale.xy,
0.0
);
float2 velocityNDC;
DecodeVelocity(velocityBuffer, velocityNDC);
hit.positionNDC * _ColorPyramidScale.xy,
(hit.positionNDC - velocityNDC) * _ColorPyramidScale.xy,
mipLevel
).rgb;

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/ShaderVariables.hlsl


CBUFFER_START(UnityLightingParameters)
// Buffer pyramid
float4 _ColorPyramidSize; // (x,y) = Actual Pixel Size, (z,w) = 1 / Actual Pixel Size
float4 _DepthPyramidSize; // (x,y) = Actual Pixel Size, (z,w) = 1 / Actual Pixel Size
float4 _ColorPyramidScale; // (x,y) = Screen Scale, z = lod count, w = unused
float4 _DepthPyramidScale; // (x,y) = Screen Scale, z = lod count, w = unused
float4 _ColorPyramidSize; // (x,y) = Actual Pixel Size, (z,w) = 1 / Actual Pixel Size
float4 _DepthPyramidSize; // (x,y) = Actual Pixel Size, (z,w) = 1 / Actual Pixel Size
float4 _CameraMotionVectorsSize; // (x,y) = Actual Pixel Size, (z,w) = 1 / Actual Pixel Size
float4 _ColorPyramidScale; // (x,y) = Screen Scale, z = lod count, w = unused
float4 _DepthPyramidScale; // (x,y) = Screen Scale, z = lod count, w = unused
float4 _CameraMotionVectorsScale; // (x,y) = Screen Scale, z = lod count, w = unused
// Screen space lighting
float _SSRefractionInvScreenWeightDistance; // Distance for screen space smoothstep with fallback

TEXTURE2D(_DepthPyramidTexture);
// Ambient occlusion texture
TEXTURE2D(_AmbientOcclusionTexture);
TEXTURE2D(_CameraMotionVectorsTexture);
// Custom generated by HDRP, not from Unity Engine (passed in via HDCamera)
#if defined(USING_STEREO_MATRICES)

正在加载...
取消
保存