Description
EDIT: using GODEBUG=cgocheck=0
can disable these checks but seems like kind of a hacky-workaround, especially if you need runtime pointer checks for other packages.
It seems that calling functions like gl.DrawElements
using gl.PtrOffset
to convert a gl buffer offset causes the Go runtime to intercept those offsets as if they were pointers into Go memory. This is just a toy example; I've run into this problem using large index/vertex buffers in other projects. Trying to index elements in the millions seems to semi-randomly throw errors.
Drilling down a bit:
// PtrOffset takes a pointer offset and returns a GL-compatible pointer.
// Useful for functions such as glVertexAttribPointer that take pointer
// parameters indicating an offset rather than an absolute memory address.
func PtrOffset(offset int) unsafe.Pointer {
return unsafe.Pointer(uintptr(offset))
}
I'm already out of my depth here but I'm wondering why offsets have to be sent to OpenGL via CGO as unsafe.Pointer? Is there some obvious way around this error I'm missing?
Program to reproduce:
package main
import (
"log"
"github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.2/glfw"
)
// var pointers = make([]*int, 40*(1<<20))
var buffer = make([]byte, 48*(1<<20))
func main() {
err := glfw.Init()
if err != nil {
log.Fatal(err)
}
window, err := glfw.CreateWindow(1024, 768, "Test", nil, nil)
if err != nil {
log.Fatal(err)
}
//for i := range pointers {
// pointers[i] = new(int)
//}
window.MakeContextCurrent()
if err := gl.Init(); err != nil {
log.Fatalln(err)
}
var vertBuf, indexBuf uint32
gl.CreateBuffers(1, &vertBuf)
gl.CreateBuffers(1, &indexBuf)
gl.BindBuffer(gl.ARRAY_BUFFER, vertBuf)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuf)
gl.BufferData(gl.ARRAY_BUFFER, len(buffer), gl.Ptr(buffer), gl.DYNAMIC_DRAW)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(buffer), gl.Ptr(buffer), gl.DYNAMIC_DRAW)
// const NumDraws = 1000
var NumDraws = len(buffer) / 4
for i := 0; i < NumDraws; i++ {
//offset := rand.Int() % 12000000 //+ 12000000
offset := i
//fmt.Printf("offset = %d", offset)
gl.DrawElements(gl.TRIANGLES, 500000, gl.UNSIGNED_INT, gl.PtrOffset(offset))
gl.DrawElementsInstancedARB(gl.TRIANGLES, 500000, gl.UNSIGNED_INT, gl.PtrOffset(offset), 1)
}
//for _, p := range pointers {
// *p++
//}
window.Destroy()
glfw.Terminate()
}
Activity