/// <summary>
			/// Query the extensions supported by current platform.
			/// </summary>
			public void Query(KhronosVersion version, IntPtr display)
			{
				LogComment("Query EGL extensions.");

				string eglExtensions = QueryString(display, Egl.EXTENSIONS);

				Query(version, eglExtensions ?? String.Empty);
			}
Пример #2
0
		public void TestConstructionByVersion(KhronosVersion version)
		{
			if (version > GraphicsContext.CurrentVersion)
				Assert.Inconclusive();

			using (GraphicsContext ctx = new GraphicsContext(null, version)) {
				Assert.IsTrue(ctx.Version >= version);
			}
		}
		public void QueryDrawMethods(KhronosVersion version)
		{
			FieldInfo[] drawDelegates = typeof(GeometryDrawDelegates).GetFields(BindingFlags.Public | BindingFlags.Instance);
			MethodInfo[] drawMethods = typeof(GraphicsContext).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);

			// Filter draw methods
			drawMethods = Array.FindAll(drawMethods, delegate (MethodInfo item) {
				return (item.Name.StartsWith("Draw"));
			});

			foreach (FieldInfo drawDelegate in drawDelegates) {
				Type drawDelegateType = typeof(GeometryDrawDelegates).GetNestedType(String.Format("{0}Delegate", drawDelegate.Name), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
				if (drawDelegateType == null)
					continue;

				List<MethodInfo> drawDelegateMethods = new List<MethodInfo>(Array.FindAll(drawMethods, delegate (MethodInfo item) {
					return (item.Name.StartsWith(drawDelegate.Name));
				}));

				if (drawDelegateMethods.Count == 0)
					continue;

				drawDelegateMethods.Sort(delegate (MethodInfo a, MethodInfo b) {
					KhronosVersion aVersion = GetDrawMethodVersion(drawDelegate, a);
					KhronosVersion bVersion = GetDrawMethodVersion(drawDelegate, b);

					return (bVersion.CompareTo(aVersion));
				});

				// Select the delegate designed for the most advanced version
				for (int i = 0; i < drawDelegateMethods.Count; i++) {
					KhronosVersion drawDelegateVersion = GetDrawMethodVersion(drawDelegate, drawDelegateMethods[i]);

					if (drawDelegateVersion <= version) {
						Delegate drawDelegateValue = Delegate.CreateDelegate(drawDelegateType, this, drawDelegateMethods[i]);

						drawDelegate.SetValue(_GeometryDrawDelegates, drawDelegateValue);
						break;
					}
				}
			}
		}
Пример #4
0
        public void KhronosVersion_Parse2()
        {
            KhronosVersion v;

            // Parse x.y
            v = KhronosVersion.Parse("1.4", KhronosVersion.ApiEgl);
            Assert.AreEqual(1, v.Major);
            Assert.AreEqual(4, v.Minor);
            Assert.AreEqual(0, v.Revision);
            Assert.AreEqual(KhronosVersion.ApiEgl, v.Api);
            Assert.AreEqual(null, v.Profile);

            // Parse x.y.z
            v = KhronosVersion.Parse("1.5.14", KhronosVersion.ApiEgl);
            Assert.AreEqual(1, v.Major);
            Assert.AreEqual(5, v.Minor);
            Assert.AreEqual(14, v.Revision);
            Assert.AreEqual(KhronosVersion.ApiEgl, v.Api);
            Assert.AreEqual(null, v.Profile);
        }
Пример #5
0
        public void TestParse1()
        {
            KhronosVersion v;

            // Parse x.y
            v = KhronosVersion.Parse("3.2");
            Assert.AreEqual(3, v.Major);
            Assert.AreEqual(2, v.Minor);
            Assert.AreEqual(0, v.Revision);
            Assert.AreEqual(KhronosVersion.ApiGl, v.Api);
            Assert.AreEqual(null, v.Profile);

            // Parse x.y.z
            v = KhronosVersion.Parse("1.2.1");
            Assert.AreEqual(1, v.Major);
            Assert.AreEqual(2, v.Minor);
            Assert.AreEqual(1, v.Revision);
            Assert.AreEqual(KhronosVersion.ApiGl, v.Api);
            Assert.AreEqual(null, v.Profile);
        }
