public void RenderDeferredLighting ( Camera camera , RenderLoop renderLoop , RenderTargetIdentifier cameraColorBufferRT )
{
var bUseClusteredForDeferred = ! usingFptl ; // doesn't work on reflections yet but will soon
var bUseClusteredForDeferred = ! usingFptl ;
var invViewProj = Utilities . GetViewProjectionMatrix ( camera ) . inverse ;
var screenSize = Utilities . ComputeScreenSize ( camera ) ;
m_DebugViewTilesMaterial . SetVector ( "_ScreenSize" , screenSize ) ;
m_DebugViewTilesMaterial . SetInt ( "_ViewTilesFlags" , ( int ) debugViewTilesFlags ) ;
m_DebugViewTilesMaterial . EnableKeyword ( bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST" ) ;
m_DebugViewTilesMaterial . DisableKeyword ( ! bUseClusteredForDeferred ? "USE_CLUSTERED_LIGHTLIST" : "USE_FPTL_LIGHTLIST" ) ;
var cmd = new CommandBuffer ( ) ;
cmd . SetGlobalBuffer ( "g_vLightListGlobal" , bUseClusteredForDeferred ? s_PerVoxelLightLists : s_LightList ) ; // opaques list (unless MSAA possibly)
// In case of bUseClusteredForDeferred disable toggle option since we're using m_perVoxelLightLists as opposed to lightList
if ( bUseClusteredForDeferred )
using ( new Utilities . ProfilingSample ( "TilePass - Deferred Lighting Pass" , renderLoop ) )
cmd . SetGlobalFloat ( "g_isOpaquesOnlyEnabled" , 0 ) ;
}
cmd . name = "DoTiledDeferredLighting" ;
var cmd = new CommandBuffer ( ) ;
cmd . SetGlobalBuffer ( "g_vLightListGlobal" , bUseClusteredForDeferred ? s_PerVoxelLightLists : s_LightList ) ; // opaques list (unless MSAA possibly)
/ *
if ( enableComputeLightEvaluation ) //TODO: temporary workaround for "All kernels must use same constant buffer layouts"
{
var w = camera . pixelWidth ;
var h = camera . pixelHeight ;
var numTilesX = ( w + 7 ) / 8 ;
var numTilesY = ( h + 7 ) / 8 ;
// In case of bUseClusteredForDeferred disable toggle option since we're using m_perVoxelLightLists as opposed to lightList
if ( bUseClusteredForDeferred )
{
}
string kernelName = "ShadeDeferred" + ( bUseClusteredForDeferred ? "_Clustered" : "_Fptl" ) + ( enableDrawTileDebug ? "_Debug" : "" ) ;
int kernel = deferredComputeShader . FindKernel ( kernelName ) ;
/ *
if ( enableComputeLightEvaluation ) //TODO: temporary workaround for "All kernels must use same constant buffer layouts"
{
var w = camera . pixelWidth ;
var h = camera . pixelHeight ;
var numTilesX = ( w + 7 ) / 8 ;
var numTilesY = ( h + 7 ) / 8 ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_CameraDepthTexture" , new RenderTargetIdentifier ( s_CameraDepthTexture ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_CameraGBufferTexture0 " , new RenderTargetIdentifier ( s_GBufferAlbedo ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_CameraGBufferTexture1 " , new RenderTargetIdentifier ( s_GBufferSpecRough ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_CameraGBufferTexture2 " , new RenderTargetIdentifier ( s_GBufferNormal ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_CameraGBufferTexture3 " , new RenderTargetIdentifier ( s_GBufferEmission ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_spotCookieTextures" , m_CookieTexArray . GetTexCache ( ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_poin tCookieTextures" , m_Cube CookieTexArray . GetTexCache ( ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_reflCubeTextures" , m_CubeRefl TexArray . GetTexCache ( ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_reflRoot CubeTexture" , ReflectionProbe . GetDefaultTextur e( ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "g_tShadowBuffer" , new RenderTargetIdentifier ( m_shadowBufferID ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "unity_NHxRoughness" , m_NHxRoughnessTexture ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_LightTextureB0" , m_LightAttentuation Texture ) ;
string kernelName = "ShadeDeferred" + ( bUseClusteredForDeferred ? "_Clustered" : "_Fptl" ) + ( enableDrawTileDebug ? "_Debug" : "" ) ;
int kernel = deferredComputeShader . FindKernel ( kernelName ) ;
cmd . SetComputeBufferParam ( deferredComputeShader , kernel , "g_vLightListGlobal" , bUseClusteredForDeferred ? s_PerVoxelLightLists : s_LightList ) ;
cmd . SetComputeBufferParam ( deferredComputeShader , kernel , "g_vLightData" , s_LightDataBuffer ) ;
cmd . SetComputeBufferParam ( deferredComputeShader , kernel , "g_dirLightData" , s_DirLightList ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_CameraDepthTexture " , new RenderTargetIdentifier ( s_CameraDepthTexture ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_CameraGBufferTexture0 " , new RenderTargetIdentifier ( s_GBufferAlbedo ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_CameraGBufferTexture1 " , new RenderTargetIdentifier ( s_GBufferSpecRough ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_CameraGBufferTexture2 " , new RenderTargetIdentifier ( s_GBufferNormal ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_CameraGBufferTexture3" , new RenderTargetIdentifier ( s_GBufferEmission ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_s potCookieTextures" , m_CookieTexArray . GetTexCache ( ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_pointCookieTextures" , m_CubeCookie TexArray . GetTexCache ( ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_reflCubeTextures " , m_CubeReflTexArray . GetTexCach e( ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_reflRootCubeTexture" , ReflectionProbe . GetDefaultTexture ( ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "g_tShadowBuffer" , new RenderTargetIdentifier ( m_shadowBufferID ) ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "unity_NHxRoughness" , m_NHxRoughness Texture ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "_LightTextureB0" , m_LightAttentuationTexture ) ;
var defdecode = ReflectionProbe . GetDefaultTextureHDRDecodeValues ( ) ;
cmd . SetComputeFloatParam ( deferredComputeShader , "_reflRootHdrDecodeMult" , defdecode . x ) ;
cmd . SetComputeFloatParam ( deferredComputeShader , "_reflRootHdrDecodeExp " , defdecode . y ) ;
cmd . SetComputeBufferParam ( deferredComputeShader , kernel , "g_vLightListGlobal" , bUseClusteredForDeferred ? s_PerVoxelLightLists : s_LightList ) ;
cmd . SetComputeBufferParam ( deferredComputeShader , kernel , "g_vLightData" , s_LightDataBuffer ) ;
cmd . SetComputeBufferParam ( deferredComputeShader , kernel , "g_dirLightData" , s_DirLightList ) ;
cmd . SetComputeFloatParam ( deferredComputeShader , "g_fClustScale" , m_ClustScale ) ;
cmd . SetComputeFloatParam ( deferredComputeShader , "g_fClustBase" , k_ClustLogBas e ) ;
cmd . SetComputeFloatParam ( deferredComputeShader , "g_fNearPlane" , camera . nearClipPlan e ) ;
cmd . SetComputeFloatParam ( deferredComputeShader , "g_fF arPlane" , camera . f arClipPlane) ;
cmd . SetComputeIn tParam ( deferredComputeShader , "g_iLog2NumClusters" , k_Log2NumClusters ) ;
cmd . SetComputeIntParam ( deferredComputeShader , "g_isLogBaseBufferEnabled" , k_UseDepthBuffer ? 1 : 0 ) ;
cmd . SetComputeIntParam ( deferredComputeShader , "g_isOpaquesOnlyEnabled" , 0 ) ;
var defdecode = ReflectionProbe . GetDefaultTextureHDRDecodeValues ( ) ;
cmd . SetComputeFloatParam ( deferredComputeShader , "_reflRootHdrDecodeMult " , defdecode . x ) ;
cmd . SetComputeFloatParam ( deferredComputeShader , "_reflRootHdrDecodeExp" , defdecode . y ) ;
cmd . SetComputeFloatParam ( deferredComputeShader , "g_fClustScale" , m_ClustScal e ) ;
cmd . SetComputeFloatParam ( deferredComputeShader , "g_fClustBase" , k_ClustLogBas e ) ;
cmd . SetComputeFloatParam ( deferredComputeShader , "g_fNe arPlane" , camera . ne arClipPlane) ;
cmd . SetComputeFloa tParam ( deferredComputeShader , "g_fFarPlane" , camera . farClipPlane ) ;
cmd . SetComputeIntParam ( deferredComputeShader , "g_iLog2NumClusters" , k_Log2NumClusters ) ;
cmd . SetComputeIntParam ( deferredComputeShader , "g_isLogBaseBufferEnabled" , k_UseDepthBuffer ? 1 : 0 ) ;
//
var proj = camera . projectionMatrix ;
var temp = new Matrix4x4 ( ) ;
temp . SetRow ( 0 , new Vector4 ( 1.0f , 0.0f , 0.0f , 0.0f ) ) ;
temp . SetRow ( 1 , new Vector4 ( 0.0f , 1.0f , 0.0f , 0.0f ) ) ;
temp . SetRow ( 2 , new Vector4 ( 0.0f , 0.0f , 0.5f , 0.5f ) ) ;
temp . SetRow ( 3 , new Vector4 ( 0.0f , 0.0f , 0.0f , 1.0f ) ) ;
var projh = temp * proj ;
var invProjh = projh . inverse ;
temp . SetRow ( 0 , new Vector4 ( 0.5f * w , 0.0f , 0.0f , 0.5f * w ) ) ;
temp . SetRow ( 1 , new Vector4 ( 0.0f , 0.5f * h , 0.0f , 0.5f * h ) ) ;
temp . SetRow ( 2 , new Vector4 ( 0.0f , 0.0f , 0.5f , 0.5f ) ) ;
temp . SetRow ( 3 , new Vector4 ( 0.0f , 0.0f , 0.0f , 1.0f ) ) ;
var projscr = temp * proj ;
var invProjscr = projscr . inverse ;
//
var proj = camera . projectionMatrix ;
var temp = new Matrix4x4 ( ) ;
temp . SetRow ( 0 , new Vector4 ( 1.0f , 0.0f , 0.0f , 0.0f ) ) ;
temp . SetRow ( 1 , new Vector4 ( 0.0f , 1.0f , 0.0f , 0.0f ) ) ;
temp . SetRow ( 2 , new Vector4 ( 0.0f , 0.0f , 0.5f , 0.5f ) ) ;
temp . SetRow ( 3 , new Vector4 ( 0.0f , 0.0f , 0.0f , 1.0f ) ) ;
var projh = temp * proj ;
var invProjh = projh . inverse ;
cmd . SetComputeIntParam ( deferredComputeShader , "g_iNrVisibLights" , numLights ) ;
SetMatrixCS ( cmd , deferredComputeShader , "g_mScrProjection" , projscr ) ;
SetMatrixCS ( cmd , deferredComputeShader , "g_mInvScrProjection" , invProjscr ) ;
SetMatrixCS ( cmd , deferredComputeShader , "g_mViewToWorld" , camera . cameraToWorldMatrix ) ;
temp . SetRow ( 0 , new Vector4 ( 0.5f * w , 0.0f , 0.0f , 0.5f * w ) ) ;
temp . SetRow ( 1 , new Vector4 ( 0.0f , 0.5f * h , 0.0f , 0.5f * h ) ) ;
temp . SetRow ( 2 , new Vector4 ( 0.0f , 0.0f , 0.5f , 0.5f ) ) ;
temp . SetRow ( 3 , new Vector4 ( 0.0f , 0.0f , 0.0f , 1.0f ) ) ;
var projscr = temp * proj ;
var invProjscr = projscr . inverse ;
cmd . SetComputeIntParam ( deferredComputeShader , "g_iNrVisibLights" , numLights ) ;
SetMatrixCS ( cmd , deferredComputeShader , "g_mScrProjection" , projscr ) ;
SetMatrixCS ( cmd , deferredComputeShader , "g_mInvScrProjection" , invProjscr ) ;
SetMatrixCS ( cmd , deferredComputeShader , "g_mViewToWorld" , camera . cameraToWorldMatrix ) ;
if ( bUseClusteredForDeferred )
{
cmd . SetComputeBufferParam ( deferredComputeShader , kernel , "g_vLayeredOffsetsBuffer" , s_PerVoxelOffset ) ;
if ( k_UseDepthBuffer )
if ( bUseClusteredForDeferred )
cmd . SetComputeBufferParam ( deferredComputeShader , kernel , "g_logBaseBuffer" , s_PerTileLogBaseTweak ) ;
cmd . SetComputeBufferParam ( deferredComputeShader , kernel , "g_vLayeredOffsetsBuffer" , s_PerVoxelOffset ) ;
if ( k_UseDepthBuffer )
{
cmd . SetComputeBufferParam ( deferredComputeShader , kernel , "g_logBaseBuffer" , s_PerTileLogBaseTweak ) ;
}
}
cmd . SetComputeIntParam ( deferredComputeShader , "g_widthRT" , w ) ;
cmd . SetComputeIntParam ( deferredComputeShader , "g_heightRT" , h ) ;
cmd . SetComputeIntParam ( deferredComputeShader , "g_nNumDirLights" , numDirLights ) ;
cmd . SetComputeBufferParam ( deferredComputeShader , kernel , "g_dirLightData" , s_DirLightList ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "uavOutput" , new RenderTargetIdentifier ( s_CameraTarget ) ) ;
cmd . SetComputeIntParam ( deferredComputeShader , "g_widthRT" , w ) ;
cmd . SetComputeIntParam ( deferredComputeShader , "g_heightRT" , h ) ;
cmd . SetComputeIntParam ( deferredComputeShader , "g_nNumDirLights" , numDirLights ) ;
cmd . SetComputeBufferParam ( deferredComputeShader , kernel , "g_dirLightData" , s_DirLightList ) ;
cmd . SetComputeTextureParam ( deferredComputeShader , kernel , "uavOutput" , new RenderTargetIdentifier ( s_CameraTarget ) ) ;
SetMatrixArrayCS ( cmd , deferredComputeShader , "g_matWorldToShadow" , m_MatWorldToShadow ) ;
SetVectorArrayCS ( cmd , deferredComputeShader , "g_vDirShadowSplitSpheres" , m_DirShadowSplitSpheres ) ;
cmd . SetComputeVectorParam ( deferredComputeShader , "g_vShadow3x3PCFTerms0" , m_Shadow3X3PCFTerms [ 0 ] ) ;
cmd . SetComputeVectorParam ( deferredComputeShader , "g_vShadow3x3PCFTerms1" , m_Shadow3X3PCFTerms [ 1 ] ) ;
cmd . SetComputeVectorParam ( deferredComputeShader , "g_vShadow3x3PCFTerms2" , m_Shadow3X3PCFTerms [ 2 ] ) ;
cmd . SetComputeVectorParam ( deferredComputeShader , "g_vShadow3x3PCFTerms3" , m_Shadow3X3PCFTerms [ 3 ] ) ;
SetMatrixArrayCS ( cmd , deferredComputeShader , "g_matWorldToShadow" , m_MatWorldToShadow ) ;
SetVectorArrayCS ( cmd , deferredComputeShader , "g_vDirShadowSplitSpheres" , m_DirShadowSplitSpheres ) ;
cmd . SetComputeVectorParam ( deferredComputeShader , "g_vShadow3x3PCFTerms0" , m_Shadow3X3PCFTerms [ 0 ] ) ;
cmd . SetComputeVectorParam ( deferredComputeShader , "g_vShadow3x3PCFTerms1" , m_Shadow3X3PCFTerms [ 1 ] ) ;
cmd . SetComputeVectorParam ( deferredComputeShader , "g_vShadow3x3PCFTerms2" , m_Shadow3X3PCFTerms [ 2 ] ) ;
cmd . SetComputeVectorParam ( deferredComputeShader , "g_vShadow3x3PCFTerms3" , m_Shadow3X3PCFTerms [ 3 ] ) ;
cmd . DispatchCompute ( deferredComputeShader , kernel , numTilesX , numTilesY , 1 ) ;
}
else
{ * /
cmd . DispatchCompute ( deferredComputeShader , kernel , numTilesX , numTilesY , 1 ) ;
}
else
{ * /
if ( enableDirectIndirectSinglePass )
{
cmd . Blit ( null , cameraColorBufferRT , m_DeferredAllMaterial , 0 ) ;
cmd . Blit ( null , cameraColorBufferRT , m_DeferredDirectMaterial , 0 ) ;
cmd . Blit ( null , cameraColorBufferRT , m_DeferredIndirectMaterial , 0 ) ;
}
//}
//}
if ( debugViewTilesFlags ! = 0 )
{
cmd . Blit ( null , cameraColorBufferRT , m_DebugViewTilesMaterial , 0 ) ;
}
if ( debugViewTilesFlags ! = 0 )
{
cmd . Blit ( null , cameraColorBufferRT , m_DebugViewTilesMaterial , 0 ) ;
}
renderLoop . ExecuteCommandBuffer ( cmd ) ;
cmd . Dispose ( ) ;
} // TilePass - Deferred Lighting Pass
renderLoop . ExecuteCommandBuffer ( cmd ) ;
cmd . Dispose ( ) ;
}
}
}