#pragma kernel BigTileLightListGen #include "ShaderBase.h" #include "LightDefinitions.cs.hlsl" #include "LightingConvexHullUtils.hlsl" #include "SortingComputeUtils.hlsl" #define EXACT_EDGE_TESTS #define PERFORM_SPHERICAL_INTERSECTION_TESTS #define MAX_NR_BIGTILE_LIGHTS (MAX_NR_BIGTILE_LIGHTS_PLUSONE-1) uniform int g_isOrthographic; uniform int g_iNrVisibLights; uniform uint2 g_viDimensions; uniform float4x4 g_mInvScrProjection; uniform float4x4 g_mScrProjection; uniform float g_fNearPlane; uniform float g_fFarPlane; StructuredBuffer g_vBoundsBuffer : register( t1 ); StructuredBuffer g_vLightData : register( t2 ); StructuredBuffer g_data : register( t3 ); #define NR_THREADS 64 // output buffer RWStructuredBuffer g_vLightList : register( u0 ); // don't support RWBuffer yet in unity // 2kB (room for roughly 30 wavefronts) groupshared unsigned int lightsListLDS[MAX_NR_BIGTILE_LIGHTS_PLUSONE]; groupshared uint lightOffs; float GetLinearDepth(float zDptBufSpace) // 0 is near 1 is far { float3 vP = float3(0.0f,0.0f,zDptBufSpace); float4 v4Pres = mul(g_mInvScrProjection, float4(vP,1.0)); return v4Pres.z / v4Pres.w; } float3 GetViewPosFromLinDepth(float2 v2ScrPos, float fLinDepth) { bool isOrthographic = g_isOrthographic!=0; float fSx = g_mScrProjection[0].x; float fSy = g_mScrProjection[1].y; float fCx = isOrthographic ? g_mScrProjection[0].w : g_mScrProjection[0].z; float fCy = isOrthographic ? g_mScrProjection[1].w : g_mScrProjection[1].z; #if USE_LEFTHAND_CAMERASPACE bool useLeftHandVersion = true; #else bool useLeftHandVersion = isOrthographic; #endif float s = useLeftHandVersion ? 1 : (-1); float2 p = float2( (s*v2ScrPos.x-fCx)/fSx, (s*v2ScrPos.y-fCy)/fSy); return float3(isOrthographic ? p.xy : (fLinDepth*p.xy), fLinDepth); } float GetOnePixDiagWorldDistAtDepthOne() { float fSx = g_mScrProjection[0].x; float fSy = g_mScrProjection[1].y; return length( float2(1.0/fSx,1.0/fSy) ); } #ifdef PERFORM_SPHERICAL_INTERSECTION_TESTS void SphericalIntersectionTests(uint threadID, int iNrCoarseLights, float2 screenCoordinate); #endif #ifdef EXACT_EDGE_TESTS void CullByExactEdgeTests(uint threadID, int iNrCoarseLights, uint2 viTilLL, uint2 viTilUR); #endif [numthreads(NR_THREADS, 1, 1)] void BigTileLightListGen(uint threadID : SV_GroupIndex, uint3 u3GroupID : SV_GroupID) { uint2 tileIDX = u3GroupID.xy; uint t=threadID; uint iWidth = g_viDimensions.x; uint iHeight = g_viDimensions.y; uint nrBigTilesX = (iWidth+63)/64; uint nrBigTilesY = (iHeight+63)/64; if(t==0) lightOffs = 0; #if !defined(SHADER_API_XBOXONE) && !defined(SHADER_API_PSSL) GroupMemoryBarrierWithGroupSync(); #endif uint2 viTilLL = 64*tileIDX; uint2 viTilUR = min( viTilLL+uint2(64,64), uint2(iWidth, iHeight) ); // not width and height minus 1 since viTilUR represents the end of the tile corner. float2 vTileLL = float2(viTilLL.x/(float) iWidth, viTilLL.y/(float) iHeight); float2 vTileUR = float2(viTilUR.x/(float) iWidth, viTilUR.y/(float) iHeight); // build coarse list using AABB for(int l=(int) t; l<(int) g_iNrVisibLights; l += NR_THREADS) { const float2 vMi = g_vBoundsBuffer[l].xy; const float2 vMa = g_vBoundsBuffer[l+g_iNrVisibLights].xy; if( all(vMa>vTileLL) && all(vMi>2; // section 0 is side edges, section 1 is near edges and section 2 is far edges int iSwizzle = e0&0x3; int i=iSwizzle + (2*(iSection&0x2)); // offset by 4 at section 2 vP0 = GetTileVertex(uint2(viTilLL.x, viTilUR.y), uint2(viTilUR.x, viTilLL.y), i, fTileFarPlane); #if USE_LEFTHAND_CAMERASPACE float3 edgeSectionZero = g_isOrthographic==0 ? vP0 : float3(0.0,0.0,1.0); #else float3 edgeSectionZero = g_isOrthographic==0 ? vP0 : float3(0.0,0.0,-1.0); #endif vE0 = iSection == 0 ? edgeSectionZero : (((iSwizzle & 0x2) == 0 ? 1.0f : (-1.0f)) * ((int)(iSwizzle & 0x1) == (iSwizzle >> 1) ? float3(1, 0, 0) : float3(0, 1, 0))); } void CullByExactEdgeTests(uint threadID, int iNrCoarseLights, uint2 viTilLL, uint2 viTilUR) { const bool bOnlyNeedFrustumSideEdges = true; const int nrFrustEdges = bOnlyNeedFrustumSideEdges ? 4 : 8; // max 8 since we never need to test 4 far edges of frustum since they are identical vectors to near edges and plane is placed at vP0 on light hull. const int totNrEdgePairs = 12*nrFrustEdges; for(int l=0; l0) ++positive; else if(fSignDist<0) ++negative; } int resh = (positive>0 && negative>0) ? 0 : (positive>0 ? 1 : (negative>0 ? (-1) : 0)); positive=0; negative=0; for(int j=0; j<8; j++) { float3 vPf = GetTileVertex(viTilLL, viTilUR, j, g_fFarPlane); float fSignDist = dot(vN, vPf-vP0); if(fSignDist>0) ++positive; else if(fSignDist<0) ++negative; } int resf = (positive>0 && negative>0) ? 0 : (positive>0 ? 1 : (negative>0 ? (-1) : 0)); bool bFoundSepPlane = (resh*resf)<0; if(bFoundSepPlane) lightsListLDS[l]=0xffffffff; } } } #if !defined(SHADER_API_XBOXONE) && !defined(SHADER_API_PSSL) GroupMemoryBarrierWithGroupSync(); #endif } #endif