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>
public class Buffer<T> : 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;
/// <summary>
/// The target for this buffer; its type
/// </summary>
public BufferTarget Target => _buffer.Target;
public BufferTarget Target => Wrapper.Target;
/// <summary>
/// The usage hint for this buffer. Use StaticDraw for one-time uploads to
@@ -31,13 +30,13 @@ namespace Diamond.Buffers
/// </summary>
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<T>();
_vdi = VertexDataInfo.GetInfo<T>();
@@ -47,7 +46,7 @@ namespace Diamond.Buffers
/// Upload data to this buffer
/// </summary>
/// <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>
/// Upload a range of data to this buffer
@@ -55,7 +54,7 @@ namespace Diamond.Buffers
/// <param name="offset">The range offset</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>
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>
/// 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
/// <summary>
/// Create an empty buffer of this type
@@ -123,6 +130,8 @@ namespace Diamond.Buffers
return service;
}
#endregion
}
/// <summary>

View File

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

View File

@@ -11,8 +11,7 @@ namespace Diamond.Shaders
/// </summary>
public class Program : GLObject
{
private readonly ProgramWrap _program;
internal override Wrapper Wrapper => _program;
internal readonly ProgramWrap Wrapper;
/// <summary>
/// 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>();
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
/// </summary>
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
/// <param name="shader">The shader to attach</param>
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;
}

View File

@@ -11,8 +11,7 @@ namespace Diamond.Shaders
/// </summary>
public class Shader : GLObject
{
private readonly ShaderWrap _shader;
internal override Wrapper Wrapper => _shader;
internal readonly ShaderWrap Wrapper;
/// <summary>
/// The source used to create this shader
@@ -24,15 +23,23 @@ namespace Diamond.Shaders
/// </summary>
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;
}

View File

@@ -13,26 +13,33 @@ namespace Diamond.Textures
/// </summary>
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;
}
/// <summary>
/// This textures target; how it is used
/// </summary>
public TextureTarget Target => _texture.Target;
public TextureTarget Target => Wrapper.Target;
/// <summary>
/// Bind this texture to a particular unit
/// </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

View File

@@ -70,6 +70,6 @@ namespace Diamond.Wrappers
#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
public override string ToString() => $"Program Wrapper - ({Id})";
public override string ToString() => $"Program ({Id})";
}
}

View File

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