ComputeBuffer m_InstanceIdPresenceMask ;
ComputeBuffer m_InstanceIdToClassId ;
ComputeBuffer m_ClassCounts ;
NativeList < int > m_InstanceIdToClassId Lookup ;
NativeList < int > m_InstanceIdToLabelIndex Lookup ;
HashSet < Camera > m_CamerasRendered = new HashSet < Camera > ( ) ;
bool m_IdBuffersNeedUpdating ;
bool m_DidComputeLastFrame ;
public override void SetupMaterialProperties ( MaterialPropertyBlock mpb , MeshRenderer meshRenderer , Labeling labeling , uint instanceId )
{
if ( ! m_InstanceIdToClassId Lookup . IsCreated )
if ( ! m_InstanceIdToLabelIndex Lookup . IsCreated )
m_InstanceIdToClassId Lookup = new NativeList < int > ( k_StartingObjectCount , Allocator . Persistent ) ;
m_InstanceIdToLabelIndex Lookup = new NativeList < int > ( k_StartingObjectCount , Allocator . Persistent ) ;
if ( LabelingConfiguration . TryGetMatchingConfigurationIndex ( labeling , out var index ) )
if ( LabelingConfiguration . TryGetMatchingConfigurationEntry ( labeling , out LabelEntry labelEntry , out var index ) )
if ( m_InstanceIdToClassId Lookup . Length < = instanceId )
if ( m_InstanceIdToLabelIndex Lookup . Length < = instanceId )
m_InstanceIdToClassId Lookup . Resize ( ( int ) instanceId + 1 , NativeArrayOptions . ClearMemory ) ;
m_InstanceIdToLabelIndex Lookup . Resize ( ( int ) instanceId + 1 , NativeArrayOptions . ClearMemory ) ;
m_InstanceIdToClassId Lookup [ ( int ) instanceId ] = index + 1 ;
m_InstanceIdToLabelIndex Lookup [ ( int ) instanceId ] = index + 1 ;
}
}
var objectCount = k_StartingObjectCount ;
UpdateIdBufferSizes ( objectCount ) ;
m_ClassCounts = new ComputeBuffer ( LabelingConfiguration . LabelingConfiguration s . Count + 1 , UnsafeUtility . SizeOf < uint > ( ) , ComputeBufferType . Structured ) ;
m_ClassCounts = new ComputeBuffer ( LabelingConfiguration . LabelEntrie s . Count + 1 , UnsafeUtility . SizeOf < uint > ( ) , ComputeBufferType . Structured ) ;
RenderPipelineManager . endCameraRendering + = OnEndCameraRendering ;
}
protected override void ExecutePass ( ScriptableRenderContext renderContext , CommandBuffer cmd , HDCamera hdCamera , CullingResults cullingResult )
{
//If there are no objects to label, skip the pass
if ( ! m_InstanceIdToClassId Lookup . IsCreated | | m_InstanceIdToClassId Lookup . Length = = 0 )
if ( ! m_InstanceIdToLabelIndex Lookup . IsCreated | | m_InstanceIdToLabelIndex Lookup . Length = = 0 )
var counts = new NativeArray < uint > ( LabelingConfiguration . LabelingConfiguration s . Count + 1 , Allocator . Temp ) ;
var counts = new NativeArray < uint > ( LabelingConfiguration . LabelEntrie s . Count + 1 , Allocator . Temp ) ;
OnClassCountReadback ( Time . frameCount , counts ) ;
counts . Dispose ( ) ;
return ;
if ( m_IdBuffersNeedUpdating )
{
UpdateIdBufferSizes ( m_InstanceIdToClassId Lookup . Capacity ) ;
m_InstanceIdToClassId . SetData ( m_InstanceIdToClassId Lookup . AsArray ( ) ) ;
UpdateIdBufferSizes ( m_InstanceIdToLabelIndex Lookup . Capacity ) ;
m_InstanceIdToClassId . SetData ( m_InstanceIdToLabelIndex Lookup . AsArray ( ) ) ;
}
//The following section kicks off the four kernels in LabeledObjectHistogram.compute
cmd . SetComputeBufferParam ( m_ComputeShader , 3 , "InstanceIdPresenceMask" , m_InstanceIdPresenceMask ) ;
cmd . SetComputeBufferParam ( m_ComputeShader , 3 , "InstanceIdToClassId" , m_InstanceIdToClassId ) ;
cmd . SetComputeBufferParam ( m_ComputeShader , 3 , "ClassCounts" , m_ClassCounts ) ;
cmd . DispatchCompute ( m_ComputeShader , 3 , m_InstanceIdToClassId Lookup . Length , 1 , 1 ) ;
cmd . DispatchCompute ( m_ComputeShader , 3 , m_InstanceIdToLabelIndex Lookup . Length , 1 , 1 ) ;
var requestFrameCount = Time . frameCount ;
cmd . RequestAsyncReadback ( m_ClassCounts , request = > OnClassCountReadback ( requestFrameCount , request . GetData < uint > ( ) ) ) ;
m_ClassCounts = null ;
WaitForAllRequests ( ) ;
if ( m_InstanceIdToClassId Lookup . IsCreated )
if ( m_InstanceIdToLabelIndex Lookup . IsCreated )
m_InstanceIdToClassId Lookup . Dispose ( ) ;
m_InstanceIdToClassId Lookup = default ;
m_InstanceIdToLabelIndex Lookup . Dispose ( ) ;
m_InstanceIdToLabelIndex Lookup = default ;
internal event Action < NativeSlice < uint > , IReadOnlyList < LabelingConfiguration Entry > , int > ClassCountsReceived ;
internal event Action < NativeSlice < uint > , IReadOnlyList < LabelEntry > , int > ClassCountsReceived ;
void OnClassCountReadback ( int requestFrameCount , NativeArray < uint > counts )
{
Debug . Log ( sb ) ;
#endif
ClassCountsReceived ? . Invoke ( new NativeSlice < uint > ( counts , 1 ) , LabelingConfiguration . LabelingConfiguration s , requestFrameCount ) ;
ClassCountsReceived ? . Invoke ( new NativeSlice < uint > ( counts , 1 ) , LabelingConfiguration . LabelEntrie s , requestFrameCount ) ;
}
public void WaitForAllRequests ( )