Unity-Technologies / NativeRenderingPlugin

C++ Rendering Plugin example for Unity
https://docs.unity3d.com/Manual/NativePluginInterface.html
MIT License
815 stars 163 forks source link

Can't work properly in Android #11

Open kidapu opened 4 years ago

kidapu commented 4 years ago

I have build this repository with Unity2019.2, targeting iPhone and Android.

capture-2

iPhone8, iOS 13.6 works successfully. But Android9 (LG V30+) can't work properly.

I tried several Graphics APIs, such as Vulkan, OpenGLES2, OpenGLES3. But all can't work.

Please let me know if you have good idea.

kidapu commented 4 years ago

I updated Android.mk and rebuilt libRenderingPlugin.so.

APP_OPTIM        := debug
APP_ABI := armeabi-v7a arm64-v8a 
APP_PLATFORM := android-21
# APP_STL := gnustl_static
APP_STL          := c++_static
APP_CPPFLAGS += -std=c++11
NDK_TOOLCHAIN_VERSION := clang
APP_BUILD_SCRIPT := Android.mk

And tried to build apk, but the app crashes when it's launched. This is the crash log.

$ adb logcat --buffer=crash
09-03 18:29:23.219 12173 12191 F libc    : ./../../../source/RenderAPI_OpenGLCoreES.cpp:182: void RenderAPI_OpenGLCoreES::CreateResources(): assertion "glGetError() == GL_NO_ERROR" failed
09-03 18:29:23.411 12173 12191 F libc    : Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0xdce2df00 in tid 12191 (UnityMain), pid 12173 (com.Company.aaa)
09-03 18:29:23.473 12232 12232 F DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
09-03 18:29:23.480 12232 12232 F DEBUG   : Build fingerprint: 'lge/joan_dcm_jp/L-01K:9/PKQ1.190414.001/20072184679ed:user/release-keys'
09-03 18:29:23.480 12232 12232 F DEBUG   : Revision: '12'
09-03 18:29:23.480 12232 12232 F DEBUG   : ABI: 'arm'
09-03 18:29:23.480 12232 12232 F DEBUG   : pid: 12173, tid: 12191, name: UnityMain  >>> com.Company.aaa <<<
09-03 18:29:23.480 12232 12232 F DEBUG   : signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0xdce2df00
09-03 18:29:23.480 12232 12232 F DEBUG   : Abort message: './../../../source/RenderAPI_OpenGLCoreES.cpp:182: void RenderAPI_OpenGLCoreES::CreateResources(): assertion "glGetError() == GL_NO_ERROR" failed'
09-03 18:29:23.480 12232 12232 F DEBUG   :     r0  c7ff984c  r1  c7ff9958  r2  00000000  r3  dce2df00
09-03 18:29:23.480 12232 12232 F DEBUG   :     r4  c7ff984c  r5  db90ab00  r6  00000000  r7  c7ff9958
09-03 18:29:23.480 12232 12232 F DEBUG   :     r8  00002f9f  r9  c7ff9e80  r10 c7ff9f00  r11 c7ff9a58
09-03 18:29:23.480 12232 12232 F DEBUG   :     ip  c603d77c  sp  c7ff96d8  lr  c5c95384  pc  dce2df00
09-03 18:29:23.500 12232 12232 F DEBUG   :
09-03 18:29:23.500 12232 12232 F DEBUG   : backtrace:
09-03 18:29:23.500 12232 12232 F DEBUG   :     #00 pc 00002f00  <anonymous:dce2b000>
09-03 18:29:23.500 12232 12232 F DEBUG   :     #01 pc 0018c380  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libmonobdwgc-2.0.so (mono_walk_stack_full+1544)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #02 pc 0018a020  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libmonobdwgc-2.0.so (mono_walk_stack_with_state+308)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #03 pc 0018c588  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libmonobdwgc-2.0.so (mono_walk_stack+64)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #04 pc 009752df  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libunity.so (NativeRuntimeException::signal_handler(int, siginfo*, void*)+334)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #05 pc 0025e5d0  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libmonobdwgc-2.0.so (mono_chain_signal+180)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #06 pc 0025e72c  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libmonobdwgc-2.0.so (__krait_sigabrt_signal_handler+148)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #07 pc 00019a24  /system/lib/libc.so
09-03 18:29:23.500 12232 12232 F DEBUG   :     #08 pc 0001ce26  /system/lib/libc.so (abort+54)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #09 pc 0001d0a3  /system/lib/libc.so (__assert2+22)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #10 pc 0000e9a8  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libRenderingPlugin.so (RenderAPI_OpenGLCoreES::CreateResources()+516)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #11 pc 0000eb44  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libRenderingPlugin.so (RenderAPI_OpenGLCoreES::ProcessDeviceEvent(UnityGfxDeviceEventType, IUnityInterfaces*)+48)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #12 pc 0000c60c  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libRenderingPlugin.so
09-03 18:29:23.500 12232 12232 F DEBUG   :     #13 pc 0000c4a0  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libRenderingPlugin.so (UnityPluginLoad+124)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #14 pc 0076da01  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libunity.so (RegisterPlugin(void*, UnityPluginFunctions&)+196)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #15 pc 0076db55  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libunity.so (InitializePlugin(void*)+92)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #16 pc 0076e109  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libunity.so (FindAndLoadUnityPlugin(char const*, void**)+120)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #17 pc 002f9294  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libmonobdwgc-2.0.so (mono_lookup_pinvoke_call+1056)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #18 pc 0030dc0c  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libmonobdwgc-2.0.so (mono_marshal_get_native_wrapper+640)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #19 pc 00090aac  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libmonobdwgc-2.0.so (mono_method_to_ir+48488)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #20 pc 00065200  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libmonobdwgc-2.0.so (mini_method_compile+4240)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #21 pc 0006a0d4  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libmonobdwgc-2.0.so (mono_jit_compile_method_inner+1808)
09-03 18:29:23.500 12232 12232 F DEBUG   :     #22 pc 0006ff00  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libmonobdwgc-2.0.so (mono_jit_compile_method_with_opt+1948)
09-03 18:29:23.501 12232 12232 F DEBUG   :     #23 pc 0006f74c  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libmonobdwgc-2.0.so (mono_jit_compile_method+80)
09-03 18:29:23.501 12232 12232 F DEBUG   :     #24 pc 001940bc  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libmonobdwgc-2.0.so (common_call_trampoline+2752)
09-03 18:29:23.501 12232 12232 F DEBUG   :     #25 pc 001935ac  /data/app/com.Company.aaa-CZ--GOFq6q-9LFPUXF3Yqw==/lib/arm/libmonobdwgc-2.0.so (mono_magic_trampoline+188)
09-03 18:29:23.501 12232 12232 F DEBUG   :     #26 pc 0000006a  <anonymous:e6280000>

