2014-12-30 19:04:52 +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.
|
|
|
|
|
2021-06-10 18:03:35 +02:00
|
|
|
//go:build android || ios
|
2017-01-25 17:32:33 +01:00
|
|
|
// +build android ios
|
2014-12-31 10:23:18 +01:00
|
|
|
|
2014-12-30 19:04:52 +01:00
|
|
|
package opengl
|
|
|
|
|
|
|
|
import (
|
2014-12-31 06:57:51 +01:00
|
|
|
"errors"
|
2014-12-31 07:11:19 +01:00
|
|
|
"fmt"
|
2015-06-20 18:33:28 +02:00
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2020-10-18 11:09:10 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gles"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2014-12-30 19:04:52 +01:00
|
|
|
)
|
|
|
|
|
2017-12-11 15:07:01 +01:00
|
|
|
type (
|
2021-07-02 12:26:09 +02:00
|
|
|
textureNative uint32
|
|
|
|
renderbufferNative uint32
|
|
|
|
framebufferNative uint32
|
|
|
|
shader uint32
|
|
|
|
program uint32
|
|
|
|
buffer uint32
|
2017-12-11 15:07:01 +01:00
|
|
|
)
|
2015-01-16 16:56:29 +01:00
|
|
|
|
2019-12-18 16:03:35 +01:00
|
|
|
func (t textureNative) equal(rhs textureNative) bool {
|
|
|
|
return t == rhs
|
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
func (r renderbufferNative) equal(rhs renderbufferNative) bool {
|
|
|
|
return r == rhs
|
|
|
|
}
|
|
|
|
|
2019-12-18 16:03:35 +01:00
|
|
|
func (f framebufferNative) equal(rhs framebufferNative) bool {
|
|
|
|
return f == rhs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s shader) equal(rhs shader) bool {
|
|
|
|
return s == rhs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b buffer) equal(rhs buffer) bool {
|
|
|
|
return b == rhs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p program) equal(rhs program) bool {
|
|
|
|
return p == rhs
|
|
|
|
}
|
|
|
|
|
2018-11-04 11:46:20 +01:00
|
|
|
var InvalidTexture textureNative
|
2017-07-02 15:18:49 +02:00
|
|
|
|
2017-12-11 15:07:01 +01:00
|
|
|
type (
|
2020-10-18 11:28:23 +02:00
|
|
|
uniformLocation int32
|
|
|
|
attribLocation int32
|
2017-12-11 15:07:01 +01:00
|
|
|
)
|
2015-01-12 15:16:34 +01:00
|
|
|
|
2019-12-18 16:03:35 +01:00
|
|
|
func (u uniformLocation) equal(rhs uniformLocation) bool {
|
|
|
|
return u == rhs
|
|
|
|
}
|
|
|
|
|
2016-02-26 18:41:38 +01:00
|
|
|
type programID uint32
|
2015-01-12 15:16:34 +01:00
|
|
|
|
2020-10-18 11:28:23 +02:00
|
|
|
const (
|
|
|
|
invalidTexture = 0
|
|
|
|
invalidFramebuffer = (1 << 32) - 1
|
|
|
|
invalidUniform = -1
|
2016-07-09 16:14:24 +02:00
|
|
|
)
|
2016-06-17 21:46:33 +02:00
|
|
|
|
2018-10-29 17:52:59 +01:00
|
|
|
func getProgramID(p program) programID {
|
2020-10-18 11:28:23 +02:00
|
|
|
return programID(p)
|
2015-01-12 15:16:34 +01:00
|
|
|
}
|
2014-12-31 13:55:40 +01:00
|
|
|
|
2018-12-25 17:51:04 +01:00
|
|
|
const (
|
2020-10-18 11:09:10 +02:00
|
|
|
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)
|
2018-12-25 17:51:04 +01:00
|
|
|
)
|
2016-07-03 11:11:37 +02:00
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
type contextImpl struct {
|
2020-10-18 11:28:23 +02:00
|
|
|
ctx gles.Context
|
2016-07-03 18:25:35 +02: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()
|
2016-07-09 16:14:24 +02:00
|
|
|
c.lastTexture = invalidTexture
|
2016-06-17 21:46:33 +02:00
|
|
|
c.lastFramebuffer = invalidFramebuffer
|
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
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.Enable(gles.BLEND)
|
2020-11-07 11:14:06 +01:00
|
|
|
c.ctx.Enable(gles.SCISSOR_TEST)
|
2022-02-06 12:41:32 +01:00
|
|
|
c.blendFunc(graphicsdriver.CompositeModeSourceOver)
|
2020-10-18 11:28:23 +02:00
|
|
|
f := make([]int32, 1)
|
|
|
|
c.ctx.GetIntegerv(f, gles.FRAMEBUFFER_BINDING)
|
|
|
|
c.screenFramebuffer = framebufferNative(f[0])
|
2016-06-18 12:55:04 +02:00
|
|
|
// TODO: Need to update screenFramebufferWidth/Height?
|
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-05-07 12:12:19 +02:00
|
|
|
if c.lastCompositeMode == mode {
|
|
|
|
return
|
2016-02-21 14:20:33 +01:00
|
|
|
}
|
2016-05-07 12:12:19 +02:00
|
|
|
c.lastCompositeMode = mode
|
2018-10-28 12:42:57 +01:00
|
|
|
s, d := mode.Operations()
|
|
|
|
s2, d2 := convertOperation(s), convertOperation(d)
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.BlendFunc(uint32(s2), uint32(d2))
|
2016-02-21 14:20:33 +01:00
|
|
|
}
|
|
|
|
|
2020-11-07 11:14:06 +01:00
|
|
|
func (c *context) scissor(x, y, width, height int) {
|
|
|
|
c.ctx.Scissor(int32(x), int32(y), int32(width), int32(height))
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newTexture(width, height int) (textureNative, error) {
|
2020-10-18 11:28:23 +02:00
|
|
|
t := c.ctx.GenTextures(1)[0]
|
|
|
|
if t <= 0 {
|
|
|
|
return 0, errors.New("opengl: creating texture failed")
|
2014-12-31 06:57:51 +01:00
|
|
|
}
|
2018-11-04 11:46:20 +01:00
|
|
|
c.bindTexture(textureNative(t))
|
2014-12-31 06:57:51 +01:00
|
|
|
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.TexParameteri(gles.TEXTURE_2D, gles.TEXTURE_MAG_FILTER, gles.NEAREST)
|
|
|
|
c.ctx.TexParameteri(gles.TEXTURE_2D, gles.TEXTURE_MIN_FILTER, gles.NEAREST)
|
|
|
|
c.ctx.TexParameteri(gles.TEXTURE_2D, gles.TEXTURE_WRAP_S, gles.CLAMP_TO_EDGE)
|
|
|
|
c.ctx.TexParameteri(gles.TEXTURE_2D, gles.TEXTURE_WRAP_T, gles.CLAMP_TO_EDGE)
|
2021-07-02 19:09:44 +02:00
|
|
|
c.ctx.PixelStorei(gles.UNPACK_ALIGNMENT, 4)
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.TexImage2D(gles.TEXTURE_2D, 0, gles.RGBA, int32(width), int32(height), gles.RGBA, gles.UNSIGNED_BYTE, nil)
|
2014-12-31 06:57:51 +01:00
|
|
|
|
2018-11-04 11:46:20 +01:00
|
|
|
return textureNative(t), nil
|
2014-12-31 06:57:51 +01:00
|
|
|
}
|
2014-12-31 07:22:15 +01:00
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) bindFramebufferImpl(f framebufferNative) {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.BindFramebuffer(gles.FRAMEBUFFER, uint32(f))
|
2016-05-31 19:33:31 +02:00
|
|
|
}
|
|
|
|
|
2022-02-27 09:41:19 +01:00
|
|
|
func (c *context) framebufferPixels(buf []byte, f *framebuffer, width, height int) {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.Flush()
|
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
|
|
|
|
2022-02-27 09:41:19 +01:00
|
|
|
c.ctx.ReadPixels(buf, 0, 0, int32(width), int32(height), gles.RGBA, gles.UNSIGNED_BYTE)
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2020-08-15 08:11:56 +02:00
|
|
|
func (c *context) framebufferPixelsToBuffer(f *framebuffer, buffer buffer, width, height int) {
|
|
|
|
c.ctx.Flush()
|
|
|
|
|
|
|
|
c.bindFramebuffer(f.native)
|
|
|
|
|
|
|
|
c.ctx.BindBuffer(gles.PIXEL_PACK_BUFFER, uint32(buffer))
|
|
|
|
c.ctx.ReadPixels(nil, 0, 0, int32(width), int32(height), gles.RGBA, gles.UNSIGNED_BYTE)
|
|
|
|
c.ctx.BindBuffer(gles.PIXEL_PACK_BUFFER, 0)
|
|
|
|
}
|
|
|
|
|
2020-05-17 16:04:13 +02:00
|
|
|
func (c *context) activeTexture(idx int) {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.ActiveTexture(uint32(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) {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.BindTexture(gles.TEXTURE_2D, uint32(t))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteTexture(t textureNative) {
|
2020-10-18 11:28:23 +02:00
|
|
|
if !c.ctx.IsTexture(uint32(t)) {
|
2016-06-12 16:19:01 +02:00
|
|
|
return
|
|
|
|
}
|
2016-07-09 22:04:25 +02:00
|
|
|
if c.lastTexture == t {
|
|
|
|
c.lastTexture = invalidTexture
|
|
|
|
}
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.DeleteTextures([]uint32{uint32(t)})
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) isTexture(t textureNative) bool {
|
2020-10-18 11:28:23 +02:00
|
|
|
return c.ctx.IsTexture(uint32(t))
|
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) {
|
|
|
|
r := c.ctx.GenRenderbuffers(1)[0]
|
|
|
|
if r <= 0 {
|
|
|
|
return 0, errors.New("opengl: creating renderbuffer failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
renderbuffer := renderbufferNative(r)
|
|
|
|
c.bindRenderbuffer(renderbuffer)
|
|
|
|
|
|
|
|
c.ctx.RenderbufferStorage(gles.RENDERBUFFER, gles.STENCIL_INDEX8, int32(width), int32(height))
|
|
|
|
|
|
|
|
return renderbuffer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) bindRenderbufferImpl(r renderbufferNative) {
|
|
|
|
c.ctx.BindRenderbuffer(gles.RENDERBUFFER, uint32(r))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) deleteRenderbuffer(r renderbufferNative) {
|
|
|
|
if !c.ctx.IsRenderbuffer(uint32(r)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.lastRenderbuffer.equal(r) {
|
|
|
|
c.lastRenderbuffer = 0
|
|
|
|
}
|
|
|
|
c.ctx.DeleteRenderbuffers([]uint32{uint32(r)})
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newFramebuffer(texture textureNative) (framebufferNative, error) {
|
2020-10-18 11:28:23 +02:00
|
|
|
f := c.ctx.GenFramebuffers(1)[0]
|
|
|
|
if f <= 0 {
|
|
|
|
return 0, fmt.Errorf("opengl: creating framebuffer failed: the returned value is not positive but %d", f)
|
2016-02-24 15:30:43 +01:00
|
|
|
}
|
2018-11-04 09:43:26 +01:00
|
|
|
c.bindFramebuffer(framebufferNative(f))
|
2014-12-31 07:22:15 +01:00
|
|
|
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.FramebufferTexture2D(gles.FRAMEBUFFER, gles.COLOR_ATTACHMENT0, gles.TEXTURE_2D, uint32(texture), 0)
|
|
|
|
s := c.ctx.CheckFramebufferStatus(gles.FRAMEBUFFER)
|
2020-10-18 11:09:10 +02:00
|
|
|
if s != gles.FRAMEBUFFER_COMPLETE {
|
2016-02-05 19:48:15 +01:00
|
|
|
if s != 0 {
|
2020-10-18 11:28:23 +02:00
|
|
|
return 0, fmt.Errorf("opengl: creating framebuffer failed: %v", s)
|
2016-02-05 19:48:15 +01:00
|
|
|
}
|
2020-10-18 11:28:23 +02:00
|
|
|
if e := c.ctx.GetError(); e != gles.NO_ERROR {
|
|
|
|
return 0, fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e)
|
2016-02-05 19:48:15 +01:00
|
|
|
}
|
2020-10-18 11:28:23 +02:00
|
|
|
return 0, fmt.Errorf("opengl: creating framebuffer failed: unknown error")
|
2014-12-31 07:22:15 +01:00
|
|
|
}
|
2018-11-04 09:43:26 +01:00
|
|
|
return framebufferNative(f), nil
|
2014-12-31 07:22:15 +01:00
|
|
|
}
|
2014-12-31 08:12:13 +01:00
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
func (c *context) bindStencilBuffer(f framebufferNative, r renderbufferNative) error {
|
|
|
|
c.bindFramebuffer(f)
|
|
|
|
|
|
|
|
c.ctx.FramebufferRenderbuffer(gles.FRAMEBUFFER, gles.STENCIL_ATTACHMENT, gles.RENDERBUFFER, uint32(r))
|
|
|
|
if s := c.ctx.CheckFramebufferStatus(gles.FRAMEBUFFER); s != gles.FRAMEBUFFER_COMPLETE {
|
|
|
|
return errors.New(fmt.Sprintf("opengl: glFramebufferRenderbuffer failed: %d", s))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) setViewportImpl(width, height int) {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.Viewport(0, 0, int32(width), int32(height))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteFramebuffer(f framebufferNative) {
|
2020-10-18 11:28:23 +02:00
|
|
|
if !c.ctx.IsFramebuffer(uint32(f)) {
|
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 {
|
2016-06-17 21:46:33 +02:00
|
|
|
c.lastFramebuffer = invalidFramebuffer
|
2016-06-05 00:47:11 +02:00
|
|
|
c.lastViewportWidth = 0
|
|
|
|
c.lastViewportHeight = 0
|
2016-06-03 20:40:56 +02:00
|
|
|
}
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.DeleteFramebuffers([]uint32{uint32(f)})
|
2014-12-31 10:23:18 +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 uint32, source string) (shader, error) {
|
|
|
|
s := c.ctx.CreateShader(shaderType)
|
2020-10-18 11:28:23 +02:00
|
|
|
if s == 0 {
|
|
|
|
return 0, fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType)
|
2014-12-31 08:12:13 +01:00
|
|
|
}
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.ShaderSource(s, source)
|
|
|
|
c.ctx.CompileShader(s)
|
|
|
|
|
|
|
|
v := make([]int32, 1)
|
|
|
|
c.ctx.GetShaderiv(v, s, gles.COMPILE_STATUS)
|
|
|
|
if v[0] == gles.FALSE {
|
|
|
|
log := c.ctx.GetShaderInfoLog(s)
|
|
|
|
return 0, fmt.Errorf("opengl: shader compile failed: %s", log)
|
2014-12-31 08:12:13 +01:00
|
|
|
}
|
2018-10-29 17:52:59 +01:00
|
|
|
return shader(s), nil
|
2014-12-31 08:12:13 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteShader(s shader) {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.DeleteShader(uint32(s))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 06:03:21 +01:00
|
|
|
func (c *context) newProgram(shaders []shader, attributes []string) (program, error) {
|
2020-10-18 11:28:23 +02:00
|
|
|
p := c.ctx.CreateProgram()
|
|
|
|
if p == 0 {
|
|
|
|
return 0, errors.New("opengl: glCreateProgram failed")
|
2014-12-31 08:12:13 +01:00
|
|
|
}
|
|
|
|
|
2014-12-31 08:48:25 +01:00
|
|
|
for _, shader := range shaders {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.AttachShader(p, uint32(shader))
|
2014-12-31 08:48:25 +01:00
|
|
|
}
|
2019-02-16 06:03:21 +01:00
|
|
|
|
|
|
|
for i, name := range attributes {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.BindAttribLocation(p, uint32(i), name)
|
2019-02-16 06:03:21 +01:00
|
|
|
}
|
|
|
|
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.LinkProgram(p)
|
|
|
|
v := make([]int32, 1)
|
|
|
|
c.ctx.GetProgramiv(v, p, gles.LINK_STATUS)
|
|
|
|
if v[0] == gles.FALSE {
|
|
|
|
info := c.ctx.GetProgramInfoLog(p)
|
|
|
|
return 0, fmt.Errorf("opengl: program error: %s", info)
|
2014-12-31 08:12:13 +01:00
|
|
|
}
|
2018-10-29 17:52:59 +01:00
|
|
|
return program(p), nil
|
2014-12-31 08:12:13 +01:00
|
|
|
}
|
2014-12-31 09:45:23 +01:00
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) useProgram(p program) {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.UseProgram(uint32(p))
|
2014-12-31 10:23:18 +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)
|
|
|
|
|
2020-10-18 11:28:23 +02:00
|
|
|
if !c.ctx.IsProgram(uint32(p)) {
|
2016-07-12 19:07:35 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.DeleteProgram(uint32(p))
|
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 {
|
2020-10-18 11:28:23 +02:00
|
|
|
u := uniformLocation(c.ctx.GetUniformLocation(uint32(p), location))
|
2015-01-28 16:58:56 +01:00
|
|
|
return u
|
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 {
|
|
|
|
l := c.locationCache.GetUniformLocation(c, p, location)
|
|
|
|
if l == invalidUniform {
|
|
|
|
return false
|
|
|
|
}
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.Uniform1i(int32(l), int32(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 {
|
|
|
|
l := c.locationCache.GetUniformLocation(c, p, location)
|
|
|
|
if l == invalidUniform {
|
|
|
|
return false
|
|
|
|
}
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.Uniform1f(int32(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 {
|
2020-05-27 04:07:21 +02:00
|
|
|
l := c.locationCache.GetUniformLocation(c, p, location)
|
|
|
|
if l == invalidUniform {
|
|
|
|
return false
|
|
|
|
}
|
2020-07-30 19:52:25 +02:00
|
|
|
|
|
|
|
base := typ.Main
|
|
|
|
if base == shaderir.Array {
|
|
|
|
base = typ.Sub[0].Main
|
|
|
|
}
|
|
|
|
|
|
|
|
switch base {
|
|
|
|
case shaderir.Float:
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.Uniform1fv(int32(l), v)
|
2020-07-30 19:52:25 +02:00
|
|
|
case shaderir.Vec2:
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.Uniform2fv(int32(l), v)
|
2020-07-30 19:52:25 +02:00
|
|
|
case shaderir.Vec3:
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.Uniform3fv(int32(l), v)
|
2020-07-30 19:52:25 +02:00
|
|
|
case shaderir.Vec4:
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.Uniform4fv(int32(l), v)
|
2020-07-30 19:52:25 +02:00
|
|
|
case shaderir.Mat2:
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.UniformMatrix2fv(int32(l), false, v)
|
2020-07-30 19:52:25 +02:00
|
|
|
case shaderir.Mat3:
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.UniformMatrix3fv(int32(l), false, v)
|
2020-07-30 19:52:25 +02:00
|
|
|
case shaderir.Mat4:
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.UniformMatrix4fv(int32(l), false, v)
|
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 12:07:27 +01:00
|
|
|
}
|
2020-05-27 04:07:21 +02:00
|
|
|
return true
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 15:02:22 +01:00
|
|
|
func (c *context) vertexAttribPointer(index int, size int, stride int, offset int) {
|
2020-11-21 15:14:43 +01:00
|
|
|
c.ctx.VertexAttribPointer(uint32(index), int32(size), gles.FLOAT, false, int32(stride), offset)
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 15:02:22 +01:00
|
|
|
func (c *context) enableVertexAttribArray(index int) {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.EnableVertexAttribArray(uint32(index))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 15:02:22 +01:00
|
|
|
func (c *context) disableVertexAttribArray(index int) {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.DisableVertexAttribArray(uint32(index))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newArrayBuffer(size int) buffer {
|
2020-10-18 11:28:23 +02:00
|
|
|
b := c.ctx.GenBuffers(1)[0]
|
2020-11-21 14:59:30 +01:00
|
|
|
c.ctx.BindBuffer(gles.ARRAY_BUFFER, b)
|
|
|
|
c.ctx.BufferData(gles.ARRAY_BUFFER, size, nil, 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 {
|
2020-10-18 11:28:23 +02:00
|
|
|
b := c.ctx.GenBuffers(1)[0]
|
2020-11-21 14:59:30 +01:00
|
|
|
c.ctx.BindBuffer(gles.ELEMENT_ARRAY_BUFFER, b)
|
|
|
|
c.ctx.BufferData(gles.ELEMENT_ARRAY_BUFFER, size, nil, 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) {
|
|
|
|
c.ctx.BindBuffer(gles.ARRAY_BUFFER, uint32(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) bindElementArrayBuffer(b buffer) {
|
|
|
|
c.ctx.BindBuffer(gles.ELEMENT_ARRAY_BUFFER, uint32(b))
|
2014-12-31 09:45:23 +01:00
|
|
|
}
|
2014-12-31 09:53:04 +01:00
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) arrayBufferSubData(data []float32) {
|
2020-11-21 14:59:30 +01:00
|
|
|
c.ctx.BufferSubData(gles.ARRAY_BUFFER, 0, float32sToBytes(data))
|
2018-05-30 17:53:02 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) elementArrayBufferSubData(data []uint16) {
|
2020-11-21 14:59:30 +01:00
|
|
|
c.ctx.BufferSubData(gles.ELEMENT_ARRAY_BUFFER, 0, uint16sToBytes(data))
|
2014-12-31 09:53:04 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteBuffer(b buffer) {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.DeleteBuffers([]uint32{uint32(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) {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.DrawElements(gles.TRIANGLES, int32(len), gles.UNSIGNED_SHORT, offsetInBytes)
|
2014-12-31 09:53:04 +01:00
|
|
|
}
|
2016-06-06 19:24:36 +02:00
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) maxTextureSizeImpl() int {
|
2020-10-18 11:28:23 +02:00
|
|
|
v := make([]int32, 1)
|
|
|
|
c.ctx.GetIntegerv(v, gles.MAX_TEXTURE_SIZE)
|
|
|
|
return int(v[0])
|
2018-03-09 03:03:55 +01:00
|
|
|
}
|
|
|
|
|
2019-06-21 21:47:48 +02:00
|
|
|
func (c *context) getShaderPrecisionFormatPrecision() int {
|
2020-10-18 11:28:23 +02:00
|
|
|
_, _, p := c.ctx.GetShaderPrecisionFormat(gles.FRAGMENT_SHADER, gles.HIGH_FLOAT)
|
2019-06-21 21:47:48 +02:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) flush() {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.Flush()
|
2016-06-06 19:24:36 +02:00
|
|
|
}
|
2019-05-26 12:08:46 +02:00
|
|
|
|
|
|
|
func (c *context) needsRestoring() bool {
|
|
|
|
return true
|
|
|
|
}
|
2020-01-01 19:58:49 +01:00
|
|
|
|
|
|
|
func (c *context) canUsePBO() bool {
|
2020-01-19 08:10:49 +01:00
|
|
|
// On Android, using PBO might slow the applications, especially when coming back from the context lost.
|
|
|
|
// Let's not use PBO until we find a good solution.
|
2020-01-01 19:58:49 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
func (c *context) texSubImage2D(t textureNative, args []*graphicsdriver.ReplacePixelsArgs) {
|
2020-01-01 19:58:49 +01:00
|
|
|
c.bindTexture(t)
|
|
|
|
for _, a := range args {
|
2020-10-18 11:28:23 +02:00
|
|
|
c.ctx.TexSubImage2D(gles.TEXTURE_2D, 0, int32(a.X), int32(a.Y), int32(a.Width), int32(a.Height), gles.RGBA, gles.UNSIGNED_BYTE, a.Pixels)
|
2020-01-01 19:58:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
func (c *context) enableStencilTest() {
|
|
|
|
c.ctx.Enable(gles.STENCIL_TEST)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) disableStencilTest() {
|
|
|
|
c.ctx.Disable(gles.STENCIL_TEST)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) beginStencilWithEvenOddRule() {
|
2021-07-02 12:26:09 +02:00
|
|
|
c.ctx.Clear(gles.STENCIL_BUFFER_BIT)
|
2021-07-02 12:26:09 +02:00
|
|
|
c.ctx.StencilFunc(gles.ALWAYS, 0x00, 0xff)
|
|
|
|
c.ctx.StencilOp(gles.KEEP, gles.KEEP, gles.INVERT)
|
|
|
|
c.ctx.ColorMask(false, false, false, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) endStencilWithEvenOddRule() {
|
|
|
|
c.ctx.StencilFunc(gles.NOTEQUAL, 0x00, 0xff)
|
2022-01-05 19:19:46 +01:00
|
|
|
c.ctx.StencilOp(gles.KEEP, gles.KEEP, gles.KEEP)
|
2021-07-02 12:26:09 +02:00
|
|
|
c.ctx.ColorMask(true, true, true, true)
|
|
|
|
}
|