Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Metal Compute Kernel vs Fragment Shader

Tags:

ios

metal

Metal supports kernel in addition to the standard vertex and fragment functions. I found a metal kernel example that converts an image to grayscale.

What exactly is the difference between doing this in a kernel vs fragment? What can a compute kernel do (better) that a fragment shader can't and vice versa?

like image 799
Era Avatar asked Dec 07 '16 10:12

Era


1 Answers

Metal has four different types of command encoders:

  • MTLRenderCommandEncoder
  • MTLComputeCommandEncoder
  • MTLBlitCommandEncoder
  • MTLParallelRenderCommandEncoder

If you're just doing graphics programming, you're most familiar with the 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. 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

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.

like image 85
Janie Larson Avatar answered Sep 27 '22 19:09

Janie Larson