When the plugin is loaded, it's down in this line. https://github.com/Unity-Technologies/NativeRenderingPlugin/blob/master/PluginSource/source/RenderAPI_OpenGLCoreES.cpp#L182

UnityPluginLoad() => OnGraphicsDeviceEvent() => RenderAPI_OpenGLCoreES::ProcessDeviceEvent() => CreateResources() => Crash ><

kidapu commented 4 years ago

When I run glGetError() before assert(), I got this view on Android. Maybe only vertex shader is disable.

IMG_4019

And I printf( glGetError() ), it says GL_INVALID_ENUM ( int 1280).

I think the error occurs before RenderPlugin.cpp => UnityPluginLoad().

kidapu commented 4 years ago

I forked your repository, and add some code for investigating this bug. https://github.com/kidapu/NativeRenderingPlugin/commits/master

alienyang commented 3 years ago

Hi @kidapu,

Root cause is RenderAPI_OpenGLCoreES::BeginModifyVertexBuffer return 0, so ModifyVertexBuffer is not working. I have walkaround this issue by use GLES3 glMapBufferRange to get point of vertex buffer:

void* RenderAPI_OpenGLCoreES::BeginModifyVertexBuffer(void* bufferHandle, size_t* outBufferSize)
{
    glBindBuffer(GL_ARRAY_BUFFER, (GLuint)(size_t)bufferHandle);
    GLint size = 0;
    glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
    *outBufferSize = size;
    void* mapped = glMapBufferRange(GL_ARRAY_BUFFER, 0, size, GL_MAP_WRITE_BIT);
    return mapped;
}

void RenderAPI_OpenGLCoreES::EndModifyVertexBuffer(void* bufferHandle)
{
    glBindBuffer(GL_ARRAY_BUFFER, (GLuint)(size_t)bufferHandle);
    glUnmapBuffer(GL_ARRAY_BUFFER);
}

Then it is working fine on Android: 螢幕擷取畫面 2021-08-09 172132