Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Span<T> for the win..

Discussion in 'Graphics Dev Blitz Day 2023 - Q&A' started by jbooth, May 24, 2023.

  1. jbooth


    Jan 6, 2014
    Could unity create an API swat team for Span<T> and GPU readback interfaces everywhere that's appropriate?

    I know Unity's planning a new terrain solution, but we're going to be stuck supporting the current one for a long time yet, and depending on if the new solution targets low end targets and all render pipelines, even longer. As an example:

    Code (CSharp):
    1. public void SetDetailLayer(int xBase, int yBase, int layer, int[,] details);
    First, Unity's documentation says the details map is an R8 texture (apparently it's not, but it certainly doesn't need to be a full int and appears to be a byte worth of values). So when generating details in MicroVerse I:

    - Render a density map to an R8 texture
    - AsyncReadback this texture to a NativeArray<byte>
    - Run a Job to convert that data into a NativeArray<int>
    - Copy the NativeArray<int> into a int[,] using a custom copy operation
    - Set data on the terrain, which in theory converts it back to the R8 texture I had in the first place, wasting 12ms of processing time.

    This is silly. My guess is that the int[,] was used because it was created back when Unity supported JavaScript, which didn't have a byte type, but if the actual types used could match that would be a big help. Further, a GPU based readback version like height maps, splats, and hole maps have would be even better.

    Anyway, I know there are a ton of places where fixing the APIs would lead to large gains for developers like me, and that Unity has done some of this for things like the mesh API. Will we be seeing more of this in the future? Any particular areas?
  2. BartPieters


    Unity Technologies

    Apr 12, 2019
    Hi jbooth,

    I know in the Mesh, Texture, and Async Readback APIs we could definitely benefit from the use of Span<T> and ReadOnlySpan<T>. We also provide a lot of API using byte[] without having a NativeArray overload. I think a lot of this is historical like you suggested. It is something we feel is important and we are looking into revisiting some of these APIs to add NativeArrays and Spans support. If you would like to see us prioritize extending these APIs, please help submit a feature request here.