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
}
Exemple #2
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)
}
Exemple #3
0
func (s *Shader) addUniform(uniformName string) error {
	csource, free := gl.Strs(uniformName + "\000")
	defer free()
	uniformLocation := gl.GetUniformLocation(s.program, *csource)

	if uniformLocation == -1 {
		return fmt.Errorf("could not find uniform: %s", uniformName)
	}

	s.uniforms[uniformName] = uniformLocation
	return nil
}
Exemple #4
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, free := gl.Strs(text + "\000")
	defer free()
	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
}
Exemple #5
0
// ShaderSource sets the source code of s to the given source code.
//
// http://www.khronos.org/opengles/sdk/docs/man3/html/glShaderSource.xhtml
func ShaderSource(s Shader, src string) {
	glsource, free := gl.Strs(src + "\x00")
	gl.ShaderSource(s.Value, 1, glsource, nil)
	free()
}
Exemple #6
0
func (c *Context) ShaderSource(shader *Shader, source string) {
	glsource, free := gl.Strs(source + "\x00")
	gl.ShaderSource(shader.uint32, 1, glsource, nil)
	free()
}
Exemple #7
0
func (c *Context) GetUniformLocation(program *Program, name string) *UniformLocation {
	strptr, free := gl.Strs(name + "\x00")
	defer free()
	return &UniformLocation{gl.GetUniformLocation(program.uint32, *strptr)}
}
Exemple #8
0
func (c *Context) GetAttribLocation(program *Program, name string) int {
	strptr, free := gl.Strs(name + "\x00")
	defer free()
	return int(gl.GetAttribLocation(program.uint32, *strptr))
}
Exemple #9
0
func (c *Context) ShaderSource(shader *Shader, source string) {
	ptr, free := gl.Strs(source + "\x00")
	defer free()
	gl.ShaderSource(shader.uint32, 1, ptr, nil)
}