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

Using Compute Shaders in Unity SteamVR (pipeline lock)

Discussion in 'Shaders' started by thepts, Aug 3, 2016.

  1. thepts

    thepts

    Joined:
    Aug 3, 2016
    Posts:
    5
    The case is really quite simple:

    * I need to use a Compute Shader in my project.

    * But: SteamVR has a "lock" on GPU processing while Update-functions are running. So if you call a ComputeShader from Update / FixedUpdate etc, you will not receive your results before VSync (which in SteamVR takes about 10ms - effectively chopping framerate in half).

    * I can't use threading or similarly to Dispatch the shader and GetData from it, because in Unity this needs to happen in the main thread.


    So the question is:

    Are there any sneaky Unity tricks to get a snippet of code to run in the main thread, before the rendering job is executed?

    What I've tried so far:

    I tried attaching a script to the camera and calling ComputeShaders from camera events (OnPreRender, OnRender*, OnPostRender - https://docs.unity3d.com/Manual/ExecutionOrder.html ), but no luck.

    I just found out about the NativePluginInterface, where I can queue up callbacks in the render thread. https://docs.unity3d.com/Manual/NativePluginInterface.html

    * Could this be a possible clue?
    * Are there other "eventslots" in Unity Execution Order I should know about?
    * Or am I completely missing the point here, and there is some other standard way of executing Compute Shaders that SteamVR wants me to use?
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
  3. thepts

    thepts

    Joined:
    Aug 3, 2016
    Posts:
    5
    Yes unfortunately, I've tried all the camera events as mentioned above.

    This goes a bit deeper, SteamVR seems to run a "homebrewn" VSync directly by calling on Unity's WaitForEndOfFrame.

    This appears to bind the GPU exclusively to either 1) Unity Camera rendering or 2) Unity WaitForEndOfFrame. Meaning that there is no GPU-time left for anything else.

    One question there is how WaitForEndOfFrame() really works with regards to custom display devices like the Vive. Does it call a VSync method directly via DirectX? Does the VSync call bind up the GPU somehow?
     
    Last edited: Aug 3, 2016
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
  5. thepts

    thepts

    Joined:
    Aug 3, 2016
    Posts:
    5
    Yes, I've used a combination of execution order and all the Camera Events (OnPreCull, OnPreRender etc - see https://docs.unity3d.com/Manual/ExecutionOrder.html ) to try to find a slot where the GPU isn't locked up and where I can do my stuff during Vertical Sync wait.

    Like I mention above, right now I fear that Unity uses OpenVR to VSync, and that the method used in OpenVR simply binds the GPU up for: a) basic rendering and b) VSync wait, leaving no room for anything else.
     
  6. chuchur

    chuchur

    Joined:
    Dec 5, 2013
    Posts:
    4
    Is something new in 5.6 about this?
     
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    It's sad there's no official reply for these questions, but are you sure your issue will be the same?