Fully migrated GLObject to factory pattern - some bug fixes still needed

This commit is contained in:
2017-03-01 02:01:12 -05:00
parent aa6b4d4b68
commit b8e184154f
4 changed files with 22 additions and 16 deletions

View File

@@ -26,10 +26,7 @@ namespace Diamond
if (disposing) if (disposing)
{ {
if (GraphicsContext.CurrentContext == null) Wrapper.Dispose();
Logger.Error("No graphics context, cannot delete {0}", this);
else
Wrapper.Dispose();
} }
_disposed = true; _disposed = true;

View File

@@ -7,6 +7,8 @@ namespace Diamond
{ {
internal abstract class GLWrapper : IDisposable internal abstract class GLWrapper : IDisposable
{ {
private static Logger Logger = LogManager.GetCurrentClassLogger();
public int Id { get; protected set; } public int Id { get; protected set; }
public override string ToString() => $"{GetType().Name} {Id}"; public override string ToString() => $"{GetType().Name} {Id}";
@@ -26,7 +28,10 @@ namespace Diamond
// no managed resources to dispose // no managed resources to dispose
GLDelete(); if (GraphicsContext.CurrentContext == null)
Logger.Error("No graphics context, cannot delete {0}", this);
else
GLDelete();
Id = 0; Id = 0;

View File

@@ -58,6 +58,7 @@ namespace Diamond.Shaders
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>();
@@ -113,6 +114,12 @@ namespace Diamond.Shaders
return true; return true;
} }
private void Attach(Shader shader)
{
_shaders.Add(shader);
_program.Attach((ShaderWrapper) shader.Wrapper);
}
public override string ToString() => $"Program \'{Name}\' ({Id})"; public override string ToString() => $"Program \'{Name}\' ({Id})";
#region Factory Methods #region Factory Methods
@@ -142,7 +149,7 @@ namespace Diamond.Shaders
return null; return null;
} }
wrapper.Attach((ShaderWrapper) shader.Wrapper); service.Attach(shader);
} }
service.Link(); service.Link();
@@ -171,9 +178,6 @@ namespace Diamond.Shaders
return FromShaders(name, shaderList); return FromShaders(name, shaderList);
} }
public static Program FromFiles(string name, params string[] paths) => FromShaders(name,
paths.Select(Shader.FromFile));
public static Program FromFiles(params string[] paths) => FromShaders(paths.Select(Shader.FromFile)); public static Program FromFiles(params string[] paths) => FromShaders(paths.Select(Shader.FromFile));
#endregion #endregion

View File

@@ -37,7 +37,7 @@ namespace Diamond.Shaders
} }
} }
public string InfoLog => GL.GetShaderInfoLog(Id).Trim(); public string InfoLog => GL.GetShaderInfoLog(Id);
public void Compile() => GL.CompileShader(Id); public void Compile() => GL.CompileShader(Id);
@@ -66,12 +66,12 @@ namespace Diamond.Shaders
#region Factory Methods #region Factory Methods
private static readonly Dictionary<string, ShaderType> _extensions = new Dictionary<string, ShaderType> private static readonly Dictionary<string, ShaderType> Extensions = new Dictionary<string, ShaderType>
{ {
[".vs"] = ShaderType.VertexShader, [".vs"] = ShaderType.VertexShader,
[".vert"] = ShaderType.VertexShader, [".vert"] = ShaderType.VertexShader,
[".fs"] = ShaderType.VertexShader, [".fs"] = ShaderType.FragmentShader,
[".frag"] = ShaderType.VertexShader, [".frag"] = ShaderType.FragmentShader,
}; };
public static Shader FromSource(string source, ShaderType type, string name = "Shader") public static Shader FromSource(string source, ShaderType type, string name = "Shader")
@@ -122,16 +122,16 @@ namespace Diamond.Shaders
if (ext != null) if (ext != null)
if (!_extensions.ContainsKey(ext)) if (!Extensions.ContainsKey(ext))
ext = Path.GetExtension(name); ext = Path.GetExtension(name);
if (ext == null || !_extensions.ContainsKey(ext)) if (ext == null || !Extensions.ContainsKey(ext))
{ {
Logger.Warn("Could not infer shader type from glsl file name {0}", path); Logger.Warn("Could not infer shader type from glsl file name {0}", path);
return null; return null;
} }
var type = _extensions[ext]; var type = Extensions[ext];
return FromFile(path, type); return FromFile(path, type);
} }