Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Graphics Texture2DArray Apply

Discussion in '5.4 Beta' started by NerdyOrc, Mar 30, 2016.

  1. NerdyOrc

    NerdyOrc

    Joined:
    Aug 6, 2013
    Posts:
    17
    Hi, I am testing the new Texture2DArray class, when using the function Apply() it seems to send all the textures to the GPU, even if I didn't alter most of them, is this the case?If so is there any chance it might change?
    I was hoping to use the Texture2DArray in my project to save both GPU processing by using fast indexation in my shader and save on the cost of transfer to the GPU.
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Currently, yes. This is very similar to Texture2D.Apply, where whole texture data is sent even if you only changed one pixel. Someday we might optimize this away somehow, but not quite yet.

    You could try using Graphics.CopyTexture to copy just one array slice of pixel data, if you already have it in some other texture on the GPU.
     
    gpierrot likes this.
  3. NerdyOrc

    NerdyOrc

    Joined:
    Aug 6, 2013
    Posts:
    17
    Thanks!
    That might do the trick i.e sending the a single texture and than copying that texture into a single array element.
     
  4. TimCabbage

    TimCabbage

    Joined:
    Feb 13, 2011
    Posts:
    66
    @Aras - this means that Graphics.CopyTexture works only with textures that are uploaded to the GPU?
    That would explain it - had to go for software atlasing because on build it didn't work for me, will check if both textures were uploaded at that time and retest.
     
  5. andywatts

    andywatts

    Joined:
    Sep 19, 2015
    Posts:
    110
    Would anyone mind sharing or pointing to shader code that uses the new texture2DArray?
     
  6. raphael-ernaelsten-heaj

    raphael-ernaelsten-heaj

    Joined:
    Mar 9, 2016
    Posts:
    78
    Here is some pseudo code straight out of my mind...

    In your shader:
    Code (CSharp):
    1. Texture2DArray<data type> _textureArray;
    Later in your shader you can call any of the Texture2DArray method referenced here:
    https://msdn.microsoft.com/en-us/library/windows/desktop/ff471526(v=vs.85).aspx
    (note that the compiler is sometimes unfriendly with some of them :p )

    In C#:
    Code (CSharp):
    1. Texture2DArray textureArray = new Texture2DArray( sizeX, sizeY, amountOfSlices, textureformat, mipMapsOrNot);
    2. Graphics.CopyTexture( inputTextureObject, inputSliceIndex, inputMipIndex, textureArray, desiredSliceIndex, desiredMipIndex);
    3.  
    4. yourMaterial.SetTexture("_textureArray",  textureArray);

    Cheerz!
     
    Covfefeh likes this.
  7. NerdyOrc

    NerdyOrc

    Joined:
    Aug 6, 2013
    Posts:
    17
    Sorry for the delay, but here you go, I am using for a terrain, so you can ignore the Vertex shader part of it if you want, you can use the code that Aras mentioned to fill in the Texture2DArray with actual textures.
     

    Attached Files: