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"
|
|
|
|
|
|
|
|
"github.com/go-gl/gl/v2.1/gl"
|
|
|
|
)
|
|
|
|
|
2017-12-11 15:07:01 +01:00
|
|
|
type (
|
|
|
|
Texture uint32
|
|
|
|
Framebuffer uint32
|
|
|
|
Shader uint32
|
|
|
|
Program uint32
|
|
|
|
Buffer uint32
|
|
|
|
)
|
2016-02-25 17:58:19 +01:00
|
|
|
|
2018-02-18 18:49:00 +01:00
|
|
|
var InvalidTexture Texture
|
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
|
|
|
|
)
|
2016-06-17 21:46:33 +02:00
|
|
|
|
2018-02-18 17:45:03 +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
|
|
|
}
|
|
|
|
|
2016-07-03 11:11:37 +02:00
|
|
|
func init() {
|
|
|
|
VertexShader = gl.VERTEX_SHADER
|
|
|
|
FragmentShader = gl.FRAGMENT_SHADER
|
|
|
|
ArrayBuffer = gl.ARRAY_BUFFER
|
|
|
|
ElementArrayBuffer = gl.ELEMENT_ARRAY_BUFFER
|
|
|
|
DynamicDraw = gl.DYNAMIC_DRAW
|
|
|
|
StaticDraw = gl.STATIC_DRAW
|
|
|
|
Triangles = gl.TRIANGLES
|
|
|
|
Lines = gl.LINES
|
2016-10-22 07:51:23 +02:00
|
|
|
Short = gl.SHORT
|
|
|
|
Float = gl.FLOAT
|
2016-07-03 11:11:37 +02:00
|
|
|
|
|
|
|
zero = gl.ZERO
|
|
|
|
one = gl.ONE
|
|
|
|
srcAlpha = gl.SRC_ALPHA
|
|
|
|
dstAlpha = gl.DST_ALPHA
|
|
|
|
oneMinusSrcAlpha = gl.ONE_MINUS_SRC_ALPHA
|
|
|
|
oneMinusDstAlpha = gl.ONE_MINUS_DST_ALPHA
|
|
|
|
}
|
|
|
|
|
2016-02-25 17:58:19 +01:00
|
|
|
type context struct {
|
2016-07-23 14:22:59 +02:00
|
|
|
init bool
|
|
|
|
runOnMainThread func(func() error) error
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2017-05-30 19:09:27 +02:00
|
|
|
func Init(runOnMainThread func(func() error) error) {
|
2016-07-03 17:47:56 +02:00
|
|
|
c := &Context{}
|
2016-07-23 14:22:59 +02:00
|
|
|
c.runOnMainThread = runOnMainThread
|
2017-05-30 19:09:27 +02:00
|
|
|
theContext = c
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2016-07-23 14:43:35 +02:00
|
|
|
func (c *Context) runOnContextThread(f func() error) error {
|
2016-07-23 14:22:59 +02:00
|
|
|
return c.runOnMainThread(f)
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 17:47:56 +02:00
|
|
|
func (c *Context) Reset() error {
|
2016-07-23 14:43:35 +02:00
|
|
|
if err := c.runOnContextThread(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
|
|
|
|
c.lastCompositeMode = CompositeModeUnknown
|
2017-05-29 18:46:48 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-06-17 23:25:40 +02:00
|
|
|
gl.Enable(gl.BLEND)
|
|
|
|
return nil
|
2017-05-29 18:46:48 +02:00
|
|
|
})
|
2016-06-09 18:19:10 +02:00
|
|
|
c.BlendFunc(CompositeModeSourceOver)
|
2017-05-29 18:46:48 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-06-17 23:25:40 +02:00
|
|
|
f := int32(0)
|
|
|
|
gl.GetIntegerv(gl.FRAMEBUFFER_BINDING, &f)
|
|
|
|
c.screenFramebuffer = Framebuffer(f)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-02-29 18:16:32 +01:00
|
|
|
func (c *Context) BlendFunc(mode CompositeMode) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(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
|
2016-07-03 11:11:37 +02:00
|
|
|
s, d := operations(mode)
|
2016-02-28 16:25:16 +01:00
|
|
|
gl.BlendFunc(uint32(s), uint32(d))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-11 15:07:01 +01:00
|
|
|
func (c *Context) NewTexture(width, height int, pixels []uint8) (Texture, error) {
|
2016-05-13 20:24:01 +02:00
|
|
|
var texture Texture
|
2016-07-23 14:43:35 +02:00
|
|
|
if err := c.runOnContextThread(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)
|
2016-07-09 12:15:53 +02:00
|
|
|
texture = Texture(t)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-09-24 17:11:19 +02:00
|
|
|
c.BindTexture(texture)
|
|
|
|
_ = c.runOnContextThread(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)
|
2016-02-25 17:58:19 +01:00
|
|
|
|
|
|
|
var p interface{}
|
|
|
|
if pixels != nil {
|
|
|
|
p = pixels
|
|
|
|
}
|
|
|
|
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p))
|
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
|
|
|
}
|
|
|
|
|
2017-09-24 17:14:25 +02:00
|
|
|
func (c *Context) bindFramebufferImpl(f Framebuffer) {
|
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-06-18 15:47:34 +02:00
|
|
|
gl.BindFramebuffer(gl.FRAMEBUFFER, uint32(f))
|
|
|
|
return nil
|
2017-09-24 17:14:25 +02:00
|
|
|
})
|
2016-05-31 19:33:31 +02:00
|
|
|
}
|
|
|
|
|
2016-05-13 20:24:01 +02:00
|
|
|
func (c *Context) FramebufferPixels(f Framebuffer, width, height int) ([]uint8, error) {
|
|
|
|
var pixels []uint8
|
2016-07-23 14:43:35 +02:00
|
|
|
if err := c.runOnContextThread(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.Flush()
|
2016-06-18 15:47:34 +02:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-24 17:14:25 +02:00
|
|
|
c.bindFramebuffer(f)
|
2016-07-23 14:43:35 +02:00
|
|
|
if err := c.runOnContextThread(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
pixels = make([]uint8, 4*width*height)
|
|
|
|
gl.ReadPixels(0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(pixels))
|
|
|
|
if e := gl.GetError(); e != gl.NO_ERROR {
|
|
|
|
pixels = nil
|
2016-05-13 20:24:01 +02:00
|
|
|
return fmt.Errorf("opengl: glReadPixels: %d", e)
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-09-24 17:11:19 +02:00
|
|
|
func (c *Context) bindTextureImpl(t Texture) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DeleteTexture(t Texture) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-12 16:54:36 +02:00
|
|
|
func (c *Context) IsTexture(t Texture) bool {
|
|
|
|
r := false
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-06-12 16:54:36 +02:00
|
|
|
r = gl.IsTexture(uint32(t))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2016-02-25 17:58:19 +01:00
|
|
|
func (c *Context) TexSubImage2D(p []uint8, width, height int) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(p))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-24 17:14:25 +02:00
|
|
|
func (c *Context) BindScreenFramebuffer() {
|
|
|
|
c.bindFramebuffer(c.screenFramebuffer)
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2016-05-13 20:24:01 +02:00
|
|
|
func (c *Context) NewFramebuffer(texture Texture) (Framebuffer, error) {
|
|
|
|
var framebuffer Framebuffer
|
2016-06-18 15:47:34 +02:00
|
|
|
var f uint32
|
2016-07-23 14:43:35 +02:00
|
|
|
if err := c.runOnContextThread(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.GenFramebuffers(1, &f)
|
|
|
|
// 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
|
|
|
|
}
|
2017-09-24 17:14:25 +02:00
|
|
|
c.bindFramebuffer(Framebuffer(f))
|
2016-07-23 14:43:35 +02:00
|
|
|
if err := c.runOnContextThread(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, uint32(texture), 0)
|
|
|
|
s := gl.CheckFramebufferStatus(gl.FRAMEBUFFER)
|
|
|
|
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
|
|
|
}
|
|
|
|
framebuffer = Framebuffer(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
|
|
|
}
|
|
|
|
|
2017-09-24 17:17:07 +02:00
|
|
|
func (c *Context) setViewportImpl(width, height int) {
|
|
|
|
_ = c.runOnContextThread(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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-02 14:25:02 +01:00
|
|
|
func (c *Context) FillFramebuffer(r, g, b, a float32) error {
|
2016-07-23 14:43:35 +02:00
|
|
|
return c.runOnContextThread(func() error {
|
2017-12-02 19:51:42 +01:00
|
|
|
gl.ClearColor(adjustForClearColor(r),
|
|
|
|
adjustForClearColor(g),
|
|
|
|
adjustForClearColor(b),
|
|
|
|
adjustForClearColor(a))
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.Clear(gl.COLOR_BUFFER_BIT)
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DeleteFramebuffer(f Framebuffer) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
ff := uint32(f)
|
2016-06-12 16:19:01 +02:00
|
|
|
if !gl.IsFramebuffer(ff) {
|
|
|
|
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
|
|
|
}
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.DeleteFramebuffers(1, &ff)
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-13 20:24:01 +02:00
|
|
|
func (c *Context) NewShader(shaderType ShaderType, source string) (Shader, error) {
|
|
|
|
var shader Shader
|
2016-07-23 14:43:35 +02:00
|
|
|
if err := c.runOnContextThread(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 {
|
|
|
|
log := []uint8{}
|
|
|
|
gl.GetShaderiv(uint32(s), gl.INFO_LOG_LENGTH, &v)
|
|
|
|
if v != 0 {
|
|
|
|
log = make([]uint8, int(v))
|
|
|
|
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
|
|
|
}
|
|
|
|
shader = Shader(s)
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return shader, nil
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DeleteShader(s Shader) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-13 20:24:01 +02:00
|
|
|
func (c *Context) NewProgram(shaders []Shader) (Program, error) {
|
|
|
|
var program Program
|
2016-07-23 14:43:35 +02:00
|
|
|
if err := c.runOnContextThread(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))
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
program = Program(p)
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return program, nil
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) UseProgram(p Program) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-03 17:23:45 +02:00
|
|
|
func (c *Context) DeleteProgram(p Program) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-03 20:50:28 +02:00
|
|
|
func (c *Context) getUniformLocationImpl(p Program, location string) uniformLocation {
|
2016-02-26 19:13:42 +01:00
|
|
|
uniform := uniformLocation(gl.GetUniformLocation(uint32(p), gl.Str(location+"\x00")))
|
2016-02-25 17:58:19 +01:00
|
|
|
if uniform == -1 {
|
|
|
|
panic("opengl: invalid uniform location: " + location)
|
|
|
|
}
|
|
|
|
return uniform
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) UniformInt(p Program, location string, v int) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-02-26 19:01:55 +01:00
|
|
|
l := int32(c.locationCache.GetUniformLocation(c, p, location))
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-22 03:46:46 +01:00
|
|
|
func (c *Context) UniformFloat(p Program, location string, v float32) {
|
|
|
|
_ = c.runOnContextThread(func() error {
|
|
|
|
l := int32(c.locationCache.GetUniformLocation(c, p, location))
|
|
|
|
gl.Uniform1f(l, v)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-02-25 17:58:19 +01:00
|
|
|
func (c *Context) UniformFloats(p Program, location string, v []float32) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-02-26 19:01:55 +01:00
|
|
|
l := int32(c.locationCache.GetUniformLocation(c, p, location))
|
2016-02-25 17:58:19 +01:00
|
|
|
switch len(v) {
|
2017-12-11 15:07:01 +01:00
|
|
|
case 2:
|
|
|
|
gl.Uniform2fv(l, 1, (*float32)(gl.Ptr(v)))
|
2016-02-25 17:58:19 +01:00
|
|
|
case 4:
|
|
|
|
gl.Uniform4fv(l, 1, (*float32)(gl.Ptr(v)))
|
|
|
|
case 16:
|
|
|
|
gl.UniformMatrix4fv(l, 1, false, (*float32)(gl.Ptr(v)))
|
|
|
|
default:
|
2017-12-11 15:07:01 +01:00
|
|
|
panic("not reached")
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-03 20:50:28 +02:00
|
|
|
func (c *Context) getAttribLocationImpl(p Program, location string) attribLocation {
|
2016-02-26 19:13:42 +01:00
|
|
|
attrib := attribLocation(gl.GetAttribLocation(uint32(p), gl.Str(location+"\x00")))
|
2016-02-25 17:58:19 +01:00
|
|
|
if attrib == -1 {
|
2016-04-10 18:45:13 +02:00
|
|
|
panic("opengl: invalid attrib location: " + location)
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
return attrib
|
|
|
|
}
|
|
|
|
|
2017-12-03 11:21:59 +01:00
|
|
|
func (c *Context) VertexAttribPointer(p Program, location string, size int, dataType DataType, stride int, offset int) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-02-26 19:01:55 +01:00
|
|
|
l := c.locationCache.GetAttribLocation(c, p, location)
|
2017-12-03 11:21:59 +01:00
|
|
|
gl.VertexAttribPointer(uint32(l), int32(size), uint32(dataType), false, int32(stride), gl.PtrOffset(offset))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) EnableVertexAttribArray(p Program, location string) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-02-26 19:01:55 +01:00
|
|
|
l := c.locationCache.GetAttribLocation(c, p, location)
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.EnableVertexAttribArray(uint32(l))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) DisableVertexAttribArray(p Program, location string) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-02-26 19:01:55 +01:00
|
|
|
l := c.locationCache.GetAttribLocation(c, p, location)
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.DisableVertexAttribArray(uint32(l))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-31 18:29:56 +02:00
|
|
|
func (c *Context) NewArrayBuffer(size int) Buffer {
|
2016-05-13 20:24:01 +02:00
|
|
|
var buffer Buffer
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
var b uint32
|
|
|
|
gl.GenBuffers(1, &b)
|
2017-08-31 18:29:56 +02:00
|
|
|
gl.BindBuffer(uint32(ArrayBuffer), b)
|
|
|
|
gl.BufferData(uint32(ArrayBuffer), size, nil, uint32(DynamicDraw))
|
|
|
|
buffer = Buffer(b)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) NewElementArrayBuffer(indices []uint16) Buffer {
|
|
|
|
var buffer Buffer
|
|
|
|
_ = c.runOnContextThread(func() error {
|
|
|
|
var b uint32
|
|
|
|
gl.GenBuffers(1, &b)
|
|
|
|
gl.BindBuffer(uint32(ElementArrayBuffer), b)
|
|
|
|
gl.BufferData(uint32(ElementArrayBuffer), 2*len(indices), gl.Ptr(indices), uint32(StaticDraw))
|
2016-02-25 17:58:19 +01:00
|
|
|
buffer = Buffer(b)
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
2016-05-13 20:24:01 +02:00
|
|
|
return buffer
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) BindElementArrayBuffer(b Buffer) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-02-25 17:58:19 +01:00
|
|
|
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, uint32(b))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-01 18:06:37 +01:00
|
|
|
func (c *Context) BufferSubData(bufferType BufferType, data []float32) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-11-01 18:06:37 +01:00
|
|
|
gl.BufferSubData(uint32(bufferType), 0, len(data)*4, gl.Ptr(data))
|
2016-05-13 20:24:01 +02:00
|
|
|
return nil
|
2016-02-25 17:58:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-03 17:23:45 +02:00
|
|
|
func (c *Context) DeleteBuffer(b Buffer) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-07-03 17:23:45 +02:00
|
|
|
bb := uint32(b)
|
|
|
|
gl.DeleteBuffers(1, &bb)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-03 05:41:18 +02:00
|
|
|
func (c *Context) DrawElements(mode Mode, len int, offsetInBytes int) {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-06-03 05:41:18 +02:00
|
|
|
gl.DrawElements(uint32(mode), int32(len), gl.UNSIGNED_SHORT, gl.PtrOffset(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
|
|
|
|
|
|
|
func (c *Context) Flush() {
|
2016-08-03 16:44:54 +02:00
|
|
|
_ = c.runOnContextThread(func() error {
|
2016-06-06 19:24:36 +02:00
|
|
|
gl.Flush()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|