Пример #6
0
        public new void FixtureSetUp()
        {
            try {
                // Create context
                _Context = CreateContext();

                // Make OpenGL context current
                if (_DeviceContext.MakeCurrent(_Context) == false)
                {
                    throw new InvalidOperationException("unable to make current the OpenGL context");
                }

                // Get OpenGL version
                if ((_VersionString = Gl.GetString(StringName.Version)) == null)
                {
                    throw new InvalidOperationException("unable to get the OpenGL version");
                }
                // Extract OpenGL version numbers
                _Version = KhronosVersion.Parse(_VersionString);
                // Get OpenGL extensions
                _GlExtensions.Query();
                // Get OpenGL window system extensions
                switch (Environment.OSVersion.Platform)
                {
                case PlatformID.Win32Windows:
                case PlatformID.Win32NT:
                    _WglExtensions.Query((WindowsDeviceContext)_DeviceContext);
                    break;

                case PlatformID.Unix:
                    _GlxExtensions.Query((XServerDeviceContext)_DeviceContext);
                    break;
                }
            } catch {
                // Release resources manually
                FixtureTearDown();

                throw;
            }
        }
Пример #7
0
        public void KhronosVersion_Parse2()
        {
            Assert.Throws <ArgumentNullException>(() => KhronosVersion.Parse(null, KhronosVersion.ApiEgl));

            KhronosVersion v;

            // Parse x.y
            v = KhronosVersion.Parse("1.4", KhronosVersion.ApiEgl);
            Assert.AreEqual(1, v.Major);
            Assert.AreEqual(4, v.Minor);
            Assert.AreEqual(0, v.Revision);
            Assert.AreEqual(KhronosVersion.ApiEgl, v.Api);
            Assert.AreEqual(null, v.Profile);

            // Parse x.y.z
            v = KhronosVersion.Parse("1.5.14", KhronosVersion.ApiEgl);
            Assert.AreEqual(1, v.Major);
            Assert.AreEqual(5, v.Minor);
            Assert.AreEqual(14, v.Revision);
            Assert.AreEqual(KhronosVersion.ApiEgl, v.Api);
            Assert.AreEqual(null, v.Profile);
        }
Пример #8
0
        public void KhronosVersion_Parse1()
        {
            KhronosVersion v;

            // Parse x.y
            v = KhronosVersion.Parse("3.2");
            Assert.AreEqual(3, v.Major);
            Assert.AreEqual(2, v.Minor);
            Assert.AreEqual(0, v.Revision);
            Assert.AreEqual(KhronosVersion.ApiGl, v.Api);
            Assert.AreEqual(null, v.Profile);

            // Parse x.y.z
            v = KhronosVersion.Parse("1.2.1");
            Assert.AreEqual(1, v.Major);
            Assert.AreEqual(2, v.Minor);
            Assert.AreEqual(1, v.Revision);
            Assert.AreEqual(KhronosVersion.ApiGl, v.Api);
            Assert.AreEqual(null, v.Profile);

            // Parse x.y.z ES
            v = KhronosVersion.Parse("2.0.17 ES");
            Assert.AreEqual(2, v.Major);
            Assert.AreEqual(0, v.Minor);
            Assert.AreEqual(17, v.Revision);
            Assert.AreEqual(KhronosVersion.ApiGles2, v.Api);
            Assert.AreEqual(null, v.Profile);

            // Parse x.y.z ES
            v = KhronosVersion.Parse("4.50");
            Assert.AreEqual(4, v.Major);
            Assert.AreEqual(5, v.Minor);
            Assert.AreEqual(0, v.Revision);
            Assert.AreEqual(KhronosVersion.ApiGl, v.Api);
            Assert.AreEqual(null, v.Profile);
        }
Пример #9
0
        public void KhronosVersion_OperatorLessOrEquals()
        {
            KhronosVersion a, b;

            Assert.Throws(typeof(InvalidOperationException), () => {
                a        = new KhronosVersion(1, 1, 0, KhronosVersion.ApiGl);
                b        = new KhronosVersion(1, 2, 0, KhronosVersion.ApiWgl);
                bool ret = a <= b;
            });

            a = new KhronosVersion(1, 0, 0, "api");
            Assert.IsFalse(a <= null);
            Assert.IsFalse(null <= a);
            Assert.IsTrue(a <= a);

            b = new KhronosVersion(1, 1, 0, "api");
            Assert.IsTrue(a <= b);

            b = new KhronosVersion(1, 0, 0, "api");
            Assert.IsTrue(a <= b);

            b = new KhronosVersion(0, 9, 0, "api");
            Assert.IsFalse(a <= b);
        }
Пример #10
0
        public void KhronosVersion_OperatorGreater()
        {
            KhronosVersion a, b;

            Assert.Throws(typeof(InvalidOperationException), () => {
                a        = new KhronosVersion(1, 1, 0, KhronosVersion.ApiGl);
                b        = new KhronosVersion(1, 2, 0, KhronosVersion.ApiWgl);
                bool ret = a > b;
            });

            a = new KhronosVersion(1, 0, 0, "api");
            Assert.IsFalse(a > null);
            Assert.IsFalse(null > a);
            Assert.IsFalse(a > a);

            b = new KhronosVersion(1, 0, 0, "api");
            Assert.IsFalse(a > b);

            b = new KhronosVersion(0, 9, 0, "api");
            Assert.IsTrue(a > b);

            b = new KhronosVersion(1, 1, 0, "api");
            Assert.IsFalse(a > b);
        }
Пример #11
0
 public void KhronosVersion_Parse1_ArgumentException(string pattern)
 {
     Assert.Throws <ArgumentException>(() => KhronosVersion.Parse(pattern));
 }
Пример #12
0
		/// <summary>
		/// Construct a GraphicsContext specifying the implemented OpenGL version.
		/// </summary>
		/// <param name="deviceContext">
		/// A <see cref="IDeviceContext"/> that specify the device context which has to be linked this
		/// this Render context.
		/// </param>
		/// <param name="sharedContext">
		/// A <see cref="GraphicsContext"/> that specify the render context which has to be linked this
		/// this Render context (to share resource with it).
		/// </param>
		/// <param name="version">
		/// A <see cref="KhronosVersion"/> that specify the minimum OpenGL version required to implement.
		/// </param>
		/// <exception cref="ArgumentException">
		/// Exception thrown in the case <paramref name="version"/> is different from the currently implemented by the derive,
		/// and the OpenGL extension WGL_ARB_create_context_profile or WGL_ARB_create_context are not implemented.
		/// </exception>
		/// <exception cref="ArgumentException">
		/// This exception is thrown in the case <paramref name="version"/> specify a forward compatible version (greater than or equal to
		/// <see cref="GLVersion.Version_3_2"/>), and the OpenGL extension WGL_ARB_create_context_profile or WGL_ARB_create_context
		/// are not implemented.
		/// </exception>
		/// <exception cref="ArgumentException">
		/// This exception is thrown in the case <paramref name="deviceContext"/> is <see cref="IntPtr.Zero"/>.
		/// </exception>
		/// <exception cref="InvalidOperationException">
		/// This exception is thrown in the case it's not possible to create a valid OpenGL context.
		/// </exception>
		/// <exception cref="ArgumentException">
		/// This exception is thrown if <paramref name="sharedContext"/> is not null and it was created by a thread different from the calling one.
		/// </exception>
		/// <exception cref="ArgumentException">
		/// This exception is thrown if <paramref name="sharedContext"/> is not null and it is disposed.
		/// </exception>
		public GraphicsContext(IDeviceContext deviceContext, GraphicsContext sharedContext, KhronosVersion version) :
			this(deviceContext, sharedContext, version, GraphicsContextFlags.None)
		{

		}
Пример #13
0
			/// <summary>
			/// Query the supported extensions.
			/// </summary>
			/// <param name="version">
			/// The <see cref="KhronosVersion"/> that specifies the version of the API context.
			/// </param>
			/// <param name="extensions">
			/// An array of strings that specifies the supported extensions.
			/// </param>
			/// <exception cref="ArgumentNullException">
			/// Exception thrown if <paramref name="extensions"/> is null.
			/// </exception>
			protected void Query(KhronosVersion version, string[] extensions)
			{
				if (version == null)
					throw new ArgumentNullException("version");
				if (extensions == null)
					throw new ArgumentNullException("extensions");

				// Cache extension names in registry
				_ExtensionsRegistry.Clear();
				foreach (string extension in extensions)
					_ExtensionsRegistry.Add(extension, true);

				// Set all extension fields
				Type thisType = GetType();

				foreach (FieldInfo fieldInfo in thisType.GetFields(BindingFlags.Instance | BindingFlags.Public)) {
					// Check boolean field (defensive)
					Debug.Assert(fieldInfo.FieldType == typeof(bool));
					if (fieldInfo.FieldType != typeof(bool))
						continue;

					bool support = false;

					// Support by extension
					Attribute[] coreAttributes = Attribute.GetCustomAttributes(fieldInfo, typeof(CoreExtensionAttribute));
					if ((coreAttributes != null) && (coreAttributes.Length > 0)) {
						foreach (CoreExtensionAttribute coreAttribute in coreAttributes) {
							if (version >= coreAttribute.Version) {
								support |= true;
								break;
							}
						}
					}

					// Support by extension
					Attribute[] extensionAttributes = Attribute.GetCustomAttributes(fieldInfo, typeof(ExtensionAttribute));
					if ((extensionAttributes != null) && (extensionAttributes.Length > 0)) {
						foreach (ExtensionAttribute extensionAttribute in extensionAttributes) {
							if (_ExtensionsRegistry.ContainsKey(extensionAttribute.ExtensionName)) {
								support |= true;
								break;
							}
						}
					}

					fieldInfo.SetValue(this, support);
				}
			}
		/// <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]);
			}
		}
