コード例 #1
0
        public sealed override void Render(DrawingContext context)
        {
            if (!EnsureInitialized())
            {
                return;
            }

            using (_context.MakeCurrent())
            {
                using (_bitmap.Lock())
                {
                    var gl = _context.GlInterface;
                    gl.BindFramebuffer(GL_FRAMEBUFFER, _fb);
                    if (_oldSize != GetPixelSize())
                    {
                        ResizeTexture(gl);
                    }

                    OnOpenGlRender(gl, _fb);
                    gl.Flush();
                }
            }

            context.DrawImage(_bitmap, new Rect(_bitmap.Size), Bounds);
            base.Render(context);
        }
コード例 #2
0
 public override void SetCurrent()
 {
     if (!IsCurrentWindow)
     {
         OpenglContext.MakeCurrent();
         IsCurrentWindow = true;
     }
 }
コード例 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <see cref="http://www.opentk.com/doc/graphics/graphicscontext"/>
        //[HandleProcessCorruptedStateExceptions]
        public override void InitSynchronizedOnce()
        {
            //Memory.WriteBytesHook += OnMemoryWrite;
            ScaleViewport = PspStoredConfig.RenderScale;

            if (!AlreadyInitialized)
            {
                AlreadyInitialized = true;
                var completedEvent = new AutoResetEvent(false);
                new Thread(() =>
                {
                    Thread.CurrentThread.CurrentCulture = new CultureInfo(GlobalConfig.ThreadCultureName);

                    OpenglContext = GlContextFactory.CreateWindowless();
                    OpenglContext.MakeCurrent();

                    Console.Out.WriteLineColored(ConsoleColor.White, "## OpenGL Context Version: {0}",
                                                 GlGetString(GL.GL_VERSION));
                    Console.Out.WriteLineColored(ConsoleColor.White, "## Depth Bits: {0}",
                                                 GL.glGetInteger(GL.GL_DEPTH_BITS));
                    Console.Out.WriteLineColored(ConsoleColor.White, "## Stencil Bits: {0}",
                                                 GL.glGetInteger(GL.GL_STENCIL_BITS));
                    Console.Out.WriteLineColored(ConsoleColor.White, "## Color Bits: {0},{1},{2},{3}",
                                                 GL.glGetInteger(GL.GL_RED_BITS), GL.glGetInteger(GL.GL_GREEN_BITS),
                                                 GL.glGetInteger(GL.GL_BLUE_BITS), GL.glGetInteger(GL.GL_ALPHA_BITS));

                    if (GL.glGetInteger(GL.GL_STENCIL_BITS) <= 0)
                    {
                        Console.Error.WriteLineColored(ConsoleColor.Red, "No stencil bits available!");
                    }

                    OpenglContext.ReleaseCurrent();

                    completedEvent.Set();
                    Console.WriteLine("OpenglGpuImpl.Init.Start()");
                    try
                    {
                        while (Running)
                        {
                            Thread.Sleep(10);
                        }
                        StopEvent.Set();
                    }
                    finally
                    {
                        Console.WriteLine("OpenglGpuImpl.Init.End()");
                    }
                })
                {
                    Name         = "GpuImplEventHandling",
                    IsBackground = true
                }.Start();

                completedEvent.WaitOne();
            }
        }
コード例 #4
0
        public sealed override void Render(DrawingContext context)
        {
            if (!EnsureInitialized())
            {
                return;
            }

            using (_context.MakeCurrent())
            {
                _context.GlInterface.BindFramebuffer(GL_FRAMEBUFFER, _fb);
                EnsureTextureAttachment();
                EnsureDepthBufferAttachment(_context.GlInterface);
                if (!CheckFramebufferStatus(_context.GlInterface))
                {
                    return;
                }

                OnOpenGlRender(_context.GlInterface, _fb);
                _attachment.Present();
            }

            context.DrawImage(_bitmap, new Rect(_bitmap.Size), Bounds);
            base.Render(context);
        }
コード例 #5
0
        public void Present()
        {
            using (_context.MakeCurrent())
            {
                if (_disposed)
                {
                    throw new ObjectDisposedException(nameof(SharedOpenGlBitmapAttachment));
                }

                var gl = _context.GlInterface;

                gl.Finish();
                using (Lock())
                {
                    gl.GetIntegerv(GL_FRAMEBUFFER_BINDING, out var oldFbo);
                    gl.GetIntegerv(GL_TEXTURE_BINDING_2D, out var oldTexture);
                    gl.GetIntegerv(GL_ACTIVE_TEXTURE, out var oldActive);

                    gl.BindFramebuffer(GL_FRAMEBUFFER, _fbo);
                    gl.BindTexture(GL_TEXTURE_2D, _frontBuffer);
                    gl.ActiveTexture(GL_TEXTURE0);

                    gl.CopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, _bitmap.PixelSize.Width,
                                         _bitmap.PixelSize.Height);

                    gl.BindFramebuffer(GL_FRAMEBUFFER, oldFbo);
                    gl.BindTexture(GL_TEXTURE_2D, oldTexture);
                    gl.ActiveTexture(oldActive);

                    gl.Finish();
                }
            }

            _bitmap.Present(this);
            _presentCallback();
        }
コード例 #6
0
        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);
                }
            }
        }
コード例 #7
0
        bool EnsureInitialized()
        {
            if (_context != null)
            {
                return(true);
            }

            if (_glFailed)
            {
                return(false);
            }

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

            if (feature == null)
            {
                return(false);
            }
            try
            {
                _context = feature.CreateContext();
            }
            catch (Exception e)
            {
                Logger.TryGet(LogEventLevel.Error)?.Log("OpenGL", "OpenGlControlBase",
                                                        "Unable to initialize OpenGL: unable to create additional OpenGL context: {exception}", e);
                _glFailed = true;
                return(false);
            }

            GlVersion = _context.Version;
            try
            {
                _bitmap = new OpenGlTextureBitmap();
            }
            catch (Exception e)
            {
                _context.Dispose();
                _context = null;
                Logger.TryGet(LogEventLevel.Error)?.Log("OpenGL", "OpenGlControlBase",
                                                        "Unable to initialize OpenGL: unable to create OpenGlTextureBitmap: {exception}", e);
                _glFailed = true;
                return(false);
            }

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

                    gl.GenTextures(1, oneArr);
                    _texture = oneArr[0];

                    ResizeTexture(gl);

                    gl.FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, _texture, 0);

                    var status = gl.CheckFramebufferStatus(GL_FRAMEBUFFER);
                    if (status != GL_FRAMEBUFFER_COMPLETE)
                    {
                        int code;
                        while ((code = gl.GetError()) != 0)
                        {
                            Logger.TryGet(LogEventLevel.Error)?.Log("OpenGL", "OpenGlControlBase",
                                                                    "Unable to initialize OpenGL FBO: {code}", code);
                        }

                        _glFailed = true;
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    Logger.TryGet(LogEventLevel.Error)?.Log("OpenGL", "OpenGlControlBase",
                                                            "Unable to initialize OpenGL FBO: {exception}", e);
                    _glFailed = true;
                }

                if (!_glFailed)
                {
                    OnOpenGlInit(_context.GlInterface, _fb);
                }
            }

            if (_glFailed)
            {
                DoCleanup(false);
            }

            return(true);
        }
コード例 #8
0
 public void MakeCurrent()
 {
     _lastMadeCurrent = _context.MakeCurrent();
     IsCurrent        = true;
 }