浏览代码

Fixed HiZ raymarching

/feature-ScreenSpaceProjection
Frédéric Vauchelles 7 年前
当前提交
a30e7108
共有 6 个文件被更改,包括 103 次插入89 次删除
  1. 10
      ScriptableRenderPipeline/Core/CoreRP/MousePositionDebug.cs
  2. 27
      ScriptableRenderPipeline/Core/CoreRP/ShaderLibrary/ScreenSpaceRaymarching.hlsl
  3. 75
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.cs
  4. 22
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugDisplay.cs.hlsl
  5. 48
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugFullScreen.shader
  6. 10
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs

10
ScriptableRenderPipeline/Core/CoreRP/MousePositionDebug.cs


{
Vector2 mousePixelCoord = Input.mousePosition;
#if UNITY_EDITOR
if (!UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
{
mousePixelCoord = m_mousePosition;
mousePixelCoord.y = (ScreenHeight - 1.0f) - mousePixelCoord.y;
}
mousePixelCoord = m_mousePosition;
mousePixelCoord.y = (ScreenHeight - 1.0f) - mousePixelCoord.y;
#endif
return mousePixelCoord;
}

#if UNITY_EDITOR
Vector2 mousePixelCoord = m_MouseClickPosition;
if (!UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode)
mousePixelCoord.y = (ScreenHeight - 1.0f) - mousePixelCoord.y;
mousePixelCoord.y = (ScreenHeight - 1.0f) - mousePixelCoord.y;
return mousePixelCoord;
#else
return Vector2.zero;

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


void FillScreenSpaceRaymarchingPostLoopDebug(
int maxUsedLevel,
int iteration,
float3 rayTXS,
ScreenSpaceRayHit hit,
inout ScreenSpaceTracingDebug debug)
{

debug.rayTXS = rayTXS;
}
void FillScreenSpaceRaymarchingPreIterationDebug(

FillScreenSpaceRaymarchingPreLoopDebug(startPositionTXS, debug);
#endif
// No need to raymarch if the ray is along camera's foward
if (!any(rayTXS.xy))
{
hit.distance = 1 / startPositionTXS.z;
hit.linearDepth = 1 / startPositionTXS.z;
hit.positionSS = uint2(startPositionTXS.xy);
}
else
{
// Initialize raymarching
float2 invRayTXS = float2(1, 1) / rayTXS.xy;

float3 positionTXS = startPositionTXS;
// Goes to the intersection with the first cell
{
float2 absInvRayTXS = abs(invRayTXS);
positionTXS += rayTXS * min(absInvRayTXS.x, absInvRayTXS.y);
}
while (currentLevel >= minLevel)
{
if (iteration >= MAX_ITERATIONS)

FillScreenSpaceRaymarchingPostLoopDebug(
maxUsedLevel,
iteration,
rayTXS,
hit,
debug);
FillScreenSpaceRaymarchingHitDebug(

}
else
{
if (abs(rayTXS.x) < abs(rayTXS.y))
// rayTXS.y is not null here
rayTXS /= abs(rayTXS.y);
else
// rayTXS.x is not null here
rayTXS /= abs(rayTXS.x);
// DDA step
rayTXS /= max(abs(rayTXS.x), abs(rayTXS.y));
// TODO: We should have a for loop from the starting point to the far/near plane
while (iteration < MAX_ITERATIONS)
{
#ifdef DEBUG_DISPLAY

FillScreenSpaceRaymarchingPostLoopDebug(
0,
iteration,
rayTXS,
hit,
debug);
FillScreenSpaceRaymarchingHitDebug(

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


public float hitLinearDepth;
public Vector2 hitPositionSS;
public float iterationDistance;
public float unused01;
public float unused02;
public Vector3 rayTXS;
public float iterationDistance;
public Vector3 unused00;
}
public class DebugDisplaySettings

public static string k_PanelLighting = "Lighting";
public static string k_PanelRendering = "Rendering";
public static string k_PanelStatistics = "Statistics";
DebugUI.Widget[] m_DebugStatisticsItems;
public float debugOverlayRatio = 0.33f;
public FullScreenDebugMode fullScreenDebugMode = FullScreenDebugMode.None;

public static int[] renderingFullScreenDebugValues = null;
public static GUIContent[] debugScreenSpaceTracingStrings = null;
public static int[] debugScreenSpaceTracingValues = null;
public ScreenSpaceTracingDebug screenSpaceTracingDebugData { get; internal set; }
public DebugDisplaySettings()
{

var panel = DebugManager.instance.GetPanel(k_PanelDisplayStats, true);
panel.flags = DebugUI.Flags.RuntimeOnly;
panel.children.Add(m_DebugDisplayStatsItems);
}
bool IsScreenSpaceTracingIterationDebugEnabled()
{
return fullScreenDebugMode == FullScreenDebugMode.ScreenSpaceTracingRefraction;
}
void SetScreenSpaceTracingIterationDebugEnabled(bool value)
{
if (value)
{
lightingDebugSettings.debugLightingMode = DebugLightingMode.ScreenSpaceTracingRefraction;
fullScreenDebugMode = FullScreenDebugMode.ScreenSpaceTracingRefraction;
}
else
{
lightingDebugSettings.debugLightingMode = DebugLightingMode.None;
fullScreenDebugMode = FullScreenDebugMode.None;
}
}
void SetScreenSpaceTracingDebugMode(int value)
{
var val = (DebugScreenSpaceTracing)value;
if (val != DebugScreenSpaceTracing.None)
{
lightingDebugSettings.debugLightingMode = DebugLightingMode.ScreenSpaceTracingRefraction;
lightingDebugSettings.debugScreenSpaceTracingMode = (DebugScreenSpaceTracing)value;
}
else
{
lightingDebugSettings.debugLightingMode = DebugLightingMode.None;
lightingDebugSettings.debugScreenSpaceTracingMode = DebugScreenSpaceTracing.None;
}
}
void RegisterStatisticsDebug()
{
var list = new List<DebugUI.Widget>();
list.Add(new DebugUI.Container
{
displayName = "Screen Space Tracing Debug",
children =
{
new DebugUI.BoolField { displayName = "Debug Iterations", getter = IsScreenSpaceTracingIterationDebugEnabled, setter = SetScreenSpaceTracingIterationDebugEnabled },
new DebugUI.EnumField { displayName = "Debug Mode", getter = GetDebugLightingSubMode, setter = SetScreenSpaceTracingDebugMode, enumNames = debugScreenSpaceTracingStrings, enumValues = debugScreenSpaceTracingValues },
new DebugUI.Value { displayName = "Cell Size", getter = () => string.Format("({0}, {1}) px", screenSpaceTracingDebugData.cellSizeW, screenSpaceTracingDebugData.cellSizeH) },
new DebugUI.Value { displayName = "Level / Max", getter = () => string.Format("{0}/{1}", screenSpaceTracingDebugData.level, screenSpaceTracingDebugData.levelMax) },
new DebugUI.Value { displayName = "Iteration / Max", getter = () => string.Format("{0}/{1}", screenSpaceTracingDebugData.iteration, screenSpaceTracingDebugData.iterationMax) },
new DebugUI.Value { displayName = "Start Depth", getter = () => string.Format("{0:F7} m", screenSpaceTracingDebugData.startLinearDepth) },
new DebugUI.Value { displayName = "Hit Depth", getter = () => string.Format("{0:F7} m", screenSpaceTracingDebugData.hitLinearDepth) },
new DebugUI.Value { displayName = "Depth Buffer", getter = () => string.Format("{0:F7} m", screenSpaceTracingDebugData.hiZLinearDepth) },
new DebugUI.Value { displayName = "Hit Distance", getter = () => string.Format("{0:F7} m", screenSpaceTracingDebugData.hitDistance) },
new DebugUI.Value { displayName = "Iteration Distance", getter = () => string.Format("{0:F7} m", screenSpaceTracingDebugData.iterationDistance) },
new DebugUI.Value { displayName = "Ray TXS", getter = () => string.Format("({0:F7}, {1:F7}) px", screenSpaceTracingDebugData.rayTXS.x, screenSpaceTracingDebugData.rayTXS.y) },
new DebugUI.Value { displayName = "Ray TXS Depth", getter = () => string.Format("({0:F7}) m", 1f / screenSpaceTracingDebugData.rayTXS.z) },
}
});
m_DebugStatisticsItems = list.ToArray();
var panel = DebugManager.instance.GetPanel(k_PanelStatistics, true);
panel.children.Add(m_DebugStatisticsItems);
}
public void RegisterMaterialDebug()

RegisterMaterialDebug();
RegisterLightingDebug();
RegisterRenderingDebug();
RegisterStatisticsDebug();
}
public void UnregisterDebug()

UnregisterDebugItems(k_PanelLighting, m_DebugLightingItems);
UnregisterDebugItems(k_PanelRendering, m_DebugLightingItems);
UnregisterDebugItems(k_PanelStatistics, m_DebugStatisticsItems);
}
void UnregisterDebugItems(string panelName, DebugUI.Widget[] items)

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


float hitDistance;
float hitLinearDepth;
float2 hitPositionSS;
float iterationDistance;
float unused01;
float unused02;
float3 rayTXS;
float iterationDistance;
float3 unused00;
};
//

{
return value.hitPositionSS;
}
float GetIterationDistance(ScreenSpaceTracingDebug value)
{
return value.iterationDistance;
}
float GetUnused01(ScreenSpaceTracingDebug value)
float3 GetRayTXS(ScreenSpaceTracingDebug value)
return value.unused01;
return value.rayTXS;
float GetUnused02(ScreenSpaceTracingDebug value)
float GetIterationDistance(ScreenSpaceTracingDebug value)
return value.unused02;
return value.iterationDistance;
}
float3 GetUnused00(ScreenSpaceTracingDebug value)
{
return value.unused00;
}

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


return output;
}
// String debug utilities
bool SampleFloatValue(uint2 pixelSS, uint2 positionSS, uint stringVal[16], float value)
{
bool isValid = false;
SAMPLE_DEBUG_STRING(pixelSS - positionSS, stringVal, isValid);
if (!isValid)
isValid = SampleDebugFloatNumber(pixelSS - positionSS - uint2(100, 00), value);
return isValid;
}
bool SampleIntOverIntValue(uint2 pixelSS, uint2 positionSS, uint stringVal[16], int valueA, int valueB)
{
bool isValid = false;
SAMPLE_DEBUG_STRING(pixelSS - positionSS, stringVal, isValid);
if (!isValid)
isValid = SampleDebugFontNumber(pixelSS - positionSS - uint2(100, 00), valueA);
if (!isValid)
isValid = SampleDebugLetter(pixelSS - positionSS - uint2(112, 00), '/');
if (!isValid)
isValid = SampleDebugFontNumber(pixelSS - positionSS - uint2(124, 00), valueB);
return isValid;
}
//
// Motion vector debug utilities
float DistanceToLine(float2 p, float2 p1, float2 p2)
{

const float positionRingSDF = clamp(ringSize - positionRingDistance, 0, 1);
const float w = clamp(1 - startPositionRingSDF - positionRingSDF, 0, 1);
col = col * w + float4(1, 1, 1, 1) * (1 - w);
if (posInput.positionSS.y < 200)
{
const uint kStrings1[16] = { 'S', 't', 'a', 'r', 't', ' ', 'D', 'e', 'p', 't', 'h', ':', ' ', 0u, ' ', ' ' };
const uint kStrings2[16] = { 'D', 'e', 'p', 't', 'h', ':', ' ', 0u, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
const uint kStrings3[16] = { 'L', 'e', 'v', 'e', 'l', ':', ' ', 0u, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
const uint kStrings4[16] = { 'I', 't', 'e', 'r', 'a', 't', 'i', 'o', 'n', ':', ' ', 0u, ' ', ' ', ' ', ' ' };
const uint kStrings5[16] = { 'I', 't', '.', ' ', 'D', 'i', 's', 't', 'a', 'n', 'c', 'e', ':', ' ', 0u, ' ' };
const uint kStrings6[16] = { 'I', 't', '.', ' ', 'H', 'i', 'Z', ' ', 'D', 'e', 'p', 't', 'h', ':', ' ', 0u };
if (
// SampleFloatValue (posInput.positionSS, uint2(70, 10), kStrings1, debug.startLinearDepth)
SampleFloatValue (posInput.positionSS, uint2(70, 30), kStrings2, debug.hitLinearDepth)
//|| SampleIntOverIntValue(posInput.positionSS, uint2(70, 50), kStrings3, debug.level, debug.levelMax)
|| SampleIntOverIntValue(posInput.positionSS, uint2(70, 70), kStrings4, debug.iteration + 1, debug.iterationMax)
|| SampleFloatValue (posInput.positionSS, uint2(300, 10), kStrings5, debug.iterationDistance)
|| SampleFloatValue (posInput.positionSS, uint2(300, 30), kStrings6, debug.hiZLinearDepth)
)
col = float4(1, 1, 1, 1);
}
return col;
}

10
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs


RTHandle m_DebugScreenSpaceTracing = null;
ComputeBuffer m_DebugScreenSpaceTracingData = null;
ScreenSpaceTracingDebug[] m_DebugScreenSpaceTracingDataArray = new ScreenSpaceTracingDebug[1];
public HDRenderPipeline(HDRenderPipelineAsset asset)
{

CommandBufferPool.Release(cmd);
renderContext.Submit();
if (m_CurrentDebugDisplaySettings.fullScreenDebugMode == FullScreenDebugMode.ScreenSpaceTracingRefraction)
{
m_DebugScreenSpaceTracingData.GetData(m_DebugScreenSpaceTracingDataArray);
var data = m_DebugScreenSpaceTracingDataArray[0];
m_CurrentDebugDisplaySettings.screenSpaceTracingDebugData = data;
}
} // For each camera
}

public void ApplyDebugDisplaySettings(HDCamera hdCamera, CommandBuffer cmd)
{
// Temporary
if (m_CurrentDebugDisplaySettings.IsDebugDisplayEnabled() ||
m_CurrentDebugDisplaySettings.fullScreenDebugMode != FullScreenDebugMode.None ||
m_CurrentDebugDisplaySettings.colorPickerDebugSettings.colorPickerMode != ColorPickerDebugMode.None)

正在加载...
取消
保存