/// <summary> /// Creates from strings. /// </summary> /// <param name="vertexShaderCode">The vertex shader code.</param> /// <param name="fragmentShaderCode">The fragment shader code.</param> /// <returns></returns> public static ShaderProgramGL CreateFromStrings(string vertexShaderCode, string fragmentShaderCode) { var vertexShader = CreateShader(ShaderType.VertexShader, vertexShaderCode); var fragmentShader = CreateShader(ShaderType.FragmentShader, fragmentShaderCode); var shaderProgram = new ShaderProgramGL(); shaderProgram.Attach(vertexShader); shaderProgram.Attach(fragmentShader); if (!shaderProgram.Link()) { var e = new ShaderLinkException(shaderProgram.Log); shaderProgram.Dispose(); throw e; } return(shaderProgram); }
private static void Update(ShaderProgramGL shaderProgram, IEnumerable <NamedStream> namedStreams, bool solutionMode, IContentLoader contentLoader) { string ShaderCode(Stream stream) { using (var reader = new StreamReader(stream, true)) { var code = reader.ReadToEnd(); if (solutionMode) { code = code.Replace("#ifdef SOLUTION", "#if 1"); } return(code); } } var count = namedStreams.Count(); if (2 > count) { return; } foreach (var res in namedStreams) { var shaderType = GetShaderTypeFromExtension(Path.GetExtension(res.Name)); var shaderCode = ShaderCode(res.Stream); string GetIncludeCode(string includeName) { var resourceName = includeName.Replace(Path.DirectorySeparatorChar, '.'); resourceName = resourceName.Replace(Path.AltDirectorySeparatorChar, '.'); var includeCode = contentLoader.Load <StringBuilder>(resourceName).ToString(); using (var includeShader = new ShaderGL(shaderType)) { if (!includeShader.Compile(includeCode)) { var e = includeShader.CreateException(includeCode); throw new NamedShaderException(contentLoader.Names.GetFullName(includeName), e); } } return(includeCode); } var expandedShaderCode = ShaderLoader.ResolveIncludes(shaderCode, GetIncludeCode); var shader = new ShaderGL(shaderType); if (shader.Compile(expandedShaderCode)) { shaderProgram.Attach(shader); } else { shaderProgram.RemoveShaders(); //clean out whole shader program to avoid double attachments next time var e = shader.CreateException(shaderCode); shader.Dispose(); throw new NamedShaderException(res.Name, e); } } if (!shaderProgram.Link()) { var e = new ShaderLinkException(shaderProgram.Log); var s = new StringBuilder(); foreach (var ns in namedStreams) { s.Append(ns.Name); s.Append(';'); } throw new NamedShaderException(s.ToString(), e); } }