ebiten/graphics/opengl/internal/shader/program.go

111 lines
2.8 KiB
Go
Raw Normal View History

2013-10-26 18:56:58 +02:00
package shader
import (
2014-12-06 07:47:48 +01:00
"github.com/go-gl/gl"
2014-12-05 14:16:58 +01:00
"github.com/hajimehoshi/ebiten/graphics/matrix"
2013-10-26 18:56:58 +02:00
)
2013-12-12 16:31:00 +01:00
type program struct {
2014-12-06 07:47:48 +01:00
native gl.Program
2013-12-12 16:31:00 +01:00
shaderIds []shaderId
}
2014-12-06 14:56:57 +01:00
var programColorMatrix = program{
shaderIds: []shaderId{shaderVertex, shaderColorMatrix},
2013-12-12 16:31:00 +01:00
}
func (p *program) create() {
2014-12-06 07:47:48 +01:00
p.native = gl.CreateProgram()
2013-12-12 16:31:00 +01:00
if p.native == 0 {
panic("glCreateProgram failed")
}
for _, shaderId := range p.shaderIds {
2014-12-06 07:47:48 +01:00
p.native.AttachShader(shaders[shaderId].native)
2013-10-26 18:56:58 +02:00
}
2014-12-06 07:47:48 +01:00
p.native.Link()
if p.native.Get(gl.LINK_STATUS) == gl.FALSE {
2013-10-26 18:56:58 +02:00
panic("program error")
}
}
func initialize() {
2013-12-12 16:31:00 +01:00
for _, shader := range shaders {
shader.compile()
2013-10-26 18:56:58 +02:00
}
2013-12-12 16:31:00 +01:00
defer func() {
for _, shader := range shaders {
shader.delete()
}
}()
2013-10-26 18:56:58 +02:00
2014-12-06 14:56:57 +01:00
programColorMatrix.create()
programColorMatrix.native.Use()
2013-10-26 18:56:58 +02:00
}
2014-12-07 20:22:50 +01:00
// NOTE: This caches are now used only for programColorMatrix
var attribLocationCache = map[string]gl.AttribLocation{}
var uniformLocationCache = map[string]gl.UniformLocation{}
2013-12-13 21:34:27 +01:00
2014-12-07 20:22:50 +01:00
func getAttributeLocation(program gl.Program, name string) gl.AttribLocation {
if location, ok := attribLocationCache[name]; ok {
2013-10-26 18:56:58 +02:00
return location
}
2014-12-07 20:22:50 +01:00
location := program.GetAttribLocation(name)
attribLocationCache[name] = location
2013-10-26 18:56:58 +02:00
return location
}
2014-12-06 07:47:48 +01:00
func getUniformLocation(program gl.Program, name string) gl.UniformLocation {
2014-12-07 20:22:50 +01:00
if location, ok := uniformLocationCache[name]; ok {
return location
}
location := program.GetUniformLocation(name)
uniformLocationCache[name] = location
return location
2013-10-26 18:56:58 +02:00
}
2014-12-07 20:22:50 +01:00
func use(projectionMatrix [16]float32, geo matrix.Geometry, color matrix.Color) gl.Program {
2014-05-11 12:44:36 +02:00
// TODO: Check the performance.
2014-12-06 14:56:57 +01:00
program := programColorMatrix
2013-10-27 11:54:28 +01:00
2014-12-06 07:47:48 +01:00
getUniformLocation(program.native, "projection_matrix").UniformMatrix4fv(false, projectionMatrix)
2013-10-26 18:56:58 +02:00
2014-12-07 20:22:50 +01:00
a := float32(geo.Elements[0][0])
b := float32(geo.Elements[0][1])
c := float32(geo.Elements[1][0])
d := float32(geo.Elements[1][1])
tx := float32(geo.Elements[0][2])
ty := float32(geo.Elements[1][2])
2013-10-26 18:56:58 +02:00
glModelviewMatrix := [...]float32{
a, c, 0, 0,
b, d, 0, 0,
0, 0, 1, 0,
tx, ty, 0, 1,
}
2014-12-06 07:47:48 +01:00
getUniformLocation(program.native, "modelview_matrix").UniformMatrix4fv(false, glModelviewMatrix)
2013-10-26 18:56:58 +02:00
2014-12-06 07:47:48 +01:00
getUniformLocation(program.native, "texture").Uniform1i(0)
2013-10-26 18:56:58 +02:00
e := [4][5]float32{}
for i := 0; i < 4; i++ {
for j := 0; j < 5; j++ {
2014-12-07 20:22:50 +01:00
e[i][j] = float32(color.Elements[i][j])
2013-10-26 18:56:58 +02:00
}
}
glColorMatrix := [...]float32{
e[0][0], e[1][0], e[2][0], e[3][0],
e[0][1], e[1][1], e[2][1], e[3][1],
e[0][2], e[1][2], e[2][2], e[3][2],
e[0][3], e[1][3], e[2][3], e[3][3],
}
2014-12-06 07:47:48 +01:00
getUniformLocation(program.native, "color_matrix").UniformMatrix4fv(false, glColorMatrix)
2013-10-26 18:56:58 +02:00
glColorMatrixTranslation := [...]float32{
e[0][4], e[1][4], e[2][4], e[3][4],
}
2014-12-06 07:47:48 +01:00
getUniformLocation(program.native, "color_matrix_translation").Uniform4fv(1, glColorMatrixTranslation[:])
2013-10-26 18:56:58 +02:00
2013-12-12 16:31:00 +01:00
return program.native
2013-10-26 18:56:58 +02:00
}