浏览代码

Fixing up HDRP rendering. Deleting ObjectCountPass

/labeler_mock_mb
Jon Hogins 4 年前
当前提交
a70e5a18
共有 7 个文件被更改,包括 11 次插入232 次删除
  1. 9
      com.unity.perception/Runtime/GroundTruth/Labelers/InstanceSegmentationLabeler.cs
  2. 5
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
  3. 5
      com.unity.perception/Tests/Runtime/GroundTruthTests/ObjectCountTests.cs
  4. 21
      com.unity.perception/Editor/GroundTruth/ObjectCountPassEditor.cs
  5. 11
      com.unity.perception/Editor/GroundTruth/ObjectCountPassEditor.cs.meta
  6. 181
      com.unity.perception/Runtime/GroundTruth/ObjectCountPass.cs
  7. 11
      com.unity.perception/Runtime/GroundTruth/ObjectCountPass.cs.meta

9
com.unity.perception/Runtime/GroundTruth/Labelers/InstanceSegmentationLabeler.cs


using System;
using Unity.Collections;
using UnityEngine.Experimental.Rendering;
#if HDRP_PRESENT
using UnityEngine.Rendering.HighDefinition;
#endif
namespace UnityEngine.Perception.GroundTruth {
[AddComponentMenu("Perception/Labelers/InstanceSegmentationLabeler")]

RenderTexture m_SegmentationTexture;
RenderTextureReader<uint> m_SegmentationReader;
#if HDRP_PRESENT
InstanceSegmentationPass m_SegmentationPass;
#endif
#if URP_PRESENT
[NonSerialized]
InstanceSegmentationUrpPass m_InstanceSegmentationUrpPass;

{
name = "Segmentation Pass",
targetCamera = myCamera,
targetTexture = segmentationTexture
targetTexture = m_SegmentationTexture
};
m_SegmentationPass.EnsureInit();
customPassVolume.customPasses.Add(m_SegmentationPass);

5
com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs


using UnityEngine.Experimental.Rendering;
using UnityEngine.Profiling;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
#endif
#if URP_PRESENT
using UnityEngine.Rendering.Universal;
#endif
namespace UnityEngine.Perception.GroundTruth

}
else
{
produceBoundingBoxAnnotations = produceObjectCountAnnotations = produceRenderedObjectInfoMetric = false;
Debug.Log("Missing InstanceSegmentationLabeler. Will not generate metrics.");
}
}

5
com.unity.perception/Tests/Runtime/GroundTruthTests/ObjectCountTests.cs


yield return null;
Object.DestroyImmediate(planeObject2);
#if HDRP_PRESENT
//TODO: Remove this when DestroyImmediate properly calls Cleanup on the pass
var labelHistogramPass = (ObjectCountPass)cameraObject.GetComponent<CustomPassVolume>().customPasses.First(p => p is ObjectCountPass);
labelHistogramPass.WaitForAllRequests();
#endif
//destroy the object to force all pending segmented image readbacks to finish and events to be fired.
DestroyTestObject(cameraObject);

21
com.unity.perception/Editor/GroundTruth/ObjectCountPassEditor.cs


#if HDRP_PRESENT
using System;
using UnityEditor.Rendering.HighDefinition;
using UnityEngine.Perception.GroundTruth;
namespace UnityEditor.Perception.GroundTruth
{
[CustomPassDrawer(typeof(ObjectCountPass))]
public class ObjectCountPassEditor : BaseCustomPassDrawer
{
protected override void Initialize(SerializedProperty customPass)
{
AddProperty(customPass.FindPropertyRelative(nameof(GroundTruthPass.targetCamera)));
AddProperty(customPass.FindPropertyRelative(nameof(ObjectCountPass.SegmentationTexture)));
AddProperty(customPass.FindPropertyRelative(nameof(ObjectCountPass.LabelingConfiguration)));
base.Initialize(customPass);
}
}
}
#endif

