浏览代码

Move V-buffer sampling to AtmosphericScattering.hlsl

/Yibing-Project-2
Evgenii Golubev 7 年前
当前提交
fc06d6c6
共有 9 个文件被更改,包括 51 次插入74 次删除
  1. 51
      ScriptableRenderPipeline/Core/ShaderLibrary/VolumeRendering.hlsl
  2. 1
      ScriptableRenderPipeline/HDRenderPipeline/HDStringConstants.cs
  3. 14
      ScriptableRenderPipeline/HDRenderPipeline/Lighting/Deferred.shader
  4. 14
      ScriptableRenderPipeline/HDRenderPipeline/Lighting/LightLoop/Deferred.compute
  5. 6
      ScriptableRenderPipeline/HDRenderPipeline/Lighting/Volumetrics/Resources/VolumetricLighting.compute
  6. 4
      ScriptableRenderPipeline/HDRenderPipeline/Lighting/Volumetrics/VolumetricLighting.cs
  7. 24
      ScriptableRenderPipeline/HDRenderPipeline/Sky/AtmosphericScattering/AtmosphericScattering.hlsl
  8. 9
      ScriptableRenderPipeline/HDRenderPipeline/Sky/HDRISky/Resources/SkyHDRI.shader
  9. 2
      ScriptableRenderPipeline/HDRenderPipeline/Sky/OpaqueAtmosphericScattering.shader

51
ScriptableRenderPipeline/Core/ShaderLibrary/VolumeRendering.hlsl


#define VBUFFER_SLICE_COUNT 128
#endif // PRESET_ULTRA
// Returns {volumetric radiance, opacity}.
float4 GetInScatteredRadianceAndTransmittance(float2 positionNDC, float linearDepth,
TEXTURE3D_ARGS(VBufferLighting, linearClampSampler),
float2 VBufferScale, float4 VBufferDepthEncodingParams)

float d = EncodeLogarithmicDepth(z, VBufferDepthEncodingParams);
// We cannot use hardware trilinear interpolation since the distance between slices is log-encoded.
// Therefore, we perform 2 bilinear taps.
// TODO: test the visual difference in practice.
float s0 = clamp(floor(d * n - 0.5), 0, n - 1); // TODO: somehow avoid the clamp...
float s1 = clamp( ceil(d * n - 0.5), 0, n - 1); // TODO: somehow avoid the clamp...
float d0 = s0 * rcp(n) + (0.5 * rcp(n));
float d1 = s1 * rcp(n) + (0.5 * rcp(n));
float z0 = DecodeLogarithmicDepth(d0, VBufferDepthEncodingParams);
float z1 = DecodeLogarithmicDepth(d1, VBufferDepthEncodingParams);
// The sampler should clamp to edge.
float4 L0 = SAMPLE_TEXTURE3D_LOD(VBufferLighting, linearClampSampler, float3(uv, d0), 0);
float4 L1 = SAMPLE_TEXTURE3D_LOD(VBufferLighting, linearClampSampler, float3(uv, d1), 0);
float4 L = lerp(L0, L1, saturate((z - z0) / (z1 - z0)));
float4 L;
return float4(L.rgb, Transmittance(L.a));
}
if (d == 1)
{
// We are behind the far plane of the V-buffer.
// The sampler should clamp to edge.
L = SAMPLE_TEXTURE3D_LOD(VBufferLighting, linearClampSampler, float3(uv, 1), 0);
}
else
{
// We cannot use hardware trilinear interpolation since the distance between slices is log-encoded.
// Therefore, we perform 2 bilinear taps.
// TODO: test the visual difference in practice.
float s0 = floor(d * n - 0.5);
float s1 = ceil( d * n - 0.5);
float d0 = s0 * rcp(n) + (0.5 * rcp(n));
float d1 = s1 * rcp(n) + (0.5 * rcp(n));
float z0 = DecodeLogarithmicDepth(d0, VBufferDepthEncodingParams);
float z1 = DecodeLogarithmicDepth(d1, VBufferDepthEncodingParams);
// A version without depth - returns the value for the far plane.
float4 GetInScatteredRadianceAndTransmittance(float2 positionNDC,
TEXTURE3D_ARGS(VBufferLighting, linearClampSampler),
float2 VBufferScale)
{
// Account for the visible area of the VBuffer.
float2 uv = positionNDC * VBufferScale;
// The sampler should clamp to edge.
float4 L0 = SAMPLE_TEXTURE3D_LOD(VBufferLighting, linearClampSampler, float3(uv, d0), 0);
float4 L1 = SAMPLE_TEXTURE3D_LOD(VBufferLighting, linearClampSampler, float3(uv, d1), 0);
// The sampler should clamp to edge.
float4 L = SAMPLE_TEXTURE3D_LOD(VBufferLighting, linearClampSampler, float3(uv, 1), 0);
L = lerp(L0, L1, saturate((z - z0) / (z1 - z0)));
}
return float4(L.rgb, Transmittance(L.a));
return float4(L.rgb, 1 - Transmittance(L.a));
}
#endif // UNITY_VOLUME_RENDERING_INCLUDED

