Convert int? *location to bool has* and int *location

This commit is contained in:
2017-03-05 00:31:31 -05:00
parent 77b28b4ce0
commit 90b7545758
4 changed files with 43 additions and 29 deletions

View File

@@ -78,9 +78,9 @@ namespace Diamond.Buffers
Wrapper.Bind(); Wrapper.Bind();
foreach (var attr in _vdi.Pointers) foreach (var attr in _vdi.Pointers)
{ {
var loc = program.AttributeLocation(attr.Name); if (program.HasAttribute(attr.Name))
if (loc.HasValue) GL.VertexAttribPointer(program.AttributeLocation(attr.Name), attr.Size, attr.Type, attr.Normalized,
GL.VertexAttribPointer((int) loc, attr.Size, attr.Type, attr.Normalized, _vdi.Stride, attr.Offset); _vdi.Stride, attr.Offset);
} }
} }

View File

@@ -51,11 +51,11 @@ namespace Diamond.Buffers
foreach (var attr in Pointers) foreach (var attr in Pointers)
{ {
var loc = Program.Current.AttributeLocation(attr.Name); if (!Program.Current.HasAttribute(attr.Name))
if (!loc.HasValue)
continue; continue;
GL.EnableVertexAttribArray((int) loc); var loc = Program.Current.AttributeLocation(attr.Name);
GL.VertexAttribDivisor((int) loc, Divisor); GL.EnableVertexAttribArray(loc);
GL.VertexAttribDivisor(loc, Divisor);
} }
} }
@@ -69,10 +69,10 @@ namespace Diamond.Buffers
foreach (var attr in Pointers) foreach (var attr in Pointers)
{ {
var loc = Program.Current.AttributeLocation(attr.Name); if (!Program.Current.HasAttribute(attr.Name))
if (!loc.HasValue)
continue; continue;
GL.DisableVertexAttribArray((int) loc); var loc = Program.Current.AttributeLocation(attr.Name);
GL.DisableVertexAttribArray(loc);
} }
} }

View File

@@ -45,16 +45,12 @@ namespace Diamond.Render
Texture.Bind(0); Texture.Bind(0);
var texLoc = Program.UniformLocation("tex"); if (Program.HasUniform("tex"))
var viewLoc = Program.UniformLocation("view"); GL.Uniform1(Program.UniformLocation("tex"), 0);
var projLoc = Program.UniformLocation("proj"); if (Program.HasUniform("view"))
GL.UniformMatrix4(Program.UniformLocation("view"), false, ref Camera.View);
if (texLoc.HasValue) if (Program.HasUniform("proj"))
GL.Uniform1(texLoc.Value, 0); GL.UniformMatrix4(Program.UniformLocation("proj"), false, ref Camera.Projection);
if (viewLoc.HasValue)
GL.UniformMatrix4(viewLoc.Value, false, ref Camera.View);
if (projLoc.HasValue)
GL.UniformMatrix4(projLoc.Value, false, ref Camera.Projection);
if (Instance != null) if (Instance != null)
Vertices.DrawInstanced(Instance); Vertices.DrawInstanced(Instance);

View File

@@ -28,28 +28,46 @@ namespace Diamond.Shaders
Name = name; Name = name;
} }
// todo change these to not use int? - possibly use TryGet, or return negative value if not present /// <summary>
/// Check if the program has a uniform
/// </summary>
/// <param name="name">The name of the uniform</param>
/// <returns>Whether the program has this uniform</returns>
public bool HasUniform(string name)
{
return _uniforms.ContainsKey(name);
}
/// <summary> /// <summary>
/// Get the location of a uniform /// Get the location of a uniform
/// </summary> /// </summary>
/// <param name="name">The name of the uniform</param> /// <param name="name">The name of the uniform</param>
/// <returns>The location, or no value if uniform not present</returns> /// <returns>The location of the uniform</returns>
public int? UniformLocation(string name) public int UniformLocation(string name)
{ {
if (_uniforms.ContainsKey(name)) return _uniforms[name]; if (HasUniform(name)) return _uniforms[name];
return null; throw new KeyNotFoundException($"Shader {this} does not contain uniform {name}");
}
/// <summary>
/// Check if the program has an attribute
/// </summary>
/// <param name="name">The name of the attribute</param>
/// <returns>Whether the program has this attribute</returns>
public bool HasAttribute(string name)
{
return _attributes.ContainsKey(name);
} }
/// <summary> /// <summary>
/// Get the location of an attribute /// Get the location of an attribute
/// </summary> /// </summary>
/// <param name="name">The name of the attribute</param> /// <param name="name">The name of the attribute</param>
/// <returns>The location, or no value if attribute not present</returns> /// <returns>The location of the attribute</returns>
public int? AttributeLocation(string name) public int AttributeLocation(string name)
{ {
if (_attributes.ContainsKey(name)) return _attributes[name]; if (HasAttribute(name)) return _attributes[name];
return null; throw new KeyNotFoundException($"Shader {this} does not contain attribute {name}");
} }
/// <summary> /// <summary>