2 // The Open Toolkit Library License
4 // Copyright (c) 2006 - 2009 the Open Toolkit library.
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights to
9 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 // the Software, and to permit persons to whom the Software is furnished to do
11 // so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 // OTHER DEALINGS IN THE SOFTWARE.
27 using System.Diagnostics;
29 namespace OpenTK.Graphics
32 /// Implements BindingsBase for the OpenTK.Graphics namespace (OpenGL and OpenGL|ES).
34 public abstract class GraphicsBindingsBase : BindingsBase
36 internal IntPtr[] _EntryPointsInstance;
37 internal byte[] _EntryPointNamesInstance;
38 internal int[] _EntryPointNameOffsetsInstance;
41 /// Retrieves an unmanaged function pointer to the specified function.
43 /// <param name="funcname">
44 /// A <see cref="System.String"/> that defines the name of the function.
47 /// A <see cref="IntPtr"/> that contains the address of funcname or IntPtr.Zero,
48 /// if the function is not supported by the drivers.
51 /// Note: some drivers are known to return non-zero values for unsupported functions.
52 /// Typical values include 1 and 2 - inheritors are advised to check for and ignore these
55 protected override IntPtr GetAddress(string funcname)
57 var context = GraphicsContext.CurrentContext as IGraphicsContextInternal;
60 throw new GraphicsContextMissingException();
62 return context != null ? context.GetAddress(funcname) : IntPtr.Zero;
65 // Loads all available entry points for the current API.
66 // Note: we prefer IGraphicsContextInternal.GetAddress over
67 // this.GetAddress to improve loading performance (less
68 // validation necessary.)
69 internal override void LoadEntryPoints()
71 Debug.Print("Loading entry points for {0}", GetType().FullName);
73 IGraphicsContext context = GraphicsContext.CurrentContext;
76 throw new GraphicsContextMissingException();
79 IGraphicsContextInternal context_internal = context as IGraphicsContextInternal;
82 fixed (byte* name = _EntryPointNamesInstance)
84 for (int i = 0; i < _EntryPointsInstance.Length; i++)
86 _EntryPointsInstance[i] = context_internal.GetAddress(
87 new IntPtr(name + _EntryPointNameOffsetsInstance[i]));