简体   繁体   English

金属计算内核与片段着色器

[英]Metal Compute Kernel vs Fragment Shader

Metal supports kernel in addition to the standard vertex and fragment functions. 除标准vertexfragment函数外,Metal还支持kernel I found a metal kernel example that converts an image to grayscale. 我发现了一个将图像转换为灰度的金属kernel示例。

What exactly is the difference between doing this in a kernel vs fragment ? kernelfragment之间做这个有什么区别? What can a compute kernel do (better) that a fragment shader can't and vice versa? 计算kernel做什么(更好) fragment着色器不能,反之亦然?

Metal has four different types of command encoders: Metal有四种不同类型的命令编码器:

  • MTLRenderCommandEncoder
  • MTLComputeCommandEncoder
  • MTLBlitCommandEncoder
  • MTLParallelRenderCommandEncoder

If you're just doing graphics programming, you're most familiar with the MTLRenderCommandEncoder . 如果您只是在进行图形编程,那么您最熟悉的是MTLRenderCommandEncoder That is where you would set up your vertex and fragment shaders. 您可以在此处设置顶点和片段着色器。 This is optimized to deal with a lot of draw calls and object primitives. 这被优化以处理大量的绘制调用和对象原语。

The kernel shaders are primarily used for the MTLComputeCommandEncoder. 内核着色器主要用于MTLComputeCommandEncoder. I think the reason a kernel shader and a compute encoder were used for the image processing example is because you're not drawing any primitives as you would be with the render command encoder. 我认为内核着色器和计算编码器用于图像处理示例的原因是因为您没有像渲染命令编码器那样绘制任何基元。 Even though both methods are utilizing graphics, in this instance it's simply modifying color data on a texture rather than calculating depth of multiple objects on a screen. 尽管两种方法都使用图形,但在这种情况下,它只是修改纹理上的颜色数据,而不是计算屏幕上多个对象的深度。

The compute command encoder is also more easily set up to do parallel computing using threads: 计算命令编码器也更容易设置为使用线程进行并行计算:

https://developer.apple.com/reference/metal/mtlcomputecommandencoder https://developer.apple.com/reference/metal/mtlcomputecommandencoder

So if your application wanted to utilize multithreading on data modification, it's easier to do that in this command encoder than the render command encoder. 因此,如果您的应用程序想要在数据修改上使用多线程,那么在此命令编码器中执行此操作比使用render命令编码器更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM