Decoupled GLObject from Wrapper

This commit is contained in:
2017-03-01 21:54:03 -05:00
parent 33415d28b8
commit ee78f4262e
10 changed files with 74 additions and 86 deletions

View File

@@ -14,16 +14,15 @@ namespace Diamond.Buffers
/// <typeparam name="T">The type of data used for this buffer</typeparam> /// <typeparam name="T">The type of data used for this buffer</typeparam>
public class Buffer<T> : GLObject where T : struct public class Buffer<T> : GLObject where T : struct
{ {
private readonly BufferWrap _buffer; internal readonly BufferWrap Wrapper;
private readonly VertexDataInfo _vdi; private readonly VertexDataInfo _vdi;
internal override Wrapper Wrapper => _buffer;
private readonly int _size; private readonly int _size;
/// <summary> /// <summary>
/// The target for this buffer; its type /// The target for this buffer; its type
/// </summary> /// </summary>
public BufferTarget Target => _buffer.Target; public BufferTarget Target => Wrapper.Target;
/// <summary> /// <summary>
/// The usage hint for this buffer. Use StaticDraw for one-time uploads to /// The usage hint for this buffer. Use StaticDraw for one-time uploads to
@@ -31,13 +30,13 @@ namespace Diamond.Buffers
/// </summary> /// </summary>
public BufferUsageHint Usage public BufferUsageHint Usage
{ {
get => _buffer.Usage; get => Wrapper.Usage;
set => _buffer.Usage = value; set => Wrapper.Usage = value;
} }
internal Buffer(BufferWrap buffer, string name) internal Buffer(BufferWrap wrapper, string name)
{ {
_buffer = buffer; Wrapper = wrapper;
Name = name; Name = name;
_size = Marshal.SizeOf<T>(); _size = Marshal.SizeOf<T>();
_vdi = VertexDataInfo.GetInfo<T>(); _vdi = VertexDataInfo.GetInfo<T>();
@@ -47,7 +46,7 @@ namespace Diamond.Buffers
/// Upload data to this buffer /// Upload data to this buffer
/// </summary> /// </summary>
/// <param name="data">The data to upload</param> /// <param name="data">The data to upload</param>
public void Data(T[] data) => _buffer.Data(_size, data); public void Data(T[] data) => Wrapper.Data(_size, data);
/// <summary> /// <summary>
/// Upload a range of data to this buffer /// Upload a range of data to this buffer
@@ -55,7 +54,7 @@ namespace Diamond.Buffers
/// <param name="offset">The range offset</param> /// <param name="offset">The range offset</param>
/// <param name="count">The range length</param> /// <param name="count">The range length</param>
/// <param name="data">The data to upload, offset and length apply to both this and the target</param> /// <param name="data">The data to upload, offset and length apply to both this and the target</param>
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);
/// <summary> /// <summary>
/// Upload a range of data to this buffer /// Upload a range of data to this buffer
@@ -77,7 +76,7 @@ namespace Diamond.Buffers
throw exception; throw exception;
} }
_buffer.Bind(); Wrapper.Bind();
foreach (var attr in _vdi.Pointers) foreach (var attr in _vdi.Pointers)
{ {
var loc = program.AttributeLocation(attr.Name); var loc = program.AttributeLocation(attr.Name);
@@ -87,8 +86,16 @@ namespace Diamond.Buffers
} }
public override string ToString() => Name == null public override string ToString() => Name == null
? $"Buffer<{typeof(T).Name}> {Target} ({Id})" ? $"Buffer<{typeof(T).Name}> {Wrapper}"
: $"Buffer<{typeof(T).Name}> {Target} {Name} ({Id})"; : $"Buffer<{typeof(T).Name}> {Wrapper} \'{Name}\'";
public override void Dispose()
{
Logger.Debug("Disposing {0}", this);
Wrapper.Dispose();
}
#region Factory Methods
/// <summary> /// <summary>
/// Create an empty buffer of this type /// Create an empty buffer of this type
@@ -123,6 +130,8 @@ namespace Diamond.Buffers
return service; return service;
} }
#endregion
} }
/// <summary> /// <summary>

View File

@@ -20,48 +20,8 @@ namespace Diamond
public string Name { get; protected set; } = "GLObject"; public string Name { get; protected set; } = "GLObject";
/// <summary> /// <summary>
/// Underlying managed wrapper to this OpenGL object /// Delegate Dispose to underlying wrapper class
/// </summary> /// </summary>
internal abstract Wrapper Wrapper { get; } public abstract void Dispose();
/// <summary>
/// The OpenGL name of this object
/// </summary>
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
} }
} }

View File

@@ -11,8 +11,7 @@ namespace Diamond.Shaders
/// </summary> /// </summary>
public class Program : GLObject public class Program : GLObject
{ {
private readonly ProgramWrap _program; internal readonly ProgramWrap Wrapper;
internal override Wrapper Wrapper => _program;
/// <summary> /// <summary>
/// The currently active program. Manually invoking glUseProgram will break this. /// The currently active program. Manually invoking glUseProgram will break this.
@@ -24,9 +23,9 @@ namespace Diamond.Shaders
private readonly Dictionary<string, int> _attributes = new Dictionary<string, int>(); private readonly Dictionary<string, int> _attributes = new Dictionary<string, int>();
internal Program(ProgramWrap program, string name) internal Program(ProgramWrap wrapper, string name)
{ {
_program = program; Wrapper = wrapper;
Name = name; Name = name;
} }
@@ -59,7 +58,7 @@ namespace Diamond.Shaders
/// </summary> /// </summary>
public void Use() public void Use()
{ {
GL.UseProgram(Id); Wrapper.Use();
Current = this; Current = this;
} }
@@ -84,16 +83,16 @@ namespace Diamond.Shaders
_uniforms.Clear(); _uniforms.Clear();
_attributes.Clear(); _attributes.Clear();
_program.Link(); Wrapper.Link();
if (!_program.Linked) if (!Wrapper.Linked)
return false; return false;
for (var i = 0; i < _program.ActiveUniforms; i++) for (var i = 0; i < Wrapper.ActiveUniforms; i++)
_uniforms[_program.UniformName(i)] = i; _uniforms[Wrapper.UniformName(i)] = i;
for (var i = 0; i < _program.ActiveAttributes; i++) for (var i = 0; i < Wrapper.ActiveAttributes; i++)
_attributes[_program.AttributeName(i)] = i; _attributes[Wrapper.AttributeName(i)] = i;
return true; return true;
} }
@@ -104,10 +103,16 @@ namespace Diamond.Shaders
/// <param name="shader">The shader to attach</param> /// <param name="shader">The shader to attach</param>
private void Attach(Shader shader) 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 #region Factory Methods
@@ -157,7 +162,7 @@ namespace Diamond.Shaders
{ {
Logger.Warn("Failed to link {0}", service); Logger.Warn("Failed to link {0}", service);
Logger.Debug("InfoLog for {0}", service); Logger.Debug("InfoLog for {0}", service);
wrapper.Dispose(); service.Dispose();
return null; return null;
} }

View File

@@ -11,8 +11,7 @@ namespace Diamond.Shaders
/// </summary> /// </summary>
public class Shader : GLObject public class Shader : GLObject
{ {
private readonly ShaderWrap _shader; internal readonly ShaderWrap Wrapper;
internal override Wrapper Wrapper => _shader;
/// <summary> /// <summary>
/// The source used to create this shader /// The source used to create this shader
@@ -24,15 +23,23 @@ namespace Diamond.Shaders
/// </summary> /// </summary>
public ShaderType Type { get; } 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; Source = source;
Type = type; Type = type;
Name = name; 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 #region Factory Methods
@@ -68,7 +75,7 @@ namespace Diamond.Shaders
{ {
Logger.Warn("Failed to compile {0}", service); Logger.Warn("Failed to compile {0}", service);
Logger.Debug("InfoLog for {0}", service); Logger.Debug("InfoLog for {0}", service);
wrapper.Dispose(); service.Dispose();
return null; return null;
} }

View File

@@ -13,26 +13,33 @@ namespace Diamond.Textures
/// </summary> /// </summary>
public class Texture : GLObject public class Texture : GLObject
{ {
private readonly TextureWrap _texture; internal readonly TextureWrap Wrapper;
internal override Wrapper Wrapper => _texture;
internal Texture(TextureWrap wrapper, string name) internal Texture(TextureWrap wrapper, string name)
{ {
_texture = wrapper; Wrapper = wrapper;
Name = name; Name = name;
} }
/// <summary> /// <summary>
/// This textures target; how it is used /// This textures target; how it is used
/// </summary> /// </summary>
public TextureTarget Target => _texture.Target; public TextureTarget Target => Wrapper.Target;
/// <summary> /// <summary>
/// Bind this texture to a particular unit /// Bind this texture to a particular unit
/// </summary> /// </summary>
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 #region Factory Methods

View File

@@ -70,6 +70,6 @@ namespace Diamond.Wrappers
#endregion #endregion
public override string ToString() => $"Buffer Wrapper - {Target} ({Id})"; public override string ToString() => $"{Target} ({Id})";
} }
} }

View File

@@ -98,6 +98,6 @@ namespace Diamond.Wrappers
#endregion #endregion
public override string ToString() => $"Program Wrapper - ({Id})"; public override string ToString() => $"Program ({Id})";
} }
} }

View File

@@ -81,6 +81,6 @@ namespace Diamond.Wrappers
#endregion #endregion
public override string ToString() => $"Shader Wrapper - {ShaderType} ({Id})"; public override string ToString() => $"{ShaderType} ({Id})";
} }
} }

View File

@@ -73,6 +73,6 @@ namespace Diamond.Wrappers
#endregion #endregion
public override string ToString() => $"Texture Wrapper - {Target} ({Id})"; public override string ToString() => $"{Target} ({Id})";
} }
} }

View File

@@ -22,6 +22,8 @@ namespace Diamond.Wrappers
Id = id; Id = id;
} }
public override string ToString() => $"{GetType().Name} {Id}";
#region IDisposable #region IDisposable
/// <summary> /// <summary>
@@ -64,7 +66,5 @@ namespace Diamond.Wrappers
#endregion #endregion
#endregion #endregion
public override string ToString() => $"{GetType().Name} {Id}";
} }
} }