Пример #15
0
        /// <summary>
        /// Initialize the conflict from the Windows window handle and platform reference.
        /// </summary>
        public void Init(IntPtr nativeDeviceHandle, IntPtr windowHandle, Win32Platform platform)
        {
            _platform = platform;

            // Load WGL.
            _openGlLibrary = _platform.LoadLibrary("opengl32.dll");
            if (_openGlLibrary == IntPtr.Zero)
            {
                Engine.Log.Error("opengl32.dll not found.", MessageSource.WGallium);
                return;
            }

            var createContext = _platform.GetFunctionByName <WglFunctions.WglCreateContext>(_openGlLibrary, "wglCreateContext");

            _deleteContext     = _platform.GetFunctionByName <WglFunctions.WglDeleteContext>(_openGlLibrary, "wglDeleteContext");
            _getProcAddress    = _platform.GetFunctionByName <WglFunctions.WglGetProcAddress>(_openGlLibrary, "wglGetProcAddress");
            _getCurrentDc      = _platform.GetFunctionByName <WglFunctions.WglGetCurrentDc>(_openGlLibrary, "wglGetCurrentDC");
            _getCurrentContext = _platform.GetFunctionByName <WglFunctions.WglGetCurrentContext>(_openGlLibrary, "wglGetCurrentContext");
            _makeCurrent       = _platform.GetFunctionByName <WglFunctions.WglMakeCurrent>(_openGlLibrary, "wglMakeCurrent");

            // A dummy context has to be created for opengl32.dll to load the. OpenGL ICD, from which we can then query WGL extensions.
            // This code will accept the Microsoft GDI ICD;
            Debug.Assert(nativeDeviceHandle != IntPtr.Zero);
            var pfd = new PixelFormatDescriptor();

            pfd.NSize      = (ushort)Marshal.SizeOf(pfd);
            pfd.NVersion   = 1;
            pfd.DwFlags    = PixelFormatFlags.DrawToWindow | PixelFormatFlags.SupportOpenGl | PixelFormatFlags.DoubleBuffer;
            pfd.PixelType  = (byte)PixelFormatFlags.RGBA;
            pfd.CColorBits = 24;

            if (!Gdi32.SetPixelFormat(nativeDeviceHandle, Gdi32.ChoosePixelFormat(nativeDeviceHandle, ref pfd), ref pfd))
            {
                Win32Platform.CheckError("WGL: Could not set pixel format on dummy context.", true);
            }

            // Establish dummy context.
            IntPtr rc = createContext(nativeDeviceHandle);

            if (rc == IntPtr.Zero)
            {
                Win32Platform.CheckError("WGL: Could not create dummy context.", true);
            }
            if (!_makeCurrent(nativeDeviceHandle, rc))
            {
                _deleteContext(rc);
                Win32Platform.CheckError("Could not make dummy context current.", true);
                return;
            }

            // Check supported version.
            KhronosVersion glGetString = Gl.QueryVersionExternal(GetProcAddress);

            if (glGetString != null)
            {
                if (glGetString.Major < 3)
                {
                    _deleteContext(rc);
                    Engine.Log.Error("Wgl support is lower than 3.0", MessageSource.Wgl);
                    return;
                }
            }
            else
            {
                Engine.Log.Warning("Couldn't verify context version.", MessageSource.Wgl);
            }

            Engine.Log.Trace("Loaded functions.", MessageSource.Wgl);

            // Functions must be loaded first as they're needed to retrieve the extension string that tells us whether the functions are supported
            _getExtensionsStringExt    = NativeHelpers.GetFunctionByPtr <WglFunctions.GetExtensionsStringExt>(_getProcAddress("wglGetExtensionsStringEXT"));
            _getExtensionsStringArb    = NativeHelpers.GetFunctionByPtr <WglFunctions.GetExtensionsStringArb>(_getProcAddress("wglGetExtensionsStringARB"));
            _createContextAttribs      = NativeHelpers.GetFunctionByPtr <WglFunctions.CreateContextAttribs>(_getProcAddress("wglCreateContextAttribsARB"));
            _swapIntervalExt           = NativeHelpers.GetFunctionByPtr <WglFunctions.SwapInternalExt>(_getProcAddress("wglSwapIntervalEXT"));
            _getPixelFormatAttribivArb = NativeHelpers.GetFunctionByPtr <WglFunctions.GetPixelFormatAttributes>(_getProcAddress("wglGetPixelFormatAttribivARB"));

            WglGetSupportedExtensions();
            _arbMultisample          = WglSupportedExtension("WGL_ARB_multisample");
            _arbFramebufferSRgb      = WglSupportedExtension("WGL_ARB_framebuffer_sRGB");
            _extFramebufferSRgb      = WglSupportedExtension("WGL_EXT_framebuffer_sRGB");
            _arbCreateContextProfile = WglSupportedExtension("WGL_ARB_create_context_profile");
            _arbPixelFormat          = WglSupportedExtension("WGL_ARB_pixel_format");
            _autoVSyncExtension      = WglSupportedExtension("WGL_EXT_swap_control_tear");
            bool arbCreateContext = WglSupportedExtension("WGL_ARB_create_context");

            // Dispose of dummy context.
            _deleteContext(rc);
            Engine.Log.Trace("Extensions loaded.", MessageSource.Wgl);

            // Start creating actual context.
            _dc = User32.GetDC(windowHandle);
            if (_dc == IntPtr.Zero)
            {
                Win32Platform.CheckError("WGL: Could not get window dc.", true);
            }
            int pixelFormatIdx = SupportedPixelFormat(_dc);

            if (pixelFormatIdx == 0)
            {
                return;
            }

            if (Gdi32.DescribePixelFormat(_dc, pixelFormatIdx, (uint)sizeof(PixelFormatDescriptor), ref pfd) == 0)
            {
                Win32Platform.CheckError("WGL: Failed to retrieve PFD for the selected pixel format.", true);
                return;
            }

            if (!Gdi32.SetPixelFormat(_dc, pixelFormatIdx, ref pfd))
            {
                Win32Platform.CheckError("WGL: Could not set pixel format.", true);
                return;
            }

            Engine.Log.Trace($"Context ARB: {arbCreateContext}, Profile ARB: {_arbCreateContextProfile}", MessageSource.Wgl);
            if (arbCreateContext)
            {
                // Context configurations to try.
                var contextFactory = new List <GLContextDescription>(2)
                {
                    new GLContextDescription {
                        Profile = GLProfile.Core, Debug = Engine.Configuration.GlDebugMode
                    },
                    new GLContextDescription {
                        Profile = GLProfile.Any
                    }
                };

                for (var i = 0; i < contextFactory.Count; i++)
                {
                    GLContextDescription current = contextFactory[i];

                    IntPtr handle = CreateContextArb(current);
                    if (handle == IntPtr.Zero)
                    {
                        continue;
                    }

                    _contextHandle = handle;
                    Engine.Log.Info($"Created WGL context - {current}", MessageSource.Wgl);
                    break;
                }

                // If that failed too, look for errors.
                // Fallback to legacy creation.
                if (_contextHandle == IntPtr.Zero)
                {
                    Win32Platform.CheckError("Creating WGL context");
                }
            }

            if (_contextHandle == IntPtr.Zero)
            {
                _contextHandle = createContext(_dc);
                if (_contextHandle == IntPtr.Zero)
                {
                    Win32Platform.CheckError("Creating WGL legacy context", true);
                }
                Engine.Log.Info("WGL legacy context created.", MessageSource.Wgl);
            }

            Win32Platform.CheckError("Checking if context creation passed.");
            Valid = true;
        }
