optimize GLTriangles SetLen and Update

This commit is contained in:
faiface 2017-06-10 01:10:59 +02:00
parent 3665eaf702
commit 1fd110ce4c
1 changed files with 30 additions and 31 deletions

View File

@ -60,9 +60,10 @@ func (gt *GLTriangles) Len() int {
// SetLen efficiently resizes GLTriangles to len. // SetLen efficiently resizes GLTriangles to len.
// //
// Time complexity is amortized O(1). // Time complexity is amortized O(1).
func (gt *GLTriangles) SetLen(len int) { func (gt *GLTriangles) SetLen(length int) {
if len > gt.Len() { switch {
needAppend := len - gt.Len() case length > gt.Len():
needAppend := length - gt.Len()
for i := 0; i < needAppend; i++ { for i := 0; i < needAppend; i++ {
gt.data = append(gt.data, gt.data = append(gt.data,
0, 0, 0, 0,
@ -71,11 +72,16 @@ func (gt *GLTriangles) SetLen(len int) {
0, 0,
) )
} }
case length < gt.Len():
gt.data = gt.data[:length*gt.vs.Stride()]
default:
return
} }
if len < gt.Len() { mainthread.CallNonBlock(func() {
gt.data = gt.data[:len*gt.vs.Stride()] gt.vs.Begin()
} gt.vs.SetLen(length)
gt.submitData() gt.vs.End()
})
} }
// Slice returns a sub-Triangles of this GLTriangles in range [i, j). // Slice returns a sub-Triangles of this GLTriangles in range [i, j).
@ -144,29 +150,6 @@ func (gt *GLTriangles) updateData(t pixel.Triangles) {
} }
} }
func (gt *GLTriangles) submitData() {
// this code is supposed to copy the vertex data and CallNonBlock the update if
// the data is small enough, otherwise it'll block and not copy the data
if len(gt.data) < 256 { // arbitrary heurestic constant
data := append([]float32{}, gt.data...)
mainthread.CallNonBlock(func() {
gt.vs.Begin()
dataLen := len(data) / gt.vs.Stride()
gt.vs.SetLen(dataLen)
gt.vs.SetVertexData(data)
gt.vs.End()
})
} else {
mainthread.Call(func() {
gt.vs.Begin()
dataLen := len(gt.data) / gt.vs.Stride()
gt.vs.SetLen(dataLen)
gt.vs.SetVertexData(gt.data)
gt.vs.End()
})
}
}
// Update copies vertex properties from the supplied Triangles into this GLTriangles. // Update copies vertex properties from the supplied Triangles into this GLTriangles.
// //
// The two Triangles (gt and t) must be of the same len. // The two Triangles (gt and t) must be of the same len.
@ -175,7 +158,23 @@ func (gt *GLTriangles) Update(t pixel.Triangles) {
panic(fmt.Errorf("(%T).Update: invalid triangles len", gt)) panic(fmt.Errorf("(%T).Update: invalid triangles len", gt))
} }
gt.updateData(t) gt.updateData(t)
gt.submitData()
// this code is supposed to copy the vertex data and CallNonBlock the update if
// the data is small enough, otherwise it'll block and not copy the data
if len(gt.data) < 256 { // arbitrary heurestic constant
data := append([]float32{}, gt.data...)
mainthread.CallNonBlock(func() {
gt.vs.Begin()
gt.vs.SetVertexData(data)
gt.vs.End()
})
} else {
mainthread.Call(func() {
gt.vs.Begin()
gt.vs.SetVertexData(gt.data)
gt.vs.End()
})
}
} }
// Copy returns an independent copy of this GLTriangles. // Copy returns an independent copy of this GLTriangles.