internal ShaderProgram(GraphicsLibrary gl, string vertexShaderSource, string fragmentShaderSource) { program = gl.CreateProgram(); var vertexShader = CompileShader(gl, ShaderType.Vertex, vertexShaderSource); var fragmentShader = CompileShader(gl, ShaderType.Fragment, fragmentShaderSource); LinkProgram(gl); gl.DeleteShader(vertexShader); gl.DeleteShader(fragmentShader); }
private unsafe uint CompileShader(GraphicsLibrary gl, ShaderType type, string source) { var shader = gl.CreateShader(type); var header = GetShaderHeader(gl); var byteCount = Encoding.UTF8.GetMaxByteCount(header.Length); byteCount += Encoding.UTF8.GetMaxByteCount(source.Length); if (byteCount + 1 > byteBuffer.Length) { byteBuffer = new byte[byteCount + 1]; } byteCount = Encoding.UTF8.GetBytes(header, 0, header.Length, byteBuffer, 0); byteCount += Encoding.UTF8.GetBytes(source, 0, source.Length, byteBuffer, byteCount); byteBuffer[byteCount] = 0; fixed(byte *p = byteBuffer) { byte *t = p; gl.ShaderSource(shader, 1, &t, null); } gl.CompileShader(shader); int len; gl.GetShaderiv(shader, ShaderParameter.InfoLogLength, &len); if (len > 1) { var buffer = new byte[len]; fixed(byte *p = buffer) { gl.GetShaderInfoLog(shader, len, &len, p); Console.WriteLine("Shader Compiler message: " + Marshal.PtrToStringAnsi(new IntPtr(p))); } } int cs; gl.GetShaderiv(shader, ShaderParameter.CompileStatus, &cs); if (cs == 0) { gl.DeleteShader(shader); throw new InvalidOperationException("Shader compilation error"); } gl.AttachShader(program, shader); return(shader); }