Пример #16
0
 public void KhronosVersion_Parse2_ArgumentNullException()
 {
     Assert.Throws <ArgumentNullException>(() => KhronosVersion.Parse(null, KhronosVersion.ApiEgl));
 }
Пример #17
0
 public new void Query(KhronosVersion version, string[] extensionsString)
 {
     base.Query(version, extensionsString);
 }
Пример #18
0
        /// <summary>
        /// Append definitions recognized in a header file.
        /// </summary>
        /// <param name="path">
        /// A <see cref="System.String"/> that specified the path of the header file.
        /// </param>
        public void AppendHeader(string path, KhronosVersion feature)
        {
            string headerFeatureName = String.Format("{0}_VERSION_{1}_{2}", Class.ToUpperInvariant(), feature.Major, feature.Minor);

            AppendHeader(path, headerFeatureName);
        }
Пример #19
0
			/// <summary>
			/// Construct a CoreExtensionAttribute specifying the version numbers.
			/// </summary>
			/// <param name="major">
			/// A <see cref="Int32"/> that specifies that major version number.
			/// </param>
			/// <param name="minor">
			/// A <see cref="Int32"/> that specifies that minor version number.
			/// </param>
			/// <param name="revision">
			/// A <see cref="Int32"/> that specifies that revision version number.
			/// </param>
			/// <param name="api">
			/// A <see cref="String"/> that specifies the API name.
			/// </param>
			/// <exception cref="ArgumentException">
			/// Exception thrown if <paramref name="major"/> is less or equals to 0, or if <paramref name="minor"/> or
			/// <paramref name="revision"/> are less than 0.
			/// </exception>
			/// <exception cref="ArgumentNullException">
			/// Exception thrown if <paramref name="api"/> is null.
			/// </exception>
			public CoreExtensionAttribute(int major, int minor, int revision, string api)
			{
				Version = new KhronosVersion(major, minor, revision, api);
			}
