浏览代码

[ScreenSpaceTracing] (wip) step by step debugging

/feature-ScreenSpaceProjection
Frédéric Vauchelles 6 年前
当前提交
17eb7e3c
共有 11 个文件被更改,包括 162 次插入36 次删除
  1. 4
      SampleScenes/HDTest/PlanarReflectionTests.unity
  2. 45
      ScriptableRenderPipeline/Core/CoreRP/MousePositionDebug.cs
  3. 77
      ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/ScreenSpaceRaymarching.hlsl
  4. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.hlsl
  5. 3
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/LightingDebug.cs
  6. 1
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/LightingDebug.cs.hlsl
  7. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs
  8. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs
  9. 9
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDUtils.cs
  10. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoop.cs
  11. 51
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl

4
SampleScenes/HDTest/PlanarReflectionTests.unity


serializedVersion: 2
m_Bits: 4294967295
m_Lightmapping: 1
m_AreaSize: {x: 18, y: 0.5}
m_AreaSize: {x: 6, y: 2}
m_BounceIntensity: 0.1
m_ColorTemperature: 6570
m_UseColorTemperature: 0

serializedVersion: 2
m_Bits: 4294967295
m_Lightmapping: 1
m_AreaSize: {x: 18, y: 0.5}
m_AreaSize: {x: 6, y: 2}
m_BounceIntensity: 0.1
m_ColorTemperature: 6570
m_UseColorTemperature: 0

45
ScriptableRenderPipeline/Core/CoreRP/MousePositionDebug.cs


}
}
public int debugStep
{
get
{
#if UNITY_EDITOR
return m_DebugStep;
#else
return 0;
#endif
}
}
Vector2 m_MouseClickPosition = Vector2.zero;
int m_DebugStep = 0;
switch (Event.current.type)
{
case EventType.MouseDown:
m_MouseClickPosition = m_mousePosition;
break;
case EventType.KeyDown:
switch (Event.current.keyCode)
{
case KeyCode.PageUp:
++m_DebugStep;
Debug.LogFormat("DebugStep: {0}", m_DebugStep);
break;
case KeyCode.PageDown:
m_DebugStep = Mathf.Max(0, m_DebugStep - 1);
Debug.LogFormat("DebugStep: {0}", m_DebugStep);
break;
}
break;
}
}
#endif

}
#endif
return mousePixelCoord;
}
public Vector2 GetMouseClickPosition(float ScreenHeight)
{
#if UNITY_EDITOR
Vector2 mousePixelCoord = m_MouseClickPosition;
if (!UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
mousePixelCoord.y = (ScreenHeight - 1.0f) - mousePixelCoord.y;
return mousePixelCoord;
#else
return Vector2.zero;
#endif
}
}
}

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


