예제 #1
0
        public void TestConstructorRaw()
        {
            using (DeviceContext deviceContext = DeviceContext.Create()) {
                // Create a context manually
                IntPtr glContext = deviceContext.CreateContext(IntPtr.Zero);

                // Create a GraphicsContext on glContext
                GraphicsContext graphicsContext = null;

                try {
                    Assert.DoesNotThrow(delegate { graphicsContext = new GraphicsContext(deviceContext, glContext); });
                    TestConstructProperties(graphicsContext);

                    Assert.AreEqual(glContext, graphicsContext.Handle, "GL context handle different from original");
                    Assert.IsTrue(deviceContext.RefCount > 0, "DeviceContext is not referenced");
                } finally {
                    if (graphicsContext != null)
                    {
                        graphicsContext.Dispose();
                        TestDisposedProperties(graphicsContext);
                    }
                }

                Assert.IsTrue(deviceContext.RefCount == 0, "DeviceContext was not referenced");

                // Context won't be deleted by GraphicsContext in this case
                deviceContext.DeleteContext(glContext);
            }
        }
예제 #2
0
        /// <summary>
        /// Delete the NativeWindow context.
        /// </summary>
        protected void DeleteContext()
        {
            if (GLContext == IntPtr.Zero)
            {
                return;
            }

            // Before deleting GL context
            OnContextDestroying();

            // Remove this context from the sharing group
            if (ContextSharing == ContextSharingOption.OwnContext && ContextSharingGroup != null)
            {
                List <IntPtr> sharingContextes;

                // Get the list previously created
                _SharingGroups.TryGetValue(ContextSharingGroup, out sharingContextes);
                // Remove this context
                bool res = sharingContextes != null && sharingContextes.Remove(GLContext);
                Debug.Assert(res);
            }

            // Delete OpenGL context
            DeviceContext.DeleteContext(GLContext);
            GLContext = IntPtr.Zero;
        }
예제 #3
0
		private static void RenderOsdPBuffer(DeviceContext deviceContext)
		{
			IntPtr glContext = IntPtr.Zero;
			uint framebuffer = 0;
			uint renderbuffer = 0;

			try {
				// Create context and make current on this thread
				if ((glContext = deviceContext.CreateContext(IntPtr.Zero)) == IntPtr.Zero)
					throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());	// XXX
				deviceContext.MakeCurrent(glContext);

				// Create framebuffer resources
				int w = Math.Min(800, 800);
				int h = Math.Min(600, 800);

				RenderOsd(w, h);
				SnapshotOsd(w, h);

			} finally {
				if (renderbuffer != 0)
					Gl.DeleteRenderbuffers(renderbuffer);
				if (framebuffer != 0)
					Gl.DeleteFramebuffers(framebuffer);
				if (glContext != IntPtr.Zero)
					deviceContext.DeleteContext(glContext);
			}
		}
예제 #4
0
        private void TestGetCurrentContext_Core()
        {
            using (DeviceContext deviceContext = DeviceContext.Create()) {
                if (deviceContext.IsPixelFormatSet == false)
                {
                    deviceContext.ChoosePixelFormat(new DevicePixelFormat(24));
                }

                IntPtr currentContext = DeviceContext.GetCurrentContext();

                // Initially no current context
                Assert.AreEqual(IntPtr.Zero, currentContext);

                IntPtr glContext = deviceContext.CreateContext(IntPtr.Zero);
                try {
                    deviceContext.MakeCurrent(glContext);

                    // No the previously GL context is current on this thread
                    currentContext = DeviceContext.GetCurrentContext();
                    Assert.AreEqual(glContext, currentContext);

                    deviceContext.MakeCurrent(IntPtr.Zero);
                } finally {
                    deviceContext.DeleteContext(glContext);
                }
            }
        }
예제 #5
0
 /// <summary>
 /// Delete the GlWidget context.
 /// </summary>
 private void DeleteContext()
 {
     // Delete OpenGL context
     if (_GraphicsContext != IntPtr.Zero)
     {
         _DeviceContext.DeleteContext(_GraphicsContext);
         _GraphicsContext = IntPtr.Zero;
     }
 }
