在金属中,一个顶点着色器是否在下一个顶点着色器执行之前完成?

In metal does one vertex shader complete before the next vertex shader executes?

假设金属顶点着色器 A 更新缓冲区 buf。还假设我有第二个顶点着色器 B,它在 A 之后编码。 B 可以使用 buf 中的结果,还是 B 会在 A 完成之前开始执行,这意味着缓冲区的内容还没有准备好?

如果第二个顶点着色器 B 在相同的 MTLRenderCommandEncoder 中编码,则它们可以在顶点着色器 A 之前自由执行。如果您想在 B 中读取 A 的输出,那么它们必须由单独的 MTLRenderCommandEncoder 编码。

但是请注意,不是 MTLComputeCommandEncoder 中的计算分派。 relevant part of the doc 状态:

Executing a Compute Command

To encode a command to execute a compute function, call the dispatchThreadgroups:threadsPerThreadgroup: method of MTLComputeCommandEncoder and specify the threadgroup dimensions and the number of threadgroups. You can query the threadExecutionWidth and maxTotalThreadsPerThreadgroup properties of MTLComputePipelineState to optimize the execution of the compute function on this device.

For most efficient execution of the compute function, set the total number of threads specified by the threadsPerThreadgroup argument to the dispatchThreadgroups:threadsPerThreadgroup: method to a multiple of threadExecutionWidth. The total number of threads in a threadgroup is the product of the components of threadsPerThreadgroup: threadsPerThreadgroup.width * threadsPerThreadgroup.height * threadsPerThreadgroup.depth. The maxTotalThreadsPerThreadgroup property specifies the maximum number of threads that can be in a single threadgroup to execute this compute function on the device.

Compute commands are executed in the order in which they are encoded into the command buffer. A compute command finishes execution when all threadgroups associated with the command finish execution and all results are written to memory. Because of this sequencing, the results of a compute command are available to any commands encoded after it in the command buffer.

To end encoding commands for a compute command encoder, call the endEncoding method of MTLComputeCommandEncoder. After ending the previous command encoder, you can create a new command encoder of any type to encode additional commands into the command buffer.