浏览代码

Use a distortion mask to discard pixels instead of depth

/stochastic_alpha_test
Frédéric Vauchelles 7 年前
当前提交
c38d4aba
共有 15 个文件被更改,包括 1007 次插入69 次删除
  1. 962
      SampleScenes/HDTest/DistortionTest.unity
  2. 2
      SampleScenes/HDTest/GraphicTest/Common/CustomTexture/Mat_DistortionMap_Voronoi.mat
  3. 18
      SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Lit_2DNoise.mat
  4. 12
      SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Lit_Only_2DNoise.mat
  5. 5
      SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Lit_Only_Ripple.mat
  6. 11
      SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Lit_Voronoi.mat
  7. 5
      SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Unlit_Only_2DNoise.mat
  8. 5
      SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Unlit_Only_Ripple.mat
  9. 10
      ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs
  10. 1
      ScriptableRenderPipeline/HDRenderPipeline/HDStringConstants.cs
  11. 7
      ScriptableRenderPipeline/HDRenderPipeline/Material/Builtin/BuiltinData.hlsl
  12. 2
      ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Lit.shader
  13. 2
      ScriptableRenderPipeline/HDRenderPipeline/Material/Unlit/Unlit.shader
  14. 31
      ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/ApplyDistorsion.compute
  15. 3
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDistortion.hlsl

962
SampleScenes/HDTest/DistortionTest.unity
文件差异内容过多而无法显示
查看文件

2
SampleScenes/HDTest/GraphicTest/Common/CustomTexture/Mat_DistortionMap_Voronoi.mat


- _RefractionMode: 0
- _ShiverDirectionality: 0.5
- _ShiverDrag: 0.2
- _Size: 4.48
- _Size: 7.49
- _Smoothness: 1
- _SmoothnessRemapMax: 1
- _SmoothnessRemapMin: 0

18
SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Lit_2DNoise.mat


m_Name: Mat_Distorsion_Lit_2DNoise
m_Shader: {fileID: 4800000, guid: 6e4ae4064600d784cac1e41a9e6f2e59, type: 3}
m_ShaderKeywords: _ALBEDOAFFECTEMISSIVE_OFF _ALPHACUTOFFENABLE_OFF _BLENDMODE_LERP
_DEPTHOFFSETENABLE_OFF _DISTORTIONDEPTHTEST_OFF _DISTORTIONNULLIFY_OFF _DISTORTION_ON
_DOUBLESIDEDENABLE_OFF _ENABLEPERPIXELDISPLACEMENT_OFF _ENABLESPECULAROCCLUSION_OFF
_DEPTHOFFSETENABLE_OFF _DISTORTIONDEPTHTEST_OFF _DISTORTIONNULLIFY_OFF _DISTORTIONONLY_OFF
_DISTORTION_ON _DOUBLESIDEDENABLE_OFF _ENABLEPERPIXELDISPLACEMENT_OFF _ENABLESPECULAROCCLUSION_OFF
_ENABLEVERTEXDISPLACEMENT_OFF _ENABLEWIND_OFF _NORMALMAP_TANGENT_SPACE
m_LightmapFlags: 4
m_EnableInstancingVariants: 0

RenderType: Transparent
disabledShaderPasses:
- MOTIONVECTORS
- Forward
- DepthOnly
- ForwardOnlyOpaqueDepthOnly
- ForwardOnlyOpaque
- GBuffer
- GBufferWithPrepass
- TransparentDepthPrepass
- META
- SHADOWCASTER
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:

- _DistortionDstBlend: 1
- _DistortionEnable: 1
- _DistortionNullify: 0
- _DistortionOnly: 1
- _DistortionOnly: 0
- _DistortionScale: 2.21
- _DistortionSrcBlend: 1
- _DistortionStencilRef: 4

12
SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Lit_Only_2DNoise.mat


stringTagMap:
RenderType: Transparent
disabledShaderPasses:
- GBuffer
- META
- SHADOWCASTER
- TransparentDepthPrepass
- MOTIONVECTORS
- GBuffer
- MOTIONVECTORS
- TransparentDepthPrepass
- META
- SHADOWCASTER
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:

- _DistortionBlendOp: 0
- _DistortionBlurBlendMode: 0
- _DistortionBlurDstBlend: 1
- _DistortionBlurRemapMax: 1
- _DistortionBlurRemapMin: 0
- _DistortionBlurScale: 1
- _DistortionBlurSrcBlend: 1
- _DistortionDepthTest: 0

5
SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Lit_Only_Ripple.mat


stringTagMap:
RenderType: Transparent
disabledShaderPasses:
- MOTIONVECTORS
- MOTIONVECTORS
- DepthOnly
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:

- _DistortionBlurDstBlend: 1
- _DistortionBlurRemapMax: 0.58173394
- _DistortionBlurRemapMin: 0
- _DistortionBlurScale: 0.71
- _DistortionBlurScale: 0
- _DistortionBlurSrcBlend: 1
- _DistortionDepthTest: 1
- _DistortionDstBlend: 1

11
SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Lit_Voronoi.mat


m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: Mat_Distorsion_Lit_2DNoise
m_Name: Mat_Distorsion_Lit_Voronoi
m_Shader: {fileID: 4800000, guid: 6e4ae4064600d784cac1e41a9e6f2e59, type: 3}
m_ShaderKeywords: _ALBEDOAFFECTEMISSIVE_OFF _ALPHACUTOFFENABLE_OFF _BLENDMODE_LERP
_DEPTHOFFSETENABLE_OFF _DISTORTIONDEPTHTEST_OFF _DISTORTIONNULLIFY_OFF _DISTORTION_ON

disabledShaderPasses:
- MOTIONVECTORS
- Forward
- DepthOnly
- SHADOWCASTER
- ForwardOnlyOpaqueDepthOnly
- ForwardOnlyOpaque
- GBuffer

- SHADOWCASTER
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:

m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DistortionVectorMap:
m_Texture: {fileID: 8600000, guid: 99d420ed53cf5bc449b014653d05770d, type: 2}
m_Texture: {fileID: 8600000, guid: 857ebf2cca737d54194a3a1077fec4cc, type: 2}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissiveColorMap:

- _DistortionBlurDstBlend: 1
- _DistortionBlurRemapMax: 1
- _DistortionBlurRemapMin: 0
- _DistortionBlurScale: 1.93
- _DistortionBlurScale: 1.3
- _DistortionBlurSrcBlend: 1
- _DistortionDepthTest: 0
- _DistortionDstBlend: 1

- _DistortionScale: 2.21
- _DistortionScale: 200000
- _DistortionSrcBlend: 1
- _DistortionStencilRef: 4
- _DoubleSidedEnable: 0

5
SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Unlit_Only_2DNoise.mat


- GBuffer
- META
- SHADOWCASTER
- DepthOnly
- TransparentDepthPrepass
- TransparentDepthPrepass
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:

- _DistortionBlendMode: 0
- _DistortionBlurBlendMode: 0
- _DistortionBlurDstBlend: 1
- _DistortionBlurRemapMax: 1
- _DistortionBlurRemapMin: 0
- _DistortionBlurScale: 1
- _DistortionBlurSrcBlend: 1
- _DistortionDepthTest: 0

5
SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Unlit_Only_Ripple.mat


- GBuffer
- META
- SHADOWCASTER
- DepthOnly
- TransparentDepthPrepass
- TransparentDepthPrepass
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:

- _DistortionBlendMode: 0
- _DistortionBlurBlendMode: 0
- _DistortionBlurDstBlend: 1
- _DistortionBlurRemapMax: 1
- _DistortionBlurRemapMin: 0
- _DistortionBlurScale: 1
- _DistortionBlurSrcBlend: 1
- _DistortionDepthTest: 1

10
ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs


// <<< Old SSS Model
readonly int m_VelocityBuffer;
readonly int m_DistortionBuffer;
readonly int m_DistortionDepthBuffer;
readonly int m_GaussianPyramidColorBuffer;
readonly int m_DepthPyramidBuffer;

// <<< Old SSS Model
readonly RenderTargetIdentifier m_VelocityBufferRT;
readonly RenderTargetIdentifier m_DistortionBufferRT;
readonly RenderTargetIdentifier m_DistortionDepthBufferRT;
readonly RenderTargetIdentifier m_GaussianPyramidColorBufferRT;
readonly RenderTargetIdentifier m_DepthPyramidBufferRT;
RenderTextureDescriptor m_GaussianPyramidColorBufferDesc;

m_DistortionBuffer = HDShaderIDs._DistortionTexture;
m_DistortionBufferRT = new RenderTargetIdentifier(m_DistortionBuffer);
m_DistortionDepthBuffer = HDShaderIDs._DistortionDepthTexture;
m_DistortionDepthBufferRT = new RenderTargetIdentifier(m_DistortionDepthBuffer);
m_GaussianPyramidKernel = m_GaussianPyramidCS.FindKernel("KMain");
m_GaussianPyramidColorBuffer = HDShaderIDs._GaussianPyramidColorTexture;

int w = camera.pixelWidth;
int h = camera.pixelHeight;
cmd.GetTemporaryRT(m_DistortionDepthBuffer, w, h, 24, FilterMode.Point, RenderTextureFormat.Depth, RenderTextureReadWrite.Linear);
cmd.SetRenderTarget(m_DistortionBufferRT, m_DistortionDepthBufferRT);
cmd.ClearRenderTarget(true, true, Color.clear);
cmd.SetRenderTarget(m_DistortionBufferRT, m_CameraDepthStencilBufferRT);
cmd.ClearRenderTarget(false, true, Color.clear);
// Only transparent object can render distortion vectors
RenderTransparentRenderList(cullResults, camera, renderContext, cmd, HDShaderPassNames.s_DistortionVectorsName);

uint x, y, z;
resources.applyDistortionCS.GetKernelThreadGroupSizes(resources.applyDistortionKernel, out x, out y, out z);
cmd.SetComputeTextureParam(resources.applyDistortionCS, resources.applyDistortionKernel, HDShaderIDs._DistortionTexture, m_DistortionBufferRT);
cmd.SetComputeTextureParam(resources.applyDistortionCS, resources.applyDistortionKernel, HDShaderIDs._DistortionDepthTexture, m_DistortionDepthBufferRT);
cmd.SetComputeTextureParam(resources.applyDistortionCS, resources.applyDistortionKernel, HDShaderIDs._GaussianPyramidColorTexture, m_GaussianPyramidColorBufferRT);
cmd.SetComputeTextureParam(resources.applyDistortionCS, resources.applyDistortionKernel, HDShaderIDs._CameraColorTexture, m_CameraColorBufferRT);
cmd.SetComputeTextureParam(resources.applyDistortionCS, resources.applyDistortionKernel, HDShaderIDs._DepthTexture, GetDepthTexture());

1
ScriptableRenderPipeline/HDRenderPipeline/HDStringConstants.cs


public static readonly int _VelocityTexture = Shader.PropertyToID("_VelocityTexture");
public static readonly int _DistortionTexture = Shader.PropertyToID("_DistortionTexture");
public static readonly int _DistortionDepthTexture = Shader.PropertyToID("_DistortionDepthTexture");
public static readonly int _GaussianPyramidColorTexture = Shader.PropertyToID("_GaussianPyramidColorTexture");
public static readonly int _DepthPyramidTexture = Shader.PropertyToID("_PyramidDepthTexture");
public static readonly int _GaussianPyramidColorMipSize = Shader.PropertyToID("_GaussianPyramidColorMipSize");

7
ScriptableRenderPipeline/HDRenderPipeline/Material/Builtin/BuiltinData.hlsl


velocity = inBuffer.xy;
}
void EncodeDistortion(float2 distortion, float distortionBlur, out float4 outBuffer)
void EncodeDistortion(float2 distortion, float distortionBlur, bool isValidSource, out float4 outBuffer)
outBuffer = float4(distortion, 0.0, distortionBlur);
outBuffer = float4(distortion, isValidSource, distortionBlur);
void DecodeDistortion(float4 inBuffer, out float2 distortion, out float2 distortionBlur)
void DecodeDistortion(float4 inBuffer, out float2 distortion, out float distortionBlur, out bool isValidSource)
isValidSource = (inBuffer.z != 0.0);
}
void GetBuiltinDataDebug(uint paramId, BuiltinData builtinData, inout float3 result, inout bool needLinearToSRGB)

