金属着色语言-更改缓冲区大小 [英] metal shading language - change buffer size

查看:118
本文介绍了金属着色语言-更改缓冲区大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在运行时更改缓冲区大小?我们在寄存器device:

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)

目前可以使用.但是,如果我有一个场景,即处于不同状态的粒子具有不同的计数,该怎么办?例如,一开始我有500个粒子,过了一会儿,粒子一次缓慢增加10,例如510、520、530,...

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, ...

我应该如何处理这种情况?每当粒子计数发生变化时,我应该重做queue吗?:

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

或者有更好的方法吗?

推荐答案

不,创建缓冲区后不能更改缓冲区的大小.

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.

或者,如果您确实希望随着粒子数量的增加而对其进行更大的分配,那么我不必每次都将其分配得足够大.相反,我会将当前所需的大小四舍五入为页面大小的倍数(4096字节).这样,您就有些懈怠.粒子数可以增加一段时间,而无需重新分配缓冲区.

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.

这篇关于金属着色语言-更改缓冲区大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