Kinc icon indicating copy to clipboard operation
Kinc copied to clipboard

Instanced rendering on android-native gives black screen

Open raouftouti opened this issue 5 years ago • 14 comments

Hello, it seems like there is an issue with the android-native export on my smartphone (Galaxy A5 2017, with Android 7.0).

With my own project, if I don't use geometry instancing, it works perfectly, but if I use instancing, it gives me a black screen. My smartphone supports OpenGL ES 3.2, so it should in theory support instancing.

With an older version of Kha, it was giving me an error after the call to glVertexAttribDivisor, but now with the latest git version, it doesn't give me an error anymore, but still the black screen. It works well with the Windows and HTML5 exports.

I also tried the instancing sample, but it didn't work on my smartphone (gave me a orange/yellow screen). It works on HTML5.

Maybe an OpenGL ES 3 driver issue?

raouftouti avatar Jan 18 '20 13:01 raouftouti

I looked at the code for the OpenGL graphics4 Kinc backend and found this: https://github.com/Kode/Kinc/blob/d7a6c06309ffb7ec3264ec6d99a97e4015ada5d7/Backends/Graphics4/OpenGL/Sources/Kore/OpenGL.cpp#L357

It doesn't call glDrawElementsInstanced if it is using OpenGL ES. Is it normal?

raouftouti avatar Jan 19 '20 14:01 raouftouti

As I guessed, we still have to check and load the extension on ES.

RobDangerous avatar Jan 19 '20 15:01 RobDangerous

So the extensions are actually loaded now - the code was already there, just inactive because the used Android API level has to set in the khafile (it defaults to the lowest possible which does not support ES 3) and a bug in kincmake in doing so. Still isn't actually rendering the sample though, there's more to do.

RobDangerous avatar Oct 22 '20 20:10 RobDangerous

Seems like glVertexAttribDivisor is not called when using OpenGL ES (in Kinc, file called VertexBufferImpl.c)

raouftouti avatar Oct 22 '20 21:10 raouftouti

Good catch, thanks, that's certainly one aspect of it. Enabling it crashes it for me though. Hm, still more to do but I need sleep now.

RobDangerous avatar Oct 22 '20 21:10 RobDangerous

GitHub, stop auto-closing when I write "partially fix".

RobDangerous avatar Dec 26 '20 08:12 RobDangerous

The example at https://github.com/Kha-Samples/InstancedExample gives me an orange/yellow screen also — but simply updating to current git version of Kha makes it work both in my device and in an emulator. That said, it looks like there is something that could be wrong, since the emulator sometimes outputs a message about low memory [1] (10~20 seconds after initiating the app), sometimes followed by a crash. In the device it looks like it doesn't crash, but maintaining the app running a few minutes it could start behaving weird — screen starts to flick a lot and instead of the camera rotating around, it looks like it gets stuck between couple of angles and... vibrates. It looks like rotating your device can trigger the error sooner.

@RobDangerous I can make some specific tests or give more information about my devices, just tell me if I can help somehow testing this.

[1] 2021-03-20 11:37:58.922 15100-15100/tech.kode.kore V/threaded_app: LowMemory: 0xdb9aad40

Sanva avatar Mar 20 '21 10:03 Sanva

Hu, it works? Oh, that's very interesting, also the behavior over time. Is there some panel in Android Studio that shows overall RAM usage over time? Have to check.

RobDangerous avatar Mar 20 '21 13:03 RobDangerous

Is there some panel in Android Studio that shows overall RAM usage over time?

Yes, accesible from View → Tool Windows → Profiler:

Android Profiler showing CPU, memory and energy usage

Android Profiler showing more detailed memory usage

Sanva avatar Mar 21 '21 10:03 Sanva

By the way, you have a lot of information about it here → https://developer.android.com/studio/profile/memory-profiler .

Sanva avatar Mar 21 '21 10:03 Sanva

@RobDangerous I've been testing this again today and I've noticed that, even working fine, the memory consumption in Linux is also very high, growing to 1 or even 2 GiB in seconds. I thought that this could be a memory leak somewhere, but the memory consumption, even growing quickly, wasn't growing linearly but only sometimes. Upon closer inspection off what https://github.com/Kha-Samples/InstancedExample does, it looks like the excessive memory allocations when calculating the per-instance transformation matrices makes the app allocate memory faster than the GC releases it...

Adding

#if cpp
cpp.vm.Gc.run(false);
#end

at the end of the render function solves the problem, both in Linux and Android, lowering the memory consumption in Linux to ~55 MiB.

I suppose the same could be achieved manually allocating the needed Matrix4 matrices and doing the math without intermediate objects.

Sanva avatar Apr 04 '21 17:04 Sanva

Matrices don't allocate at all when used in a way that pleases Haxe's inline constructor mechanism - so that's what we should do. But that's luckily only a problem with the Kha sample then.

RobDangerous avatar Apr 04 '21 17:04 RobDangerous

I dug a bit deeper into this issue:

Aside from the memory issue, the instanced rendering works when targeted to android-native but still does not work on android-hl. It complies and runs, but still displays a yellow-orange screen.

When I debug with android studio, I noticed that this line g.instancedRenderingAvailable() is false https://github.com/Kha-Samples/InstancedExample/blob/3ac2257523d4c8265981d8ac23f049f403349bb9/Sources/InstancedExample.hx#L180

If I disable this check, kinc_g4_draw_indexed_vertices_instanced_from_to() is called but nothing is drawn. image

QuantumCoderQC avatar Sep 09 '21 15:09 QuantumCoderQC

Hu, it works? Have to check. HL things should go into a different issue (I have no idea how well HL things work nowadays, I don't use it at all).

RobDangerous avatar Sep 09 '21 15:09 RobDangerous