Example #1
0
        public GlPlatformFeature(IAvnGlDisplay display)
        {
            _display = display;
            var immediate = display.CreateContext(null);
            var deferred  = display.CreateContext(immediate);


            int         major, minor;
            GlInterface glInterface;

            using (immediate.MakeCurrent())
            {
                var basic = new GlBasicInfoInterface(display.GetProcAddress);
                basic.GetIntegerv(GlConsts.GL_MAJOR_VERSION, out major);
                basic.GetIntegerv(GlConsts.GL_MINOR_VERSION, out minor);
                _version    = new GlVersion(GlProfileType.OpenGL, major, minor);
                glInterface = new GlInterface(_version, (name) =>
                {
                    var rv = _display.GetProcAddress(name);
                    return(rv);
                });
            }

            GlDisplay = new GlDisplay(display, glInterface, immediate.SampleCount, immediate.StencilSize);

            ImmediateContext = new GlContext(GlDisplay, immediate, _version);
            DeferredContext  = new GlContext(GlDisplay, deferred, _version);
        }
 public GlContext(GlDisplay display, GlContext sharedWith, IAvnGlContext context, GlVersion version)
 {
     _display    = display;
     _sharedWith = sharedWith;
     Context     = context;
     Version     = version;
 }
Example #3
0
            public GLShaderProgram(GlInterface gl, string vertex_shader_source, string fragment_shader_source)
            {
                this.gl        = gl;
                this.GlVersion = gl.ContextInfo.Version;

                vertex_shader_source   = GetShader(false, vertex_shader_source);
                fragment_shader_source = GetShader(true, fragment_shader_source);

                vertex_shader   = new GLShader(gl, GL_VERTEX_SHADER, vertex_shader_source);
                fragment_shader = new GLShader(gl, GL_FRAGMENT_SHADER, fragment_shader_source);

                ProgramId = gl.CreateProgram();
                gl.AttachShader(ProgramId, vertex_shader.ShaderId);
                gl.AttachShader(ProgramId, fragment_shader.ShaderId);
                gl.LinkProgram(ProgramId);
                int link_succeeded;

                gl.GetProgramiv(ProgramId, GL_LINK_STATUS, &link_succeeded);

                if (link_succeeded == 0)
                {
                    byte[] buf = new byte[MaxErrorLength];
                    string log;
                    fixed(byte *pbuf = buf)
                    {
                        gl.GetProgramInfoLog(ProgramId, MaxErrorLength, out int log_length, pbuf);
                        log = Encoding.UTF8.GetString(pbuf, log_length);
                    }

                    throw new InvalidOperationException($"Failed to link program : {log}");
                }
            }
Example #4
0
 public EglContext(EglDisplay display, EglInterface egl, EglContext sharedWith, IntPtr ctx, Func <EglContext, EglSurface> offscreenSurface,
                   GlVersion version, int sampleCount, int stencilSize)
 {
     _disp            = display;
     _egl             = egl;
     _sharedWith      = sharedWith;
     Context          = ctx;
     OffscreenSurface = offscreenSurface(this);
     Version          = version;
     SampleCount      = sampleCount;
     StencilSize      = stencilSize;
     using (MakeCurrent())
         GlInterface = GlInterface.FromNativeUtf8GetProcAddress(version, b => _egl.GetProcAddress(b));
 }
Example #5
0
 public GlxContext(GlxInterface glx, IntPtr handle, GlxDisplay display,
                   GlVersion version, int sampleCount, int stencilSize,
                   X11Info x11, IntPtr defaultXid,
                   bool ownsPBuffer)
 {
     Handle       = handle;
     Glx          = glx;
     _x11         = x11;
     _defaultXid  = defaultXid;
     _ownsPBuffer = ownsPBuffer;
     Display      = display;
     Version      = version;
     SampleCount  = sampleCount;
     StencilSize  = stencilSize;
     using (MakeCurrent())
         GlInterface = new GlInterface(version, GlxInterface.SafeGetProcAddress);
 }
Example #6
0
 public WglContext(WglContext sharedWith, GlVersion version, IntPtr context, IntPtr hWnd, IntPtr dc, int pixelFormat,
                   PixelFormatDescriptor formatDescriptor)
 {
     Version           = version;
     _sharedWith       = sharedWith;
     _context          = context;
     _hWnd             = hWnd;
     _dc               = dc;
     _pixelFormat      = pixelFormat;
     _formatDescriptor = formatDescriptor;
     StencilSize       = formatDescriptor.StencilBits;
     using (MakeCurrent())
         GlInterface = new GlInterface(version, proc =>
         {
             var ext = wglGetProcAddress(proc);
             if (ext != IntPtr.Zero)
             {
                 return(ext);
             }
             return(GetProcAddress(WglDisplay.OpenGl32Handle, proc));
         });
 }
Example #7
0
 public static GlTextureInfo GlTextureInfoFor(this GpuBufferFormat gpuBufferFormat, int plane, GlVersion glVersion = GlVersion.kGLES3)
 {
     UnsafeNativeMethods.mp__GlTextureInfoForGpuBufferFormat__ui_i_ui(gpuBufferFormat, plane, glVersion, out var glTextureInfo).Assert();
     return(glTextureInfo);
 }
