2013-10-26 18:56:58 +02:00
|
|
|
package shader
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -framework OpenGL
|
|
|
|
//
|
|
|
|
// #include <OpenGL/gl.h>
|
|
|
|
// #include <stdlib.h>
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2013-12-12 16:31:00 +01:00
|
|
|
type program struct {
|
|
|
|
native C.GLuint
|
|
|
|
shaderIds []shaderId
|
|
|
|
}
|
|
|
|
|
|
|
|
type programId int
|
|
|
|
|
|
|
|
const (
|
|
|
|
programRegular programId = iota
|
|
|
|
programColorMatrix
|
|
|
|
)
|
|
|
|
|
|
|
|
var programs = map[programId]*program{
|
2014-05-11 12:44:36 +02:00
|
|
|
// TODO: programRegular is not used for now. Remove this.
|
2013-12-12 16:31:00 +01:00
|
|
|
programRegular: &program{
|
|
|
|
shaderIds: []shaderId{shaderVertex, shaderFragment},
|
|
|
|
},
|
|
|
|
programColorMatrix: &program{
|
|
|
|
shaderIds: []shaderId{shaderVertex, shaderColorMatrix},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *program) create() {
|
|
|
|
p.native = C.glCreateProgram()
|
|
|
|
if p.native == 0 {
|
|
|
|
panic("glCreateProgram failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, shaderId := range p.shaderIds {
|
|
|
|
C.glAttachShader(p.native, shaders[shaderId].native)
|
2013-10-26 18:56:58 +02:00
|
|
|
}
|
2013-12-12 16:31:00 +01:00
|
|
|
C.glLinkProgram(p.native)
|
2013-10-26 18:56:58 +02:00
|
|
|
linked := C.GLint(C.GL_FALSE)
|
2013-12-12 16:31:00 +01:00
|
|
|
C.glGetProgramiv(p.native, C.GL_LINK_STATUS, &linked)
|
2013-10-26 18:56:58 +02:00
|
|
|
if linked == C.GL_FALSE {
|
|
|
|
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
|
|
|
|
2013-12-12 16:31:00 +01:00
|
|
|
for _, program := range programs {
|
|
|
|
program.create()
|
|
|
|
}
|
2013-10-26 18:56:58 +02:00
|
|
|
}
|
|
|
|
|
2013-12-13 21:34:27 +01:00
|
|
|
type qualifierVariableType int
|
|
|
|
|
2013-10-26 18:56:58 +02:00
|
|
|
const (
|
2013-12-13 21:34:27 +01:00
|
|
|
qualifierVariableTypeAttribute qualifierVariableType = iota
|
2013-10-26 18:56:58 +02:00
|
|
|
qualifierVariableTypeUniform
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2013-12-13 21:34:27 +01:00
|
|
|
shaderLocationCache = map[qualifierVariableType]map[string]C.GLint{
|
2013-10-26 18:56:58 +02:00
|
|
|
qualifierVariableTypeAttribute: map[string]C.GLint{},
|
|
|
|
qualifierVariableTypeUniform: map[string]C.GLint{},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2013-12-13 21:34:27 +01:00
|
|
|
func getLocation(program C.GLuint, name string, qvType qualifierVariableType) C.GLint {
|
|
|
|
if location, ok := shaderLocationCache[qvType][name]; ok {
|
2013-10-26 18:56:58 +02:00
|
|
|
return location
|
|
|
|
}
|
|
|
|
|
|
|
|
locationName := C.CString(name)
|
|
|
|
defer C.free(unsafe.Pointer(locationName))
|
|
|
|
|
2014-05-03 20:29:45 +02:00
|
|
|
const invalidLocation = -1
|
|
|
|
location := C.GLint(invalidLocation)
|
2013-10-27 11:54:28 +01:00
|
|
|
|
2013-12-13 21:34:27 +01:00
|
|
|
switch qvType {
|
2013-10-26 18:56:58 +02:00
|
|
|
case qualifierVariableTypeAttribute:
|
|
|
|
location = C.glGetAttribLocation(program, (*C.GLchar)(locationName))
|
|
|
|
case qualifierVariableTypeUniform:
|
|
|
|
location = C.glGetUniformLocation(program, (*C.GLchar)(locationName))
|
|
|
|
default:
|
|
|
|
panic("no reach")
|
|
|
|
}
|
2014-05-03 20:29:45 +02:00
|
|
|
if location == invalidLocation {
|
2013-10-26 18:56:58 +02:00
|
|
|
panic("glGetUniformLocation failed")
|
|
|
|
}
|
2013-12-13 21:34:27 +01:00
|
|
|
shaderLocationCache[qvType][name] = location
|
2013-10-26 18:56:58 +02:00
|
|
|
|
|
|
|
return location
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAttributeLocation(program C.GLuint, name string) C.GLint {
|
|
|
|
return getLocation(program, name, qualifierVariableTypeAttribute)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUniformLocation(program C.GLuint, name string) C.GLint {
|
|
|
|
return getLocation(program, name, qualifierVariableTypeUniform)
|
|
|
|
}
|
|
|
|
|
2013-11-25 17:08:12 +01:00
|
|
|
func use(projectionMatrix [16]float32,
|
2014-05-03 20:29:45 +02:00
|
|
|
geometryMatrix matrix.Geometry,
|
|
|
|
colorMatrix matrix.Color) C.GLuint {
|
2014-05-11 12:44:36 +02:00
|
|
|
programId := programColorMatrix
|
2013-12-12 16:31:00 +01:00
|
|
|
program := programs[programId]
|
2014-05-11 12:44:36 +02:00
|
|
|
// TODO: Check the performance.
|
2013-12-12 16:31:00 +01:00
|
|
|
C.glUseProgram(program.native)
|
2013-10-27 11:54:28 +01:00
|
|
|
|
2013-12-12 16:31:00 +01:00
|
|
|
C.glUniformMatrix4fv(C.GLint(getUniformLocation(program.native, "projection_matrix")),
|
2013-10-26 18:56:58 +02:00
|
|
|
1, C.GL_FALSE, (*C.GLfloat)(&projectionMatrix[0]))
|
|
|
|
|
|
|
|
a := float32(geometryMatrix.Elements[0][0])
|
|
|
|
b := float32(geometryMatrix.Elements[0][1])
|
|
|
|
c := float32(geometryMatrix.Elements[1][0])
|
|
|
|
d := float32(geometryMatrix.Elements[1][1])
|
|
|
|
tx := float32(geometryMatrix.Elements[0][2])
|
|
|
|
ty := float32(geometryMatrix.Elements[1][2])
|
|
|
|
glModelviewMatrix := [...]float32{
|
|
|
|
a, c, 0, 0,
|
|
|
|
b, d, 0, 0,
|
|
|
|
0, 0, 1, 0,
|
|
|
|
tx, ty, 0, 1,
|
|
|
|
}
|
2013-12-12 16:31:00 +01:00
|
|
|
C.glUniformMatrix4fv(getUniformLocation(program.native, "modelview_matrix"),
|
2013-10-26 18:56:58 +02:00
|
|
|
1, C.GL_FALSE,
|
|
|
|
(*C.GLfloat)(&glModelviewMatrix[0]))
|
|
|
|
|
2013-12-12 16:31:00 +01:00
|
|
|
C.glUniform1i(getUniformLocation(program.native, "texture"), 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++ {
|
|
|
|
e[i][j] = float32(colorMatrix.Elements[i][j])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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],
|
|
|
|
}
|
2013-12-12 16:31:00 +01:00
|
|
|
C.glUniformMatrix4fv(getUniformLocation(program.native, "color_matrix"),
|
2013-10-26 18:56:58 +02:00
|
|
|
1, C.GL_FALSE, (*C.GLfloat)(&glColorMatrix[0]))
|
|
|
|
|
|
|
|
glColorMatrixTranslation := [...]float32{
|
|
|
|
e[0][4], e[1][4], e[2][4], e[3][4],
|
|
|
|
}
|
2013-12-12 16:31:00 +01:00
|
|
|
C.glUniform4fv(getUniformLocation(program.native, "color_matrix_translation"),
|
2013-10-26 18:56:58 +02:00
|
|
|
1, (*C.GLfloat)(&glColorMatrixTranslation[0]))
|
|
|
|
|
2013-12-12 16:31:00 +01:00
|
|
|
return program.native
|
2013-10-26 18:56:58 +02:00
|
|
|
}
|