浏览代码

(wip) screen space tracing debug

/feature-ScreenSpaceProjection
Frédéric Vauchelles 6 年前
当前提交
210bb00c
共有 10 个文件被更改,包括 145 次插入59 次删除
  1. 43
      ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/ScreenSpaceRaymarching.hlsl
  2. 18
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.cs
  3. 71
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.cs.hlsl
  4. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.hlsl
  5. 25
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugFullScreen.shader
  6. 3
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/LightingDebug.cs
  7. 1
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/LightingDebug.cs.hlsl
  8. 13
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs
  9. 1
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs
  10. 27
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl

43
ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/ScreenSpaceRaymarching.hlsl


int maxLevel;
#ifdef DEBUG_DISPLAY
uint2 initialStartPositionSS;
float initialStartLinearDepth;
bool writeStepDebug;
#endif
};

float2 crossOffset = CROSS_OFFSET * cellPlanes;
cellPlanes = clamp(cellPlanes, 0, 1);
int2 startPositionTXS = int2(startPositionNDC * input.bufferSize);
uint2 startPositionTXS = uint2(startPositionNDC * input.bufferSize);
ZERO_INITIALIZE(ScreenSpaceRayHit, hit);

#ifdef DEBUG_DISPLAY
int maxUsedLevel = currentLevel;
uint2 debugCellSize = uint2(10000, 10000);
float3 debugPositionTXS = float3(0, 0, 0);
ScreenSpaceTracingDebug debug;
ZERO_INITIALIZE(ScreenSpaceTracingDebug, debug);
debug.startPositionSSX = startPositionTXS.x;
debug.startPositionSSY = startPositionTXS.y;
debug.startLinearDepth = input.startLinearDepth;
debug.levelMax = input.maxLevel;
debug.iterationMax = MAX_ITERATIONS;
#endif
while (currentLevel >= input.minLevel)

#ifdef DEBUG_DISPLAY
if (_DebugStep == iteration)
{
debugCellSize = cellSize;
debugPositionTXS = positionTXS;
debug.cellSizeW = cellSize.x;
debug.cellSizeH = cellSize.y;
debug.positionTXS = positionTXS;
}
#endif

hit.positionSS = float2(positionTXS.xy) / float2(input.bufferSize);
#ifdef DEBUG_DISPLAY
debug.level = maxUsedLevel;
debug.iteration = iteration;
if (input.writeStepDebug)
_DebugScreenSpaceTracing[0] = debug;
if (_DebugLightingMode == DEBUGLIGHTINGMODE_SCREEN_SPACE_TRACING_REFRACTION)
{
switch (_DebugLightingSubMode)

case DEBUGSCREENSPACETRACING_MAX_USED_LEVEL:
hit.debugOutput = float(maxUsedLevel) / float(input.maxLevel);
break;
case DEBUGSCREENSPACETRACING_STEP_BY_STEP:
{
float2 distanceToCell = abs(float2(input.initialStartPositionSS % debugCellSize) - float2(debugCellSize) / float2(2, 2));
distanceToCell = clamp(1 - distanceToCell, 0, 1);
float cellSDF = max(distanceToCell.x, distanceToCell.y);
float distanceToPosition = length(input.initialStartPositionSS - debugPositionTXS.xy);
float positionSDF = clamp(3 - distanceToPosition, 0, 1);
float3 debugColor = float3(
0,
positionSDF,
cellSDF);
hit.debugOutput = debugColor * 0.5 + frac(input.initialStartLinearDepth * 0.1).xxx * 0.5;
break;
}
}
}
#endif

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


PreRefractionColorPyramid,
DepthPyramid,
FinalColorPyramid,
ScreenSpaceTracingRefraction,
MaxLightingFullScreenDebug,
// Rendering

MaxRenderingFullScreenDebug
}
[GenerateHLSL]
public struct ScreenSpaceTracingDebug
{
public uint startPositionSSX;
public uint startPositionSSY;
public uint cellSizeW;
public uint cellSizeH;
public Vector3 positionTXS;
public float startLinearDepth;
public uint level;
public uint levelMax;
public uint iteration;
public uint iterationMax;
}
public class DebugDisplaySettings

71
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.cs.hlsl


