From b64f2a030c15cb17c60205705f533e4af3d31c65 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Wed, 1 Mar 2017 03:02:48 -0500 Subject: [PATCH] Consistent structure to all Wrapper classes --- Diamond/Wrappers/BufferWrap.cs | 22 ++++++++++++++++++---- Diamond/Wrappers/ProgramWrap.cs | 27 +++++++++++++++++++++------ Diamond/Wrappers/ShaderWrap.cs | 22 ++++++++++++++++++++-- Diamond/Wrappers/TextureWrap.cs | 24 +++++++++++++++++++++--- Diamond/Wrappers/Wrapper.cs | 16 ++++++++++------ 5 files changed, 90 insertions(+), 21 deletions(-) diff --git a/Diamond/Wrappers/BufferWrap.cs b/Diamond/Wrappers/BufferWrap.cs index d7106e7..3621b36 100644 --- a/Diamond/Wrappers/BufferWrap.cs +++ b/Diamond/Wrappers/BufferWrap.cs @@ -3,10 +3,9 @@ using OpenTK.Graphics.OpenGL4; namespace Diamond.Wrappers { - internal class BufferWrap : Wrapper + internal sealed class BufferWrap : Wrapper { - public BufferTarget Target { get; } - public BufferUsageHint Usage { get; set; } + #region Constructor, Delete() internal BufferWrap(BufferTarget target, BufferUsageHint usage) { @@ -15,6 +14,19 @@ namespace Diamond.Wrappers Usage = usage; } + public override void Delete() => GL.DeleteBuffer(Id); + + #endregion + + #region Properties + + #region Stored + + public BufferTarget Target { get; } + public BufferUsageHint Usage { get; set; } + + #endregion + public void Bind() => GL.BindBuffer(Target, Id); public void Data(int size, T[] data) where T : struct @@ -29,6 +41,8 @@ namespace Diamond.Wrappers GL.BufferSubData(Target, (IntPtr) (offset * size), (IntPtr) (count * size), data); } - public override void GLDelete() => GL.DeleteBuffer(Id); + #endregion + + public override string ToString() => $"Buffer Wrapper - {Target} ({Id})"; } } \ No newline at end of file diff --git a/Diamond/Wrappers/ProgramWrap.cs b/Diamond/Wrappers/ProgramWrap.cs index b98bac0..884e94a 100644 --- a/Diamond/Wrappers/ProgramWrap.cs +++ b/Diamond/Wrappers/ProgramWrap.cs @@ -3,16 +3,20 @@ using OpenTK.Graphics.OpenGL4; namespace Diamond.Wrappers { - internal class ProgramWrap : Wrapper + internal sealed class ProgramWrap : Wrapper { + #region Constructor, Delete() + internal ProgramWrap() { Id = GL.CreateProgram(); } - public int ActiveUniforms => Get(GetProgramParameterName.ActiveUniforms); - public int ActiveAttributes => Get(GetProgramParameterName.ActiveAttributes); - public bool Linked => Get(GetProgramParameterName.LinkStatus) != 0; + public override void Delete() => GL.DeleteProgram(Id); + + #endregion + + #region Properties public int Get(GetProgramParameterName parameter) { @@ -20,11 +24,20 @@ namespace Diamond.Wrappers return res; } - public void Link() => GL.LinkProgram(Id); + public int ActiveUniforms => Get(GetProgramParameterName.ActiveUniforms); + public int ActiveAttributes => Get(GetProgramParameterName.ActiveAttributes); + public bool Linked => Get(GetProgramParameterName.LinkStatus) != 0; + public string InfoLog => GL.GetProgramInfoLog(Id).Trim(); + #endregion + + #region Methods + public void Attach(ShaderWrap shader) => GL.AttachShader(Id, shader.Id); + public void Link() => GL.LinkProgram(Id); + public void Use() => GL.UseProgram(Id); public string UniformName(int location) @@ -41,6 +54,8 @@ namespace Diamond.Wrappers return sb.ToString(); } - public override void GLDelete() => GL.DeleteProgram(Id); + #endregion + + public override string ToString() => $"Program Wrapper - ({Id})"; } } \ No newline at end of file diff --git a/Diamond/Wrappers/ShaderWrap.cs b/Diamond/Wrappers/ShaderWrap.cs index 7031a30..b65f470 100644 --- a/Diamond/Wrappers/ShaderWrap.cs +++ b/Diamond/Wrappers/ShaderWrap.cs @@ -5,13 +5,25 @@ namespace Diamond.Wrappers { internal sealed class ShaderWrap : Wrapper { + #region Constructor, Delete() + internal ShaderWrap(ShaderType shaderType) { Id = GL.CreateShader(shaderType); ShaderType = shaderType; } - public readonly ShaderType ShaderType; + public override void Delete() => GL.DeleteShader(Id); + + #endregion + + #region Properties + + #region Stored + + public ShaderType ShaderType { get; } + + #endregion public string Source { @@ -35,8 +47,14 @@ namespace Diamond.Wrappers public string InfoLog => GL.GetShaderInfoLog(Id); + #endregion + + #region Methods + public void Compile() => GL.CompileShader(Id); - public override void GLDelete() => GL.DeleteShader(Id); + #endregion + + public override string ToString() => $"Shader Wrapper - {ShaderType} ({Id})"; } } \ No newline at end of file diff --git a/Diamond/Wrappers/TextureWrap.cs b/Diamond/Wrappers/TextureWrap.cs index a78125b..1673ffa 100644 --- a/Diamond/Wrappers/TextureWrap.cs +++ b/Diamond/Wrappers/TextureWrap.cs @@ -3,15 +3,31 @@ using OpenTK.Graphics.OpenGL4; namespace Diamond.Wrappers { - internal class TextureWrap : Wrapper + internal sealed class TextureWrap : Wrapper { + #region Constructor, Delete() + internal TextureWrap(TextureTarget target) { Id = GL.GenTexture(); Target = target; } - public readonly TextureTarget Target; + public override void Delete() => GL.DeleteTexture(Id); + + #endregion + + #region Properties + + #region Stored + + public TextureTarget Target { get; } + + #endregion + + #endregion + + #region Methods public void Bind() => GL.BindTexture(Target, Id); @@ -28,6 +44,8 @@ namespace Diamond.Wrappers PixelType type, IntPtr pixels) => GL.TexImage2D(Target, 0, internalFormat, width, height, 0, format, type, pixels); - public override void GLDelete() => GL.DeleteTexture(Id); + #endregion + + public override string ToString() => $"Texture Wrapper - {Target} ({Id})"; } } \ No newline at end of file diff --git a/Diamond/Wrappers/Wrapper.cs b/Diamond/Wrappers/Wrapper.cs index 999b6a0..3f09ae5 100644 --- a/Diamond/Wrappers/Wrapper.cs +++ b/Diamond/Wrappers/Wrapper.cs @@ -10,15 +10,11 @@ namespace Diamond.Wrappers public int Id { get; protected set; } - public override string ToString() => $"{GetType().Name} {Id}"; - public static explicit operator int(Wrapper o) => o.Id; #region IDisposable - public abstract void GLDelete(); - - private bool _disposed; + public abstract void Delete(); protected virtual void Dispose(bool disposing) { @@ -30,13 +26,17 @@ namespace Diamond.Wrappers if (GraphicsContext.CurrentContext == null) Logger.Error("No graphics context, cannot delete {0}", this); else - GLDelete(); + Delete(); Id = 0; _disposed = true; } + #region Implemented + + private bool _disposed; + public void Dispose() { Dispose(true); @@ -49,5 +49,9 @@ namespace Diamond.Wrappers } #endregion + + #endregion + + public override string ToString() => $"{GetType().Name} {Id}"; } } \ No newline at end of file