浏览代码

Adding option to force synchronous readback. This would help with visualizing bounding boxes.

/readback_mode
Jon Hogins 5 年前
当前提交
433a9fa6
共有 4 个文件被更改,包括 62 次插入20 次删除
  1. 11
      com.unity.perception/Runtime/GroundTruth/PerceptionCamera.cs
  2. 48
      com.unity.perception/Runtime/GroundTruth/RenderTextureReader.cs
  3. 20
      com.unity.perception/Runtime/GroundTruth/ReadbackMode.cs
  4. 3
      com.unity.perception/Runtime/GroundTruth/ReadbackMode.cs.meta

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


public LabelingConfiguration LabelingConfiguration;
/// <summary>
/// Controls the timing of GPU ground truth readback. Synchronous will produce ground truth in the same frame as
/// rendering but will result in significantly slower rendering. Synchronous is intended to be used primarily
/// for debugging.
/// </summary>
public ReadbackMode ReadbackMode = ReadbackMode.Async;
/// <summary>
/// Invoked when RenderedObjectInfos (bounding boxes) are calculated. The first parameter is the Time.frameCount at which the objects were rendered. This may be called many frames after the frame in which the objects were rendered.
/// </summary>
public event Action<int, NativeArray<RenderedObjectInfo>> renderedObjectInfosCalculated;

m_SegmentationAnnotationDefinition = SimulationManager.RegisterAnnotationDefinition("semantic segmentation", specs, "pixel-wise semantic segmentation label", "PNG");
m_ClassLabelingTextureReader = new RenderTextureReader<short>(m_LabelingTexture, myCamera,
(frameCount, data, tex) => OnSemanticSegmentationImageRead(frameCount, data));
(frameCount, data, tex) => OnSemanticSegmentationImageRead(frameCount, data), ReadbackMode);
}
if (produceObjectCountAnnotations || produceBoundingBoxAnnotations)

if (produceVisiblePixelsMetric)
ProduceVisiblePixelsMetric(renderedObjectInfos, frameCount);
});
}, ReadbackMode);
}
RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;

48
com.unity.perception/Runtime/GroundTruth/RenderTextureReader.cs


Camera m_CameraRenderingToSource;
ProfilerMarker m_WaitingForCompletionMarker = new ProfilerMarker("RenderTextureReader_WaitingForCompletion");
ReadbackMode m_ReadbackMode;
public RenderTextureReader(RenderTexture source, Camera cameraRenderingToSource, Action<int, NativeArray<T>, RenderTexture> imageReadCallback)
public RenderTextureReader(RenderTexture source, Camera cameraRenderingToSource, Action<int, NativeArray<T>, RenderTexture> imageReadCallback, ReadbackMode readbackMode = ReadbackMode.Async)
{
this.m_Source = source;
this.m_ImageReadCallback = imageReadCallback;

m_ReadbackMode = readbackMode;
m_ReadbackMode = ReadbackMode.Synchronous;
if (m_ReadbackMode == ReadbackMode.Synchronous)
m_CpuTexture = new Texture2D(m_Source.width, m_Source.height, m_Source.graphicsFormat, TextureCreationFlags.None);
RenderPipelineManager.endFrameRendering += OnEndFrameRendering;

m_NextFrameToCapture = Time.frameCount + 1;
if (!GraphicsUtilities.SupportsAsyncReadback())
switch (m_ReadbackMode)
RenderTexture.active = m_Source;
m_CpuTexture.ReadPixels(new Rect(
Vector2.zero,
new Vector2(m_Source.width, m_Source.height)),
0, 0);
RenderTexture.active = null;
var data = m_CpuTexture.GetRawTextureData<T>();
m_ImageReadCallback(Time.frameCount, data, m_Source);
return;
case ReadbackMode.Synchronous:
{
RenderTexture.active = m_Source;
m_CpuTexture.ReadPixels(new Rect(
Vector2.zero,
new Vector2(m_Source.width, m_Source.height)),
0, 0);
RenderTexture.active = null;
var data = m_CpuTexture.GetRawTextureData<T>();
m_ImageReadCallback(Time.frameCount, data, m_Source);
break;
}
case ReadbackMode.Async:
{
var commandBuffer = CommandBufferPool.Get("RenderTextureReader");
var frameCount = Time.frameCount;
commandBuffer.RequestAsyncReadback(m_Source, r => OnGpuReadback(r, frameCount));
context.ExecuteCommandBuffer(commandBuffer);
context.Submit();
CommandBufferPool.Release(commandBuffer);
break;
}
var commandBuffer = CommandBufferPool.Get("RenderTextureReader");
var frameCount = Time.frameCount;
commandBuffer.RequestAsyncReadback(m_Source, r => OnGpuReadback(r, frameCount));
context.ExecuteCommandBuffer(commandBuffer);
context.Submit();
CommandBufferPool.Release(commandBuffer);
}
void OnGpuReadback(AsyncGPUReadbackRequest request, int frameCount)

20
com.unity.perception/Runtime/GroundTruth/ReadbackMode.cs


using System;
namespace UnityEngine.Perception.Sensors
{
/// <summary>
/// Types of GPU readback
/// </summary>
public enum ReadbackMode
{
/// <summary>
/// Readback should occur asynchronously. The results may be retrieved many frames past the actual rendering.
/// </summary>
Async,
/// <summary>
/// Readback should occur synchronously. The results will always be retrieved in the same frame as rendering
/// at the cost of performance.
/// </summary>
Synchronous
}
}

3
com.unity.perception/Runtime/GroundTruth/ReadbackMode.cs.meta


fileFormatVersion: 2
guid: 4624b43574394ee6ad1662653f8b0034
timeCreated: 1587566302
正在加载...
取消
保存