示例#1
0
        /// <summary>
        /// Control the the buffers swap of a device.
        /// </summary>
        /// <param name="interval">
        /// A <see cref="System.Int32"/> that specifies the minimum number of video frames that are displayed
        /// before a buffer swap will occur.
        /// </param>
        /// <returns>
        /// It returns a boolean value indicating whether the operation was successful.
        /// </returns>
        public override bool SwapInterval(int interval)
        {
            // Keep into account the SwapIntervalEXT and SwapIntervalSGI entry points, relative to
            // two equivalent GLX extensions

#if false
            using (Glx.XLock displayLock = new Glx.XLock(Display)) {
                if (Glx.Delegates.pglXSwapIntervalEXT != null)
                {
                    Glx.SwapIntervalEXT(Display, _WindowHandle, interval);
                    return(true);
                }
                else if (Glx.Delegates.pglXSwapIntervalSGI != null)
                {
                    return(Glx.SwapIntervalSGI(interval) == 0);
                }
                else
                {
                    throw new InvalidOperationException("binding point SwapInterval{EXT|SGI} cannot be found");
                }
            }
#else
            return(false);
#endif
        }
示例#2
0
        /// <summary>
        /// Query the GLX version supported by current implementation.
        /// </summary>
        private void QueryVersion()
        {
            using (Glx.XLock xLock = new Glx.XLock(Display)) {
                int[] majorArg = new int[1], minorArg = new int[1];

                Glx.QueryVersion(Display, majorArg, minorArg);

                _GlxVersion = new KhronosVersion(majorArg[0], minorArg[0], KhronosVersion.ApiGlx);
            }
        }
        /// <summary>
        /// Creates a context.
        /// </summary>
        /// <param name="sharedContext">
        /// A <see cref="IntPtr"/> that specify a context that will share objects with the returned one. If
        /// it is IntPtr.Zero, no sharing is performed.
        /// </param>
        /// <returns>
        /// A <see cref="IntPtr"/> that represents the handle of the created context. If the context cannot be
        /// created, it returns IntPtr.Zero.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// Exception thrown in the case <paramref name="sharedContext"/> is different from IntPtr.Zero, and the objects
        /// cannot be shared with it.
        /// </exception>
        public override IntPtr CreateContext(IntPtr sharedContext)
        {
            if (XVisualInfo == null)
            {
                throw new InvalidOperationException("no visual information");
            }

            using (Glx.XLock displayLock = new Glx.XLock(Display)) {
                return(Glx.CreateContext(Display, XVisualInfo, sharedContext, true));
            }
        }
示例#4
0
        /// <summary>
        /// Deletes a context.
        /// </summary>
        /// <param name="ctx">
        /// A <see cref="IntPtr"/> that specify the context to be deleted.
        /// </param>
        /// <returns>
        /// It returns a boolean value indicating whether the operation was successful. If it returns false,
        /// query the exception by calling <see cref="GetPlatformException"/>.
        /// </returns>
        /// <remarks>
        /// <para>The context <paramref name="ctx"/> must not be current on any thread.</para>
        /// </remarks>
        /// <exception cref="ArgumentException">
        /// Exception thrown if <paramref name="ctx"/> is IntPtr.Zero.
        /// </exception>
        public override bool DeleteContext(IntPtr ctx)
        {
            if (ctx == IntPtr.Zero)
            {
                throw new ArgumentException("ctx");
            }

            using (Glx.XLock displayLock = new Glx.XLock(Display)) {
                Glx.DestroyContext(Display, ctx);
            }

            return(true);
        }
示例#5
0
        /// <summary>
        /// Create a simple context.
        /// </summary>
        /// <returns>
        /// A <see cref="IntPtr"/> that represents the handle of the created context. If the context cannot be
        /// created, it returns IntPtr.Zero.
        /// </returns>
        internal override IntPtr CreateSimpleContext()
        {
            using (Glx.XLock xLock = new Glx.XLock(Display)) {
                // Create direct context
                IntPtr rContext = CreateContext(IntPtr.Zero);

                if (rContext == IntPtr.Zero)
                {
                    throw new InvalidOperationException("unable to create context");
                }

                return(rContext);
            }
        }
