2016-02-25 17:58:19 +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-05-11 12:09:13 +02:00
|
|
|
// +build darwin freebsd linux windows
|
2016-02-25 17:58:19 +01:00
|
|
|
// +build !js
|
2016-05-18 20:17:50 +02:00
|
|
|
// +build !android
|
2016-06-15 17:49:22 +02:00
|
|
|
// +build !ios
|
2016-02-25 17:58:19 +01:00
|
|
|
|
|
|
|
package opengl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2019-06-25 17:43:09 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
2018-12-08 18:35:13 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphicsdriver/opengl/gl"
|
2020-07-30 19:52:25 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/shaderir"
|
2016-02-25 17:58:19 +01:00
|
|
|
)
|
|
|
|
|
2017-12-11 15:07:01 +01:00
|
|
|
type (
|
2018-11-04 11:46:20 +01:00
|
|
|
textureNative uint32
|
2018-11-04 09:43:26 +01:00
|
|
|
framebufferNative uint32
|
|
|
|
shader uint32
|
|
|
|
program uint32
|
|
|
|
buffer uint32
|
2017-12-11 15:07:01 +01:00
|
|
|
)
|
2016-02-25 17:58:19 +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 (u uniformLocation) equal(rhs uniformLocation) bool {
|
|
|
|
return u == 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 int32
|
|
|
|
attribLocation int32
|
|
|
|
)
|
2016-02-25 17:58:19 +01:00
|
|
|
|
2016-02-26 18:41:38 +01:00
|
|
|
type programID uint32
|
2016-02-25 17:58:19 +01:00
|
|
|
|
2016-07-09 16:14:24 +02:00
|
|
|
const (
|
|
|
|
invalidTexture = 0
|
|
|
|
invalidFramebuffer = (1 << 32) - 1
|
2020-05-27 04:07:21 +02:00
|
|
|
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 {
|
2016-02-26 18:41:38 +01:00
|
|
|
return programID(p)
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-12-25 17:51:04 +01:00
|
|
|
const (
|
|
|
|
vertexShader = shaderType(gl.VERTEX_SHADER)
|
|
|
|
fragmentShader = shaderType(gl.FRAGMENT_SHADER)
|
|
|
|
arrayBuffer = bufferType(gl.ARRAY_BUFFER)
|
|
|
|
elementArrayBuffer = bufferType(gl.ELEMENT_ARRAY_BUFFER)
|
|
|
|
dynamicDraw = bufferUsage(gl.DYNAMIC_DRAW)
|
|
|
|
short = dataType(gl.SHORT)
|
|
|
|
float = dataType(gl.FLOAT)
|
2016-07-03 11:11:37 +02:00
|
|
|
|
2018-12-25 17:51:04 +01:00
|
|
|
zero = operation(gl.ZERO)
|
|
|
|
one = operation(gl.ONE)
|
|
|
|
srcAlpha = operation(gl.SRC_ALPHA)
|
|
|
|
dstAlpha = operation(gl.DST_ALPHA)
|
|
|
|
oneMinusSrcAlpha = operation(gl.ONE_MINUS_SRC_ALPHA)
|
|
|
|
oneMinusDstAlpha = operation(gl.ONE_MINUS_DST_ALPHA)
|
2020-07-18 14:37:17 +02:00
|
|
|
dstColor = operation(gl.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 {
|
2018-11-04 15:32:18 +01:00
|
|
|
init bool
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) reset() error {
|
2019-06-05 17:19:12 +02:00
|
|
|
if err := c.t.Call(func() error {
|
2016-07-03 17:47:56 +02:00
|
|
|
if c.init {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Note that this initialization must be done after Loop is called.
|
2016-02-25 17:58:19 +01:00
|
|
|
if err := gl.Init(); err != nil {
|
2016-05-13 20:24:01 +02:00
|
|
|
return fmt.Errorf("opengl: initializing error %v", err)
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
2016-07-03 17:47:56 +02:00
|
|
|
c.init = true
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2017-05-29 18:46:48 +02:00
|
|
|
return err
|
2016-06-17 23:25:40 +02:00
|
|
|
}
|
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
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-06-17 23:25:40 +02:00
|
|
|
gl.Enable(gl.BLEND)
|
|
|
|
return nil
|
2017-05-29 18:46:48 +02:00
|
|
|
})
|
2019-06-25 17:43:09 +02:00
|
|
|
c.blendFunc(driver.CompositeModeSourceOver)
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-06-17 23:25:40 +02:00
|
|
|
f := int32(0)
|
|
|
|
gl.GetIntegerv(gl.FRAMEBUFFER_BINDING, &f)
|
2018-11-04 09:43:26 +01:00
|
|
|
c.screenFramebuffer = framebufferNative(f)
|
2016-06-17 23:25:40 +02:00
|
|
|
return nil
|
2017-05-29 18:46:48 +02:00
|
|
|
})
|
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) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-02-29 18:16:32 +01:00
|
|
|
if c.lastCompositeMode == mode {
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-28 17:44:09 +01:00
|
|
|
}
|
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)
|
|
|
|
gl.BlendFunc(uint32(s2), uint32(d2))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newTexture(width, height int) (textureNative, error) {
|
2018-11-04 11:46:20 +01:00
|
|
|
var texture textureNative
|
2019-06-05 17:19:12 +02:00
|
|
|
if err := c.t.Call(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
var t uint32
|
|
|
|
gl.GenTextures(1, &t)
|
2016-06-27 19:53:35 +02:00
|
|
|
// TODO: Use gl.IsTexture
|
2016-02-25 17:58:19 +01:00
|
|
|
if t <= 0 {
|
2016-05-13 20:24:01 +02:00
|
|
|
return errors.New("opengl: creating texture failed")
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
|
2018-11-04 11:46:20 +01:00
|
|
|
texture = textureNative(t)
|
2016-07-09 12:15:53 +02:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2018-11-01 19:43:42 +01:00
|
|
|
c.bindTexture(texture)
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2017-12-11 15:07:01 +01:00
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
|
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
|
2018-03-20 18:27:11 +01:00
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
|
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
|
2018-11-30 00:59:31 +01:00
|
|
|
// If data is nil, this just allocates memory and the content is undefined.
|
|
|
|
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml
|
2019-02-11 19:34:06 +01:00
|
|
|
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2017-09-24 17:11:19 +02:00
|
|
|
})
|
2016-05-13 20:24:01 +02:00
|
|
|
return texture, nil
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) bindFramebufferImpl(f framebufferNative) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2018-05-08 17:33:25 +02:00
|
|
|
gl.BindFramebufferEXT(gl.FRAMEBUFFER, uint32(f))
|
2016-06-18 15:47:34 +02:00
|
|
|
return nil
|
2017-09-24 17:14:25 +02:00
|
|
|
})
|
2016-05-31 19:33:31 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) framebufferPixels(f *framebuffer, width, height int) ([]byte, error) {
|
2018-02-28 16:27:55 +01:00
|
|
|
var pixels []byte
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2019-02-19 02:54:34 +01:00
|
|
|
gl.Flush()
|
2016-06-18 15:47:34 +02:00
|
|
|
return nil
|
2018-05-01 18:01:04 +02:00
|
|
|
})
|
2018-11-04 09:28:33 +01:00
|
|
|
c.bindFramebuffer(f.native)
|
2019-06-05 17:19:12 +02:00
|
|
|
if err := c.t.Call(func() error {
|
2018-02-28 16:27:55 +01:00
|
|
|
pixels = make([]byte, 4*width*height)
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.ReadPixels(0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(pixels))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pixels, nil
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2020-05-17 16:04:13 +02:00
|
|
|
func (c *context) activeTexture(idx int) {
|
|
|
|
_ = c.t.Call(func() error {
|
|
|
|
gl.ActiveTexture(gl.TEXTURE0 + uint32(idx))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) bindTextureImpl(t textureNative) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.BindTexture(gl.TEXTURE_2D, uint32(t))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteTexture(t textureNative) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
tt := uint32(t)
|
2016-06-12 16:19:01 +02:00
|
|
|
if !gl.IsTexture(tt) {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-09 22:04:25 +02:00
|
|
|
if c.lastTexture == t {
|
|
|
|
c.lastTexture = invalidTexture
|
|
|
|
}
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.DeleteTextures(1, &tt)
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) isTexture(t textureNative) bool {
|
2020-05-30 13:56:13 +02:00
|
|
|
panic("opengl: isTexture is not implemented")
|
2016-06-12 16:54:36 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newFramebuffer(texture textureNative) (framebufferNative, error) {
|
2018-11-04 09:43:26 +01:00
|
|
|
var framebuffer framebufferNative
|
2016-06-18 15:47:34 +02:00
|
|
|
var f uint32
|
2019-06-05 17:19:12 +02:00
|
|
|
if err := c.t.Call(func() error {
|
2018-05-08 17:33:25 +02:00
|
|
|
gl.GenFramebuffersEXT(1, &f)
|
2016-02-25 17:58:19 +01:00
|
|
|
// TODO: Use gl.IsFramebuffer
|
|
|
|
if f <= 0 {
|
2016-05-13 20:24:01 +02:00
|
|
|
return errors.New("opengl: creating framebuffer failed: gl.IsFramebuffer returns false")
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
2016-06-18 15:47:34 +02:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2018-11-04 09:43:26 +01:00
|
|
|
c.bindFramebuffer(framebufferNative(f))
|
2019-06-05 17:19:12 +02:00
|
|
|
if err := c.t.Call(func() error {
|
2018-05-08 17:33:25 +02:00
|
|
|
gl.FramebufferTexture2DEXT(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, uint32(texture), 0)
|
|
|
|
s := gl.CheckFramebufferStatusEXT(gl.FRAMEBUFFER)
|
2016-02-25 17:58:19 +01:00
|
|
|
if s != gl.FRAMEBUFFER_COMPLETE {
|
|
|
|
if s != 0 {
|
2016-05-13 20:24:01 +02:00
|
|
|
return fmt.Errorf("opengl: creating framebuffer failed: %v", s)
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
if e := gl.GetError(); e != gl.NO_ERROR {
|
2016-05-13 20:24:01 +02:00
|
|
|
return fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e)
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
2016-05-13 20:24:01 +02:00
|
|
|
return fmt.Errorf("opengl: creating framebuffer failed: unknown error")
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
2018-11-04 09:43:26 +01:00
|
|
|
framebuffer = framebufferNative(f)
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return framebuffer, nil
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) setViewportImpl(width, height int) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-06-18 15:47:34 +02:00
|
|
|
gl.Viewport(0, 0, int32(width), int32(height))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteFramebuffer(f framebufferNative) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
ff := uint32(f)
|
2018-05-08 17:33:25 +02:00
|
|
|
if !gl.IsFramebufferEXT(ff) {
|
2016-06-12 16:19:01 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-06-03 20:40:56 +02:00
|
|
|
if c.lastFramebuffer == f {
|
2016-06-17 21:46:33 +02:00
|
|
|
c.lastFramebuffer = invalidFramebuffer
|
2016-06-05 01:16:16 +02:00
|
|
|
c.lastViewportWidth = 0
|
|
|
|
c.lastViewportHeight = 0
|
2016-06-03 20:40:56 +02:00
|
|
|
}
|
2018-05-08 17:33:25 +02:00
|
|
|
gl.DeleteFramebuffersEXT(1, &ff)
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newShader(shaderType shaderType, source string) (shader, error) {
|
2018-10-29 17:52:59 +01:00
|
|
|
var sh shader
|
2019-06-05 17:19:12 +02:00
|
|
|
if err := c.t.Call(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
s := gl.CreateShader(uint32(shaderType))
|
|
|
|
if s == 0 {
|
2016-11-25 16:57:02 +01:00
|
|
|
return fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType)
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
cSources, free := gl.Strs(source + "\x00")
|
|
|
|
gl.ShaderSource(uint32(s), 1, cSources, nil)
|
|
|
|
free()
|
|
|
|
gl.CompileShader(s)
|
|
|
|
|
|
|
|
var v int32
|
|
|
|
gl.GetShaderiv(s, gl.COMPILE_STATUS, &v)
|
|
|
|
if v == gl.FALSE {
|
2018-02-28 16:27:55 +01:00
|
|
|
log := []byte{}
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.GetShaderiv(uint32(s), gl.INFO_LOG_LENGTH, &v)
|
|
|
|
if v != 0 {
|
2018-02-28 16:27:55 +01:00
|
|
|
log = make([]byte, int(v))
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.GetShaderInfoLog(uint32(s), v, nil, (*uint8)(gl.Ptr(log)))
|
|
|
|
}
|
2016-05-13 20:24:01 +02:00
|
|
|
return fmt.Errorf("opengl: shader compile failed: %s", log)
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
2018-10-29 17:52:59 +01:00
|
|
|
sh = shader(s)
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2018-10-29 17:52:59 +01:00
|
|
|
return sh, nil
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteShader(s shader) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.DeleteShader(uint32(s))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-16 06:03:21 +01:00
|
|
|
func (c *context) newProgram(shaders []shader, attributes []string) (program, error) {
|
2018-10-29 17:52:59 +01:00
|
|
|
var pr program
|
2019-06-05 17:19:12 +02:00
|
|
|
if err := c.t.Call(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
p := gl.CreateProgram()
|
|
|
|
if p == 0 {
|
2016-05-13 20:24:01 +02:00
|
|
|
return errors.New("opengl: glCreateProgram failed")
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, shader := range shaders {
|
|
|
|
gl.AttachShader(p, uint32(shader))
|
|
|
|
}
|
2019-02-16 06:03:21 +01:00
|
|
|
|
|
|
|
for i, name := range attributes {
|
|
|
|
l, free := gl.Strs(name + "\x00")
|
|
|
|
gl.BindAttribLocation(p, uint32(i), *l)
|
|
|
|
free()
|
|
|
|
}
|
|
|
|
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.LinkProgram(p)
|
|
|
|
var v int32
|
|
|
|
gl.GetProgramiv(p, gl.LINK_STATUS, &v)
|
|
|
|
if v == gl.FALSE {
|
2016-05-13 20:24:01 +02:00
|
|
|
return errors.New("opengl: program error")
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
2018-10-29 17:52:59 +01:00
|
|
|
pr = program(p)
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2018-10-29 17:52:59 +01:00
|
|
|
return pr, nil
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) useProgram(p program) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.UseProgram(uint32(p))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteProgram(p program) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-07-12 19:07:35 +02:00
|
|
|
if !gl.IsProgram(uint32(p)) {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-03 17:23:45 +02:00
|
|
|
gl.DeleteProgram(uint32(p))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) getUniformLocationImpl(p program, location string) uniformLocation {
|
2018-08-03 22:22:40 +02:00
|
|
|
l, free := gl.Strs(location + "\x00")
|
|
|
|
uniform := uniformLocation(gl.GetUniformLocation(uint32(p), *l))
|
|
|
|
free()
|
2016-02-25 17:58:19 +01:00
|
|
|
return uniform
|
|
|
|
}
|
|
|
|
|
2020-05-27 04:07:21 +02:00
|
|
|
func (c *context) uniformInt(p program, location string, v int) bool {
|
|
|
|
var r bool
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-02-26 19:01:55 +01:00
|
|
|
l := int32(c.locationCache.GetUniformLocation(c, p, location))
|
2020-05-27 04:07:21 +02:00
|
|
|
if l == invalidUniform {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r = true
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.Uniform1i(l, int32(v))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
2020-05-27 04:07:21 +02:00
|
|
|
return r
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 04:07:21 +02:00
|
|
|
func (c *context) uniformFloat(p program, location string, v float32) bool {
|
|
|
|
var r bool
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2018-02-22 03:46:46 +01:00
|
|
|
l := int32(c.locationCache.GetUniformLocation(c, p, location))
|
2020-05-27 04:07:21 +02:00
|
|
|
if l == invalidUniform {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r = true
|
2018-02-22 03:46:46 +01:00
|
|
|
gl.Uniform1f(l, v)
|
|
|
|
return nil
|
|
|
|
})
|
2020-05-27 04:07:21 +02:00
|
|
|
return r
|
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
|
|
|
var r bool
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-02-26 19:01:55 +01:00
|
|
|
l := int32(c.locationCache.GetUniformLocation(c, p, location))
|
2020-05-27 04:07:21 +02:00
|
|
|
if l == invalidUniform {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r = true
|
2020-07-30 19:52:25 +02:00
|
|
|
|
|
|
|
base := typ.Main
|
|
|
|
len := int32(1)
|
|
|
|
if base == shaderir.Array {
|
|
|
|
base = typ.Sub[0].Main
|
|
|
|
len = int32(typ.Length)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch base {
|
|
|
|
case shaderir.Float:
|
|
|
|
gl.Uniform1fv(l, len, (*float32)(gl.Ptr(v)))
|
|
|
|
case shaderir.Vec2:
|
|
|
|
gl.Uniform2fv(l, len, (*float32)(gl.Ptr(v)))
|
|
|
|
case shaderir.Vec3:
|
|
|
|
gl.Uniform3fv(l, len, (*float32)(gl.Ptr(v)))
|
|
|
|
case shaderir.Vec4:
|
|
|
|
gl.Uniform4fv(l, len, (*float32)(gl.Ptr(v)))
|
|
|
|
case shaderir.Mat2:
|
|
|
|
gl.UniformMatrix2fv(l, len, false, (*float32)(gl.Ptr(v)))
|
|
|
|
case shaderir.Mat3:
|
|
|
|
gl.UniformMatrix3fv(l, len, false, (*float32)(gl.Ptr(v)))
|
|
|
|
case shaderir.Mat4:
|
|
|
|
gl.UniformMatrix4fv(l, len, false, (*float32)(gl.Ptr(v)))
|
2016-02-25 17:58:19 +01:00
|
|
|
default:
|
2020-07-30 19:52:25 +02:00
|
|
|
panic(fmt.Sprintf("opengl: unexpected type: %s", typ.String()))
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
2020-05-27 04:07:21 +02:00
|
|
|
return r
|
2016-02-25 17:58:19 +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) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2019-09-30 18:47:15 +02:00
|
|
|
gl.VertexAttribPointer(uint32(index), int32(size), uint32(dataType), false, int32(stride), uintptr(offset))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-16 06:03:21 +01:00
|
|
|
func (c *context) enableVertexAttribArray(p program, index int) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2019-02-16 06:03:21 +01:00
|
|
|
gl.EnableVertexAttribArray(uint32(index))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-16 06:03:21 +01:00
|
|
|
func (c *context) disableVertexAttribArray(p program, index int) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2019-02-16 06:03:21 +01:00
|
|
|
gl.DisableVertexAttribArray(uint32(index))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newArrayBuffer(size int) buffer {
|
2018-10-29 17:52:59 +01:00
|
|
|
var bf buffer
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
var b uint32
|
|
|
|
gl.GenBuffers(1, &b)
|
2018-10-30 14:41:05 +01:00
|
|
|
gl.BindBuffer(uint32(arrayBuffer), b)
|
|
|
|
gl.BufferData(uint32(arrayBuffer), size, nil, uint32(dynamicDraw))
|
2018-10-29 17:52:59 +01:00
|
|
|
bf = buffer(b)
|
2017-08-31 18:29:56 +02:00
|
|
|
return nil
|
|
|
|
})
|
2018-10-29 17:52:59 +01:00
|
|
|
return bf
|
2017-08-31 18:29:56 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newElementArrayBuffer(size int) buffer {
|
2018-10-29 17:52:59 +01:00
|
|
|
var bf buffer
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2017-08-31 18:29:56 +02:00
|
|
|
var b uint32
|
|
|
|
gl.GenBuffers(1, &b)
|
2018-10-30 14:41:05 +01:00
|
|
|
gl.BindBuffer(uint32(elementArrayBuffer), b)
|
|
|
|
gl.BufferData(uint32(elementArrayBuffer), size, nil, uint32(dynamicDraw))
|
2018-10-29 17:52:59 +01:00
|
|
|
bf = buffer(b)
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
2018-10-29 17:52:59 +01:00
|
|
|
return bf
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) bindBuffer(bufferType bufferType, b buffer) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2018-05-27 17:33:58 +02:00
|
|
|
gl.BindBuffer(uint32(bufferType), uint32(b))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) arrayBufferSubData(data []float32) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2018-10-30 14:41:05 +01:00
|
|
|
gl.BufferSubData(uint32(arrayBuffer), 0, len(data)*4, gl.Ptr(data))
|
2018-05-30 17:53:02 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) elementArrayBufferSubData(data []uint16) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2018-10-30 14:41:05 +01:00
|
|
|
gl.BufferSubData(uint32(elementArrayBuffer), 0, len(data)*2, gl.Ptr(data))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteBuffer(b buffer) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-07-03 17:23:45 +02:00
|
|
|
bb := uint32(b)
|
|
|
|
gl.DeleteBuffers(1, &bb)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) drawElements(len int, offsetInBytes int) {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2019-09-30 18:47:15 +02:00
|
|
|
gl.DrawElements(gl.TRIANGLES, int32(len), gl.UNSIGNED_SHORT, uintptr(offsetInBytes))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +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
|
|
|
size := 0
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2018-03-09 03:03:55 +01:00
|
|
|
s := int32(0)
|
|
|
|
gl.GetIntegerv(gl.MAX_TEXTURE_SIZE, &s)
|
|
|
|
size = int(s)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2019-06-21 21:47:48 +02:00
|
|
|
func (c *context) getShaderPrecisionFormatPrecision() int {
|
|
|
|
// glGetShaderPrecisionFormat is not defined at OpenGL 2.0. Assume that desktop environments always have
|
|
|
|
// enough highp precision.
|
|
|
|
return highpPrecision
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) flush() {
|
2019-06-05 17:19:12 +02:00
|
|
|
_ = c.t.Call(func() error {
|
2016-06-06 19:24:36 +02:00
|
|
|
gl.Flush()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2019-05-26 12:08:46 +02:00
|
|
|
|
|
|
|
func (c *context) needsRestoring() bool {
|
|
|
|
return false
|
|
|
|
}
|
2019-11-16 18:10:10 +01:00
|
|
|
|
2020-01-01 19:58:49 +01:00
|
|
|
func (c *context) canUsePBO() bool {
|
2020-07-23 12:15:34 +02:00
|
|
|
var available bool
|
|
|
|
_ = c.t.Call(func() error {
|
|
|
|
available = isPBOAvailable()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return available
|
2020-01-01 19:58:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) texSubImage2D(t textureNative, width, height int, args []*driver.ReplacePixelsArgs) {
|
2020-07-23 12:15:34 +02:00
|
|
|
c.bindTexture(t)
|
|
|
|
_ = c.t.Call(func() error {
|
|
|
|
for _, a := range args {
|
|
|
|
gl.TexSubImage2D(gl.TEXTURE_2D, 0, int32(a.X), int32(a.Y), int32(a.Width), int32(a.Height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(a.Pixels))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2020-01-01 19:58:49 +01:00
|
|
|
}
|
|
|
|
|
2019-11-16 18:10:10 +01:00
|
|
|
func (c *context) newPixelBufferObject(width, height int) buffer {
|
|
|
|
var bf buffer
|
|
|
|
_ = c.t.Call(func() error {
|
|
|
|
var b uint32
|
|
|
|
gl.GenBuffers(1, &b)
|
|
|
|
gl.BindBuffer(gl.PIXEL_UNPACK_BUFFER, b)
|
|
|
|
gl.BufferData(gl.PIXEL_UNPACK_BUFFER, 4*width*height, nil, gl.STREAM_DRAW)
|
|
|
|
gl.BindBuffer(gl.PIXEL_UNPACK_BUFFER, 0)
|
|
|
|
bf = buffer(b)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return bf
|
|
|
|
}
|
|
|
|
|
2019-12-31 18:24:18 +01:00
|
|
|
func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, height int, args []*driver.ReplacePixelsArgs) {
|
|
|
|
c.bindTexture(t)
|
2019-11-16 18:10:10 +01:00
|
|
|
_ = c.t.Call(func() error {
|
|
|
|
gl.BindBuffer(gl.PIXEL_UNPACK_BUFFER, uint32(buffer))
|
|
|
|
|
2019-12-31 18:24:18 +01:00
|
|
|
stride := 4 * width
|
|
|
|
for _, a := range args {
|
|
|
|
offset := 4 * (a.Y*width + a.X)
|
|
|
|
for j := 0; j < a.Height; j++ {
|
|
|
|
gl.BufferSubData(gl.PIXEL_UNPACK_BUFFER, offset+stride*j, 4*a.Width, gl.Ptr(a.Pixels[4*a.Width*j:4*a.Width*(j+1)]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-24 10:07:21 +01:00
|
|
|
gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, nil)
|
2019-11-16 18:10:10 +01:00
|
|
|
gl.BindBuffer(gl.PIXEL_UNPACK_BUFFER, 0)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|