1
ScriptableRenderPipeline/HDRenderPipeline/HDStringConstants.cs


public static readonly int _VBufferCoordToViewDirWS = Shader.PropertyToID("_VBufferCoordToViewDirWS");
public static readonly int _VBufferDensity = Shader.PropertyToID("_VBufferDensity");
public static readonly int _VBufferLighting = Shader.PropertyToID("_VBufferLighting");
public static readonly int _VBufferLightingCurr = Shader.PropertyToID("_VBufferLightingCurr");
public static readonly int _VBufferLightingPrev = Shader.PropertyToID("_VBufferLightingPrev");
}
}

14
ScriptableRenderPipeline/HDRenderPipeline/Lighting/Deferred.shader


#ifdef SHADOWS_SHADOWMASK
TEXTURE2D(_ShadowMaskTexture);
#endif
#ifdef VOLUMETRIC_LIGHTING_ENABLED
TEXTURE3D(_VBufferLighting);
#endif
struct Attributes
{

float3 diffuseLighting;
float3 specularLighting;
LightLoop(V, posInput, preLightData, bsdfData, bakeLightingData, LIGHT_FEATURE_MASK_FLAGS_OPAQUE, diffuseLighting, specularLighting);
#ifdef VOLUMETRIC_LIGHTING_ENABLED
float4 volumetricLighting = GetInScatteredRadianceAndTransmittance(posInput.positionNDC, posInput.linearDepth,
TEXTURE3D_PARAM(_VBufferLighting, s_linear_clamp_sampler),
_VBufferResolutionAndScale.zw,
_VBufferDepthEncodingParams);
// TODO: apply volumetrics after SSS.
diffuseLighting *= volumetricLighting.a;
specularLighting *= volumetricLighting.a;
specularLighting += volumetricLighting.rgb;
#endif
Outputs outputs;

14
ScriptableRenderPipeline/HDRenderPipeline/Lighting/LightLoop/Deferred.compute


#ifdef SHADOWS_SHADOWMASK
TEXTURE2D(_ShadowMaskTexture);
#endif
#ifdef VOLUMETRIC_LIGHTING_ENABLED
TEXTURE3D(_VBufferLighting);
#endif
RWTexture2D<float3> diffuseLightingUAV;
RWTexture2D<float4> specularLightingUAV;

float3 diffuseLighting;
float3 specularLighting;
LightLoop(V, posInput, preLightData, bsdfData, bakeLightingData, featureFlags, diffuseLighting, specularLighting);
#ifdef VOLUMETRIC_LIGHTING_ENABLED
float4 volumetricLighting = GetInScatteredRadianceAndTransmittance(posInput.positionNDC, posInput.linearDepth,
TEXTURE3D_PARAM(_VBufferLighting, s_linear_clamp_sampler),
_VBufferResolutionAndScale.zw,
_VBufferDepthEncodingParams);
// TODO: apply volumetrics after SSS.
diffuseLighting *= volumetricLighting.a;
specularLighting *= volumetricLighting.a;
specularLighting += volumetricLighting.rgb;
#endif
if (_EnableSSSAndTransmission != 0 && bsdfData.materialId == MATERIALID_LIT_SSS && HasMaterialFeatureFlag(MATERIALFEATUREFLAGS_LIT_SSS))
{

6
ScriptableRenderPipeline/HDRenderPipeline/Lighting/Volumetrics/Resources/VolumetricLighting.compute


// Inputs & outputs
//--------------------------------------------------------------------------------------------------
RW_TEXTURE3D(float4, _VBufferLighting); // RGB = radiance, A = optical depth
TEXTURE3D(_VBufferLightingPrev); // RGB = radiance, A = optical depth
RW_TEXTURE3D(float4, _VBufferLightingCurr); // RGB = radiance, A = optical depth
TEXTURE3D(_VBufferLightingCurrPrev); // RGB = radiance, A = optical depth
CBUFFER_START(UnityVolumetricLighting)
float4x4 _VBufferCoordToViewDirWS; // Actually just 3x3, but Unity can only set 4x4

opticalDepth += 0.5 * extinction * dt;
// Store the voxel data. TODO: reprojection of 'tc' (or 'centerWS').
_VBufferLighting[uint3(posInput.positionSS, slice)] = float4(totalRadiance, opticalDepth);
_VBufferLightingCurr[uint3(posInput.positionSS, slice)] = float4(totalRadiance, opticalDepth);
// Compute the optical depth up to the end of the interval.
opticalDepth += 0.5 * extinction * dt;

4
ScriptableRenderPipeline/HDRenderPipeline/Lighting/Volumetrics/VolumetricLighting.cs


// TODO: set 'm_VolumetricLightingPreset'.
cmd.SetComputeMatrixParam( m_VolumetricLightingCS, HDShaderIDs._VBufferCoordToViewDirWS, transform);
cmd.SetComputeTextureParam(m_VolumetricLightingCS, kernel, HDShaderIDs._VBufferLighting, m_VBufferLightingRT[0]);
cmd.SetComputeTextureParam(m_VolumetricLightingCS, kernel, HDShaderIDs._VBufferLightingPrev, m_VBufferLightingRT[1]);
cmd.SetComputeTextureParam(m_VolumetricLightingCS, kernel, HDShaderIDs._VBufferLightingCurr, m_VBufferLightingRT[0]);
cmd.SetComputeTextureParam(m_VolumetricLightingCS, kernel, HDShaderIDs._VBufferLightingPrev, m_VBufferLightingRT[1]);
// The shader defines GROUP_SIZE_1D = 16.
cmd.DispatchCompute(m_VolumetricLightingCS, kernel, (w + 15) / 16, (h + 15) / 16, 1);

24
ScriptableRenderPipeline/HDRenderPipeline/Sky/AtmosphericScattering/AtmosphericScattering.hlsl


#include "../SkyVariables.hlsl"
#include "../../ShaderVariables.hlsl"
#ifdef VOLUMETRIC_LIGHTING_ENABLED
TEXTURE3D(_VBufferLighting);
#endif
CBUFFER_START(AtmosphericScattering)
float _AtmosphericScatteringType;
// Common

// Returns fog color in rgb and fog factor in alpha.
float4 EvaluateAtmosphericScattering(PositionInputs posInput)
{
#ifdef VOLUMETRIC_LIGHTING_ENABLED
return GetInScatteredRadianceAndTransmittance(posInput.positionNDC, posInput.linearDepth,
TEXTURE3D_PARAM(_VBufferLighting, s_linear_clamp_sampler),
_VBufferResolutionAndScale.zw,
_VBufferDepthEncodingParams);
#endif
float3 fogColor; float fogFactor;
float3 fogColor = GetFogColor(posInput);
float fogFactor = _FogDensity * (1.0f - TransmittanceHomogeneousMedium(1.0f / _ExpFogDistance, posInput.linearDepth));
return float4(fogColor, fogFactor);
fogColor = GetFogColor(posInput);
fogFactor = _FogDensity * (1.0f - TransmittanceHomogeneousMedium(1.0f / _ExpFogDistance, posInput.linearDepth));
float3 fogColor = GetFogColor(posInput);
float fogFactor = _FogDensity * saturate((posInput.linearDepth - _LinearFogStart) * _LinearFogOneOverRange);
fogColor = GetFogColor(posInput);
fogFactor = _FogDensity * saturate((posInput.linearDepth - _LinearFogStart) * _LinearFogOneOverRange);
return float4(fogColor, fogFactor);
}
else // NONE

return float4(fogColor * fogFactor, fogFactor); // Premultiplied alpha
}

9
ScriptableRenderPipeline/HDRenderPipeline/Sky/HDRISky/Resources/SkyHDRI.shader


float3 skyColor = ClampToFloat16Max(SAMPLE_TEXTURECUBE_LOD(_Cubemap, s_linear_clamp_sampler, dir, 0).rgb * exp2(_SkyParam.x) * _SkyParam.y);
#ifdef VOLUMETRIC_LIGHTING_ENABLED
PositionInputs posInput = GetPositionInput(input.positionCS.xy, _ScreenSize.zw);
float4 volumetricLighting = GetInScatteredRadianceAndTransmittance(posInput.positionNDC,
TEXTURE3D_PARAM(_VBufferLighting, s_linear_clamp_sampler),
_VBufferResolutionAndScale.zw);
skyColor *= volumetricLighting.a;
skyColor += volumetricLighting.rgb;
#endif
return float4(skyColor, 1.0);
}

2
ScriptableRenderPipeline/HDRenderPipeline/Sky/OpaqueAtmosphericScattering.shader


Cull Off
ZTest Always
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Blend One OneMinusSrcAlpha // Premultiplied Alpha
HLSLPROGRAM
#pragma target 4.5

正在加载...
取消
保存