Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Android + GearVR + Multi-Threaded

Discussion in 'AR/VR (XR) Discussion' started by Schick, Oct 30, 2015.

  1. Schick

    Schick

    Joined:
    Aug 18, 2015
    Posts:
    9
    I am trying to build a native (C++) plugin for the GearVR. But even a very easy plugin seems to fail when "Virtual Reality Supported" and "Multi-Threaded" is set. The native plugin is pretty much the following code:

    void UnityRenderEvent(int textureId)
    {
    glBindTexture(GL_TEXTURE_2D, textureId);
    FillTextureFromCode(data); // fill colors
    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
    glBindTexture(GL_TEXTURE_2D, 0);
    }

    This leads to following LogCat. As being said, "Single-Threaded" everything runs just fine. Any ideas what needs to be done for a multi-threaded environment?

    10-30 11:21:47.131: I/E:\tw\cefb39eb0290d6c5\Software\OculusSDK\Integrations\Unity\Releases\0(12330): OVR_TW_SetDebugMode(0,0)
    10-30 11:21:47.131: I/LocalPreferences(12330): Set( dev_timeWarpDebugGraphMode, 0 )
    10-30 11:21:47.131: I/LocalPreferences(12330): Couldn't open /sdcard/.oculusprefs for writing
    10-30 11:21:47.131: I/LocalPreferences(12330): Set( dev_timeWarpDebugGraphValue, 0 )
    10-30 11:21:47.131: I/LocalPreferences(12330): Couldn't open /sdcard/.oculusprefs for writing
    10-30 11:21:47.136: E/libEGL(12330): eglMakeCurrent:980 error 3002 (EGL_BAD_ACCESS)
    10-30 11:21:47.136: E/Unity(12330): [EGL] Unable to acquire context: EGL_BAD_ACCESS: EGL cannot access a requested resource (for example a context is bound in another thread).
    10-30 11:21:47.136: E/Unity(12330):
    10-30 11:21:47.136: E/Unity(12330): (Filename: ./Runtime/GfxDevice/egl/WindowContextEGL.cpp Line: 205)
    10-30 11:21:47.136: E/libEGL(12330): eglMakeCurrent:980 error 3002 (EGL_BAD_ACCESS)
    10-30 11:21:47.136: E/Unity(12330): [EGL] Unable to acquire context: EGL_BAD_ACCESS: EGL cannot access a requested resource (for example a context is bound in another thread).
    10-30 11:21:47.136: E/Unity(12330):
    10-30 11:21:47.136: E/Unity(12330): (Filename: ./Runtime/GfxDevice/egl/WindowContextEGL.cpp Line: 205)
     
  2. williamj

    williamj

    Unity Technologies

    Joined:
    May 26, 2015
    Posts:
    94
    Hi @Schick

    What version of Unity are you using? This is a known issue that has been fixed, I just want to make sure that the issue is not a regression.

    -Will
     
  3. Schick

    Schick

    Joined:
    Aug 18, 2015
    Posts:
    9
    Unity v5.2.2f1, Oculus Utilities v0.1.2.0, OVRPlugin v0.1.2.1.
    UnityEngine.Debug:Log(Object)
    OVRManager:Awake() (at Assets/OVR/Scripts/OVRManager.cs:314)
     
  4. Schick

    Schick

    Joined:
    Aug 18, 2015
    Posts:
    9
    @williamj It's a Note4, running Android 5.1.1.

    It is good to know that there might be a regression, I ran out of ideas :)
     
  5. Schick

    Schick

    Joined:
    Aug 18, 2015
    Posts:
    9
    When I am using a shared OpenGL context (created from "UnitySetGraphicsDevice") any access to a Unity created Texture2D fails.

    In my specific case "glFramebufferTexture2D" fails for GearVR. As mentioned the code runs perfectly fine when using single-threaded rendering for Unity or when disabling "Virtual Reality Supported".

    mTextureId = ... // from a Unity created Texture2D, the id is valid
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTextureId, 0);
    <-- fails with GL Error 1282
     
  6. williamj

    williamj

    Unity Technologies

    Joined:
    May 26, 2015
    Posts:
    94
    Sorry, I think the issue I mentioned earlier is actually unrelated. Can you submit a bug report with a small project that reproduces the problem and PM me the report number? I can take a closer look at the problem this way.

    Thanks,

    Will
     
  7. Schick

    Schick

    Joined:
    Aug 18, 2015
    Posts:
    9
    Hi Will,

    it seems that it is an issue on our site :-( I got a multi-threaded version running with a simple example.

    Our "real" project using a shared OpenGL context that is created with the first call to "UnitySetGraphicsDevice". Then a thread is created that uses that shared context to process an incoming camera frame. We need a shared context because we want to enable the camera frame for Unity as well (live camera texture).

    What drives me crazy is that our plugin only fails if "Virtual Reality Supported" is enabled. Are there any changes to the OpenGL context if we enable VR that do not support context sharing?

    Best, Fred
     
  8. Schick

    Schick

    Joined:
    Aug 18, 2015
    Posts:
    9
    Quick overview

    Init: UnitySetGraphicsDevice -> Create shared context -> Create thread -> Make shared context current -> Initialize our library -> Close thread

    Running: On every new camera frame -> Make shared context current -> Copy texture using a shader to a Unity Texture2D object

    That is it.
     
  9. Schick

    Schick

    Joined:
    Aug 18, 2015
    Posts:
    9
    Hi Will,

    everything runs now.

    Problem was: UnitySetGraphicsDevice may be called with an invalid OpenGL context. Only UnityRenderEvent will always have a valid OpenGL context.

    When using the GearVR it seems that UnityRenderEvent may be called before UnitySetGraphicsDevice.

    Best, Fred