浏览代码

(wip) Use depth to discard pixels in front of distortion object

/stochastic_alpha_test
Frédéric Vauchelles 7 年前
当前提交
ba8dc625
共有 12 个文件被更改,包括 81 次插入36 次删除
  1. 4
      SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Lit_Only_2DNoise.mat
  2. 12
      SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Lit_Only_Ripple.mat
  3. 2
      SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Lit_Reject.mat
  4. 8
      SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Unlit_2DNoise.mat
  5. 12
      SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Unlit_Only_2DNoise.mat
  6. 12
      SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Unlit_Only_Ripple.mat
  7. 12
      SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Unlit_Reject.mat
  8. 8
      SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Fire.mat
  9. 2
      ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs
  10. 10
      ScriptableRenderPipeline/HDRenderPipeline/Material/Builtin/BuiltinData.hlsl
  11. 30
      ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/ApplyDistorsion.compute
  12. 5
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDistortion.hlsl

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


- _DistortionBlendMode: 0
- _DistortionBlendOp: 0
- _DistortionBlurBlendMode: 0
- _DistortionBlurDstBlend: 0
- _DistortionBlurDstBlend: 1
- _DistortionBlurSrcBlend: 0
- _DistortionBlurSrcBlend: 1
- _DistortionDepthTest: 0
- _DistortionDstBlend: 1
- _DistortionEnable: 1

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


- _DetailAlbedoScale: 1
- _DetailNormalScale: 1
- _DetailSmoothnessScale: 1
- _DistortionBlendMode: 1
- _DistortionBlendMode: 0
- _DistortionBlurDstBlend: 0
- _DistortionBlurDstBlend: 1
- _DistortionBlurSrcBlend: 8
- _DistortionBlurSrcBlend: 1
- _DistortionDstBlend: 0
- _DistortionDstBlend: 1
- _DistortionScale: 0
- _DistortionSrcBlend: 2
- _DistortionScale: 1
- _DistortionSrcBlend: 1
- _DistortionStencilRef: 4
- _DoubleSidedEnable: 0
- _DoubleSidedNormalMode: 1

2
SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Lit_Reject.mat


- _DistortionBlurBlendMode: 0
- _DistortionBlurDstBlend: 0
- _DistortionBlurScale: 0
- _DistortionBlurSrcBlend: 0
- _DistortionBlurSrcBlend: 7
- _DistortionDepthTest: 0
- _DistortionDstBlend: 0
- _DistortionEnable: 1

8
SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Distorsion_Unlit_2DNoise.mat


- _DetailAlbedoScale: 1
- _DetailNormalScale: 1
- _DetailSmoothnessScale: 1
- _DistortionBlendMode: 0
- _DistortionBlurBlendMode: 0
- _DistortionBlurDstBlend: 1
- _DistortionBlurScale: 1
- _DistortionBlurSrcBlend: 1
- _DistortionDstBlend: 1
- _DistortionScale: 1
- _DistortionSrcBlend: 1
- _DistortionStencilRef: 4
- _DoubleSidedEnable: 0
- _DoubleSidedNormalMode: 1

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


- DepthOnly
- MOTIONVECTORS
- Forward
- ForwardOnlyOpaqueDepthOnly
- ForwardOnlyOpaque
- GBufferWithPrepass
- TransparentDepthPrepass
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:

- _DetailAlbedoScale: 1
- _DetailNormalScale: 1
- _DetailSmoothnessScale: 1
- _DistortionBlendMode: 0
- _DistortionBlurBlendMode: 0
- _DistortionBlurDstBlend: 1
- _DistortionBlurScale: 1
- _DistortionBlurSrcBlend: 1
- _DistortionDstBlend: 1
- _DistortionScale: 1
- _DistortionSrcBlend: 1
- _DistortionStencilRef: 4
- _DoubleSidedEnable: 0
- _DoubleSidedNormalMode: 1

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


- DepthOnly
- MOTIONVECTORS
- Forward
- ForwardOnlyOpaqueDepthOnly
- ForwardOnlyOpaque
- GBufferWithPrepass
- TransparentDepthPrepass
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:

- _DetailAlbedoScale: 1
- _DetailNormalScale: 1
- _DetailSmoothnessScale: 1
- _DistortionBlendMode: 0
- _DistortionBlurBlendMode: 0
- _DistortionBlurDstBlend: 1
- _DistortionBlurScale: 1
- _DistortionBlurSrcBlend: 1
- _DistortionDstBlend: 1
- _DistortionScale: 1
- _DistortionSrcBlend: 1
- _DistortionStencilRef: 4
- _DoubleSidedEnable: 0
- _DoubleSidedNormalMode: 1

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


- DepthOnly
- MOTIONVECTORS
- Forward
- ForwardOnlyOpaqueDepthOnly
- ForwardOnlyOpaque
- GBufferWithPrepass
- TransparentDepthPrepass
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:

- _DetailAlbedoScale: 1
- _DetailNormalScale: 1
- _DetailSmoothnessScale: 1
- _DistortionBlendMode: 1
- _DistortionBlurBlendMode: 0
- _DistortionBlurDstBlend: 0
- _DistortionBlurScale: 0
- _DistortionBlurSrcBlend: 7
- _DistortionDstBlend: 0
- _DistortionScale: 0
- _DistortionSrcBlend: 2
- _DistortionStencilRef: 0
- _DoubleSidedEnable: 0
- _DoubleSidedNormalMode: 1

8
SampleScenes/HDTest/GraphicTest/Common/Material/Mat_Fire.mat


