所以我有这个代码用gpu的顶点数据构建数组。现在我可以拥有的粒子数量受到CPU的严重限制,特别是这个功能。
这个函数基本上覆盖了预先分配的数组中的值(方式更有效,然后我认为是附加的),基于"粒子"的数组中的值。对象。
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"似乎越来越快。他们有什么方法可以加速这段代码,还是现代swift的限制?
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;
}
}
只是因为(虽然我怀疑问题在这里)这里是数学课的相关摘录。
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)
}
}
答案 0 :(得分:0)
好吧,当我意识到这一点时,我正坐在一个讲座中(我完全注意了),为什么我在结构中保存所有内容,为什么不直接在gpu的数组中保持位置。
所以我摆脱了“Point”结构,并且所有内容都基于数组位置!现在瓶颈在于gpu!
在结构中存储值,然后将它们按组件逐个复制到数组中效率不高。