#ifndef UNITY_SCREEN_SPACE_RAYMARCHING_INCLUDED
#define UNITY_SCREEN_SPACE_RAYMARCHING_INCLUDED
struct ScreenSpaceRaymarchInput
{
float3 startPositionVS;
float startLinearDepth;
float3 dirVS;
float4x4 projectionMatrix;
int2 bufferSize;
int minLevel;
int maxLevel;
#ifdef DEBUG_DISPLAY
uint2 initialStartPositionSS;
float initialStartLinearDepth;
#endif
};
struct ScreenSpaceRayHit
{
float distance;

};
bool ScreenSpaceRaymarch(
float3 startPositionVS,
float startLinearDepth,
float3 dirVS,
float4x4 projectionMatrix,
int2 bufferSize,
int minLevel,
int maxLevel,
ScreenSpaceRaymarchInput input,
out ScreenSpaceRayHit hit)
{
// dirVS must be normalized

float4 startPositionCS = mul(projectionMatrix, float4(startPositionVS, 1));
float4 dirCS = mul(projectionMatrix, float4(dirVS, 1));
float4 startPositionCS = mul(input.projectionMatrix, float4(input.startPositionVS, 1));
float4 dirCS = mul(input.projectionMatrix, float4(input.dirVS, 1));
#if UNITY_UV_STARTS_AT_TOP
// Our clip space is correct, but the NDC is flipped.
// Conceptually, it should be (positionNDC.y = 1.0 - positionNDC.y), but this is more efficient.

float2 invDirNDC = 1 / dirNDC.xy;
int2 cellPlanes = clamp(sign(dirNDC.xy), 0, 1);
int2 startPositionTXS = int2(startPositionNDC * bufferSize);
int2 startPositionTXS = int2(startPositionNDC * input.bufferSize);
int currentLevel = minLevel;
int2 cellCount = bufferSize >> currentLevel;
int2 cellSize = int2(1 / float2(cellCount));
int currentLevel = input.minLevel;
int2 cellCount = input.bufferSize >> currentLevel;
uint2 cellSize = uint2(1, 1) << currentLevel;
float3 positionTXS = float3(float2(startPositionTXS), startLinearDepth);
float3 positionTXS = float3(float2(startPositionTXS), input.startLinearDepth);
int iteration = 0;
bool hitSuccessful = true;

uint2 debugCellSize = cellSize;
while (currentLevel >= minLevel)
while (currentLevel >= input.minLevel)
if (++iteration < MAX_ITERATIONS)
if (iteration < MAX_ITERATIONS)
#ifdef DEBUG_DISPLAY
if (_DebugStep == iteration)
{
debugCellSize = cellSize;
}
#endif
// 1. Calculate hit in this HiZ cell
int2 cellId = int2(positionTXS.xy) / cellCount;

// https://gamedev.autodesk.com/blogs/1/post/5866685274515295601
testHitPositionTXS.xy += (planeHits.x < planeHits.y) ? float2(CROSS_OFFSET.x, 0) : float2(0, CROSS_OFFSET.y);
if (any(testHitPositionTXS.xy > bufferSize)
if (any(testHitPositionTXS.xy > input.bufferSize)
|| any(testHitPositionTXS.xy < 0))
{
hitSuccessful = false;

if (hiZLinearDepth > testHitPositionTXS.z)
{
currentLevel = min(maxLevel, currentLevel + 1);
currentLevel = min(input.maxLevel, currentLevel + 1);
#ifdef DEBUG_DISPLAY
maxUsedLevel = max(maxUsedLevel, currentLevel);
#endif

--currentLevel;
}
cellCount = bufferSize >> currentLevel;
cellSize = int2(1 / float2(cellCount));
cellCount = input.bufferSize >> currentLevel;
cellSize = uint2(1, 1) << currentLevel;
++iteration;
hit.positionSS = float2(positionTXS.xy) / float2(bufferSize);
hit.positionSS = float2(positionTXS.xy) / float2(input.bufferSize);
#ifdef DEBUG_DISPLAY
if (_DebugLightingMode == DEBUGLIGHTINGMODE_SCREEN_SPACE_TRACING_REFRACTION)

hit.debugOutput = float3(startPositionNDC.xy, 0);
break;
case DEBUGSCREENSPACETRACING_DIR_VS:
hit.debugOutput = dirVS * 0.5 + 0.5;
hit.debugOutput = input.dirVS * 0.5 + 0.5;
break;
case DEBUGSCREENSPACETRACING_DIR_NDC:
hit.debugOutput = float3(dirNDC.xy * 0.5 + 0.5, dirNDC.z);

hit.debugOutput = float(iteration) / float(MAX_ITERATIONS);
break;
case DEBUGSCREENSPACETRACING_MAX_USED_LEVEL:
hit.debugOutput = float(maxUsedLevel) / float(maxLevel);
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);
float3 gridColor = float3(
frac(input.initialStartLinearDepth * 0.1).xx,
cellSDF);
hit.debugOutput = gridColor;
}
}
}
#endif

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


int _DebugLightingSubMode; // Match an enum depending on DebugLightingMode
int _DebugViewMaterial; // Contain the id (define in various materialXXX.cs.hlsl) of the property to display
int _DebugMipMapMode; // Match enum DebugMipMapMode
int _DebugStep;
float4 _MouseClickPixelCoord; // xy unorm, zw norm
float _DebugEnvironmentProxyDepthScale;
CBUFFER_END

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


HitDepth,
HitSuccess,
IterationCount,
MaxUsedLevel
MaxUsedLevel,
StepByStep
}
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

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs


cmd.SetGlobalVector(HDShaderIDs._DebugLightingNormal, debugNormal);
cmd.SetGlobalVector(HDShaderIDs._MousePixelCoord, HDUtils.GetMouseCoordinates(hdCamera));
cmd.SetGlobalInt(HDShaderIDs._DebugStep, HDUtils.debugStep);
cmd.SetGlobalVector(HDShaderIDs._MouseClickPixelCoord, HDUtils.GetMouseClickCoordinates(hdCamera));
cmd.SetGlobalTexture(HDShaderIDs._DebugFont, m_Asset.renderPipelineResources.debugFontTexture);
}

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs


public static readonly int _ViewTilesFlags = Shader.PropertyToID("_ViewTilesFlags");
public static readonly int _MousePixelCoord = Shader.PropertyToID("_MousePixelCoord");
public static readonly int _MouseClickPixelCoord = Shader.PropertyToID("_MouseClickPixelCoord");
public static readonly int _DebugStep = Shader.PropertyToID("_DebugStep");
public static readonly int _DebugEnvironmentProxyDepthScale = Shader.PropertyToID("_DebugEnvironmentProxyDepthScale");
public static readonly int _DebugViewMaterial = Shader.PropertyToID("_DebugViewMaterial");