예제 #6
0
파일: Program.cs 프로젝트: koson/OpenGL.Net
        private static void RenderOsdFramebuffer(DeviceContext deviceContext)
        {
            IntPtr glContext    = IntPtr.Zero;
            uint   framebuffer  = 0;
            uint   renderbuffer = 0;

            try {
                // Create context and make current on this thread
                if ((glContext = deviceContext.CreateContext(IntPtr.Zero)) == IntPtr.Zero)
                {
                    throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());                         // XXX
                }
                deviceContext.MakeCurrent(glContext);

                // Create framebuffer resources
                int w = Math.Min(800, Gl.CurrentLimits.MaxRenderbufferSize);
                int h = Math.Min(600, Gl.CurrentLimits.MaxRenderbufferSize);

                renderbuffer = Gl.GenRenderbuffer();
                Gl.BindRenderbuffer(RenderbufferTarget.Renderbuffer, renderbuffer);
                Gl.RenderbufferStorage(RenderbufferTarget.Renderbuffer, InternalFormat.Rgb8, w, h);

                framebuffer = Gl.GenFramebuffer();
                Gl.BindFramebuffer(FramebufferTarget.ReadFramebuffer, framebuffer);
                Gl.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer);
                Gl.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, RenderbufferTarget.Renderbuffer, renderbuffer);

                FramebufferStatus framebufferStatus = Gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
                if (framebufferStatus != FramebufferStatus.FramebufferComplete)
                {
                    throw new InvalidOperationException("framebuffer not complete");
                }

                Gl.DrawBuffers(Gl.COLOR_ATTACHMENT0);

                RenderOsd(w, h);

                Gl.ReadBuffer(ReadBufferMode.ColorAttachment0);

                SnapshotOsd(w, h);
            } finally {
                if (renderbuffer != 0)
                {
                    Gl.DeleteRenderbuffers(renderbuffer);
                }
                if (framebuffer != 0)
                {
                    Gl.DeleteFramebuffers(framebuffer);
                }
                if (glContext != IntPtr.Zero)
                {
                    deviceContext.DeleteContext(glContext);
                }
            }
        }
예제 #7
0
        static void Main()
        {
            try {
                string envDebug = Environment.GetEnvironmentVariable("DEBUG");
                if (envDebug == "GL")
                {
                    KhronosApi.Log += delegate(object sender, KhronosLogEventArgs e) {
                        Console.WriteLine(e.ToString());
                    };
                    KhronosApi.LogEnabled = true;
                }

                // RPi runs on EGL
                Egl.IsRequired = true;

                if (Egl.IsAvailable == false)
                {
                    throw new InvalidOperationException("EGL is not available. Aborting.");
                }

                using (VideoCoreWindow nativeWindow = new VideoCoreWindow()) {
                    using (DeviceContext eglContext = DeviceContext.Create(nativeWindow.Display, nativeWindow.Handle)) {
                        eglContext.ChoosePixelFormat(new DevicePixelFormat(32));

                        IntPtr glContext = eglContext.CreateContext(IntPtr.Zero);

                        eglContext.MakeCurrent(glContext);

                        Initialize();

                        Gl.Viewport(0, 0, 1920, 1080);
                        Gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);

                        while (true)
                        {
                            Gl.Clear(ClearBufferMask.ColorBufferBit);
                            Draw();
                            eglContext.SwapBuffers();
                            break;
                        }

                        System.Threading.Thread.Sleep(10000);

                        Terminate();
                        eglContext.DeleteContext(glContext);
                    }
                }
            } catch (Exception exception) {
                Console.WriteLine(exception.ToString());
            }
        }
