2014-12-31 13:55:40 +01:00
|
|
|
// Copyright 2014 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// +build js
|
|
|
|
|
|
|
|
package opengl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gopherjs/gopherjs/js"
|
|
|
|
"github.com/gopherjs/webgl"
|
|
|
|
)
|
|
|
|
|
2015-01-15 15:26:05 +01:00
|
|
|
type Texture struct {
|
|
|
|
js.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
type Framebuffer struct {
|
|
|
|
js.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
type Shader struct {
|
|
|
|
js.Object
|
|
|
|
}
|
|
|
|
|
|
|
|
type Program struct {
|
|
|
|
js.Object
|
|
|
|
}
|
|
|
|
|
2015-01-16 16:56:29 +01:00
|
|
|
// TODO: Remove this after the GopherJS bug was fixed (#159)
|
|
|
|
func (p Program) Equals(other Program) bool {
|
|
|
|
return p.Object == other.Object
|
|
|
|
}
|
|
|
|
|
2015-01-15 15:26:05 +01:00
|
|
|
type UniformLocation struct {
|
|
|
|
js.Object
|
|
|
|
}
|
|
|
|
|
2015-01-03 07:56:54 +01:00
|
|
|
type AttribLocation int
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2015-01-12 15:16:34 +01:00
|
|
|
type ProgramID int
|
|
|
|
|
|
|
|
func GetProgramID(p Program) ProgramID {
|
|
|
|
return ProgramID(p.Get("__ebiten_programId").Int())
|
|
|
|
}
|
|
|
|
|
2014-12-31 13:55:40 +01:00
|
|
|
type context struct {
|
|
|
|
gl *webgl.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewContext(gl *webgl.Context) *Context {
|
|
|
|
c := &Context{
|
|
|
|
Nearest: FilterType(gl.NEAREST),
|
|
|
|
Linear: FilterType(gl.LINEAR),
|
|
|
|
VertexShader: ShaderType(gl.VERTEX_SHADER),
|
|
|
|
FragmentShader: ShaderType(gl.FRAGMENT_SHADER),
|
|
|
|
ArrayBuffer: BufferType(gl.ARRAY_BUFFER),
|
|
|
|
ElementArrayBuffer: BufferType(gl.ELEMENT_ARRAY_BUFFER),
|
|
|
|
DynamicDraw: BufferUsageType(gl.DYNAMIC_DRAW),
|
|
|
|
StaticDraw: BufferUsageType(gl.STATIC_DRAW),
|
|
|
|
}
|
2015-01-02 07:20:05 +01:00
|
|
|
c.gl = gl
|
2014-12-31 13:55:40 +01:00
|
|
|
c.init()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) init() {
|
|
|
|
gl := c.gl
|
|
|
|
// Textures' pixel formats are alpha premultiplied.
|
|
|
|
gl.Enable(gl.BLEND)
|
|
|
|
gl.BlendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) NewTexture(width, height int, pixels []uint8, filter FilterType) (Texture, error) {
|
|
|
|
gl := c.gl
|
|
|
|
t := gl.CreateTexture()
|
2015-01-04 14:25:58 +01:00
|
|
|
if t == nil {
|
2015-01-15 15:26:05 +01:00
|
|
|
return Texture{nil}, errors.New("glGenTexture failed")
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
|
|
|
|
gl.BindTexture(gl.TEXTURE_2D, t)
|
|
|
|
|
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, int(filter))
|
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, int(filter))
|
|
|
|
|
|
|
|
// void texImage2D(GLenum target, GLint level, GLenum internalformat,
|
|
|
|
// GLsizei width, GLsizei height, GLint border, GLenum format,
|
|
|
|
// GLenum type, ArrayBufferView? pixels);
|
2015-01-02 15:30:22 +01:00
|
|
|
var p interface{}
|
|
|
|
if pixels != nil {
|
|
|
|
p = pixels
|
|
|
|
}
|
|
|
|
gl.Call("texImage2D", gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, p)
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2015-01-15 15:26:05 +01:00
|
|
|
return Texture{t}, nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) TexturePixels(t Texture, width, height int) ([]uint8, error) {
|
|
|
|
gl := c.gl
|
2015-01-02 17:14:36 +01:00
|
|
|
gl.Flush()
|
2014-12-31 13:55:40 +01:00
|
|
|
// TODO: Use glGetTexLevelParameteri and GL_TEXTURE_WIDTH?
|
|
|
|
pixels := js.Global.Get("Uint8Array").New(4 * width * height)
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.BindTexture(gl.TEXTURE_2D, t.Object)
|
2014-12-31 13:55:40 +01:00
|
|
|
gl.ReadPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
|
|
|
|
if e := gl.GetError(); e != gl.NO_ERROR {
|
|
|
|
return nil, errors.New(fmt.Sprintf("gl error: %d", e))
|
|
|
|
}
|
|
|
|
return pixels.Interface().([]uint8), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) BindTexture(t Texture) {
|
|
|
|
gl := c.gl
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.BindTexture(gl.TEXTURE_2D, t.Object)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DeleteTexture(t Texture) {
|
|
|
|
gl := c.gl
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.DeleteTexture(t.Object)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-02 15:30:22 +01:00
|
|
|
func (c *Context) GlslHighpSupported() bool {
|
|
|
|
gl := c.gl
|
2015-01-14 15:38:25 +01:00
|
|
|
// headless-gl library may not define getShaderPrecisionFormat.
|
2015-01-13 16:18:36 +01:00
|
|
|
if gl.Get("getShaderPrecisionFormat") == js.Undefined {
|
|
|
|
return false
|
|
|
|
}
|
2015-01-02 15:30:22 +01:00
|
|
|
return gl.Call("getShaderPrecisionFormat", gl.FRAGMENT_SHADER, gl.HIGH_FLOAT).Get("precision").Int() != 0
|
|
|
|
}
|
|
|
|
|
2015-01-15 15:26:05 +01:00
|
|
|
func (c *Context) NewFramebuffer(t Texture) (Framebuffer, error) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
|
|
|
f := gl.CreateFramebuffer()
|
|
|
|
gl.BindFramebuffer(gl.FRAMEBUFFER, f)
|
|
|
|
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, t.Object, 0)
|
2014-12-31 13:55:40 +01:00
|
|
|
if gl.CheckFramebufferStatus(gl.FRAMEBUFFER) != gl.FRAMEBUFFER_COMPLETE {
|
2015-01-15 15:26:05 +01:00
|
|
|
return Framebuffer{nil}, errors.New("creating framebuffer failed")
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-15 15:26:05 +01:00
|
|
|
return Framebuffer{f}, nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-03 08:21:09 +01:00
|
|
|
var lastFramebuffer Framebuffer
|
|
|
|
|
2014-12-31 13:55:40 +01:00
|
|
|
func (c *Context) SetViewport(f Framebuffer, width, height int) error {
|
|
|
|
gl := c.gl
|
2015-01-16 16:56:29 +01:00
|
|
|
// TODO: Fix this after the GopherJS bug was fixed (#159)
|
|
|
|
if lastFramebuffer.Object != f.Object {
|
2015-01-03 08:21:09 +01:00
|
|
|
gl.Flush()
|
|
|
|
lastFramebuffer = f
|
|
|
|
}
|
2015-01-15 15:26:05 +01:00
|
|
|
if f.Object != nil {
|
|
|
|
gl.BindFramebuffer(gl.FRAMEBUFFER, f.Object)
|
2015-01-02 15:52:40 +01:00
|
|
|
} else {
|
2015-01-04 14:25:58 +01:00
|
|
|
gl.BindFramebuffer(gl.FRAMEBUFFER, nil)
|
2015-01-02 15:52:40 +01:00
|
|
|
}
|
2015-01-02 18:01:30 +01:00
|
|
|
// gl.CheckFramebufferStatus might cause a performance problem. Don't call this here.
|
2014-12-31 13:55:40 +01:00
|
|
|
gl.Viewport(0, 0, width, height)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-15 15:42:08 +01:00
|
|
|
func (c *Context) FillFramebuffer(r, g, b, a float64) error {
|
2015-01-15 15:26:05 +01:00
|
|
|
// TODO: Use f?
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
|
|
|
gl.ClearColor(float32(r), float32(g), float32(b), float32(a))
|
|
|
|
gl.Clear(gl.COLOR_BUFFER_BIT)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DeleteFramebuffer(f Framebuffer) {
|
|
|
|
gl := c.gl
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.DeleteFramebuffer(f.Object)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) {
|
|
|
|
gl := c.gl
|
|
|
|
s := gl.CreateShader(int(shaderType))
|
2015-01-04 14:25:58 +01:00
|
|
|
if s == nil {
|
2014-12-31 13:55:40 +01:00
|
|
|
println(gl.GetError())
|
2015-01-15 15:26:05 +01:00
|
|
|
return Shader{nil}, errors.New("glCreateShader failed")
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gl.ShaderSource(s, source)
|
|
|
|
gl.CompileShader(s)
|
|
|
|
|
|
|
|
if !gl.GetShaderParameterb(s, gl.COMPILE_STATUS) {
|
|
|
|
log := gl.GetShaderInfoLog(s)
|
2015-01-15 15:26:05 +01:00
|
|
|
return Shader{nil}, errors.New(fmt.Sprintf("shader compile failed: %s", log))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2015-01-15 15:26:05 +01:00
|
|
|
return Shader{s}, nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DeleteShader(s Shader) {
|
|
|
|
gl := c.gl
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.DeleteShader(s.Object)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-12 15:16:34 +01:00
|
|
|
var lastProgramID ProgramID = 0
|
|
|
|
|
2014-12-31 13:55:40 +01:00
|
|
|
func (c *Context) NewProgram(shaders []Shader) (Program, error) {
|
|
|
|
gl := c.gl
|
|
|
|
p := gl.CreateProgram()
|
2015-01-04 14:25:58 +01:00
|
|
|
if p == nil {
|
2015-01-15 15:26:05 +01:00
|
|
|
return Program{nil}, errors.New("glCreateProgram failed")
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2015-01-12 15:16:34 +01:00
|
|
|
p.Set("__ebiten_programId", lastProgramID)
|
|
|
|
lastProgramID++
|
2014-12-31 13:55:40 +01:00
|
|
|
|
|
|
|
for _, shader := range shaders {
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.AttachShader(p, shader.Object)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
gl.LinkProgram(p)
|
|
|
|
if !gl.GetProgramParameterb(p, gl.LINK_STATUS) {
|
2015-01-15 15:26:05 +01:00
|
|
|
return Program{nil}, errors.New("program error")
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2015-01-15 15:26:05 +01:00
|
|
|
return Program{p}, nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) UseProgram(p Program) {
|
|
|
|
gl := c.gl
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.UseProgram(p.Object)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-12 15:16:34 +01:00
|
|
|
func (c *Context) GetUniformLocation(p Program, location string) UniformLocation {
|
|
|
|
gl := c.gl
|
2015-01-15 15:26:05 +01:00
|
|
|
return UniformLocation{gl.GetUniformLocation(p.Object, location)}
|
2015-01-12 15:16:34 +01:00
|
|
|
}
|
|
|
|
|
2015-01-03 07:52:02 +01:00
|
|
|
func (c *Context) UniformInt(p Program, location string, v int) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2015-01-12 15:16:34 +01:00
|
|
|
l := GetUniformLocation(c, p, location)
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.Uniform1i(l.Object, v)
|
2015-01-03 07:52:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) UniformFloats(p Program, location string, v []float32) {
|
|
|
|
gl := c.gl
|
2015-01-12 15:16:34 +01:00
|
|
|
l := GetUniformLocation(c, p, location)
|
2015-01-03 07:52:02 +01:00
|
|
|
switch len(v) {
|
|
|
|
case 4:
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.Call("uniform4fv", l.Object, v)
|
2015-01-03 07:52:02 +01:00
|
|
|
case 16:
|
2015-01-15 15:26:05 +01:00
|
|
|
gl.UniformMatrix4fv(l.Object, false, v)
|
2015-01-03 07:21:47 +01:00
|
|
|
default:
|
|
|
|
panic("not reach")
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 15:16:34 +01:00
|
|
|
func (c *Context) GetAttribLocation(p Program, location string) AttribLocation {
|
|
|
|
gl := c.gl
|
2015-01-15 15:26:05 +01:00
|
|
|
return AttribLocation(gl.GetAttribLocation(p.Object, location))
|
2015-01-12 15:16:34 +01:00
|
|
|
}
|
|
|
|
|
2015-01-16 02:37:26 +01:00
|
|
|
func (c *Context) VertexAttribPointer(p Program, location string, signed bool, normalize bool, stride int, size int, v uintptr) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2015-01-12 15:16:34 +01:00
|
|
|
l := GetAttribLocation(c, p, location)
|
2015-01-16 02:37:26 +01:00
|
|
|
t := gl.SHORT
|
|
|
|
if !signed {
|
|
|
|
t = gl.UNSIGNED_SHORT
|
|
|
|
}
|
|
|
|
gl.VertexAttribPointer(int(l), size, t, normalize, stride, int(v))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) EnableVertexAttribArray(p Program, location string) {
|
|
|
|
gl := c.gl
|
2015-01-12 15:16:34 +01:00
|
|
|
l := GetAttribLocation(c, p, location)
|
2015-01-03 07:56:54 +01:00
|
|
|
gl.EnableVertexAttribArray(int(l))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DisableVertexAttribArray(p Program, location string) {
|
|
|
|
gl := c.gl
|
2015-01-12 15:16:34 +01:00
|
|
|
l := GetAttribLocation(c, p, location)
|
2015-01-03 07:56:54 +01:00
|
|
|
gl.DisableVertexAttribArray(int(l))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) NewBuffer(bufferType BufferType, v interface{}, bufferUsageType BufferUsageType) {
|
|
|
|
gl := c.gl
|
|
|
|
b := gl.CreateBuffer()
|
|
|
|
gl.BindBuffer(int(bufferType), b)
|
|
|
|
gl.BufferData(int(bufferType), v, int(bufferUsageType))
|
|
|
|
}
|
|
|
|
|
2015-01-16 02:37:26 +01:00
|
|
|
func (c *Context) BufferSubData(bufferType BufferType, data []int16) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
|
|
|
gl.BufferSubData(int(bufferType), 0, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DrawElements(len int) {
|
|
|
|
gl := c.gl
|
|
|
|
gl.DrawElements(gl.TRIANGLES, len, gl.UNSIGNED_SHORT, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) Flush() {
|
|
|
|
gl := c.gl
|
|
|
|
gl.Flush()
|
|
|
|
}
|