浏览代码

Removed iteration blending for linear raymarching

Fixed linear raymarching depth test
/main
Frédéric Vauchelles 6 年前
当前提交
dd47524a
共有 9 个文件被更改,包括 42 次插入82 次删除
  1. 6
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Debug/DebugFullScreen.shader
  2. 8
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/ScreenSpaceLightingEditor.cs
  3. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs
  4. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs
  5. 5
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceLighting.cs
  6. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceReflection.cs
  7. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceRefraction.cs
  8. 61
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceTracing.hlsl
  9. 36
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Material/Lit/Lit.hlsl

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


const float raySegmentSDF = clamp(1 - distanceToRaySegment, 0, 1);
float cellSDF = 0;
float debugLinearDepth = 0;
float debugLinearDepth = LinearEyeDepth(deviceDepth, _ZBufferParams);;
if (debug.tracingModel == PROJECTIONMODEL_HI_Z
|| debug.tracingModel == PROJECTIONMODEL_LINEAR)
{

distanceToCell = min(distanceToCell, float2(iterationCellSize) - distanceToCell);
distanceToCell = clamp(1 - distanceToCell, 0, 1);
cellSDF = max(distanceToCell.x, distanceToCell.y) * _ShowGrid;
debugLinearDepth = posInput.linearDepth;
}
col = float4(

col.rgb = col.rgb * w + float3(1, 1, 1) * (1 - w);
if (_ShowDepthPyramidDebug == 1)
color.rgb = frac(float3(debugLinearDepth, debugLinearDepth, debugLinearDepth) * 0.1);
color.rgb = frac(debugLinearDepth * 0.1);
col = float4(col.rgb * col.a + color.rgb, 1);

8
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Lighting/Reflection/ScreenSpaceLightingEditor.cs


{
protected SerializedDataParameter m_RayLevel;
protected SerializedDataParameter m_RayMaxLinearIterationsLevel;
protected SerializedDataParameter m_RayIterationBlending;
protected SerializedDataParameter m_RayMinLevel;
protected SerializedDataParameter m_RayMaxLevel;
protected SerializedDataParameter m_RayMaxIterations;

m_RayLevel = Unpack(o.Find(x => x.rayLevel));
m_RayMaxLinearIterationsLevel = Unpack(o.Find(x => x.rayMaxLinearIterationsLevel));
m_RayIterationBlending = Unpack(o.Find(x => x.rayIterationBlending));
m_RayMinLevel = Unpack(o.Find(x => x.rayMinLevel));
m_RayMaxLevel = Unpack(o.Find(x => x.rayMaxLevel));
m_RayMaxIterations = Unpack(o.Find(x => x.rayMaxIterations));

protected virtual void OnProxyInspectorGUI()
{
EditorGUILayout.LabelField(CoreEditorUtils.GetContent("Proxy Settings"));
PropertyField(m_RayLevel, CoreEditorUtils.GetContent("Linear Ray Level"));
PropertyField(m_RayMaxLinearIterationsLevel, CoreEditorUtils.GetContent("Linear Iterations"));
PropertyField(m_RayDepthSuccessBias, CoreEditorUtils.GetContent("Linear Ray Depth Success Bias"));
PropertyField(m_RayIterationBlending, CoreEditorUtils.GetContent("Linear Ray Iteration Blending"));
}
protected virtual void OnCommonInspectorGUI()

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs


// (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._DebugScreenSpaceTracingData, m_DebugScreenSpaceTracingData);
m_DebugFullScreen.SetTexture(HDShaderIDs._DepthPyramidTexture, hdCamera.GetPreviousFrameRT((int)HDCameraFrameHistoryType.DepthPyramid));
m_DebugFullScreen.SetTexture(HDShaderIDs._DepthPyramidTexture, hdCamera.GetCurrentFrameRT((int)HDCameraFrameHistoryType.DepthPyramid));
HDUtils.DrawFullScreen(cmd, hdCamera, m_DebugFullScreen, (RenderTargetIdentifier)BuiltinRenderTextureType.CameraTarget);
PushColorPickerDebugTexture(cmd, (RenderTargetIdentifier)BuiltinRenderTextureType.CameraTarget, hdCamera);

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDStringConstants.cs


Shader.PropertyToID("_SSSBufferTexture3"),
};
public static readonly int _SSRefractionRayIterationBlending = Shader.PropertyToID("_SSRefractionRayIterationBlending");
public static readonly int _SSRefractionRayLevel = Shader.PropertyToID("_SSRefractionRayLevel");
public static readonly int _SSRefractionRayMaxLinearIterations = Shader.PropertyToID("_SSRefractionRayMaxLinearIterations");
public static readonly int _SSRefractionRayMinLevel = Shader.PropertyToID("_SSRefractionRayMinLevel");

public static readonly int _SSRefractionInvScreenWeightDistance = Shader.PropertyToID("_SSRefractionInvScreenWeightDistance");
public static readonly int _SSReflectionRayIterationBlending = Shader.PropertyToID("_SSReflectionRayIterationBlending");
public static readonly int _SSReflectionRayMaxLinearIterations = Shader.PropertyToID("_SSReflectionRayMaxLinearIterations");
public static readonly int _SSReflectionRayLevel = Shader.PropertyToID("_SSReflectionRayLevel");
public static readonly int _SSReflectionRayMinLevel = Shader.PropertyToID("_SSReflectionRayMinLevel");

5
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceLighting.cs


{
int m_RayLevelID;
int m_RayMaxLinearIterationsID;
int m_RayIterationBlendingID;
int m_RayMinLevelID;
int m_RayMaxLevelID;
int m_RayMaxIterationsID;

public IntParameter rayLevel = new IntParameter(2);
public IntParameter rayMaxLinearIterationsLevel = new IntParameter(2);
public IntParameter rayIterationBlending = new IntParameter(2);
public IntParameter rayMinLevel = new IntParameter(2);
public IntParameter rayMaxLevel = new IntParameter(6);
public IntParameter rayMaxIterations = new IntParameter(32);

{
cmd.SetGlobalInt(m_RayLevelID, rayLevel.value);
cmd.SetGlobalInt(m_RayMaxLinearIterationsID, rayMaxLinearIterationsLevel.value);
cmd.SetGlobalInt(m_RayIterationBlendingID, rayIterationBlending.value);
cmd.SetGlobalInt(m_RayMinLevelID, rayMinLevel.value);
cmd.SetGlobalInt(m_RayMaxLevelID, rayMaxLevel.value);
cmd.SetGlobalInt(m_RayMaxIterationsID, rayMaxIterations.value);

protected abstract void FetchIDs(
out int rayLevelID,
out int rayMaxLinearIterationsLevelID,
out int rayIterationBlendingID,
out int rayMinLevelID,
out int rayMaxLevelID,
out int rayMaxIterationsID,

FetchIDs(
out m_RayLevelID,
out m_RayMaxLinearIterationsID,
out m_RayIterationBlendingID,
out m_RayMinLevelID,
out m_RayMaxLevelID,
out m_RayMaxIterationsID,

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceReflection.cs


protected override void FetchIDs(
out int rayLevelID,
out int rayMaxLinearIterationsLevelID,
out int rayIterationBlending,
out int rayMinLevelID,
out int rayMaxLevelID,
out int rayMaxIterationsID,

{
rayLevelID = HDShaderIDs._SSReflectionRayLevel;
rayMaxLinearIterationsLevelID = HDShaderIDs._SSReflectionRayMaxLinearIterations;
rayIterationBlending = HDShaderIDs._SSReflectionRayIterationBlending;
rayMinLevelID = HDShaderIDs._SSReflectionRayMinLevel;
rayMaxLevelID = HDShaderIDs._SSReflectionRayMaxLevel;
rayMaxIterationsID = HDShaderIDs._SSReflectionRayMaxIterations;

2
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceRefraction.cs


protected override void FetchIDs(
out int rayLevelID,
out int rayMaxLinearIterationsID,
out int rayIterationBlending,
out int rayMinLevelID,
out int rayMaxLevelID,
out int rayMaxIterationsID,

{
rayLevelID = HDShaderIDs._SSRefractionRayLevel;
rayMaxLinearIterationsID = HDShaderIDs._SSRefractionRayMaxLinearIterations;
rayIterationBlending = HDShaderIDs._SSRefractionRayIterationBlending;
rayMinLevelID = HDShaderIDs._SSRefractionRayMinLevel;
rayMaxLevelID = HDShaderIDs._SSRefractionRayMaxLevel;
rayMaxIterationsID = HDShaderIDs._SSRefractionRayMaxIterations;

61
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Lighting/Reflection/ScreenSpaceTracing.hlsl


int settingRayLevel, // Mip level to use to ray march depth buffer
uint settingsRayMaxIterations, // Maximum number of iterations (= max number of depth samples)
int settingsDebuggedAlgorithm, // currently debugged algorithm (see PROJECTIONMODEL defines)
float settingsRayDepthSuccessBias, // Bias to use when trying to detect whenever we raymarch behind a surface
// Precomputed properties
float3 startPositionSS, // Start position in Screen Space (x in pixel, y in pixel, z = 1/linearDepth)
float3 raySS, // Ray direction in Screen Space (dx in pixel, dy in pixel, z = 1/endPointLinearDepth - 1/startPointLinearDepth)

)
{
ZERO_INITIALIZE(ScreenSpaceRayHit, hit);
bool hitSuccessful = true;
bool hitSuccessful = false;
iteration = 0u;
int mipLevel = min(max(settingRayLevel, 0), int(_DepthPyramidScale.z));
uint maxIterations = settingsRayMaxIterations;

#ifdef DEBUG_DISPLAY
// Fetch post iteration debug values
if (input.debug && _DebugStep >= iteration)
if (input.debug && _DebugStep >= int(iteration))
{
debugIterationPositionSS = positionSS;
debugIterationLinearDepthBuffer = 1 / invLinearDepth;

if (!IsPositionAboveDepth(positionSS.z, invLinearDepth))
{
hitSuccessful = true;
}
// Check if we are out of the buffer
if (any(int2(positionSS.xy) > int2(bufferSize))

hit.linearDepth = 1 / positionSS.z;
hit.positionNDC = float2(positionSS.xy) / float2(bufferSize);
hit.positionSS = uint2(positionSS.xy);
if (hit.linearDepth > (1 / invLinearDepth) + settingsRayDepthSuccessBias)
hitSuccessful = false;
#ifdef DEBUG_DISPLAY
DebugComputeCommonOutput(input.rayDirWS, hitSuccessful, PROJECTIONMODEL_LINEAR, hit);

// Settings (linear)
int settingRayLevel, // Mip level to use to ray march depth buffer
uint settingsRayMaxIterations, // Maximum number of iterations (= max number of depth samples)
uint settingsIterationBlending, // Number of linear raymarching iterations to blend with proxy raycast
float settingsRayDepthSuccessBias, // Bias to use when trying to detect whenever we raymarch behind a surface
out ScreenSpaceRayHit linearHit, // Linear raymarching hit
out ScreenSpaceRayHit proxyHit, // Proxy raycasting hit
float hitBlending // hit = linearHit * (1.0 - hitBlending) + proxyHit * hitBlending
out ScreenSpaceRayHit hit
)
{
// Perform linear raymarch

settingRayLevel,
settingsRayMaxIterations,
settingsDebuggedAlgorithm,
settingsRayDepthSuccessBias,
// Precomputed properties
startPositionSS,
raySS,

linearHit,
hit,
hitBlending = settingsIterationBlending > 0
? 1.0 + (int(iteration) - int(settingsRayMaxIterations)) / float(settingsIterationBlending)
: 0;
if (!hitSuccessful || hitBlending > 0)
if (!hitSuccessful)
bool proxyHitSuccessful = ScreenSpaceProxyRaycast(
hitSuccessful = ScreenSpaceProxyRaycast(
proxyHit
hit
hitSuccessful = hitSuccessful && proxyHitSuccessful;
}
return hitSuccessful;

// Initialize loop
ZERO_INITIALIZE(ScreenSpaceRayHit, hit);
bool hitSuccessful = true;
bool hitSuccessful = false;
uint iteration = 0u;
int minMipLevel = max(settingsRayMinLevel, 0u);
int maxMipLevel = min(settingsRayMaxLevel, uint(_DepthPyramidScale.z));

// settings
settingsRayLevel,
settingsRayMaxLinearIterations,
settingsRayDepthSuccessBias,
#ifdef DEBUG_DISPLAY
settingsDebuggedAlgorithm,
#else

while (currentLevel >= minMipLevel)
{
hitSuccessful = true;
if (iteration >= maxIterations)
{
hitSuccessful = false;

#ifdef DEBUG_DISPLAY
// Fetch pre iteration debug values
if (input.debug && _DebugStep >= iteration)
if (input.debug && _DebugStep >= int(iteration))
debugIterationMipLevel = currentLevel;
#endif

#ifdef DEBUG_DISPLAY
// Fetch post iteration debug values
if (input.debug && _DebugStep >= iteration)
if (input.debug && _DebugStep >= int(iteration))
{
debugLoopMipMaxUsedLevel = max(debugLoopMipMaxUsedLevel, currentLevel);
debugIterationPositionSS = positionSS;

int SSRT_SETTING(RayMaxLevel, SSRTID);
int SSRT_SETTING(RayMaxLinearIterations, SSRTID);
int SSRT_SETTING(RayMaxIterations, SSRTID);
int SSRT_SETTING(RayIterationBlending, SSRTID);
float SSRT_SETTING(RayDepthSuccessBias, SSRTID);
#ifdef DEBUG_DISPLAY

// settings
SSRT_SETTING(RayLevel, SSRTID),
SSRT_SETTING(RayMaxIterations, SSRTID),
SSRT_SETTING(RayDepthSuccessBias, SSRTID),
#ifdef DEBUG_DISPLAY
SSRT_SETTING(DebuggedAlgorithm, SSRTID),
#else

// -------------------------------------------------
bool MERGE_NAME(ScreenSpaceProxyRaycast, SSRTID)(
ScreenSpaceProxyRaycastInput input,
out ScreenSpaceRayHit primaryHit,
out ScreenSpaceRayHit secondaryHit,
out float hitBlending
out ScreenSpaceRayHit hit
hitBlending = 0;
return ScreenSpaceLinearProxyRaycast(
return ScreenSpaceProxyRaycast(
// Settings (linear)
SSRT_SETTING(RayLevel, SSRTID),
SSRT_SETTING(RayMaxLinearIterations, SSRTID),
SSRT_SETTING(RayIterationBlending, SSRTID),
// Settings (common)
// Settings
PROJECTIONMODEL_NONE,
int(PROJECTIONMODEL_NONE),
primaryHit,
secondaryHit,
hitBlending
hit
);
}

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


// -------------------------------
// Screen space tracing query
// -------------------------------
ScreenSpaceRayHit primaryHit, secondaryHit;
float hitBlending = 0;
ZERO_INITIALIZE(ScreenSpaceRayHit, primaryHit);
ZERO_INITIALIZE(ScreenSpaceRayHit, secondaryHit);
ScreenSpaceRayHit hit;
ZERO_INITIALIZE(ScreenSpaceRayHit, hit);
bool hitSuccessful = false;
// -------------------------------

#if HAS_REFRACTION
if (GPUImageBasedLightingType == GPUIMAGEBASEDLIGHTINGTYPE_REFRACTION)
hitSuccessful = ScreenSpaceProxyRaycastRefraction(ssRayInput, primaryHit, secondaryHit, hitBlending);
hitSuccessful = ScreenSpaceProxyRaycastRefraction(ssRayInput, hit);
hitSuccessful = ScreenSpaceProxyRaycastReflection(ssRayInput, primaryHit, secondaryHit, hitBlending);
hitSuccessful = ScreenSpaceProxyRaycastReflection(ssRayInput, hit);
}
// -------------------------------

#if HAS_REFRACTION
if (GPUImageBasedLightingType == GPUIMAGEBASEDLIGHTINGTYPE_REFRACTION)
hitSuccessful = ScreenSpaceHiZRaymarchRefraction(ssRayInput, primaryHit);
hitSuccessful = ScreenSpaceHiZRaymarchRefraction(ssRayInput, hit);
hitSuccessful = ScreenSpaceHiZRaymarchReflection(ssRayInput, primaryHit);
hitSuccessful = ScreenSpaceHiZRaymarchReflection(ssRayInput, hit);
}
// Debug screen space tracing

float weight = 1.0;
UpdateLightingHierarchyWeights(hierarchyWeight, weight);
if (GPUImageBasedLightingType == GPUIMAGEBASEDLIGHTINGTYPE_REFRACTION)
lighting.specularTransmitted = primaryHit.debugOutput;
lighting.specularTransmitted = hit.debugOutput;
lighting.specularReflected = primaryHit.debugOutput;
lighting.specularReflected = hit.debugOutput;
return lighting;
}
#endif

// -------------------------------
// Resolve weight and color
// -------------------------------
float2 weightNDC = clamp(min(primaryHit.positionNDC, 1 - primaryHit.positionNDC) * invScreenWeightDistance, 0, 1);
float2 weightNDC = clamp(min(hit.positionNDC, 1 - hit.positionNDC) * invScreenWeightDistance, 0, 1);
float hitDeviceDepth = LOAD_TEXTURE2D_LOD(_DepthPyramidTexture, primaryHit.positionSS, 0).r;
float hitDeviceDepth = LOAD_TEXTURE2D_LOD(_DepthPyramidTexture, hit.positionSS, 0).r;
float hitLinearDepth = LinearEyeDepth(hitDeviceDepth, _ZBufferParams);
// Exit if texel is out of color buffer

float3 preLD = SAMPLE_TEXTURE2D_LOD(
_ColorPyramidTexture,
s_trilinear_clamp_sampler,
primaryHit.positionNDC * _ColorPyramidScale.xy,
hit.positionNDC * _ColorPyramidScale.xy,
if (hitBlending > 0)
{
float3 preLD2 = SAMPLE_TEXTURE2D_LOD(
_ColorPyramidTexture,
s_trilinear_clamp_sampler,
secondaryHit.positionNDC * _ColorPyramidScale.xy,
mipLevel
).rgb;
preLD = preLD * (1.0 - hitBlending) + preLD2 * hitBlending;
}
// We use specularFGD as an approximation of the fresnel effect (that also handle smoothness)
float3 F = preLightData.specularFGD;

正在加载...
取消
保存