Read data with AsyncGPUReadbackRequest from texture with TextureFormat.RGBAHalf

Hi, this might be a quick one. The title pretty much sums it up. I have a texture with the format RBGAHalf and I want to read some data from it. What I have is something like this:

if (!request.done)
{
    Debug.Log("Request is not done yet");
    return;
}

if (request.hasError)
{
    Debug.Log("Request finished with error");
}
else
{
    NativeArray<T> data = request.GetData<T>();
}

However, I don’t know what datatype to replace the T by as there is no half datatype available afaik. No matter what I tried, I always seem to get 0 when trying to access any of the data. Does someone have some experience with this and can help me?

Commenting for future readers.

I believe ushort is the most obvious answer since it is represented as a 16bit value (same as half on GPU), but other types can be used if memory alignment is satisfied. It can be converted to float using Mathf.HalfToFloat. Alternatively I’ve had success with Unity.Mathematics.half but their memory might not always line up to the GPU equivalent, so better to stick to ushort imo.

1 Like