using System; using SlatedGameToolkit.Framework.Exceptions; using SlatedGameToolkit.Framework.Graphics.Shaders; namespace SlatedGameToolkit.Framework.Graphics.Programs { public class GLShaderProgram : GLProgram { private readonly GLShader[] shaders; private readonly GLAttachShader attachShader; private readonly GLDetachShader detachShader; private GLBindAttribLocation bindAttribLocation; public GLShaderProgram(bool separable = true, params GLShader[] shaders) : base() { if (shaders.Length == 0) throw new ArgumentException("Requires at least one shader for shader program."); this.shaders = shaders; attachShader = GLFunctionUtils.RetrieveGLDelegate("glAttachShader"); bindAttribLocation = GLFunctionUtils.RetrieveGLDelegate("glBindAttribLocation"); detachShader = GLFunctionUtils.RetrieveGLDelegate("glDetachShader"); Use(); foreach (GLShader shader in shaders) { attachShader(handle, shader.Handle); } linkProgram(handle); uint length; string log; getProgramInfoLog(handle, 1024, out length, out log); if (length > 0) { Dispose(); throw new OpenGLException(log); } foreach (GLShader shader in shaders) { detachShader(handle, shader.Handle); } } /// /// Disposes of the shaders and this program. /// public override void Dispose() { foreach (GLShader shader in shaders) { shader.Dispose(); } base.Dispose(); } ~GLShaderProgram() { Dispose(); } } }