func (me *Mesh) GpuUpload() (err error) { sizeVerts, sizeIndices := gl.Sizeiptr(4*len(me.raw.verts)), gl.Sizeiptr(4*len(me.raw.indices)) me.GpuDelete() if sizeVerts > gl.Sizeiptr(me.meshBuffer.memSizeVertices) { err = errf("Cannot upload mesh '%v': vertex size (%vB) exceeds mesh buffer's available vertex memory (%vB)", me.Name, sizeVerts, me.meshBuffer.memSizeVertices) } else if sizeIndices > gl.Sizeiptr(me.meshBuffer.memSizeIndices) { err = errf("Cannot upload mesh '%v': index size (%vB) exceeds mesh buffer's available index memory (%vB)", me.Name, sizeIndices, me.meshBuffer.memSizeIndices) } else { me.meshBufOffsetBaseIndex, me.meshBufOffsetIndices, me.meshBufOffsetVerts = me.meshBuffer.offsetBaseIndex, me.meshBuffer.offsetIndices, me.meshBuffer.offsetVerts Diag.LogMeshes("Upload %v at voff=%v ioff=%v boff=%v", me.Name, me.meshBufOffsetVerts, me.meshBufOffsetIndices, me.meshBufOffsetBaseIndex) me.meshBuffer.glIbo.Bind() defer me.meshBuffer.glIbo.Unbind() me.meshBuffer.glVbo.Bind() defer me.meshBuffer.glVbo.Unbind() if err = me.meshBuffer.glVbo.SubData(gl.Intptr(me.meshBufOffsetVerts), sizeVerts, gl.Ptr(&me.raw.verts[0])); err == nil { me.meshBuffer.offsetVerts += int32(sizeVerts) if err = me.meshBuffer.glIbo.SubData(gl.Intptr(me.meshBufOffsetIndices), sizeIndices, gl.Ptr(&me.raw.indices[0])); err == nil { me.meshBuffer.offsetIndices += int32(sizeIndices) me.meshBuffer.offsetBaseIndex += int32(len(me.raw.indices)) if err == nil { me.gpuSynced = true } } } } return }
func newMeshBuffer(name string, capacity int32) (me *MeshBuffer, err error) { me = &MeshBuffer{} me.Name = name me.meshIDs = make([]int, 0, 256) me.glVaos = make([]ugl.VertexArray, 16) numVerts, numIndices := capacity, capacity me.memSizeIndices = Core.Mesh.Buffers.MemSizePerIndex() * numIndices me.memSizeVertices = Core.Mesh.Buffers.MemSizePerVertex() * numVerts if err = me.glVbo.Recreate(gl.ARRAY_BUFFER, gl.Sizeiptr(me.memSizeVertices), ugl.PtrNil, gl.STATIC_DRAW); err == nil { err = me.glIbo.Recreate(gl.ELEMENT_ARRAY_BUFFER, gl.Sizeiptr(me.memSizeIndices), ugl.PtrNil, gl.STATIC_DRAW) } // if err == nil { // var ok bool // for i := 0; i < len(ogl.progs.All); i++ { // if err = me.setupVao(i); err != nil { // break // } // } // } if err != nil { me.dispose() me = nil } return }
func uploadGeometry(mesh *geometry) { gl.GenVertexArrays(1, &mesh.glVao) gl.BindVertexArray(mesh.glVao) gl.GenBuffers(1, &mesh.glVertBuf) gl.BindBuffer(gl.ARRAY_BUFFER, mesh.glVertBuf) gl.EnableVertexAttribArray(progPosAttrLoc) gl.VertexAttribPointer(progPosAttrLoc, 3, gl.FLOAT, gl.FALSE, 0, gl.Ptr(nil)) gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(4*len(mesh.glVerts)), gl.Ptr(&mesh.glVerts[0]), gl.STATIC_DRAW) gl.BindBuffer(gl.ARRAY_BUFFER, 0) gl.BindVertexArray(0) }
func (me *TextureBase) subImage(glTarget gl.Enum, x, y gl.Int, width, height gl.Sizei, ptr gl.Ptr) (err error) { if ptr != PtrNil { if Support.Textures.StreamUpdatesViaPixelBuffer { buf := NewPixelUnpackBuffer() defer buf.Dispose() buf.Recreate(0, gl.Sizeiptr(width*height*4), PtrNil, gl.STREAM_DRAW) buf.Bind() defer buf.Unbind() pbo := buf.Map(true, true) *pbo = gl.Ptr(ptr) buf.Unmap() err = Try.TexSubImage2D(glTarget, 0, x, y, width, height, me.PixelData.Format, me.PixelData.Type, PtrNil) } else { err = Try.TexSubImage2D(glTarget, 0, x, y, width, height, me.PixelData.Format, me.PixelData.Type, ptr) } } return }