2016-11-23 13:06:34 -06:00
|
|
|
package pixelgl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-12-18 13:16:27 -06:00
|
|
|
"runtime"
|
2016-11-23 13:06:34 -06:00
|
|
|
|
2017-01-20 10:45:19 -06:00
|
|
|
"github.com/faiface/mainthread"
|
2016-11-23 13:06:34 -06:00
|
|
|
"github.com/go-gl/gl/v3.3-core/gl"
|
2016-12-01 09:44:54 -06:00
|
|
|
"github.com/go-gl/mathgl/mgl32"
|
2016-11-23 13:06:34 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Shader is an OpenGL shader program.
|
|
|
|
type Shader struct {
|
2016-12-15 17:28:52 -06:00
|
|
|
program binder
|
|
|
|
vertexFmt AttrFormat
|
|
|
|
uniformFmt AttrFormat
|
2017-01-13 17:47:49 -06:00
|
|
|
uniformLoc []int32
|
2016-11-23 13:06:34 -06:00
|
|
|
}
|
|
|
|
|
2016-12-30 10:53:47 -06:00
|
|
|
// NewShader creates a new shader program from the specified vertex shader and fragment shader
|
|
|
|
// sources.
|
2016-11-23 13:06:34 -06:00
|
|
|
//
|
2016-12-30 10:53:47 -06:00
|
|
|
// Note that vertexShader and fragmentShader parameters must contain the source code, they're
|
|
|
|
// not filenames.
|
2016-12-30 18:33:53 -06:00
|
|
|
func NewShader(vertexFmt, uniformFmt AttrFormat, vertexShader, fragmentShader string) (*Shader, error) {
|
2016-11-23 13:06:34 -06:00
|
|
|
shader := &Shader{
|
2016-12-14 09:24:31 -06:00
|
|
|
program: binder{
|
|
|
|
restoreLoc: gl.CURRENT_PROGRAM,
|
|
|
|
bindFunc: func(obj uint32) {
|
|
|
|
gl.UseProgram(obj)
|
|
|
|
},
|
|
|
|
},
|
2016-12-15 17:28:52 -06:00
|
|
|
vertexFmt: vertexFmt,
|
|
|
|
uniformFmt: uniformFmt,
|
2017-01-13 17:47:49 -06:00
|
|
|
uniformLoc: make([]int32, len(uniformFmt)),
|
2016-11-23 13:06:34 -06:00
|
|
|
}
|
2016-11-25 15:49:56 -06:00
|
|
|
|
2016-12-30 18:33:53 -06:00
|
|
|
var vshader, fshader uint32
|
|
|
|
|
|
|
|
// vertex shader
|
|
|
|
{
|
|
|
|
vshader = gl.CreateShader(gl.VERTEX_SHADER)
|
|
|
|
src, free := gl.Strs(vertexShader)
|
|
|
|
defer free()
|
|
|
|
length := int32(len(vertexShader))
|
|
|
|
gl.ShaderSource(vshader, 1, src, &length)
|
|
|
|
gl.CompileShader(vshader)
|
|
|
|
|
|
|
|
var (
|
|
|
|
success int32
|
|
|
|
infoLog = make([]byte, 512)
|
|
|
|
)
|
|
|
|
gl.GetShaderiv(vshader, gl.COMPILE_STATUS, &success)
|
|
|
|
if success == 0 {
|
|
|
|
gl.GetShaderInfoLog(vshader, int32(len(infoLog)), nil, &infoLog[0])
|
|
|
|
return nil, fmt.Errorf("error compiling vertex shader: %s", string(infoLog))
|
|
|
|
}
|
|
|
|
|
|
|
|
defer gl.DeleteShader(vshader)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fragment shader
|
|
|
|
{
|
|
|
|
fshader = gl.CreateShader(gl.FRAGMENT_SHADER)
|
|
|
|
src, free := gl.Strs(fragmentShader)
|
|
|
|
defer free()
|
|
|
|
length := int32(len(fragmentShader))
|
|
|
|
gl.ShaderSource(fshader, 1, src, &length)
|
|
|
|
gl.CompileShader(fshader)
|
|
|
|
|
|
|
|
var (
|
|
|
|
success int32
|
|
|
|
infoLog = make([]byte, 512)
|
|
|
|
)
|
|
|
|
gl.GetShaderiv(fshader, gl.COMPILE_STATUS, &success)
|
|
|
|
if success == 0 {
|
|
|
|
gl.GetShaderInfoLog(fshader, int32(len(infoLog)), nil, &infoLog[0])
|
|
|
|
return nil, fmt.Errorf("error compiling fragment shader: %s", string(infoLog))
|
|
|
|
}
|
|
|
|
|
|
|
|
defer gl.DeleteShader(fshader)
|
|
|
|
}
|
|
|
|
|
|
|
|
// shader program
|
|
|
|
{
|
|
|
|
shader.program.obj = gl.CreateProgram()
|
|
|
|
gl.AttachShader(shader.program.obj, vshader)
|
|
|
|
gl.AttachShader(shader.program.obj, fshader)
|
|
|
|
gl.LinkProgram(shader.program.obj)
|
|
|
|
|
|
|
|
var (
|
|
|
|
success int32
|
|
|
|
infoLog = make([]byte, 512)
|
|
|
|
)
|
|
|
|
gl.GetProgramiv(shader.program.obj, gl.LINK_STATUS, &success)
|
|
|
|
if success == 0 {
|
|
|
|
gl.GetProgramInfoLog(shader.program.obj, int32(len(infoLog)), nil, &infoLog[0])
|
|
|
|
return nil, fmt.Errorf("error linking shader program: %s", string(infoLog))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uniforms
|
2017-01-13 17:47:49 -06:00
|
|
|
for i, uniform := range uniformFmt {
|
|
|
|
loc := gl.GetUniformLocation(shader.program.obj, gl.Str(uniform.Name+"\x00"))
|
|
|
|
shader.uniformLoc[i] = loc
|
2016-11-23 13:06:34 -06:00
|
|
|
}
|
2016-11-25 15:49:56 -06:00
|
|
|
|
2016-12-18 13:16:27 -06:00
|
|
|
runtime.SetFinalizer(shader, (*Shader).delete)
|
|
|
|
|
2016-11-23 13:06:34 -06:00
|
|
|
return shader, nil
|
|
|
|
}
|
|
|
|
|
2016-12-18 13:16:27 -06:00
|
|
|
func (s *Shader) delete() {
|
2017-01-20 10:45:19 -06:00
|
|
|
mainthread.CallNonBlock(func() {
|
2016-12-18 18:11:34 -06:00
|
|
|
gl.DeleteProgram(s.program.obj)
|
2016-11-23 13:06:34 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-02 11:03:51 -06:00
|
|
|
// VertexFormat returns the vertex attribute format of this shader. Do not change it.
|
2016-12-15 17:28:52 -06:00
|
|
|
func (s *Shader) VertexFormat() AttrFormat {
|
|
|
|
return s.vertexFmt
|
2016-12-02 11:03:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// UniformFormat returns the uniform attribute format of this shader. Do not change it.
|
2016-12-15 17:28:52 -06:00
|
|
|
func (s *Shader) UniformFormat() AttrFormat {
|
|
|
|
return s.uniformFmt
|
2016-12-02 11:03:51 -06:00
|
|
|
}
|
|
|
|
|
2017-01-13 17:47:49 -06:00
|
|
|
// SetUniformAttr sets the value of a uniform attribute of a shader. The attribute is
|
|
|
|
// specified by the index in the Shader's uniform format.
|
2016-12-14 09:24:31 -06:00
|
|
|
//
|
2017-01-13 17:47:49 -06:00
|
|
|
// If the uniform attribute does not exist in the Shader, this method returns false.
|
2016-12-14 09:24:31 -06:00
|
|
|
//
|
2016-12-30 10:53:47 -06:00
|
|
|
// Supplied value must correspond to the type of the attribute. Correct types are these
|
|
|
|
// (right-hand is the type of the value):
|
2016-12-14 09:24:31 -06:00
|
|
|
// Attr{Type: Int}: int32
|
|
|
|
// Attr{Type: Float}: float32
|
|
|
|
// Attr{Type: Vec2}: mgl32.Vec2
|
|
|
|
// Attr{Type: Vec3}: mgl32.Vec3
|
|
|
|
// Attr{Type: Vec4}: mgl32.Vec4
|
|
|
|
// Attr{Type: Mat2}: mgl32.Mat2
|
|
|
|
// Attr{Type: Mat23}: mgl32.Mat2x3
|
|
|
|
// Attr{Type: Mat24}: mgl32.Mat2x4
|
|
|
|
// Attr{Type: Mat3}: mgl32.Mat3
|
|
|
|
// Attr{Type: Mat32}: mgl32.Mat3x2
|
|
|
|
// Attr{Type: Mat34}: mgl32.Mat3x4
|
|
|
|
// Attr{Type: Mat4}: mgl32.Mat4
|
|
|
|
// Attr{Type: Mat42}: mgl32.Mat4x2
|
|
|
|
// Attr{Type: Mat43}: mgl32.Mat4x3
|
|
|
|
// No other types are supported.
|
2016-12-30 18:33:53 -06:00
|
|
|
//
|
|
|
|
// The shader must be bound before calling this method.
|
2017-01-13 17:47:49 -06:00
|
|
|
func (s *Shader) SetUniformAttr(uniform int, value interface{}) (ok bool) {
|
|
|
|
if s.uniformLoc[uniform] < 0 {
|
2016-12-01 09:10:05 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-01-13 17:47:49 -06:00
|
|
|
switch s.uniformFmt[uniform].Type {
|
2016-12-30 18:33:53 -06:00
|
|
|
case Int:
|
|
|
|
value := value.(int32)
|
2017-01-13 17:47:49 -06:00
|
|
|
gl.Uniform1iv(s.uniformLoc[uniform], 1, &value)
|
2016-12-30 18:33:53 -06:00
|
|
|
case Float:
|
|
|
|
value := value.(float32)
|
2017-01-13 17:47:49 -06:00
|
|
|
gl.Uniform1fv(s.uniformLoc[uniform], 1, &value)
|
2016-12-30 18:33:53 -06:00
|
|
|
case Vec2:
|
|
|
|
value := value.(mgl32.Vec2)
|
2017-01-13 17:47:49 -06:00
|
|
|
gl.Uniform2fv(s.uniformLoc[uniform], 1, &value[0])
|
2016-12-30 18:33:53 -06:00
|
|
|
case Vec3:
|
|
|
|
value := value.(mgl32.Vec3)
|
2017-01-13 17:47:49 -06:00
|
|
|
gl.Uniform3fv(s.uniformLoc[uniform], 1, &value[0])
|
2016-12-30 18:33:53 -06:00
|
|
|
case Vec4:
|
|
|
|
value := value.(mgl32.Vec4)
|
2017-01-13 17:47:49 -06:00
|
|
|
gl.Uniform4fv(s.uniformLoc[uniform], 1, &value[0])
|
2016-12-30 18:33:53 -06:00
|
|
|
case Mat2:
|
|
|
|
value := value.(mgl32.Mat2)
|
2017-01-13 17:47:49 -06:00
|
|
|
gl.UniformMatrix2fv(s.uniformLoc[uniform], 1, false, &value[0])
|
2016-12-30 18:33:53 -06:00
|
|
|
case Mat23:
|
|
|
|
value := value.(mgl32.Mat2x3)
|
2017-01-13 17:47:49 -06:00
|
|
|
gl.UniformMatrix2x3fv(s.uniformLoc[uniform], 1, false, &value[0])
|
2016-12-30 18:33:53 -06:00
|
|
|
case Mat24:
|
|
|
|
value := value.(mgl32.Mat2x4)
|
2017-01-13 17:47:49 -06:00
|
|
|
gl.UniformMatrix2x4fv(s.uniformLoc[uniform], 1, false, &value[0])
|
2016-12-30 18:33:53 -06:00
|
|
|
case Mat3:
|
|
|
|
value := value.(mgl32.Mat3)
|
2017-01-13 17:47:49 -06:00
|
|
|
gl.UniformMatrix3fv(s.uniformLoc[uniform], 1, false, &value[0])
|
2016-12-30 18:33:53 -06:00
|
|
|
case Mat32:
|
|
|
|
value := value.(mgl32.Mat3x2)
|
2017-01-13 17:47:49 -06:00
|
|
|
gl.UniformMatrix3x2fv(s.uniformLoc[uniform], 1, false, &value[0])
|
2016-12-30 18:33:53 -06:00
|
|
|
case Mat34:
|
|
|
|
value := value.(mgl32.Mat3x4)
|
2017-01-13 17:47:49 -06:00
|
|
|
gl.UniformMatrix3x4fv(s.uniformLoc[uniform], 1, false, &value[0])
|
2016-12-30 18:33:53 -06:00
|
|
|
case Mat4:
|
|
|
|
value := value.(mgl32.Mat4)
|
2017-01-13 17:47:49 -06:00
|
|
|
gl.UniformMatrix4fv(s.uniformLoc[uniform], 1, false, &value[0])
|
2016-12-30 18:33:53 -06:00
|
|
|
case Mat42:
|
|
|
|
value := value.(mgl32.Mat4x2)
|
2017-01-13 17:47:49 -06:00
|
|
|
gl.UniformMatrix4x2fv(s.uniformLoc[uniform], 1, false, &value[0])
|
2016-12-30 18:33:53 -06:00
|
|
|
case Mat43:
|
|
|
|
value := value.(mgl32.Mat4x3)
|
2017-01-13 17:47:49 -06:00
|
|
|
gl.UniformMatrix4x3fv(s.uniformLoc[uniform], 1, false, &value[0])
|
2016-12-30 18:33:53 -06:00
|
|
|
default:
|
|
|
|
panic("set uniform attr: invalid attribute type")
|
|
|
|
}
|
2016-12-14 09:24:31 -06:00
|
|
|
|
2016-12-01 09:10:05 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-12-30 18:33:53 -06:00
|
|
|
// Begin binds a shader program. This is necessary before using the shader.
|
|
|
|
func (s *Shader) Begin() {
|
|
|
|
s.program.bind()
|
|
|
|
}
|
|
|
|
|
|
|
|
// End unbinds a shader program and restores the previous one.
|
|
|
|
func (s *Shader) End() {
|
|
|
|
s.program.restore()
|
2016-11-23 13:06:34 -06:00
|
|
|
}
|