2
ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Lit.shader


Blend [_DistortionSrcBlend] [_DistortionDstBlend], [_DistortionBlurSrcBlend] [_DistortionBlurDstBlend]
BlendOp Add, [_DistortionBlurBlendOp]
ZTest [_ZTestMode]
ZWrite on
ZWrite off
Cull [_CullMode]
HLSLPROGRAM

2
ScriptableRenderPipeline/HDRenderPipeline/Material/Unlit/Unlit.shader


Blend [_DistortionSrcBlend] [_DistortionDstBlend], [_DistortionBlurSrcBlend] [_DistortionBlurDstBlend]
BlendOp Add, [_DistortionBlurBlendOp]
ZTest [_ZTestMode]
ZWrite on
ZWrite off
Cull [_CullMode]
HLSLPROGRAM

31
ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/ApplyDistorsion.compute


#include "../../Core/ShaderLibrary/Common.hlsl"
#include "../Material/Builtin/BuiltinData.hlsl"
TEXTURE2D(_DepthTexture);
TEXTURE2D(_DistortionDepthTexture);
TEXTURE2D(_GaussianPyramidColorTexture);
RW_TEXTURE2D(float4, _CameraColorTexture);

// Get distortion values
float4 encodedDistortion = LOAD_TEXTURE2D(_DistortionTexture, dispatchThreadId);
float depthRaw = LOAD_TEXTURE2D(_DistortionDepthTexture, dispatchThreadId).r;
float2 distortionBlur;
DecodeDistortion(encodedDistortion, distortion, distortionBlur);
float distortionBlur;
bool distortionIsSourceValid;
DecodeDistortion(encodedDistortion, distortion, distortionBlur, distortionIsSourceValid);
float2 distordedDistortionBlur;
float distordedDistortionBlur;
bool distordedIsSourceValid;
float distordedDepthRaw = LOAD_TEXTURE2D(_DepthTexture, distortedEncodedDistortionId).r;
DecodeDistortion(encodedDistordedDistortion, distordedDistortion, distordedDistortionBlur);
DecodeDistortion(encodedDistordedDistortion, distordedDistortion, distordedDistortionBlur, distordedIsSourceValid);
if (distordedDepthRaw > depthRaw)
return;
// We apply distortion only if both this pixel and source pixel are behind any distortion object
if (distortionIsSourceValid && distordedIsSourceValid)
{
// Get source pixel for distortion
float2 distordedUV = float2(dispatchThreadId + int2(distortion * _FetchBias)) * _Size.zw;
float mip = (_GaussianPyramidColorMipSize.z - 1) * clamp(distortionBlur, 0.0, 1.0);
float4 sampled = SAMPLE_TEXTURE2D_LOD(_GaussianPyramidColorTexture, sampler_GaussianPyramidColorTexture, distordedUV, mip);
// Get source pixel for distortion
float2 distordedUV = float2(dispatchThreadId + int2(distortion * _FetchBias)) * _Size.zw;
float mip = (_GaussianPyramidColorMipSize.z - 1) * clamp(distortionBlur.x, 0.0, 1.0);
float4 sampled = SAMPLE_TEXTURE2D_LOD(_GaussianPyramidColorTexture, sampler_GaussianPyramidColorTexture, distordedUV, mip);
_CameraColorTexture[dispatchThreadId] = sampled;
_CameraColorTexture[dispatchThreadId] = sampled;
}
}

3
ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDistortion.hlsl


GetSurfaceAndBuiltinData(input, V, posInput, surfaceData, builtinData);
float4 outBuffer;
EncodeDistortion(builtinData.distortion, builtinData.distortionBlur, outBuffer);
// Mark this pixel as eligible as source for distortion
EncodeDistortion(builtinData.distortion, builtinData.distortionBlur, true, outBuffer);
return outBuffer;
}
正在加载...
取消
保存