Example #1
0
        void ResizeTexture(GlInterface gl)
        {
            var size = GetPixelSize();

            gl.GetIntegerv(GL_TEXTURE_BINDING_2D, out var oldTexture);
            gl.BindTexture(GL_TEXTURE_2D, _texture);
            gl.TexImage2D(GL_TEXTURE_2D, 0,
                          GlVersion.Type == GlProfileType.OpenGLES ? GL_RGBA : GL_RGBA8,
                          size.Width, size.Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, IntPtr.Zero);
            gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            gl.BindTexture(GL_TEXTURE_2D, oldTexture);

            gl.GetIntegerv(GL_RENDERBUFFER_BINDING, out var oldRenderBuffer);
            gl.DeleteRenderbuffers(1, new[] { _renderBuffer });
            var oneArr = new int[1];

            gl.GenRenderbuffers(1, oneArr);
            _renderBuffer = oneArr[0];
            gl.BindRenderbuffer(GL_RENDERBUFFER, _renderBuffer);
            gl.RenderbufferStorage(GL_RENDERBUFFER,
                                   GlVersion.Type == GlProfileType.OpenGLES ? GL_DEPTH_COMPONENT16 : GL_DEPTH_COMPONENT,
                                   size.Width, size.Height);
            gl.FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _renderBuffer);
            gl.BindRenderbuffer(GL_RENDERBUFFER, oldRenderBuffer);
            using (_bitmap.Lock())
                _bitmap.SetTexture(_texture, GL_RGBA8, size, 1);
        }
Example #2
0
 public EglContext(EglDisplay display, EglInterface egl, IntPtr ctx, EglSurface offscreenSurface,
                   GlVersion version, int sampleCount, int stencilSize)
 {
     _disp            = display;
     _egl             = egl;
     Context          = ctx;
     OffscreenSurface = offscreenSurface;
     Version          = version;
     SampleCount      = sampleCount;
     StencilSize      = stencilSize;
     using (MakeCurrent())
         GlInterface = GlInterface.FromNativeUtf8GetProcAddress(version, b => _egl.GetProcAddress(b));
 }
 public static OpenGlException GetFormattedException(string funcName, GlInterface gl)
 {
     return(GetFormattedException(typeof(GlErrors), funcName, gl.GetError()));
 }
Example #4
0
        public EglDisplay(EglInterface egl, int platformType, IntPtr platformDisplay, int[] attrs)
        {
            _egl = egl;

            if (platformType == -1 && platformDisplay == IntPtr.Zero)
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    if (_egl.GetPlatformDisplayEXT == null)
                    {
                        throw new OpenGlException("eglGetPlatformDisplayEXT is not supported by libegl.dll");
                    }

                    var allowedApis = AvaloniaLocator.Current.GetService <AngleOptions>()?.AllowedPlatformApis
                                      ?? new List <AngleOptions.PlatformApi> {
                        AngleOptions.PlatformApi.DirectX9
                    };

                    foreach (var platformApi in allowedApis)
                    {
                        int dapi;
                        if (platformApi == AngleOptions.PlatformApi.DirectX9)
                        {
                            dapi = EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE;
                        }
                        else if (platformApi == AngleOptions.PlatformApi.DirectX11)
                        {
                            dapi = EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE;
                        }
                        else
                        {
                            continue;
                        }

                        _display = _egl.GetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, IntPtr.Zero,
                                                              new[] { EGL_PLATFORM_ANGLE_TYPE_ANGLE, dapi, EGL_NONE });
                        if (_display != IntPtr.Zero)
                        {
                            _angleApi = platformApi;
                            break;
                        }
                    }

                    if (_display == IntPtr.Zero)
                    {
                        throw new OpenGlException("Unable to create ANGLE display");
                    }
                }

                if (_display == IntPtr.Zero)
                {
                    _display = _egl.GetDisplay(IntPtr.Zero);
                }
            }
            else
            {
                if (_egl.GetPlatformDisplayEXT == null)
                {
                    throw new OpenGlException("eglGetPlatformDisplayEXT is not supported by libegl");
                }
                _display = _egl.GetPlatformDisplayEXT(platformType, platformDisplay, attrs);
            }

            if (_display == IntPtr.Zero)
            {
                throw OpenGlException.GetFormattedException("eglGetDisplay", _egl);
            }

            if (!_egl.Initialize(_display, out var major, out var minor))
            {
                throw OpenGlException.GetFormattedException("eglInitialize", _egl);
            }

            foreach (var cfg in new[]
            {
                new
                {
                    Attributes = new[] { EGL_NONE },
                    Api = EGL_OPENGL_API,
                    RenderableTypeBit = EGL_OPENGL_BIT,
                    Type = GlDisplayType.OpenGL2
                },
                new
                {
                    Attributes = new[]
                    {
                        EGL_CONTEXT_CLIENT_VERSION, 2,
                        EGL_NONE
                    },
                    Api = EGL_OPENGL_ES_API,
                    RenderableTypeBit = EGL_OPENGL_ES2_BIT,
                    Type = GlDisplayType.OpenGLES2
                }
            })
            {
                if (!_egl.BindApi(cfg.Api))
                {
                    continue;
                }
                foreach (var surfaceType in new[] { EGL_PBUFFER_BIT | EGL_WINDOW_BIT, EGL_WINDOW_BIT })
                {
                    foreach (var stencilSize in new[] { 8, 1, 0 })
                    {
                        foreach (var depthSize in new [] { 8, 1, 0 })
                        {
                            var attribs = new[]
                            {
                                EGL_SURFACE_TYPE, surfaceType,
                                EGL_RENDERABLE_TYPE, cfg.RenderableTypeBit,
                                EGL_RED_SIZE, 8,
                                EGL_GREEN_SIZE, 8,
                                EGL_BLUE_SIZE, 8,
                                EGL_ALPHA_SIZE, 8,
                                EGL_STENCIL_SIZE, stencilSize,
                                EGL_DEPTH_SIZE, depthSize,
                                EGL_NONE
                            };
                            if (!_egl.ChooseConfig(_display, attribs, out _config, 1, out int numConfigs))
                            {
                                continue;
                            }
                            if (numConfigs == 0)
                            {
                                continue;
                            }
                            _contextAttributes = cfg.Attributes;
                            _surfaceType       = surfaceType;
                            Type = cfg.Type;
                        }
                    }
                }
            }

            if (_contextAttributes == null)
            {
                throw new OpenGlException("No suitable EGL config was found");
            }

            GlInterface = GlInterface.FromNativeUtf8GetProcAddress(b => _egl.GetProcAddress(b));
        }
