在Metal中,将顶点和片段缓冲区设置为同一MTLBuffer是否仅将其复制到GPU一次? [英] In Metal, does setting vertex and fragment buffer to same MTLBuffer copies it to GPU only once?

查看:83
本文介绍了在Metal中,将顶点和片段缓冲区设置为同一MTLBuffer是否仅将其复制到GPU一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将统一缓冲区传递给顶点着色器和片段着色器.

I am passing a uniform buffer to both vertex and fragment shaders.

let uniformBuffer = device.makeBuffer(length: 4096, options: [])
renderEncoder.setVertexBuffer(uniformBuffer, offset: 0, at: 1)
renderEncoder.setFragmentBuffer(uniformBuffer,offset:0, at: 1)

此操作是否将uniformBuffer从CPU复制到GPU两次?然后,我将缓冲区从顶点着色器传递到片段着色器,该着色器仅发生在GPU内部.

Does this copy the uniformBuffer from CPU to GPU twice? Then I will instead pass the buffer from vertex shader to fragment shader which happens just inside the GPU.

推荐答案

没有专门与setVertexBuffer()setFragmentBuffer()有关的副本.那些仅将对缓冲区的 reference 放入渲染编码器的缓冲区表中.

There are no copies involved specifically with setVertexBuffer() or setFragmentBuffer(). Those only put a reference to the buffer into the render encoder's buffer tables.

您的代码和Metal只需确保缓冲区的内容在GPU上是最新的,并将从那里开始对其进行引用.正如Matthjis所述,如何执行此操作取决于storageMode.如果使用的是托管模式,则如果修改CPU上的缓冲区内容,则需要使用didModifyRange()告知Metal.完成此操作后,Metal便知道下次将修改后的范围复制到GPU上.不管编码器缓冲区表引用了多少次缓冲区,单个副本就足够了.

Your code and Metal need only ensure that the contents of the buffer are up-to-date on the GPU and from there it will be referenced. As Matthjis mentioned, how you do this depends on the storageMode. If you're using managed mode, if you modify the buffer contents on the CPU, you need to tell Metal about that using didModifyRange(). Once you do that, Metal knows to copy the modified range to the GPU the next time it's needed there. That single copy is sufficient, regardless of how many times the buffer is referenced by encoder buffer tables.

请注意,setVertexBytes()setFragmentBytes()的工作原理不同.每次调用它们时,都必须(并且确实)立即复制传入的字节数组.

Note that setVertexBytes() and setFragmentBytes() work differently. Those have to (and do) make immediate copies of the passed-in byte array, each time they are called.

这篇关于在Metal中,将顶点和片段缓冲区设置为同一MTLBuffer是否仅将其复制到GPU一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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