是否可以在运行时更改缓冲区大小?我们在注册期间分配缓冲区大小device
:
device = MTLCreateSystemDefaultDevice()
queue = device!.makeCommandQueue()
do {
let library = device!.newDefaultLibrary()!
let kernel = library.makeFunction(name: "compute")!
cps = try device!.makeComputePipelineState(function: kernel)
} catch let e {
Swift.print("\(e)")
}
paramBuffer = device!.makeBuffer(length: MemoryLayout<Float>.size*2, options: [])
然后我们在运行时相应地更新它:
override public func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
if let drawable = currentDrawable {
let command_buffer = queue.makeCommandBuffer()
let command_encoder = command_buffer.makeComputeCommandEncoder()
command_encoder.setComputePipelineState(cps)
command_encoder.setTexture(drawable.texture, at: 0)
command_encoder.setBuffer(paramBuffer, offset: 0, at: 0)
目前有效。但是,如果我有一个场景,即具有不同状态的粒子有不同的计数。例如,一开始我有 500 个粒子,过了一段时间,粒子慢慢地一次增加 10 个,比如 510、520、530,...
我应该如何处理这种情况?我应该重做queue
每次粒子计数发生变化时?:
queue = device!.makeCommandQueue()
do {
let library = device!.newDefaultLibrary()!
let kernel = library.makeFunction(name: "compute")!
cps = try device!.makeComputePipelineState(function: kernel)
} catch let e {
Swift.print("\(e)")
}
paramBuffer = device!.makeBuffer(length: MemoryLayout<Float>.size*particleCount, options: [])//<--particleCount
或者有更好的方法来做到这一点?