11
com.unity.perception/Editor/GroundTruth/ObjectCountPassEditor.cs.meta


fileFormatVersion: 2
guid: d28b6eb98e988b34b9da6e73fccc09bb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

181
com.unity.perception/Runtime/GroundTruth/ObjectCountPass.cs


#if HDRP_PRESENT
using Unity.Collections.LowLevel.Unsafe;
using System;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.Rendering;
namespace UnityEngine.Perception.GroundTruth
{
/// <summary>
/// CustomPass which computes object count for each label in the given LabelingConfiguration in the frame.
/// Requires the texture produced by an InstanceSegmentationPass.
/// </summary>
public class ObjectCountPass : GroundTruthPass
{
const int k_StartingObjectCount = 1 << 8;
public RenderTexture SegmentationTexture;
public LabelingConfiguration LabelingConfiguration;
ComputeShader m_ComputeShader;
ComputeBuffer m_InstanceIdPresenceMask;
ComputeBuffer m_InstanceIdToClassId;
ComputeBuffer m_ClassCounts;
NativeList<int> m_InstanceIdToLabelIndexLookup;
HashSet<Camera> m_CamerasRendered = new HashSet<Camera>();
bool m_IdBuffersNeedUpdating;
bool m_DidComputeLastFrame;
public ObjectCountPass(Camera camera) : base(camera)
{
}
// ReSharper disable once UnusedMember.Global
public ObjectCountPass() : base(null)
{
}
public override void SetupMaterialProperties(MaterialPropertyBlock mpb, Renderer meshRenderer, Labeling labeling, uint instanceId)
{
if (!m_InstanceIdToLabelIndexLookup.IsCreated)
{
m_InstanceIdToLabelIndexLookup = new NativeList<int>(k_StartingObjectCount, Allocator.Persistent);
}
if (LabelingConfiguration.TryGetMatchingConfigurationEntry(labeling, out LabelEntry labelEntry, out var index))
{
if (m_InstanceIdToLabelIndexLookup.Length <= instanceId)
{
m_InstanceIdToLabelIndexLookup.Resize((int)instanceId + 1, NativeArrayOptions.ClearMemory);
}
m_IdBuffersNeedUpdating = true;
m_InstanceIdToLabelIndexLookup[(int)instanceId] = index + 1;
}
}
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
base.Setup(renderContext, cmd);
m_ComputeShader = Resources.Load<ComputeShader>("LabeledObjectHistogram");
var objectCount = k_StartingObjectCount;
UpdateIdBufferSizes(objectCount);
m_ClassCounts = new ComputeBuffer(LabelingConfiguration.LabelEntries.Count + 1, UnsafeUtility.SizeOf<uint>(), ComputeBufferType.Structured);
RenderPipelineManager.endCameraRendering += OnEndCameraRendering;
}
void OnEndCameraRendering(ScriptableRenderContext renderContext, Camera camera)
{
}
void UpdateIdBufferSizes(int objectCount)
{
var presenceMaskSizeNeeded = objectCount;
if (m_InstanceIdPresenceMask == null || presenceMaskSizeNeeded > m_InstanceIdPresenceMask.count)
{
m_InstanceIdPresenceMask?.Release();
m_InstanceIdPresenceMask = new ComputeBuffer(presenceMaskSizeNeeded, UnsafeUtility.SizeOf<uint>(), ComputeBufferType.Structured);
}
if (m_InstanceIdToClassId == null || m_InstanceIdToClassId.count < objectCount)
{
m_InstanceIdToClassId?.Release();
m_InstanceIdToClassId = new ComputeBuffer(objectCount, UnsafeUtility.SizeOf<uint>(), ComputeBufferType.Structured);
}
}
protected override void ExecutePass(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
{
//If there are no objects to label, skip the pass
if (!m_InstanceIdToLabelIndexLookup.IsCreated || m_InstanceIdToLabelIndexLookup.Length == 0)
{
var counts = new NativeArray<uint>(LabelingConfiguration.LabelEntries.Count + 1, Allocator.Temp);
OnClassCountReadback(Time.frameCount, counts);
counts.Dispose();
return;
}
m_CamerasRendered.Add(hdCamera.camera);
if (m_IdBuffersNeedUpdating)
{
UpdateIdBufferSizes(m_InstanceIdToLabelIndexLookup.Capacity);
m_InstanceIdToClassId.SetData(m_InstanceIdToLabelIndexLookup.AsArray());
}
//The following section kicks off the four kernels in LabeledObjectHistogram.compute
//clear ClassCounts
cmd.SetComputeBufferParam(m_ComputeShader, 1, "ClassCounts", m_ClassCounts);
cmd.DispatchCompute(m_ComputeShader, 1, m_ClassCounts.count, 1, 1);
//clear InstanceIdPresenceMask
cmd.SetComputeBufferParam(m_ComputeShader, 2, "InstanceIdPresenceMask", m_InstanceIdPresenceMask);
cmd.DispatchCompute(m_ComputeShader, 2, m_InstanceIdPresenceMask.count, 1, 1);
//clear InstanceIdPresenceMask
cmd.SetComputeTextureParam(m_ComputeShader, 0, "SegmentationTexture", SegmentationTexture);
cmd.SetComputeBufferParam(m_ComputeShader, 0, "InstanceIdPresenceMask", m_InstanceIdPresenceMask);
cmd.SetComputeIntParam(m_ComputeShader, "Width", SegmentationTexture.width);
cmd.SetComputeIntParam(m_ComputeShader, "Height", SegmentationTexture.height);
cmd.DispatchCompute(m_ComputeShader, 0, SegmentationTexture.width, SegmentationTexture.height, 1);
//clear InstanceIdPresenceMask
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_InstanceIdToLabelIndexLookup.Length, 1, 1);
var requestFrameCount = Time.frameCount;
cmd.RequestAsyncReadback(m_ClassCounts, request => OnClassCountReadback(requestFrameCount, request.GetData<uint>()));
}
protected override void Cleanup()
{
base.Cleanup();
m_InstanceIdPresenceMask?.Dispose();
m_InstanceIdPresenceMask = null;
m_InstanceIdToClassId?.Dispose();
m_InstanceIdToClassId = null;
m_ClassCounts?.Dispose();
m_ClassCounts = null;
WaitForAllRequests();
if (m_InstanceIdToLabelIndexLookup.IsCreated)
{
m_InstanceIdToLabelIndexLookup.Dispose();
m_InstanceIdToLabelIndexLookup = default;
}
}
internal event Action<NativeSlice<uint>, IReadOnlyList<LabelEntry>, int> ClassCountsReceived;
void OnClassCountReadback(int requestFrameCount, NativeArray<uint> counts)
{
#if PERCEPTION_DEBUG
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Histogram data. Frame {0}", requestFrameCount.ToString());
for (int i = 0; i < LabelingConfiguration.LabelingConfigurations.Count; i++)
{
sb.AppendFormat("{0}: {1}", LabelingConfiguration.LabelingConfigurations[i].label,
counts[i + 1].ToString());
sb.AppendLine();
}
Debug.Log(sb);
#endif
ClassCountsReceived?.Invoke(new NativeSlice<uint>(counts, 1), LabelingConfiguration.LabelEntries, requestFrameCount);
}
public void WaitForAllRequests()
{
var commandBuffer = CommandBufferPool.Get("LabelHistorgramCleanup");
commandBuffer.WaitAllAsyncReadbackRequests();
Graphics.ExecuteCommandBuffer(commandBuffer);
CommandBufferPool.Release(commandBuffer);
}
}
}
#endif

11
com.unity.perception/Runtime/GroundTruth/ObjectCountPass.cs.meta


fileFormatVersion: 2
guid: 0e6829808d75dc849a5c29b6b547c0e1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存