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.
|
|
|
|
|
|
|
|
package opengl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-04-30 19:15:28 +02:00
|
|
|
"syscall/js"
|
2018-10-28 12:42:57 +01:00
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2020-11-08 11:23:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gles"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/jsutil"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2014-12-31 13:55:40 +01:00
|
|
|
)
|
|
|
|
|
2018-02-18 18:03:01 +01:00
|
|
|
type (
|
2021-07-02 12:26:09 +02:00
|
|
|
textureNative js.Value
|
|
|
|
renderbufferNative js.Value
|
|
|
|
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 {
|
2021-06-07 14:43:25 +02:00
|
|
|
return js.Value(t).Equal(js.Value(rhs))
|
2019-12-18 16:03:35 +01:00
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
func (r renderbufferNative) equal(rhs renderbufferNative) bool {
|
|
|
|
return js.Value(r).Equal(js.Value(rhs))
|
|
|
|
}
|
|
|
|
|
2019-12-18 16:03:35 +01:00
|
|
|
func (f framebufferNative) equal(rhs framebufferNative) bool {
|
2021-06-07 14:43:25 +02:00
|
|
|
return js.Value(f).Equal(js.Value(rhs))
|
2019-12-18 16:03:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s shader) equal(rhs shader) bool {
|
2021-06-07 14:43:25 +02:00
|
|
|
return js.Value(s).Equal(js.Value(rhs))
|
2019-12-18 16:03:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b buffer) equal(rhs buffer) bool {
|
2021-06-07 14:43:25 +02:00
|
|
|
return js.Value(b).Equal(js.Value(rhs))
|
2019-12-18 16:03:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u uniformLocation) equal(rhs uniformLocation) bool {
|
2021-06-07 14:43:25 +02:00
|
|
|
return js.Value(u).Equal(js.Value(rhs))
|
2019-12-18 16:03:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p program) equal(rhs program) bool {
|
2021-06-07 14:43:25 +02:00
|
|
|
return p.value.Equal(rhs.value) && p.id == rhs.id
|
2019-12-18 16:03:35 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-08 11:23:32 +01:00
|
|
|
const (
|
|
|
|
zero = operation(gles.ZERO)
|
|
|
|
one = operation(gles.ONE)
|
|
|
|
srcAlpha = operation(gles.SRC_ALPHA)
|
|
|
|
dstAlpha = operation(gles.DST_ALPHA)
|
|
|
|
oneMinusSrcAlpha = operation(gles.ONE_MINUS_SRC_ALPHA)
|
|
|
|
oneMinusDstAlpha = operation(gles.ONE_MINUS_DST_ALPHA)
|
|
|
|
dstColor = operation(gles.DST_COLOR)
|
|
|
|
)
|
2020-05-17 16:04:13 +02:00
|
|
|
|
2021-08-01 14:50:21 +02:00
|
|
|
type webGLVersion int
|
|
|
|
|
|
|
|
const (
|
|
|
|
webGLVersionUnknown webGLVersion = iota
|
|
|
|
webGLVersion1
|
|
|
|
webGLVersion2
|
|
|
|
)
|
|
|
|
|
2020-11-08 11:23:32 +01:00
|
|
|
var (
|
2022-08-07 15:17:47 +02:00
|
|
|
webGL2MightBeAvailable = !forceWebGL1 && (js.Global().Get("WebGL2RenderingContext").Truthy())
|
2020-01-01 19:58:49 +01:00
|
|
|
)
|
|
|
|
|
2021-10-30 11:33:43 +02:00
|
|
|
func uint8ArrayToSlice(value js.Value, length int) []byte {
|
|
|
|
if l := value.Get("byteLength").Int(); length > l {
|
|
|
|
length = l
|
|
|
|
}
|
|
|
|
s := make([]byte, length)
|
|
|
|
js.CopyBytesToGo(s, value)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
type contextImpl struct {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl *gl
|
2022-06-11 08:34:17 +02:00
|
|
|
canvas js.Value
|
2016-05-31 19:33:31 +02:00
|
|
|
lastProgramID programID
|
2021-08-01 14:50:21 +02:00
|
|
|
webGLVersion webGLVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) usesWebGL2() bool {
|
|
|
|
return c.webGLVersion == webGLVersion2
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2021-10-05 17:38:44 +02:00
|
|
|
func (c *context) initGL() error {
|
2021-08-01 14:50:21 +02:00
|
|
|
c.webGLVersion = webGLVersionUnknown
|
|
|
|
|
2020-01-01 19:58:49 +01:00
|
|
|
var gl js.Value
|
2020-11-22 10:22:52 +01:00
|
|
|
|
|
|
|
if doc := js.Global().Get("document"); doc.Truthy() {
|
2022-06-11 08:34:17 +02:00
|
|
|
canvas := c.canvas
|
2020-11-22 10:22:52 +01:00
|
|
|
attr := js.Global().Get("Object").New()
|
|
|
|
attr.Set("alpha", true)
|
|
|
|
attr.Set("premultipliedAlpha", true)
|
2021-07-02 12:26:09 +02:00
|
|
|
attr.Set("stencil", true)
|
2020-11-22 10:22:52 +01:00
|
|
|
|
2021-08-01 14:50:21 +02:00
|
|
|
if webGL2MightBeAvailable {
|
2020-11-22 10:22:52 +01:00
|
|
|
gl = canvas.Call("getContext", "webgl2", attr)
|
2021-08-01 14:50:21 +02:00
|
|
|
if gl.Truthy() {
|
|
|
|
c.webGLVersion = webGLVersion2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even though WebGL2RenderingContext exists, getting a webgl2 context might fail (#1738).
|
|
|
|
if !gl.Truthy() {
|
2020-11-22 10:22:52 +01:00
|
|
|
gl = canvas.Call("getContext", "webgl", attr)
|
2021-06-07 14:43:25 +02:00
|
|
|
if !gl.Truthy() {
|
2020-11-22 10:22:52 +01:00
|
|
|
gl = canvas.Call("getContext", "experimental-webgl", attr)
|
2020-01-01 19:58:49 +01:00
|
|
|
}
|
2021-08-01 14:50:21 +02:00
|
|
|
if gl.Truthy() {
|
|
|
|
c.webGLVersion = webGLVersion1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !gl.Truthy() {
|
2021-10-05 17:38:44 +02:00
|
|
|
return fmt.Errorf("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
|
|
|
|
2021-08-01 14:50:21 +02:00
|
|
|
c.gl = c.newGL(gl)
|
2021-10-05 17:38:44 +02:00
|
|
|
return nil
|
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
|
2022-02-06 12:41:32 +01:00
|
|
|
c.lastCompositeMode = graphicsdriver.CompositeModeUnknown
|
2018-11-04 15:32:18 +01:00
|
|
|
|
2021-10-05 17:38:44 +02:00
|
|
|
if err := c.initGL(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-21 11:40:07 +01:00
|
|
|
|
2021-04-05 16:53:17 +02:00
|
|
|
if c.gl.isContextLost.Invoke().Bool() {
|
2022-02-06 12:41:32 +01:00
|
|
|
return graphicsdriver.GraphicsNotReady
|
2019-01-29 17:44:57 +01:00
|
|
|
}
|
2016-06-09 18:19:10 +02:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.enable.Invoke(gles.BLEND)
|
|
|
|
gl.enable.Invoke(gles.SCISSOR_TEST)
|
2022-02-06 12:41:32 +01:00
|
|
|
c.blendFunc(graphicsdriver.CompositeModeSourceOver)
|
2021-04-05 16:53:17 +02:00
|
|
|
f := gl.getParameter.Invoke(gles.FRAMEBUFFER_BINDING)
|
2018-11-04 09:43:26 +01:00
|
|
|
c.screenFramebuffer = framebufferNative(f)
|
2020-10-26 02:33:11 +01:00
|
|
|
|
2021-08-01 14:50:21 +02:00
|
|
|
if !c.usesWebGL2() {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.getExtension.Invoke("OES_standard_derivatives")
|
2020-10-26 02:33:11 +01:00
|
|
|
}
|
2016-06-17 23:25:40 +02:00
|
|
|
return nil
|
2016-06-09 18:19:10 +02:00
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (c *context) blendFunc(mode graphicsdriver.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)
|
2016-02-28 16:25:16 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.blendFunc.Invoke(int(s2), int(d2))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2020-11-07 11:14:06 +01:00
|
|
|
func (c *context) scissor(x, y, width, height int) {
|
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.scissor.Invoke(x, y, width, height)
|
2020-11-07 11:14:06 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newTexture(width, height int) (textureNative, error) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
t := gl.createTexture.Invoke()
|
2021-06-07 14:43:25 +02:00
|
|
|
if !t.Truthy() {
|
2021-07-02 12:26:09 +02:00
|
|
|
return textureNative(js.Null()), errors.New("opengl: createTexture failed")
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2018-11-04 11:46:20 +01:00
|
|
|
c.bindTexture(textureNative(t))
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.texParameteri.Invoke(gles.TEXTURE_2D, gles.TEXTURE_MAG_FILTER, gles.NEAREST)
|
|
|
|
gl.texParameteri.Invoke(gles.TEXTURE_2D, gles.TEXTURE_MIN_FILTER, gles.NEAREST)
|
|
|
|
gl.texParameteri.Invoke(gles.TEXTURE_2D, gles.TEXTURE_WRAP_S, gles.CLAMP_TO_EDGE)
|
|
|
|
gl.texParameteri.Invoke(gles.TEXTURE_2D, gles.TEXTURE_WRAP_T, gles.CLAMP_TO_EDGE)
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2021-07-02 19:09:44 +02:00
|
|
|
gl.pixelStorei.Invoke(gles.UNPACK_ALIGNMENT, 4)
|
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.
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.texImage2D.Invoke(gles.TEXTURE_2D, 0, gles.RGBA, width, height, 0, gles.RGBA, gles.UNSIGNED_BYTE, 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) {
|
2015-02-19 18:02:23 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.bindFramebuffer.Invoke(gles.FRAMEBUFFER, js.Value(f))
|
2015-02-19 18:02:23 +01:00
|
|
|
}
|
|
|
|
|
2022-08-27 14:22:31 +02:00
|
|
|
func (c *context) framebufferPixels(buf []byte, f *framebuffer, x, y, width, height int) {
|
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
|
|
|
|
2020-12-14 17:09:40 +01:00
|
|
|
l := 4 * width * height
|
2021-10-30 11:43:46 +02:00
|
|
|
p := jsutil.TemporaryUint8ArrayFromUint8Slice(l, nil)
|
2022-08-27 14:22:31 +02:00
|
|
|
gl.readPixels.Invoke(x, y, width, height, gles.RGBA, gles.UNSIGNED_BYTE, p)
|
2022-02-27 09:41:19 +01:00
|
|
|
copy(buf, uint8ArrayToSlice(p, l))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2020-08-15 08:11:56 +02:00
|
|
|
func (c *context) framebufferPixelsToBuffer(f *framebuffer, buffer buffer, width, height int) {
|
|
|
|
gl := c.gl
|
|
|
|
|
|
|
|
c.bindFramebuffer(f.native)
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.bindBuffer.Invoke(gles.PIXEL_PACK_BUFFER, js.Value(buffer))
|
2020-08-15 08:11:56 +02:00
|
|
|
// void gl.readPixels(x, y, width, height, format, type, GLintptr offset);
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.readPixels.Invoke(0, 0, width, height, gles.RGBA, gles.UNSIGNED_BYTE, 0)
|
|
|
|
gl.bindBuffer.Invoke(gles.PIXEL_PACK_BUFFER, nil)
|
2020-08-15 08:11:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 16:04:13 +02:00
|
|
|
func (c *context) activeTexture(idx int) {
|
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.activeTexture.Invoke(gles.TEXTURE0 + idx)
|
2020-05-17 16:04:13 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) bindTextureImpl(t textureNative) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.bindTexture.Invoke(gles.TEXTURE_2D, 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) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
if !gl.isTexture.Invoke(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
|
|
|
}
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.deleteTexture.Invoke(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 {
|
2020-05-30 12:43:59 +02:00
|
|
|
// isTexture should not be called to detect context-lost since this performance is not good (#1175).
|
|
|
|
panic("opengl: isTexture is not implemented")
|
2016-06-12 16:54:36 +02:00
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
func (c *context) newRenderbuffer(width, height int) (renderbufferNative, error) {
|
|
|
|
gl := c.gl
|
|
|
|
r := gl.createRenderbuffer.Invoke()
|
|
|
|
if !r.Truthy() {
|
|
|
|
return renderbufferNative(js.Null()), errors.New("opengl: createRenderbuffer failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
c.bindRenderbuffer(renderbufferNative(r))
|
|
|
|
// TODO: Is STENCIL_INDEX8 portable?
|
|
|
|
// https://stackoverflow.com/questions/11084961/binding-a-stencil-render-buffer-to-a-frame-buffer-in-opengl
|
|
|
|
gl.renderbufferStorage.Invoke(gles.RENDERBUFFER, gles.STENCIL_INDEX8, width, height)
|
|
|
|
|
|
|
|
return renderbufferNative(r), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) bindRenderbufferImpl(r renderbufferNative) {
|
|
|
|
gl := c.gl
|
|
|
|
gl.bindRenderbuffer.Invoke(gles.RENDERBUFFER, js.Value(r))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) deleteRenderbuffer(r renderbufferNative) {
|
|
|
|
gl := c.gl
|
|
|
|
if !gl.isRenderbuffer.Invoke(js.Value(r)).Bool() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.lastRenderbuffer.equal(r) {
|
|
|
|
c.lastRenderbuffer = renderbufferNative(js.Null())
|
|
|
|
}
|
|
|
|
gl.deleteRenderbuffer.Invoke(js.Value(r))
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newFramebuffer(t textureNative) (framebufferNative, error) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
f := gl.createFramebuffer.Invoke()
|
2018-11-04 09:43:26 +01:00
|
|
|
c.bindFramebuffer(framebufferNative(f))
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.framebufferTexture2D.Invoke(gles.FRAMEBUFFER, gles.COLOR_ATTACHMENT0, gles.TEXTURE_2D, js.Value(t), 0)
|
|
|
|
if s := gl.checkFramebufferStatus.Invoke(gles.FRAMEBUFFER); s.Int() != gles.FRAMEBUFFER_COMPLETE {
|
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
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
func (c *context) bindStencilBuffer(f framebufferNative, r renderbufferNative) error {
|
|
|
|
gl := c.gl
|
|
|
|
c.bindFramebuffer(f)
|
|
|
|
|
|
|
|
gl.framebufferRenderbuffer.Invoke(gles.FRAMEBUFFER, gles.STENCIL_ATTACHMENT, gles.RENDERBUFFER, js.Value(r))
|
|
|
|
if s := gl.checkFramebufferStatus.Invoke(gles.FRAMEBUFFER); s.Int() != gles.FRAMEBUFFER_COMPLETE {
|
|
|
|
return errors.New(fmt.Sprintf("opengl: framebufferRenderbuffer failed: %d", s.Int()))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) setViewportImpl(width, height int) {
|
2016-06-18 15:47:34 +02:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.viewport.Invoke(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) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
if !gl.isFramebuffer.Invoke(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
|
|
|
}
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.deleteFramebuffer.Invoke(js.Value(f))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2020-11-21 15:32:13 +01:00
|
|
|
func (c *context) newVertexShader(source string) (shader, error) {
|
|
|
|
return c.newShader(gles.VERTEX_SHADER, source)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) newFragmentShader(source string) (shader, error) {
|
|
|
|
return c.newShader(gles.FRAGMENT_SHADER, source)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) newShader(shaderType int, source string) (shader, error) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
s := gl.createShader.Invoke(int(shaderType))
|
2021-06-07 14:43:25 +02:00
|
|
|
if !s.Truthy() {
|
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
|
|
|
}
|
|
|
|
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.shaderSource.Invoke(js.Value(s), source)
|
|
|
|
gl.compileShader.Invoke(js.Value(s))
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2021-04-05 16:53:17 +02:00
|
|
|
if !gl.getShaderParameter.Invoke(js.Value(s), gles.COMPILE_STATUS).Bool() {
|
|
|
|
log := gl.getShaderInfoLog.Invoke(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) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.deleteShader.Invoke(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) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
v := gl.createProgram.Invoke()
|
2021-06-07 14:43:25 +02:00
|
|
|
if !v.Truthy() {
|
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 {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.attachShader.Invoke(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 {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.bindAttribLocation.Invoke(v, i, name)
|
2019-02-16 06:03:21 +01:00
|
|
|
}
|
|
|
|
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.linkProgram.Invoke(v)
|
|
|
|
if !gl.getProgramParameter.Invoke(v, gles.LINK_STATUS).Bool() {
|
|
|
|
info := gl.getProgramInfoLog.Invoke(v).String()
|
2020-08-01 11:39:17 +02:00
|
|
|
return program{}, fmt.Errorf("opengl: program error: %s", info)
|
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) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.useProgram.Invoke(p.value)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteProgram(p program) {
|
2021-08-14 08:11:41 +02:00
|
|
|
c.locationCache.deleteProgram(p)
|
|
|
|
|
2016-07-03 17:23:45 +02:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
if !gl.isProgram.Invoke(p.value).Bool() {
|
2016-07-12 19:07:35 +02:00
|
|
|
return
|
|
|
|
}
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.deleteProgram.Invoke(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 {
|
2015-01-12 15:16:34 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
return uniformLocation(gl.getUniformLocation.Invoke(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 {
|
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
|
|
|
|
}
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniform1i.Invoke(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-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
|
|
|
|
}
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniform1f.Invoke(js.Value(l), v)
|
2020-05-27 04:07:21 +02:00
|
|
|
return true
|
2018-02-22 03:46:46 +01:00
|
|
|
}
|
|
|
|
|
2020-07-30 19:52:25 +02:00
|
|
|
func (c *context) uniformFloats(p program, location string, v []float32, typ shaderir.Type) bool {
|
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
|
|
|
|
}
|
2020-07-30 19:52:25 +02:00
|
|
|
|
|
|
|
base := typ.Main
|
|
|
|
if base == shaderir.Array {
|
|
|
|
base = typ.Sub[0].Main
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:30:09 +01:00
|
|
|
arr := jsutil.TemporaryFloat32Array(len(v), v)
|
2020-12-04 04:10:05 +01:00
|
|
|
|
2020-07-30 19:52:25 +02:00
|
|
|
switch base {
|
|
|
|
case shaderir.Float:
|
2021-08-01 14:50:21 +02:00
|
|
|
if c.usesWebGL2() {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniform1fv.Invoke(js.Value(l), arr, 0, len(v))
|
2020-12-13 19:02:58 +01:00
|
|
|
} else {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniform1fv.Invoke(js.Value(l), arr.Call("subarray", 0, len(v)))
|
2020-12-13 19:02:58 +01:00
|
|
|
}
|
2020-07-30 19:52:25 +02:00
|
|
|
case shaderir.Vec2:
|
2021-08-01 14:50:21 +02:00
|
|
|
if c.usesWebGL2() {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniform2fv.Invoke(js.Value(l), arr, 0, len(v))
|
2020-12-13 19:02:58 +01:00
|
|
|
} else {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniform2fv.Invoke(js.Value(l), arr.Call("subarray", 0, len(v)))
|
2020-12-13 19:02:58 +01:00
|
|
|
}
|
2020-07-30 19:52:25 +02:00
|
|
|
case shaderir.Vec3:
|
2021-08-01 14:50:21 +02:00
|
|
|
if c.usesWebGL2() {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniform3fv.Invoke(js.Value(l), arr, 0, len(v))
|
2020-12-13 19:02:58 +01:00
|
|
|
} else {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniform3fv.Invoke(js.Value(l), arr.Call("subarray", 0, len(v)))
|
2020-12-13 19:02:58 +01:00
|
|
|
}
|
2020-07-30 19:52:25 +02:00
|
|
|
case shaderir.Vec4:
|
2021-08-01 14:50:21 +02:00
|
|
|
if c.usesWebGL2() {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniform4fv.Invoke(js.Value(l), arr, 0, len(v))
|
2020-12-13 19:02:58 +01:00
|
|
|
} else {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniform4fv.Invoke(js.Value(l), arr.Call("subarray", 0, len(v)))
|
2020-12-13 19:02:58 +01:00
|
|
|
}
|
2020-07-30 19:52:25 +02:00
|
|
|
case shaderir.Mat2:
|
2021-08-01 14:50:21 +02:00
|
|
|
if c.usesWebGL2() {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniformMatrix2fv.Invoke(js.Value(l), false, arr, 0, len(v))
|
2020-12-13 19:02:58 +01:00
|
|
|
} else {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniformMatrix2fv.Invoke(js.Value(l), false, arr.Call("subarray", 0, len(v)))
|
2020-12-13 19:02:58 +01:00
|
|
|
}
|
2020-07-30 19:52:25 +02:00
|
|
|
case shaderir.Mat3:
|
2021-08-01 14:50:21 +02:00
|
|
|
if c.usesWebGL2() {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniformMatrix3fv.Invoke(js.Value(l), false, arr, 0, len(v))
|
2020-12-13 19:02:58 +01:00
|
|
|
} else {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniformMatrix3fv.Invoke(js.Value(l), false, arr.Call("subarray", 0, len(v)))
|
2020-12-13 19:02:58 +01:00
|
|
|
}
|
2020-07-30 19:52:25 +02:00
|
|
|
case shaderir.Mat4:
|
2021-08-01 14:50:21 +02:00
|
|
|
if c.usesWebGL2() {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniformMatrix4fv.Invoke(js.Value(l), false, arr, 0, len(v))
|
2020-12-13 19:02:58 +01:00
|
|
|
} else {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.uniformMatrix4fv.Invoke(js.Value(l), false, arr.Call("subarray", 0, len(v)))
|
2020-12-13 19:02:58 +01:00
|
|
|
}
|
2015-01-03 07:21:47 +01:00
|
|
|
default:
|
2021-11-01 16:05:03 +01:00
|
|
|
panic(fmt.Sprintf("opengl: unexpected type: %s", typ.String()))
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
2020-07-30 19:52:25 +02:00
|
|
|
|
2020-05-27 04:07:21 +02:00
|
|
|
return true
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 15:02:22 +01:00
|
|
|
func (c *context) vertexAttribPointer(index int, size int, stride int, offset int) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.vertexAttribPointer.Invoke(index, size, gles.FLOAT, false, stride, offset)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 15:02:22 +01:00
|
|
|
func (c *context) enableVertexAttribArray(index int) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.enableVertexAttribArray.Invoke(index)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 15:02:22 +01:00
|
|
|
func (c *context) disableVertexAttribArray(index int) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.disableVertexAttribArray.Invoke(index)
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newArrayBuffer(size int) buffer {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
b := gl.createBuffer.Invoke()
|
|
|
|
gl.bindBuffer.Invoke(gles.ARRAY_BUFFER, js.Value(b))
|
|
|
|
gl.bufferData.Invoke(gles.ARRAY_BUFFER, size, gles.DYNAMIC_DRAW)
|
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 {
|
2017-08-31 18:29:56 +02:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
b := gl.createBuffer.Invoke()
|
|
|
|
gl.bindBuffer.Invoke(gles.ELEMENT_ARRAY_BUFFER, js.Value(b))
|
|
|
|
gl.bufferData.Invoke(gles.ELEMENT_ARRAY_BUFFER, size, gles.DYNAMIC_DRAW)
|
2018-10-29 17:52:59 +01:00
|
|
|
return buffer(b)
|
2015-01-17 04:45:19 +01:00
|
|
|
}
|
|
|
|
|
2020-11-21 14:59:30 +01:00
|
|
|
func (c *context) bindArrayBuffer(b buffer) {
|
2015-01-17 04:45:19 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.bindBuffer.Invoke(gles.ARRAY_BUFFER, js.Value(b))
|
2020-11-21 14:59:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) bindElementArrayBuffer(b buffer) {
|
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.bindBuffer.Invoke(gles.ELEMENT_ARRAY_BUFFER, 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) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2020-12-15 17:14:19 +01:00
|
|
|
l := len(data) * 4
|
2021-10-30 11:43:46 +02:00
|
|
|
arr := jsutil.TemporaryUint8ArrayFromFloat32Slice(l, data)
|
2021-08-01 14:50:21 +02:00
|
|
|
if c.usesWebGL2() {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.bufferSubData.Invoke(gles.ARRAY_BUFFER, 0, arr, 0, l)
|
2020-12-15 17:14:19 +01:00
|
|
|
} else {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.bufferSubData.Invoke(gles.ARRAY_BUFFER, 0, arr.Call("subarray", 0, l))
|
2020-12-15 17:14:19 +01:00
|
|
|
}
|
2018-05-30 17:53:02 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) elementArrayBufferSubData(data []uint16) {
|
2018-05-30 17:53:02 +02:00
|
|
|
gl := c.gl
|
2020-12-15 17:14:19 +01:00
|
|
|
l := len(data) * 2
|
2021-10-30 11:43:46 +02:00
|
|
|
arr := jsutil.TemporaryUint8ArrayFromUint16Slice(l, data)
|
2021-08-01 14:50:21 +02:00
|
|
|
if c.usesWebGL2() {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.bufferSubData.Invoke(gles.ELEMENT_ARRAY_BUFFER, 0, arr, 0, l)
|
2020-12-15 17:14:19 +01:00
|
|
|
} else {
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.bufferSubData.Invoke(gles.ELEMENT_ARRAY_BUFFER, 0, arr.Call("subarray", 0, l))
|
2020-12-15 17:14:19 +01:00
|
|
|
}
|
2014-12-31 13:55:40 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteBuffer(b buffer) {
|
2016-07-03 17:23:45 +02:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.deleteBuffer.Invoke(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) {
|
2014-12-31 13:55:40 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.drawElements.Invoke(gles.TRIANGLES, len, gles.UNSIGNED_SHORT, 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-03-09 03:03:55 +01:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
return gl.getParameter.Invoke(gles.MAX_TEXTURE_SIZE).Int()
|
2018-03-09 03:03:55 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) flush() {
|
2016-06-06 19:24:36 +02:00
|
|
|
gl := c.gl
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.flush.Invoke()
|
2016-06-06 19:24:36 +02:00
|
|
|
}
|
2019-05-26 12:08:46 +02:00
|
|
|
|
|
|
|
func (c *context) needsRestoring() bool {
|
2021-06-26 09:50:12 +02:00
|
|
|
// Though it is possible to have a logic to restore the graphics data for GPU, do not use it for performance (#1603).
|
|
|
|
return false
|
2019-05-26 12:08:46 +02:00
|
|
|
}
|
2020-01-01 19:58:49 +01:00
|
|
|
|
|
|
|
func (c *context) canUsePBO() bool {
|
2021-06-24 05:49:36 +02:00
|
|
|
return false
|
2020-01-01 19:58:49 +01:00
|
|
|
}
|
|
|
|
|
2022-08-07 20:02:12 +02:00
|
|
|
func (c *context) texSubImage2D(t textureNative, args []*graphicsdriver.WritePixelsArgs) {
|
2020-01-01 19:58:49 +01:00
|
|
|
c.bindTexture(t)
|
|
|
|
gl := c.gl
|
|
|
|
for _, a := range args {
|
2021-10-30 11:43:46 +02:00
|
|
|
arr := jsutil.TemporaryUint8ArrayFromUint8Slice(len(a.Pixels), a.Pixels)
|
2021-08-01 14:50:21 +02:00
|
|
|
if c.usesWebGL2() {
|
2020-12-15 17:14:19 +01:00
|
|
|
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
|
|
|
|
// GLsizei width, GLsizei height,
|
|
|
|
// GLenum format, GLenum type, ArrayBufferView pixels, srcOffset);
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.texSubImage2D.Invoke(gles.TEXTURE_2D, 0, a.X, a.Y, a.Width, a.Height, gles.RGBA, gles.UNSIGNED_BYTE, arr, 0)
|
2020-12-15 17:14:19 +01:00
|
|
|
} else {
|
|
|
|
// void texSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset,
|
|
|
|
// GLsizei width, GLsizei height,
|
|
|
|
// GLenum format, GLenum type, ArrayBufferView? pixels);
|
2021-04-05 16:53:17 +02:00
|
|
|
gl.texSubImage2D.Invoke(gles.TEXTURE_2D, 0, a.X, a.Y, a.Width, a.Height, gles.RGBA, gles.UNSIGNED_BYTE, arr)
|
2020-12-15 17:14:19 +01:00
|
|
|
}
|
2020-01-01 19:58:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
func (c *context) enableStencilTest() {
|
|
|
|
gl := c.gl
|
|
|
|
gl.enable.Invoke(gles.STENCIL_TEST)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) disableStencilTest() {
|
|
|
|
gl := c.gl
|
|
|
|
gl.disable.Invoke(gles.STENCIL_TEST)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) beginStencilWithEvenOddRule() {
|
|
|
|
gl := c.gl
|
2021-07-02 12:26:09 +02:00
|
|
|
gl.clear.Invoke(gles.STENCIL_BUFFER_BIT)
|
2021-07-02 12:26:09 +02:00
|
|
|
gl.stencilFunc.Invoke(gles.ALWAYS, 0x00, 0xff)
|
|
|
|
gl.stencilOp.Invoke(gles.KEEP, gles.KEEP, gles.INVERT)
|
|
|
|
gl.colorMask.Invoke(false, false, false, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) endStencilWithEvenOddRule() {
|
|
|
|
gl := c.gl
|
|
|
|
gl.stencilFunc.Invoke(gles.NOTEQUAL, 0x00, 0xff)
|
2022-01-05 19:19:46 +01:00
|
|
|
gl.stencilOp.Invoke(gles.KEEP, gles.KEEP, gles.KEEP)
|
2021-07-02 12:26:09 +02:00
|
|
|
gl.colorMask.Invoke(true, true, true, true)
|
|
|
|
}
|