//~CanvasRenderingContextHolographicSpace() //{ // deviceContext.MakeCurrent(IntPtr.Zero); // deviceContext.DeleteContext(context); //} private IntPtr CreateEglContext(HolographicSpace holographicSpace, SpatialStationaryFrameOfReference stationaryReferenceFrame) { IntPtr context; int[] configAttribs = new int[] { Egl.RED_SIZE, 8, Egl.GREEN_SIZE, 8, Egl.BLUE_SIZE, 8, Egl.ALPHA_SIZE, 8, Egl.DEPTH_SIZE, 8, Egl.STENCIL_SIZE, 8, Egl.NONE }; int[] contextAttribs = new int[] { Egl.CONTEXT_CLIENT_VERSION, 2, Egl.NONE }; int[] surfaceAttribs = new int[] { EGL_ANGLE_SURFACE_RENDER_TO_BACK_BUFFER, Egl.TRUE, Egl.NONE }; int[] defaultDisplayAttribs = new int[] { EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, Egl.TRUE, EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, Egl.TRUE, Egl.NONE }; int[] fl9_3DisplayAttributes = new int[] { // These can be used to request ANGLE's D3D11 renderer, with D3D11 Feature Level 9_3. // These attributes are used if the call to eglInitialize fails with the default display attributes. EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, 9, EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, 3, EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, Egl.TRUE, EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, Egl.TRUE, Egl.NONE }; int[] warpDisplayAttributes = new int[] { // These attributes can be used to request D3D11 WARP. // They are used if eglInitialize fails with both the default display attributes and the 9_3 display attributes. EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, EGL_PLATFORM_ANGLE_DEVICE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_DEVICE_TYPE_WARP_ANGLE, EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER, Egl.TRUE, EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE, Egl.TRUE, Egl.NONE }; int[] configCount = new int[1]; IntPtr[] configs = new IntPtr[8]; if (Egl.BindAPI(Egl.OPENGL_ES_API) == false) { throw new InvalidOperationException("No OpenGL ES API"); } if ((display = Egl.GetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, IntPtr.Zero, defaultDisplayAttribs)) == IntPtr.Zero) { throw new InvalidOperationException("Unable to get EGL display"); } if (!Egl.Initialize(display, null, null)) { if ((display = Egl.GetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, IntPtr.Zero, fl9_3DisplayAttributes)) == IntPtr.Zero) { throw new InvalidOperationException("Unable to get EGL display"); } if (!Egl.Initialize(display, null, null)) { if ((display = Egl.GetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, IntPtr.Zero, warpDisplayAttributes)) == IntPtr.Zero) { throw new InvalidOperationException("Unable to get EGL display"); } if (!Egl.Initialize(display, null, null)) { throw new InvalidOperationException("Unable to initialize EGL"); } } } if (!Egl.ChooseConfig(display, configAttribs, configs, 1, configCount)) { throw new InvalidOperationException("Unable to choose configuration"); } holographicSpaceHandle = GCHandle.Alloc(holographicSpace); IntPtr holographicSpacePointer = Marshal.GetIUnknownForObject(holographicSpace); //PropertySet surfaceCreationProperties = new PropertySet //{ // { "EGLNativeWindowTypeProperty", (IntPtr)holographicSpaceHandle } //}; PropertySet surfaceCreationProperties = new PropertySet(); surfaceCreationProperties.Add("EGLNativeWindowTypeProperty", holographicSpacePointer); //if (stationaryReferenceFrame != null) //{ // stationaryReferenceFrameHandle = GCHandle.Alloc(stationaryReferenceFrame); // surfaceCreationProperties.Add("EGLBaseCoordinateSystemProperty", (IntPtr)stationaryReferenceFrameHandle); //} GCHandle surfaceCreationPropertiesHandle = GCHandle.Alloc(surfaceCreationProperties); IntPtr surfaceCreationPropertiesPointer = Marshal.GetIUnknownForObject(surfaceCreationProperties); if ((surface = Egl.CreateWindowSurface(display, configs[0], surfaceCreationPropertiesPointer, surfaceAttribs)) == IntPtr.Zero) { throw new InvalidOperationException("Unable to create EGL fullscreen surface"); } if ((context = Egl.CreateContext(display, configs[0], IntPtr.Zero, contextAttribs)) == IntPtr.Zero) { throw new InvalidOperationException("Unable to create context"); } return(context); }
public void Init(IntPtr nativeDeviceHandle, IntPtr nativeWindowHandle, PlatformBase platform) { _platform = platform; _eglLib = platform.LoadLibrary("libEGL"); if (_eglLib == IntPtr.Zero) { Engine.Log.Error("Couldn't load EGL.", MessageSource.Egl); return; } IntPtr gles = platform.LoadLibrary("libGLESv2"); if (gles == IntPtr.Zero) { Engine.Log.Error("Couldn't load GLES.", MessageSource.Egl); return; } string extensions = Egl.QueryString(IntPtr.Zero, Egl.Query.EGL_EXTENSIONS); Egl.Error err = Egl.GetError(); if (extensions == null || err != Egl.Error.Success) { Engine.Log.Error($"Couldn't load extensions. {err}", MessageSource.Egl); extensions = ""; } _display = Egl.GetDisplay(nativeDeviceHandle); if (_display == IntPtr.Zero) { Engine.Log.Error($"Couldn't initialize display. {Egl.GetError()}", MessageSource.Egl); return; } var majorVersion = 3; int majorVer = majorVersion; var minorVer = 0; if (RenderDoc.Loaded) { minorVer = 1; } if (!Egl.Init(_display, ref majorVer, ref minorVer)) { Engine.Log.Error($"Couldn't initialize Egl. {Egl.GetError()}", MessageSource.Egl); return; } // Pick config var totalConfigs = 0; Egl.GetConfigs(_display, null, 0, ref totalConfigs); if (totalConfigs == 0) { Engine.Log.Error($"No configs for current display. {Egl.GetError()}", MessageSource.Egl); return; } var configs = new IntPtr[totalConfigs]; Egl.GetConfigs(_display, configs, totalConfigs, ref totalConfigs); if (totalConfigs == 0) { Engine.Log.Error($"No configs for current display. {Egl.GetError()}", MessageSource.Egl); return; } int configHandle = SupportedPixelFormat(configs); if (configHandle == 0) { Engine.Log.Error("No valid config found.", MessageSource.Egl); return; } IntPtr config = configs[configHandle - 1]; if (!Egl.BindAPI(Egl.API.EGL_OPENGL_ES_API)) { Engine.Log.Error($"Couldn't bind EGL API. {Egl.GetError()}", MessageSource.Egl); return; } int[] attributes = { (int)Egl.ContextAttribute.EGL_CONTEXT_CLIENT_VERSION, majorVersion, 0x3038 // EGL_NONE }; _context = Egl.CreateContext(_display, config, IntPtr.Zero, attributes); if (_context == IntPtr.Zero) { Engine.Log.Error($"Context creation failed. {Egl.GetError()}", MessageSource.Egl); return; } _surface = Egl.CreateWindowSurface(_display, config, nativeWindowHandle, null); if (_surface == IntPtr.Zero) { Engine.Log.Error($"Surface creation failed. {Egl.GetError()}", MessageSource.Egl); return; } Valid = true; }