Wrapper and GLObject each have one logger instance - all globjects and wrappers use that logger

This commit is contained in:
2017-03-01 15:25:50 -05:00
parent b64f2a030c
commit e4081ea662
12 changed files with 70 additions and 56 deletions

View File

@@ -42,8 +42,8 @@ namespace Diamond.Buffers
{ {
if (_vdi == null) if (_vdi == null)
{ {
var exception = new InvalidOperationException($"Cannot use type {typeof(T)} to create a VertexBuffer"); var exception = new InvalidOperationException($"Cannot use type {typeof(T)} to create a Vertex Buffer");
GLBuffer.Logger.Error(exception); Logger.Error(exception);
throw exception; throw exception;
} }
@@ -56,15 +56,11 @@ namespace Diamond.Buffers
} }
} }
public override string ToString() => Name == null ? $"{Target} ({Id})" : $"{Target} {Name} ({Id})"; public override string ToString() => Name == null
} ? $"Buffer<{typeof(T).Name}> {Target} ({Id})"
: $"Buffer<{typeof(T).Name}> {Target} {Name} ({Id})";
public static class GLBuffer internal static Buffer<T> Empty(BufferTarget target, BufferUsageHint usage, string name)
{
internal static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public static Buffer<T> Empty<T>(BufferTarget target, BufferUsageHint usage = BufferUsageHint.StaticDraw,
string name = null) where T : struct
{ {
var wrapper = new BufferWrap(target, usage); var wrapper = new BufferWrap(target, usage);
var service = new Buffer<T>(wrapper, name); var service = new Buffer<T>(wrapper, name);
@@ -74,15 +70,23 @@ namespace Diamond.Buffers
return service; return service;
} }
public static Buffer<T> FromData<T>(T[] data, BufferTarget target, internal static Buffer<T> FromData(T[] data, BufferTarget target, BufferUsageHint usage, string name = null)
BufferUsageHint usage = BufferUsageHint.StaticDraw,
string name = null) where T : struct
{ {
var service = Empty<T>(target, usage, name); var service = Empty(target, usage, name);
service?.Data(data); service?.Data(data);
return service; return service;
} }
} }
public static class Buffer
{
public static Buffer<T> Empty<T>(BufferTarget target, BufferUsageHint usage = BufferUsageHint.StaticDraw,
string name = null) where T : struct => Buffer<T>.Empty(target, usage, name);
public static Buffer<T> FromData<T>(T[] data, BufferTarget target,
BufferUsageHint usage = BufferUsageHint.StaticDraw,
string name = null) where T : struct => Buffer<T>.FromData(data, target, usage, name);
}
} }

View File

