add error handling
This commit is contained in:
parent
ff30a4d2b2
commit
c5c93d9442
|
@ -0,0 +1,41 @@
|
|||
package pixelgl
|
||||
|
||||
import (
|
||||
"github.com/go-gl/gl/v3.3-core/gl"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// GetLastError returns (and consumes) the last error generated by OpenGL.
|
||||
// If no error has been generated, this function returns nil.
|
||||
//
|
||||
// Call this function only inside the OpenGL thread (Do, DoErr or DoVal function). It's not guaranteed
|
||||
// to work correctly outside of it, because the thread swallows extra unchecked errors.
|
||||
func GetLastError() error {
|
||||
err := uint32(gl.NO_ERROR)
|
||||
for e := gl.GetError(); e != gl.NO_ERROR; e = gl.GetError() {
|
||||
err = e
|
||||
}
|
||||
if err == gl.NO_ERROR {
|
||||
return nil
|
||||
}
|
||||
switch err {
|
||||
case gl.INVALID_ENUM:
|
||||
return errors.New("invalid enum")
|
||||
case gl.INVALID_VALUE:
|
||||
return errors.New("invalid value")
|
||||
case gl.INVALID_OPERATION:
|
||||
return errors.New("invalid operation")
|
||||
case gl.STACK_OVERFLOW:
|
||||
return errors.New("stack overflow")
|
||||
case gl.STACK_UNDERFLOW:
|
||||
return errors.New("stack underflow")
|
||||
case gl.OUT_OF_MEMORY:
|
||||
return errors.New("out of memory")
|
||||
case gl.INVALID_FRAMEBUFFER_OPERATION:
|
||||
return errors.New("invalid framebuffer operation")
|
||||
case gl.CONTEXT_LOST:
|
||||
return errors.New("context lost")
|
||||
default:
|
||||
return errors.New("unknown error")
|
||||
}
|
||||
}
|
|
@ -1,6 +1,9 @@
|
|||
package pixelgl
|
||||
|
||||
import "github.com/go-gl/gl/v3.3-core/gl"
|
||||
import (
|
||||
"github.com/go-gl/gl/v3.3-core/gl"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// Texture is an OpenGL texture.
|
||||
type Texture struct {
|
||||
|
@ -10,11 +13,12 @@ type Texture struct {
|
|||
|
||||
// NewTexture creates a new texture with the specified width and height.
|
||||
// The pixels must be a sequence of RGBA values.
|
||||
func NewTexture(parent BeginEnder, width, height int, pixels []uint8) *Texture {
|
||||
func NewTexture(parent BeginEnder, width, height int, pixels []uint8) (*Texture, error) {
|
||||
texture := &Texture{parent: parent}
|
||||
Do(func() {
|
||||
err := DoErr(func() error {
|
||||
gl.GenTextures(1, &texture.tex)
|
||||
gl.BindTexture(gl.TEXTURE_2D, texture.tex)
|
||||
|
||||
gl.TexImage2D(
|
||||
gl.TEXTURE_2D,
|
||||
0,
|
||||
|
@ -27,9 +31,15 @@ func NewTexture(parent BeginEnder, width, height int, pixels []uint8) *Texture {
|
|||
gl.Ptr(pixels),
|
||||
)
|
||||
gl.GenerateMipmap(gl.TEXTURE_2D)
|
||||
|
||||
gl.BindTexture(gl.TEXTURE_2D, 0)
|
||||
|
||||
return GetLastError()
|
||||
})
|
||||
return texture
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create a texture")
|
||||
}
|
||||
return texture, nil
|
||||
}
|
||||
|
||||
// Begin binds a texture.
|
||||
|
|
|
@ -16,6 +16,7 @@ func init() {
|
|||
go func() {
|
||||
runtime.LockOSThread()
|
||||
for f := range callQueue {
|
||||
GetLastError() // swallow unchecked errors
|
||||
f()
|
||||
}
|
||||
}()
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package pixelgl
|
||||
|
||||
import "github.com/go-gl/gl/v3.3-core/gl"
|
||||
import (
|
||||
"github.com/go-gl/gl/v3.3-core/gl"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// VertexFormat defines a data format in a vertex buffer.
|
||||
//
|
||||
|
@ -96,13 +99,13 @@ type VertexArray struct {
|
|||
}
|
||||
|
||||
// NewVertexArray creates a new vertex array and wraps another BeginEnder around it.
|
||||
func NewVertexArray(parent BeginEnder, format VertexFormat, mode VertexDrawMode, usage VertexUsage, data []float64) *VertexArray {
|
||||
func NewVertexArray(parent BeginEnder, format VertexFormat, mode VertexDrawMode, usage VertexUsage, data []float64) (*VertexArray, error) {
|
||||
va := &VertexArray{
|
||||
parent: parent,
|
||||
format: format,
|
||||
mode: mode,
|
||||
}
|
||||
Do(func() {
|
||||
err := DoErr(func() error {
|
||||
gl.GenVertexArrays(1, &va.vao)
|
||||
gl.BindVertexArray(va.vao)
|
||||
|
||||
|
@ -129,8 +132,13 @@ func NewVertexArray(parent BeginEnder, format VertexFormat, mode VertexDrawMode,
|
|||
|
||||
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
|
||||
gl.BindVertexArray(0)
|
||||
|
||||
return GetLastError()
|
||||
})
|
||||
return va
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create a vertex array")
|
||||
}
|
||||
return va, nil
|
||||
}
|
||||
|
||||
// VertexFormat returns the format of the vertices inside a vertex array.
|
||||
|
@ -164,12 +172,17 @@ func (va *VertexArray) Draw() {
|
|||
// UpdateData overwrites the current vertex array data starting at the index offset.
|
||||
//
|
||||
// Offset is not a number of bytes, instead, it's an index in the array.
|
||||
func (va *VertexArray) UpdateData(offset int, data []float64) {
|
||||
Do(func() {
|
||||
func (va *VertexArray) UpdateData(offset int, data []float64) error {
|
||||
err := DoErr(func() error {
|
||||
gl.BindBuffer(gl.ARRAY_BUFFER, va.vbo)
|
||||
gl.BufferSubData(gl.ARRAY_BUFFER, 8*offset, 8*len(data), gl.Ptr(data))
|
||||
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
|
||||
return GetLastError()
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to update vertex array")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Begin binds a vertex array and it's associated vertex buffer.
|
||||
|
|
Loading…
Reference in New Issue