|
| 1 | +#region License |
| 2 | +// |
| 3 | +// The Open Toolkit Library License |
| 4 | +// |
| 5 | +// Copyright (c) 2006 - 2009 the Open Toolkit library. |
| 6 | +// |
| 7 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +// of this software and associated documentation files (the "Software"), to deal |
| 9 | +// in the Software without restriction, including without limitation the rights to |
| 10 | +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 11 | +// the Software, and to permit persons to whom the Software is furnished to do |
| 12 | +// so, subject to the following conditions: |
| 13 | +// |
| 14 | +// The above copyright notice and this permission notice shall be included in all |
| 15 | +// copies or substantial portions of the Software. |
| 16 | +// |
| 17 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 19 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 21 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 22 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 24 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 25 | +// |
| 26 | +#endregion |
| 27 | + |
| 28 | +using System; |
| 29 | +using System.Collections.Generic; |
| 30 | +using System.Text; |
| 31 | +using System.Diagnostics; |
| 32 | + |
| 33 | +namespace OpenTK.Graphics.ES30 |
| 34 | +{ |
| 35 | + // Used in debug-mode only, for automatic OpenGL error-checking. |
| 36 | + // |
| 37 | + // Works like this: an instance is created before each OpenGL function is called. |
| 38 | + // The constructor resets the OpenGL error state. Once the native function returns, |
| 39 | + // the error state is checked again, raising the relevant exceptions. |
| 40 | + // |
| 41 | + // A using-region is used to ensure Dispose() is called. |
| 42 | + // |
| 43 | + // Make sure that no error checking is added to the GetError function, |
| 44 | + // as that would cause infinite recursion! |
| 45 | + struct ErrorHelper : IDisposable |
| 46 | + { |
| 47 | + #region Fields |
| 48 | + |
| 49 | + static readonly object SyncRoot = new object(); |
| 50 | + static readonly Dictionary<GraphicsContext, List<ErrorCode>> ContextErrors = |
| 51 | + new Dictionary<GraphicsContext, List<ErrorCode>>(); |
| 52 | + readonly GraphicsContext Context; |
| 53 | + |
| 54 | + #endregion |
| 55 | + |
| 56 | + #region Constructors |
| 57 | + |
| 58 | + public ErrorHelper(IGraphicsContext context) |
| 59 | + { |
| 60 | + if (context == null) |
| 61 | + throw new GraphicsContextMissingException(); |
| 62 | + |
| 63 | + Context = (GraphicsContext)context; |
| 64 | + lock (SyncRoot) |
| 65 | + { |
| 66 | + if (!ContextErrors.ContainsKey(Context)) |
| 67 | + ContextErrors.Add(Context, new List<ErrorCode>()); |
| 68 | + } |
| 69 | + ResetErrors(); |
| 70 | + } |
| 71 | + |
| 72 | + #endregion |
| 73 | + |
| 74 | + #region Public Members |
| 75 | + |
| 76 | + // Retrieve all OpenGL errors to clear the error list. |
| 77 | + // See http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/geterror.html |
| 78 | + [Conditional("DEBUG")] |
| 79 | + internal void ResetErrors() |
| 80 | + { |
| 81 | + if (Context.ErrorChecking) |
| 82 | + { |
| 83 | + while ((ErrorCode)GL.GetError() != ErrorCode.NoError) |
| 84 | + { } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Retrieve all OpenGL errors and throw an exception if anything other than NoError is returned. |
| 89 | + [Conditional("DEBUG")] |
| 90 | + internal void CheckErrors() |
| 91 | + { |
| 92 | + if (Context.ErrorChecking) |
| 93 | + { |
| 94 | + List<ErrorCode> error_list = ContextErrors[Context]; |
| 95 | + error_list.Clear(); |
| 96 | + ErrorCode error; |
| 97 | + do |
| 98 | + { |
| 99 | + error = (ErrorCode)GL.GetError(); |
| 100 | + error_list.Add(error); |
| 101 | + } while (error != ErrorCode.NoError); |
| 102 | + |
| 103 | + if (error_list.Count != 1) |
| 104 | + { |
| 105 | + StringBuilder sb = new StringBuilder(); |
| 106 | + foreach (ErrorCode e in error_list) |
| 107 | + { |
| 108 | + if (e != ErrorCode.NoError) |
| 109 | + { |
| 110 | + sb.Append(e.ToString()); |
| 111 | + sb.Append(", "); |
| 112 | + } |
| 113 | + else |
| 114 | + break; |
| 115 | + } |
| 116 | + sb.Remove(sb.Length - 2, 2); // Remove the last comma |
| 117 | + |
| 118 | + throw new GraphicsErrorException(sb.ToString()); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + #endregion |
| 124 | + |
| 125 | + #region IDisposable Members |
| 126 | + |
| 127 | + public void Dispose() |
| 128 | + { |
| 129 | + CheckErrors(); |
| 130 | + } |
| 131 | + |
| 132 | + #endregion |
| 133 | + } |
| 134 | +} |
0 commit comments