Search Unity

OpenCL from Unity

Discussion in 'Scripting' started by fct509, Aug 2, 2019.

  1. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    Hi,

    I'm working on a Unity Editor extension for Unity 2018.4 (I'm expecting it to work on newer versions too), and I need to do some computationally intensive work from the GPU. I already got a simple prototype working with a Compute Shader, but it turns out that the only way to run a Compute Shader is by freezing the main thread. I even tried to dispatch the Compute Shader from the an IJob, but the only thing that did was enable the Editor to catch the exception without me wrapping the code in my own try-catch.

    I asked the folks over on the GPU Progressive Lightmapper thread how they managed to run code on the GPU without freezing the main thread and they said that this was done by using OpenCL from a background thread.

    Does anyone have any recent advise on how to run OpenCL from Unity; please keep in mind that I've never used OpenCL before. Most of what I found was either people talking about error codes from the GPU Progressive Lightmapper or a few threads from 2012. Unity has changed a lot these last four years, so I'm not very hopeful when it comes to those older threads.

    Can anyone point me to some resources on running OpenCL from a background thread?

    Thanks,
    -Francisco
     
    ModLunar likes this.
  2. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    374
    I'm not sure how to run OpenCL, but I'd be really interested to hear how this could be done.

    I assume you could use something like C# TPL (Task Parallel Library) under System.Threading.Tasks to run it from a background thread though, specifically using the static method Task.Run(...).