From 3b95b7e871a95acfa53d5c5599208907e96007ac Mon Sep 17 00:00:00 2001 From: David Allemang Date: Tue, 28 Feb 2017 23:12:51 -0500 Subject: [PATCH] Started migrating to Factory/Wrapper pattern - Shader complete --- Diamond/Buffers/GLBuffer.cs | 2 +- Diamond/Diamond.csproj | 3 +- Diamond/GLObject.cs | 71 +++---- Diamond/GLWrapper.cs | 41 ++++ Diamond/Level/Level.cs | 3 +- Diamond/NLog.config | 10 +- Diamond/NLog.xsd | 380 ++++++++++++++++++------------------ Diamond/Shaders/Program.cs | 20 +- Diamond/Shaders/Shader.cs | 203 ++++++++++--------- Diamond/Textures/Texture.cs | 2 +- 10 files changed, 384 insertions(+), 351 deletions(-) create mode 100644 Diamond/GLWrapper.cs diff --git a/Diamond/Buffers/GLBuffer.cs b/Diamond/Buffers/GLBuffer.cs index dce5f51..de5f3fd 100644 --- a/Diamond/Buffers/GLBuffer.cs +++ b/Diamond/Buffers/GLBuffer.cs @@ -5,7 +5,7 @@ using OpenTK.Graphics.OpenGL4; namespace Diamond.Buffers { - public class GLBuffer : GLObject where T : struct + public class GLBuffer : GLWrapper where T : struct { public readonly BufferTarget Target; public readonly BufferUsageHint Usage; diff --git a/Diamond/Diamond.csproj b/Diamond/Diamond.csproj index c40d2ad..2474d1b 100644 --- a/Diamond/Diamond.csproj +++ b/Diamond/Diamond.csproj @@ -52,9 +52,10 @@ + - + diff --git a/Diamond/GLObject.cs b/Diamond/GLObject.cs index 04dfd7e..03eb209 100644 --- a/Diamond/GLObject.cs +++ b/Diamond/GLObject.cs @@ -1,66 +1,47 @@ using System; -using System.Diagnostics; -using OpenTK.Graphics; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; using NLog; +using OpenTK.Graphics; namespace Diamond { - /// - /// Parent class for all gl Object wrappers. - /// public abstract class GLObject : IDisposable { - /// - /// Logger for this class - /// - protected Logger Log { get; } + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + private bool _disposed; - /// - /// The name of this object - /// - public uint Id { get; protected set; } + public abstract int Id { get; } - /// - /// Force all GLObjects to define their name. - /// - /// The name of this object - protected GLObject(uint id) + protected abstract GLWrapper Wrapper { get; } + + protected virtual void Dispose(bool disposing) { - Id = id; - - Log = LogManager.GetLogger(GetType().FullName); - Log.Trace("Created {0}", this); - } - - /// - /// Called to free the name of this object. Usually corresponds to glDelete*. - /// - protected abstract void Delete(); - - /// - /// Free the name of this object - /// - public void Dispose() - { - if (GraphicsContext.CurrentContext == null) - { - Log.Warn("No current context, assuming {0} is disposed.", this); + if (_disposed) return; + + if (disposing) + { + if (GraphicsContext.CurrentContext == null) + Logger.Warn("No graphics context, cannot dispose GLObject: {0}", Wrapper); + else + Wrapper.Dispose(); } - Delete(); - Log.Trace("Disposed {0}", this); + _disposed = true; + } + + public void Dispose() + { + Dispose(true); GC.SuppressFinalize(this); } ~GLObject() { - Dispose(); + Dispose(false); } - - public override string ToString() => $"{GetType().Name} {Id}"; - - public static explicit operator uint(GLObject o) => o.Id; - public static explicit operator int(GLObject o) => (int) o.Id; } } \ No newline at end of file diff --git a/Diamond/GLWrapper.cs b/Diamond/GLWrapper.cs new file mode 100644 index 0000000..4b31019 --- /dev/null +++ b/Diamond/GLWrapper.cs @@ -0,0 +1,41 @@ +using System; +using System.Diagnostics; +using OpenTK.Graphics; +using NLog; + +namespace Diamond +{ + internal abstract class GLWrapper : IDisposable + { + protected static Logger Logger = LogManager.GetCurrentClassLogger(); + + public int Id { get; protected set; } + + public override string ToString() => $"{GetType().Name} {Id}"; + + #region IDisposable + + public abstract void GLDelete(); + + private bool _disposed; + + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + GLDelete(); + + _disposed = true; + } + + public void Dispose() + { + Dispose(true); + } + + #endregion + + public static explicit operator int(GLWrapper o) => o.Id; + } +} \ No newline at end of file diff --git a/Diamond/Level/Level.cs b/Diamond/Level/Level.cs index 6554a78..63e28ed 100644 --- a/Diamond/Level/Level.cs +++ b/Diamond/Level/Level.cs @@ -7,6 +7,7 @@ using System.Linq; using Diamond.Buffers; using Diamond.Shaders; using Diamond.Textures; +using Diamond.Util; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using OpenTK; @@ -70,7 +71,7 @@ namespace Diamond.Level var texturePaths = levelData["textures"] .Select(path => (string) path) .ToArray(); - var textures = texturePaths.Select(path => Texture.FromBitmap(new Bitmap(Path.Combine(dir, (string)path)))).ToArray(); + var textures = texturePaths.Select(path => Texture.FromBitmap(new Bitmap(Path.Combine(dir, path)))).ToArray(); var textureMap = texturePaths.Select((path, i) => new {path = path, i = i}) .ToDictionary(v => v.path, v => v.i); diff --git a/Diamond/NLog.config b/Diamond/NLog.config index 2c561a4..5931ee4 100644 --- a/Diamond/NLog.config +++ b/Diamond/NLog.config @@ -1,15 +1,15 @@ - - + + internalLoggerLevel="Off" internalLoggerFile="c:\temp\nlog-internal.log"> - diff --git a/Diamond/NLog.xsd b/Diamond/NLog.xsd index 946b2ea..e7ad6b0 100644 --- a/Diamond/NLog.xsd +++ b/Diamond/NLog.xsd @@ -1,13 +1,13 @@  - - - + + + - - - - - + + + + + @@ -15,43 +15,43 @@ Watch config file for changes and reload automatically. - + - Print internal NLog messages to the console. Default value is: false + Print internal NLogger messages to the console. Default value is: false - + - Print internal NLog messages to the console error output. Default value is: false + Print internal NLogger messages to the console error output. Default value is: false - + - Write internal NLog messages to the specified file. + Write internal NLogger messages to the specified file. - + - Log level threshold for internal log messages. Default value is: Info. + Logger level threshold for internal log messages. Default value is: Info. - + Global log level threshold for application log messages. Messages below this level won't be logged.. - Pass NLog internal exceptions to the application. Default value is: false. + Pass NLogger internal exceptions to the application. Default value is: false. - + - Write internal NLog messages to the the System.Diagnostics.Trace. Default value is: false + Write internal NLogger messages to the the System.Diagnostics.Trace. Default value is: false - + @@ -65,17 +65,17 @@ - + - + - + - + - + Prefix for targets/layout renderers/filters/conditions loaded from this assembly. @@ -83,45 +83,45 @@ - Load NLog extensions from the specified file (*.dll) + Load NLogger extensions from the specified file (*.dll) - Load NLog extensions from the specified assembly. Assembly name should be fully qualified. + Load NLogger extensions from the specified assembly. Assembly name should be fully qualified. - + - + Name of the logger. May include '*' character which acts like a wildcard. Allowed forms are: *, Name, *Name, Name* and *Name* - + Comma separated list of levels that this rule matches. - + Minimum level that this rule matches. - + Maximum level that this rule matches. - + Level that this rule matches. - + Comma separated list of target names. @@ -137,7 +137,7 @@ - + @@ -146,7 +146,7 @@ - + @@ -166,12 +166,12 @@ - + - + Name of the file to be included. The name is relative to the name of the current config file. @@ -183,7 +183,7 @@ - + Variable name. @@ -195,7 +195,7 @@ - + @@ -251,7 +251,7 @@ - + @@ -271,7 +271,7 @@ Limit of full s to write before yielding into Performance is better when writing many small batches, than writing a single large batch - + Action to be taken when the lazy writer thread request queue count exceeds the set limit. @@ -288,13 +288,13 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit - + @@ -316,12 +316,12 @@ - Condition expression. Log events who meet this condition will cause a flush on the wrapped target. + Condition expression. Logger events who meet this condition will cause a flush on the wrapped target. - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -359,7 +359,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -375,17 +375,17 @@ - - + + - + - + @@ -422,12 +422,12 @@ Indicates whether to append newline at the end of log message. - + Action that should be taken if the will be more connections than . - + Action that should be taken if the message is larger than maxMessageSize. @@ -467,9 +467,9 @@ Indicates whether to include source info (file name and line number) in the information sent over the network. - + - Indicates whether to include NLog-specific extensions to log4j schema. + Indicates whether to include NLogger-specific extensions to log4j schema. @@ -494,27 +494,27 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit - + - + - + @@ -539,8 +539,8 @@ - - + + @@ -588,13 +588,13 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit - + @@ -615,37 +615,37 @@ - + - - + + Condition that must be met in order to set the specified foreground and background color. - + Background color. - + Foreground color. - + - - + + @@ -672,12 +672,12 @@ Compile the ? This can improve the performance, but at the costs of more memory usage. If false, the Regex Cache is used. - + Background color. - + Foreground color. @@ -733,7 +733,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -754,10 +754,10 @@ - - + + - + @@ -818,7 +818,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -841,12 +841,12 @@ - + - + @@ -870,7 +870,7 @@ - + @@ -936,7 +936,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -962,13 +962,13 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit - + @@ -979,7 +979,7 @@ - + @@ -1006,12 +1006,12 @@ - Name of the Event Log to write to. This can be System, Application or any user-defined name. + Name of the Event Logger to write to. This can be System, Application or any user-defined name. - Name of the machine on which Event Log service is running. + Name of the machine on which Event Logger service is running. @@ -1019,7 +1019,7 @@ Value to be used as the event Source. - + Action to take if the message is larger than the option. @@ -1031,18 +1031,18 @@ - Message length limit to write to the Event Log. + Message length limit to write to the Event Logger. - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit - + @@ -1069,7 +1069,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -1085,30 +1085,30 @@ - + - + - + - + - - + + - + @@ -1148,7 +1148,7 @@ Line ending mode. - + Way file archives are numbered. @@ -1158,7 +1158,7 @@ Name of the file to be used for an archive. - + Indicates whether to automatically archive log files every time the specified time passes. @@ -1183,7 +1183,7 @@ Gets or set a value indicating whether a managed file stream is forced, instead of using the native implementation. - + Is the an absolute or relative path? @@ -1195,10 +1195,10 @@ - Whether or not this target should just discard all data that its asked to write. Mostly used for when testing NLog Stack except final write + Whether or not this target should just discard all data that its asked to write. Mostly used for when testing NLogger Stack except final write - + Is the an absolute or relative path? @@ -1233,12 +1233,12 @@ Indicates whether to create directories if they do not exist. - + Indicates whether to enable log file(s) to be deleted. - + File attributes (Windows only). @@ -1263,7 +1263,7 @@ Indicates whether to keep log file open instead of opening and closing it on each logging event. - + Maximum number of log filenames that should be stored as existing. @@ -1285,12 +1285,12 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit - Log file buffer size in bytes. + Logger file buffer size in bytes. @@ -1305,13 +1305,13 @@ - Number of times the write is appended on the file before NLog discards the log message. + Number of times the write is appended on the file before NLogger discards the log message. - + @@ -1319,7 +1319,7 @@ - + @@ -1329,14 +1329,14 @@ - + - + @@ -1352,7 +1352,7 @@ - + @@ -1371,12 +1371,12 @@ - Condition expression. Log events who meet this condition will be forwarded to the wrapped target. + Condition expression. Logger events who meet this condition will be forwarded to the wrapped target. - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -1388,9 +1388,9 @@ - - - + + + @@ -1406,19 +1406,19 @@ Windows domain name to change context to. - + Required impersonation level. - + ShaderType of the logon provider. - + - Logon ShaderType. + Loggeron ShaderType. @@ -1438,13 +1438,13 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit - + @@ -1452,12 +1452,12 @@ - + - + @@ -1493,13 +1493,13 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit - + @@ -1509,7 +1509,7 @@ - + @@ -1550,13 +1550,13 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit - + @@ -1600,7 +1600,7 @@ - + @@ -1691,7 +1691,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -1699,7 +1699,7 @@ SMTP Server to be used for sending. - + SMTP Authentication mode. @@ -1742,7 +1742,7 @@ - + @@ -1776,7 +1776,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -1824,7 +1824,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -1857,7 +1857,7 @@ - + @@ -1877,7 +1877,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -1893,8 +1893,8 @@ - - + + @@ -1932,12 +1932,12 @@ Indicates whether to append newline at the end of log message. - + Action that should be taken if the will be more connections than . - + Action that should be taken if the message is larger than maxMessageSize. @@ -1969,13 +1969,13 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit - + @@ -1985,17 +1985,17 @@ - - + + - + - + @@ -2032,12 +2032,12 @@ Indicates whether to append newline at the end of log message. - + Action that should be taken if the will be more connections than . - + Action that should be taken if the message is larger than maxMessageSize. @@ -2077,9 +2077,9 @@ Indicates whether to include source info (file name and line number) in the information sent over the network. - + - Indicates whether to include NLog-specific extensions to log4j schema. + Indicates whether to include NLogger-specific extensions to log4j schema. @@ -2104,7 +2104,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -2136,7 +2136,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -2162,7 +2162,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -2224,7 +2224,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -2268,7 +2268,7 @@ - + @@ -2283,13 +2283,13 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit - + @@ -2319,7 +2319,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -2340,7 +2340,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -2367,7 +2367,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -2397,7 +2397,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -2417,7 +2417,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -2443,7 +2443,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -2455,14 +2455,14 @@ - + - + - + @@ -2479,7 +2479,7 @@ - Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLog-targets, that expects buffers to remain stable after Write-method exit + Target supports reuse of internal buffers, and doesn't have to constantly allocate new buffers Required for legacy NLogger-targets, that expects buffers to remain stable after Write-method exit @@ -2487,9 +2487,9 @@ Encoding. - + - Value whether escaping be done according to the old NLog style (Very non-standard) + Value whether escaping be done according to the old NLogger style (Very non-standard) @@ -2507,7 +2507,7 @@ Web service namespace. Only used with Soap. - + Protocol to be used when calling web service. @@ -2530,7 +2530,7 @@ - + @@ -2559,11 +2559,11 @@ - + - + - + @@ -2586,7 +2586,7 @@ Custom column delimiter value (valid when ColumnDelimiter is set to 'Custom'). - + Column delimiter. @@ -2596,7 +2596,7 @@ Quote Character. - + Quoting mode. @@ -2609,7 +2609,7 @@ - + @@ -2620,14 +2620,14 @@ - + - + @@ -2647,7 +2647,7 @@ - + @@ -2676,7 +2676,7 @@ - + @@ -2724,7 +2724,7 @@ - + @@ -2768,9 +2768,9 @@ - + - + diff --git a/Diamond/Shaders/Program.cs b/Diamond/Shaders/Program.cs index acb1bee..4ff55cd 100644 --- a/Diamond/Shaders/Program.cs +++ b/Diamond/Shaders/Program.cs @@ -5,18 +5,18 @@ using OpenTK.Graphics.OpenGL4; namespace Diamond.Shaders { - public class Program : GLObject + public class Program : GLWrapper { public static Program Current { get; private set; } private readonly Dictionary _uniforms = new Dictionary(); private readonly Dictionary _attributes = new Dictionary(); - public string InfoLog => GL.GetProgramInfoLog((int) Id).Trim(); + public string InfoLog => GL.GetProgramInfoLog(Id).Trim(); public Program() - : base((uint) GL.CreateProgram()) { + Id = GL.CreateProgram(); } protected override void Delete() @@ -47,18 +47,18 @@ namespace Diamond.Shaders if (!Linked) { - Log.Warn("Failed to link Program {0}", Id); - Log.Debug("Program {0} InfoLog\n{1}", Id, InfoLog); + Logger.Warn("Failed to link Program {0}", Id); + Logger.Debug("Program {0} InfoLog\n{1}", Id, InfoLog); return false; } - Log.Info("Successfully linked Program {0}", Id); + Logger.Info("Successfully linked Program {0}", Id); GL.GetProgram(Id, GetProgramParameterName.ActiveUniforms, out int uniformcount); for (var i = 0; i < uniformcount; i++) { var sb = new StringBuilder(256); - GL.GetActiveUniformName((int) Id, i, sb.Capacity, out int length, sb); + GL.GetActiveUniformName(Id, i, sb.Capacity, out int length, sb); _uniforms[sb.ToString()] = i; } @@ -66,7 +66,7 @@ namespace Diamond.Shaders for (var i = 0; i < attributecount; i++) { var sb = new StringBuilder(256); - GL.GetActiveAttrib((int) Id, i, sb.Capacity, out int length, out int size, + GL.GetActiveAttrib(Id, i, sb.Capacity, out int length, out int size, out ActiveAttribType type, sb); _attributes[sb.ToString()] = i; } @@ -83,7 +83,7 @@ namespace Diamond.Shaders { if (TryGetUniform(name, out int id)) return id; - Log.Warn("Attempted to access uniform {0} on Program {1}", name, Id); + Logger.Warn("Attempted to access uniform {0} on Program {1}", name, Id); throw new ShaderException($"Shader Program {Id} does not contain uniform '{name}'"); } @@ -96,7 +96,7 @@ namespace Diamond.Shaders { if (TryGetAttribute(name, out int id)) return id; - Log.Warn("Attempted to access attribute {0} on Program {1}", name, Id); + Logger.Warn("Attempted to access attribute {0} on Program {1}", name, Id); throw new ShaderException($"Shader Program {Id} does not contain id '{name}'"); } diff --git a/Diamond/Shaders/Shader.cs b/Diamond/Shaders/Shader.cs index 9a18954..7f7e97f 100644 --- a/Diamond/Shaders/Shader.cs +++ b/Diamond/Shaders/Shader.cs @@ -1,27 +1,16 @@ -using System.IO; +using System; +using System.Collections.Generic; +using System.IO; using System.Text; +using NLog; using OpenTK.Graphics.OpenGL4; namespace Diamond.Shaders { - /// - /// Wraps methods for GL Shader objects. - /// - public class Shader : GLObject + internal sealed class ShaderWrapper : GLWrapper { - /// - /// The type of this shader. - /// public readonly ShaderType ShaderType; - /// - /// The source file name, if it was loaded from a file. - /// - public string SourceFile { get; private set; } - - /// - /// Gets and sets the shader source with glShaderSource and glGetShaderSource. - /// public string Source { get @@ -30,103 +19,123 @@ namespace Diamond.Shaders GL.GetShaderSource(Id, sb.Capacity, out int length, sb); return sb.ToString(); } - set { GL.ShaderSource((int) Id, value); } + set { GL.ShaderSource(Id, value); } } - /// - /// Retrieves this shader's compilation log with glGetShaderInfoLog. - /// - public string InfoLog => GL.GetShaderInfoLog((int) Id).Trim(); - - /// - /// Checks the compilation status of this shader with glGetShader. - /// public bool Compiled { get { - GL.GetShader(Id, ShaderParameter.CompileStatus, out int success); - return success != 0; + GL.GetShader(Id, ShaderParameter.CompileStatus, out int res); + return res != 0; } } - /// - /// Creates a wrapper for a gl Shader object. - /// - /// The type of the shader to create - public Shader(ShaderType shaderType) - : base((uint) GL.CreateShader(shaderType)) + public string InfoLog => GL.GetShaderInfoLog(Id).Trim(); + + internal ShaderWrapper(ShaderType shaderType) { + Id = GL.CreateShader(shaderType); ShaderType = shaderType; } - protected override void Delete() => GL.DeleteShader(Id); + public override void GLDelete() + { + GL.DeleteShader(Id); + } - /// - /// Compile the shader. - /// - /// Compilation success - public bool Compile() + public void Compile() { GL.CompileShader(Id); - - var compiled = Compiled; - if (!compiled) - { - Log.Warn("Failed to compile {0} {1} {2}", ShaderType, Id, SourceFile); - Log.Debug("{0} {1} InfoLog\n{2}", ShaderType, Id, InfoLog); - } - - return compiled; - } - - /// - /// Creates and compiles a shader from a source file. Infers shader type from file extension - /// Extension must be of the form .[type] or .[type].glsl - /// Valid types are vs, vert, fs, and frag. - /// - /// Source file location - /// The compiled shader - public static Shader FromFile(string path) - { - var ex = Path.GetExtension(path); - - if (ex == ".glsl") - { - var name = Path.GetFileNameWithoutExtension(path); - if (Path.HasExtension(name)) - ex = Path.GetExtension(name); - } - - switch (ex) - { - case ".vs": - case ".vert": - return FromFile(path, ShaderType.VertexShader); - case ".fs": - case ".frag": - return FromFile(path, ShaderType.FragmentShader); - default: - throw new ShaderException("Can't infer shader type from extension"); - } - } - - /// - /// Creates and compiles a shader from a source file. - /// - /// Source file location - /// Type of the shader - /// The compiled shader - public static Shader FromFile(string path, ShaderType type) - { - var s = new Shader(type) - { - Source = File.ReadAllText(path), - SourceFile = path - }; - - s.Compile(); - return s; } } + + public class Shader : GLObject + { + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + + private readonly ShaderWrapper _shader; + + public override int Id => _shader.Id; + protected override GLWrapper Wrapper => _shader; + + public string Name { get; } + public string Source { get; } + + private Shader(ShaderWrapper shader, string source, string name) + { + _shader = shader; + Name = name; + Source = source; + } + + #region Factory Methods + + public static Shader FromSource(string source, ShaderType type, string name = "Shader") + { + var wrapper = new ShaderWrapper(type); + Logger.Debug("Created {0} \'{1}\' {2}", type, name, wrapper.Id); + + wrapper.Source = source; + wrapper.Compile(); + + if (!wrapper.Compiled) + { + Logger.Warn("Failed to compile {0} \'{1}\' {2}", type, name, wrapper.Id); + Logger.Debug("InfoLog for {0} \'{1}\' {2}", type, name, wrapper.Id); + wrapper.Dispose(); + return null; + } + + Logger.Debug("Successfully compiled {0} \'{1}\' {2}", type, name, wrapper.Id); + return new Shader(wrapper, source, name); + } + + public static Shader FromFile(string path, ShaderType type) + { + if (!File.Exists(path)) + { + Logger.Warn("Could not find glsl file {0}", path); + return null; + } + + var name = Path.GetFileNameWithoutExtension(path); + return FromSource(File.ReadAllText(path), type, name); + } + + public static Shader FromFile(string path) + { + if (!File.Exists(path)) + { + Logger.Warn("Could not find glsl file {0}", path); + return null; + } + + var ext = Path.GetExtension(path); + var name = Path.GetFileNameWithoutExtension(path); + + var extensions = new Dictionary + { + [".vs"] = ShaderType.VertexShader, + [".vert"] = ShaderType.VertexShader, + [".fs"] = ShaderType.VertexShader, + [".frag"] = ShaderType.VertexShader, + }; + + if (ext != null) + if (!extensions.ContainsKey(ext)) + ext = Path.GetExtension(name); + + if (ext == null || !extensions.ContainsKey(ext)) + { + Logger.Warn("Could not infer shader type from glsl file name {0}", path); + return null; + } + + var type = extensions[ext]; + return FromFile(path, type); + } + + #endregion + } } \ No newline at end of file diff --git a/Diamond/Textures/Texture.cs b/Diamond/Textures/Texture.cs index bdd69b4..fa1b757 100644 --- a/Diamond/Textures/Texture.cs +++ b/Diamond/Textures/Texture.cs @@ -8,7 +8,7 @@ namespace Diamond.Textures /// /// Wrapper class for gl Textures. /// - public class Texture : GLObject + public class Texture : GLWrapper { public TextureTarget Target;