Starting to use NLog for logging.
This commit is contained in:
@@ -34,6 +34,9 @@
|
||||
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NLog.4.4.3\lib\net45\NLog.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="OpenTK, Version=2.0.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\OpenTK.2.0.0\lib\net20\OpenTK.dll</HintPath>
|
||||
</Reference>
|
||||
@@ -61,6 +64,12 @@
|
||||
<Compile Include="Buffers\VertexPointerAttribute.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="NLog.config">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<None Include="NLog.xsd">
|
||||
<SubType>Designer</SubType>
|
||||
</None>
|
||||
<None Include="OpenTK.dll.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using OpenTK.Graphics;
|
||||
using NLog;
|
||||
|
||||
namespace Diamond
|
||||
{
|
||||
@@ -9,6 +10,11 @@ namespace Diamond
|
||||
/// </summary>
|
||||
public abstract class GLObject : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Logger for this class
|
||||
/// </summary>
|
||||
protected Logger Log { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The name of this object
|
||||
/// </summary>
|
||||
@@ -21,6 +27,9 @@ namespace Diamond
|
||||
protected GLObject(uint id)
|
||||
{
|
||||
Id = id;
|
||||
|
||||
Log = LogManager.GetLogger(GetType().FullName);
|
||||
Log.Trace("Created {0}", this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -35,10 +44,12 @@ namespace Diamond
|
||||
{
|
||||
if (GraphicsContext.CurrentContext == null)
|
||||
{
|
||||
Debug.WriteLine($"No current context, assuming {GetType().Name} {Id} is disposed.", "Warning");
|
||||
Log.Warn("No current context, assuming {0} is disposed.", this);
|
||||
return;
|
||||
}
|
||||
|
||||
Delete();
|
||||
Log.Trace("Disposed {0}", this);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
@@ -47,6 +58,8 @@ namespace Diamond
|
||||
Dispose();
|
||||
}
|
||||
|
||||
public override string ToString() => $"{GetType().Name} {Id}";
|
||||
|
||||
public static explicit operator uint(GLObject o) => o.Id;
|
||||
public static explicit operator int(GLObject o) => (int) o.Id;
|
||||
}
|
||||
|
||||
21
Diamond/NLog.config
Normal file
21
Diamond/NLog.config
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
|
||||
autoReload="true"
|
||||
throwExceptions="false"
|
||||
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
|
||||
|
||||
<variable name="Layout" value="[${level}] ${logger}: ${message}" />
|
||||
|
||||
<targets>
|
||||
<target xsi:type="File" name="file" fileName="${basedir}/Log/${shortdate}.log"
|
||||
layout="${Layout}"/>
|
||||
<target xsi:type="Debugger" name="debug" layout="${Layout}"/>
|
||||
</targets>
|
||||
|
||||
<rules>
|
||||
<logger name="*" minlevel="Info" writeTo="file"/>
|
||||
<logger name="*" minlevel="Debug" writeTo="debug"/>
|
||||
</rules>
|
||||
</nlog>
|
||||
2933
Diamond/NLog.xsd
Normal file
2933
Diamond/NLog.xsd
Normal file
File diff suppressed because it is too large
Load Diff
@@ -9,19 +9,25 @@ namespace Diamond.Shaders
|
||||
{
|
||||
public static Program Current { get; private set; }
|
||||
|
||||
private Dictionary<string, int> uniforms = new Dictionary<string, int>();
|
||||
private Dictionary<string, int> attributes = new Dictionary<string, int>();
|
||||
private readonly Dictionary<string, int> _uniforms = new Dictionary<string, int>();
|
||||
private readonly Dictionary<string, int> _attributes = new Dictionary<string, int>();
|
||||
|
||||
public string Log => GL.GetProgramInfoLog((int) Id).Trim();
|
||||
public string InfoLog => GL.GetProgramInfoLog((int) Id).Trim();
|
||||
|
||||
public Program()
|
||||
: base((uint) GL.CreateProgram())
|
||||
{
|
||||
}
|
||||
|
||||
protected override void Delete() => GL.DeleteProgram(Id);
|
||||
protected override void Delete()
|
||||
{
|
||||
GL.DeleteProgram(Id);
|
||||
}
|
||||
|
||||
public void Attach(Shader shader) => GL.AttachShader(Id, shader.Id);
|
||||
public void Attach(Shader shader)
|
||||
{
|
||||
GL.AttachShader(Id, shader.Id);
|
||||
}
|
||||
|
||||
public bool Linked
|
||||
{
|
||||
@@ -34,18 +40,26 @@ namespace Diamond.Shaders
|
||||
|
||||
public bool Link()
|
||||
{
|
||||
uniforms.Clear();
|
||||
attributes.Clear();
|
||||
_uniforms.Clear();
|
||||
_attributes.Clear();
|
||||
|
||||
GL.LinkProgram(Id);
|
||||
GL.GetProgram(Id, GetProgramParameterName.LinkStatus, out int success);
|
||||
if (success == 0) return false;
|
||||
|
||||
if (!Linked)
|
||||
{
|
||||
Log.Warn("Failed to link Program {0}", Id);
|
||||
Log.Debug("Program {0} InfoLog\n{1}", Id, InfoLog);
|
||||
return false;
|
||||
}
|
||||
|
||||
Log.Info("Successfully linked Program {0}", Id);
|
||||
|
||||
GL.GetProgram(Id, GetProgramParameterName.ActiveUniforms, out int uniformcount);
|
||||
for (var i = 0; i < uniformcount; i++)
|
||||
{
|
||||
var sb = new StringBuilder(256);
|
||||
GL.GetActiveUniformName((int) Id, i, sb.Capacity, out int length, sb);
|
||||
uniforms[sb.ToString()] = i;
|
||||
_uniforms[sb.ToString()] = i;
|
||||
}
|
||||
|
||||
GL.GetProgram(Id, GetProgramParameterName.ActiveAttributes, out int attributecount);
|
||||
@@ -54,7 +68,7 @@ namespace Diamond.Shaders
|
||||
var sb = new StringBuilder(256);
|
||||
GL.GetActiveAttrib((int) Id, i, sb.Capacity, out int length, out int size,
|
||||
out ActiveAttribType type, sb);
|
||||
attributes[sb.ToString()] = i;
|
||||
_attributes[sb.ToString()] = i;
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -62,26 +76,28 @@ namespace Diamond.Shaders
|
||||
|
||||
public bool TryGetUniform(string name, out int id)
|
||||
{
|
||||
return uniforms.TryGetValue(name, out id);
|
||||
return _uniforms.TryGetValue(name, out id);
|
||||
}
|
||||
|
||||
public int GetUniform(string name)
|
||||
{
|
||||
if (!TryGetUniform(name, out int id))
|
||||
throw new ShaderException($"Shader Program {Id} does not contain uniform '{name}'");
|
||||
return id;
|
||||
if (TryGetUniform(name, out int id)) return id;
|
||||
|
||||
Log.Warn("Attempted to access uniform {0} on Program {1}", name, Id);
|
||||
throw new ShaderException($"Shader Program {Id} does not contain uniform '{name}'");
|
||||
}
|
||||
|
||||
public bool TryGetAttribute(string name, out int id)
|
||||
{
|
||||
return attributes.TryGetValue(name, out id);
|
||||
return _attributes.TryGetValue(name, out id);
|
||||
}
|
||||
|
||||
public int GetAttribute(string name)
|
||||
{
|
||||
if (!TryGetAttribute(name, out int id))
|
||||
throw new ShaderException($"Shader Program {Id} does not contain id '{name}'");
|
||||
return id;
|
||||
if (TryGetAttribute(name, out int id)) return id;
|
||||
|
||||
Log.Warn("Attempted to access attribute {0} on Program {1}", name, Id);
|
||||
throw new ShaderException($"Shader Program {Id} does not contain id '{name}'");
|
||||
}
|
||||
|
||||
public void SetAttribPointers<T>(GLBuffer<T> buff) where T : struct
|
||||
@@ -92,10 +108,11 @@ namespace Diamond.Shaders
|
||||
foreach (var attr in vdi.Pointers)
|
||||
{
|
||||
if (!TryGetAttribute(attr.Name, out int loc)) continue;
|
||||
|
||||
GL.VertexAttribPointer(loc, attr.Size, attr.Type, attr.Normalized, vdi.Stride, attr.Offset);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Use()
|
||||
{
|
||||
GL.UseProgram(Id);
|
||||
@@ -111,11 +128,29 @@ namespace Diamond.Shaders
|
||||
public static Program FromShaders(params Shader[] shaders)
|
||||
{
|
||||
var p = new Program();
|
||||
|
||||
foreach (var shader in shaders)
|
||||
{
|
||||
p.Attach(shader);
|
||||
}
|
||||
|
||||
p.Link();
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
public static Program FromFiles(params string[] files)
|
||||
{
|
||||
var shaders = new Shader[files.Length];
|
||||
|
||||
for (var i = 0; i < files.Length; i++)
|
||||
shaders[i] = Shader.FromFile(files[i]);
|
||||
|
||||
var p = FromShaders(shaders);
|
||||
|
||||
for (var i = 0; i < shaders.Length; i++)
|
||||
shaders[i]?.Dispose();
|
||||
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace Diamond.Shaders
|
||||
/// <summary>
|
||||
/// The type of this shader.
|
||||
/// </summary>
|
||||
public readonly ShaderType Type;
|
||||
public readonly ShaderType ShaderType;
|
||||
|
||||
/// <summary>
|
||||
/// The source file name, if it was loaded from a file.
|
||||
@@ -36,7 +36,7 @@ namespace Diamond.Shaders
|
||||
/// <summary>
|
||||
/// Retrieves this shader's compilation log with <code>glGetShaderInfoLog</code>.
|
||||
/// </summary>
|
||||
public string Log => GL.GetShaderInfoLog((int) Id).Trim();
|
||||
public string InfoLog => GL.GetShaderInfoLog((int) Id).Trim();
|
||||
|
||||
/// <summary>
|
||||
/// Checks the compilation status of this shader with <code>glGetShader</code>.
|
||||
@@ -53,11 +53,11 @@ namespace Diamond.Shaders
|
||||
/// <summary>
|
||||
/// Creates a wrapper for a gl Shader object.
|
||||
/// </summary>
|
||||
/// <param name="type">The type of the shader to create</param>
|
||||
public Shader(ShaderType type)
|
||||
: base((uint) GL.CreateShader(type))
|
||||
/// <param name="shaderType">The type of the shader to create</param>
|
||||
public Shader(ShaderType shaderType)
|
||||
: base((uint) GL.CreateShader(shaderType))
|
||||
{
|
||||
Type = type;
|
||||
ShaderType = shaderType;
|
||||
}
|
||||
|
||||
protected override void Delete() => GL.DeleteShader(Id);
|
||||
@@ -69,7 +69,46 @@ namespace Diamond.Shaders
|
||||
public bool Compile()
|
||||
{
|
||||
GL.CompileShader(Id);
|
||||
return Compiled;
|
||||
|
||||
var compiled = Compiled;
|
||||
if (!compiled)
|
||||
{
|
||||
Log.Warn("Failed to compile {0} {1} {2}", ShaderType, Id, SourceFile);
|
||||
Log.Debug("{0} {1} InfoLog\n{2}", ShaderType, Id, InfoLog);
|
||||
}
|
||||
|
||||
return compiled;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates and compiles a shader from a source file. Infers shader type from file extension
|
||||
/// Extension must be of the form .[type] or .[type].glsl
|
||||
/// Valid types are vs, vert, fs, and frag.
|
||||
/// </summary>
|
||||
/// <param name="path">Source file location</param>
|
||||
/// <returns>The compiled shader</returns>
|
||||
public static Shader FromFile(string path)
|
||||
{
|
||||
var ex = Path.GetExtension(path);
|
||||
|
||||
if (ex == ".glsl")
|
||||
{
|
||||
var name = Path.GetFileNameWithoutExtension(path);
|
||||
if (Path.HasExtension(name))
|
||||
ex = Path.GetExtension(name);
|
||||
}
|
||||
|
||||
switch (ex)
|
||||
{
|
||||
case ".vs":
|
||||
case ".vert":
|
||||
return FromFile(path, ShaderType.VertexShader);
|
||||
case ".fs":
|
||||
case ".frag":
|
||||
return FromFile(path, ShaderType.FragmentShader);
|
||||
default:
|
||||
throw new ShaderException("Can't infer shader type from extension");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -79,25 +118,14 @@ namespace Diamond.Shaders
|
||||
/// <param name="type">Type of the shader</param>
|
||||
/// <returns>The compiled shader</returns>
|
||||
public static Shader FromFile(string path, ShaderType type)
|
||||
{
|
||||
return FromFile(path, type, out bool success);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates and compiles a shader from a source file.
|
||||
/// </summary>
|
||||
/// <param name="path">Source file location</param>
|
||||
/// <param name="type">Type of the shader</param>
|
||||
/// <param name="success">Compilation success</param>
|
||||
/// <returns>The compiled shader</returns>
|
||||
public static Shader FromFile(string path, ShaderType type, out bool success)
|
||||
{
|
||||
var s = new Shader(type)
|
||||
{
|
||||
Source = File.ReadAllText(path),
|
||||
SourceFile = ""
|
||||
SourceFile = path
|
||||
};
|
||||
success = s.Compile();
|
||||
|
||||
s.Compile();
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
|
||||
<package id="NLog" version="4.4.3" targetFramework="net461" />
|
||||
<package id="NLog.Config" version="4.4.3" targetFramework="net461" />
|
||||
<package id="NLog.Schema" version="4.4.3" targetFramework="net461" />
|
||||
<package id="OpenTK" version="2.0.0" targetFramework="net452" />
|
||||
</packages>
|
||||
@@ -83,49 +83,8 @@ namespace hexworld
|
||||
{
|
||||
base.OnLoad(e);
|
||||
|
||||
using (var vs = Shader.FromFile(@"res\s.vs.glsl", ShaderType.VertexShader))
|
||||
using (var fs = Shader.FromFile(@"res\s.fs.glsl", ShaderType.FragmentShader))
|
||||
{
|
||||
if (!vs.Compiled | !fs.Compiled)
|
||||
{
|
||||
Debug.WriteLine("Failed to compile shaders:");
|
||||
Debug.WriteLineIf(!vs.Compiled, $"Vertex Log ({vs.Id}):\n{vs.Log}");
|
||||
Debug.WriteLineIf(!fs.Compiled, $"Fragment Log ({fs.Id}):\n{fs.Log}");
|
||||
Exit();
|
||||
return;
|
||||
}
|
||||
|
||||
_jsonPgm = Program.FromShaders(vs, fs);
|
||||
|
||||
if (!_jsonPgm.Link())
|
||||
{
|
||||
Debug.WriteLine($"Failed to link program ({_jsonPgm.Id}):\n{_jsonPgm.Log}");
|
||||
Exit();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
using (var vs = Shader.FromFile(@"res\obj.vs.glsl", ShaderType.VertexShader))
|
||||
using (var fs = Shader.FromFile(@"res\obj.fs.glsl", ShaderType.FragmentShader))
|
||||
{
|
||||
if (!vs.Compiled | !fs.Compiled)
|
||||
{
|
||||
Debug.WriteLine("Failed to compile shaders:");
|
||||
Debug.WriteLineIf(!vs.Compiled, $"Vertex Log ({vs.Id}):\n{vs.Log}");
|
||||
Debug.WriteLineIf(!fs.Compiled, $"Fragment Log ({fs.Id}):\n{fs.Log}");
|
||||
Exit();
|
||||
return;
|
||||
}
|
||||
|
||||
_objPgm = Program.FromShaders(vs, fs);
|
||||
|
||||
if (!_objPgm.Link())
|
||||
{
|
||||
Debug.WriteLine($"Failed to link program ({_objPgm.Id}):\n{_jsonPgm.Log}");
|
||||
Exit();
|
||||
return;
|
||||
}
|
||||
}
|
||||
_jsonPgm = Program.FromFiles(@"res\s.vs.glsl", @"res\s.fs.glsl");
|
||||
_objPgm = Program.FromFiles(@"res\obj.vs.glsl", @"res\obj.fs.glsl");
|
||||
|
||||
_cubeMesh = Mesh.FromJson<Vertex>(@"res\data_vert_cubes.json");
|
||||
_panelMesh = Mesh.FromJson<Vertex>(@"res\data_vert_panels.json");
|
||||
@@ -141,7 +100,6 @@ namespace hexworld
|
||||
_tableTiles = new SubArray<Tile>(
|
||||
JsonConvert.DeserializeObject<Tile[]>(File.ReadAllText(@"res\data_tile_table.json")));
|
||||
|
||||
|
||||
_allTiles = SubArray.Join(_stoneTiles, _grassTiles, _grayTiles, _tableTiles);
|
||||
_tileBuffer = new GLBuffer<Tile>(BufferTarget.ArrayBuffer, BufferUsageHint.DynamicDraw);
|
||||
_tileBuffer.Data(_allTiles);
|
||||
@@ -196,42 +154,52 @@ namespace hexworld
|
||||
GL.DepthFunc(DepthFunction.Lequal);
|
||||
GL.Enable(EnableCap.Blend);
|
||||
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
|
||||
// GL.Enable(EnableCap.CullFace);
|
||||
// GL.CullFace(CullFaceMode.Back);
|
||||
GL.Enable(EnableCap.CullFace);
|
||||
GL.CullFace(CullFaceMode.Back);
|
||||
|
||||
_jsonPgm.Use();
|
||||
if (_jsonPgm.Linked)
|
||||
{
|
||||
_jsonPgm.Use();
|
||||
|
||||
_jsonPgm.SetAttribPointers(_tileBuffer);
|
||||
_jsonPgm.SetAttribPointers(_vertexBuffer);
|
||||
_jsonPgm.SetAttribPointers(_tileBuffer);
|
||||
_jsonPgm.SetAttribPointers(_vertexBuffer);
|
||||
|
||||
_grass.Bind(0);
|
||||
_stone.Bind(1);
|
||||
_gray.Bind(2);
|
||||
_grass.Bind(0);
|
||||
_stone.Bind(1);
|
||||
_gray.Bind(2);
|
||||
|
||||
GL.Uniform1(_jsonPgm.GetUniform("tex"), 0);
|
||||
GL.UniformMatrix4(_jsonPgm.GetUniform("view"), false, ref _view);
|
||||
GL.UniformMatrix4(_jsonPgm.GetUniform("proj"), false, ref _proj);
|
||||
GL.Uniform1(_jsonPgm.GetUniform("tex"), 0);
|
||||
GL.UniformMatrix4(_jsonPgm.GetUniform("view"), false, ref _view);
|
||||
GL.UniformMatrix4(_jsonPgm.GetUniform("proj"), false, ref _proj);
|
||||
|
||||
_cubeMesh.DrawInstanced(_grassTiles);
|
||||
_cubeMesh.DrawInstanced(_grassTiles);
|
||||
|
||||
GL.Uniform1(_jsonPgm.GetUniform("tex"), 1);
|
||||
GL.Uniform1(_jsonPgm.GetUniform("tex"), 1);
|
||||
|
||||
_panelMesh.DrawInstanced(_stoneTiles);
|
||||
_panelMesh.DrawInstanced(_stoneTiles);
|
||||
|
||||
GL.Uniform1(_jsonPgm.GetUniform("tex"), 2);
|
||||
GL.Uniform1(_jsonPgm.GetUniform("tex"), 2);
|
||||
|
||||
_sidesMesh.DrawInstanced(_grayTiles);
|
||||
_sidesMesh.DrawInstanced(_grayTiles);
|
||||
}
|
||||
|
||||
_objPgm.Use();
|
||||
if (_objPgm.Linked)
|
||||
{
|
||||
_objPgm.Use();
|
||||
|
||||
_objPgm.SetAttribPointers(_tileBuffer);
|
||||
_objPgm.SetAttribPointers(_objBuffer);
|
||||
_grass.Bind(0);
|
||||
_stone.Bind(1);
|
||||
_gray.Bind(2);
|
||||
|
||||
GL.Uniform1(_jsonPgm.GetUniform("tex"), 2);
|
||||
GL.UniformMatrix4(_jsonPgm.GetUniform("view"), false, ref _view);
|
||||
GL.UniformMatrix4(_jsonPgm.GetUniform("proj"), false, ref _proj);
|
||||
_objPgm.SetAttribPointers(_tileBuffer);
|
||||
_objPgm.SetAttribPointers(_objBuffer);
|
||||
|
||||
_objMesh.DrawInstanced(_tableTiles);
|
||||
GL.Uniform1(_objPgm.GetUniform("tex"), 2);
|
||||
GL.UniformMatrix4(_objPgm.GetUniform("view"), false, ref _view);
|
||||
GL.UniformMatrix4(_objPgm.GetUniform("proj"), false, ref _proj);
|
||||
|
||||
_objMesh.DrawInstanced(_tableTiles);
|
||||
}
|
||||
|
||||
SwapBuffers();
|
||||
}
|
||||
|
||||
@@ -5,10 +5,9 @@ in float light;
|
||||
|
||||
uniform sampler2D tex;
|
||||
|
||||
void main ()
|
||||
void main ()l
|
||||
{
|
||||
vec4 color = texture(tex, vcoord);
|
||||
color.w = 1;
|
||||
color.xyz *= light;
|
||||
gl_FragColor = clamp(color, 0, 1);
|
||||
}
|
||||
Reference in New Issue
Block a user