veldrid / veldrid

A low-level, portable graphics library for .NET.

Home Page:https://veldrid.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Android .NET 6.0 CreateOpenGLES() failed to get the default Android EGLDisplay

eightonegulf opened this issue · comments

Hi,
I'm trying to setup Veldrid within an Android .NET 6.0 application.
My MainActivity class looks as follow:

    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle? savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            GraphicsDeviceOptions options = new GraphicsDeviceOptions(
                false,
                PixelFormat.R16_UNorm,
                false,
                ResourceBindingModel.Improved,
                true,
                true);
            var surface = new VeldridSurfaceView(this, Veldrid.GraphicsBackend.OpenGLES, options);
            SetContentView(surface);
        }
    }

The VeldridSurfaceView is directly copied from
https://github.com/mellinoe/veldrid-samples/blob/master/src/SampleBase.Android/VeldridSurfaceView.cs

When I run this on my Android device, line #84 in VeldridSurfaceView GraphicsDevice = GraphicsDevice.CreateOpenGLES(DeviceOptions, sd); will throw an exception stating;

CreateOpenGLES() failed to get the default Android EGLDisplay: BadParameter

This exception originates from https://github.com/mellinoe/veldrid/blob/master/src/Veldrid/OpenGL/OpenGLGraphicsDevice.cs
InitializeANativeWindow(), line #621.
For some reason the display can't be found.

When I run exactly the same code from an Xamarin Android app, everything works as expected, and I can use Veldrid just fine.
Only when I run from a .NET 6.0 app this goes wrong.

I am using VS 2022 Preview in order to use .NET 6.0 on Android.

I can't figure out what I'm missing. Is this a compatability issue, or am I missing something else?

Thanks.

Hi, that might be caused by the wrong eglGetDisplay signature in EGLNative.cs, the actual c function requires a argument of pointer type , but the C# wrapper supplies a System.Int32 type, on 64-bit Android OS (which is the majority now) this causes a invalid argument exception because the upper 32 bit half is just rubbish data.

The fix is also very simple, just clone the repo, add a local project reference, and change IntPtr eglGetDisplay(int native_display) to IntPtr eglGetDisplay(IntPtr native_display), and everything will be fine

It would be great to confirm if @0x5b25 's change does indeed fix it. I don't have an active Android setup right now so this will take a bit for me to debug.

commented

Use IntPtr fixed it
sharex-20221113154438