using UnityEngine.Assertions ;
using UnityEngine.Rendering ;
namespace UnityEngine.Experimental.Rendering.HDPipeline
public class MipGenerator
{
RTHandle m_TempFullscreen Target ;
RTHandle m_TempColor Target ;
ComputeShader m_DepthPyramidCS ;
ComputeShader m_ColorPyramidCS ;
int m_ColorDownsampleKernelCopyMip0 ;
int m_ColorGaussianKernel ;
public MipGenerator ( HDRenderPipelineAsset asset )
m_DepthDownsampleKernel = m_DepthPyramidCS . FindKernel ( "KDepthDownsample8DualUav" ) ;
m_ColorDownsampleKernel = m_ColorPyramidCS . FindKernel ( "KColorDownsample" ) ;
m_ColorDownsampleKernelCopyMip0 = m_ColorPyramidCS . FindKernel ( "KColorDownsampleCopyMip0" ) ;
RTHandles . Release ( m_TempFullscreenTarget ) ;
m_TempFullscreenTarget = null ;
RTHandles . Release ( m_TempColorTarget ) ;
m_TempColorTarget = null ;
public int RenderMinDepthPyramid ( CommandBuffer cmd , Vector2Int size , RTHandle texture )
public int RenderMinDepthPyramid ( CommandBuffer cmd , Vector2Int size , RenderTexture texture )
{
var cs = m_DepthPyramidCS ;
int kernel = m_DepthDownsampleKernel ;
return srcMipLevel - 1 ;
}
// Generates an in-place gaussian pyramid
// Generates the gaussian pyramid of source into destination
// We can't do it in place as the color pyramid has to be read while writing to the color
// buffer in some cases (e.g. refraction, distortion)
public int RenderColorGaussianPyramid ( CommandBuffer cmd , Vector2Int size , RTHandle texture )
{
return RenderColorGaussianPyramid ( cmd , size , texture . rt ) ;
}
// Need this RenderTexture variant because some of the code in HDRP still hasn't switched to
// the RTHandle system :(
public int RenderColorGaussianPyramid ( CommandBuffer cmd , Vector2Int size , RenderTexture texture )
public int RenderColorGaussianPyramid ( CommandBuffer cmd , Vector2Int size , RenderTexture source , RenderTexture destination )
if ( m_TempFullscreenTarget = = null )
if ( m_TempColorTarget = = null )
m_TempFullscreenTarget = RTHandles . Alloc (
m_TempColorTarget = RTHandles . Alloc (
Vector2 . one * 0.5f ,
filterMode : FilterMode . Bilinear ,
colorFormat : RenderTextureFormat . ARGBHalf ,
var cs = m_ColorPyramidCS ;
int downsampleKernel = m_ColorDownsampleKernel ;
int downsampleKernelMip0 = m_ColorDownsampleKernelCopyMip0 ;
while ( srcMipWidth > = 2 | | srcMipHeight > = 2 )
while ( srcMipWidth > = 8 | | srcMipHeight > = 8 )
cmd . SetComputeTextureParam ( cs , downsampleKernel , HDShaderIDs . _Source , texture , srcMipLevel ) ;
cmd . SetComputeTextureParam ( cs , downsampleKernel , HDShaderIDs . _Destination , m_TempFullscreenTarget ) ;
cmd . DispatchCompute ( cs , downsampleKernel , ( dstMipWidth + 7 ) / 8 , ( dstMipHeight + 7 ) / 8 , 1 ) ;
// First dispatch also copies src to dst mip0
if ( srcMipLevel = = 0 )
{
cmd . SetComputeTextureParam ( cs , downsampleKernelMip0 , HDShaderIDs . _Source , source , 0 ) ;
cmd . SetComputeTextureParam ( cs , downsampleKernelMip0 , HDShaderIDs . _Mip0 , destination , 0 ) ;
cmd . SetComputeTextureParam ( cs , downsampleKernelMip0 , HDShaderIDs . _Destination , m_TempColorTarget ) ;
cmd . DispatchCompute ( cs , downsampleKernelMip0 , ( dstMipWidth + 7 ) / 8 , ( dstMipHeight + 7 ) / 8 , 1 ) ;
}
else
{
cmd . SetComputeTextureParam ( cs , downsampleKernel , HDShaderIDs . _Source , destination , srcMipLevel ) ;
cmd . SetComputeTextureParam ( cs , downsampleKernel , HDShaderIDs . _Destination , m_TempColorTarget ) ;
cmd . DispatchCompute ( cs , downsampleKernel , ( dstMipWidth + 7 ) / 8 , ( dstMipHeight + 7 ) / 8 , 1 ) ;
}
cmd . SetComputeTextureParam ( cs , gaussianKernel , HDShaderIDs . _Source , m_TempFullscreenTarget ) ;
cmd . SetComputeTextureParam ( cs , gaussianKernel , HDShaderIDs . _Destination , texture , srcMipLevel + 1 ) ;
cmd . SetComputeTextureParam ( cs , gaussianKernel , HDShaderIDs . _Source , m_TempColorTarget ) ;
cmd . SetComputeTextureParam ( cs , gaussianKernel , HDShaderIDs . _Destination , destination , srcMipLevel + 1 ) ;
cmd . DispatchCompute ( cs , gaussianKernel , ( dstMipWidth + 7 ) / 8 , ( dstMipHeight + 7 ) / 8 , 1 ) ;
srcMipLevel + + ;