Esempio n. 1
0
func (prg *Program) CreateAndLink(ctx gl.Context, compilers ...func(gl.Context) gl.Shader) {
	prg.Program = ctx.CreateProgram()
	for _, c := range compilers {
		s := c(ctx)
		ctx.AttachShader(prg.Program, s)
		defer ctx.DeleteShader(s)
	}
	ctx.LinkProgram(prg.Program)
	if ctx.GetProgrami(prg.Program, gl.LINK_STATUS) == 0 {
		log.Fatalf("program link: %s", ctx.GetProgramInfoLog(prg.Program))
	}
}
Esempio n. 2
0
func loadShader(glctx gl.Context, shaderType gl.Enum, src string) (gl.Shader, error) {
	shader := glctx.CreateShader(shaderType)
	if shader.Value == 0 {
		return gl.Shader{}, fmt.Errorf("glutil: could not create shader (type %v)", shaderType)
	}
	glctx.ShaderSource(shader, src)
	glctx.CompileShader(shader)
	if glctx.GetShaderi(shader, gl.COMPILE_STATUS) == 0 {
		defer glctx.DeleteShader(shader)
		return gl.Shader{}, fmt.Errorf("shader compile: %s", glctx.GetShaderInfoLog(shader))
	}
	return shader, nil
}
Esempio n. 3
0
func loadShader(glctx gl.Context, shaderType gl.Enum, assetName string) (gl.Shader, error) {
	// Borrowed from golang.org/x/mobile/exp/gl/glutil
	src, err := loadAsset(assetName)
	if err != nil {
		return gl.Shader{}, err
	}

	shader := glctx.CreateShader(shaderType)
	if shader.Value == 0 {
		return gl.Shader{}, fmt.Errorf("glutil: could not create shader (type %v)", shaderType)
	}
	glctx.ShaderSource(shader, string(src))
	glctx.CompileShader(shader)
	if glctx.GetShaderi(shader, gl.COMPILE_STATUS) == 0 {
		defer glctx.DeleteShader(shader)
		return gl.Shader{}, fmt.Errorf("shader compile: %s", glctx.GetShaderInfoLog(shader))
	}
	return shader, nil
}
Esempio n. 4
0
func LoadShaders(glctx gl.Context, program gl.Program, vertexAsset, fragmentAsset string) error {
	vertexShader, err := loadShader(glctx, gl.VERTEX_SHADER, vertexAsset)
	if err != nil {
		return err
	}
	fragmentShader, err := loadShader(glctx, gl.FRAGMENT_SHADER, fragmentAsset)
	if err != nil {
		glctx.DeleteShader(vertexShader)
		return err
	}

	if glctx.GetProgrami(program, gl.ATTACHED_SHADERS) > 0 {
		for _, shader := range glctx.GetAttachedShaders(program) {
			glctx.DetachShader(program, shader)
		}
	}

	glctx.AttachShader(program, vertexShader)
	glctx.AttachShader(program, fragmentShader)
	glctx.LinkProgram(program)

	// Flag shaders for deletion when program is unlinked.
	glctx.DeleteShader(vertexShader)
	glctx.DeleteShader(fragmentShader)

	if glctx.GetProgrami(program, gl.LINK_STATUS) == 0 {
		defer glctx.DeleteProgram(program)
		return fmt.Errorf("LoadShaders: %s", glctx.GetProgramInfoLog(program))
	}
	return nil
}
Esempio n. 5
0
// CreateProgram creates, compiles, and links a gl.Program.
func CreateProgram(glctx gl.Context, vertexSrc, fragmentSrc string) (gl.Program, error) {
	program := glctx.CreateProgram()
	if program.Value == 0 {
		return gl.Program{}, fmt.Errorf("glutil: no programs available")
	}

	vertexShader, err := loadShader(glctx, gl.VERTEX_SHADER, vertexSrc)
	if err != nil {
		return gl.Program{}, err
	}
	fragmentShader, err := loadShader(glctx, gl.FRAGMENT_SHADER, fragmentSrc)
	if err != nil {
		glctx.DeleteShader(vertexShader)
		return gl.Program{}, err
	}

	glctx.AttachShader(program, vertexShader)
	glctx.AttachShader(program, fragmentShader)
	glctx.LinkProgram(program)

	// Flag shaders for deletion when program is unlinked.
	glctx.DeleteShader(vertexShader)
	glctx.DeleteShader(fragmentShader)

	if glctx.GetProgrami(program, gl.LINK_STATUS) == 0 {
		defer glctx.DeleteProgram(program)
		return gl.Program{}, fmt.Errorf("glutil: %s", glctx.GetProgramInfoLog(program))
	}
	return program, nil
}