ebiten/internal/opengl/context_js.go

409 lines
12 KiB
Go
Raw Normal View History

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"
2016-06-14 20:35:35 +02:00
2014-12-31 13:55:40 +01:00
"github.com/gopherjs/gopherjs/js"
2017-12-02 08:46:55 +01:00
"github.com/hajimehoshi/ebiten/internal/web"
2014-12-31 13:55:40 +01:00
)
// Note that `type Texture *js.Object` doesn't work.
// There is no way to get the internal object in that case.
type (
Texture interface{}
Framebuffer interface{}
Shader interface{}
Program interface{}
Buffer interface{}
uniformLocation interface{}
)
2015-01-17 04:45:19 +01:00
type attribLocation int
2014-12-31 13:55:40 +01:00
2016-02-26 18:41:38 +01:00
type programID int
2015-01-12 15:16:34 +01:00
var InvalidTexture = Texture((*js.Object)(nil))
func getProgramID(p Program) programID {
return programID(p.(*js.Object).Get("__ebiten_programId").Int())
2015-01-12 15:16:34 +01:00
}
2016-07-03 11:11:37 +02:00
func init() {
// Accessing the prototype is rquired on Safari.
c := js.Global.Get("WebGLRenderingContext").Get("prototype")
2016-07-03 11:11:37 +02:00
VertexShader = ShaderType(c.Get("VERTEX_SHADER").Int())
FragmentShader = ShaderType(c.Get("FRAGMENT_SHADER").Int())
ArrayBuffer = BufferType(c.Get("ARRAY_BUFFER").Int())
ElementArrayBuffer = BufferType(c.Get("ELEMENT_ARRAY_BUFFER").Int())
DynamicDraw = BufferUsage(c.Get("DYNAMIC_DRAW").Int())
StaticDraw = BufferUsage(c.Get("STATIC_DRAW").Int())
Triangles = Mode(c.Get("TRIANGLES").Int())
Lines = Mode(c.Get("LINES").Int())
2016-10-22 07:51:23 +02:00
Short = DataType(c.Get("SHORT").Int())
Float = DataType(c.Get("FLOAT").Int())
2016-07-03 11:11:37 +02:00
zero = operation(c.Get("ZERO").Int())
one = operation(c.Get("ONE").Int())
srcAlpha = operation(c.Get("SRC_ALPHA").Int())
dstAlpha = operation(c.Get("DST_ALPHA").Int())
oneMinusSrcAlpha = operation(c.Get("ONE_MINUS_SRC_ALPHA").Int())
oneMinusDstAlpha = operation(c.Get("ONE_MINUS_DST_ALPHA").Int())
}
2014-12-31 13:55:40 +01:00
type context struct {
gl *js.Object
loseContext *js.Object
2016-05-31 19:33:31 +02:00
lastProgramID programID
2014-12-31 13:55:40 +01:00
}
func Init() error {
2017-12-02 08:46:55 +01:00
if web.IsNodeJS() {
return fmt.Errorf("opengl: Node.js is not supported")
}
if js.Global.Get("WebGLRenderingContext") == js.Undefined {
return fmt.Errorf("opengl: WebGL is not supported")
}
2017-12-02 08:46:55 +01:00
// TODO: Define id?
canvas := js.Global.Get("document").Call("querySelector", "canvas")
attr := map[string]bool{
"alpha": true,
"premultipliedAlpha": true,
}
gl := canvas.Call("getContext", "webgl", attr)
if gl == nil {
gl = canvas.Call("getContext", "experimental-webgl", attr)
if gl == nil {
return fmt.Errorf("opengl: getContext failed")
}
2015-01-27 14:02:23 +01:00
}
2016-07-03 17:55:04 +02:00
c := &Context{}
2015-01-02 07:20:05 +01:00
c.gl = gl
2017-12-02 08:46:55 +01:00
// Getting an extension might fail after the context is lost, so
// it is required to get the extension here.
c.loseContext = gl.Call("getExtension", "WEBGL_lose_context")
if c.loseContext != nil {
// This testing function name is temporary.
js.Global.Set("_ebiten_loseContextForTesting", func() {
c.loseContext.Call("loseContext")
})
}
theContext = c
return nil
2014-12-31 13:55:40 +01:00
}
func (c *Context) Reset() error {
2016-06-09 18:19:10 +02:00
c.locationCache = newLocationCache()
c.lastTexture = nil
c.lastFramebuffer = nil
2016-06-09 18:19:10 +02:00
c.lastViewportWidth = 0
c.lastViewportHeight = 0
c.lastCompositeMode = CompositeModeUnknown
gl := c.gl
gl.Call("enable", gl.Get("BLEND"))
2016-06-09 18:19:10 +02:00
c.BlendFunc(CompositeModeSourceOver)
f := gl.Call("getParameter", gl.Get("FRAMEBUFFER_BINDING"))
c.screenFramebuffer = f
return nil
2016-06-09 18:19:10 +02:00
}
func (c *Context) BlendFunc(mode CompositeMode) {
if c.lastCompositeMode == mode {
2016-02-28 17:44:09 +01:00
return
}
c.lastCompositeMode = mode
2016-07-03 11:11:37 +02:00
s, d := operations(mode)
gl := c.gl
gl.Call("blendFunc", int(s), int(d))
2014-12-31 13:55:40 +01:00
}
func (c *Context) NewTexture(width, height int) (Texture, error) {
2014-12-31 13:55:40 +01:00
gl := c.gl
t := gl.Call("createTexture")
2015-01-04 14:25:58 +01:00
if t == nil {
return nil, errors.New("opengl: glGenTexture failed")
2014-12-31 13:55:40 +01:00
}
gl.Call("pixelStorei", gl.Get("UNPACK_ALIGNMENT"), 4)
c.BindTexture(t)
2014-12-31 13:55:40 +01:00
gl.Call("texParameteri", gl.Get("TEXTURE_2D"), gl.Get("TEXTURE_MAG_FILTER"), gl.Get("NEAREST"))
gl.Call("texParameteri", gl.Get("TEXTURE_2D"), gl.Get("TEXTURE_MIN_FILTER"), gl.Get("NEAREST"))
gl.Call("texParameteri", gl.Get("TEXTURE_2D"), gl.Get("TEXTURE_WRAP_S"), gl.Get("CLAMP_TO_EDGE"))
gl.Call("texParameteri", gl.Get("TEXTURE_2D"), gl.Get("TEXTURE_WRAP_T"), gl.Get("CLAMP_TO_EDGE"))
2014-12-31 13:55:40 +01:00
// void texImage2D(GLenum target, GLint level, GLenum internalformat,
// GLsizei width, GLsizei height, GLint border, GLenum format,
// GLenum type, ArrayBufferView? pixels);
gl.Call("texImage2D", gl.Get("TEXTURE_2D"), 0, gl.Get("RGBA"), width, height, 0, gl.Get("RGBA"), gl.Get("UNSIGNED_BYTE"), nil)
2014-12-31 13:55:40 +01:00
return t, nil
2014-12-31 13:55:40 +01:00
}
func (c *Context) bindFramebufferImpl(f Framebuffer) {
2015-02-19 18:02:23 +01:00
gl := c.gl
gl.Call("bindFramebuffer", gl.Get("FRAMEBUFFER"), f.(*js.Object))
2015-02-19 18:02:23 +01:00
}
func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]byte, error) {
2014-12-31 13:55:40 +01:00
gl := c.gl
2015-02-19 18:02:23 +01:00
c.bindFramebuffer(f)
2014-12-31 13:55:40 +01:00
pixels := js.Global.Get("Uint8Array").New(4 * width * height)
gl.Call("readPixels", 0, 0, width, height, gl.Get("RGBA"), gl.Get("UNSIGNED_BYTE"), pixels)
if e := gl.Call("getError"); e != gl.Get("NO_ERROR") {
2016-02-23 16:31:25 +01:00
return nil, errors.New(fmt.Sprintf("opengl: error: %d", e))
2014-12-31 13:55:40 +01:00
}
return pixels.Interface().([]byte), nil
2014-12-31 13:55:40 +01:00
}
func (c *Context) bindTextureImpl(t Texture) {
2014-12-31 13:55:40 +01:00
gl := c.gl
gl.Call("bindTexture", gl.Get("TEXTURE_2D"), t.(*js.Object))
2014-12-31 13:55:40 +01:00
}
func (c *Context) DeleteTexture(t Texture) {
gl := c.gl
if !gl.Call("isTexture", t.(*js.Object)).Bool() {
return
}
if c.lastTexture == t {
c.lastTexture = nil
}
gl.Call("deleteTexture", t.(*js.Object))
2014-12-31 13:55:40 +01:00
}
2016-06-12 16:54:36 +02:00
func (c *Context) IsTexture(t Texture) bool {
gl := c.gl
return gl.Call("isTexture", t.(*js.Object)).Bool()
2016-06-12 16:54:36 +02:00
}
func (c *Context) TexSubImage2D(p []byte, x, y, width, height int) {
2015-01-02 15:30:22 +01:00
gl := c.gl
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
// GLsizei width, GLsizei height,
// GLenum format, GLenum type, ArrayBufferView? pixels);
gl.Call("texSubImage2D", gl.Get("TEXTURE_2D"), 0, x, y, width, height, gl.Get("RGBA"), gl.Get("UNSIGNED_BYTE"), p)
2015-01-02 15:30:22 +01:00
}
func (c *Context) NewFramebuffer(t Texture) (Framebuffer, error) {
2014-12-31 13:55:40 +01:00
gl := c.gl
f := gl.Call("createFramebuffer")
c.bindFramebuffer(f)
2014-12-31 13:55:40 +01:00
gl.Call("framebufferTexture2D", gl.Get("FRAMEBUFFER"), gl.Get("COLOR_ATTACHMENT0"), gl.Get("TEXTURE_2D"), t.(*js.Object), 0)
if s := gl.Call("checkFramebufferStatus", gl.Get("FRAMEBUFFER")); s != gl.Get("FRAMEBUFFER_COMPLETE") {
return nil, errors.New(fmt.Sprintf("opengl: creating framebuffer failed: %d", s))
2014-12-31 13:55:40 +01:00
}
return f, nil
2014-12-31 13:55:40 +01:00
}
func (c *Context) setViewportImpl(width, height int) {
gl := c.gl
gl.Call("viewport", 0, 0, width, height)
2014-12-31 13:55:40 +01:00
}
func (c *Context) DeleteFramebuffer(f Framebuffer) {
gl := c.gl
if !gl.Call("isFramebuffer", f.(*js.Object)).Bool() {
return
}
2016-07-06 19:08:28 +02:00
// If a framebuffer to be deleted is bound, a newly bound framebuffer
// will be a default framebuffer.
// https://www.khronos.org/opengles/sdk/docs/man/xhtml/glDeleteFramebuffers.xml
if c.lastFramebuffer == f {
c.lastFramebuffer = nil
c.lastViewportWidth = 0
c.lastViewportHeight = 0
}
gl.Call("deleteFramebuffer", f.(*js.Object))
2014-12-31 13:55:40 +01:00
}
func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) {
gl := c.gl
s := gl.Call("createShader", int(shaderType))
2015-01-04 14:25:58 +01:00
if s == nil {
return nil, fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType)
2014-12-31 13:55:40 +01:00
}
gl.Call("shaderSource", s, source)
gl.Call("compileShader", s)
2014-12-31 13:55:40 +01:00
if !gl.Call("getShaderParameter", s, gl.Get("COMPILE_STATUS")).Bool() {
log := gl.Call("getShaderInfoLog", s)
return nil, fmt.Errorf("opengl: shader compile failed: %s", log)
2014-12-31 13:55:40 +01:00
}
2018-02-18 18:20:28 +01:00
return s, nil
2014-12-31 13:55:40 +01:00
}
func (c *Context) DeleteShader(s Shader) {
gl := c.gl
gl.Call("deleteShader", s.(*js.Object))
2014-12-31 13:55:40 +01:00
}
func (c *Context) NewProgram(shaders []Shader) (Program, error) {
gl := c.gl
p := gl.Call("createProgram")
2015-01-04 14:25:58 +01:00
if p == nil {
return nil, errors.New("opengl: glCreateProgram failed")
2014-12-31 13:55:40 +01:00
}
2016-02-26 19:05:12 +01:00
p.Set("__ebiten_programId", c.lastProgramID)
c.lastProgramID++
2014-12-31 13:55:40 +01:00
for _, shader := range shaders {
gl.Call("attachShader", p, shader.(*js.Object))
2014-12-31 13:55:40 +01:00
}
gl.Call("linkProgram", p)
if !gl.Call("getProgramParameter", p, gl.Get("LINK_STATUS")).Bool() {
return nil, errors.New("opengl: program error")
2014-12-31 13:55:40 +01:00
}
2018-02-18 18:20:28 +01:00
return p, nil
2014-12-31 13:55:40 +01:00
}
func (c *Context) UseProgram(p Program) {
gl := c.gl
gl.Call("useProgram", p.(*js.Object))
2014-12-31 13:55:40 +01:00
}
func (c *Context) DeleteProgram(p Program) {
gl := c.gl
if !gl.Call("isProgram", p.(*js.Object)).Bool() {
return
}
gl.Call("deleteProgram", p.(*js.Object))
}
2016-06-03 20:50:28 +02:00
func (c *Context) getUniformLocationImpl(p Program, location string) uniformLocation {
2015-01-12 15:16:34 +01:00
gl := c.gl
return gl.Call("getUniformLocation", p.(*js.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
2016-02-26 19:01:55 +01:00
l := c.locationCache.GetUniformLocation(c, p, location)
gl.Call("uniform1i", l.(*js.Object), v)
2015-01-03 07:52:02 +01:00
}
func (c *Context) UniformFloat(p Program, location string, v float32) {
gl := c.gl
l := c.locationCache.GetUniformLocation(c, p, location)
gl.Call("uniform1f", l.(*js.Object), v)
}
2015-01-03 07:52:02 +01:00
func (c *Context) UniformFloats(p Program, location string, v []float32) {
gl := c.gl
2016-02-26 19:01:55 +01:00
l := c.locationCache.GetUniformLocation(c, p, location)
2015-01-03 07:52:02 +01:00
switch len(v) {
case 2:
gl.Call("uniform2fv", l.(*js.Object), v)
2015-01-03 07:52:02 +01:00
case 4:
gl.Call("uniform4fv", l.(*js.Object), v)
2015-01-03 07:52:02 +01:00
case 16:
gl.Call("uniformMatrix4fv", l.(*js.Object), false, v)
default:
panic("not reached")
2014-12-31 13:55:40 +01:00
}
}
2016-06-03 20:50:28 +02:00
func (c *Context) getAttribLocationImpl(p Program, location string) attribLocation {
2015-01-12 15:16:34 +01:00
gl := c.gl
return attribLocation(gl.Call("getAttribLocation", p.(*js.Object), location).Int())
2015-01-12 15:16:34 +01:00
}
func (c *Context) VertexAttribPointer(p Program, location string, size int, dataType DataType, stride int, offset int) {
2014-12-31 13:55:40 +01:00
gl := c.gl
2016-02-26 19:01:55 +01:00
l := c.locationCache.GetAttribLocation(c, p, location)
gl.Call("vertexAttribPointer", int(l), size, int(dataType), false, stride, offset)
2014-12-31 13:55:40 +01:00
}
func (c *Context) EnableVertexAttribArray(p Program, location string) {
gl := c.gl
2016-02-26 19:01:55 +01:00
l := c.locationCache.GetAttribLocation(c, p, location)
gl.Call("enableVertexAttribArray", int(l))
2014-12-31 13:55:40 +01:00
}
func (c *Context) DisableVertexAttribArray(p Program, location string) {
gl := c.gl
2016-02-26 19:01:55 +01:00
l := c.locationCache.GetAttribLocation(c, p, location)
gl.Call("disableVertexAttribArray", int(l))
2014-12-31 13:55:40 +01:00
}
func (c *Context) NewArrayBuffer(size int) Buffer {
2014-12-31 13:55:40 +01:00
gl := c.gl
b := gl.Call("createBuffer")
gl.Call("bindBuffer", int(ArrayBuffer), b)
gl.Call("bufferData", int(ArrayBuffer), size, int(DynamicDraw))
2018-02-18 18:20:28 +01:00
return b
}
func (c *Context) NewElementArrayBuffer(indices []uint16) Buffer {
gl := c.gl
b := gl.Call("createBuffer")
gl.Call("bindBuffer", int(ElementArrayBuffer), b)
gl.Call("bufferData", int(ElementArrayBuffer), indices, int(StaticDraw))
2018-02-18 18:20:28 +01:00
return b
2015-01-17 04:45:19 +01:00
}
func (c *Context) BindElementArrayBuffer(b Buffer) {
gl := c.gl
gl.Call("bindBuffer", gl.Get("ELEMENT_ARRAY_BUFFER"), b.(*js.Object))
2014-12-31 13:55:40 +01:00
}
func (c *Context) BufferSubData(bufferType BufferType, data []float32) {
2014-12-31 13:55:40 +01:00
gl := c.gl
gl.Call("bufferSubData", int(bufferType), 0, data)
2014-12-31 13:55:40 +01:00
}
func (c *Context) DeleteBuffer(b Buffer) {
gl := c.gl
gl.Call("deleteBuffer", b.(*js.Object))
}
func (c *Context) DrawElements(mode Mode, len int, offsetInBytes int) {
2014-12-31 13:55:40 +01:00
gl := c.gl
gl.Call("drawElements", int(mode), len, gl.Get("UNSIGNED_SHORT"), offsetInBytes)
2014-12-31 13:55:40 +01:00
}
func (c *Context) maxTextureSizeImpl() int {
gl := c.gl
return gl.Call("getParameter", gl.Get("MAX_TEXTURE_SIZE")).Int()
}
func (c *Context) Flush() {
gl := c.gl
gl.Call("flush")
}
2017-01-19 18:20:41 +01:00
func (c *Context) IsContextLost() bool {
2017-01-19 18:20:41 +01:00
gl := c.gl
return gl.Call("isContextLost").Bool()
2017-01-19 18:20:41 +01:00
}
func (c *Context) RestoreContext() {
if c.loseContext != nil {
c.loseContext.Call("restoreContext")
}
}