예제 #8
0
        private void DeviceContext_CreateContext_DefaultAPI_Core(string api)
        {
            using (Device device = new Device())
            {
                DeviceContext deviceContext = device.Context;
                List <string> availableAPIs = new List <string>(deviceContext.AvailableAPIs);

                if (availableAPIs.Contains(api) == false)
                {
                    Assert.Inconclusive("underlying device is unable to support '" + api + "'");
                }

                if (deviceContext.IsPixelFormatSet == false)
                {
                    deviceContext.ChoosePixelFormat(new DevicePixelFormat(24));
                }

                IntPtr glContext = deviceContext.CreateContext(IntPtr.Zero);
                Assert.AreNotEqual(IntPtr.Zero, glContext);

                try {
                    if (deviceContext.MakeCurrent(glContext) == false)
                    {
                        Assert.Fail("unable to make current");
                    }

                    string         glVersionString = Gl.GetString(StringName.Version);
                    KhronosVersion glVersion       = KhronosVersion.Parse(glVersionString);

                    switch (api)
                    {
                    case KhronosVersion.ApiGlsc2:
                        // SC2 is a special case: based on ES2
                        Assert.AreEqual(KhronosVersion.ApiGles2, glVersion.Api);
                        break;

                    default:
                        Assert.AreEqual(api, glVersion.Api);
                        break;
                    }

                    deviceContext.MakeCurrent(IntPtr.Zero);
                } finally {
                    if (glContext != IntPtr.Zero)
                    {
                        deviceContext.DeleteContext(glContext);
                    }
                }
            }
        }
예제 #9
0
 void CloseOpenGL()
 {
     if (_ctx != IntPtr.Zero)
     {
         _deviceContext.MakeCurrent(IntPtr.Zero);
         _deviceContext.DeleteContext(_ctx);
         _ctx = IntPtr.Zero;
     }
     if (_deviceContext != null)
     {
         _deviceContext.Dispose();
         _deviceContext = null;
     }
 }
예제 #10
0
        public void Dispose()
        {
            if (_deviceContext == null)
            {
                return;
            }
            _deviceContext.MakeCurrent(IntPtr.Zero);
            if (_glContext == IntPtr.Zero)
            {
                return;
            }
            _deviceContext.DeleteContext(_glContext);
            _glContext = IntPtr.Zero;

            // Don't dispose _deviceContext as we don't own it
        }
예제 #11
0
        private void TestUserCase1_Core()
        {
            using (DeviceContext deviceContext = DeviceContext.Create()) {
                if (deviceContext.IsPixelFormatSet == false)
                {
                    deviceContext.ChoosePixelFormat(new DevicePixelFormat(24));
                }

                IntPtr glContext = deviceContext.CreateContext(IntPtr.Zero);

                deviceContext.MakeCurrent(glContext);
                // Perform drawing...
                deviceContext.MakeCurrent(IntPtr.Zero);
                deviceContext.DeleteContext(glContext);
            }
        }
예제 #12
0
		private static void RenderOsdFramebuffer(DeviceContext deviceContext)
		{
			IntPtr glContext = IntPtr.Zero;
			uint framebuffer = 0;
			uint renderbuffer = 0;

			try {
				// Create context and make current on this thread
				if ((glContext = deviceContext.CreateContext(IntPtr.Zero)) == IntPtr.Zero)
					throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());	// XXX
				deviceContext.MakeCurrent(glContext);

				// Create framebuffer resources
				int w = Math.Min(800, Gl.CurrentLimits.MaxRenderBufferSize);
				int h = Math.Min(600, Gl.CurrentLimits.MaxRenderBufferSize);

				renderbuffer = Gl.GenRenderbuffer();
				Gl.BindRenderbuffer(Gl.RENDERBUFFER, renderbuffer);
				Gl.RenderbufferStorage(Gl.RENDERBUFFER, Gl.RGB8, w, h);

				framebuffer = Gl.GenFramebuffer();
				Gl.BindFramebuffer(Gl.READ_FRAMEBUFFER, framebuffer);
				Gl.BindFramebuffer(Gl.FRAMEBUFFER, framebuffer);
				Gl.FramebufferRenderbuffer(Gl.FRAMEBUFFER, Gl.COLOR_ATTACHMENT0, Gl.RENDERBUFFER, renderbuffer);

				int framebufferStatus = Gl.CheckFramebufferStatus(Gl.FRAMEBUFFER);
				if (framebufferStatus != Gl.FRAMEBUFFER_COMPLETE)
					throw new InvalidOperationException("framebuffer not complete");

				Gl.DrawBuffers(Gl.COLOR_ATTACHMENT0);

				RenderOsd(w, h);

				Gl.ReadBuffer(ReadBufferMode.ColorAttachment0);

				SnapshotOsd(w, h);

			} finally {
				if (renderbuffer != 0)
					Gl.DeleteRenderbuffers(renderbuffer);
				if (framebuffer != 0)
					Gl.DeleteFramebuffers(framebuffer);
				if (glContext != IntPtr.Zero)
					deviceContext.DeleteContext(glContext);
			}
		}
