Add opengl.UniformLocation

This commit is contained in:
Hajime Hoshi 2014-12-31 16:48:25 +09:00
parent dc6a153c65
commit b8dde70b03
3 changed files with 56 additions and 60 deletions

View File

@ -15,35 +15,11 @@
package shader package shader
import ( import (
"errors"
"github.com/go-gl/gl" "github.com/go-gl/gl"
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
) )
type program struct { var programColorMatrix opengl.Program
native gl.Program
shaderIds []shaderId
}
var programColorMatrix = program{
shaderIds: []shaderId{shaderVertex, shaderColorMatrix},
}
func (p *program) create() error {
p.native = gl.CreateProgram()
if p.native == 0 {
return errors.New("glCreateProgram failed")
}
for _, shaderId := range p.shaderIds {
p.native.AttachShader(gl.Shader(shaders[shaderId].native))
}
p.native.Link()
if p.native.Get(gl.LINK_STATUS) == gl.FALSE {
return errors.New("program error")
}
return nil
}
func initialize(c *opengl.Context) error { func initialize(c *opengl.Context) error {
var err error var err error
@ -51,36 +27,37 @@ func initialize(c *opengl.Context) error {
if err != nil { if err != nil {
return err return err
} }
defer shaders[shaderVertex].delete() defer shaders[shaderVertex].native.Delete()
shaders[shaderColorMatrix].native, err = c.NewShader(c.FragmentShader, shaders[shaderColorMatrix].source) shaders[shaderColorMatrix].native, err = c.NewShader(c.FragmentShader, shaders[shaderColorMatrix].source)
if err != nil { if err != nil {
return err return err
} }
defer shaders[shaderColorMatrix].delete() defer shaders[shaderColorMatrix].native.Delete()
return programColorMatrix.create() shaders := []opengl.Shader{
shaders[shaderVertex].native,
shaders[shaderColorMatrix].native,
}
programColorMatrix, err = c.NewProgram(shaders)
return err
} }
func getAttributeLocation(program gl.Program, name string) gl.AttribLocation { func getAttributeLocation(program gl.Program, name string) gl.AttribLocation {
return program.GetAttribLocation(name) return program.GetAttribLocation(name)
} }
func getUniformLocation(program gl.Program, name string) gl.UniformLocation { var lastProgram opengl.Program = 0
return program.GetUniformLocation(name)
}
var lastProgram gl.Program = 0
func useProgramColorMatrix(projectionMatrix [16]float32, geo Matrix, color Matrix) gl.Program { func useProgramColorMatrix(projectionMatrix [16]float32, geo Matrix, color Matrix) gl.Program {
if lastProgram != programColorMatrix.native { if lastProgram != programColorMatrix {
programColorMatrix.native.Use() programColorMatrix.Use()
lastProgram = programColorMatrix.native lastProgram = programColorMatrix
} }
// TODO: Check the performance. // TODO: Check the performance.
program := programColorMatrix program := programColorMatrix
getUniformLocation(program.native, "projection_matrix").UniformMatrix4fv(false, projectionMatrix) program.GetUniformLocation("projection_matrix").UniformMatrix4fv(projectionMatrix)
a := float32(geo.Element(0, 0)) a := float32(geo.Element(0, 0))
b := float32(geo.Element(0, 1)) b := float32(geo.Element(0, 1))
@ -94,9 +71,9 @@ func useProgramColorMatrix(projectionMatrix [16]float32, geo Matrix, color Matri
0, 0, 1, 0, 0, 0, 1, 0,
tx, ty, 0, 1, tx, ty, 0, 1,
} }
getUniformLocation(program.native, "modelview_matrix").UniformMatrix4fv(false, glModelviewMatrix) program.GetUniformLocation("modelview_matrix").UniformMatrix4fv(glModelviewMatrix)
getUniformLocation(program.native, "texture").Uniform1i(0) program.GetUniformLocation("texture").Uniform1i(0)
e := [4][5]float32{} e := [4][5]float32{}
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
@ -111,11 +88,11 @@ func useProgramColorMatrix(projectionMatrix [16]float32, geo Matrix, color Matri
e[0][2], e[1][2], e[2][2], e[3][2], e[0][2], e[1][2], e[2][2], e[3][2],
e[0][3], e[1][3], e[2][3], e[3][3], e[0][3], e[1][3], e[2][3], e[3][3],
} }
getUniformLocation(program.native, "color_matrix").UniformMatrix4fv(false, glColorMatrix) program.GetUniformLocation("color_matrix").UniformMatrix4fv(glColorMatrix)
glColorMatrixTranslation := [...]float32{ glColorMatrixTranslation := [...]float32{
e[0][4], e[1][4], e[2][4], e[3][4], e[0][4], e[1][4], e[2][4], e[3][4],
} }
getUniformLocation(program.native, "color_matrix_translation").Uniform4fv(1, glColorMatrixTranslation[:]) program.GetUniformLocation("color_matrix_translation").Uniform4fv(1, glColorMatrixTranslation[:])
return program.native return gl.Program(program)
} }

View File

@ -15,7 +15,6 @@
package shader package shader
import ( import (
"github.com/go-gl/gl"
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
) )
@ -71,7 +70,3 @@ void main(void) {
`, `,
}, },
} }
func (s *shader) delete() {
gl.Shader(s.native).Delete()
}

View File

@ -80,14 +80,38 @@ func (f Framebuffer) Delete() {
type Shader gl.Shader type Shader gl.Shader
type Program gl.Program func (s Shader) Delete() {
gl.Shader(s).Delete()
func (p Program) GetAttribLocation(name string) int {
return int(p.GetAttribLocation(name))
} }
func (p Program) GetUniformLocation(name string) int { type Program gl.Program
return int(p.GetUniformLocation(name))
func (p Program) Use() {
gl.Program(p).Use()
}
func (p Program) GetAttribLocation(name string) AttribLocation {
return AttribLocation(gl.Program(p).GetAttribLocation(name))
}
func (p Program) GetUniformLocation(name string) UniformLocation {
return UniformLocation(gl.Program(p).GetUniformLocation(name))
}
type AttribLocation int
type UniformLocation int
func (u UniformLocation) UniformMatrix4fv(matrix [16]float32) {
gl.UniformLocation(u).UniformMatrix4fv(false, matrix)
}
func (u UniformLocation) Uniform4fv(count int, v []float32) {
gl.UniformLocation(u).Uniform4fv(count, v)
}
func (u UniformLocation) Uniform1i(v int) {
gl.UniformLocation(u).Uniform1i(v)
} }
type Context struct { type Context struct {
@ -165,18 +189,18 @@ func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error
return Shader(s), nil return Shader(s), nil
} }
func (c *Context) NewProgram() (Program, error) { func (c *Context) NewProgram(shaders []Shader) (Program, error) {
p := gl.CreateProgram() p := gl.CreateProgram()
if p == 0 { if p == 0 {
return 0, errors.New("glCreateProgram failed") return 0, errors.New("glCreateProgram failed")
} }
/*for _, shaderId := range p.shaderIds { for _, shader := range shaders {
p.native.AttachShader(shaders[shaderId].native) p.AttachShader(gl.Shader(shader))
}
p.Link()
if p.Get(gl.LINK_STATUS) == gl.FALSE {
return 0, errors.New("program error")
} }
p.native.Link()
if p.native.Get(gl.LINK_STATUS) == gl.FALSE {
return errors.New("program error")
}*/
return Program(p), nil return Program(p), nil
} }