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.
|
|
|
|
|
2022-09-12 16:28:43 +02:00
|
|
|
//go:build !android && !ios && !js && !opengles
|
2016-02-25 17:58:19 +01:00
|
|
|
|
|
|
|
package opengl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl/gl"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2016-02-25 17:58:19 +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
|
|
|
)
|
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
|
|
|
|
}
|
|
|
|
|
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 (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-11-10 17:14:26 +01:00
|
|
|
type contextImpl struct {
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) reset() error {
|
2016-06-09 18:19:10 +02:00
|
|
|
c.locationCache = newLocationCache()
|
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-10-16 13:44:09 +02:00
|
|
|
c.lastBlend = graphicsdriver.Blend{}
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.Enable(gl.BLEND)
|
|
|
|
gl.Enable(gl.SCISSOR_TEST)
|
2020-10-12 17:39:45 +02:00
|
|
|
|
2022-10-15 11:58:56 +02:00
|
|
|
// Set the source over blending.
|
|
|
|
c.blend(graphicsdriver.BlendSourceOver)
|
2020-10-12 17:39:45 +02:00
|
|
|
|
|
|
|
f := int32(0)
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.GetIntegerv(gl.FRAMEBUFFER_BINDING, &f)
|
2020-10-12 17:39:45 +02:00
|
|
|
c.screenFramebuffer = framebufferNative(f)
|
2016-06-17 23:25:40 +02:00
|
|
|
return nil
|
2016-06-09 18:19:10 +02:00
|
|
|
}
|
|
|
|
|
2022-10-15 11:58:56 +02:00
|
|
|
func (c *context) blend(blend graphicsdriver.Blend) {
|
|
|
|
if c.lastBlend == blend {
|
2020-10-12 17:39:45 +02:00
|
|
|
return
|
|
|
|
}
|
2022-10-15 11:58:56 +02:00
|
|
|
c.lastBlend = blend
|
|
|
|
gl.BlendFuncSeparate(
|
2022-10-16 17:49:56 +02:00
|
|
|
uint32(convertBlendFactor(blend.BlendFactorSourceRGB)),
|
|
|
|
uint32(convertBlendFactor(blend.BlendFactorDestinationRGB)),
|
2022-10-15 11:58:56 +02:00
|
|
|
uint32(convertBlendFactor(blend.BlendFactorSourceAlpha)),
|
|
|
|
uint32(convertBlendFactor(blend.BlendFactorDestinationAlpha)),
|
|
|
|
)
|
|
|
|
gl.BlendEquationSeparate(
|
2022-10-16 17:49:56 +02:00
|
|
|
uint32(convertBlendOperation(blend.BlendOperationRGB)),
|
2022-10-15 11:58:56 +02:00
|
|
|
uint32(convertBlendOperation(blend.BlendOperationAlpha)),
|
|
|
|
)
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2020-11-07 11:14:06 +01:00
|
|
|
func (c *context) scissor(x, y, width, height int) {
|
|
|
|
gl.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-12 17:39:45 +02:00
|
|
|
var t uint32
|
|
|
|
gl.GenTextures(1, &t)
|
|
|
|
// TODO: Use gl.IsTexture
|
|
|
|
if t <= 0 {
|
|
|
|
return 0, errors.New("opengl: creating texture failed")
|
2016-07-09 12:15:53 +02:00
|
|
|
}
|
2020-10-12 17:39:45 +02:00
|
|
|
texture := textureNative(t)
|
2018-11-01 19:43:42 +01:00
|
|
|
c.bindTexture(texture)
|
2021-07-02 19:09:44 +02:00
|
|
|
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
|
|
|
|
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
|
|
|
|
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)
|
2021-07-02 19:09:44 +02:00
|
|
|
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
|
2020-10-12 17:39:45 +02: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
|
2022-11-13 05:24:54 +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 texture, nil
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) bindFramebufferImpl(f framebufferNative) {
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.BindFramebufferEXT(gl.FRAMEBUFFER, uint32(f))
|
2020-10-12 17:39:45 +02:00
|
|
|
}
|
|
|
|
|
2022-08-27 14:22:31 +02:00
|
|
|
func (c *context) framebufferPixels(buf []byte, f *framebuffer, x, y, width, height int) {
|
2020-10-12 17:39:45 +02:00
|
|
|
gl.Flush()
|
2018-11-04 09:28:33 +01:00
|
|
|
c.bindFramebuffer(f.native)
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.ReadPixels(int32(x), int32(y), int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(buf))
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2020-08-15 08:11:56 +02:00
|
|
|
func (c *context) framebufferPixelsToBuffer(f *framebuffer, buffer buffer, width, height int) {
|
|
|
|
gl.Flush()
|
|
|
|
c.bindFramebuffer(f.native)
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.BindBuffer(gl.PIXEL_PACK_BUFFER, uint32(buffer))
|
|
|
|
gl.ReadPixels(0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, nil)
|
|
|
|
gl.BindBuffer(gl.PIXEL_PACK_BUFFER, 0)
|
2020-08-15 08:11:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 16:04:13 +02:00
|
|
|
func (c *context) activeTexture(idx int) {
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.ActiveTexture(gl.TEXTURE0 + uint32(idx))
|
2020-05-17 16:04:13 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) bindTextureImpl(t textureNative) {
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.BindTexture(gl.TEXTURE_2D, uint32(t))
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteTexture(t textureNative) {
|
2020-10-12 17:39:45 +02:00
|
|
|
tt := uint32(t)
|
|
|
|
if !gl.IsTexture(tt) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.lastTexture == t {
|
|
|
|
c.lastTexture = invalidTexture
|
|
|
|
}
|
|
|
|
gl.DeleteTextures(1, &tt)
|
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
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
func (c *context) newRenderbuffer(width, height int) (renderbufferNative, error) {
|
|
|
|
var r uint32
|
|
|
|
gl.GenRenderbuffersEXT(1, &r)
|
|
|
|
if r <= 0 {
|
|
|
|
return 0, errors.New("opengl: creating renderbuffer failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
renderbuffer := renderbufferNative(r)
|
|
|
|
c.bindRenderbuffer(renderbuffer)
|
|
|
|
|
|
|
|
// GL_STENCIL_INDEX8 might not be available with OpenGL 2.1.
|
|
|
|
// https://www.khronos.org/opengl/wiki/Image_Format
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.RenderbufferStorageEXT(gl.RENDERBUFFER, gl.DEPTH24_STENCIL8, int32(width), int32(height))
|
2021-07-02 12:26:09 +02:00
|
|
|
|
|
|
|
return renderbuffer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) bindRenderbufferImpl(r renderbufferNative) {
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.BindRenderbufferEXT(gl.RENDERBUFFER, uint32(r))
|
2021-07-02 12:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) deleteRenderbuffer(r renderbufferNative) {
|
|
|
|
rr := uint32(r)
|
|
|
|
if !gl.IsRenderbufferEXT(rr) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.lastRenderbuffer.equal(r) {
|
|
|
|
c.lastRenderbuffer = 0
|
|
|
|
}
|
|
|
|
gl.DeleteRenderbuffersEXT(1, &rr)
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newFramebuffer(texture textureNative) (framebufferNative, error) {
|
2016-06-18 15:47:34 +02:00
|
|
|
var f uint32
|
2020-10-12 17:39:45 +02:00
|
|
|
gl.GenFramebuffersEXT(1, &f)
|
|
|
|
if f <= 0 {
|
2022-05-23 15:25:40 +02:00
|
|
|
return 0, errors.New("opengl: creating framebuffer failed")
|
2016-06-18 15:47:34 +02:00
|
|
|
}
|
2018-11-04 09:43:26 +01:00
|
|
|
c.bindFramebuffer(framebufferNative(f))
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.FramebufferTexture2DEXT(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, uint32(texture), 0)
|
|
|
|
s := gl.CheckFramebufferStatusEXT(gl.FRAMEBUFFER)
|
|
|
|
if s != gl.FRAMEBUFFER_COMPLETE {
|
2020-10-12 17:39:45 +02:00
|
|
|
if s != 0 {
|
|
|
|
return 0, fmt.Errorf("opengl: creating framebuffer failed: %v", s)
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
2022-11-13 05:24:54 +01:00
|
|
|
if e := gl.GetError(); e != gl.NO_ERROR {
|
2020-10-12 17:39:45 +02:00
|
|
|
return 0, fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e)
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("opengl: creating framebuffer failed: unknown error")
|
2016-05-13 20:24:01 +02:00
|
|
|
}
|
2020-10-12 17:39:45 +02:00
|
|
|
return framebufferNative(f), nil
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
func (c *context) bindStencilBuffer(f framebufferNative, r renderbufferNative) error {
|
|
|
|
c.bindFramebuffer(f)
|
|
|
|
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.FramebufferRenderbufferEXT(gl.FRAMEBUFFER, gl.STENCIL_ATTACHMENT, gl.RENDERBUFFER, uint32(r))
|
|
|
|
if s := gl.CheckFramebufferStatusEXT(gl.FRAMEBUFFER); s != gl.FRAMEBUFFER_COMPLETE {
|
2021-07-02 12:26:09 +02:00
|
|
|
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-12 17:39:45 +02:00
|
|
|
gl.Viewport(0, 0, int32(width), int32(height))
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteFramebuffer(f framebufferNative) {
|
2020-10-12 17:39:45 +02:00
|
|
|
ff := uint32(f)
|
|
|
|
if !gl.IsFramebufferEXT(ff) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.lastFramebuffer == f {
|
|
|
|
c.lastFramebuffer = invalidFramebuffer
|
|
|
|
c.lastViewportWidth = 0
|
|
|
|
c.lastViewportHeight = 0
|
|
|
|
}
|
|
|
|
gl.DeleteFramebuffersEXT(1, &ff)
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2020-11-21 15:32:13 +01:00
|
|
|
func (c *context) newVertexShader(source string) (shader, error) {
|
2022-11-13 05:24:54 +01:00
|
|
|
return c.newShader(gl.VERTEX_SHADER, source)
|
2020-11-21 15:32:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) newFragmentShader(source string) (shader, error) {
|
2022-11-13 05:24:54 +01:00
|
|
|
return c.newShader(gl.FRAGMENT_SHADER, source)
|
2020-11-21 15:32:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) newShader(shaderType uint32, source string) (shader, error) {
|
|
|
|
s := gl.CreateShader(shaderType)
|
2020-10-12 17:39:45 +02:00
|
|
|
if s == 0 {
|
|
|
|
return 0, fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType)
|
|
|
|
}
|
2022-11-09 08:07:30 +01:00
|
|
|
cSources, free := gl.CStr(source)
|
|
|
|
gl.ShaderSource(uint32(s), 1, &cSources, nil)
|
2020-10-12 17:39:45 +02:00
|
|
|
free()
|
|
|
|
gl.CompileShader(s)
|
|
|
|
|
|
|
|
var v int32
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.GetShaderiv(s, gl.COMPILE_STATUS, &v)
|
|
|
|
if v == gl.FALSE {
|
2020-10-12 17:39:45 +02:00
|
|
|
var l int32
|
|
|
|
var log []byte
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.GetShaderiv(uint32(s), gl.INFO_LOG_LENGTH, &l)
|
2020-10-12 17:39:45 +02:00
|
|
|
if l != 0 {
|
|
|
|
log = make([]byte, l)
|
|
|
|
gl.GetShaderInfoLog(s, l, nil, (*uint8)(gl.Ptr(log)))
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
2020-10-12 17:39:45 +02:00
|
|
|
return 0, fmt.Errorf("opengl: shader compile failed: %s", log)
|
2016-05-13 20:24:01 +02:00
|
|
|
}
|
2020-10-12 17:39:45 +02:00
|
|
|
return shader(s), nil
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteShader(s shader) {
|
2020-10-12 17:39:45 +02:00
|
|
|
gl.DeleteShader(uint32(s))
|
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) {
|
2020-10-12 17:39:45 +02:00
|
|
|
p := gl.CreateProgram()
|
|
|
|
if p == 0 {
|
|
|
|
return 0, errors.New("opengl: glCreateProgram failed")
|
|
|
|
}
|
2016-02-25 17:58:19 +01:00
|
|
|
|
2020-10-12 17:39:45 +02:00
|
|
|
for _, shader := range shaders {
|
|
|
|
gl.AttachShader(p, uint32(shader))
|
|
|
|
}
|
2019-02-16 06:03:21 +01:00
|
|
|
|
2020-10-12 17:39:45 +02:00
|
|
|
for i, name := range attributes {
|
2022-11-09 08:07:30 +01:00
|
|
|
l, free := gl.CStr(name)
|
|
|
|
gl.BindAttribLocation(p, uint32(i), l)
|
2020-10-12 17:39:45 +02:00
|
|
|
free()
|
|
|
|
}
|
2019-02-16 06:03:21 +01:00
|
|
|
|
2020-10-12 17:39:45 +02:00
|
|
|
gl.LinkProgram(p)
|
|
|
|
var v int32
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.GetProgramiv(p, gl.LINK_STATUS, &v)
|
|
|
|
if v == gl.FALSE {
|
2020-10-12 17:39:45 +02:00
|
|
|
var l int32
|
|
|
|
var log []byte
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.GetProgramiv(p, gl.INFO_LOG_LENGTH, &l)
|
2020-10-12 17:39:45 +02:00
|
|
|
if l != 0 {
|
|
|
|
log = make([]byte, l)
|
|
|
|
gl.GetProgramInfoLog(p, l, nil, (*uint8)(gl.Ptr(log)))
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
2020-10-12 17:39:45 +02:00
|
|
|
return 0, fmt.Errorf("opengl: program error: %s", log)
|
2016-05-13 20:24:01 +02:00
|
|
|
}
|
2020-10-12 17:39:45 +02:00
|
|
|
return program(p), nil
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) useProgram(p program) {
|
2020-10-12 17:39:45 +02:00
|
|
|
gl.UseProgram(uint32(p))
|
2016-02-25 17:58:19 +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-12 17:39:45 +02:00
|
|
|
if !gl.IsProgram(uint32(p)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
gl.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 {
|
2022-11-09 08:07:30 +01:00
|
|
|
l, free := gl.CStr(location)
|
|
|
|
uniform := uniformLocation(gl.GetUniformLocation(uint32(p), l))
|
2018-08-03 22:22:40 +02:00
|
|
|
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 {
|
2020-10-12 17:39:45 +02:00
|
|
|
l := int32(c.locationCache.GetUniformLocation(c, p, location))
|
|
|
|
if l == invalidUniform {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
gl.Uniform1i(l, int32(v))
|
|
|
|
return true
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2022-11-12 10:38:15 +01:00
|
|
|
func (c *context) uniforms(p program, location string, v []uint32, typ shaderir.Type) bool {
|
2020-10-12 17:39:45 +02:00
|
|
|
l := int32(c.locationCache.GetUniformLocation(c, p, location))
|
|
|
|
if l == invalidUniform {
|
|
|
|
return false
|
|
|
|
}
|
2020-07-30 19:52:25 +02:00
|
|
|
|
2020-10-12 17:39:45 +02:00
|
|
|
base := typ.Main
|
|
|
|
len := int32(1)
|
|
|
|
if base == shaderir.Array {
|
|
|
|
base = typ.Sub[0].Main
|
|
|
|
len = int32(typ.Length)
|
|
|
|
}
|
2020-07-30 19:52:25 +02:00
|
|
|
|
2020-10-12 17:39:45 +02:00
|
|
|
switch base {
|
|
|
|
case shaderir.Float:
|
|
|
|
gl.Uniform1fv(l, len, (*float32)(gl.Ptr(v)))
|
2022-11-12 10:38:15 +01:00
|
|
|
case shaderir.Int:
|
|
|
|
gl.Uniform1iv(l, len, (*int32)(gl.Ptr(v)))
|
2020-10-12 17:39:45 +02:00
|
|
|
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)))
|
|
|
|
default:
|
2021-11-01 16:05:03 +01:00
|
|
|
panic(fmt.Sprintf("opengl: unexpected type: %s", typ.String()))
|
2020-10-12 17:39:45 +02:00
|
|
|
}
|
|
|
|
return true
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 15:02:22 +01:00
|
|
|
func (c *context) vertexAttribPointer(index int, size int, stride int, offset int) {
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.VertexAttribPointer(uint32(index), int32(size), gl.FLOAT, false, int32(stride), uintptr(offset))
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 15:02:22 +01:00
|
|
|
func (c *context) enableVertexAttribArray(index int) {
|
2020-10-12 17:39:45 +02:00
|
|
|
gl.EnableVertexAttribArray(uint32(index))
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 15:02:22 +01:00
|
|
|
func (c *context) disableVertexAttribArray(index int) {
|
2020-10-12 17:39:45 +02:00
|
|
|
gl.DisableVertexAttribArray(uint32(index))
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) newArrayBuffer(size int) buffer {
|
2020-10-12 17:39:45 +02:00
|
|
|
var b uint32
|
|
|
|
gl.GenBuffers(1, &b)
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.BindBuffer(gl.ARRAY_BUFFER, b)
|
|
|
|
gl.BufferData(gl.ARRAY_BUFFER, size, nil, gl.DYNAMIC_DRAW)
|
2020-10-12 17:39:45 +02: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-12 17:39:45 +02:00
|
|
|
var b uint32
|
|
|
|
gl.GenBuffers(1, &b)
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, b)
|
|
|
|
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, size, nil, gl.DYNAMIC_DRAW)
|
2020-10-12 17:39:45 +02:00
|
|
|
return buffer(b)
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2020-11-21 14:59:30 +01:00
|
|
|
func (c *context) bindArrayBuffer(b buffer) {
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.BindBuffer(gl.ARRAY_BUFFER, uint32(b))
|
2020-11-21 14:59:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) bindElementArrayBuffer(b buffer) {
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, uint32(b))
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) arrayBufferSubData(data []float32) {
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.BufferSubData(gl.ARRAY_BUFFER, 0, len(data)*4, gl.Ptr(data))
|
2018-05-30 17:53:02 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) elementArrayBufferSubData(data []uint16) {
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.BufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, len(data)*2, gl.Ptr(data))
|
2016-02-25 17:58:19 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) deleteBuffer(b buffer) {
|
2020-10-12 17:39:45 +02:00
|
|
|
bb := uint32(b)
|
|
|
|
gl.DeleteBuffers(1, &bb)
|
2016-07-03 17:23:45 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) drawElements(len int, offsetInBytes int) {
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.DrawElements(gl.TRIANGLES, int32(len), gl.UNSIGNED_SHORT, uintptr(offsetInBytes))
|
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 {
|
2020-10-12 17:39:45 +02:00
|
|
|
s := int32(0)
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.GetIntegerv(gl.MAX_TEXTURE_SIZE, &s)
|
2020-10-12 17:39:45 +02:00
|
|
|
return int(s)
|
2018-03-09 03:03:55 +01:00
|
|
|
}
|
|
|
|
|
2018-11-10 17:14:26 +01:00
|
|
|
func (c *context) flush() {
|
2020-10-12 17:39:45 +02:00
|
|
|
gl.Flush()
|
2016-06-06 19:24:36 +02:00
|
|
|
}
|
2019-05-26 12:08:46 +02:00
|
|
|
|
2022-08-07 20:02:12 +02:00
|
|
|
func (c *context) texSubImage2D(t textureNative, args []*graphicsdriver.WritePixelsArgs) {
|
2020-07-23 12:15:34 +02:00
|
|
|
c.bindTexture(t)
|
2020-10-12 17:39:45 +02:00
|
|
|
for _, a := range args {
|
2022-11-13 05:24:54 +01:00
|
|
|
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))
|
2020-10-12 17:39:45 +02:00
|
|
|
}
|
2020-01-01 19:58:49 +01:00
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
func (c *context) enableStencilTest() {
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.Enable(gl.STENCIL_TEST)
|
2021-07-02 12:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) disableStencilTest() {
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.Disable(gl.STENCIL_TEST)
|
2021-07-02 12:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) beginStencilWithEvenOddRule() {
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.Clear(gl.STENCIL_BUFFER_BIT)
|
|
|
|
gl.StencilFunc(gl.ALWAYS, 0x00, 0xff)
|
|
|
|
gl.StencilOp(gl.KEEP, gl.KEEP, gl.INVERT)
|
2021-07-02 12:26:09 +02:00
|
|
|
gl.ColorMask(false, false, false, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *context) endStencilWithEvenOddRule() {
|
2022-11-13 05:24:54 +01:00
|
|
|
gl.StencilFunc(gl.NOTEQUAL, 0x00, 0xff)
|
|
|
|
gl.StencilOp(gl.KEEP, gl.KEEP, gl.KEEP)
|
2021-07-02 12:26:09 +02:00
|
|
|
gl.ColorMask(true, true, true, true)
|
|
|
|
}
|