Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Texture SetPixel vs GetRawTextureData way

Discussion in 'General Graphics' started by castor76, Feb 23, 2021.

  1. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,087
    It is interesting to find out, Fo setting a single pixel on a texture :

    1. Using SetPixel then Apply
    2. Using GetRawTextureData and then set the data then Apply

    which one would be faster? This is for setting a single pixel only.

    I am guessing that internally Unity will do GetRawTextureData even if we use SetPixel API ?
     
  2. npatch

    npatch

    Joined:
    Jun 26, 2015
    Posts:
    180
    Looking at UnityCsReference, SetPixel is calling a native function, without any mention of GetRawTextureData. Internally I'd expect it to get access to the system buffer and just change the value with pointer math. Which is similar to how you'd work with GetRawTextureData in C#. I'd expect for 1 pixel to have negligible impact.
    Documentation of SetPixel mentions SetPixels being faster when "regenerating a texture every frame, especially large textures".
     
  3. AljoshaD

    AljoshaD

    Unity Technologies

    Joined:
    May 27, 2019
    Posts:
    68
    SetPixel will need to convert the pixel from a float format (Color is 4 floats) to the format of the texture. Potentially, it could be faster to write directly to the texture memory in the correct format (8bit/channel for example).
    But if you call SetPixel only a few times per frame then the performance impact will be negligible so it's best to pick the option that reduces the amount of code you need to write.

    You can find some additional info the difference between GetPixels and GetRawTextureData here.
    We are also improving the documentation of these methods for 2021.2. This should be available soon.
     
    bgolus and npatch like this.
  4. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,087
    I just wonder how much does GetRawTextureData cost in terms of overhead of setting the data up for the scripts to access them. I wonder if the performance or memory wise there is difference if I use GetRawTextureData on the textures of different sizes?

    Because I can just use SetPixel to one pixel, but on large texture such as 2k texture. But then I can also use GetRawTextureData to get all the data and set one value. I wonder which one is faster... does getting managed reference to the texture data using GetRawTextureData costs differently per texture size?
     
  5. AljoshaD

    AljoshaD

    Unity Technologies

    Joined:
    May 27, 2019
    Posts:
    68
    The cost of GetRawTextureData should be independent from the texture size. It just returns a wrapper around the native pointer. It's something you could measure if it is really important for your usage case. Let us know what you find! :)
     
    npatch likes this.
unityunity