- _DetailAlbedoScale: 1
- _DetailNormalScale: 1
- _DetailSmoothnessScale: 1
- _DistortionBlendMode: 0
- _DistortionBlendMode: 1
- _DistortionBlurSrcBlend: 0
- _DistortionBlurSrcBlend: 7
- _DistortionDstBlend: 1
- _DistortionDstBlend: 0
- _DistortionSrcBlend: 1
- _DistortionSrcBlend: 2
- _DistortionStencilRef: 4
- _DoubleSidedEnable: 0
- _DoubleSidedNormalMode: 1

2
ScriptableRenderPipeline/HDRenderPipeline/HDRenderPipeline.cs


cmd.SetComputeTextureParam(resources.applyDistortionCS, resources.applyDistortionKernel, HDShaderIDs._DistortionTexture, m_DistortionBufferRT);
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());
cmd.SetComputeVectorParam(resources.applyDistortionCS, HDShaderIDs._ZBufferParams, Shader.GetGlobalVector(HDShaderIDs._ZBufferParams));
cmd.SetComputeVectorParam(resources.applyDistortionCS, HDShaderIDs._GaussianPyramidColorMipSize, Shader.GetGlobalVector(HDShaderIDs._GaussianPyramidColorMipSize));
cmd.DispatchCompute(resources.applyDistortionCS, resources.applyDistortionKernel, (int)(size.x) / (int)x, (int)(size.y) / (int)y, 1);
}

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


velocity = inBuffer.xy;
}
void EncodeDistortion(float2 distortion, float distortionBlur, bool validSourceForDistortion, out float4 outBuffer)
void EncodeDistortion(float2 distortion, float distortionBlur, float depthRaw, out float4 outBuffer)
// Tag pixel as a pixel that can be used as source for distortion (b != 0)
// It is compatible with Add blend mode (a pixel cannot be a valid distortion source only if there is no distorting object in front of it)
outBuffer = float4(distortion, validSourceForDistortion ? 1.0 : 0.0, distortionBlur);
outBuffer = float4(distortion, depthRaw, distortionBlur);
void DecodeDistortion(float4 inBuffer, out float2 distortion, out float2 distortionBlur, out bool validSourceForDistortion)
void DecodeDistortion(float4 inBuffer, out float2 distortion, out float2 distortionBlur, out float depthRaw)
validSourceForDistortion = inBuffer.z != 0.0;
depthRaw = inBuffer.z;
}
void GetBuiltinDataDebug(uint paramId, BuiltinData builtinData, inout float3 result, inout bool needLinearToSRGB)

30
ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/ApplyDistorsion.compute


#include "../../Core/ShaderLibrary/Common.hlsl"
#include "../Material/Builtin/BuiltinData.hlsl"
#define DISTORTIONENABLED_STENCILBIT 4
Texture2D<float4> _DistortionTexture;
Texture2D<float4> _GaussianPyramidColorTexture;
RWTexture2D<float4> _CameraColorTexture;
TEXTURE2D(_DepthTexture);
TEXTURE2D(_DistortionTexture);
TEXTURE2D(_GaussianPyramidColorTexture);
RW_TEXTURE2D(float4, _CameraColorTexture);
SamplerState sampler_GaussianPyramidColorTexture;

const float _FetchBias = 0.9;
// Get distortion values
float4 encodedDistortion = _DistortionTexture.Load(int3(dispatchThreadId, 0));
float4 encodedDistortion = LOAD_TEXTURE2D(_DistortionTexture, dispatchThreadId);
bool validForDistortion;
DecodeDistortion(encodedDistortion, distortion, distortionBlur, validForDistortion);
float depthRaw;
DecodeDistortion(encodedDistortion, distortion, distortionBlur, depthRaw);
// Get distortion source pixel mask (stored in _DistortionTexture.z)
// Discard pixels that are in front of the distortion object
float distordedDepth = LOAD_TEXTURE2D(_DepthTexture, distortedEncodedDistortionId).r;
/*if (distordedDepth > depthRaw)
return;*/
bool distordedValidForDistortion;
float4 encodedDistordedDistortion = _DistortionTexture.Load(int3(distortedEncodedDistortionId, 0));
DecodeDistortion(encodedDistordedDistortion, distordedDistortion, distordedDistortionBlur, distordedValidForDistortion);
float distordedObjectDepth;
float4 encodedDistordedDistortion = LOAD_TEXTURE2D(_DistortionTexture, distortedEncodedDistortionId);
DecodeDistortion(encodedDistordedDistortion, distordedDistortion, distordedDistortionBlur, distordedObjectDepth);
float4 sampled = _GaussianPyramidColorTexture.SampleLevel(sampler_GaussianPyramidColorTexture, distordedUV, mip);
float4 sampled = SAMPLE_TEXTURE2D_LOD(_GaussianPyramidColorTexture, sampler_GaussianPyramidColorTexture, distordedUV, mip);
_CameraColorTexture[dispatchThreadId] = sampled;
}

5
ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDistortion.hlsl


GetSurfaceAndBuiltinData(input, V, posInput, surfaceData, builtinData);
float4 outBuffer;
// We do not use pixel outside of distorted objects, so we use masking to reject invalid pixels
// https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter19.html (refraction mask)
// true = tag this pixel as a valid distortion pixel source
EncodeDistortion(builtinData.distortion, builtinData.distortionBlur, true, outBuffer);
EncodeDistortion(builtinData.distortion, builtinData.distortionBlur, posInput.depthRaw, outBuffer);
return outBuffer;
}
正在加载...
取消
保存