Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Best solution for a long-running job?

Discussion in 'Burst' started by MaxEdOwlcat, Feb 27, 2021.

  1. MaxEdOwlcat

    MaxEdOwlcat

    Joined:
    Nov 26, 2019
    Posts:
    8
    I have some code to compress textures on the fly, in background. I managed to implement it with a Bursted Job, but there is a problem: it seems like I've chosen the wrong tool, because Jobs aren't meant to last more than 4 frames, yet I have no upper bound on time needed to finish the compression, and I don't care (i.e. I don't want to call Complete() on a compression job from anywhere - instead, I'd like to receive a callback when it's done).

    What's my options here? I'm thinking about running compression in a separate thread and using a Burst-compiled delegate for actual compression code, but delegates have too many restrictions compared to jobs. Mainly, they can't use NativeArray, and can't use byte[], too - frankly, I don't understand what kind of memory can I allocate for my temporary arrays inside delegate. Documentation shows an example for using a static readonly array (I'm fine with it), but I think it only works if the array is defined inside a Job structure, which I don't have, since I'm not using jobs in this case. I've tried it with a non-Job class, and I get error about unsupported type in runtime.
     
    Ashkan_gc likes this.
  2. MaxEdOwlcat

    MaxEdOwlcat

    Joined:
    Nov 26, 2019
    Posts:
    8
    Finally resolved it by declaring my helper arrays as fixed fields inside a structure that gets passed from the compiled delegate to compression functions (i.e.
    Code (CSharp):
    1. public fixed byte array[32];
    ). This let me use byte* in all function declarations where these arrays are needed, and Burst is OK with that.