Example #8
0
        public EglDisplay(EglInterface egl, bool supportsSharing, IntPtr display)
        {
            _egl            = egl;
            SupportsSharing = supportsSharing;
            _display        = display;
            if (_display == IntPtr.Zero)
            {
                throw new ArgumentException();
            }


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

            var glProfiles = AvaloniaLocator.Current.GetService <AngleOptions>()?.GlProfiles
                             ?? new[]
            {
                new GlVersion(GlProfileType.OpenGLES, 3, 0),
                new GlVersion(GlProfileType.OpenGLES, 2, 0)
            };

            var cfgs = glProfiles.Select(x =>
            {
                var typeBit = EGL_OPENGL_ES3_BIT;

                switch (x.Major)
                {
                case 2:
                    typeBit = EGL_OPENGL_ES2_BIT;
                    break;

                case 1:
                    typeBit = EGL_OPENGL_ES_BIT;
                    break;
                }

                return(new
                {
                    Attributes = new[]
                    {
                        EGL_CONTEXT_MAJOR_VERSION, x.Major,
                        EGL_CONTEXT_MINOR_VERSION, x.Minor,
                        EGL_NONE
                    },
                    Api = EGL_OPENGL_ES_API,
                    RenderableTypeBit = typeBit,
                    Version = x
                });
            });

            foreach (var cfg in cfgs)
            {
                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;
                            _version           = cfg.Version;
                            _egl.GetConfigAttrib(_display, _config, EGL_SAMPLES, out _sampleCount);
                            _egl.GetConfigAttrib(_display, _config, EGL_STENCIL_SIZE, out _stencilSize);
                            goto Found;
                        }
                    }
                }
            }
Found:
            if (_contextAttributes == null)
            {
                throw new OpenGlException("No suitable EGL config was found");
            }
        }
Example #9
0
 public static extern MpReturnCode mp__GlTextureInfoForGpuBufferFormat__ui_i_ui(
     GpuBufferFormat format, int plane, GlVersion glVersion, out GlTextureInfo glTextureInfo);
        private bool EnsureInitializedCore()
        {
            if (_context != null)
            {
                return(true);
            }

            if (_glFailed)
            {
                return(false);
            }

            var feature = AvaloniaLocator.Current.GetService <IPlatformOpenGlInterface>();

            if (feature == null)
            {
                return(false);
            }
            if (!feature.CanShareContexts)
            {
                Logger.TryGet(LogEventLevel.Error, "OpenGL")?.Log("OpenGlControlBase",
                                                                  "Unable to initialize OpenGL: current platform does not support multithreaded context sharing");
                return(false);
            }
            try
            {
                _context = feature.CreateSharedContext();
            }
            catch (Exception e)
            {
                Logger.TryGet(LogEventLevel.Error, "OpenGL")?.Log("OpenGlControlBase",
                                                                  "Unable to initialize OpenGL: unable to create additional OpenGL context: {exception}", e);
                return(false);
            }

            GlVersion = _context.Version;
            try
            {
                _bitmap = new OpenGlBitmap(GetPixelSize(), new Vector(96, 96));
                if (!_bitmap.SupportsContext(_context))
                {
                    Logger.TryGet(LogEventLevel.Error, "OpenGL")?.Log("OpenGlControlBase",
                                                                      "Unable to initialize OpenGL: unable to create OpenGlBitmap: OpenGL context is not compatible");
                    return(false);
                }
            }
            catch (Exception e)
            {
                _context.Dispose();
                _context = null;
                Logger.TryGet(LogEventLevel.Error, "OpenGL")?.Log("OpenGlControlBase",
                                                                  "Unable to initialize OpenGL: unable to create OpenGlBitmap: {exception}", e);
                return(false);
            }

            using (_context.MakeCurrent())
            {
                try
                {
                    _depthBufferSize = GetPixelSize();
                    var gl     = _context.GlInterface;
                    var oneArr = new int[1];
                    gl.GenFramebuffers(1, oneArr);
                    _fb = oneArr[0];
                    gl.BindFramebuffer(GL_FRAMEBUFFER, _fb);

                    EnsureDepthBufferAttachment(gl);
                    EnsureTextureAttachment();

                    return(CheckFramebufferStatus(gl));
                }
                catch (Exception e)
                {
                    Logger.TryGet(LogEventLevel.Error, "OpenGL")?.Log("OpenGlControlBase",
                                                                      "Unable to initialize OpenGL FBO: {exception}", e);
                    return(false);
                }
            }
        }
Example #11
0
 public GlContext(GlDisplay display, IAvnGlContext context, GlVersion version)
 {
     _display = display;
     Context  = context;
     Version  = version;
 }
        public static GlTextureInfo GlTextureInfoFor(this GpuBufferFormat gpuBufferFormat, int plane, GlVersion glVersion = GlVersion.kGLES3)
        {
            UnsafeNativeMethods.mp__GlTextureInfoForGpuBufferFormat__ui_i_ui(gpuBufferFormat, plane, glVersion, out var glTextureInfoPtr);
            var glTextureInfo = Marshal.PtrToStructure <GlTextureInfo>(glTextureInfoPtr);

            UnsafeNativeMethods.mp_GlTextureInfo__delete(glTextureInfoPtr);

            return(glTextureInfo);
        }