Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Problem with GL.IssuePluginEvent in Unity 5.3 OS X editor, maybe the OpenGL Core?

Discussion in 'Editor & General Support' started by onevcat, Dec 9, 2015.

  1. onevcat


    Jan 4, 2013
    Hi, Unity team

    Thanks for the effort on the new OpenGL Core support for OS X. I am developing a plugin to render a texture in native code for Unity OS X Editor. This the core code in native side:

    Code (CSharp):
    1. // The plain image data is stored in `bitmap`
    2. NSInteger samplesPerPixel = [bitmap samplesPerPixel];
    3. int rowLength = 0;
    4. int unpackAlign = 0;
    6. // Save original value temporary
    7. glGetIntegerv(GL_UNPACK_ROW_LENGTH, &rowLength);
    8. glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpackAlign);
    10. // Calculate and store necessary values for texture
    11. glPixelStorei(GL_UNPACK_ROW_LENGTH, (int)[bitmap bytesPerRow] / samplesPerPixel);
    12. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    14. // `textureId` is the pointer for the texture shared between Unity and native
    15. glBindTexture(GL_TEXTURE_2D, textureId);
    18. if (![bitmap isPlanar] &&
    19.     (samplesPerPixel == 3 || samplesPerPixel == 4)) {
    20.     glTexImage2D(GL_TEXTURE_2D,
    21.                 0,
    22.                 samplesPerPixel == 4 ? GL_RGBA8 : GL_RGB8,
    23.                 (int)[bitmap pixelsWide],
    24.                 (int)[bitmap pixelsHigh],
    25.                 0,
    26.                 samplesPerPixel == 4 ? GL_RGBA : GL_RGB,
    27.                 GL_UNSIGNED_BYTE,
    28.                 [bitmap bitmapData]);
    29. }
    31. // Restore values
    32. glPixelStorei(GL_UNPACK_ROW_LENGTH, rowLength);
    33. glPixelStorei(GL_UNPACK_ALIGNMENT, unpackAlign);
    It is nothing more than binding and rendering some image data. I am using the GL.IssuePluginEvent(IntPtr callback, int eventID) to issue the render process and everything works fine in Unity versions before Unity 5. However, after upgrading to Unity 5.3 today, I can only get a gray plane with half transparency.

    I noticed that the new OpenGL Core is used in OS X Editor in Unity 5.3, so I guess it caused this problem. I tried to launch Unity 5.3 with "-force-opengl" option and everything goes back fine for me. But it seems that the legacy OpenGL back-end would be removed in 5.4, so it could not be a long run for me.

    I also tried the "-force-clamped" option, and texture could be rendered correctly. So I guess it is some platform related issue. Would you please take a look at it? I tested in a rMBP Early 2013 and an iMac Late 2013, both with OS X 10.11.1.

    P.S. I also tried the low-level rendering plugin example project in this manual page with Unity 5.3, it turns out that the sample project could not work properly as well, maybe due to the similar issue?

    Any advice or help would be appreciated. Thanks in advance!
    Last edited: Dec 9, 2015
  2. neginfinity


    Jan 27, 2013
    Usually, when something like that happens, I'd try to run glGetError() after every command to pinpoint which one causes the trouble. Have you tried that?

    Do you refer to OpenGL 4 "core profile" or OS X specific feature?
  3. onevcat


    Jan 4, 2013
    Thanks for reply. I will try glGetError() as well to see if I can find anything.

    I didn't change anything after installing Unity 5.3. By saying "OpenGL Core" I mean the newly introduced OpenGL Core back-end for OS X:
    • Graphics: New OpenGL Core (GL3/4) back-end for Mac OS X editor and player
    I could see "<OpenGL 4.1>" is labeled in the title bar of Unity 5.3, so I guess it is the version I am using.

    Attached Files:

  4. onevcat


    Jan 4, 2013
    I tried the glGetError() and it turns out the problem is in glTexImage2D, which gives me a 0x502 (GL_INVALID_OPERATION). I will look deeper into it to see how could I do with it now.
  5. onevcat


    Jan 4, 2013
    After some investigating, according to the reference of OpenGL 4 ,I found the `GL_INVALID_OPERATION` of "glTexImage2D" would be generated if:

    But it seems none of them fit for my situation. And since the same code could run without errors before Unity 5.3 and even in Unity 5.3 with "-force-clamped", I am confused now and have no idea on what caused this problem.
  6. groovounet


    Unity Technologies

    Jul 15, 2013

    The link is being updated. Meanwhile the native plugin for 5.3 with OpenGL core support is available here:

    The native plugin should also be included in 5.3 installer already.

    Let me know if that help.

  7. neginfinity


    Jan 27, 2013
    I don't see any obvious problems with the code, but here are the steps I would try:

    I checked my old opengl code, and the only one place where I ever used "glPixelStorei(GL_UNPACK_ROW_LENGTH .. )" iwhen I had call to glTexSubImage2D. glTeximage2D should work without it.

    In your case, I'd try to commenting that line out and see what happens.

    I would also temporarily try to temporarily create 1x1 texture (instead of using bitmap data) from predefined constant and see if that works - just to check if there's a problem with size calculations.

    Another possible issue is that your gl implementation might not like GL_RGBA8 being used as internal format for whatever reason. Check if using GL_RGBA (not GL_RGBA8) works.

    It also makes sense to check gl version, vendor and extension in both old and new versions and see if their output is different. See:

    That's just general troubleshooting steps, though, can' recommend anything more specific than that.
  8. onevcat


    Jan 4, 2013
    Hi, Christophe. Thanks for the information. The new sample project works well in Unity 5.3.0, and it becomes a good example for me to dig in.

    Thanks, @neginfinity. I just found the reason. It seems that the size (width and height) of my texture is not match the bitmap I am drawing to the texture. It goes well in both legacy OpenGL and the new OpenGL Core without extension ("-force-clamped"), but without luck if I use the normal one. After recalculating the generated texture width and height and let it matching the bitmap, everything goes well for me again.

    Cheers! Thanks for all your help!
  9. Aurore


    Head of Learn Content Production Unity Technologies

    Aug 1, 2012
    This is not for the general discussion forum.