예제 #13
0
        public void SurfaceDestroyed(ISurfaceHolder holder)
        {
            Debug.WriteLine("Sparrow: SurfaceDestroyed");
            if (_RenderTimer != null)
            {
                _RenderTimer.Change(Timeout.Infinite, Timeout.Infinite);
                _RenderTimer.Dispose();
                _RenderTimer = null;
            }

            if (_DeviceContext != null)
            {
                if (_RenderContext != IntPtr.Zero)
                {
                    _DeviceContext.DeleteContext(_RenderContext);
                }
                _DeviceContext.Dispose();
                _DeviceContext = null;
            }
        }
예제 #14
0
        /// <summary>
        /// Delete the NativeWindow context.
        /// </summary>
        protected void DeleteContext()
        {
            // Remove this context from the sharing group
            if (ContextSharing == ContextSharingOption.OwnContext && ContextSharingGroup != null)
            {
                List <IntPtr> sharingContextes;

                // Get the list previously created
                _SharingGroups.TryGetValue(ContextSharingGroup, out sharingContextes);
                // Remove this context
                bool res = sharingContextes.Remove(_RenderContext);
                Debug.Assert(res);
            }

            // Delete OpenGL context
            if (_RenderContext != IntPtr.Zero)
            {
                _DeviceContext.DeleteContext(_RenderContext);
                _RenderContext = IntPtr.Zero;
            }
        }
예제 #15
0
        private void TestUserCase3_Core()
        {
            if (DeviceContext.IsPBufferSupported == false)
            {
                Assert.Inconclusive("platform don't support P-Buffers");
            }

            using (INativePBuffer nativePBuffer = DeviceContext.CreatePBuffer(new DevicePixelFormat(24), 64, 64)) {
                using (DeviceContext deviceContext = DeviceContext.Create(nativePBuffer)) {
                    // The pixel format is already defined by INativePBuffer
                    Assert.IsTrue(deviceContext.IsPixelFormatSet);

                    IntPtr glContext = deviceContext.CreateContext(IntPtr.Zero);

                    deviceContext.MakeCurrent(glContext);
                    // Perform drawing...
                    deviceContext.MakeCurrent(IntPtr.Zero);
                    deviceContext.DeleteContext(glContext);
                }
            }
        }
예제 #16
0
        private void TestCreateContextAttrib_Core()
        {
            using (DeviceContext deviceContext = DeviceContext.Create()) {
                if (deviceContext.IsPixelFormatSet == false)
                {
                    deviceContext.ChoosePixelFormat(new DevicePixelFormat(24));
                }

                IntPtr glContext = IntPtr.Zero;

                Assert.DoesNotThrow(delegate() { glContext = deviceContext.CreateContext(IntPtr.Zero); });
                try {
                    Assert.AreNotEqual(IntPtr.Zero, glContext);
                    Assert.DoesNotThrow(delegate() { deviceContext.MakeCurrent(glContext); });

                    Assert.DoesNotThrow(delegate() { deviceContext.MakeCurrent(IntPtr.Zero); });
                } finally {
                    if (glContext != IntPtr.Zero)
                    {
                        deviceContext.DeleteContext(glContext);
                    }
                }
            }
        }
예제 #17
0
 public static void Shutdown()
 {
     devCtx.DeleteContext(glCtx);
     pixelBuff.Dispose();
     devCtx.Dispose();
 }
예제 #18
0
 public RCode DestroyDevice()
 {
     deviceContext?.DeleteContext(renderContext);
     deviceContext?.Dispose();
     return(RCode.OK);
 }