示例#6
0
        /// <summary>
        /// Creates a context.
        /// </summary>
        /// <param name="sharedContext">
        /// A <see cref="IntPtr"/> that specify a context that will share objects with the returned one. If
        /// it is IntPtr.Zero, no sharing is performed.
        /// </param>
        /// <returns>
        /// A <see cref="IntPtr"/> that represents the handle of the created context. If the context cannot be
        /// created, it returns IntPtr.Zero.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// Exception thrown in the case <paramref name="sharedContext"/> is different from IntPtr.Zero, and the objects
        /// cannot be shared with it.
        /// </exception>
        public override IntPtr CreateContext(IntPtr sharedContext)
        {
            if (Glx.CurrentExtensions == null || Glx.CurrentExtensions.CreateContext_ARB == false)
            {
                using (Glx.XLock displayLock = new Glx.XLock(Display)) {
                    // Get the corresponding X visual info
                    Glx.XVisualInfo xVisualInfo = _XVisualInfo != null ? _XVisualInfo : GetVisualInfoFromXWindow(_WindowHandle);

                    Debug.Assert(xVisualInfo != null, "SetPixelFormat not executed or undetected XVisualInfo");
                    return(Glx.CreateContext(Display, xVisualInfo, sharedContext, true));
                }
            }
            else
            {
                return(CreateContextAttrib(sharedContext, new int[] { Gl.NONE }));
            }
        }
示例#7
0
        /// <summary>
        /// Creates an OpenGL context from a Unix/Linux platform.
        /// </summary>
        /// <returns>
        /// A <see cref="IDeviceContext"/> that specify the device context.
        /// </returns>
        private static IntPtr CreateX11SimpleContext(IDeviceContext rDevice)
        {
            XServerDeviceContext x11DeviceCtx = (XServerDeviceContext)rDevice;
            IntPtr rContext;

            using (Glx.XLock xLock = new Glx.XLock(x11DeviceCtx.Display)) {
                int[] attributes = new int[] {
                    Glx.RENDER_TYPE, (int)Glx.RGBA_BIT,
                    0
                };

                // Get basic visual
                unsafe {
                    int[]   choosenConfigCount = new int[1];
                    IntPtr *choosenConfigs     = Glx.ChooseFBConfig(x11DeviceCtx.Display, x11DeviceCtx.Screen, attributes, choosenConfigCount);

                    if (choosenConfigCount[0] == 0)
                    {
                        throw new InvalidOperationException("unable to find basic visual");
                    }

                    IntPtr choosenConfig = *choosenConfigs;

                    x11DeviceCtx.XVisualInfo = Glx.GetVisualFromFBConfig(x11DeviceCtx.Display, choosenConfig);
                    x11DeviceCtx.FBConfig    = choosenConfig;

                    Glx.UnsafeNativeMethods.XFree((IntPtr)choosenConfigs);
                }

                // Create direct context
                rContext = x11DeviceCtx.CreateContext(IntPtr.Zero);
                if (rContext == IntPtr.Zero)
                {
                    // Fallback to not direct context
                    rContext = Glx.CreateContext(x11DeviceCtx.Display, x11DeviceCtx.XVisualInfo, IntPtr.Zero, false);
                }

                if (rContext == IntPtr.Zero)
                {
                    throw new InvalidOperationException("unable to create context");
                }

                return(rContext);
            }
        }
        /// <summary>
        /// Creates a context, specifying attributes.
        /// </summary>
        /// <param name="sharedContext">
        /// A <see cref="IntPtr"/> that specify a context that will share objects with the returned one. If
        /// it is IntPtr.Zero, no sharing is performed.
        /// </param>
        /// <param name="attribsList">
        /// A <see cref="T:Int32[]"/> that specifies the attributes list.
        /// </param>
        /// <returns>
        /// A <see cref="IntPtr"/> that represents the handle of the created context. If the context cannot be
        /// created, it returns IntPtr.Zero.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <see cref="attribsList"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Exception thrown if <paramref name="attribsList"/> length is zero or if the last item of <paramref name="attribsList"/>
        /// is not zero.
        /// </exception>
        public override IntPtr CreateContextAttrib(IntPtr sharedContext, int[] attribsList)
        {
            if (attribsList == null)
            {
                throw new ArgumentNullException("attribsList");
            }
            if (attribsList.Length == 0)
            {
                throw new ArgumentException("zero length array", "attribsList");
            }
            if (attribsList[attribsList.Length - 1] != 0)
            {
                throw new ArgumentException("not zero-terminated array", "attribsList");
            }

            using (Glx.XLock displayLock = new Glx.XLock(Display)) {
                return(Glx.CreateContextAttribsARB(Display, FBConfig, sharedContext, true, attribsList));
            }
        }
