func newProgram(vertexShaderSource, fragmentShaderSource string) (uint32, error) { vertexShader, err := compileShader(vertexShaderSource, gl.VERTEX_SHADER) if err != nil { return 0, err } fragmentShader, err := compileShader(fragmentShaderSource, gl.FRAGMENT_SHADER) if err != nil { return 0, err } program := gl.CreateProgram() gl.AttachShader(program, vertexShader) gl.AttachShader(program, fragmentShader) gl.LinkProgram(program) var status int32 gl.GetProgramiv(program, gl.LINK_STATUS, &status) if status == gl.FALSE { var logLength int32 gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength) log := strings.Repeat("\x00", int(logLength+1)) gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log)) return 0, fmt.Errorf("failed to link program: %v", log) } gl.DeleteShader(vertexShader) gl.DeleteShader(fragmentShader) return program, nil }
func LoadShaders(shaders []ShaderInfo) (uint32, error) { program := gl.CreateProgram() for _, shaderInfo := range shaders { shader, err := shaderInfo.CompileShader() if err != nil { return 0, err } gl.AttachShader(program, shader) gl.DeleteShader(shader) } gl.LinkProgram(program) var status int32 gl.GetProgramiv(program, gl.LINK_STATUS, &status) if status == gl.FALSE { var logLength int32 gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength) log := strings.Repeat("\x00", int(logLength+1)) gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log)) return 0, fmt.Errorf("Failed to link program: %v", log) } return program, nil }
func newProgram(shaders ...uint32) (uint32, error) { program := gl.CreateProgram() for _, shader := range shaders { gl.AttachShader(program, shader) } gl.LinkProgram(program) var status int32 gl.GetProgramiv(program, gl.LINK_STATUS, &status) if status == gl.FALSE { var logLength int32 gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength) log := strings.Repeat("\x00", int(logLength+1)) gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log)) return 0, errors.New(fmt.Sprintf("failed to link program: %v", log)) } for _, shader := range shaders { gl.DeleteShader(shader) } return program, nil }
func NewProgram(shaderSource map[string]string) (*GLSLProgram, error) { var hasVertexShader bool var hasFragmentShader bool vertexShaderSource, ok := shaderSource["vertex"] var vertexShader uint32 if ok { vertexShader, err := compileShader(vertexShaderSource, gl.VERTEX_SHADER) if err != nil { return nil, err } hasVertexShader = true } fragmentShaderSource, ok := shaderSource["fragment"] var fragmentShader uint32 if ok { fragmentShader, err := compileShader(fragmentShaderSource, gl.FRAGMENT_SHADER) if err != nil { return nil, err } hasFragmentShader = true } program := gl.CreateProgram() if !(hasVertexShader && hasFragmentShader) { return nil, fmt.Errorf("Need vertex shader: %v, and fragment shader: %v", hasVertexShader, hasFragmentShader) } gl.AttachShader(program, vertexShader) gl.AttachShader(program, fragmentShader) gl.LinkProgram(program) var status int32 gl.GetProgramiv(program, gl.LINK_STATUS, &status) if status == gl.FALSE { var logLength int32 gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength) log := strings.Repeat("\x00", int(logLength+1)) gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log)) return nil, fmt.Errorf("Failed to link program(%v): %v", program, log) } gl.DeleteShader(vertexShader) gl.DeleteShader(fragmentShader) p := &GLSLProgram{program, vertexShaderSource, fragmentShaderSource, true} return p, nil }
func (p *Program) GetProgramInfoLog() string { var logSize int32 = 0 gl.GetProgramiv(p.prog, gl.INFO_LOG_LENGTH, &logSize) var infoLog []uint8 = make([]uint8, logSize) gl.GetProgramInfoLog(p.prog, logSize, &logSize, &infoLog[0]) return gl.GoStr(&infoLog[0]) }
func newProgram(vertexShaderFilename, fragmentShaderFilename string) (uint32, error) { program := gl.CreateProgram() //vertexShaderSource, err := shaders.Asset(vertexShaderFilename) //if err != nil { // return 0, err //} vertexShader, err := shaders.Compile(vertexShaderSource, gl.VERTEX_SHADER) if err != nil { return 0, err } //fragmentShaderSource, err := shaders.Asset(fragmentShaderFilename) //if err != nil { // return 0, err //} fragmentShader, err := shaders.Compile(fragmentShaderSource, gl.FRAGMENT_SHADER) if err != nil { return 0, err } gl.AttachShader(program, vertexShader) gl.AttachShader(program, fragmentShader) gl.LinkProgram(program) var status int32 gl.GetProgramiv(program, gl.LINK_STATUS, &status) if status == gl.FALSE { var logLength int32 gl.GetProgramiv(program, gl.INFO_LOG_LENGTH, &logLength) log := strings.Repeat("\x00", int(logLength+1)) gl.GetProgramInfoLog(program, logLength, nil, gl.Str(log)) return 0, errors.New(fmt.Sprintf("failed to link program: %v", log)) } gl.DeleteShader(vertexShader) gl.DeleteShader(fragmentShader) return program, nil }
func (p *Program) ValidateProgram() error { gl.ValidateProgram(p.prog) var success int32 = gl.FALSE gl.GetProgramiv(p.prog, gl.VALIDATE_STATUS, &success) if success == gl.FALSE { return fmt.Errorf("Failed to validate the program!\n%s", p.GetProgramInfoLog()) } return nil }
// loads shader objects and then attaches them to a program func LoadShaderProgram(vertShader string, fragShader string, attribs []string) (*Program, error) { // create the program var prog uint32 = gl.CreateProgram() var p *Program = &Program{prog, make(map[string]int32)} // create the vertex shader var vs uint32 = gl.CreateShader(gl.VERTEX_SHADER) ShaderSource(vs, vertShader) gl.CompileShader(vs) var success int32 = gl.FALSE gl.GetShaderiv(vs, gl.COMPILE_STATUS, &success) if success == gl.FALSE { return nil, fmt.Errorf("Failed to compile the vertex shader!\n%s", GetShaderInfoLog(vs)) } // create the fragment shader var fs uint32 = gl.CreateShader(gl.FRAGMENT_SHADER) ShaderSource(fs, fragShader) gl.CompileShader(fs) success = gl.FALSE gl.GetShaderiv(fs, gl.COMPILE_STATUS, &success) if success == gl.FALSE { return nil, fmt.Errorf("Failed to compile the fragment shader!\n%s", GetShaderInfoLog(fs)) } // attach the shaders to the program and link gl.AttachShader(prog, vs) gl.AttachShader(prog, fs) for i := 1; i <= len(attribs); i++ { var attr_array []uint8 = StringToArray(attribs[i-1]) gl.BindAttribLocation(prog, uint32(i), &attr_array[0]) } gl.LinkProgram(prog) success = gl.FALSE gl.GetProgramiv(prog, gl.LINK_STATUS, &success) if success == gl.FALSE { return nil, fmt.Errorf("Failed to link the program!\n%s", p.GetProgramInfoLog()) } // at this point the shaders can be deleted gl.DeleteShader(vs) gl.DeleteShader(fs) return p, nil }