From ee78f4262e6d3e0245be4959980c1b5c2f7e51a6 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Wed, 1 Mar 2017 21:54:03 -0500 Subject: [PATCH] Decoupled GLObject from Wrapper --- Diamond/Buffers/Buffer.cs | 33 ++++++++++++++++--------- Diamond/GLObject.cs | 44 ++------------------------------- Diamond/Shaders/Program.cs | 33 ++++++++++++++----------- Diamond/Shaders/Shader.cs | 19 +++++++++----- Diamond/Textures/Texture.cs | 19 +++++++++----- Diamond/Wrappers/BufferWrap.cs | 2 +- Diamond/Wrappers/ProgramWrap.cs | 2 +- Diamond/Wrappers/ShaderWrap.cs | 2 +- Diamond/Wrappers/TextureWrap.cs | 2 +- Diamond/Wrappers/Wrapper.cs | 4 +-- 10 files changed, 74 insertions(+), 86 deletions(-) diff --git a/Diamond/Buffers/Buffer.cs b/Diamond/Buffers/Buffer.cs index 1aa2ae9..b308dcc 100644 --- a/Diamond/Buffers/Buffer.cs +++ b/Diamond/Buffers/Buffer.cs @@ -14,16 +14,15 @@ namespace Diamond.Buffers /// The type of data used for this buffer public class Buffer : GLObject where T : struct { - private readonly BufferWrap _buffer; + internal readonly BufferWrap Wrapper; private readonly VertexDataInfo _vdi; - internal override Wrapper Wrapper => _buffer; private readonly int _size; /// /// The target for this buffer; its type /// - public BufferTarget Target => _buffer.Target; + public BufferTarget Target => Wrapper.Target; /// /// The usage hint for this buffer. Use StaticDraw for one-time uploads to @@ -31,13 +30,13 @@ namespace Diamond.Buffers /// public BufferUsageHint Usage { - get => _buffer.Usage; - set => _buffer.Usage = value; + get => Wrapper.Usage; + set => Wrapper.Usage = value; } - internal Buffer(BufferWrap buffer, string name) + internal Buffer(BufferWrap wrapper, string name) { - _buffer = buffer; + Wrapper = wrapper; Name = name; _size = Marshal.SizeOf(); _vdi = VertexDataInfo.GetInfo(); @@ -47,7 +46,7 @@ namespace Diamond.Buffers /// Upload data to this buffer /// /// The data to upload - public void Data(T[] data) => _buffer.Data(_size, data); + public void Data(T[] data) => Wrapper.Data(_size, data); /// /// Upload a range of data to this buffer @@ -55,7 +54,7 @@ namespace Diamond.Buffers /// The range offset /// The range length /// The data to upload, offset and length apply to both this and the target - public void Data(int offset, int count, T[] data) => _buffer.SubData(_size, offset, count, data); + public void Data(int offset, int count, T[] data) => Wrapper.SubData(_size, offset, count, data); /// /// Upload a range of data to this buffer @@ -77,7 +76,7 @@ namespace Diamond.Buffers throw exception; } - _buffer.Bind(); + Wrapper.Bind(); foreach (var attr in _vdi.Pointers) { var loc = program.AttributeLocation(attr.Name); @@ -87,8 +86,16 @@ namespace Diamond.Buffers } public override string ToString() => Name == null - ? $"Buffer<{typeof(T).Name}> {Target} ({Id})" - : $"Buffer<{typeof(T).Name}> {Target} {Name} ({Id})"; + ? $"Buffer<{typeof(T).Name}> {Wrapper}" + : $"Buffer<{typeof(T).Name}> {Wrapper} \'{Name}\'"; + + public override void Dispose() + { + Logger.Debug("Disposing {0}", this); + Wrapper.Dispose(); + } + + #region Factory Methods /// /// Create an empty buffer of this type @@ -123,6 +130,8 @@ namespace Diamond.Buffers return service; } + + #endregion } /// diff --git a/Diamond/GLObject.cs b/Diamond/GLObject.cs index d784304..e520130 100644 --- a/Diamond/GLObject.cs +++ b/Diamond/GLObject.cs @@ -20,48 +20,8 @@ namespace Diamond public string Name { get; protected set; } = "GLObject"; /// - /// Underlying managed wrapper to this OpenGL object + /// Delegate Dispose to underlying wrapper class /// - internal abstract Wrapper Wrapper { get; } - - /// - /// The OpenGL name of this object - /// - public int Id => Wrapper.Id; - - #region IDisposable - - protected virtual void Dispose(bool disposing) - { - if (_disposed) - return; - - if (disposing) - { - Logger.Debug("Disposing {0}", this); - Wrapper.Dispose(); - } - - _disposed = true; - } - - #region Implemented - - private bool _disposed; - - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - ~GLObject() - { - Dispose(false); - } - - #endregion - - #endregion + public abstract void Dispose(); } } \ No newline at end of file diff --git a/Diamond/Shaders/Program.cs b/Diamond/Shaders/Program.cs index 90aad0a..cec6a77 100644 --- a/Diamond/Shaders/Program.cs +++ b/Diamond/Shaders/Program.cs @@ -11,8 +11,7 @@ namespace Diamond.Shaders /// public class Program : GLObject { - private readonly ProgramWrap _program; - internal override Wrapper Wrapper => _program; + internal readonly ProgramWrap Wrapper; /// /// The currently active program. Manually invoking glUseProgram will break this. @@ -24,9 +23,9 @@ namespace Diamond.Shaders private readonly Dictionary _attributes = new Dictionary(); - internal Program(ProgramWrap program, string name) + internal Program(ProgramWrap wrapper, string name) { - _program = program; + Wrapper = wrapper; Name = name; } @@ -59,7 +58,7 @@ namespace Diamond.Shaders /// public void Use() { - GL.UseProgram(Id); + Wrapper.Use(); Current = this; } @@ -84,16 +83,16 @@ namespace Diamond.Shaders _uniforms.Clear(); _attributes.Clear(); - _program.Link(); + Wrapper.Link(); - if (!_program.Linked) + if (!Wrapper.Linked) return false; - for (var i = 0; i < _program.ActiveUniforms; i++) - _uniforms[_program.UniformName(i)] = i; + for (var i = 0; i < Wrapper.ActiveUniforms; i++) + _uniforms[Wrapper.UniformName(i)] = i; - for (var i = 0; i < _program.ActiveAttributes; i++) - _attributes[_program.AttributeName(i)] = i; + for (var i = 0; i < Wrapper.ActiveAttributes; i++) + _attributes[Wrapper.AttributeName(i)] = i; return true; } @@ -104,10 +103,16 @@ namespace Diamond.Shaders /// The shader to attach private void Attach(Shader shader) { - _program.Attach((ShaderWrap) shader.Wrapper); + Wrapper.Attach(shader.Wrapper); } - public override string ToString() => $"Program \'{Name}\' ({Id})"; + public override string ToString() => $"Program {Wrapper} \'{Name}\'"; + + public override void Dispose() + { + Logger.Debug("Disposing {0}", this); + Wrapper.Dispose(); + } #region Factory Methods @@ -157,7 +162,7 @@ namespace Diamond.Shaders { Logger.Warn("Failed to link {0}", service); Logger.Debug("InfoLog for {0}", service); - wrapper.Dispose(); + service.Dispose(); return null; } diff --git a/Diamond/Shaders/Shader.cs b/Diamond/Shaders/Shader.cs index 18d672a..d872b01 100644 --- a/Diamond/Shaders/Shader.cs +++ b/Diamond/Shaders/Shader.cs @@ -11,8 +11,7 @@ namespace Diamond.Shaders /// public class Shader : GLObject { - private readonly ShaderWrap _shader; - internal override Wrapper Wrapper => _shader; + internal readonly ShaderWrap Wrapper; /// /// The source used to create this shader @@ -24,15 +23,23 @@ namespace Diamond.Shaders /// public ShaderType Type { get; } - internal Shader(ShaderWrap shader, string source, ShaderType type, string name) + internal Shader(ShaderWrap wrapper, string source, ShaderType type, string name) { - _shader = shader; + Wrapper = wrapper; Source = source; Type = type; Name = name; } - public override string ToString() => $"{Type} \'{Name}\' ({Id})"; + public override string ToString() => Name == null + ? $"{Wrapper}" + : $"{Wrapper} \'{Name}\'"; + + public override void Dispose() + { + Logger.Debug("Disposing {0}", this); + Wrapper.Dispose(); + } #region Factory Methods @@ -68,7 +75,7 @@ namespace Diamond.Shaders { Logger.Warn("Failed to compile {0}", service); Logger.Debug("InfoLog for {0}", service); - wrapper.Dispose(); + service.Dispose(); return null; } diff --git a/Diamond/Textures/Texture.cs b/Diamond/Textures/Texture.cs index a5c9eb7..43ac59d 100644 --- a/Diamond/Textures/Texture.cs +++ b/Diamond/Textures/Texture.cs @@ -13,26 +13,33 @@ namespace Diamond.Textures /// public class Texture : GLObject { - private readonly TextureWrap _texture; - internal override Wrapper Wrapper => _texture; + internal readonly TextureWrap Wrapper; internal Texture(TextureWrap wrapper, string name) { - _texture = wrapper; + Wrapper = wrapper; Name = name; } /// /// This textures target; how it is used /// - public TextureTarget Target => _texture.Target; + public TextureTarget Target => Wrapper.Target; /// /// Bind this texture to a particular unit /// - public void Bind(int unit) => _texture.Bind(unit); + public void Bind(int unit) => Wrapper.Bind(unit); - public override string ToString() => Name == null ? $"{Target} ({Id})" : $"{Target} \'{Name}\' ({Id})"; + public override string ToString() => Name == null + ? $"{Wrapper}" + : $"{Wrapper} \'{Name}\'"; + + public override void Dispose() + { + Logger.Debug("Disposing {0}", this); + Wrapper.Dispose(); + } #region Factory Methods diff --git a/Diamond/Wrappers/BufferWrap.cs b/Diamond/Wrappers/BufferWrap.cs index 88e2f48..a508b0d 100644 --- a/Diamond/Wrappers/BufferWrap.cs +++ b/Diamond/Wrappers/BufferWrap.cs @@ -70,6 +70,6 @@ namespace Diamond.Wrappers #endregion - public override string ToString() => $"Buffer Wrapper - {Target} ({Id})"; + public override string ToString() => $"{Target} ({Id})"; } } \ No newline at end of file diff --git a/Diamond/Wrappers/ProgramWrap.cs b/Diamond/Wrappers/ProgramWrap.cs index ea4c64f..7e014bd 100644 --- a/Diamond/Wrappers/ProgramWrap.cs +++ b/Diamond/Wrappers/ProgramWrap.cs @@ -98,6 +98,6 @@ namespace Diamond.Wrappers #endregion - public override string ToString() => $"Program Wrapper - ({Id})"; + public override string ToString() => $"Program ({Id})"; } } \ No newline at end of file diff --git a/Diamond/Wrappers/ShaderWrap.cs b/Diamond/Wrappers/ShaderWrap.cs index 4b67e1c..f63c24f 100644 --- a/Diamond/Wrappers/ShaderWrap.cs +++ b/Diamond/Wrappers/ShaderWrap.cs @@ -81,6 +81,6 @@ namespace Diamond.Wrappers #endregion - public override string ToString() => $"Shader Wrapper - {ShaderType} ({Id})"; + public override string ToString() => $"{ShaderType} ({Id})"; } } \ No newline at end of file diff --git a/Diamond/Wrappers/TextureWrap.cs b/Diamond/Wrappers/TextureWrap.cs index 2304bd4..8423298 100644 --- a/Diamond/Wrappers/TextureWrap.cs +++ b/Diamond/Wrappers/TextureWrap.cs @@ -73,6 +73,6 @@ namespace Diamond.Wrappers #endregion - public override string ToString() => $"Texture Wrapper - {Target} ({Id})"; + public override string ToString() => $"{Target} ({Id})"; } } \ No newline at end of file diff --git a/Diamond/Wrappers/Wrapper.cs b/Diamond/Wrappers/Wrapper.cs index 8f34e96..347f56d 100644 --- a/Diamond/Wrappers/Wrapper.cs +++ b/Diamond/Wrappers/Wrapper.cs @@ -22,6 +22,8 @@ namespace Diamond.Wrappers Id = id; } + public override string ToString() => $"{GetType().Name} {Id}"; + #region IDisposable /// @@ -64,7 +66,5 @@ namespace Diamond.Wrappers #endregion #endregion - - public override string ToString() => $"{GetType().Name} {Id}"; } } \ No newline at end of file