internal static readonly int _AOBuffer = Shader . PropertyToID ( "_AmbientOcclusionTexture" ) ;
internal static readonly int _TempTex1 = Shader . PropertyToID ( "_TempTex1" ) ;
internal static readonly int _TempTex2 = Shader . PropertyToID ( "_TempTex2" ) ;
internal static readonly int _CameraDepthTexture = Shader . PropertyToID ( "_CameraDepthTexture" ) ;
CommandBuffer m_Command ;
// For the AO buffer, use R8 or RHalf if available.
static RenderTextureFormat GetAOBufferFormat ( )
{
if ( SystemInfo . SupportsRenderTextureFormat ( RenderTextureFormat . R8 ) )
return RenderTextureFormat . R8 ;
if ( SystemInfo . SupportsRenderTextureFormat ( RenderTextureFormat . RHalf ) )
return RenderTextureFormat . RHalf ;
return RenderTextureFormat . Default ;
}
public ScreenSpaceAmbientOcclusionEffect ( )
{ }
m_Material . hideFlags = HideFlags . DontSave ;
}
public void Render ( ScreenSpaceAmbientOcclusionSettings . Settings settings , HDRenderPipeline hdRP , HDCamera hdCamera , ScriptableRenderContext renderContext , RenderTargetIdentifier depthID , bool isForward )
public void Render ( ScreenSpaceAmbientOcclusionSettings . Settings settings , HDRenderPipeline hdRP , HDCamera hdCamera , ScriptableRenderContext renderContext , bool isForward )
const RenderTextureFormat kFormat = RenderTextureFormat . ARGB32 ;
const RenderTextureFormat kTempFormat = RenderTextureFormat . ARGB32 ;
if ( m_Command = = null )
{
m_Command = new CommandBuffer { name = "Ambient Occlusion" } ;
}
else
{
m_Command . Clear ( ) ;
}
var cmd2 = new CommandBuffer { name = "Setup neutral Ambient Occlusion (1x1)" } ;
cmd2 . SetGlobalTexture ( "_AmbientOcclusionTexture" , PostProcessing . RuntimeUtilities . blackTexture ) ; // Neutral is black, see the comment in the shaders
renderContext . ExecuteCommandBuffer ( cmd2 ) ;
cmd2 . Dispose ( ) ;
return ;
m_Command . SetGlobalTexture ( Uniforms . _AOBuffer , PostProcessing . RuntimeUtilities . blackTexture ) ; // Neutral is black, see the comment in the shaders
renderContext . ExecuteCommandBuffer ( m_Command ) ;
return ;
}
var width = hdCamera . camera . pixelWidth ;
m_Material . SetFloat ( Uniforms . _Downsample , 1.0f / downsize ) ;
m_Material . SetFloat ( Uniforms . _SampleCount , settings . sampleCount ) ;
// Start building a command buffer.
var cmd = new CommandBuffer { name = "Ambient Occlusion" } ;
cmd . SetGlobalTexture ( Uniforms . _CameraDepthTexture , depthID ) ;
// Note: GBuffer is automatically bind
cmd . GetTemporaryRT ( Uniforms . _TempTex1 , width / downsize , height / downsize , 0 , kFilter , kFormat , kRWMode ) ;
cmd . SetGlobalTexture ( Uniforms . _MainTex , depthID ) ;
Utilities . DrawFullScreen ( cmd , m_Material , hdCamera , Uniforms . _TempTex1 , null , 0 ) ;
hdRP . PushFullScreenDebugTexture ( cmd , Uniforms . _TempTex1 , hdCamera . camera , renderContext , FullScreenDebugMode . SSAOBeforeFiltering ) ;
m_Command . GetTemporaryRT ( Uniforms . _TempTex1 , width / downsize , height / downsize , 0 , kFilter , kTempFormat , kRWMode ) ;
Utilities . DrawFullScreen ( m_Command , m_Material , hdCamera , Uniforms . _TempTex1 , null , 0 ) ;
hdRP . PushFullScreenDebugTexture ( m_Command , Uniforms . _TempTex1 , hdCamera . camera , renderContext , FullScreenDebugMode . SSAOBeforeFiltering ) ;
cmd . GetTemporaryRT ( Uniforms . _TempTex2 , width , height , 0 , kFilter , kFormat , kRWMode ) ;
cmd . SetGlobalTexture ( Uniforms . _MainTex , Uniforms . _TempTex1 ) ;
Utilities . DrawFullScreen ( cmd , m_Material , hdCamera , Uniforms . _TempTex2 , null , 1 ) ;
cmd . ReleaseTemporaryRT ( Uniforms . _TempTex1 ) ;
m_Command . GetTemporaryRT ( Uniforms . _TempTex2 , width , height , 0 , kFilter , kTempFormat , kRWMode ) ;
m_Command . SetGlobalTexture ( Uniforms . _MainTex , Uniforms . _TempTex1 ) ;
Utilities . DrawFullScreen ( m_Command , m_Material , hdCamera , Uniforms . _TempTex2 , null , 1 ) ;
m_Command . ReleaseTemporaryRT ( Uniforms . _TempTex1 ) ;
cmd . GetTemporaryRT ( Uniforms . _TempTex1 , width , height , 0 , kFilter , kFormat , kRWMode ) ;
cmd . SetGlobalTexture ( Uniforms . _MainTex , Uniforms . _TempTex2 ) ;
Utilities . DrawFullScreen ( cmd , m_Material , hdCamera , Uniforms . _TempTex1 , null , 2 ) ;
cmd . ReleaseTemporaryRT ( Uniforms . _TempTex2 ) ;
m_Command . GetTemporaryRT ( Uniforms . _TempTex1 , width , height , 0 , kFilter , kTempFormat , kRWMode ) ;
m_Command . SetGlobalTexture ( Uniforms . _MainTex , Uniforms . _TempTex2 ) ;
Utilities . DrawFullScreen ( m_Command , m_Material , hdCamera , Uniforms . _TempTex1 , null , 2 ) ;
m_Command . ReleaseTemporaryRT ( Uniforms . _TempTex2 ) ;
cmd . GetTemporaryRT ( Uniforms . _AOBuffer , width , height , 0 , kFilter , kFormat , kRWMode ) ;
cmd . SetGlobalTexture ( Uniforms . _MainTex , Uniforms . _TempTex1 ) ;
Utilities . DrawFullScreen ( cmd , m_Material , hdCamera , Uniforms . _AOBuffer , null , 3 ) ;
cmd . ReleaseTemporaryRT ( Uniforms . _TempTex1 ) ;
m_Command . GetTemporaryRT ( Uniforms . _AOBuffer , width , height , 0 , kFilter , GetAOBufferFormat ( ) , kRWMode ) ;
m_Command . SetGlobalTexture ( Uniforms . _MainTex , Uniforms . _TempTex1 ) ;
Utilities . DrawFullScreen ( m_Command , m_Material , hdCamera , Uniforms . _AOBuffer , null , 3 ) ;
m_Command . ReleaseTemporaryRT ( Uniforms . _TempTex1 ) ;
cmd . SetGlobalTexture ( "_AmbientOcclusionTexture" , Uniforms . _AOBuffer ) ;
hdRP . PushFullScreenDebugTexture ( cmd , Uniforms . _AOBuffer , hdCamera . camera , renderContext , FullScreenDebugMode . SSAO ) ;
m_Command . SetGlobalTexture ( "_AmbientOcclusionTexture" , Uniforms . _AOBuffer ) ;
hdRP . PushFullScreenDebugTexture ( m_Command , Uniforms . _AOBuffer , hdCamera . camera , renderContext , FullScreenDebugMode . SSAO ) ;
renderContext . ExecuteCommandBuffer ( cmd ) ;
cmd . Dispose ( ) ;
renderContext . ExecuteCommandBuffer ( m_Command ) ;
if ( m_Command ! = null ) m_Command . Dispose ( ) ;
}
}
}