Skip to content

Commit

Permalink
Refactored imageEncodeJob to update namespaces and merge target textu…
Browse files Browse the repository at this point in the history
…re data into a single array; disabled HUD display for ROS TCP Connector to avoid lag caused by frequent GC at high frame rates; removed redundant AlwaysIncludedShaders
  • Loading branch information
Bob-Eric committed Nov 20, 2024
1 parent bbcb79c commit eb29f5a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 90 deletions.
4 changes: 2 additions & 2 deletions Assets/Resources/ROSConnectionPrefab.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5759000335426716240}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &8497029536680012343
MonoBehaviour:
Expand All @@ -50,7 +50,7 @@ MonoBehaviour:
m_KeepaliveTime: 1
m_NetworkTimeoutSeconds: 2
m_SleepTimeSeconds: 0.01
m_ShowHUD: 1
m_ShowHUD: 0
m_TFTopics:
- /tf
listenForTFMessages: 1
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@
using Unity.Jobs;
using UnityEngine;

namespace UnitySensors.ROS.Utils.Image
namespace UnitySensors.ROS.Serializer.Image
{
[BurstCompile]
struct ImageEncodeJob : IJobParallelFor
{
[ReadOnly]
public NativeArray<Color> sourceTextureRawData;
[WriteOnly]
public NativeArray<ColorRGB8> targetTextureRGB8;
[WriteOnly]
public NativeArray<Color32FC1> targetTexture32FC1;
[WriteOnly]
public NativeArray<Color16UC1> targetTexture16UC1;
public NativeArray<byte> targetTextureRawData;
[ReadOnly]
public int width;
[ReadOnly]
Expand All @@ -25,10 +21,13 @@ struct ImageEncodeJob : IJobParallelFor
public float distanceFactor;
[ReadOnly]
public Encoding encoding;
[ReadOnly]
public int bytesPerPixel;
public void Execute(int index)
{
int i = index % width;
int j = index / width;
int targetIndex = index * bytesPerPixel;

var sourceColor = sourceTextureRawData[(height - j - 1) * width + i];

Expand All @@ -37,20 +36,20 @@ public void Execute(int index)
case Encoding._32FC1:
var targetColor32FC1 = new Color32FC1();
targetColor32FC1.r = sourceColor.r * distanceFactor;
targetTexture32FC1[index] = targetColor32FC1;
targetTextureRawData.ReinterpretStore(targetIndex, targetColor32FC1);
break;
case Encoding._16UC1:
var targetColor16UC1 = new Color16UC1();
targetColor16UC1.r = (ushort)(sourceColor.r * distanceFactor);
targetTexture16UC1[index] = targetColor16UC1;
targetTextureRawData.ReinterpretStore(targetIndex, targetColor16UC1);
break;
case Encoding._RGB8:
default:
var targetColorRGB8 = new ColorRGB8();
targetColorRGB8.r = (byte)(sourceColor.r * 255);
targetColorRGB8.g = (byte)(sourceColor.g * 255);
targetColorRGB8.b = (byte)(sourceColor.b * 255);
targetTextureRGB8[index] = targetColorRGB8;
targetTextureRawData.ReinterpretStore(targetIndex, targetColorRGB8);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Unity.Jobs;
using UnityEngine;

using RosMessageTypes.Sensor;

using UnitySensors.Attribute;
using UnitySensors.Interface.Sensor;
using UnitySensors.ROS.Serializer.Std;
using UnitySensors.ROS.Utils.Image;
using Unity.Jobs;
using Unity.Collections;
using UnitySensors.ROS.Serializer.Image;

namespace UnitySensors.ROS.Serializer.Sensor
{
Expand All @@ -26,12 +25,13 @@ private enum SourceTexture
private SourceTexture _sourceTexture;
[SerializeField]
private Encoding _encoding;
[SerializeField, Attribute.ReadOnly]
[SerializeField, ReadOnly]
private Texture2D _encodedTexture;

[SerializeField]
private HeaderSerializer _header;

private int _width;
private int _height;
private ITextureInterface _sourceInterface;
private ImageEncodeJob _imageEncodeJob;

Expand All @@ -50,107 +50,67 @@ public override void Init()
return;
}

int width = texture.width;
int height = texture.height;
_width = texture.width;
_height = texture.height;

uint bytesPerPixel;
TextureFormat textureFormat;

_imageEncodeJob = new()
{
width = _width,
height = _height,
encoding = _encoding,
sourceTextureRawData = texture.GetRawTextureData<Color>(),
};

switch (_encoding)
{
case Encoding._32FC1:
_msg.encoding = "32FC1";
bytesPerPixel = 1 * 4;
textureFormat = TextureFormat.RFloat;
float distanceFactor = _sourceInterface.texture0FarClipPlane;
_imageEncodeJob = new()
{
width = width,
height = height,
distanceFactor = distanceFactor,
encoding = _encoding,
targetTexture16UC1 = new NativeArray<Color16UC1>(0, Allocator.Persistent),
targetTextureRGB8 = new NativeArray<ColorRGB8>(0, Allocator.Persistent)
};
_imageEncodeJob.distanceFactor = _sourceInterface.texture0FarClipPlane;

break;
case Encoding._16UC1:
_msg.encoding = "16UC1";
bytesPerPixel = 1 * 2;
textureFormat = TextureFormat.R16;
distanceFactor = _sourceInterface.texture0FarClipPlane * 1000;
_imageEncodeJob = new()
{
width = width,
height = height,
distanceFactor = distanceFactor,
encoding = _encoding,
targetTexture32FC1 = new NativeArray<Color32FC1>(0, Allocator.Persistent),
targetTextureRGB8 = new NativeArray<ColorRGB8>(0, Allocator.Persistent)
};
_imageEncodeJob.distanceFactor = _sourceInterface.texture0FarClipPlane * 1000;
break;
case Encoding._RGB8:
default:
_msg.encoding = "rgb8";
bytesPerPixel = 3 * 1;
textureFormat = TextureFormat.RGB24;
distanceFactor = 1;
_imageEncodeJob = new()
{
width = width,
height = height,
distanceFactor = distanceFactor,
encoding = _encoding,
targetTexture32FC1 = new NativeArray<Color32FC1>(0, Allocator.Persistent),
targetTexture16UC1 = new NativeArray<Color16UC1>(0, Allocator.Persistent),
};
_imageEncodeJob.distanceFactor = 1;
break;
}

_msg.is_bigendian = 0;
_msg.width = (uint)width;
_msg.height = (uint)height;
_msg.step = (uint)(bytesPerPixel * width);
_msg.data = new byte[_msg.step * height];
_msg.width = (uint)_width;
_msg.height = (uint)_height;
_msg.step = (uint)(bytesPerPixel * _width);
_msg.data = new byte[_msg.step * _height];

_encodedTexture = new Texture2D(width, height, textureFormat, false);
_imageEncodeJob.bytesPerPixel = (int)bytesPerPixel;

_encodedTexture = new Texture2D(_width, _height, textureFormat, false);
}

public override ImageMsg Serialize()
{
_msg.header = _header.Serialize();
var texture = _sourceTexture == SourceTexture.Texture0 ? _sourceInterface.texture0 : _sourceInterface.texture1;

_imageEncodeJob.sourceTextureRawData = texture.GetRawTextureData<Color>();

switch (_encoding)
{
case Encoding._32FC1:
_imageEncodeJob.targetTexture32FC1 = _encodedTexture.GetRawTextureData<Color32FC1>();
break;
case Encoding._16UC1:
_imageEncodeJob.targetTexture16UC1 = _encodedTexture.GetRawTextureData<Color16UC1>();
break;
case Encoding._RGB8:
default:
_imageEncodeJob.targetTextureRGB8 = _encodedTexture.GetRawTextureData<ColorRGB8>();
break;
}

_imageEncodeJob.Schedule(texture.width * texture.height, 1024).Complete();
_imageEncodeJob.targetTextureRawData = _encodedTexture.GetRawTextureData<byte>();
_imageEncodeJob.Schedule(_width * _height, 1024).Complete();

_encodedTexture.Apply();

// Manually copy the data to the message to avoid GC allocation
_encodedTexture.GetRawTextureData<byte>().CopyTo(_msg.data);
return _msg;
}

public override void OnDestroy()
{
base.OnDestroy();
_imageEncodeJob.targetTexture32FC1.Dispose();
_imageEncodeJob.targetTexture16UC1.Dispose();
_imageEncodeJob.targetTextureRGB8.Dispose();
}
}
}
8 changes: 0 additions & 8 deletions Assets/UnitySensorsROS/Runtime/Scripts/Utils/Image.meta

This file was deleted.

3 changes: 0 additions & 3 deletions ProjectSettings/GraphicsSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ GraphicsSettings:
- {fileID: 15106, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 10770, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 4800000, guid: 0faabdfb36dc37944bd5237eb87dff3e, type: 3}
- {fileID: 4800000, guid: f9fa29512657a5b408ef0c50698b141b, type: 3}
- {fileID: 4800000, guid: 417e6f59011b2b743a51b9344a78b15d, type: 3}
m_PreloadedShaders: []
m_PreloadShadersBatchTimeLimit: -1
m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
Expand Down

0 comments on commit eb29f5a

Please sign in to comment.