Skip to content

Commit

Permalink
Performance tuning
Browse files Browse the repository at this point in the history
- Lidar, Camera
  : get buffer from render texture using AsyngGPUReadback
  • Loading branch information
hyunseok-yang committed Jul 26, 2020
1 parent acd31f7 commit 68926e5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
23 changes: 13 additions & 10 deletions Assets/Scripts/Devices/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public partial class Camera : Device

protected UnityEngine.Camera cam = null;

public float adjustCapturingRate = 0.95f;

public bool runningDeviceWork = true;

protected string targetRTname;
Expand Down Expand Up @@ -184,10 +182,12 @@ private IEnumerator CameraWorker()
cam.Render();

var readback = AsyncGPUReadback.Request(cam.targetTexture, 0, readbackDstFormat);
cam.enabled = false;

yield return new WaitUntil(() => readback.done);
yield return null;

readback.WaitForCompletion();

cam.enabled = false;

if (readback.hasError)
{
Expand All @@ -196,13 +196,16 @@ private IEnumerator CameraWorker()
}
// Debug.Assert(request.done);

camData.SetTextureData(readback.GetData<byte>());

if (parameters.save_enabled)
if (readback.done)
{
var saveName = name + "_" + Time.time;
camData.SaveRawImageData(parameters.save_path, saveName);
// Debug.LogFormat("{0}|{1} captured", parameters.save_path, saveName);
camData.SetTextureData(readback.GetData<byte>());

if (parameters.save_enabled)
{
var saveName = name + "_" + Time.time;
camData.SaveRawImageData(parameters.save_path, saveName);
// Debug.LogFormat("{0}|{1} captured", parameters.save_path, saveName);
}
}

yield return waitForSeconds;
Expand Down
2 changes: 2 additions & 0 deletions Assets/Scripts/Devices/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public abstract class Device : MonoBehaviour

private float transportingTimeSeconds = 0;

public float adjustCapturingRate = 0.85f;

void OnDestroy()
{
if (memoryStreamOutboundQueue.IsCompleted)
Expand Down
31 changes: 22 additions & 9 deletions Assets/Scripts/Devices/Lidar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ public partial class Lidar : Device

private LaserCamData[] laserCamData;

public float adjustWaitingPeriod = 0.80f;

void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (depthMaterial)
Expand Down Expand Up @@ -189,7 +187,8 @@ private void SetupLaserCameraData()
private IEnumerator LaserCameraWorker()
{
var axisRotation = Vector3.zero;
var waitForSeconds = new WaitForSeconds(UpdatePeriod * adjustWaitingPeriod);
var waitForSeconds = new WaitForSeconds(UpdatePeriod * adjustCapturingRate);
var readbacks = new AsyncGPUReadbackRequest[numberOfLaserCamData];

while (true)
{
Expand All @@ -204,20 +203,34 @@ private IEnumerator LaserCameraWorker()

laserCam.Render();

var readback = AsyncGPUReadback.Request(laserCam.targetTexture, 0, TextureFormat.RGBA32);

yield return new WaitUntil(() => readback.done);
readbacks[dataIndex] = AsyncGPUReadback.Request(laserCam.targetTexture, 0, TextureFormat.RGBA32);

laserCam.enabled = false;
}

yield return null;

for (var dataIndex = 0; dataIndex < numberOfLaserCamData; dataIndex++)
{
var readback = readbacks[dataIndex];
readback.WaitForCompletion();

if (readback.hasError)
{
Debug.LogError("Failed to read GPU texture");
Debug.LogError("Failed to read GPU texture, dataIndex: " + dataIndex);
continue;
}
// Debug.Assert(readback.done);

data.SetBufferData(readback.GetData<byte>());
if (readback.done)
{
var data = laserCamData[dataIndex];
data.SetBufferData(readback.GetData<byte>());
}
else
{
Debug.LogWarning("AsyncGPUReadBackback Request was failed, dataIndex: " + dataIndex);
}
}

yield return waitForSeconds;
Expand All @@ -226,7 +239,7 @@ private IEnumerator LaserCameraWorker()

protected override IEnumerator MainDeviceWorker()
{
var waitForSeconds = new WaitForSeconds(UpdatePeriod * adjustWaitingPeriod);
var waitForSeconds = new WaitForSeconds(UpdatePeriod * adjustCapturingRate);
var sw = new Stopwatch();
while (true)
{
Expand Down

0 comments on commit 68926e5

Please sign in to comment.