Texture2D could accept a SetPixels with NativeArray<Color>?

Hello, I would like to know if would be convenient for Texture2D method of SetPixels to also allows receiving a NativeArray as argument.

I’m doing some native work that creates a mini “render buffer” to be applied to a Texture2D, but I need to copy the content back to a normal Color[ ] before calling SetPixels.
By allowing NativeArray as argument, we could work with native pixel processing and avoid a unnecessary array copy every frame.

Current code:

s_displayPixels = tex.GetPixels();
m_displayPixelsNative = new NativeArray<Color>(s_displayPixels.Length, Unity.Collections.Allocator.Persistent);
// < .... >
unsafe
{
    void* ptr = m_displayPixelsNative.GetUnsafePtr();
    for (int i = 0; i < s_displayPixels.Length; i++)
    {
        s_displayPixels[i] = *(Color*)((byte*)ptr + i * sizeof(Color));
    }
}

tex.SetPixels(s_displayPixels);
tex.Apply();

Thank you

Hi. I did not test it yet, but 2018.2 Beta is supposed to have NativeArray access to texture data.
It would allow you to directly modify the data buffer without the need of copying.

This is from the release notes.

“Added Texture2D.GetRawTextureData overload that returns a direct NativeArray into texture’s pixel data. This allows runtime pixel data modification without an extra memory copy of SetPixels32 or LoadRawTextureData. (1015723)”

Lennart

Thank you for letting me know. It works great for my needs (after changing the color format from floats to bytes).

Yes. And you can do the conversion in a job. Offloading everything from the main thread.