RWBuffer<uint> g_LayeredSingleIdxBuffer : register( u2 );
#ifdef ENABLE_DEPTH_TEXTURE_BACKPLANE
RWBuffer<float> g_fModulUserscale : register( u3 );
RWBuffer<float> g_logBaseBuffer : register( u3 );
#endif
float4 FetchPlane(int l, int p);
bool CheckIntersection(int l, int k, uint2 viTilLL, uint2 viTilUR, float fModulUserScal e)
bool CheckIntersection(int l, int k, uint2 viTilLL, uint2 viTilUR, float suggestedBas e)
{
unsigned int val = (clusterIdxs[l>>1]>>(16*(l&1)))&0xffff;
bool bIsHit = ((val>>0)&0xff)<=((uint) k) && ((uint) k)<=((val>>8)&0xff);
float depthAtNearZ = ClusterIdxToZ(k, fModulUserScal e);
float depthAtFarZ = ClusterIdxToZ(k+1, fModulUserScal e);
float depthAtNearZ = ClusterIdxToZ(k, suggestedBas e);
float depthAtFarZ = ClusterIdxToZ(k+1, suggestedBas e);
for(int p=0; p<6; p++)
{
#endif
float3 vTileLL = float3(viTilLL.x/(float) iWidth, viTilLL.y/(float) iHeight, 0.0);
float3 vTileUR = float3((viTilLL.x+16)/(float) iWidth, (viTilLL.y+16)/(float) iHeight, dpt_ma );
float3 vTileUR = float3((viTilLL.x+16)/(float) iWidth, (viTilLL.y+16)/(float) iHeight, 1.0 );
vTileUR.xy = min(vTileUR.xy,float2(1.0,1.0)).xy;
const float3 vMi = g_vBoundsBuffer[l];
const float3 vMa = g_vBoundsBuffer[l+g_iNrVisibLights];
#ifdef ENABLE_DEPTH_TEXTURE_BACKPLANE
if( all(vMa.xy>vTileLL.xy) && all(vMi.xyz<vTileUR.xyz))
#else
#endif
{
unsigned int uInc = 1;
unsigned int uIndex;
#else
float fTileFarPlane = -GetLinearDepth(dpt_ma);
#endif
float fDenom = fTileFarPlane - g_fNearPlane;
float fModulUserScale = fDenom>0.0 ? ((g_fFarPlane-g_fNearPlane)/fDenom) : 1.0; // readjust to new range.
float suggestedBase = SuggestLogBase50(fTileFarPlane);
float fModulUserScale = 1.0;
float suggestedBase = g_fClustBase;
#endif
for(int l=(int) t; l<((iNrCoarseLights+1)>>1); l += NR_THREADS)
{
const int l0 = coarseList[2*l+0], l1 = coarseList[min(2*l+1,iNrCoarseLights)];
const unsigned int clustIdxMi0 = (const unsigned int) min(255,SnapToClusterIdx(GetLinearDepth(g_vBoundsBuffer[l0].z), fModulUserScal e));
const unsigned int clustIdxMa0 = (const unsigned int) min(255,SnapToClusterIdx(GetLinearDepth(g_vBoundsBuffer[l0+g_iNrVisibLights].z), fModulUserScal e));
const unsigned int clustIdxMi1 = (const unsigned int) min(255,SnapToClusterIdx(GetLinearDepth(g_vBoundsBuffer[l1].z), fModulUserScal e));
const unsigned int clustIdxMa1 = (const unsigned int) min(255,SnapToClusterIdx(GetLinearDepth(g_vBoundsBuffer[l1+g_iNrVisibLights].z), fModulUserScal e));
const unsigned int clustIdxMi0 = (const unsigned int) min(255,SnapToClusterIdx(GetLinearDepth(g_vBoundsBuffer[l0].z), suggestedBas e));
const unsigned int clustIdxMa0 = (const unsigned int) min(255,SnapToClusterIdx(GetLinearDepth(g_vBoundsBuffer[l0+g_iNrVisibLights].z), suggestedBas e));
const unsigned int clustIdxMi1 = (const unsigned int) min(255,SnapToClusterIdx(GetLinearDepth(g_vBoundsBuffer[l1].z), suggestedBas e));
const unsigned int clustIdxMa1 = (const unsigned int) min(255,SnapToClusterIdx(GetLinearDepth(g_vBoundsBuffer[l1+g_iNrVisibLights].z), suggestedBas e));
clusterIdxs[l] = (clustIdxMa1<<24) | (clustIdxMi1<<16) | (clustIdxMa0<<8) | (clustIdxMi0<<0);
}
for(int l=ll; l<min(iNrCoarseLights,(ll+4)); l++)
{
if(offs<(start+iSpaceAvail) && i<nrClusters && CheckIntersection(l, i, viTilLL.xy, viTilUR.xy, fModulUserScal e) )
if(offs<(start+iSpaceAvail) && i<nrClusters && CheckIntersection(l, i, viTilLL.xy, viTilUR.xy, suggestedBas e) )
{
uint lightModel = g_vLightData[ coarseList[l] ].uLightModel;
++modelListCount[ lightModel==REFLECTION_LIGHT ? 1 : 0];
}
#ifdef ENABLE_DEPTH_TEXTURE_BACKPLANE
g_fModulUserscale[tileIDX.y*nrTilesX + tileIDX.x] = fModulUserScal e;
g_logBaseBuffer[tileIDX.y*nrTilesX + tileIDX.x] = suggestedBas e;
#endif
}