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"
|
2019-04-30 19:15:28 +02:00
|
|
|
"syscall/js"
|
2018-10-28 12:42:57 +01:00
|
|
|
|
2019-06-25 17:43:09 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
2019-06-02 19:37:57 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/jsutil"
|
2019-05-26 12:08:46 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/web"
|
2014-12-31 13:55:40 +01:00
|
|
|
)
|
|
|
|
|
2018-02-18 18:03:01 +01:00
|
|
|
type (
|
2018-11-04 11:46:20 +01:00
|
|
|
textureNative js.Value
|
2018-11-04 09:43:26 +01:00
|
|
|
framebufferNative js.Value
|
|
|
|
shader js.Value
|
|
|
|
buffer js.Value
|
|
|
|
uniformLocation js.Value
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2018-06-21 18:45:25 +02:00
|
|
|
attribLocation int
|
|
|
|
programID int
|
2018-10-29 17:52:59 +01:00
|
|
|
program struct {
|
2018-06-21 18:45:25 +02:00
|
|
|
value js.Value
|
|
|
|
id programID
|
|
|
|
}
|
|
|
|
)
|
2015-01-12 15:16:34 +01:00
|
|
|
|
2019-12-18 16:03:35 +01:00
|
|
|
func (t textureNative) equal(rhs textureNative) bool {
|
|
|
|
return jsutil.Equal(js.Value(t), js.Value(rhs))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f framebufferNative) equal(rhs framebufferNative) bool {
|
|
|
|
return jsutil.Equal(js.Value(f), js.Value(rhs))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s shader) equal(rhs shader) bool {
|
|
|
|
return jsutil.Equal(js.Value(s), js.Value(rhs))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b buffer) equal(rhs buffer) bool {
|
|
|
|
return jsutil.Equal(js.Value(b), js.Value(rhs))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u uniformLocation) equal(rhs uniformLocation) bool {
|
|
|
|
return jsutil.Equal(js.Value(u), js.Value(rhs))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p program) equal(rhs program) bool {
|
|
|
|
return jsutil.Equal(p.value, rhs.value) && p.id == rhs.id
|
|
|
|
}
|
|
|
|
|
2018-11-04 11:46:20 +01:00
|
|
|
var InvalidTexture = textureNative(js.Null())
|
2016-06-17 21:46:33 +02:00
|
|
|
|
2020-05-27 04:07:21 +02:00
|
|
|
var invalidUniform = uniformLocation(js.Null())
|
|
|
|
|
2018-10-29 17:52:59 +01:00
|
|
|
func getProgramID(p program) programID {
|
2018-06-21 18:45:25 +02:00
|
|
|
return p.id
|
2015-01-12 15:16:34 +01:00
|
|
|
}
|
|
|
|
|
2018-06-18 21:02:56 +02:00
|
|
|
var (
|
2020-01-01 19:58:49 +01:00
|
|
|
vertexShader shaderType
|
|
|
|
fragmentShader shaderType
|
|
|
|
arrayBuffer bufferType
|
|
|
|
elementArrayBuffer bufferType
|
|
|
|
dynamicDraw bufferUsage
|
|
|
|
streamDraw bufferUsage
|
|
|
|
pixelUnpackBuffer bufferType
|
|
|
|
short dataType
|
|
|
|
float dataType
|
|
|
|
|
|
|
|
zero operation
|
|
|
|
one operation
|
|
|
|
srcAlpha operation
|
|
|
|
dstAlpha operation
|
|
|
|
oneMinusSrcAlpha operation
|
|
|
|
oneMinusDstAlpha operation
|
|
|
|
|
|
|
|
blend js.Value
|
|
|
|
clampToEdge js.Value
|
|
|
|
compileStatus js.Value
|
|
|
|
colorAttachment0 js.Value
|
|
|
|
framebuffer_ js.Value
|
|
|
|
framebufferBinding js.Value
|
|
|
|
framebufferComplete js.Value
|
|
|
|
highFloat js.Value
|
|
|
|
linkStatus js.Value
|
|
|
|
maxTextureSize js.Value
|
|
|
|
nearest js.Value
|
|
|
|
noError js.Value
|
|
|
|
rgba js.Value
|
|
|
|
texture2d js.Value
|
|
|
|
textureMagFilter js.Value
|
|
|
|
textureMinFilter js.Value
|
|
|
|
textureWrapS js.Value
|
|
|
|
textureWrapT js.Value
|
|
|
|
triangles js.Value
|
|
|
|
unpackAlignment js.Value
|
|
|
|
unsignedByte js.Value
|
|
|
|
unsignedShort js.Value
|
|
|
|
|
2020-05-17 16:04:13 +02:00
|
|
|
texture0 int
|
|
|
|
|
2020-01-01 19:58:49 +01:00
|
|
|
isWebGL2Available bool
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-11-25 17:17:22 +01:00
|
|
|
// Accessing the prototype is rquired on Safari.
|
2020-01-01 19:58:49 +01:00
|
|
|
var contextPrototype js.Value
|
|
|
|
if !jsutil.Equal(js.Global().Get("WebGL2RenderingContext"), js.Undefined()) {
|
|
|
|
contextPrototype = js.Global().Get("WebGL2RenderingContext").Get("prototype")
|
|
|
|
isWebGL2Available = true
|
|
|
|
} else {
|
|
|
|
contextPrototype = js.Global().Get("WebGLRenderingContext").Get("prototype")
|
|
|
|
}
|
2018-12-25 17:51:04 +01:00
|
|
|
|
2020-01-01 19:58:49 +01:00
|
|
|
vertexShader = shaderType(contextPrototype.Get("VERTEX_SHADER").Int())
|
|
|
|
fragmentShader = shaderType(contextPrototype.Get("FRAGMENT_SHADER").Int())
|
|
|
|
arrayBuffer = bufferType(contextPrototype.Get("ARRAY_BUFFER").Int())
|
2018-12-25 17:51:04 +01:00
|
|
|
elementArrayBuffer = bufferType(contextPrototype.Get("ELEMENT_ARRAY_BUFFER").Int())
|
2020-01-01 19:58:49 +01:00
|
|
|
dynamicDraw = bufferUsage(contextPrototype.Get("DYNAMIC_DRAW").Int())
|
|
|
|
streamDraw = bufferUsage(contextPrototype.Get("STREAM_DRAW").Int())
|
|
|
|
short = dataType(contextPrototype.Get("SHORT").Int())
|
|
|
|
float = dataType(contextPrototype.Get("FLOAT").Int())
|
|
|
|
|
|
|
|
zero = operation(contextPrototype.Get("ZERO").Int())
|
|
|
|
one = operation(contextPrototype.Get("ONE").Int())
|
|
|
|
srcAlpha = operation(contextPrototype.Get("SRC_ALPHA").Int())
|
|
|
|
dstAlpha = operation(contextPrototype.Get("DST_ALPHA").Int())
|
2018-12-25 17:51:04 +01:00
|
|
|
oneMinusSrcAlpha = operation(contextPrototype.Get("ONE_MINUS_SRC_ALPHA").Int())
|
|
|
|
oneMinusDstAlpha = operation(contextPrototype.Get("ONE_MINUS_DST_ALPHA").Int())
|
|
|
|
|
2020-01-01 19:58:49 +01:00
|
|
|
blend = contextPrototype.Get("BLEND")
|
|
|
|
clampToEdge = contextPrototype.Get("CLAMP_TO_EDGE")
|
|
|
|
compileStatus = contextPrototype.Get("COMPILE_STATUS")
|
|
|
|
colorAttachment0 = contextPrototype.Get("COLOR_ATTACHMENT0")
|
|
|
|
framebuffer_ = contextPrototype.Get("FRAMEBUFFER")
|
|
|
|
framebufferBinding = contextPrototype.Get("FRAMEBUFFER_BINDING")
|
2018-12-25 17:51:04 +01:00
|
|
|
framebufferComplete = contextPrototype.Get("FRAMEBUFFER_COMPLETE")
|
2020-01-01 19:58:49 +01:00
|
|
|
highFloat = contextPrototype.Get("HIGH_FLOAT")
|
|
|
|
linkStatus = contextPrototype.Get("LINK_STATUS")
|
|
|
|
maxTextureSize = contextPrototype.Get("MAX_TEXTURE_SIZE")
|
|
|
|
nearest = contextPrototype.Get("NEAREST")
|
|
|
|
noError = contextPrototype.Get("NO_ERROR")
|
|
|
|
rgba = contextPrototype.Get("RGBA")
|
2020-05-17 16:04:13 +02:00
|
|
|
texture0 = contextPrototype.Get("TEXTURE0").Int()
|
2020-01-01 19:58:49 +01:00
|
|
|
texture2d = contextPrototype.Get("TEXTURE_2D")
|
|
|
|
textureMagFilter = contextPrototype.Get("TEXTURE_MAG_FILTER")
|
|
|
|
textureMinFilter = contextPrototype.Get("TEXTURE_MIN_FILTER")
|
|
|
|
textureWrapS = contextPrototype.Get("TEXTURE_WRAP_S")
|
|
|
|
textureWrapT = contextPrototype.Get("TEXTURE_WRAP_T")
|
|
|
|
triangles = contextPrototype.Get("TRIANGLES")
|
|
|
|
unpackAlignment = contextPrototype.Get("UNPACK_ALIGNMENT")
|
|
|
|
unsignedByte = contextPrototype.Get("UNSIGNED_BYTE")
|
|
|
|
unsignedShort = contextPrototype.Get("UNSIGNED_SHORT")
|
|
|
|
|
|
|
|
if isWebGL2Available {
|
|
|
|
pixelUnpackBuffer = bufferType(contextPrototype.Get("PIXEL_UNPACK_BUFFER").Int())
|
|
|
|
}
|
|
|
|
}
|
2016-07-03 11:11:37 +02:00
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
type contextImpl struct {
|
2018-05-23 20:04:56 +02:00
|
|
|
gl js.Value
|
2016-05-31 19:33:31 +02:00
|
|
|
lastProgramID programID
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) ensureGL() {
|
2019-12-18 16:03:35 +01:00
|
|
|
if !jsutil.Equal(c.gl, js.Value{}) {
|
2018-11-04 15:32:18 +01:00
|
|
|
return
|
2018-05-21 18:41:16 +02:00
|
|
|
}
|
|
|
|
|
2017-12-02 08:46:55 +01:00
|
|
|
// TODO: Define id?
|
2018-06-29 17:02:15 +02:00
|
|
|
canvas := js.Global().Get("document").Call("querySelector", "canvas")
|
|
|
|
attr := js.Global().Get("Object").New()
|
2018-05-23 20:04:56 +02:00
|
|
|
attr.Set("alpha", true)
|
|
|
|
attr.Set("premultipliedAlpha", true)
|
2020-01-01 19:58:49 +01:00
|
|
|
|
|
|
|
var gl js.Value
|
|
|
|
if isWebGL2Available {
|
|
|
|
gl = canvas.Call("getContext", "webgl2", attr)
|
|
|
|
} else {
|
|
|
|
gl = canvas.Call("getContext", "webgl", attr)
|
2019-12-18 16:03:35 +01:00
|
|
|
if jsutil.Equal(gl, js.Null()) {
|
2020-01-01 19:58:49 +01:00
|
|
|
gl = canvas.Call("getContext", "experimental-webgl", attr)
|
|
|
|
if jsutil.Equal(gl, js.Null()) {
|
|
|
|
panic("opengl: getContext failed")
|
|
|
|
}
|
2018-05-21 18:41:16 +02:00
|
|
|
}
|
2015-01-27 14:02:23 +01:00
|
|
|
}
|
2018-11-04 15:32:18 +01:00
|
|
|
|
2015-01-02 07:20:05 +01:00
|
|
|
c.gl = gl
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) reset() error {
|
2016-06-09 18:19:10 +02:00
|
|
|
c.locationCache = newLocationCache()
|
2018-11-04 11:46:20 +01:00
|
|
|
c.lastTexture = textureNative(js.Null())
|
2018-11-04 09:43:26 +01:00
|
|
|
c.lastFramebuffer = framebufferNative(js.Null())
|
2016-06-09 18:19:10 +02:00
|
|
|
c.lastViewportWidth = 0
|
|
|
|
c.lastViewportHeight = 0
|
2019-06-25 17:43:09 +02:00
|
|
|
c.lastCompositeMode = driver.CompositeModeUnknown
|
2018-11-04 15:32:18 +01:00
|
|
|
|
2019-01-29 17:44:57 +01:00
|
|
|
c.gl = js.Value{}
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2019-01-29 17:44:57 +01:00
|
|
|
if c.gl.Call("isContextLost").Bool() {
|
2020-04-12 13:01:15 +02:00
|
|
|
return driver.GraphicsNotReady
|
2019-01-29 17:44:57 +01:00
|
|
|
}
|
2016-06-09 18:19:10 +02:00
|
|
|
gl := c.gl
|
2018-06-18 21:02:56 +02:00
|
|
|
gl.Call("enable", blend)
|
2019-06-25 17:43:09 +02:00
|
|
|
c.blendFunc(driver.CompositeModeSourceOver)
|
2018-06-18 21:02:56 +02:00
|
|
|
f := gl.Call("getParameter", framebufferBinding)
|
2018-11-04 09:43:26 +01:00
|
|
|
c.screenFramebuffer = framebufferNative(f)
|
2016-06-17 23:25:40 +02:00
|
|
|
return nil
|
2016-06-09 18:19:10 +02:00
|
|
|
}
|
|
|
|
|
2019-06-25 17:43:09 +02:00
|
|
|
func (c *context) blendFunc(mode driver.CompositeMode) {
|
2016-02-29 18:16:32 +01:00
|
|
|
if c.lastCompositeMode == mode {
|
2016-02-28 17:44:09 +01:00
|
|
|
return
|
|
|
|
}
|
2016-02-29 18:16:32 +01:00
|
|
|
c.lastCompositeMode = mode
|
2018-10-28 12:42:57 +01:00
|
|
|
s, d := mode.Operations()
|
|
|
|
s2, d2 := convertOperation(s), convertOperation(d)
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2016-02-28 16:25:16 +01:00
|
|
|
gl := c.gl
|
2018-10-28 12:42:57 +01:00
|
|
|
gl.Call("blendFunc", int(s2), int(d2))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newTexture(width, height int) (textureNative, error) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-05-21 18:41:16 +02:00
|
|
|
t := gl.Call("createTexture")
|
2019-12-18 16:03:35 +01:00
|
|
|
if jsutil.Equal(t, js.Null()) {
|
2018-11-04 11:46:20 +01:00
|
|
|
return textureNative(js.Null()), errors.New("opengl: glGenTexture failed")
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2018-06-18 21:02:56 +02:00
|
|
|
gl.Call("pixelStorei", unpackAlignment, 4)
|
2018-11-04 11:46:20 +01:00
|
|
|
c.bindTexture(textureNative(t))
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2018-06-18 21:02:56 +02:00
|
|
|
gl.Call("texParameteri", texture2d, textureMagFilter, nearest)
|
|
|
|
gl.Call("texParameteri", texture2d, textureMinFilter, nearest)
|
|
|
|
gl.Call("texParameteri", texture2d, textureWrapS, clampToEdge)
|
|
|
|
gl.Call("texParameteri", texture2d, textureWrapT, clampToEdge)
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2018-09-28 19:20:02 +02:00
|
|
|
// Firefox warns the usage of textures without specifying pixels (#629)
|
|
|
|
//
|
|
|
|
// Error: WebGL warning: drawElements: This operation requires zeroing texture data. This is slow.
|
|
|
|
//
|
|
|
|
// In Ebiten, textures are filled with pixels laster by the filter that ignores destination, so it is fine
|
|
|
|
// to leave textures as uninitialized here. Rather, extra memory allocating for initialization should be
|
|
|
|
// avoided.
|
2018-06-18 21:02:56 +02:00
|
|
|
gl.Call("texImage2D", texture2d, 0, rgba, width, height, 0, rgba, unsignedByte, nil)
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2018-11-04 11:46:20 +01:00
|
|
|
return textureNative(t), nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) bindFramebufferImpl(f framebufferNative) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2015-02-19 18:02:23 +01:00
|
|
|
gl := c.gl
|
2018-11-04 11:49:09 +01:00
|
|
|
gl.Call("bindFramebuffer", framebuffer_, js.Value(f))
|
2015-02-19 18:02:23 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) framebufferPixels(f *framebuffer, width, height int) ([]byte, error) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2015-01-17 10:22:58 +01:00
|
|
|
|
2018-11-04 09:28:33 +01:00
|
|
|
c.bindFramebuffer(f.native)
|
2015-01-17 10:22:58 +01:00
|
|
|
|
2019-10-27 14:40:36 +01:00
|
|
|
p := jsutil.TemporaryUint8Array(4 * width * height)
|
2018-07-05 19:34:17 +02:00
|
|
|
gl.Call("readPixels", 0, 0, width, height, rgba, unsignedByte, p)
|
2019-06-02 19:37:57 +02:00
|
|
|
|
|
|
|
return jsutil.Uint8ArrayToSlice(p), nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 16:04:13 +02:00
|
|
|
func (c *context) activeTexture(idx int) {
|
|
|
|
c.ensureGL()
|
|
|
|
gl := c.gl
|
|
|
|
gl.Call("activeTexture", texture0+idx)
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) bindTextureImpl(t textureNative) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-06-18 21:02:56 +02:00
|
|
|
gl.Call("bindTexture", texture2d, js.Value(t))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteTexture(t textureNative) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-05-23 20:08:00 +02:00
|
|
|
if !gl.Call("isTexture", js.Value(t)).Bool() {
|
2016-06-12 16:19:01 +02:00
|
|
|
return
|
|
|
|
}
|
2019-12-18 16:03:35 +01:00
|
|
|
if c.lastTexture.equal(t) {
|
2018-11-04 11:46:20 +01:00
|
|
|
c.lastTexture = textureNative(js.Null())
|
2016-07-09 22:04:25 +02:00
|
|
|
}
|
2018-05-23 20:08:00 +02:00
|
|
|
gl.Call("deleteTexture", js.Value(t))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) isTexture(t textureNative) bool {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2016-06-12 16:54:36 +02:00
|
|
|
gl := c.gl
|
2018-05-23 20:08:00 +02:00
|
|
|
return gl.Call("isTexture", js.Value(t)).Bool()
|
2016-06-12 16:54:36 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newFramebuffer(t textureNative) (framebufferNative, error) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-05-21 18:41:16 +02:00
|
|
|
f := gl.Call("createFramebuffer")
|
2018-11-04 09:43:26 +01:00
|
|
|
c.bindFramebuffer(framebufferNative(f))
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2018-11-04 11:49:09 +01:00
|
|
|
gl.Call("framebufferTexture2D", framebuffer_, colorAttachment0, texture2d, js.Value(t), 0)
|
|
|
|
if s := gl.Call("checkFramebufferStatus", framebuffer_); s.Int() != framebufferComplete.Int() {
|
2018-11-04 09:43:26 +01:00
|
|
|
return framebufferNative(js.Null()), errors.New(fmt.Sprintf("opengl: creating framebuffer failed: %d", s.Int()))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-04 09:43:26 +01:00
|
|
|
return framebufferNative(f), nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) setViewportImpl(width, height int) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2016-06-18 15:47:34 +02:00
|
|
|
gl := c.gl
|
2018-05-21 18:41:16 +02:00
|
|
|
gl.Call("viewport", 0, 0, width, height)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteFramebuffer(f framebufferNative) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-05-23 20:08:00 +02:00
|
|
|
if !gl.Call("isFramebuffer", js.Value(f)).Bool() {
|
2016-06-12 16:19:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-07-06 19:08:28 +02:00
|
|
|
// If a framebuffer to be deleted is bound, a newly bound framebuffer
|
2016-06-03 20:40:56 +02:00
|
|
|
// will be a default framebuffer.
|
|
|
|
// https://www.khronos.org/opengles/sdk/docs/man/xhtml/glDeleteFramebuffers.xml
|
2019-12-18 16:03:35 +01:00
|
|
|
if c.lastFramebuffer.equal(f) {
|
2018-11-04 09:43:26 +01:00
|
|
|
c.lastFramebuffer = framebufferNative(js.Null())
|
2016-06-05 01:16:16 +02:00
|
|
|
c.lastViewportWidth = 0
|
|
|
|
c.lastViewportHeight = 0
|
2016-06-03 20:40:56 +02:00
|
|
|
}
|
2018-05-23 20:08:00 +02:00
|
|
|
gl.Call("deleteFramebuffer", js.Value(f))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newShader(shaderType shaderType, source string) (shader, error) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-05-21 18:41:16 +02:00
|
|
|
s := gl.Call("createShader", int(shaderType))
|
2019-12-18 16:03:35 +01:00
|
|
|
if jsutil.Equal(s, js.Null()) {
|
2018-10-29 17:52:59 +01:00
|
|
|
return shader(js.Null()), fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 20:37:14 +02:00
|
|
|
gl.Call("shaderSource", js.Value(s), source)
|
|
|
|
gl.Call("compileShader", js.Value(s))
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2018-06-18 21:02:56 +02:00
|
|
|
if !gl.Call("getShaderParameter", js.Value(s), compileStatus).Bool() {
|
2018-05-23 20:37:14 +02:00
|
|
|
log := gl.Call("getShaderInfoLog", js.Value(s))
|
2018-10-29 17:52:59 +01:00
|
|
|
return shader(js.Null()), fmt.Errorf("opengl: shader compile failed: %s", log)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2018-10-29 17:52:59 +01:00
|
|
|
return shader(s), nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteShader(s shader) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-05-23 20:37:14 +02:00
|
|
|
gl.Call("deleteShader", js.Value(s))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 06:03:21 +01:00
|
|
|
func (c *context) newProgram(shaders []shader, attributes []string) (program, error) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-06-21 18:45:25 +02:00
|
|
|
v := gl.Call("createProgram")
|
2019-12-18 16:03:35 +01:00
|
|
|
if jsutil.Equal(v, js.Null()) {
|
2018-10-29 17:52:59 +01:00
|
|
|
return program{}, errors.New("opengl: glCreateProgram failed")
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, shader := range shaders {
|
2018-06-21 18:45:25 +02:00
|
|
|
gl.Call("attachShader", v, js.Value(shader))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2019-02-16 06:03:21 +01:00
|
|
|
|
|
|
|
for i, name := range attributes {
|
|
|
|
gl.Call("bindAttribLocation", v, i, name)
|
|
|
|
}
|
|
|
|
|
2018-06-21 18:45:25 +02:00
|
|
|
gl.Call("linkProgram", v)
|
|
|
|
if !gl.Call("getProgramParameter", v, linkStatus).Bool() {
|
2018-10-29 17:52:59 +01:00
|
|
|
return program{}, errors.New("opengl: program error")
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2018-06-21 18:45:25 +02:00
|
|
|
|
|
|
|
id := c.lastProgramID
|
|
|
|
c.lastProgramID++
|
2018-10-29 17:52:59 +01:00
|
|
|
return program{
|
2018-06-21 18:45:25 +02:00
|
|
|
value: v,
|
|
|
|
id: id,
|
|
|
|
}, nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) useProgram(p program) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-06-21 18:45:25 +02:00
|
|
|
gl.Call("useProgram", p.value)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteProgram(p program) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2016-07-03 17:23:45 +02:00
|
|
|
gl := c.gl
|
2018-06-30 05:32:15 +02:00
|
|
|
if !gl.Call("isProgram", p.value).Bool() {
|
2016-07-12 19:07:35 +02:00
|
|
|
return
|
|
|
|
}
|
2018-06-30 05:32:15 +02:00
|
|
|
gl.Call("deleteProgram", p.value)
|
2016-07-03 17:23:45 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) getUniformLocationImpl(p program, location string) uniformLocation {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2015-01-12 15:16:34 +01:00
|
|
|
gl := c.gl
|
2018-06-21 18:45:25 +02:00
|
|
|
return uniformLocation(gl.Call("getUniformLocation", p.value, location))
|
2015-01-12 15:16:34 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 04:07:21 +02:00
|
|
|
func (c *context) uniformInt(p program, location string, v int) bool {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
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)
|
2020-05-27 04:07:21 +02:00
|
|
|
if l.equal(invalidUniform) {
|
|
|
|
return false
|
|
|
|
}
|
2018-05-23 20:11:13 +02:00
|
|
|
gl.Call("uniform1i", js.Value(l), v)
|
2020-05-27 04:07:21 +02:00
|
|
|
return true
|
2015-01-03 07:52:02 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 04:07:21 +02:00
|
|
|
func (c *context) uniformFloat(p program, location string, v float32) bool {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2018-02-22 03:46:46 +01:00
|
|
|
gl := c.gl
|
|
|
|
l := c.locationCache.GetUniformLocation(c, p, location)
|
2020-05-27 04:07:21 +02:00
|
|
|
if l.equal(invalidUniform) {
|
|
|
|
return false
|
|
|
|
}
|
2018-05-23 20:11:13 +02:00
|
|
|
gl.Call("uniform1f", js.Value(l), v)
|
2020-05-27 04:07:21 +02:00
|
|
|
return true
|
2018-02-22 03:46:46 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 04:07:21 +02:00
|
|
|
func (c *context) uniformFloats(p program, location string, v []float32) bool {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2015-01-03 07:52:02 +01:00
|
|
|
gl := c.gl
|
2016-02-26 19:01:55 +01:00
|
|
|
l := c.locationCache.GetUniformLocation(c, p, location)
|
2020-05-27 04:07:21 +02:00
|
|
|
if l.equal(invalidUniform) {
|
|
|
|
return false
|
|
|
|
}
|
2015-01-03 07:52:02 +01:00
|
|
|
switch len(v) {
|
2017-12-11 15:07:01 +01:00
|
|
|
case 2:
|
2018-05-23 20:11:13 +02:00
|
|
|
gl.Call("uniform2f", js.Value(l), v[0], v[1])
|
2015-01-03 07:52:02 +01:00
|
|
|
case 4:
|
2018-05-23 20:11:13 +02:00
|
|
|
gl.Call("uniform4f", js.Value(l), v[0], v[1], v[2], v[3])
|
2015-01-03 07:52:02 +01:00
|
|
|
case 16:
|
2019-10-27 14:40:36 +01:00
|
|
|
arr8 := jsutil.TemporaryUint8Array(len(v) * 4)
|
|
|
|
arr := js.Global().Get("Float32Array").New(arr8.Get("buffer"), arr8.Get("byteOffset"), len(v))
|
|
|
|
jsutil.CopySliceToJS(arr, v)
|
2018-07-05 19:34:17 +02:00
|
|
|
gl.Call("uniformMatrix4fv", js.Value(l), false, arr)
|
2015-01-03 07:21:47 +01:00
|
|
|
default:
|
2019-02-07 09:19:24 +01:00
|
|
|
panic(fmt.Sprintf("opengl: invalid uniform floats num: %d", len(v)))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2020-05-27 04:07:21 +02:00
|
|
|
return true
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 06:03:21 +01:00
|
|
|
func (c *context) vertexAttribPointer(p program, index int, size int, dataType dataType, stride int, offset int) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2019-02-16 06:03:21 +01:00
|
|
|
gl.Call("vertexAttribPointer", index, size, int(dataType), false, stride, offset)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 06:03:21 +01:00
|
|
|
func (c *context) enableVertexAttribArray(p program, index int) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2019-02-16 06:03:21 +01:00
|
|
|
gl.Call("enableVertexAttribArray", index)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 06:03:21 +01:00
|
|
|
func (c *context) disableVertexAttribArray(p program, index int) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2019-02-16 06:03:21 +01:00
|
|
|
gl.Call("disableVertexAttribArray", index)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newArrayBuffer(size int) buffer {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-05-21 18:41:16 +02:00
|
|
|
b := gl.Call("createBuffer")
|
2018-10-30 14:41:05 +01:00
|
|
|
gl.Call("bindBuffer", int(arrayBuffer), js.Value(b))
|
|
|
|
gl.Call("bufferData", int(arrayBuffer), size, int(dynamicDraw))
|
2018-10-29 17:52:59 +01:00
|
|
|
return buffer(b)
|
2017-08-31 18:29:56 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newElementArrayBuffer(size int) buffer {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2017-08-31 18:29:56 +02:00
|
|
|
gl := c.gl
|
2018-05-21 18:41:16 +02:00
|
|
|
b := gl.Call("createBuffer")
|
2018-10-30 14:41:05 +01:00
|
|
|
gl.Call("bindBuffer", int(elementArrayBuffer), js.Value(b))
|
|
|
|
gl.Call("bufferData", int(elementArrayBuffer), size, int(dynamicDraw))
|
2018-10-29 17:52:59 +01:00
|
|
|
return buffer(b)
|
2015-01-17 04:45:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) bindBuffer(bufferType bufferType, b buffer) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2015-01-17 04:45:19 +01:00
|
|
|
gl := c.gl
|
2018-05-23 20:37:14 +02:00
|
|
|
gl.Call("bindBuffer", int(bufferType), js.Value(b))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) arrayBufferSubData(data []float32) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2019-10-27 14:40:36 +01:00
|
|
|
arr8 := jsutil.TemporaryUint8Array(len(data) * 4)
|
|
|
|
arr := js.Global().Get("Float32Array").New(arr8.Get("buffer"), arr8.Get("byteOffset"), len(data))
|
|
|
|
jsutil.CopySliceToJS(arr, data)
|
2018-10-30 14:41:05 +01:00
|
|
|
gl.Call("bufferSubData", int(arrayBuffer), 0, arr)
|
2018-05-30 17:53:02 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) elementArrayBufferSubData(data []uint16) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2018-05-30 17:53:02 +02:00
|
|
|
gl := c.gl
|
2019-10-27 14:40:36 +01:00
|
|
|
arr8 := jsutil.TemporaryUint8Array(len(data) * 2)
|
|
|
|
arr := js.Global().Get("Uint16Array").New(arr8.Get("buffer"), arr8.Get("byteOffset"), len(data))
|
|
|
|
jsutil.CopySliceToJS(arr, data)
|
2018-10-30 14:41:05 +01:00
|
|
|
gl.Call("bufferSubData", int(elementArrayBuffer), 0, arr)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteBuffer(b buffer) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2016-07-03 17:23:45 +02:00
|
|
|
gl := c.gl
|
2018-05-23 20:37:14 +02:00
|
|
|
gl.Call("deleteBuffer", js.Value(b))
|
2016-07-03 17:23:45 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) drawElements(len int, offsetInBytes int) {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-10-30 14:29:54 +01:00
|
|
|
gl.Call("drawElements", triangles, len, unsignedShort, offsetInBytes)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2016-06-06 19:24:36 +02:00
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) maxTextureSizeImpl() int {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2018-03-09 03:03:55 +01:00
|
|
|
gl := c.gl
|
2018-06-18 21:02:56 +02:00
|
|
|
return gl.Call("getParameter", maxTextureSize).Int()
|
2018-03-09 03:03:55 +01:00
|
|
|
}
|
|
|
|
|
2019-06-21 21:47:48 +02:00
|
|
|
func (c *context) getShaderPrecisionFormatPrecision() int {
|
|
|
|
c.ensureGL()
|
|
|
|
gl := c.gl
|
|
|
|
return gl.Call("getShaderPrecisionFormat", js.ValueOf(int(fragmentShader)), highFloat).Get("precision").Int()
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) flush() {
|
2018-11-04 15:32:18 +01:00
|
|
|
c.ensureGL()
|
2016-06-06 19:24:36 +02:00
|
|
|
gl := c.gl
|
2018-05-21 18:41:16 +02:00
|
|
|
gl.Call("flush")
|
2016-06-06 19:24:36 +02:00
|
|
|
}
|
2019-05-26 12:08:46 +02:00
|
|
|
|
|
|
|
func (c *context) needsRestoring() bool {
|
|
|
|
return !web.IsMobileBrowser()
|
|
|
|
}
|
2020-01-01 19:58:49 +01:00
|
|
|
|
|
|
|
func (c *context) canUsePBO() bool {
|
|
|
|
return isWebGL2Available
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) texSubImage2D(t textureNative, width, height int, args []*driver.ReplacePixelsArgs) {
|
|
|
|
c.ensureGL()
|
|
|
|
c.bindTexture(t)
|
|
|
|
gl := c.gl
|
|
|
|
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
|
|
|
|
// GLsizei width, GLsizei height,
|
|
|
|
// GLenum format, GLenum type, ArrayBufferView? pixels);
|
|
|
|
for _, a := range args {
|
|
|
|
arr := jsutil.TemporaryUint8Array(len(a.Pixels))
|
|
|
|
jsutil.CopySliceToJS(arr, a.Pixels)
|
|
|
|
gl.Call("texSubImage2D", texture2d, 0, a.X, a.Y, a.Width, a.Height, rgba, unsignedByte, arr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) newPixelBufferObject(width, height int) buffer {
|
|
|
|
c.ensureGL()
|
|
|
|
gl := c.gl
|
|
|
|
b := gl.Call("createBuffer")
|
|
|
|
gl.Call("bindBuffer", int(pixelUnpackBuffer), js.Value(b))
|
|
|
|
gl.Call("bufferData", int(pixelUnpackBuffer), 4*width*height, int(streamDraw))
|
|
|
|
gl.Call("bindBuffer", int(pixelUnpackBuffer), nil)
|
|
|
|
return buffer(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, height int, args []*driver.ReplacePixelsArgs) {
|
|
|
|
c.ensureGL()
|
|
|
|
c.bindTexture(t)
|
|
|
|
gl := c.gl
|
|
|
|
gl.Call("bindBuffer", int(pixelUnpackBuffer), js.Value(buffer))
|
|
|
|
|
|
|
|
stride := 4 * width
|
|
|
|
for _, a := range args {
|
|
|
|
arr := jsutil.TemporaryUint8Array(len(a.Pixels))
|
|
|
|
jsutil.CopySliceToJS(arr, a.Pixels)
|
|
|
|
offset := 4 * (a.Y*width + a.X)
|
|
|
|
for j := 0; j < a.Height; j++ {
|
|
|
|
gl.Call("bufferSubData", int(pixelUnpackBuffer), offset+stride*j, arr, 4*a.Width*j, 4*a.Width)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
|
|
|
|
// GLsizei width, GLsizei height,
|
|
|
|
// GLenum format, GLenum type, GLintptr offset);
|
|
|
|
gl.Call("texSubImage2D", texture2d, 0, 0, 0, width, height, rgba, unsignedByte, 0)
|
|
|
|
gl.Call("bindBuffer", int(pixelUnpackBuffer), nil)
|
|
|
|
}
|