diff --git a/Diamond/Buffers/Buffer.cs b/Diamond/Buffers/Buffer.cs index 28142c2..6e8af2e 100644 --- a/Diamond/Buffers/Buffer.cs +++ b/Diamond/Buffers/Buffer.cs @@ -78,9 +78,9 @@ namespace Diamond.Buffers Wrapper.Bind(); foreach (var attr in _vdi.Pointers) { - var loc = program.AttributeLocation(attr.Name); - if (loc.HasValue) - GL.VertexAttribPointer((int) loc, attr.Size, attr.Type, attr.Normalized, _vdi.Stride, attr.Offset); + if (program.HasAttribute(attr.Name)) + GL.VertexAttribPointer(program.AttributeLocation(attr.Name), attr.Size, attr.Type, attr.Normalized, + _vdi.Stride, attr.Offset); } } diff --git a/Diamond/Buffers/VertexDataInfo.cs b/Diamond/Buffers/VertexDataInfo.cs index 8e7fd7d..0989f83 100644 --- a/Diamond/Buffers/VertexDataInfo.cs +++ b/Diamond/Buffers/VertexDataInfo.cs @@ -51,11 +51,11 @@ namespace Diamond.Buffers foreach (var attr in Pointers) { - var loc = Program.Current.AttributeLocation(attr.Name); - if (!loc.HasValue) + if (!Program.Current.HasAttribute(attr.Name)) continue; - GL.EnableVertexAttribArray((int) loc); - GL.VertexAttribDivisor((int) loc, Divisor); + var loc = Program.Current.AttributeLocation(attr.Name); + GL.EnableVertexAttribArray(loc); + GL.VertexAttribDivisor(loc, Divisor); } } @@ -69,10 +69,10 @@ namespace Diamond.Buffers foreach (var attr in Pointers) { - var loc = Program.Current.AttributeLocation(attr.Name); - if (!loc.HasValue) + if (!Program.Current.HasAttribute(attr.Name)) continue; - GL.DisableVertexAttribArray((int) loc); + var loc = Program.Current.AttributeLocation(attr.Name); + GL.DisableVertexAttribArray(loc); } } diff --git a/Diamond/Render/RenderGroup.cs b/Diamond/Render/RenderGroup.cs index 4a40e26..8a3e928 100644 --- a/Diamond/Render/RenderGroup.cs +++ b/Diamond/Render/RenderGroup.cs @@ -45,16 +45,12 @@ namespace Diamond.Render Texture.Bind(0); - var texLoc = Program.UniformLocation("tex"); - var viewLoc = Program.UniformLocation("view"); - var projLoc = Program.UniformLocation("proj"); - - if (texLoc.HasValue) - GL.Uniform1(texLoc.Value, 0); - if (viewLoc.HasValue) - GL.UniformMatrix4(viewLoc.Value, false, ref Camera.View); - if (projLoc.HasValue) - GL.UniformMatrix4(projLoc.Value, false, ref Camera.Projection); + if (Program.HasUniform("tex")) + GL.Uniform1(Program.UniformLocation("tex"), 0); + if (Program.HasUniform("view")) + GL.UniformMatrix4(Program.UniformLocation("view"), false, ref Camera.View); + if (Program.HasUniform("proj")) + GL.UniformMatrix4(Program.UniformLocation("proj"), false, ref Camera.Projection); if (Instance != null) Vertices.DrawInstanced(Instance); diff --git a/Diamond/Shaders/Program.cs b/Diamond/Shaders/Program.cs index 86f990e..0095e35 100644 --- a/Diamond/Shaders/Program.cs +++ b/Diamond/Shaders/Program.cs @@ -28,28 +28,46 @@ namespace Diamond.Shaders Name = name; } - // todo change these to not use int? - possibly use TryGet, or return negative value if not present + /// + /// Check if the program has a uniform + /// + /// The name of the uniform + /// Whether the program has this uniform + public bool HasUniform(string name) + { + return _uniforms.ContainsKey(name); + } /// /// Get the location of a uniform /// /// The name of the uniform - /// The location, or no value if uniform not present - public int? UniformLocation(string name) + /// The location of the uniform + public int UniformLocation(string name) { - if (_uniforms.ContainsKey(name)) return _uniforms[name]; - return null; + if (HasUniform(name)) return _uniforms[name]; + throw new KeyNotFoundException($"Shader {this} does not contain uniform {name}"); + } + + /// + /// Check if the program has an attribute + /// + /// The name of the attribute + /// Whether the program has this attribute + public bool HasAttribute(string name) + { + return _attributes.ContainsKey(name); } /// /// Get the location of an attribute /// /// The name of the attribute - /// The location, or no value if attribute not present - public int? AttributeLocation(string name) + /// The location of the attribute + public int AttributeLocation(string name) { - if (_attributes.ContainsKey(name)) return _attributes[name]; - return null; + if (HasAttribute(name)) return _attributes[name]; + throw new KeyNotFoundException($"Shader {this} does not contain attribute {name}"); } ///