func (b *VertexBuffer) SetVertices(src []byte, usage Usage) error { gl.VertexArray(0).Bind() b.bind() // set size of buffer and invalidate it gl.BufferData(gl.ARRAY_BUFFER, len(src), nil, usage.gl()) if len(src) > 0 { // if unmap returns false, the buffer we wrote to is no longer valid and we // need to try again. though, this is apparently uncommon in modern // drivers. this means it is not feasible to compute/copy vertices directly // into the mapped buffer. however, it would be nice to provide a // failure-capable API to do this. const maxretries = 5 retries := 0 for ; retries < maxretries; retries++ { ptr := gl.MapBuffer(gl.ARRAY_BUFFER, gl.WRITE_ONLY) slicehdr := reflect.SliceHeader{ Data: uintptr(ptr), Len: len(src), Cap: len(src), } dest := *(*[]byte)(unsafe.Pointer(&slicehdr)) copy(dest, src) if gl.UnmapBuffer(gl.ARRAY_BUFFER) { break } } if retries == maxretries { return errMapBufferFailed } } b.count = len(src) / b.format.Stride() return nil }
func (renderer *renderer) unbind() { gl.VertexArray(0).Bind() if err := CheckGlError(); err != nil { err.Description = "gl.VertexArray(0).Bind()" panic(err) } }
func (b *IndexBuffer) setIndices(copyTo func(unsafe.Pointer), usage Usage, size int) error { gl.VertexArray(0).Bind() b.bind() gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, size, nil, usage.gl()) if size > 0 { const maxretries = 5 retries := 0 for ; retries < maxretries; retries++ { ptr := gl.MapBuffer(gl.ELEMENT_ARRAY_BUFFER, gl.WRITE_ONLY) copyTo(ptr) if gl.UnmapBuffer(gl.ELEMENT_ARRAY_BUFFER) { break } } if retries == maxretries { return errMapBufferFailed } } return nil }
func (batch VaoBatch) Exit() { gl.VertexArray(0).Bind() // Cannot fail with argument 0. }
func (this VAO) Bind() { if lastVAO != this { lastVAO = this gl.VertexArray.Bind(gl.VertexArray(this)) } }