Search Unity

Slow texture creation

Discussion in 'General Graphics' started by Marco-Playable, Jun 30, 2021.

  1. Marco-Playable

    Marco-Playable

    Joined:
    Nov 12, 2020
    Posts:
    53
    I would like to create a new texture, then get its NativeArray pointer so I can fill it with data at a later point before pushing it to the GPU. I was shocked to see that creating a texture and getting the pointer took over 15ms according to the profiler.

    Code looks like this:
    var newOutputTex = new Texture2D(2048, 2048, GraphicsFormat.R8G8B8A8_UNorm, TextureCreationFlags.MipChain);
    var outputTexPixels = newOutputTex.GetRawTextureData<byte>();


    Profiler output:
    upload_2021-6-29_17-36-20.png

    If I am reading this right it takes Unity a full 5ms to allocate CPU texture memory, then when I request the NativeArray pointer Unity issues an UploadTexture request which takes 7ms to complete on GPU and another 3ms overhead on the CPU side.
    This would be understandable if it was a Texture2D.Apply() call, but it looks like Unity is uploading a blank texture before it lets me access it. Is there a better way that avoids the upload until I have actually filled in the initial texture data?

    The manual says "GetRawTextureData does not allocate memory; the returned NativeArray directly points to the texture system memory data buffer."
     

    Attached Files: