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 virtual async Task <GLShader> CreateFromResourceAsync(string resourceName) { ResetObject(); var shader = await _context.CreateShaderAsync(_shaderType); await _context.ShaderSourceAsync(shader, await ResourceLoader.LoadAsTextAsync(resourceName)); await _context.CompileShaderAsync(shader); if (!await _context.GetShaderParameterAsync <bool>(shader, ShaderParameter.COMPILE_STATUS)) { LastError = await _context.GetShaderInfoLogAsync(shader); LastState = LastStateType.Failed; return(this); } await _context.AttachShaderAsync(_program, shader); LastState = LastStateType.Success; GLObjectValue = shader; return(this); }
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)); }