#pragma kernel BuildDispatchIndirect #include "TilePass.cs.hlsl" #define UNITY_MATERIAL_LIT // Need to be define before including Material.hlsl #include "../../Material/Material.hlsl" // This includes Material.hlsl RWBuffer g_DispatchIndirectBuffer : register( u0 ); // Indirect arguments have to be in a _buffer_, not a structured buffer RWStructuredBuffer g_TileList; StructuredBuffer g_TileFeatureFlags; uniform uint g_NumTiles; uniform uint g_NumTilesX; [numthreads(64, 1, 1)] void BuildDispatchIndirect(uint dispatchThreadId : SV_DispatchThreadID) { if (dispatchThreadId >= g_NumTiles) return; uint featureFlags = g_TileFeatureFlags[dispatchThreadId]; uint tileY = (dispatchThreadId + 0.5f) / (float)g_NumTilesX; // Integer division is extremely expensive, so we better avoid it uint tileX = dispatchThreadId - tileY * g_NumTilesX; // Check if there is no light or no material (mean we are sky/background pixel) / Both test as we can enable/disable light/material classification if ((featureFlags & LIGHT_FEATURE_MASK_FLAGS) != 0 && (featureFlags & MATERIAL_FEATURE_MASK_FLAGS) != 0) { uint variant = FeatureFlagsToTileVariant(featureFlags); uint offset; InterlockedAdd(g_DispatchIndirectBuffer[variant * 3 + 0], 1, offset); g_TileList[variant * g_NumTiles + offset] = (tileY << 16) | tileX; } }