0
所以我有這個代碼構建與GPU的頂點數據陣列。現在我可以擁有的粒子數量受到CPU的嚴格限制,特別是這個功能。爲什麼這段代碼每秒運行1000次時運行速度很慢?
基於「粒子」對象數組中的值,此函數基本上覆蓋預分配數組中的值(更有效,然後附加我認爲的方式)。
circles = [GLfloat](count: ((MAX_PARTICLES) * 8) * 2, repeatedValue: 0)
squares = [GLfloat](count: ((MAX_PARTICLES) * (6 * 4)), repeatedValue: 0)
indices = [GLushort](count: ((MAX_PARTICLES) * 6), repeatedValue: 0)
令人驚訝的是不是obj.update(1/60.0)調用,是造成這種延遲,以及「calcRectangle」似乎是儘可能快地去。他們有什麼方法來加速這個代碼,或者這是現代迅速的限制嗎?
private func _buildArrays1()
{
var circlePos:Int = 0
var rectPos:Int = 0
var buffer_index:Int = 0
var accum:Int = 0
for obj in particles
{
obj.update(1.0/60.0)
let pos:Point = obj.position
let lpos:Point = obj.lastPosition
var color:ColorHSV = obj.color
let width:GLfloat = obj.size
let rect = Math.makeRectangle(pos, p2: lpos, w: width)
color = ColorHSV(h: 1.0, s: 0.0, v: 0.0, a: 1.0)
circles[circlePos] = pos.x
circles[circlePos + 1] = pos.y
circles[circlePos + 2] = 0.0
circles[circlePos + 3] = color.h
circles[circlePos + 4] = color.s
circles[circlePos + 5] = color.v
circles[circlePos + 6] = color.a
circles[circlePos + 7] = width
circles[circlePos + 8] = lpos.x
circles[circlePos + 9] = lpos.y
circles[circlePos + 10] = 0.0
circles[circlePos + 11] = color.h
circles[circlePos + 12] = color.s
circles[circlePos + 13] = color.v
circles[circlePos + 14] = color.a
circles[circlePos + 15] = width
circlePos += 16
color = ColorHSV(h: 1.0, s: 1.0, v: 0.0, a: 1.0)
squares[rectPos] = rect.0.x; rectPos += 1;
squares[rectPos] = rect.0.y; rectPos += 1;
squares[rectPos] = color.h; rectPos += 1;
squares[rectPos] = color.s; rectPos += 1;
squares[rectPos] = color.v; rectPos += 1;
squares[rectPos] = color.a; rectPos += 1;
squares[rectPos] = rect.1.x; rectPos += 1;
squares[rectPos] = rect.1.y; rectPos += 1;
squares[rectPos] = color.h; rectPos += 1;
squares[rectPos] = color.s; rectPos += 1;
squares[rectPos] = color.v; rectPos += 1;
squares[rectPos] = color.a; rectPos += 1;
squares[rectPos] = rect.2.x; rectPos += 1;
squares[rectPos] = rect.2.y; rectPos += 1;
squares[rectPos] = color.h; rectPos += 1;
squares[rectPos] = color.s; rectPos += 1;
squares[rectPos] = color.v; rectPos += 1;
squares[rectPos] = color.a; rectPos += 1;
squares[rectPos] = rect.3.x; rectPos += 1;
squares[rectPos] = rect.3.y; rectPos += 1;
squares[rectPos] = color.h; rectPos += 1;
squares[rectPos] = color.s; rectPos += 1;
squares[rectPos] = color.v; rectPos += 1;
squares[rectPos] = color.a; rectPos += 1;
indices[buffer_index] = GLushort(accum); buffer_index += 1;
indices[buffer_index] = GLushort(accum + 1); buffer_index += 1;
indices[buffer_index] = GLushort(accum + 2); buffer_index += 1;
indices[buffer_index] = GLushort(accum + 3); buffer_index += 1;
indices[buffer_index] = GLushort(accum + 2); buffer_index += 1;
indices[buffer_index] = GLushort(accum + 1); buffer_index += 1;
accum += 4;
}
}
只是incase(雖然我懷疑這個問題在這裏),這裏是從數學課的相關摘錄。
class Math {
static func makeRectangle(p1: Point, p2: Point, w: GLfloat) -> (Point, Point, Point, Point)
{
//Returns the vertices of a rectangle made with the two point
if ((p2.x < p1.x) && (p2.y < p1.y))
{
//POINTs are not in the right order
return makeRectangle(p2, p2: p1, w: w)
}
let vector = subtract(p2, p1)
let unit = devide(vector, length(vector))
let perp = Point(x: -unit.y, y: unit.x)
let calc = multiply(perp, w/2)
return (add(p1, calc), subtract(p1, calc), add(p2, calc), r: subtract(p2, calc))
}
static func subtract(l:Point, _ r:Point) -> Point
{
return Point(x: l.x - r.x, y: l.y - r.y)
}
static func add(l:Point, _ r:Point) -> Point
{
return Point(x: r.x + l.x, y: r.y + l.y)
}
static func devide(l:Point, _ r:GLfloat) -> Point
{
return Point(x: l.x/r, y: l.y/r)
}
static func length(o:Point) -> GLfloat
{
return sqrt(o.x * o.x + o.y * o.y)
}
static func multiply(o:Point, _ v:GLfloat) -> Point
{
return Point(x: o.x * v, y: o.y * v)
}
}
你有多少'粒子'?每秒運行整個代碼1000次是很常見的 - 而且您正在做很多工作。粒子系統是一項相當艱鉅的任務。 – luk2302