Example #5
0
 protected abstract void OnOpenGlRender(GlInterface gl, int fb);
Example #6
0
 protected virtual void OnOpenGlDeinit(GlInterface gl, int fb)
 {
 }
Example #7
0
        public EglDisplay(EglInterface egl)
        {
            _egl = egl;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && _egl.GetPlatformDisplayEXT != null)
            {
                foreach (var dapi in new[] { EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE })
                {
                    _display = _egl.GetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, IntPtr.Zero, new[]
                    {
                        EGL_PLATFORM_ANGLE_TYPE_ANGLE, dapi, EGL_NONE
                    });
                    if (_display != IntPtr.Zero)
                    {
                        break;
                    }
                }
            }

            if (_display == IntPtr.Zero)
            {
                _display = _egl.GetDisplay(IntPtr.Zero);
            }

            if (_display == IntPtr.Zero)
            {
                throw OpenGlException.GetFormattedException("eglGetDisplay", _egl);
            }

            if (!_egl.Initialize(_display, out var major, out var minor))
            {
                throw OpenGlException.GetFormattedException("eglInitialize", _egl);
            }

            foreach (var cfg in new[]
            {
                new
                {
                    Attributes = new[] { EGL_NONE },
                    Api = EGL_OPENGL_API,
                    RenderableTypeBit = EGL_OPENGL_BIT,
                    Type = GlDisplayType.OpenGL2
                },
                new
                {
                    Attributes = new[]
                    {
                        EGL_CONTEXT_CLIENT_VERSION, 2,
                        EGL_NONE
                    },
                    Api = EGL_OPENGL_ES_API,
                    RenderableTypeBit = EGL_OPENGL_ES2_BIT,
                    Type = GlDisplayType.OpenGLES2
                }
            })
            {
                if (!_egl.BindApi(cfg.Api))
                {
                    continue;
                }

                var attribs = new[]
                {
                    EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
                    EGL_RENDERABLE_TYPE, cfg.RenderableTypeBit,
                    EGL_RED_SIZE, 8,
                    EGL_GREEN_SIZE, 8,
                    EGL_BLUE_SIZE, 8,
                    EGL_ALPHA_SIZE, 8,
                    EGL_STENCIL_SIZE, 8,
                    EGL_DEPTH_SIZE, 8,
                    EGL_NONE
                };
                if (!_egl.ChooseConfig(_display, attribs, out _config, 1, out int numConfigs))
                {
                    continue;
                }
                if (numConfigs == 0)
                {
                    continue;
                }
                _contextAttributes = cfg.Attributes;
                Type = cfg.Type;
            }

            if (_contextAttributes == null)
            {
                throw new OpenGlException("No suitable EGL config was found");
            }

            GlInterface = new GlInterface((proc, optional) =>
            {
                using (var u = new Utf8Buffer(proc))
                {
                    var rv = _egl.GetProcAddress(u);
                    if (rv == IntPtr.Zero && !optional)
                    {
                        throw new OpenGlException("Missing function " + proc);
                    }
                    return(rv);
                }
            });
        }