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.
|
|
|
|
|
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
|
|
|
|
2016-02-18 19:06:23 +01:00
|
|
|
mgl "golang.org/x/mobile/gl"
|
2018-10-28 12:42:57 +01:00
|
|
|
|
2019-06-25 17:43:09 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
2014-12-30 19:04:52 +01:00
|
|
|
)
|
|
|
|
|
2017-12-11 15:07:01 +01:00
|
|
|
type (
|
2018-11-04 11:46:20 +01:00
|
|
|
textureNative mgl.Texture
|
2018-11-04 09:43:26 +01:00
|
|
|
framebufferNative mgl.Framebuffer
|
|
|
|
shader mgl.Shader
|
|
|
|
program mgl.Program
|
|
|
|
buffer mgl.Buffer
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (
|
|
|
|
uniformLocation mgl.Uniform
|
|
|
|
attribLocation mgl.Attrib
|
|
|
|
)
|
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
|
|
|
|
2016-07-09 16:14:24 +02:00
|
|
|
var (
|
2018-11-04 11:46:20 +01:00
|
|
|
invalidTexture = textureNative(mgl.Texture{})
|
2018-11-04 09:43:26 +01:00
|
|
|
invalidFramebuffer = framebufferNative(mgl.Framebuffer{(1 << 32) - 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 {
|
2016-02-26 18:41:38 +01:00
|
|
|
return programID(p.Value)
|
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 (
|
|
|
|
vertexShader = shaderType(mgl.VERTEX_SHADER)
|
|
|
|
fragmentShader = shaderType(mgl.FRAGMENT_SHADER)
|
|
|
|
arrayBuffer = bufferType(mgl.ARRAY_BUFFER)
|
|
|
|
elementArrayBuffer = bufferType(mgl.ELEMENT_ARRAY_BUFFER)
|
|
|
|
dynamicDraw = bufferUsage(mgl.DYNAMIC_DRAW)
|
|
|
|
short = dataType(mgl.SHORT)
|
|
|
|
float = dataType(mgl.FLOAT)
|
|
|
|
|
|
|
|
zero = operation(mgl.ZERO)
|
|
|
|
one = operation(mgl.ONE)
|
|
|
|
srcAlpha = operation(mgl.SRC_ALPHA)
|
|
|
|
dstAlpha = operation(mgl.DST_ALPHA)
|
|
|
|
oneMinusSrcAlpha = operation(mgl.ONE_MINUS_SRC_ALPHA)
|
|
|
|
oneMinusDstAlpha = operation(mgl.ONE_MINUS_DST_ALPHA)
|
|
|
|
)
|
2016-07-03 11:11:37 +02:00
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
type contextImpl struct {
|
2019-06-26 17:07:39 +02:00
|
|
|
gl mgl.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
|
2019-06-25 17:43:09 +02:00
|
|
|
c.lastCompositeMode = driver.CompositeModeUnknown
|
2016-06-09 18:19:10 +02:00
|
|
|
c.gl.Enable(mgl.BLEND)
|
2019-06-25 17:43:09 +02:00
|
|
|
c.blendFunc(driver.CompositeModeSourceOver)
|
2016-06-17 21:46:33 +02:00
|
|
|
f := c.gl.GetInteger(mgl.FRAMEBUFFER_BINDING)
|
2018-11-04 09:43:26 +01:00
|
|
|
c.screenFramebuffer = framebufferNative(mgl.Framebuffer{uint32(f)})
|
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
|
|
|
}
|
|
|
|
|
2019-06-25 17:43:09 +02:00
|
|
|
func (c *context) blendFunc(mode driver.CompositeMode) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
|
|
|
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)
|
|
|
|
gl.BlendFunc(mgl.Enum(s2), mgl.Enum(d2))
|
2016-02-21 14:20:33 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newTexture(width, height int) (textureNative, error) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
t := gl.CreateTexture()
|
2016-02-24 18:17:28 +01:00
|
|
|
if t.Value <= 0 {
|
2018-11-04 11:46:20 +01:00
|
|
|
return textureNative{}, errors.New("opengl: creating texture failed")
|
2014-12-31 06:57:51 +01:00
|
|
|
}
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.PixelStorei(mgl.UNPACK_ALIGNMENT, 4)
|
2018-11-04 11:46:20 +01:00
|
|
|
c.bindTexture(textureNative(t))
|
2014-12-31 06:57:51 +01:00
|
|
|
|
2017-12-11 15:07:01 +01:00
|
|
|
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MAG_FILTER, mgl.NEAREST)
|
|
|
|
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_MIN_FILTER, mgl.NEAREST)
|
2018-03-20 18:27:11 +01:00
|
|
|
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_WRAP_S, mgl.CLAMP_TO_EDGE)
|
|
|
|
gl.TexParameteri(mgl.TEXTURE_2D, mgl.TEXTURE_WRAP_T, mgl.CLAMP_TO_EDGE)
|
2019-02-11 19:34:06 +01:00
|
|
|
gl.TexImage2D(mgl.TEXTURE_2D, 0, mgl.RGBA, width, height, mgl.RGBA, mgl.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) {
|
2016-05-31 19:33:31 +02:00
|
|
|
gl := c.gl
|
|
|
|
gl.BindFramebuffer(mgl.FRAMEBUFFER, mgl.Framebuffer(f))
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) framebufferPixels(f *framebuffer, width, height int) ([]byte, error) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2019-02-19 02:54:34 +01:00
|
|
|
gl.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
|
|
|
|
2018-02-28 16:27:55 +01:00
|
|
|
pixels := make([]byte, 4*width*height)
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.ReadPixels(pixels, 0, 0, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE)
|
2014-12-31 10:23:18 +01:00
|
|
|
return pixels, nil
|
|
|
|
}
|
|
|
|
|
2020-05-17 16:04:13 +02:00
|
|
|
func (c *context) activeTexture(idx int) {
|
|
|
|
gl := c.gl
|
|
|
|
gl.ActiveTexture(mgl.Enum(mgl.TEXTURE0 + idx))
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) bindTextureImpl(t textureNative) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.BindTexture(mgl.TEXTURE_2D, mgl.Texture(t))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteTexture(t textureNative) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-06-12 16:19:01 +02:00
|
|
|
if !gl.IsTexture(mgl.Texture(t)) {
|
|
|
|
return
|
|
|
|
}
|
2016-07-09 22:04:25 +02:00
|
|
|
if c.lastTexture == t {
|
|
|
|
c.lastTexture = invalidTexture
|
|
|
|
}
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.DeleteTexture(mgl.Texture(t))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) isTexture(t textureNative) bool {
|
2016-06-12 16:54:36 +02:00
|
|
|
gl := c.gl
|
|
|
|
return gl.IsTexture(mgl.Texture(t))
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newFramebuffer(texture textureNative) (framebufferNative, error) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
f := gl.CreateFramebuffer()
|
2016-02-24 18:17:28 +01:00
|
|
|
if f.Value <= 0 {
|
2018-11-04 09:43:26 +01:00
|
|
|
return framebufferNative{}, errors.New("opengl: creating framebuffer failed: gl.IsFramebuffer returns false")
|
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
|
|
|
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.FramebufferTexture2D(mgl.FRAMEBUFFER, mgl.COLOR_ATTACHMENT0, mgl.TEXTURE_2D, mgl.Texture(texture), 0)
|
|
|
|
s := gl.CheckFramebufferStatus(mgl.FRAMEBUFFER)
|
|
|
|
if s != mgl.FRAMEBUFFER_COMPLETE {
|
2016-02-05 19:48:15 +01:00
|
|
|
if s != 0 {
|
2018-11-04 09:43:26 +01:00
|
|
|
return framebufferNative{}, fmt.Errorf("opengl: creating framebuffer failed: %v", s)
|
2016-02-05 19:48:15 +01:00
|
|
|
}
|
2016-02-18 19:06:23 +01:00
|
|
|
if e := gl.GetError(); e != mgl.NO_ERROR {
|
2018-11-04 09:43:26 +01:00
|
|
|
return framebufferNative{}, fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e)
|
2016-02-05 19:48:15 +01:00
|
|
|
}
|
2018-11-04 09:43:26 +01:00
|
|
|
return framebufferNative{}, 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
|
|
|
|
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
|
|
|
|
gl.Viewport(0, 0, width, height)
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteFramebuffer(f framebufferNative) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-06-12 16:19:01 +02:00
|
|
|
if !gl.IsFramebuffer(mgl.Framebuffer(f)) {
|
|
|
|
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
|
|
|
}
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.DeleteFramebuffer(mgl.Framebuffer(f))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newShader(shaderType shaderType, source string) (shader, error) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
s := gl.CreateShader(mgl.Enum(shaderType))
|
|
|
|
if s.Value == 0 {
|
2018-10-29 17:52:59 +01:00
|
|
|
return shader{}, fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType)
|
2014-12-31 08:12:13 +01:00
|
|
|
}
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.ShaderSource(s, source)
|
2015-01-28 16:58:56 +01:00
|
|
|
gl.CompileShader(s)
|
|
|
|
|
2016-02-18 19:06:23 +01:00
|
|
|
v := gl.GetShaderi(s, mgl.COMPILE_STATUS)
|
|
|
|
if v == mgl.FALSE {
|
|
|
|
log := gl.GetShaderInfoLog(s)
|
2018-10-29 17:52:59 +01:00
|
|
|
return shader{}, 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) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.DeleteShader(mgl.Shader(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) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2014-12-31 08:12:13 +01:00
|
|
|
p := gl.CreateProgram()
|
2016-02-18 19:06:23 +01:00
|
|
|
if p.Value == 0 {
|
2018-10-29 17:52:59 +01:00
|
|
|
return program{}, 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 {
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.AttachShader(p, mgl.Shader(shader))
|
2014-12-31 08:48:25 +01:00
|
|
|
}
|
2019-02-16 06:03:21 +01:00
|
|
|
|
|
|
|
for i, name := range attributes {
|
2020-01-13 10:55:19 +01:00
|
|
|
gl.BindAttribLocation(p, mgl.Attrib{Value: uint(i)}, name)
|
2019-02-16 06:03:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-28 16:58:56 +01:00
|
|
|
gl.LinkProgram(p)
|
2016-02-18 19:06:23 +01:00
|
|
|
v := gl.GetProgrami(p, mgl.LINK_STATUS)
|
|
|
|
if v == mgl.FALSE {
|
2018-10-29 17:52:59 +01:00
|
|
|
return program{}, errors.New("opengl: program error")
|
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) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.UseProgram(mgl.Program(p))
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteProgram(p program) {
|
2016-07-03 17:23:45 +02:00
|
|
|
gl := c.gl
|
2016-07-12 19:07:35 +02:00
|
|
|
if !gl.IsProgram(mgl.Program(p)) {
|
|
|
|
return
|
|
|
|
}
|
2016-07-03 17:23:45 +02:00
|
|
|
gl.DeleteProgram(mgl.Program(p))
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) getUniformLocationImpl(p program, location string) uniformLocation {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-26 19:13:42 +01:00
|
|
|
u := uniformLocation(gl.GetUniformLocation(mgl.Program(p), location))
|
2016-02-18 19:06:23 +01:00
|
|
|
if u.Value == -1 {
|
2015-01-28 16:58:56 +01:00
|
|
|
panic("invalid uniform location: " + location)
|
|
|
|
}
|
|
|
|
return u
|
2015-01-12 15:16:34 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) uniformInt(p program, location string, v int) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-26 19:01:55 +01:00
|
|
|
gl.Uniform1i(mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location)), v)
|
2015-01-03 07:52:02 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) uniformFloat(p program, location string, v float32) {
|
2018-02-22 03:46:46 +01:00
|
|
|
gl := c.gl
|
|
|
|
gl.Uniform1f(mgl.Uniform(c.locationCache.GetUniformLocation(c, p, location)), v)
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) uniformFloats(p program, location string, v []float32) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-26 19:01:55 +01:00
|
|
|
l := mgl.Uniform(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:
|
|
|
|
gl.Uniform2fv(l, v)
|
2015-01-03 07:52:02 +01:00
|
|
|
case 4:
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.Uniform4fv(l, v)
|
2015-01-03 07:52:02 +01:00
|
|
|
case 16:
|
2016-02-18 19:06:23 +01:00
|
|
|
gl.UniformMatrix4fv(l, v)
|
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 12:07:27 +01:00
|
|
|
}
|
2014-12-31 10:23:18 +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) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2020-01-13 10:55:19 +01:00
|
|
|
gl.VertexAttribPointer(mgl.Attrib{Value: uint(index)}, size, mgl.Enum(dataType), false, stride, offset)
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 06:03:21 +01:00
|
|
|
func (c *context) enableVertexAttribArray(p program, index int) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2020-01-13 10:55:19 +01:00
|
|
|
gl.EnableVertexAttribArray(mgl.Attrib{Value: uint(index)})
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 06:03:21 +01:00
|
|
|
func (c *context) disableVertexAttribArray(p program, index int) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2020-01-13 10:55:19 +01:00
|
|
|
gl.DisableVertexAttribArray(mgl.Attrib{Value: uint(index)})
|
2014-12-31 10:23:18 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newArrayBuffer(size int) buffer {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2016-02-18 19:06:23 +01:00
|
|
|
b := gl.CreateBuffer()
|
2018-10-30 14:41:05 +01:00
|
|
|
gl.BindBuffer(mgl.Enum(arrayBuffer), b)
|
|
|
|
gl.BufferInit(mgl.Enum(arrayBuffer), size, mgl.Enum(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 {
|
2017-08-31 18:29:56 +02:00
|
|
|
gl := c.gl
|
|
|
|
b := gl.CreateBuffer()
|
2018-10-30 14:41:05 +01:00
|
|
|
gl.BindBuffer(mgl.Enum(elementArrayBuffer), b)
|
|
|
|
gl.BufferInit(mgl.Enum(elementArrayBuffer), size, mgl.Enum(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) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2018-05-27 17:33:58 +02:00
|
|
|
gl.BindBuffer(mgl.Enum(bufferType), mgl.Buffer(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) {
|
2018-05-30 17:53:02 +02:00
|
|
|
gl := c.gl
|
2018-10-30 14:41:05 +01:00
|
|
|
gl.BufferSubData(mgl.Enum(arrayBuffer), 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) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2018-10-30 14:41:05 +01:00
|
|
|
gl.BufferSubData(mgl.Enum(elementArrayBuffer), 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) {
|
2016-07-03 17:23:45 +02:00
|
|
|
gl := c.gl
|
|
|
|
gl.DeleteBuffer(mgl.Buffer(b))
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) drawElements(len int, offsetInBytes int) {
|
2016-05-07 12:12:19 +02:00
|
|
|
gl := c.gl
|
2018-10-30 14:29:54 +01:00
|
|
|
gl.DrawElements(mgl.TRIANGLES, len, mgl.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 {
|
2018-03-09 03:03:55 +01:00
|
|
|
gl := c.gl
|
|
|
|
return gl.GetInteger(mgl.MAX_TEXTURE_SIZE)
|
|
|
|
}
|
|
|
|
|
2019-06-21 21:47:48 +02:00
|
|
|
func (c *context) getShaderPrecisionFormatPrecision() int {
|
|
|
|
gl := c.gl
|
|
|
|
_, _, p := gl.GetShaderPrecisionFormat(mgl.FRAGMENT_SHADER, mgl.HIGH_FLOAT)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) flush() {
|
2016-06-06 19:24:36 +02:00
|
|
|
gl := c.gl
|
|
|
|
gl.Flush()
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) texSubImage2D(t textureNative, width, height int, args []*driver.ReplacePixelsArgs) {
|
|
|
|
c.bindTexture(t)
|
|
|
|
gl := c.gl
|
|
|
|
for _, a := range args {
|
|
|
|
gl.TexSubImage2D(mgl.TEXTURE_2D, 0, a.X, a.Y, a.Width, a.Height, mgl.RGBA, mgl.UNSIGNED_BYTE, a.Pixels)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) newPixelBufferObject(width, height int) buffer {
|
|
|
|
gl := c.gl
|
|
|
|
b := gl.CreateBuffer()
|
|
|
|
gl.BindBuffer(mgl.PIXEL_UNPACK_BUFFER, b)
|
|
|
|
gl.BufferInit(mgl.PIXEL_UNPACK_BUFFER, 4*width*height, mgl.STREAM_DRAW)
|
|
|
|
gl.BindBuffer(mgl.PIXEL_UNPACK_BUFFER, mgl.Buffer{0})
|
|
|
|
return buffer(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, height int, args []*driver.ReplacePixelsArgs) {
|
2020-01-19 08:10:49 +01:00
|
|
|
// This implementation is not used yet so far. See the comment at canUsePBO.
|
|
|
|
|
2020-01-01 19:58:49 +01:00
|
|
|
c.bindTexture(t)
|
|
|
|
gl := c.gl
|
|
|
|
gl.BindBuffer(mgl.PIXEL_UNPACK_BUFFER, mgl.Buffer(buffer))
|
|
|
|
|
|
|
|
stride := 4 * width
|
|
|
|
for _, a := range args {
|
|
|
|
offset := 4 * (a.Y*width + a.X)
|
|
|
|
for j := 0; j < a.Height; j++ {
|
|
|
|
gl.BufferSubData(mgl.PIXEL_UNPACK_BUFFER, offset+stride*j, a.Pixels[4*a.Width*j:4*a.Width*(j+1)])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gl.TexSubImage2D(mgl.TEXTURE_2D, 0, 0, 0, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE, nil)
|
|
|
|
gl.BindBuffer(mgl.PIXEL_UNPACK_BUFFER, mgl.Buffer{0})
|
|
|
|
}
|