Пример #20
0
 public void TestParse1_ArgumentNullException()
 {
     KhronosVersion.Parse(null);
 }
Пример #21
0
		/// <summary>
		/// Construct a GraphicsContext specifying the implemented OpenGL version.
		/// </summary>
		/// <param name="deviceContext">
		/// A <see cref="IDeviceContext"/> that specify the device context which has to be linked this
		/// this Render context.
		/// </param>
		/// <param name="sharedContext">
		/// A <see cref="GraphicsContext"/> that specify the render context which has to be linked this
		/// this Render context (to share resource with it).
		/// </param>
		/// <param name="version">
		/// A <see cref="KhronosVersion"/> that specify the minimum OpenGL version required to implement.
		/// </param>
		/// <param name="flags">
		/// A <see cref="GraphicsContextFlags"/> that specify special features to enable in the case they are supported.
		/// </param>
		/// <exception cref="ArgumentException">
		/// Exception thrown in the case <paramref name="version"/> is different from the currently implemented by the derive,
		/// and the OpenGL extension WGL_ARB_create_context_profile or WGL_ARB_create_context are not implemented.
		/// </exception>
		/// <exception cref="ArgumentException">
		/// This exception is thrown in the case <paramref name="version"/> specify a forward compatible version (greater than or equal to
		/// <see cref="GLVersion.Version_3_2"/>), and the OpenGL extension WGL_ARB_create_context_profile or WGL_ARB_create_context
		/// are not implemented.
		/// </exception>
		/// <exception cref="ArgumentException">
		/// This exception is thrown in the case <paramref name="devctx"/> is <see cref="IntPtr.Zero"/>.
		/// </exception>
		/// <exception cref="InvalidOperationException">
		/// This exception is thrown in the case it's not possible to create a valid OpenGL context.
		/// </exception>
		/// <exception cref="ArgumentException">
		/// This exception is thrown if <paramref name="sharedContext"/> is not null and it was created by a thread different from the calling one.
		/// </exception>
		/// <exception cref="ArgumentException">
		/// This exception is thrown if <paramref name="sharedContext"/> is not null and it is disposed.
		/// </exception>
		public GraphicsContext(IDeviceContext deviceContext, GraphicsContext sharedContext, KhronosVersion version, GraphicsContextFlags flags)
		{
			try {
				IntPtr sharedContextHandle = (sharedContext != null) ? sharedContext._RenderContext : IntPtr.Zero;

#if DEBUG
				_ConstructorStackTrace = Environment.StackTrace;
#endif

				// Store thread ID of the render context
				_RenderContextThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
				// Store thread ID of the device context
				_DeviceContextThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId;

				if (deviceContext == null)
					throw new ArgumentNullException("deviceContext");
				if ((sharedContext != null) && (sharedContext._DeviceContext == null))
					throw new ArgumentException("shared context disposed", "hSharedContext");
				if ((sharedContext != null) && (sharedContext._RenderContextThreadId != _RenderContextThreadId))
					throw new ArgumentException("shared context created from another thread", "hSharedContext");
				if ((version != null) && (version != _CurrentVersion) && ((CurrentCaps.PlatformExtensions.CreateContext_ARB == false) && (CurrentCaps.PlatformExtensions.CreateContextProfile_ARB == false)))
					throw new ArgumentException("unable to specify OpenGL version when GL_ARB_create_context[_profile] is not supported");

				// Store device context handle
				_DeviceContext = deviceContext;
				_DeviceContext.IncRef();

				// Allow version to be null (fallback to current version)
				version = version ?? _CurrentVersion;
				// Set flags
				_ContextFlags = flags;

				if ((CurrentCaps.PlatformExtensions.CreateContext_ARB || CurrentCaps.PlatformExtensions.CreateContextProfile_ARB) && (version.Major >= 3)) {
					List<int> cAttributes = new List<int>();

					#region Context Version

					// Requires a specific version
					Debug.Assert(Wgl.CONTEXT_MAJOR_VERSION_ARB == Glx.CONTEXT_MAJOR_VERSION_ARB);
					Debug.Assert(Wgl.CONTEXT_MINOR_VERSION_ARB == Glx.CONTEXT_MINOR_VERSION_ARB);
					cAttributes.AddRange(new int[] {
						Wgl.CONTEXT_MAJOR_VERSION_ARB, version.Major,
						Wgl.CONTEXT_MINOR_VERSION_ARB, version.Minor
					});

					#endregion

					#region Context Profile

					uint contextProfile = 0;

					// Check binary compatibility between WGL and GLX
					Debug.Assert(Wgl.CONTEXT_PROFILE_MASK_ARB == Glx.CONTEXT_PROFILE_MASK_ARB);
					Debug.Assert(Wgl.CONTEXT_CORE_PROFILE_BIT_ARB == Glx.CONTEXT_CORE_PROFILE_BIT_ARB);
					Debug.Assert(Wgl.CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB == Glx.CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB);
					Debug.Assert(Wgl.CONTEXT_ES_PROFILE_BIT_EXT == Glx.CONTEXT_ES_PROFILE_BIT_EXT);

					// By default, Core profile

					// Core profile?
					if ((flags & GraphicsContextFlags.CoreProfile) != 0)
						contextProfile |= Wgl.CONTEXT_CORE_PROFILE_BIT_ARB;
					// Compatibility profile?
					if ((flags & GraphicsContextFlags.CompatibilityProfile) != 0)
						contextProfile |= Wgl.CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
					// OpenGL ES profile?
					if ((flags & GraphicsContextFlags.EmbeddedProfile) != 0)
						contextProfile |= Wgl.CONTEXT_ES_PROFILE_BIT_EXT;

					if (contextProfile != 0) {
						cAttributes.AddRange(new int[] {
							Wgl.CONTEXT_PROFILE_MASK_ARB, unchecked((int)contextProfile)
						});
					}

					#endregion

					#region Context Flags

					uint contextFlags = 0;

					// Check binary compatibility between WGL and GLX
					Debug.Assert(Wgl.CONTEXT_FLAGS_ARB == Glx.CONTEXT_FLAGS_ARB);
					Debug.Assert(Wgl.CONTEXT_FORWARD_COMPATIBLE_BIT_ARB == Glx.CONTEXT_FORWARD_COMPATIBLE_BIT_ARB);
					Debug.Assert(Wgl.CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB == Glx.CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB);
					Debug.Assert(Wgl.CONTEXT_DEBUG_BIT_ARB == Glx.CONTEXT_DEBUG_BIT_ARB);
					Debug.Assert(Wgl.CONTEXT_ROBUST_ACCESS_BIT_ARB == Glx.CONTEXT_ROBUST_ACCESS_BIT_ARB);
					Debug.Assert(Wgl.CONTEXT_RESET_ISOLATION_BIT_ARB == Glx.CONTEXT_RESET_ISOLATION_BIT_ARB);

					if (((flags & GraphicsContextFlags.CompatibilityProfile) != 0) && (_CurrentCaps.GlExtensions.Compatibility_ARB == false))
						throw new NotSupportedException("compatibility profile not supported");
					if (((flags & GraphicsContextFlags.Robust) != 0) && (_CurrentCaps.GlExtensions.Robustness_ARB == false && _CurrentCaps.GlExtensions.Robustness_EXT == false))
						throw new NotSupportedException("robust profile not supported");

					// Context flags: debug context
					if ((flags & GraphicsContextFlags.Debug) != 0)
						contextFlags |= Wgl.CONTEXT_DEBUG_BIT_ARB;
					// Context flags: forward compatible context
					if ((flags & GraphicsContextFlags.ForwardCompatible) != 0)
						contextFlags |= Wgl.CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
					// Context flags: robust behavior
					if ((flags & GraphicsContextFlags.Robust) != 0)
						contextFlags |= Wgl.CONTEXT_ROBUST_ACCESS_BIT_ARB;
					// Context flags: reset isolation
					if ((flags & GraphicsContextFlags.ResetIsolation) != 0)
						contextFlags |= Wgl.CONTEXT_RESET_ISOLATION_BIT_ARB;

					if (contextFlags != 0) {
						cAttributes.AddRange(new int[] {
							Wgl.CONTEXT_FLAGS_ARB, unchecked((int)contextFlags)
						});
					}

					#endregion

					// End of attributes
					cAttributes.Add(0);

					// Create rendering context
					int[] contextAttributes = cAttributes.ToArray();

					_RenderContext = _DeviceContext.CreateContextAttrib(sharedContextHandle, contextAttributes);
					Debug.Assert(_RenderContext != IntPtr.Zero);
				} else {
					// Create rendering context
					_RenderContext = _DeviceContext.CreateContext(sharedContextHandle);
					Debug.Assert(_RenderContext != IntPtr.Zero);
				}

				if (_RenderContext == IntPtr.Zero)
					throw new InvalidOperationException(String.Format("unable to create context {0}", version));

				// Allow the creation of a GraphicsContext while another GraphicsContext is currently current to the
				// calling thread: restore currency after the job get done
				GraphicsContext prevContext = GetCurrentContext();
				IDeviceContext prevContextDevice = (prevContext != null) ? prevContext._CurrentDeviceContext : null;

				// This will cause OpenGL operation flushed... not too bad
				MakeCurrent(deviceContext, true);

				// Get the current OpenGL implementation supported by this GraphicsContext
				_Version = KhronosVersion.Parse(Gl.GetString(StringName.Version));
				// Get the current OpenGL Shading Language implementation supported by this GraphicsContext
				_ShadingVersion = KhronosVersion.Parse(Gl.GetString(StringName.ShadingLanguageVersion));
				// Query context capabilities
				_CapsStack.Push(GraphicsCapabilities.Query(this, deviceContext));

				// Determine this GraphicsContext object namespace
				if (sharedContext != null) {
					// Sharing same object name space
					_ObjectNameSpace = sharedContext._ObjectNameSpace;
				} else {
					// Reserved object name space
					_ObjectNameSpace = Guid.NewGuid();
				}

				// Create shader include library (GLSL #include support)
				_ShaderIncludeLibrary = new ShaderIncludeLibrary();
				_ShaderIncludeLibrary.Create(this);

				// Restore previous current context, if any. Otherwise, make uncurrent
				if (prevContext != null)
					prevContext.MakeCurrent(prevContextDevice, true);
				else
					MakeCurrent(deviceContext, false);
			} catch {
				// Rethrow the exception
				throw;
			}
		}
