func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) { var shader Shader if err := c.runOnContextThread(func() error { s := gl.CreateShader(uint32(shaderType)) if s == 0 { return fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType) } cSources, free := gl.Strs(source + "\x00") gl.ShaderSource(uint32(s), 1, cSources, nil) free() gl.CompileShader(s) var v int32 gl.GetShaderiv(s, gl.COMPILE_STATUS, &v) if v == gl.FALSE { log := []uint8{} gl.GetShaderiv(uint32(s), gl.INFO_LOG_LENGTH, &v) if v != 0 { log = make([]uint8, int(v)) gl.GetShaderInfoLog(uint32(s), v, nil, (*uint8)(gl.Ptr(log))) } return fmt.Errorf("opengl: shader compile failed: %s", log) } shader = Shader(s) return nil }); err != nil { return 0, err } return shader, nil }
func (s *Shader) getShaderInfoLog(shader uint32, context string) error { var logLength int32 gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength) log := strings.Repeat("\x00", int(logLength+1)) gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log)) return fmt.Errorf("%s: %s", context, log) }
// GetShaderInfoLog is a method you can call to get the compilation logs of a shader func (c *Context) GetShaderInfoLog(shader *Shader) string { var maxLength int32 gl.GetShaderiv(shader.uint32, gl.INFO_LOG_LENGTH, &maxLength) errorLog := make([]byte, maxLength) gl.GetShaderInfoLog(shader.uint32, maxLength, &maxLength, (*uint8)(gl.Ptr(errorLog))) return string(errorLog) }
func (s *Shader) getShaderInfoLog(shader uint32, context string) error { var logLength int32 gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength) cLog, free := gl.Strs(strings.Repeat("\x00", int(logLength+1))) defer free() gl.GetShaderInfoLog(shader, logLength, nil, *cLog) return fmt.Errorf("%s: %s", context, cLog) }
// GetShaderInfoLog returns the information log for a shader. // // http://www.khronos.org/opengles/sdk/docs/man3/html/glGetShaderInfoLog.xhtml func GetShaderInfoLog(s Shader) string { var logLength int32 gl.GetShaderiv(s.Value, gl.INFO_LOG_LENGTH, &logLength) if logLength == 0 { return "" } logBuffer := make([]uint8, logLength) gl.GetShaderInfoLog(s.Value, logLength, nil, &logBuffer[0]) return gl.GoStr(&logBuffer[0]) }
func (*backend) CreateShader(src []byte, typ gg.Enum) (*gg.Shader, error) { csrc := gl.Str(string(append([]byte(src), 0))) shader := gl.CreateShader(uint32(typ)) gl.ShaderSource(shader, 1, &csrc, nil) gl.CompileShader(shader) var status int32 gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status) if status == gl.TRUE { return &gg.Shader{Value: shader}, nil } var logLength int32 gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength) log := strings.Repeat("\x00", int(logLength+1)) gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log)) return nil, fmt.Errorf("compile shader: %s%s", src, log) }
// PrintLog prints the error log for an object func PrintLog(object uint32) { var logLength int32 if gl.IsShader(object) { gl.GetShaderiv(object, gl.INFO_LOG_LENGTH, &logLength) } else if gl.IsProgram(object) { gl.GetProgramiv(object, gl.INFO_LOG_LENGTH, &logLength) } else { log.Fatal("PrintLog: not a shader or program") } infoLog := strings.Repeat("\x00", int(logLength+1)) if gl.IsShader(object) { gl.GetShaderInfoLog(object, logLength, nil, gl.Str(infoLog)) } else if gl.IsProgram(object) { gl.GetProgramInfoLog(object, logLength, nil, gl.Str(infoLog)) } log.Fatal(infoLog) }
func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) { s := gl.CreateShader(uint32(shaderType)) if s == 0 { return 0, errors.New("glCreateShader failed") } glSource := gl.Str(source + "\x00") gl.ShaderSource(uint32(s), 1, &glSource, nil) gl.CompileShader(s) var v int32 gl.GetShaderiv(s, gl.COMPILE_STATUS, &v) if v == gl.FALSE { log := []uint8{} gl.GetShaderiv(uint32(s), gl.INFO_LOG_LENGTH, &v) if v != 0 { log = make([]uint8, int(v)) gl.GetShaderInfoLog(uint32(s), v, nil, (*uint8)(gl.Ptr(log))) } return 0, errors.New(fmt.Sprintf("shader compile failed: %s", string(log))) } return Shader(s), nil }
func newShader(source string, shaderType uint32) (uint32, error) { id := gl.CreateShader(shaderType) if id == 0 { return 0, fmt.Errorf("Unable to allocate shader") } ptr, free := convc.StringToC(source) defer free() gl.ShaderSource(id, 1, &ptr, nil) gl.CompileShader(id) var result int32 gl.GetShaderiv(id, gl.COMPILE_STATUS, &result) if result == int32(gl.FALSE) { var loglength int32 gl.GetShaderiv(id, gl.INFO_LOG_LENGTH, &loglength) log := make([]byte, loglength) var length int32 gl.GetShaderInfoLog(id, loglength, &length, &log[0]) gl.DeleteShader(id) return 0, fmt.Errorf("Unable to compile shader: %s", log[:length]) } return id, nil }