Hi there again!
When I initialise the app I create a buffer with the length to fit 25K Triangles. After updating the triangles accessing directly to the buffer (binding memory) I need to render the buffer:
1.- My first approach is to extract the vertices and do only one non-indexed draw call:
private func extractVertices()->[float3]{
var vertices:[float3]=[]
for i in 0..<Emitter.trianglesCount {
if Emitter.trianglesPointer?.advanced(by: i).pointee.active == 1{
vertices.append(Emitter.trianglesPointer!.advanced(by: i).pointee.vertex0)
vertices.append(Emitter.trianglesPointer!.advanced(by: i).pointee.vertex1)
vertices.append(Emitter.trianglesPointer!.advanced(by: i).pointee.vertex2)
}
}
return vertices
}
but extractVertices()
becomes a bottleneck
2.- The second approach is send the buffer with the triangles directly and do an indexed call per triangle.
for i in 0..<Emitter.trianglesCount {
if Emitter.triangles.contents().bindMemory(to: Triangle.self, capacity: Emitter.trianglesCount).advanced(by: i).pointee.active == 1 {
renderEncoder.setVertexBuffer(Emitter.triangles, offset: MemoryLayout<Triangle>.stride*i, index: 0)
renderEncoder.drawIndexedPrimitives(type: .triangle,
indexCount: 3,
indexType: .uint16,
indexBuffer: emitter.triangleIndiciesBuffer,
indexBufferOffset: 0)
}
}
But there are a 25K drawcalls… so another bottleneck!!!
There is a way to render the buffer with the 25K triangles with only one drawcall ?!?
Triangle
looks like :
typedef struct {
simd_float3 vertex0;
simd_float3 vertex1;
simd_float3 vertex2;
simd_float3 direction;
float velocity;
int age;
int mainAge;
int subdivisionLevel;
TriangleType type;
TriangleMainState mainSubdState;
float distanceToCam;
int active ;
}Triangle;
Thanks in advance!!!