@@ -6,7 +6,7 @@ namespace Diamond
{ {
public abstract class GLObject : IDisposable public abstract class GLObject : IDisposable
{ {
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); protected static readonly Logger Logger = LogManager.GetLogger("GLObject");
private bool _disposed; private bool _disposed;
@@ -22,6 +22,7 @@ namespace Diamond
if (disposing) if (disposing)
{ {
Logger.Debug("Disposing {0}", this);
Wrapper.Dispose(); Wrapper.Dispose();
} }

View File

@@ -2173,7 +2173,7 @@
<xs:extension base="Target"> <xs:extension base="Target">
<xs:choice minOccurs="0" maxOccurs="unbounded"> <xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="name" minOccurs="0" maxOccurs="1" shaderType="xs:string" /> <xs:element name="name" minOccurs="0" maxOccurs="1" shaderType="xs:string" />
<xs:element name="autoCreate" minOccurs="0" maxOccurs="1" shaderType="xs:boolean" /> <xs:element name="autoGLCreate" minOccurs="0" maxOccurs="1" shaderType="xs:boolean" />
<xs:element name="categoryName" minOccurs="0" maxOccurs="1" shaderType="xs:string" /> <xs:element name="categoryName" minOccurs="0" maxOccurs="1" shaderType="xs:string" />
<xs:element name="counterHelp" minOccurs="0" maxOccurs="1" shaderType="xs:string" /> <xs:element name="counterHelp" minOccurs="0" maxOccurs="1" shaderType="xs:string" />
<xs:element name="counterName" minOccurs="0" maxOccurs="1" shaderType="xs:string" /> <xs:element name="counterName" minOccurs="0" maxOccurs="1" shaderType="xs:string" />
@@ -2187,7 +2187,7 @@
<xs:documentation>Name of the target.</xs:documentation> <xs:documentation>Name of the target.</xs:documentation>
</xs:annotation> </xs:annotation>
</xs:attribute> </xs:attribute>
<xs:attribute name="autoCreate" shaderType="xs:boolean"> <xs:attribute name="autoGLCreate" shaderType="xs:boolean">
<xs:annotation> <xs:annotation>
<xs:documentation>Indicates whether performance counter should be automatically created.</xs:documentation> <xs:documentation>Indicates whether performance counter should be automatically created.</xs:documentation>
</xs:annotation> </xs:annotation>

View File

@@ -8,14 +8,11 @@ namespace Diamond.Shaders
{ {
public class Program : GLObject public class Program : GLObject
{ {
internal static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private readonly ProgramWrap _program;
internal ProgramWrap _program;
internal override Wrapper Wrapper => _program; internal override Wrapper Wrapper => _program;
public static Program Current { get; private set; } public static Program Current { get; private set; }
private readonly List<Shader> _shaders = new List<Shader>();
private readonly Dictionary<string, int> _uniforms = new Dictionary<string, int>(); private readonly Dictionary<string, int> _uniforms = new Dictionary<string, int>();
private readonly Dictionary<string, int> _attributes = new Dictionary<string, int>(); private readonly Dictionary<string, int> _attributes = new Dictionary<string, int>();
@@ -73,7 +70,6 @@ namespace Diamond.Shaders
private void Attach(Shader shader) private void Attach(Shader shader)
{ {
_shaders.Add(shader);
_program.Attach((ShaderWrap) shader.Wrapper); _program.Attach((ShaderWrap) shader.Wrapper);
} }
@@ -81,9 +77,6 @@ namespace Diamond.Shaders
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{ {
foreach (var shader in _shaders)
shader.Dispose();
base.Dispose(disposing); base.Dispose(disposing);
} }
@@ -143,7 +136,17 @@ namespace Diamond.Shaders
return FromShaders(name, shaderList); return FromShaders(name, shaderList);
} }
public static Program FromFiles(params string[] paths) => FromShaders(paths.Select(Shader.FromFile)); public static Program FromFiles(params string[] paths)
{
var shaders = paths.Select(Shader.FromFile).ToList();
var program = FromShaders(shaders);
foreach (var shader in shaders)
shader.Dispose();
return program;
}
#endregion #endregion
} }

View File

@@ -8,8 +8,6 @@ namespace Diamond.Shaders
{ {
public class Shader : GLObject public class Shader : GLObject
{ {
internal static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly ShaderWrap _shader; private readonly ShaderWrap _shader;
internal override Wrapper Wrapper => _shader; internal override Wrapper Wrapper => _shader;
@@ -82,7 +80,6 @@ namespace Diamond.Shaders
var ext = Path.GetExtension(path); var ext = Path.GetExtension(path);
var name = Path.GetFileNameWithoutExtension(path); var name = Path.GetFileNameWithoutExtension(path);
if (ext != null) if (ext != null)
if (!Extensions.ContainsKey(ext)) if (!Extensions.ContainsKey(ext))
ext = Path.GetExtension(name); ext = Path.GetExtension(name);
@@ -94,6 +91,7 @@ namespace Diamond.Shaders
} }
var type = Extensions[ext]; var type = Extensions[ext];
return FromFile(path, type); return FromFile(path, type);
} }

View File

@@ -9,8 +9,6 @@ namespace Diamond.Textures
{ {
public class Texture : GLObject public class Texture : GLObject
{ {
internal static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly TextureWrap _texture; private readonly TextureWrap _texture;
internal override Wrapper Wrapper => _texture; internal override Wrapper Wrapper => _texture;
@@ -27,29 +25,33 @@ namespace Diamond.Textures
public override string ToString() => Name == null ? $"{Target} ({Id})" : $"{Target} \'{Name}\' ({Id})"; public override string ToString() => Name == null ? $"{Target} ({Id})" : $"{Target} \'{Name}\' ({Id})";
#region Factory Methods
public static Texture FromBitmap(Bitmap bmp, string name = null) public static Texture FromBitmap(Bitmap bmp, string name = null)
{ {
var wrapper= new TextureWrap(TextureTarget.Texture2D); var wrapper = new TextureWrap(TextureTarget.Texture2D);
var service = new Texture(wrapper, null); var service = new Texture(wrapper, null);
Logger.Debug("Created texture {0}", service); Logger.Debug("Created Texture {0}", service);
wrapper.Bind(); wrapper.Bind();
wrapper.TexParameter(TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); wrapper.TexParameter(TextureParameterName.TextureMinFilter, (int) TextureMinFilter.Nearest);
wrapper.TexParameter(TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); wrapper.TexParameter(TextureParameterName.TextureMagFilter, (int) TextureMagFilter.Nearest);
var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Drawing.Imaging.PixelFormat.Format32bppArgb);
wrapper.Image2D(PixelInternalFormat.Rgba, bmp.Width,bmp.Height,PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); wrapper.Image2D(PixelInternalFormat.Rgba, bmp.Width, bmp.Height, PixelFormat.Bgra, PixelType.UnsignedByte,
data.Scan0);
bmp.UnlockBits(data); bmp.UnlockBits(data);
return service; return service;
} }
public static Texture FromFile(string path) public static Texture FromFile(string path)
{ {
return FromBitmap(new Bitmap(path)); return FromBitmap(new Bitmap(path));
} }
#endregion
} }
} }

View File

@@ -5,16 +5,16 @@ namespace Diamond.Wrappers
{ {
internal sealed class BufferWrap : Wrapper internal sealed class BufferWrap : Wrapper
{ {
#region Constructor, Delete() #region Constructor, GLDelete()
internal BufferWrap(BufferTarget target, BufferUsageHint usage) internal BufferWrap(BufferTarget target, BufferUsageHint usage)
: base(GL.GenBuffer())
{ {
Id = GL.GenBuffer();
Target = target; Target = target;
Usage = usage; Usage = usage;
} }
public override void Delete() => GL.DeleteBuffer(Id); protected override void GLDelete() => GL.DeleteBuffer(Id);
#endregion #endregion

View File

@@ -5,14 +5,14 @@ namespace Diamond.Wrappers
{ {
internal sealed class ProgramWrap : Wrapper internal sealed class ProgramWrap : Wrapper
{ {
#region Constructor, Delete() #region Constructor, GLDelete()
internal ProgramWrap() internal ProgramWrap()
: base(GL.CreateProgram())
{ {
Id = GL.CreateProgram();
} }
public override void Delete() => GL.DeleteProgram(Id); protected override void GLDelete() => GL.DeleteProgram(Id);
#endregion #endregion

View File

@@ -5,15 +5,15 @@ namespace Diamond.Wrappers
{ {
internal sealed class ShaderWrap : Wrapper internal sealed class ShaderWrap : Wrapper
{ {
#region Constructor, Delete() #region Constructor, GLDelete()
internal ShaderWrap(ShaderType shaderType) internal ShaderWrap(ShaderType shaderType)
: base(GL.CreateShader(shaderType))
{ {
Id = GL.CreateShader(shaderType);
ShaderType = shaderType; ShaderType = shaderType;
} }
public override void Delete() => GL.DeleteShader(Id); protected override void GLDelete() => GL.DeleteShader(Id);
#endregion #endregion

View File

@@ -5,15 +5,15 @@ namespace Diamond.Wrappers
{ {
internal sealed class TextureWrap : Wrapper internal sealed class TextureWrap : Wrapper
{ {
#region Constructor, Delete() #region Constructor, GLDelete()
internal TextureWrap(TextureTarget target) internal TextureWrap(TextureTarget target)
: base(GL.GenTexture())
{ {
Id = GL.GenTexture();
Target = target; Target = target;
} }
public override void Delete() => GL.DeleteTexture(Id); protected override void GLDelete() => GL.DeleteTexture(Id);
#endregion #endregion

View File

@@ -6,15 +6,20 @@ namespace Diamond.Wrappers
{ {
internal abstract class Wrapper : IDisposable internal abstract class Wrapper : IDisposable
{ {
private static readonly Logger Logger = LogManager.GetLogger("OpenGL Wrapper"); protected static readonly Logger Logger = LogManager.GetLogger("Wrapper");
public int Id { get; protected set; } public int Id { get; private set; }
public static explicit operator int(Wrapper o) => o.Id; public static explicit operator int(Wrapper o) => o.Id;
protected Wrapper(int id)
{
Id = id;
}
#region IDisposable #region IDisposable
public abstract void Delete(); protected abstract void GLDelete();
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
{ {
@@ -26,7 +31,7 @@ namespace Diamond.Wrappers
if (GraphicsContext.CurrentContext == null) if (GraphicsContext.CurrentContext == null)
Logger.Error("No graphics context, cannot delete {0}", this); Logger.Error("No graphics context, cannot delete {0}", this);
else else
Delete(); GLDelete();
Id = 0; Id = 0;

View File

@@ -10,6 +10,7 @@ using Newtonsoft.Json.Linq;
using OpenTK; using OpenTK;
using OpenTK.Graphics; using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4; using OpenTK.Graphics.OpenGL4;
using Buffer = Diamond.Buffers.Buffer;
namespace hexworld namespace hexworld
{ {
@@ -96,9 +97,9 @@ namespace hexworld
_cubeMesh = cubeMesh; _cubeMesh = cubeMesh;
_tileBuffer = GLBuffer.FromData(SubArray.Join(_doorTiles, _floorTiles), BufferTarget.ArrayBuffer, _tileBuffer = Buffer.FromData(SubArray.Join(_doorTiles, _floorTiles), BufferTarget.ArrayBuffer,
BufferUsageHint.DynamicDraw, "tile"); BufferUsageHint.DynamicDraw, "tile");
_meshBuffer = GLBuffer.FromData(cubeMesh.Vertices.ToArray(), BufferTarget.ArrayBuffer, _meshBuffer = Buffer.FromData(cubeMesh.Vertices.ToArray(), BufferTarget.ArrayBuffer,
BufferUsageHint.StaticDraw, "mesh"); BufferUsageHint.StaticDraw, "mesh");
} }