private async Task <WebGLProgram> InitProgramAsync(WebGLContext gl, string vsSource, string fsSource) { var vertexShader = await this.LoadShaderAsync(gl, ShaderType.VERTEX_SHADER, vsSource); var fragmentShader = await this.LoadShaderAsync(gl, ShaderType.FRAGMENT_SHADER, fsSource); var program = await gl.CreateProgramAsync(); await gl.AttachShaderAsync(program, vertexShader); await gl.AttachShaderAsync(program, fragmentShader); await gl.LinkProgramAsync(program); await gl.DeleteShaderAsync(vertexShader); await gl.DeleteShaderAsync(fragmentShader); if (!await gl.GetProgramParameterAsync <bool>(program, ProgramParameter.LINK_STATUS)) { string info = await gl.GetProgramInfoLogAsync(program); throw new Exception("An error occured while linking the program: " + info); } u_matrix_location = await _context.GetUniformLocationAsync(program, "u_matrix"); return(program); }
private async Task InitProgramAsync(WebGLContext gl, string vsSource, string fsSource) { var vertexShader = await LoadShaderAsync(gl, ShaderType.VERTEX_SHADER, vsSource); var fragmentShader = await LoadShaderAsync(gl, ShaderType.FRAGMENT_SHADER, fsSource); var program = await gl.CreateProgramAsync(); await gl.AttachShaderAsync(program, vertexShader); await gl.AttachShaderAsync(program, fragmentShader); await gl.LinkProgramAsync(program); await gl.DeleteShaderAsync(vertexShader); await gl.DeleteShaderAsync(fragmentShader); if (!await gl.GetProgramParameterAsync <bool>(program, ProgramParameter.LINK_STATUS)) { string info = await gl.GetProgramInfoLogAsync(program); throw new Exception("An error occured while linking the program: " + info); } Console.WriteLine($"shader program : id = {program.Id} , type = {program.WebGLType}"); ShaderProgram = program; }
public static async Task <ShaderProgram> InitShaderProgram(WebGLContext gl, string vsSource, string fsSource, List <string> attributesNames = null, List <string> uniformsNames = null) { var vertexShader = await LoadShaderAsync(gl, ShaderType.VERTEX_SHADER, vsSource); var fragmentShader = await LoadShaderAsync(gl, ShaderType.FRAGMENT_SHADER, fsSource); var program = await gl.CreateProgramAsync(); await gl.AttachShaderAsync(program, vertexShader); await gl.AttachShaderAsync(program, fragmentShader); await gl.LinkProgramAsync(program); await gl.DeleteShaderAsync(vertexShader); await gl.DeleteShaderAsync(fragmentShader); if (!await gl.GetProgramParameterAsync <bool>(program, ProgramParameter.LINK_STATUS)) { string info = await gl.GetProgramInfoLogAsync(program); throw new Exception("An error occured while linking the program: " + info); } attributesNames ??= new List <string>(); var attributesDict = new Dictionary <string, int>(); foreach (var attribute in attributesNames) { attributesDict.Add(attribute, await gl.GetAttribLocationAsync(program, attribute)); } uniformsNames ??= new List <string>(); var uniformsDict = new Dictionary <string, WebGLUniformLocation>(); foreach (var uniform in uniformsNames) { uniformsDict.Add(uniform, await gl.GetUniformLocationAsync(program, uniform)); } return(new ShaderProgram(program, gl, attributesDict, uniformsDict)); }
private async Task <WebGLShader> LoadShaderAsync(WebGLContext gl, ShaderType type, string source) { var shader = await gl.CreateShaderAsync(type); await gl.ShaderSourceAsync(shader, source); await gl.CompileShaderAsync(shader); if (!await gl.GetShaderParameterAsync <bool>(shader, ShaderParameter.COMPILE_STATUS)) { string info = await gl.GetShaderInfoLogAsync(shader); await gl.DeleteShaderAsync(shader); throw new Exception("An error occured while compiling the shader: " + info); } return(shader); }