#define FULLSCREENDEBUGMODE_PRE_REFRACTION_COLOR_PYRAMID (4)
#define FULLSCREENDEBUGMODE_DEPTH_PYRAMID (5)
#define FULLSCREENDEBUGMODE_FINAL_COLOR_PYRAMID (6)
#define FULLSCREENDEBUGMODE_MAX_LIGHTING_FULL_SCREEN_DEBUG (7)
#define FULLSCREENDEBUGMODE_MIN_RENDERING_FULL_SCREEN_DEBUG (8)
#define FULLSCREENDEBUGMODE_MOTION_VECTORS (9)
#define FULLSCREENDEBUGMODE_NAN_TRACKER (10)
#define FULLSCREENDEBUGMODE_MAX_RENDERING_FULL_SCREEN_DEBUG (11)
#define FULLSCREENDEBUGMODE_SCREEN_SPACE_TRACING_REFRACTION (7)
#define FULLSCREENDEBUGMODE_MAX_LIGHTING_FULL_SCREEN_DEBUG (8)
#define FULLSCREENDEBUGMODE_MIN_RENDERING_FULL_SCREEN_DEBUG (9)
#define FULLSCREENDEBUGMODE_MOTION_VECTORS (10)
#define FULLSCREENDEBUGMODE_NAN_TRACKER (11)
#define FULLSCREENDEBUGMODE_MAX_RENDERING_FULL_SCREEN_DEBUG (12)
// Generated from UnityEngine.Experimental.Rendering.HDPipeline.ScreenSpaceTracingDebug
// PackingRules = Exact
struct ScreenSpaceTracingDebug
{
uint startPositionSSX;
uint startPositionSSY;
uint cellSizeW;
uint cellSizeH;
float3 positionTXS;
float startLinearDepth;
uint level;
uint levelMax;
uint iteration;
uint iterationMax;
};
//
// Accessors for UnityEngine.Experimental.Rendering.HDPipeline.ScreenSpaceTracingDebug
//
uint GetStartPositionSSX(ScreenSpaceTracingDebug value)
{
return value.startPositionSSX;
}
uint GetStartPositionSSY(ScreenSpaceTracingDebug value)
{
return value.startPositionSSY;
}
uint GetCellSizeW(ScreenSpaceTracingDebug value)
{
return value.cellSizeW;
}
uint GetCellSizeH(ScreenSpaceTracingDebug value)
{
return value.cellSizeH;
}
float3 GetPositionTXS(ScreenSpaceTracingDebug value)
{
return value.positionTXS;
}
float GetStartLinearDepth(ScreenSpaceTracingDebug value)
{
return value.startLinearDepth;
}
uint GetLevel(ScreenSpaceTracingDebug value)
{
return value.level;
}
uint GetLevelMax(ScreenSpaceTracingDebug value)
{
return value.levelMax;
}
uint GetIteration(ScreenSpaceTracingDebug value)
{
return value.iteration;
}
uint GetIterationMax(ScreenSpaceTracingDebug value)
{
return value.iterationMax;
}
#endif

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.hlsl


float _DebugEnvironmentProxyDepthScale;
CBUFFER_END
RWStructuredBuffer<ScreenSpaceTracingDebug> _DebugScreenSpaceTracing;
TEXTURE2D(_DebugFont); // Debug font to write string in shader
void GetPropertiesDataDebug(uint paramId, inout float3 result, inout bool needLinearToSRGB)

25
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugFullScreen.shader


