Search Unity

Resolved Correct way to create texture at runtime with compression, mipmaps, & makeNoLongerReadable?

Discussion in 'General Graphics' started by Lo-renzo, Sep 23, 2020.

  1. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,513
    I'm generating a texture at runtime. I want compression and mipmaps. This is how I'm getting it to work now.
    Code (CSharp):
    1. targetTex.Apply(updateMipmaps: true);
    2. targetTex.Compress(true);
    3. targetTex.Apply(updateMipmaps: false, makeNoLongerReadable: true);
    Is the above the correct way to do things? The first Apply generates the mipmaps, then we compress it all, then we apply again to make sure everything is good?

    If I don't plan on updating this texture further, I should include makeNoLongerReadable=true? Are there any consequences to that? I remember reading that Unity can lose control of graphics in certain situations (locked computer, switching apps), requiring a reupload to the GPU or else there's pink materials. Is makeNoLongerReadable dangerous when it's a runtime-generated texture with no preserved system copy? Or am I misunderstanding?
     
  2. cholleme

    cholleme

    Unity Technologies

    Joined:
    Apr 23, 2019
    Posts:
    31
    That sequence looks all-right. Compress will simply compress the mips that are there. The engine will take care of reloading textures after loosing the graphics device so you do not need to worry about this. If you do not need to access the texture on the cpu anymore making it non-readable will save you memory. If you keep it readable the second apply can be omitted as Compress does something similar to apply(false,false) internally.
     
    Lo-renzo likes this.
  3. Marco-Playable

    Marco-Playable

    Joined:
    Nov 12, 2020
    Posts:
    53
    Why is that first Apply before Compress necessary? I am assuming the compression is done on CPU side, so uploading the texture to GPU with Apply shouldn't be necessary but I can't get it to work without that call.