浏览代码

Added distortion masking

/stochastic_alpha_test
Frédéric Vauchelles 7 年前
当前提交
fcadaafc
共有 3 个文件被更改,包括 38 次插入14 次删除
  1. 9
      ScriptableRenderPipeline/HDRenderPipeline/Material/Builtin/BuiltinData.hlsl
  2. 38
      ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/ApplyDistorsion.compute
  3. 5
      ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDistortion.hlsl

9
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 validSourceForDistortion, out float4 outBuffer)
outBuffer = float4(distortion, 0.0, distortionBlur);
// Tag pixel as a pixel that can be used as source for distortion (b != 0)
// It is compatible with blend mode: either Add or Multiply
outBuffer = float4(distortion, validSourceForDistortion ? 1.0 : 0.0, distortionBlur);
void DecodeDistortion(float4 inBuffer, out float2 distortion, out float2 distortionBlur)
void DecodeDistortion(float4 inBuffer, out float2 distortion, out float2 distortionBlur, out bool validSourceForDistortion)
validSourceForDistortion = inBuffer.z > 0.1 || inBuffer.z < 0.1;
}
void GetBuiltinDataDebug(uint paramId, BuiltinData builtinData, inout float3 result, inout bool needLinearToSRGB)

38
ScriptableRenderPipeline/HDRenderPipeline/RenderPipelineResources/ApplyDistorsion.compute


#include "../Material/Builtin/BuiltinData.hlsl"
Texture2D<float4> _DistortionTexture;
Texture2D<float> _PyramidDepthTexture;
SamplerState sampler_PyramidDepthTexture;
CBUFFER_START(cb)
float4 _Size;

[numthreads(8, 8, 1)]
void KMain(uint2 groupId : SV_GroupID, uint2 groupThreadId : SV_GroupThreadID, uint2 dispatchThreadId : SV_DispatchThreadID)
{
float4 encodedDistorsion = _DistortionTexture.Load(int3(dispatchThreadId, 0));
float2 distorsion;
float2 distorsionBlur;
DecodeDistortion(encodedDistorsion, distorsion, distorsionBlur);
// We use a bias when fetching distortion source pixels
// This avoid artifacts when a distortion is overlapped by an opaque object
const float _FetchBias = 0.9;
float2 distordedUV = (float2(dispatchThreadId) + distorsion) * _Size.zw;
float mip = (_GaussianPyramidColorMipSize.z - 1) * clamp(distorsionBlur.x, 0.0, 1.0);
float4 sampled = _GaussianPyramidColorTexture.SampleLevel(sampler_GaussianPyramidColorTexture, distordedUV, mip);
// Get distortion values
float4 encodedDistortion = _DistortionTexture.Load(int3(dispatchThreadId, 0));
_CameraColorTexture[dispatchThreadId] = sampled;
float2 distortion;
float2 distortionBlur;
bool validForDistortion;
DecodeDistortion(encodedDistortion, distortion, distortionBlur, validForDistortion);
// Get distortion source pixel mask (stored in _DistortionTexture.z)
uint2 distortedEncodedDistortionId = dispatchThreadId + uint2(distortion);
float2 distordedDistortion;
float2 distordedDistortionBlur;
bool distordedValidForDistortion;
float4 encodedDistordedDistortion = _DistortionTexture.Load(int3(distortedEncodedDistortionId, 0));
DecodeDistortion(encodedDistordedDistortion, distordedDistortion, distordedDistortionBlur, distordedValidForDistortion);
if (distordedValidForDistortion && validForDistortion)
{
// Get source pixel for distortion
float2 distordedUV = float2(dispatchThreadId + uint2(distortion * _FetchBias)) * _Size.zw;
float mip = (_GaussianPyramidColorMipSize.z - 1) * clamp(distortionBlur.x, 0.0, 1.0);
float4 sampled = _GaussianPyramidColorTexture.SampleLevel(sampler_GaussianPyramidColorTexture, distordedUV, mip);
_CameraColorTexture[dispatchThreadId] = sampled;
}
}

5
ScriptableRenderPipeline/HDRenderPipeline/ShaderPass/ShaderPassDistortion.hlsl


GetSurfaceAndBuiltinData(input, V, posInput, surfaceData, builtinData);
float4 outBuffer;
EncodeDistortion(builtinData.distortion, builtinData.distortionBlur, 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);
return outBuffer;
}
正在加载...
取消
保存