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
|
|
|
|
2018-06-24 09:10:24 +02:00
|
|
|
"github.com/gopherjs/gopherwasm/js"
|
2017-12-02 08:46:55 +01: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-05-23 20:08:00 +02:00
|
|
|
Texture js.Value
|
|
|
|
Framebuffer js.Value
|
2018-05-23 20:37:14 +02:00
|
|
|
Shader js.Value
|
|
|
|
Buffer js.Value
|
2018-05-23 20:11:13 +02:00
|
|
|
uniformLocation js.Value
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2018-06-21 18:45:25 +02:00
|
|
|
attribLocation int
|
|
|
|
programID int
|
|
|
|
Program struct {
|
|
|
|
value js.Value
|
|
|
|
id programID
|
|
|
|
}
|
|
|
|
)
|
2015-01-12 15:16:34 +01:00
|
|
|
|
2018-05-23 20:04:56 +02:00
|
|
|
var InvalidTexture = Texture(js.Null)
|
2016-06-17 21:46:33 +02:00
|
|
|
|
2018-02-18 17:45:03 +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 (
|
|
|
|
blend js.Value
|
|
|
|
clampToEdge js.Value
|
|
|
|
colorAttachment0 js.Value
|
|
|
|
compileStatus js.Value
|
|
|
|
framebuffer js.Value
|
|
|
|
framebufferBinding js.Value
|
|
|
|
framebufferComplete js.Value
|
|
|
|
linkStatus js.Value
|
|
|
|
maxTextureSize js.Value
|
|
|
|
nearest js.Value
|
|
|
|
noError js.Value
|
|
|
|
texture2d js.Value
|
|
|
|
textureMagFilter js.Value
|
|
|
|
textureMinFilter js.Value
|
|
|
|
textureWrapS js.Value
|
|
|
|
textureWrapT js.Value
|
|
|
|
rgba js.Value
|
|
|
|
unpackAlignment js.Value
|
|
|
|
unsignedByte js.Value
|
|
|
|
unsignedShort js.Value
|
|
|
|
)
|
|
|
|
|
2016-07-03 11:11:37 +02:00
|
|
|
func init() {
|
2016-11-25 17:17:22 +01:00
|
|
|
// 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())
|
|
|
|
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())
|
2018-06-18 21:02:56 +02:00
|
|
|
|
|
|
|
blend = c.Get("BLEND")
|
|
|
|
clampToEdge = c.Get("CLAMP_TO_EDGE")
|
|
|
|
compileStatus = c.Get("COMPILE_STATUS")
|
|
|
|
colorAttachment0 = c.Get("COLOR_ATTACHMENT0")
|
|
|
|
framebuffer = c.Get("FRAMEBUFFER")
|
|
|
|
framebufferBinding = c.Get("FRAMEBUFFER_BINDING")
|
|
|
|
framebufferComplete = c.Get("FRAMEBUFFER_COMPLETE")
|
|
|
|
linkStatus = c.Get("LINK_STATUS")
|
|
|
|
maxTextureSize = c.Get("MAX_TEXTURE_SIZE")
|
|
|
|
nearest = c.Get("NEAREST")
|
|
|
|
noError = c.Get("NO_ERROR")
|
|
|
|
rgba = c.Get("RGBA")
|
|
|
|
texture2d = c.Get("TEXTURE_2D")
|
|
|
|
textureMagFilter = c.Get("TEXTURE_MAG_FILTER")
|
|
|
|
textureMinFilter = c.Get("TEXTURE_MIN_FILTER")
|
|
|
|
textureWrapS = c.Get("TEXTURE_WRAP_S")
|
|
|
|
textureWrapT = c.Get("TEXTURE_WRAP_T")
|
|
|
|
unpackAlignment = c.Get("UNPACK_ALIGNMENT")
|
|
|
|
unsignedByte = c.Get("UNSIGNED_BYTE")
|
|
|
|
unsignedShort = c.Get("UNSIGNED_SHORT")
|
2016-07-03 11:11:37 +02:00
|
|
|
}
|
|
|
|
|
2014-12-31 13:55:40 +01:00
|
|
|
type context struct {
|
2018-05-23 20:04:56 +02:00
|
|
|
gl js.Value
|
|
|
|
loseContext js.Value
|
2016-05-31 19:33:31 +02:00
|
|
|
lastProgramID programID
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2017-05-30 19:09:27 +02:00
|
|
|
func Init() error {
|
2017-12-02 08:46:55 +01:00
|
|
|
if web.IsNodeJS() {
|
|
|
|
return fmt.Errorf("opengl: Node.js is not supported")
|
|
|
|
}
|
|
|
|
|
2018-05-21 18:41:16 +02:00
|
|
|
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")
|
2018-05-23 20:04:56 +02:00
|
|
|
attr := js.Global.Get("Object").New()
|
|
|
|
attr.Set("alpha", true)
|
|
|
|
attr.Set("premultipliedAlpha", true)
|
2018-05-21 18:41:16 +02:00
|
|
|
gl := canvas.Call("getContext", "webgl", attr)
|
2018-05-23 20:04:56 +02:00
|
|
|
if gl == js.Null {
|
2018-05-21 18:41:16 +02:00
|
|
|
gl = canvas.Call("getContext", "experimental-webgl", attr)
|
2018-05-23 20:04:56 +02:00
|
|
|
if gl == js.Null {
|
2018-05-21 18:41:16 +02:00
|
|
|
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
|
|
|
|
2017-01-20 18:47:31 +01:00
|
|
|
// Getting an extension might fail after the context is lost, so
|
|
|
|
// it is required to get the extension here.
|
2018-05-21 18:41:16 +02:00
|
|
|
c.loseContext = gl.Call("getExtension", "WEBGL_lose_context")
|
2018-05-23 20:04:56 +02:00
|
|
|
if c.loseContext != js.Null {
|
2017-01-20 18:47:31 +01:00
|
|
|
// This testing function name is temporary.
|
2018-05-23 20:04:56 +02:00
|
|
|
js.Global.Set("_ebiten_loseContextForTesting", js.NewCallback(func([]js.Value) {
|
2017-01-20 18:47:31 +01:00
|
|
|
c.loseContext.Call("loseContext")
|
2018-05-23 20:04:56 +02:00
|
|
|
}))
|
2017-01-20 18:47:31 +01:00
|
|
|
}
|
2017-05-30 19:09:27 +02:00
|
|
|
theContext = c
|
|
|
|
return nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 17:30:14 +02:00
|
|
|
func (c *Context) Reset() error {
|
2016-06-09 18:19:10 +02:00
|
|
|
c.locationCache = newLocationCache()
|
2018-05-23 20:08:00 +02:00
|
|
|
c.lastTexture = Texture(js.Null)
|
|
|
|
c.lastFramebuffer = Framebuffer(js.Null)
|
2016-06-09 18:19:10 +02:00
|
|
|
c.lastViewportWidth = 0
|
|
|
|
c.lastViewportHeight = 0
|
|
|
|
c.lastCompositeMode = CompositeModeUnknown
|
|
|
|
gl := c.gl
|
2018-06-18 21:02:56 +02:00
|
|
|
gl.Call("enable", blend)
|
2016-06-09 18:19:10 +02:00
|
|
|
c.BlendFunc(CompositeModeSourceOver)
|
2018-06-18 21:02:56 +02:00
|
|
|
f := gl.Call("getParameter", framebufferBinding)
|
2018-05-23 20:08:00 +02:00
|
|
|
c.screenFramebuffer = Framebuffer(f)
|
2016-06-17 23:25:40 +02:00
|
|
|
return nil
|
2016-06-09 18:19:10 +02:00
|
|
|
}
|
|
|
|
|
2016-02-29 18:16:32 +01:00
|
|
|
func (c *Context) BlendFunc(mode CompositeMode) {
|
|
|
|
if c.lastCompositeMode == mode {
|
2016-02-28 17:44:09 +01:00
|
|
|
return
|
|
|
|
}
|
2016-02-29 18:16:32 +01:00
|
|
|
c.lastCompositeMode = mode
|
2016-07-03 11:11:37 +02:00
|
|
|
s, d := operations(mode)
|
2016-02-28 16:25:16 +01:00
|
|
|
gl := c.gl
|
2018-05-21 18:41:16 +02:00
|
|
|
gl.Call("blendFunc", int(s), int(d))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-02-25 15:34:34 +01:00
|
|
|
func (c *Context) NewTexture(width, height int) (Texture, error) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-05-21 18:41:16 +02:00
|
|
|
t := gl.Call("createTexture")
|
2018-05-23 20:04:56 +02:00
|
|
|
if t == js.Null {
|
2018-05-23 20:08:00 +02:00
|
|
|
return Texture(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-05-23 20:08:00 +02:00
|
|
|
c.BindTexture(Texture(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
|
|
|
|
|
|
|
// void texImage2D(GLenum target, GLint level, GLenum internalformat,
|
|
|
|
// GLsizei width, GLsizei height, GLint border, GLenum format,
|
|
|
|
// GLenum type, ArrayBufferView? pixels);
|
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-05-23 20:08:00 +02:00
|
|
|
return Texture(t), nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2017-09-24 17:14:25 +02:00
|
|
|
func (c *Context) bindFramebufferImpl(f Framebuffer) {
|
2015-02-19 18:02:23 +01:00
|
|
|
gl := c.gl
|
2018-06-18 21:02:56 +02:00
|
|
|
gl.Call("bindFramebuffer", framebuffer, js.Value(f))
|
2015-02-19 18:02:23 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 16:27:55 +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-01-17 10:22:58 +01:00
|
|
|
|
2015-02-19 18:02:23 +01:00
|
|
|
c.bindFramebuffer(f)
|
2015-01-17 10:22:58 +01:00
|
|
|
|
2018-05-23 20:04:56 +02:00
|
|
|
pixels := make([]byte, 4*width*height)
|
2018-06-18 21:02:56 +02:00
|
|
|
gl.Call("readPixels", 0, 0, width, height, rgba, unsignedByte, pixels)
|
|
|
|
if e := gl.Call("getError"); e.Int() != noError.Int() {
|
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
|
|
|
}
|
2018-05-23 20:04:56 +02:00
|
|
|
return pixels, nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2017-09-24 17:11:19 +02:00
|
|
|
func (c *Context) bindTextureImpl(t Texture) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DeleteTexture(t Texture) {
|
|
|
|
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
|
|
|
|
}
|
2016-07-09 22:04:25 +02:00
|
|
|
if c.lastTexture == t {
|
2018-05-23 20:08:00 +02:00
|
|
|
c.lastTexture = Texture(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
|
|
|
}
|
|
|
|
|
2016-06-12 16:54:36 +02:00
|
|
|
func (c *Context) IsTexture(t Texture) bool {
|
|
|
|
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-02-28 16:27:55 +01:00
|
|
|
func (c *Context) TexSubImage2D(p []byte, x, y, width, height int) {
|
2015-01-02 15:30:22 +01:00
|
|
|
gl := c.gl
|
2015-01-20 15:58:58 +01:00
|
|
|
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
|
|
|
|
// GLsizei width, GLsizei height,
|
|
|
|
// GLenum format, GLenum type, ArrayBufferView? pixels);
|
2018-06-18 21:02:56 +02:00
|
|
|
gl.Call("texSubImage2D", texture2d, 0, x, y, width, height, rgba, unsignedByte, p)
|
2015-01-02 15:30:22 +01:00
|
|
|
}
|
|
|
|
|
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
|
2018-05-21 18:41:16 +02:00
|
|
|
f := gl.Call("createFramebuffer")
|
2018-05-23 20:08:00 +02:00
|
|
|
c.bindFramebuffer(Framebuffer(f))
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2018-06-18 21:02:56 +02:00
|
|
|
gl.Call("framebufferTexture2D", framebuffer, colorAttachment0, texture2d, js.Value(t), 0)
|
|
|
|
if s := gl.Call("checkFramebufferStatus", framebuffer); s.Int() != framebufferComplete.Int() {
|
2018-05-23 20:08:00 +02:00
|
|
|
return Framebuffer(js.Null), errors.New(fmt.Sprintf("opengl: creating framebuffer failed: %d", s.Int()))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 20:08:00 +02:00
|
|
|
return Framebuffer(f), nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2017-09-24 17:17:07 +02:00
|
|
|
func (c *Context) setViewportImpl(width, height int) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DeleteFramebuffer(f Framebuffer) {
|
|
|
|
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
|
|
|
|
if c.lastFramebuffer == f {
|
2018-05-23 20:08:00 +02:00
|
|
|
c.lastFramebuffer = Framebuffer(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
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) {
|
|
|
|
gl := c.gl
|
2018-05-21 18:41:16 +02:00
|
|
|
s := gl.Call("createShader", int(shaderType))
|
2018-05-23 20:04:56 +02:00
|
|
|
if s == js.Null {
|
2018-05-23 20:37:14 +02: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))
|
|
|
|
return Shader(js.Null), fmt.Errorf("opengl: shader compile failed: %s", log)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2018-05-23 20:37:14 +02:00
|
|
|
return Shader(s), nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DeleteShader(s Shader) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) NewProgram(shaders []Shader) (Program, error) {
|
|
|
|
gl := c.gl
|
2018-06-21 18:45:25 +02:00
|
|
|
v := gl.Call("createProgram")
|
|
|
|
if v == js.Null {
|
|
|
|
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
|
|
|
}
|
2018-06-21 18:45:25 +02:00
|
|
|
gl.Call("linkProgram", v)
|
|
|
|
if !gl.Call("getProgramParameter", v, linkStatus).Bool() {
|
|
|
|
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++
|
|
|
|
return Program{
|
|
|
|
value: v,
|
|
|
|
id: id,
|
|
|
|
}, nil
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) UseProgram(p Program) {
|
|
|
|
gl := c.gl
|
2018-06-21 18:45:25 +02:00
|
|
|
gl.Call("useProgram", p.value)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 17:23:45 +02:00
|
|
|
func (c *Context) DeleteProgram(p Program) {
|
|
|
|
gl := c.gl
|
2018-05-22 18:29:16 +02:00
|
|
|
if !gl.Call("isProgram", p).Bool() {
|
2016-07-12 19:07:35 +02:00
|
|
|
return
|
|
|
|
}
|
2018-05-22 18:29:16 +02:00
|
|
|
gl.Call("deleteProgram", p)
|
2016-07-03 17:23:45 +02:00
|
|
|
}
|
|
|
|
|
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
|
2018-06-21 18:45:25 +02:00
|
|
|
return uniformLocation(gl.Call("getUniformLocation", p.value, 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)
|
2018-05-23 20:11:13 +02:00
|
|
|
gl.Call("uniform1i", js.Value(l), v)
|
2015-01-03 07:52:02 +01:00
|
|
|
}
|
|
|
|
|
2018-02-22 03:46:46 +01:00
|
|
|
func (c *Context) UniformFloat(p Program, location string, v float32) {
|
|
|
|
gl := c.gl
|
|
|
|
l := c.locationCache.GetUniformLocation(c, p, location)
|
2018-05-23 20:11:13 +02:00
|
|
|
gl.Call("uniform1f", js.Value(l), v)
|
2018-02-22 03:46:46 +01:00
|
|
|
}
|
|
|
|
|
2018-06-18 20:40:37 +02:00
|
|
|
var (
|
|
|
|
float32Array = js.Global.Get("Float32Array")
|
|
|
|
)
|
|
|
|
|
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) {
|
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:
|
2018-06-23 19:02:35 +02:00
|
|
|
gl.Call("uniformMatrix4fv", js.Value(l), false, js.ValueOf(v))
|
2015-01-03 07:21:47 +01:00
|
|
|
default:
|
2017-12-11 15:07:01 +01:00
|
|
|
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
|
2018-06-21 18:45:25 +02:00
|
|
|
return attribLocation(gl.Call("getAttribLocation", p.value, location).Int())
|
2015-01-12 15:16:34 +01:00
|
|
|
}
|
|
|
|
|
2017-12-03 11:21:59 +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)
|
2018-05-21 18:41:16 +02:00
|
|
|
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)
|
2018-05-21 18:41:16 +02:00
|
|
|
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)
|
2018-05-21 18:41:16 +02:00
|
|
|
gl.Call("disableVertexAttribArray", int(l))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2017-08-31 18:29:56 +02:00
|
|
|
func (c *Context) NewArrayBuffer(size int) Buffer {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-05-21 18:41:16 +02:00
|
|
|
b := gl.Call("createBuffer")
|
2018-05-23 20:37:14 +02:00
|
|
|
gl.Call("bindBuffer", int(ArrayBuffer), js.Value(b))
|
2018-05-21 18:41:16 +02:00
|
|
|
gl.Call("bufferData", int(ArrayBuffer), size, int(DynamicDraw))
|
2018-05-23 20:37:14 +02:00
|
|
|
return Buffer(b)
|
2017-08-31 18:29:56 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:02 +02:00
|
|
|
func (c *Context) NewElementArrayBuffer(size int) Buffer {
|
2017-08-31 18:29:56 +02:00
|
|
|
gl := c.gl
|
2018-05-21 18:41:16 +02:00
|
|
|
b := gl.Call("createBuffer")
|
2018-05-23 20:37:14 +02:00
|
|
|
gl.Call("bindBuffer", int(ElementArrayBuffer), js.Value(b))
|
2018-05-30 17:53:02 +02:00
|
|
|
gl.Call("bufferData", int(ElementArrayBuffer), size, int(DynamicDraw))
|
2018-05-23 20:37:14 +02:00
|
|
|
return Buffer(b)
|
2015-01-17 04:45:19 +01:00
|
|
|
}
|
|
|
|
|
2018-05-27 17:33:58 +02:00
|
|
|
func (c *Context) BindBuffer(bufferType BufferType, b Buffer) {
|
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-05-30 17:53:02 +02:00
|
|
|
func (c *Context) ArrayBufferSubData(data []float32) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-06-15 19:19:18 +02:00
|
|
|
gl.Call("bufferSubData", int(ArrayBuffer), 0, js.ValueOf(data))
|
2018-05-30 17:53:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) ElementArrayBufferSubData(data []uint16) {
|
|
|
|
gl := c.gl
|
2018-06-15 19:19:18 +02:00
|
|
|
gl.Call("bufferSubData", int(ElementArrayBuffer), 0, js.ValueOf(data))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 17:23:45 +02:00
|
|
|
func (c *Context) DeleteBuffer(b Buffer) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-06-03 05:41:18 +02:00
|
|
|
func (c *Context) DrawElements(mode Mode, len int, offsetInBytes int) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2018-06-18 21:02:56 +02:00
|
|
|
gl.Call("drawElements", int(mode), len, unsignedShort, offsetInBytes)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2016-06-06 19:24:36 +02:00
|
|
|
|
2018-03-09 03:03:55 +01:00
|
|
|
func (c *Context) maxTextureSizeImpl() int {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-06-06 19:24:36 +02:00
|
|
|
func (c *Context) Flush() {
|
|
|
|
gl := c.gl
|
2018-05-21 18:41:16 +02:00
|
|
|
gl.Call("flush")
|
2016-06-06 19:24:36 +02:00
|
|
|
}
|
2017-01-19 18:20:41 +01:00
|
|
|
|
2017-01-20 18:47:31 +01:00
|
|
|
func (c *Context) IsContextLost() bool {
|
2017-01-19 18:20:41 +01:00
|
|
|
gl := c.gl
|
2018-05-21 18:41:16 +02:00
|
|
|
return gl.Call("isContextLost").Bool()
|
2017-01-19 18:20:41 +01:00
|
|
|
}
|
2017-01-20 18:47:31 +01:00
|
|
|
|
|
|
|
func (c *Context) RestoreContext() {
|
2018-05-23 20:04:56 +02:00
|
|
|
if c.loseContext != js.Null {
|
2017-01-20 18:47:31 +01:00
|
|
|
c.loseContext.Call("restoreContext")
|
|
|
|
}
|
|
|
|
}
|