9
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDUtils.cs


}
}
public static int debugStep { get { return MousePositionDebug.instance.debugStep; } }
static MaterialPropertyBlock s_PropertyBlock = new MaterialPropertyBlock();
public static List<RenderPipelineMaterial> GetRenderPipelineMaterialList()

public static Vector4 GetMouseCoordinates(HDCamera camera)
{
Vector2 mousePixelCoord = MousePositionDebug.instance.GetMousePosition(camera.screenSize.y);
return new Vector4(mousePixelCoord.x, mousePixelCoord.y, camera.scaleBias.x * mousePixelCoord.x / camera.screenSize.x, camera.scaleBias.y * mousePixelCoord.y / camera.screenSize.y);
}
// Returns mouse click coordinates: (x,y) in pixels and (z,w) normalized inside the render target (not the viewport)
public static Vector4 GetMouseClickCoordinates(HDCamera camera)
{
Vector2 mousePixelCoord = MousePositionDebug.instance.GetMouseClickPosition(camera.screenSize.y);
return new Vector4(mousePixelCoord.x, mousePixelCoord.y, camera.scaleBias.x * mousePixelCoord.x / camera.screenSize.x, camera.scaleBias.y * mousePixelCoord.y / camera.screenSize.y);
}
}

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/LightLoop/LightLoop.cs


m_DebugViewTilesMaterial.SetInt(HDShaderIDs._NumTiles, numTiles);
m_DebugViewTilesMaterial.SetInt(HDShaderIDs._ViewTilesFlags, (int)lightingDebug.tileClusterDebugByCategory);
m_DebugViewTilesMaterial.SetVector(HDShaderIDs._MousePixelCoord, HDUtils.GetMouseCoordinates(hdCamera));
m_DebugViewTilesMaterial.SetVector(HDShaderIDs._MouseClickPixelCoord, HDUtils.GetMouseClickCoordinates(hdCamera));
m_DebugViewTilesMaterial.SetBuffer(HDShaderIDs.g_TileList, s_TileList);
m_DebugViewTilesMaterial.SetBuffer(HDShaderIDs.g_DispatchIndirectBuffer, s_DispatchIndirectBuffer);
m_DebugViewTilesMaterial.EnableKeyword("USE_FPTL_LIGHTLIST");

// lightCategories
m_DebugViewTilesMaterial.SetInt(HDShaderIDs._ViewTilesFlags, (int)lightingDebug.tileClusterDebugByCategory);
m_DebugViewTilesMaterial.SetVector(HDShaderIDs._MousePixelCoord, HDUtils.GetMouseCoordinates(hdCamera));
m_DebugViewTilesMaterial.SetVector(HDShaderIDs._MouseClickPixelCoord, HDUtils.GetMouseClickCoordinates(hdCamera));
m_DebugViewTilesMaterial.SetBuffer(HDShaderIDs.g_vLightListGlobal, bUseClustered ? s_PerVoxelLightLists : s_LightList);
m_DebugViewTilesMaterial.EnableKeyword(bUseClustered ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");
m_DebugViewTilesMaterial.DisableKeyword(!bUseClustered ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST");

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


ZERO_INITIALIZE(IndirectLighting, lighting);
#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

uint2 depthSize = uint2(_PyramidDepthMipSize.xy);
ScreenSpaceRaymarchInput ssInput;
ZERO_INITIALIZE(ScreenSpaceRaymarchInput, ssInput);
ssInput.startPositionVS = positionVS;
ssInput.startLinearDepth = posInput.linearDepth;
ssInput.dirVS = transparentRefractVVS;
ssInput.projectionMatrix = UNITY_MATRIX_P;
ssInput.bufferSize = depthSize;
ssInput.minLevel = 2;
ssInput.maxLevel = int(_PyramidDepthMipSize.z);
bool hitSuccessful = ScreenSpaceRaymarch(
positionVS,
posInput.linearDepth,
transparentRefractVVS,
UNITY_MATRIX_P,
depthSize,
0,
int(_PyramidDepthMipSize.z),
hit
);
#ifdef DEBUG_DISPLAY
ssInput.initialStartPositionSS = initialPositionInputs.positionSS;
#endif
bool hitSuccessful = ScreenSpaceRaymarch(ssInput, hit);
#ifdef DEBUG_DISPLAY
if (_DebugLightingMode == DEBUGLIGHTINGMODE_SCREEN_SPACE_TRACING_REFRACTION)

正在加载...
取消
保存