TEXTURE2D(_DebugFullScreenTexture);
float _FullScreenDebugMode;
float _RequireToFlipInputTexture;
RWStructuredBuffer<ScreenSpaceTracingDebug> _DebugScreenSpaceTracing;
struct Attributes
{

PositionInputs posInput = GetPositionInput(input.positionCS.xy, _ScreenSize.zw, depth, UNITY_MATRIX_I_VP, UNITY_MATRIX_VP);
float linearDepth = frac(posInput.linearDepth * 0.1);
return float4(linearDepth.xxx, 1.0);
}
if (_FullScreenDebugMode == FULLSCREENDEBUGMODE_SCREEN_SPACE_TRACING_REFRACTION)
{
ScreenSpaceTracingDebug debug = _DebugScreenSpaceTracing[0];
uint2 positionTXS = uint2((input.positionCS.xy * 0.5 + 0.5) * _ScreenParams.zw);
uint2 startPositionSS = uint2(debug.startPositionSSX, debug.startPositionSSY);
uint2 cellSize = uint2(debug.cellSizeW, debug.cellSizeH);
float2 distanceToCell = abs(float2(positionTXS % cellSize) - float2(cellSize) / float2(2, 2));
distanceToCell = clamp(1 - distanceToCell, 0, 1);
float cellSDF = max(distanceToCell.x, distanceToCell.y);
float distanceToPosition = length(positionTXS - debug.positionTXS.xy);
float positionSDF = clamp(3 - distanceToPosition, 0, 1);
float3 debugColor = float3(
0,
positionSDF,
cellSDF
);
return float4(debugColor * 0.5 + frac(debug.startLinearDepth * 0.1).xxx * 0.5, 1);
}
return float4(0.0, 0.0, 0.0, 0.0);

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


HitDepth,
HitSuccess,
IterationCount,
MaxUsedLevel,
StepByStep
MaxUsedLevel
}
public enum ShadowMapDebugMode

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


#define DEBUGSCREENSPACETRACING_HIT_SUCCESS (6)
#define DEBUGSCREENSPACETRACING_ITERATION_COUNT (7)
#define DEBUGSCREENSPACETRACING_MAX_USED_LEVEL (8)
#define DEBUGSCREENSPACETRACING_STEP_BY_STEP (9)
#endif

13
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs


FrameSettings m_FrameSettings; // Init every frame
static ComputeBuffer s_DebugScreenSpaceTracing = null;
static ScreenSpaceTracingDebug[] s_DebugScreenSpaceTracingArray = new ScreenSpaceTracingDebug[1];
public HDRenderPipeline(HDRenderPipelineAsset asset)
{
SetRenderingFeatures();

// We don't need the debug of Default camera at runtime (each camera have its own debug settings)
FrameSettings.RegisterDebug("Default Camera", m_Asset.GetFrameSettings());
#endif
// struct ScreenSpaceTracingDebug, defined in ScreenSpaceRaymarching.hlsl
s_DebugScreenSpaceTracing = new ComputeBuffer(1, System.Runtime.InteropServices.Marshal.SizeOf(typeof(ScreenSpaceTracingDebug)));
InitializeRenderTextures();

foreach (var material in m_MaterialList)
material.RenderInit(cmd);
s_DebugScreenSpaceTracingArray[0] = new ScreenSpaceTracingDebug();
s_DebugScreenSpaceTracing.SetData(s_DebugScreenSpaceTracingArray);
using (new ProfilingSample(cmd, "HDRenderPipeline::Render", CustomSamplerId.HDRenderPipelineRender.GetSampler()))
{
// Do anything we need to do upon a new frame.

RenderDistortion(cmd, m_Asset.renderPipelineResources, hdCamera);
PushFullScreenDebugTexture(cmd, m_CameraColorBuffer, hdCamera, FullScreenDebugMode.NanTracker);
PushFullScreenDebugTexture(cmd, m_CameraColorBuffer, hdCamera, FullScreenDebugMode.ScreenSpaceTracingRefraction);
PushColorPickerDebugTexture(cmd, m_CameraColorBuffer, hdCamera);
// The final pass either postprocess of Blit will flip the screen (as it is reverse by default due to Unity openGL legacy)

cmd.SetGlobalInt(HDShaderIDs._DebugViewMaterial, (int)m_CurrentDebugDisplaySettings.GetDebugMaterialIndex());
cmd.SetGlobalInt(HDShaderIDs._DebugLightingMode, (int)m_CurrentDebugDisplaySettings.GetDebugLightingMode());
cmd.SetGlobalInt(HDShaderIDs._DebugLightingSubMode, m_CurrentDebugDisplaySettings.GetDebugLightingSubMode());
cmd.SetGlobalInt(HDShaderIDs._DebugMipMapMode, (int)m_CurrentDebugDisplaySettings.GetDebugMipMapMode());
cmd.SetGlobalVector(HDShaderIDs._DebugLightingAlbedo, debugAlbedo);
cmd.SetGlobalVector(HDShaderIDs._DebugLightingSmoothness, debugSmoothness);

cmd.SetGlobalVector(HDShaderIDs._MouseClickPixelCoord, HDUtils.GetMouseClickCoordinates(hdCamera));
cmd.SetGlobalTexture(HDShaderIDs._DebugFont, m_Asset.renderPipelineResources.debugFontTexture);
cmd.SetGlobalBuffer(HDShaderIDs._DebugScreenSpaceTracing, s_DebugScreenSpaceTracing);
}
else
{

// Everything we have capture is flipped (as it happen before FinalPass/postprocess/Blit. So if we are not in SceneView
// (i.e. we have perform a flip, we need to flip the input texture)
m_DebugFullScreen.SetFloat(HDShaderIDs._RequireToFlipInputTexture, hdCamera.camera.cameraType != CameraType.SceneView ? 1.0f : 0.0f);
m_DebugFullScreen.SetBuffer(HDShaderIDs._DebugScreenSpaceTracing, s_DebugScreenSpaceTracing);
HDUtils.DrawFullScreen(cmd, hdCamera, m_DebugFullScreen, (RenderTargetIdentifier)BuiltinRenderTextureType.CameraTarget);
PushColorPickerDebugTexture(cmd, (RenderTargetIdentifier)BuiltinRenderTextureType.CameraTarget, hdCamera);

1
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs


public static readonly int _DebugLightingNormal = Shader.PropertyToID("_DebugLightingNormal");
public static readonly int _AmbientOcclusionTexture = Shader.PropertyToID("_AmbientOcclusionTexture");
public static readonly int _DebugMipMapMode = Shader.PropertyToID("_DebugMipMapMode");
public static readonly int _DebugScreenSpaceTracing = Shader.PropertyToID("_DebugScreenSpaceTracing");
public static readonly int _UseTileLightList = Shader.PropertyToID("_UseTileLightList");
public static readonly int _Time = Shader.PropertyToID("_Time");

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


#if HAS_REFRACTION
#ifdef DEBUG_DISPLAY
PositionInputs initialPositionInputs = posInput;
float3 initialV = V;
BSDFData initialBSDFData = bsdfData;
PreLightData initialPreLightData = preLightData;
if (_DebugLightingMode == DEBUGLIGHTINGMODE_SCREEN_SPACE_TRACING_REFRACTION
&& _DebugLightingSubMode == DEBUGSCREENSPACETRACING_STEP_BY_STEP)
{
// Use input from clicked point
float depth = LOAD_TEXTURE2D(_MainDepthTexture, _MouseClickPixelCoord.xy).x;
posInput = GetPositionInput(_MouseClickPixelCoord.xy, _ScreenSize.zw, depth, UNITY_MATRIX_I_VP, UNITY_MATRIX_VP, uint2(_MouseClickPixelCoord.xy) / GetTileSize());
V = GetWorldSpaceNormalizeViewDir(posInput.positionWS);
BSDFData bsdfData;
BakeLightingData bakeLightingData;
DecodeFromGBuffer(posInput.positionSS, UINT_MAX, bsdfData, bakeLightingData.bakeDiffuseLighting);
#ifdef SHADOWS_SHADOWMASK
DecodeShadowMask(LOAD_TEXTURE2D(_ShadowMaskTexture, posInput.positionSS), bakeLightingData.bakeShadowMask);
#endif
preLightData = GetPreLightData(V, posInput, bsdfData);
}
#endif
// Refraction process:
// 1. Depending on the shape model, we calculate the refracted point in world space and the optical depth
// 2. We calculate the screen space position of the refracted point

ssInput.maxLevel = int(_PyramidDepthMipSize.z);
#ifdef DEBUG_DISPLAY
ssInput.initialStartPositionSS = initialPositionInputs.positionSS;
ssInput.initialStartLinearDepth = initialPositionInputs.linearDepth;
ssInput.writeStepDebug = (uint2(_MouseClickPixelCoord.xy) == posInput.positionSS);
#endif
bool hitSuccessful = ScreenSpaceRaymarch(ssInput, hit);

正在加载...
取消
保存