Пример #22
0
 public static void CheckExtensionCommands(KhronosVersion version, ExtensionsCollection extensions, bool enableExtensions)
 {
     CheckExtensionCommands <TestApi>(version, extensions, enableExtensions);
 }
Пример #23
0
 public void TestParse1_ArgumentException(string pattern)
 {
     KhronosVersion.Parse(pattern);
 }
Пример #24
0
			/// <summary>
			/// Query the supported extensions.
			/// </summary>
			/// <param name="version">
			/// The <see cref="KhronosVersion"/> that specifies the version of the API context.
			/// </param>
			/// <param name="extensionsString">
			/// A string that specifies the supported extensions, those names are separated by spaces.
			/// </param>
			/// <exception cref="ArgumentNullException">
			/// Exception thrown if <paramref name="extensionsString"/> is null.
			/// </exception>
			protected void Query(KhronosVersion version, string extensionsString)
			{
				if (extensionsString == null)
					throw new ArgumentNullException("extensionsString");

				Query(version, extensionsString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
			}
Пример #25
0
 public void TestParse2_ArgumentNullException()
 {
     KhronosVersion.Parse(null, KhronosVersion.ApiEgl);
 }
Пример #26
0
 public void TestParse1_ArgumentNullException()
 {
     Assert.Throws <ArgumentNullException>(() => KhronosVersion.Parse(null));
 }
Пример #27
0
		/// <summary>
		/// Construct a GraphicsContext specifying the implemented OpenGL version.
		/// </summary>
		/// <param name="sharedContext">
		/// A <see cref="GraphicsContext"/> that specify the render context which has to be linked this
		/// this Render context (to share resource with it).
		/// </param>
		/// <param name="version">
		/// A <see cref="KhronosVersion"/> that specify the minimum OpenGL version required to implement.
		/// </param>
		/// <param name="flags">
		/// A <see cref="GraphicsContextFlags"/> that specify special features to enable in the case they are supported.
		/// </param>
		/// <exception cref="ArgumentException">
		/// Exception thrown in the case <paramref name="version"/> is different from the currently implemented by the derive,
		/// and the OpenGL extension WGL_ARB_create_context_profile or WGL_ARB_create_context are not implemented.
		/// </exception>
		/// <exception cref="ArgumentException">
		/// This exception is thrown in the case <paramref name="version"/> specify a forward compatible version (greater than or equal to
		/// <see cref="GLVersion.Version_3_2"/>), and the OpenGL extension WGL_ARB_create_context_profile or WGL_ARB_create_context
		/// are not implemented.
		/// </exception>
		/// <exception cref="InvalidOperationException">
		/// This exception is thrown in the case it's not possible to create a valid OpenGL context.
		/// </exception>
		/// <exception cref="ArgumentException">
		/// This exception is thrown if <paramref name="sharedContext"/> is not null and it was created by a thread different from the calling one.
		/// </exception>
		/// <exception cref="ArgumentException">
		/// This exception is thrown if <paramref name="sharedContext"/> is not null and it is disposed.
		/// </exception>
		public GraphicsContext(GraphicsContext sharedContext, KhronosVersion version, GraphicsContextFlags flags) :
			this(_HiddenWindowDevice, sharedContext, version, flags)
		{

		}