示例#9
0
            /// <summary>
            /// Query the extensions supported by current platform.
            /// </summary>
            /// <param name="deviceContext">
            /// A <see cref="XServerDeviceContext"/> that specifies the device context to query extensions for.
            /// </param>
            public void Query(XServerDeviceContext deviceContext)
            {
                if (deviceContext == null)
                {
                    throw new ArgumentNullException("deviceContext");
                }

                LogComment("Query GLX extensions.");

                string glxExtensions = null;

                int[] majorArg = new int[1], minorArg = new int[1];

                using (Glx.XLock xLock = new Glx.XLock(deviceContext.Display)) {
                    Glx.QueryVersion(deviceContext.Display, majorArg, minorArg);

                    if ((majorArg[0] >= 1) && (minorArg[0] >= 1))
                    {
                        glxExtensions = Glx.QueryExtensionsString(deviceContext.Display, 0);
                    }
                }

                Query(new KhronosVersion(majorArg[0], minorArg[1], KhronosVersion.ApiGlx), glxExtensions ?? String.Empty);
            }
示例#10
0
 /// <summary>
 /// Swap the buffers of a device.
 /// </summary>
 public override void SwapBuffers()
 {
     using (Glx.XLock displayLock = new Glx.XLock(Display)) {
         Glx.SwapBuffers(Display, _WindowHandle);
     }
 }
示例#11
0
 /// <summary>
 /// Makes the context current on the calling thread.
 /// </summary>
 /// <param name="ctx">
 /// A <see cref="IntPtr"/> that specify the context to be current on the calling thread, bound to
 /// thise device context. It can be IntPtr.Zero indicating that no context will be current.
 /// </param>
 /// <returns>
 /// It returns a boolean value indicating whether the operation was successful.
 /// </returns>
 /// <exception cref="NotSupportedException">
 /// Exception thrown if the current platform is not supported.
 /// </exception>
 protected override bool MakeCurrentCore(IntPtr ctx)
 {
     using (Glx.XLock displayLock = new Glx.XLock(Display)) {
         return(Glx.MakeCurrent(Display, ctx != IntPtr.Zero ? _WindowHandle : IntPtr.Zero, ctx));
     }
 }
