metal shading language - change buffer size

99封情书 提交于 2019-12-11 04:19:36

问题


Is it possible to change the buffer size at runtime? We allocate the buffer size during the register our 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: [])

then we update it accordingly at run time:

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)

It works for now. But what if I have a scenario that, Have particles, at different state have different counts. For example, at the beginning I have 500 particles, After a while, the particles slowly increase by 10 at a time, say 510, 520, 530, ...

How should I approach this scenario? should I redo the queue every time there is a changes on the particle counts?:

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

Or any better way to do this?


回答1:


No, you can't change the size of a buffer after it's been created.

Why would you think you need to get a new command queue each time? The command queue is not related to the buffer size.

The only thing you would have to recreate is the buffer itself.

If there is an upper bound on the number of particles, though, you can simply create the buffer at that maximum size from the start. There's no requirement that the buffer be exactly as big as currently needed. It can be bigger than needed even if some portion of it is temporarily wasted.

Alternatively, if you do want to reallocate it bigger as the number of particles increases, I would not necessarily reallocate it to just big enough every time. Instead, I would round the currently required size to a multiple of the page size (4096 bytes). That way, you have some slack. The particle count can increase for a while without requiring reallocation of the buffer.



来源:https://stackoverflow.com/questions/43571708/metal-shading-language-change-buffer-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!