RenderTargetIdentifier [ ] m_ColorMRTs ;
RenderTargetIdentifier [ ] m_RTIDs = new RenderTargetIdentifier [ k_MaxSSSBuffer ] ;
// Disney SSS Model
ComputeShader m_SubsurfaceScatteringCS ;
int m_SubsurfaceScatteringKernel ;
Material m_CombineLightingPass ;
readonly int m_SSSHTileBuffer ;
readonly RenderTargetIdentifier m_SSSHTileBufferRT ;
// End Disney SSS Model
// Jimenez SSS Model
Material m_SssVerticalFilterPass ;
Material m_SssHorizontalFilterAndCombinePass ;
// Jimenez on some platform need an extra buffer
readonly int m_CameraFilteringBuffer ;
readonly RenderTargetIdentifier m_CameraFilteringBufferRT ;
// End Jimenez SSS Model
// This is use to be able to read stencil value in compute shader
Material m_CopyStencilForSplitLighting ;
SubsurfaceScatteringSettings m_sssSettings ;
// Use with Disney
m_SSSHTileBuffer = HDShaderIDs . _SSSHTile ;
m_SSSHTileBufferRT = new RenderTargetIdentifier ( m_SSSHTileBuffer ) ;
// Use with Jimenez
m_CameraFilteringBuffer = HDShaderIDs . _CameraFilteringBuffer ;
m_CameraFilteringBufferRT = new RenderTargetIdentifier ( m_CameraFilteringBuffer ) ;
public void InitGBuffers ( int width , int height , GBufferManager gbufferManager , CommandBuffer cmd )
public void InitSSSBuffersFromGBuffer ( int width , int height , GBufferManager gbufferManager , CommandBuffer cmd )
{
m_RTIDs [ 0 ] = gbufferManager . GetGBuffers ( ) [ 0 ] ;
}
public void InitGBuffers ( int width , int height , CommandBuffer cmd )
public void InitSSSBuffers ( int width , int height , CommandBuffer cmd )
cmd . GetTemporaryRT ( m_SSSBuffer0 , width , height , 0 , FilterMode . Point , RenderTextureFormat . ARGB32 , RenderTextureReadWrite . Linear ) ;
cmd . GetTemporaryRT ( m_SSSBuffer0 , width , height , 0 , FilterMode . Point , RenderTextureFormat . ARGB32 , RenderTextureReadWrite . Linear ) ;
}
public RenderTargetIdentifier GetSSSBuffers ( int index )
}
public void CreateSssMaterials ( )
public void Build ( RenderPipelineResources renderPipelineResources , SubsurfaceScatteringSettings sssSettings )
m_sssSettings = sssSettings ;
// Disney SSS (compute + combine)
m_SubsurfaceScatteringCS = renderPipelineResources . subsurfaceScatteringCS ;
CoreUtils . Destroy ( m_CombineLightingPass ) ;
m_CombineLightingPass = CoreUtils . CreateEngineMaterial ( "Hidden/HDRenderPipeline/CombineLighting" ) ;
m_CombineLightingPass = CoreUtils . CreateEngineMaterial ( renderPipelineResources . combineLighting ) ;
// Old SSS Model >>>
CoreUtils . Destroy ( m_SssVerticalFilterPass ) ;
m_SssVerticalFilterPass = CoreUtils . CreateEngineMaterial ( "Hidden/HDRenderPipeline/SubsurfaceScattering" ) ;
// Jimenez SSS Model (shader)
m_SssVerticalFilterPass = CoreUtils . CreateEngineMaterial ( renderPipelineResources . subsurfaceScattering ) ;
CoreUtils . Destroy ( m_SssHorizontalFilterAndCombinePass ) ;
m_SssHorizontalFilterAndCombinePass = CoreUtils . CreateEngineMaterial ( "Hidden/HDRenderPipeline/SubsurfaceScattering" ) ;
m_SssHorizontalFilterAndCombinePass = CoreUtils . CreateEngineMaterial ( renderPipelineResources . subsurfaceScattering ) ;
// <<< Old SSS Model
m_CopyStencilForSplitLighting = CoreUtils . CreateEngineMaterial ( renderPipelineResources . copyStencilBuffer ) ;
m_CopyStencilForSplitLighting . SetInt ( HDShaderIDs . _StencilRef , ( int ) StencilLightingUsage . SplitLighting ) ;
}
public void Cleanup ( )
{
CoreUtils . Destroy ( m_CombineLightingPass ) ;
CoreUtils . Destroy ( m_SssVerticalFilterPass ) ;
CoreUtils . Destroy ( m_SssHorizontalFilterAndCombinePass ) ;
CoreUtils . Destroy ( m_CopyStencilForSplitLighting ) ;
}
bool NeedTemporarySubsurfaceBuffer ( )
{
// Typed UAV loads from FORMAT_R16G16B16A16_FLOAT is an optional feature of Direct3D 11.
// Most modern GPUs support it. We can avoid performing a costly copy in this case.
// TODO: test/implement for other platforms.
return SystemInfo . graphicsDeviceType ! = GraphicsDeviceType . PlayStation4 & &
SystemInfo . graphicsDeviceType ! = GraphicsDeviceType . XboxOne & &
SystemInfo . graphicsDeviceType ! = GraphicsDeviceType . XboxOneD3D12 ;
void SubsurfaceScatteringPass ( HDCamera hdCamera , CommandBuffer cmd , SubsurfaceScatteringSettings sssParameters )
public void SubsurfaceScatteringPass ( HDCamera hdCamera , CommandBuffer cmd , SubsurfaceScatteringSettings sssParameters , DebugDisplaySettings debugDisplaySettings ,
RenderTargetIdentifier colorBufferRT , RenderTargetIdentifier diffuseBufferRT , RenderTargetIdentifier depthStencilBufferRT , RenderTargetIdentifier depthTextureRT )
if ( ! m_CurrentDebugDisplaySettings . renderingDebugSettings . enableSSSAndTransmission )
if ( ! debugDisplaySettings . renderingDebugSettings . enableSSSAndTransmission )
using ( new ProfilingSample ( cmd , "Subsurface Scattering" , GetSampler ( CustomSamplerId . SubsurfaceScattering ) ) )
using ( new ProfilingSample ( cmd , "Subsurface Scattering" , HDRenderPipeline . GetSampler ( CustomSamplerId . SubsurfaceScattering ) ) )
if ( sssSettings . useDisneySSS )
if ( m_sssSettings . useDisneySSS )
using ( new ProfilingSample ( cmd , "HTile for SSS" , GetSampler ( CustomSamplerId . HTileForSSS ) ) )
int w = hdCamera . camera . pixelWidth ;
int h = hdCamera . camera . pixelHeight ;
using ( new ProfilingSample ( cmd , "HTile for SSS" , HDRenderPipeline . GetSampler ( CustomSamplerId . HTileForSSS ) ) )
CoreUtils . SetRenderTarget ( cmd , m_HTileRT , ClearFlag . Color , CoreUtils . clearColorAllBlack ) ;
// Currently, Unity does not offer a way to access the GCN HTile even on PS4 and Xbox One.
// Therefore, it's computed in a pixel shader, and optimized to only contain the SSS bit.
// Caution: must be same format as m_CameraSssDiffuseLightingBuffer
cmd . ReleaseTemporaryRT ( m_SSSHTileBuffer ) ;
// Note: DXGI_FORMAT_R8_UINT is not supported by Unity
// We use 8x8 tiles in order to match the native GCN HTile as closely as possible.
cmd . GetTemporaryRT ( m_SSSHTileBuffer , ( w + 7 ) / 8 , ( h + 7 ) / 8 , 0 , FilterMode . Point , RenderTextureFormat . R8 , RenderTextureReadWrite . Linear , 1 , true ) ; // Enable UAV
cmd . SetRandomWriteTarget ( 1 , GetHTile ( ) ) ;
CoreUtils . SetRenderTarget ( cmd , m_SSSHTileBufferRT , ClearFlag . Color , CoreUtils . clearColorAllBlack ) ;
cmd . SetRandomWriteTarget ( 1 , m_SSSHTileBufferRT ) ;
CoreUtils . DrawFullScreen ( cmd , m_CopyStencilForSplitLighting , m_CameraStencilBufferCopyRT , m_CameraDepthStencilBufferRT , null , 2 ) ;
CoreUtils . SetRenderTarget ( cmd , depthStencilBufferRT ) ; // No need for color buffer here
CoreUtils . DrawFullScreen ( cmd , m_CopyStencilForSplitLighting , null , 2 ) ;
cmd . ClearRandomWriteTargets ( ) ;
}
cmd . SetComputeVectorArrayParam ( m_SubsurfaceScatteringCS , HDShaderIDs . _FilterKernels , sssParameters . filterKernels ) ;
cmd . SetComputeVectorArrayParam ( m_SubsurfaceScatteringCS , HDShaderIDs . _ShapeParams , sssParameters . shapeParams ) ;
cmd . SetComputeTextureParam ( m_SubsurfaceScatteringCS , m_SubsurfaceScatteringKernel , HDShaderIDs . _DepthTexture , GetDepthTexture ( ) ) ;
cmd . SetComputeTextureParam ( m_SubsurfaceScatteringCS , m_SubsurfaceScatteringKernel , HDShaderIDs . _HTile , GetHTile ( ) ) ;
cmd . SetComputeTextureParam ( m_SubsurfaceScatteringCS , m_SubsurfaceScatteringKernel , HDShaderIDs . _IrradianceSource , m_CameraSssDiffuseLightingBufferRT ) ;
cmd . SetComputeTextureParam ( m_SubsurfaceScatteringCS , m_SubsurfaceScatteringKernel , HDShaderIDs . _DepthTexture , depthTextureRT ) ;
cmd . SetComputeTextureParam ( m_SubsurfaceScatteringCS , m_SubsurfaceScatteringKernel , HDShaderIDs . _SSSHTile , m_SSSHTileBufferRT ) ;
cmd . SetComputeTextureParam ( m_SubsurfaceScatteringCS , m_SubsurfaceScatteringKernel , HDShaderIDs . _IrradianceSource , diffuseBufferRT ) ;
for ( int i = 0 ; i < m_SSSBufferManager . sssBufferCount ; + + i )
for ( int i = 0 ; i < sssBufferCount ; + + i )
cmd . SetComputeTextureParam ( m_SubsurfaceScatteringCS , m_SubsurfaceScatteringKernel , HDShaderIDs . _SSSBufferTexture [ i ] , m_SSSBufferManager . GetSSSBuffers ( i ) ) ;
cmd . SetComputeTextureParam ( m_SubsurfaceScatteringCS , m_SubsurfaceScatteringKernel , HDShaderIDs . _SSSBufferTexture [ i ] , GetSSSBuffers ( i ) ) ;
// Caution: must be same format as m_CameraSssDiffuseLightingBuffer
cmd . ReleaseTemporaryRT ( m_CameraFilteringBuffer ) ;
cmd . GetTemporaryRT ( m_CameraFilteringBuffer , w , h , 0 , FilterMode . Point , RenderTextureFormat . RGB111110Float , RenderTextureReadWrite . Linear , 1 , true ) ; // Enable UAV
// Clear the SSS filtering target
using ( new ProfilingSample ( cmd , "Clear SSS filtering target" , HDRenderPipeline . GetSampler ( CustomSamplerId . ClearSSSFilteringTarget ) ) )
{
CoreUtils . SetRenderTarget ( cmd , m_CameraFilteringBufferRT , ClearFlag . Color , CoreUtils . clearColorAllBlack ) ;
}
// We dispatch 4x swizzled 16x16 groups per a 32x32 macrotile.
// We dispatch 4x swizzled 16x16 groups per a 32x32 macro tile.
CoreUtils . DrawFullScreen ( cmd , m_CombineLightingPass , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT ) ;
CoreUtils . DrawFullScreen ( cmd , m_CombineLightingPass , colorBufferRT , depthStencilBufferRT ) ;
cmd . SetComputeTextureParam ( m_SubsurfaceScatteringCS , m_SubsurfaceScatteringKernel , HDShaderIDs . _CameraColorTexture , m_CameraColorBufferRT ) ;
cmd . SetComputeTextureParam ( m_SubsurfaceScatteringCS , m_SubsurfaceScatteringKernel , HDShaderIDs . _CameraColorTexture , colorBufferRT ) ;
// Perform the SSS filtering pass which performs an in-place update of 'm_CameraColorBufferRT'.
// We dispatch 4x swizzled 16x16 groups per a 32x32 macrotile.
// Perform the SSS filtering pass which performs an in-place update of 'colorBuffer'.
// We dispatch 4x swizzled 16x16 groups per a 32x32 macro tile.
for ( int i = 0 ; i < m_SSSBufferManager . sssBufferCount ; + + i )
for ( int i = 0 ; i < sssBufferCount ; + + i )
cmd . SetGlobalTexture ( HDShaderIDs . _SSSBufferTexture [ i ] , m_SSSBufferManager . GetSSSBuffers ( i ) ) ;
cmd . SetGlobalTexture ( HDShaderIDs . _SSSBufferTexture [ i ] , GetSSSBuffers ( i ) ) ;
cmd . SetGlobalTexture ( HDShaderIDs . _IrradianceSource , m_CameraSssDiffuseLightingBufferRT ) ; // Cannot set a RT on a material
cmd . SetGlobalTexture ( HDShaderIDs . _IrradianceSource , diffuseBufferRT ) ; // Cannot set a RT on a material
CoreUtils . DrawFullScreen ( cmd , m_SssVerticalFilterPass , m_CameraFilteringBufferRT , m_CameraDepthStencilBufferRT ) ;
CoreUtils . DrawFullScreen ( cmd , m_SssVerticalFilterPass , m_CameraFilteringBufferRT , depthStencilBufferRT ) ;
CoreUtils . DrawFullScreen ( cmd , m_SssHorizontalFilterAndCombinePass , m_CameraColorBufferRT , m_CameraDepthStencilBufferRT ) ;
CoreUtils . DrawFullScreen ( cmd , m_SssHorizontalFilterAndCombinePass , colorBufferRT , depthStencilBufferRT ) ;
}
}
}