示例#12
0
        /// <summary>
        /// Creates a context, specifying attributes.
        /// </summary>
        /// <param name="sharedContext">
        /// A <see cref="IntPtr"/> that specify a context that will share objects with the returned one. If
        /// it is IntPtr.Zero, no sharing is performed.
        /// </param>
        /// <param name="attribsList">
        /// A <see cref="T:Int32[]"/> that specifies the attributes list.
        /// </param>
        /// <param name="api">
        /// A <see cref="KhronosVersion"/> that specifies the API to be implemented by the returned context. It can be null indicating the
        /// default API for this DeviceContext implementation. If it is possible, try to determine the API version also.
        /// </param>
        /// <returns>
        /// A <see cref="IntPtr"/> that represents the handle of the created context. If the context cannot be
        /// created, it returns IntPtr.Zero.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Exception thrown if <see cref="attribsList"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Exception thrown if <paramref name="attribsList"/> length is zero or if the last item of <paramref name="attribsList"/>
        /// is not zero.
        /// </exception>
        public override IntPtr CreateContextAttrib(IntPtr sharedContext, int[] attribsList, KhronosVersion api)
        {
            if (api != null && api.Api != KhronosVersion.ApiGl)
            {
                List <int> adulteredAttribs = new List <int>(attribsList);

                // Support check
                switch (api.Api)
                {
                case KhronosVersion.ApiGles1:
                case KhronosVersion.ApiGles2:
                    if (Glx.CurrentExtensions.CreateContextEsProfile_EXT == false)
                    {
                        throw new NotSupportedException("OpenGL ES API not supported");
                    }
                    break;

                default:
                    throw new NotSupportedException(String.Format("'{0}' API not supported", api.Api));
                }

                // Remove trailing 0
                if (adulteredAttribs.Count > 0 && adulteredAttribs[adulteredAttribs.Count - 1] == Gl.NONE)
                {
                    adulteredAttribs.RemoveAt(adulteredAttribs.Count - 1);
                }

                // Add required attributes
                int majorVersionIndex = adulteredAttribs.FindIndex(delegate(int item) { return(item == Gl.MAJOR_VERSION); });
                int minorVersionIndex = adulteredAttribs.FindIndex(delegate(int item) { return(item == Gl.MINOR_VERSION); });
                int profileMaskIndex  = adulteredAttribs.FindIndex(delegate(int item) { return(item == Gl.CONTEXT_PROFILE_MASK); });

                if (majorVersionIndex < 0)
                {
                    adulteredAttribs.AddRange(new int[] { Gl.MAJOR_VERSION, api.Major });
                    majorVersionIndex = adulteredAttribs.Count - 2;
                }

                if (minorVersionIndex < 0)
                {
                    adulteredAttribs.AddRange(new int[] { Gl.MINOR_VERSION, api.Minor });
                    minorVersionIndex = adulteredAttribs.Count - 2;
                }

                if (profileMaskIndex < 0)
                {
                    adulteredAttribs.AddRange(new int[] { Gl.CONTEXT_PROFILE_MASK, 0 });
                    profileMaskIndex = adulteredAttribs.Count - 2;
                }

                switch (api.Api)
                {
                case KhronosVersion.ApiGles1:
                    // Ignore API version: force always to 1.0
                    adulteredAttribs[majorVersionIndex + 1] = 1;
                    adulteredAttribs[minorVersionIndex + 1] = 1;
                    adulteredAttribs[profileMaskIndex + 1] |= (int)Glx.CONTEXT_ES_PROFILE_BIT_EXT;
                    break;

                case KhronosVersion.ApiGles2:
                    // Uses API version: it may be greater than 2.0(?)
                    adulteredAttribs[majorVersionIndex + 1] = api.Major;
                    adulteredAttribs[minorVersionIndex + 1] = api.Minor;
                    adulteredAttribs[profileMaskIndex + 1] |= (int)Glx.CONTEXT_ES_PROFILE_BIT_EXT;
                    break;

                default:
                    throw new NotSupportedException(String.Format("'{0}' API not supported", api.Api));
                }

                // Restore trailing 0
                adulteredAttribs.Add(Gl.NONE);

                using (Glx.XLock displayLock = new Glx.XLock(Display)) {
                    Debug.Assert(_FBConfig != IntPtr.Zero, "SetPixelFormat not executed");
                    return(Glx.CreateContextAttribsARB(Display, _FBConfig, sharedContext, true, adulteredAttribs.ToArray()));
                }
            }
            else
            {
                using (Glx.XLock displayLock = new Glx.XLock(Display)) {
                    Debug.Assert(_FBConfig != IntPtr.Zero, "SetPixelFormat not executed");
                    return(Glx.CreateContextAttribsARB(Display, _FBConfig, sharedContext, true, attribsList));
                }
            }
        }