Ejemplo n.º 1
0
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
}
Ejemplo n.º 2
0
Archivo: gg.go Proyecto: dmac/gg
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)
}
Ejemplo n.º 3
0
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)
}
Ejemplo n.º 4
0
// 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)
}
Ejemplo n.º 5
0
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)
}
Ejemplo n.º 6
0
// 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])
}
Ejemplo n.º 7
0
func initResources() (uint32, int32) {
	vSrc0 := fixupSrc(vShaderSrc)
	vSrc := gl.Str(vSrc0)
	vs := gl.CreateShader(gl.VERTEX_SHADER)
	gl.ShaderSource(vs, 1, &vSrc, nil)
	gl.CompileShader(vs)
	var vok int32
	gl.GetShaderiv(vs, gl.COMPILE_STATUS, &vok)
	if vok == 0 {
		log.Fatal("error in vertex shader")

	}

	fSrc := gl.Str(fShaderSrc)
	fs := gl.CreateShader(gl.FRAGMENT_SHADER)
	gl.ShaderSource(fs, 1, &fSrc, nil)
	gl.CompileShader(fs)
	var fok int32
	gl.GetShaderiv(vs, gl.COMPILE_STATUS, &fok)
	if fok == 0 {
		log.Fatal("error in fragment shader")

	}

	var linkOk int32
	program := gl.CreateProgram()
	gl.AttachShader(program, vs)
	gl.AttachShader(program, fs)
	gl.LinkProgram(program)
	gl.GetProgramiv(program, gl.LINK_STATUS, &linkOk)
	if linkOk == 0 {
		log.Fatal("gl.LinkProgram")
	}

	attribName := fixupSrc("coord2d")
	attribCoord2d := gl.GetAttribLocation(program, gl.Str(attribName))
	if attribCoord2d == -1 {
		log.Fatal("failed to bind attribute")
	}
	return program, attribCoord2d
}
Ejemplo n.º 8
0
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
}
Ejemplo n.º 9
0
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
}
Ejemplo n.º 10
0
func (s *Shader) addProgram(text string, typ uint32) error {
	shader := gl.CreateShader(typ)
	if shader == 0 {
		return errors.New("could not find valid memory location when adding shader")
	}
	cStr := gl.Str(text + "\000")
	gl.ShaderSource(shader, 1, &cStr, nil)
	gl.CompileShader(shader)

	var result int32
	gl.GetShaderiv(shader, gl.COMPILE_STATUS, &result)
	if result == 0 {
		return s.getShaderInfoLog(shader, "get shader error")
	}

	gl.AttachShader(s.program, shader)
	return nil
}
Ejemplo n.º 11
0
// 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)
}
Ejemplo n.º 12
0
func loadShader(shaderType uint32, source string) uint32 {
	shader := gl.CreateShader(shaderType)
	if err := gl.GetError(); err != gl.NO_ERROR {
		panic(fmt.Errorf("gl error: %v", err))
	}

	csource := gl.Str(source)
	gl.ShaderSource(shader, 1, &csource, nil)
	gl.CompileShader(shader)

	var status int32
	gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
	if status != gl.TRUE {
		//log := shader.GetInfoLog()
		panic(fmt.Errorf("Failed to compile shader:  shader: %v", source))
	}

	return shader
}
Ejemplo n.º 13
0
// CreateShader creates a shader object from a source file.
func CreateShader(filename string, xtype uint32) uint32 {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		log.Fatal(err)
	}

	dataString := "#version 120\n" + string(data) + "\x00"
	dataSrc := gl.Str(dataString)

	res := gl.CreateShader(xtype)
	gl.ShaderSource(res, 1, &dataSrc, nil)
	gl.CompileShader(res)

	var compileOk int32
	gl.GetShaderiv(res, gl.COMPILE_STATUS, &compileOk)
	if compileOk == gl.FALSE {
		PrintLog(res)
		gl.DeleteShader(res)
	}
	return res
}
Ejemplo n.º 14
0
// GetRenderbufferParameteri returns a parameter value for a shader.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glGetShaderiv.xhtml
func GetShaderi(s Shader, pname Enum) int {
	var result int32
	gl.GetShaderiv(s.Value, uint32(pname), &result)
	return int(result)
}
Ejemplo n.º 15
0
// Returns a parameter from a shader object
func (c *Context) GetShaderiv(shader *Shader, pname uint32) bool {
	var success int32
	gl.GetShaderiv(shader.uint32, pname, &success)
	return success == int32(gl.TRUE)
}