Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Bug (Case 1408325) ScriptedImporter problem with "Load texture data on demand"

Discussion in '2022.1 Beta' started by Peter77, Mar 3, 2022.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Hello wonderful people of the Unity beta forum, long time no see :)

    My Texture3D Atlas Import Pipeline (link) causes problems in Unity 2022.1 with "Load texture data on demand" enabled.



    Case 1408325 contains the project I use in the video.
     
    LeonhardP and Prodigga like this.
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    chrismarch and Prodigga like this.
  3. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,135
    Hi @Peter77, good to see you! Thanks as always for the great report.
     
    Peter77 likes this.
  4. Genebris

    Genebris

    Joined:
    Mar 18, 2013
    Posts:
    144
    Not sure if I should report it separately or add to this report. When I make a build with this option, some textures end up empty in a build. I also get this errors multiple times during building (it says access denied). And error "There was no texture data to upload" appears in a log.
    2022.2.16
    I've spend two weeks not understanding what is going on.
    upload_2023-4-26_17-4-54.png

    Would love to comment on issue tracker if it allowed me to log in at all.
     
    Last edited: Apr 26, 2023
  5. hopelesscate

    hopelesscate

    Joined:
    Mar 1, 2017
    Posts:
    4
    Same issue for me on 2022.2.18f1

    UPD: Also reproduces on 2022.3.5f1 (LTS). Would hope that LTS will also get the fix.
     
    Last edited: Aug 7, 2023
  6. Genebris

    Genebris

    Joined:
    Mar 18, 2013
    Posts:
    144
    Still reproducing on 2022.3.16. Have to always turn it off before build.
     
  7. jonaskhalil

    jonaskhalil

    Unity Technologies

    Joined:
    Apr 6, 2019
    Posts:
    33
    Hey, I wasn't aware there was this forum thread as well ... @Peter77 and @Genebris , to what extent is it a solution to mark your source textures as readable?

    In an accurate sense, this is actually by design but CopyTexture is not strict enough apparently ... As the docs mention, "This method copies pixel data from one texture to another on the GPU. If you set Texture.isReadable to true for both src and dst textures, the method also copies pixel data on the CPU."; so, since this code is trying to store a Texture3D asset using the CPU data, by design the source should have its data readable ... The observed effect is indeed this: there's no readable data, so nothing gets copied and you see garbage in that Texture3D.

    Just for me to get some grip on the context: is having those source textures readable a solution here? Are there reasons why this is actually not a (desired) option? Or was it simply not clear that this was the cause of the issues?
     
    Last edited: Mar 27, 2024
  8. Genebris

    Genebris

    Joined:
    Mar 18, 2013
    Posts:
    144
    I'm marking several textures as read/write in inspector to solve this bug in the editor. Some special textures appear empty even in the editor. This is not happening with textures for regular models but for things like clouds texture in HDRP cloud layer or SMAA texture which results in SMAA doing nothing. Marking them as read/write solves this. But this is in the editor. I'm going to assume this also works in build but I obviously can't mark all my textures as readable as this doubles the memory cost afaik. In build this can happen for any random textures used by models or .exe icon or cursor, so this would require marking everything.
     
  9. jonaskhalil

    jonaskhalil

    Unity Technologies

    Joined:
    Apr 6, 2019
    Posts:
    33
    Well, since the original post concerned a "Texture3D Atlas Import Pipeline" I was under the impression that this was about behavior in the Editor :)

    Regarding behavior in Player builds, this should never have worked ... If something did regress there, that sounds like a genuine bug and should be reported. "Load texture data on demand" should definitely not affect Player behavior.

    But in short, yes, if you need the CPU data you need to mark the textures to be readable. It indeed "doubles" the memory cost, in the sense that you'll have a copy in CPU memory and a copy in GPU memory. Again, this is only if you need the data on the CPU. I don't know the exact use-case, but:
    • if you just need to copy the data for rendering, just the GPU copy maybe suffices and you don't need readable textures?
    • if you actually need to do some pixel operations, maybe an alternative is to implement in a compute shader to again avoid needing readable textures?
    • if you know when you "no longer need the CPU data", you can call tex.Apply with makeNoLongerReadable to discard the CPU memory ... Just mentioning it in case it would be a viable way forward

    Does any of this help you? Or am I missing details for your use-case?
     
  10. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Hi jonaskhalil,

    thank you for your interest to help.

    I use Graphics.CopyTexture to copy a Texture2D into a Texture3DArray, because it doesn't require the source texture to be marked as readable and is quite fast.

    The problem is that Graphics.CopyTexture doesn't work when the "Load texture data on demand" project setting is enabled.

    What I'm looking for is a way in the editor to make Graphics.CopyTexture work when "Load texture data on demand" is enabled. My idea why that happens was that perhaps the texture data isn't loaded when I call Graphics.CopyTexture, because it's loaded on-demand only (perhaps even asynchronously).

    I'm looking for a way to load the texture data synchronously to make Graphics.CopyTexture work (without altering any asset settings), even when the "Load texture data on demand" project setting is enabled.

    Making every texture "isReadable" doesn't sound like a great solution to me.

    The code utilizes Graphics.CopyTexture that, according to the Unity documentation, performs the copy on the GPU.
     
    Last edited: Mar 28, 2024
  11. Genebris

    Genebris

    Joined:
    Mar 18, 2013
    Posts:
    144
    Have you tried toggling Load on demand option right before doing your logic? It's not in the API so I had to edit the config file itself in the build pre processor. In my experience it solves the problem most of the time.