Use of GPU in parallel with CPU

Hi

I am trying to understand the parallel processing nature of using metal and if I am taking advantage of it fully enough (at all ?).

I have a draw function which I have set up to make a set of calculation on the CPU, then issue a drawprimative call. It carries this out 3 times for different meshes I am rendering (CPU calculations, draw primitive call, CPU calculations, draw primitive call etc). Finally I issue the end-encoding, present and commit statements.

Am I actually taking advantage at all of using the GPU in parallel with the CPU with this configuration ? If not, can you please point me towards an article which could help me.

Thanks

Hi @imcloughlin and welcome to the forums :smiley:

How far are you into the book? Chapter 3, The Rendering Pipeline might help.

In broad strokes, this is the way your frame will render:

// your code
create command buffer
create render encoder
renderEncoder.setPipelineState(...)
loop 3 times {
  calculate(...)
  renderEncoder.draw(...)
}
renderEncoder.endEncoding()
commandBuffer.present(...)
commandBuffer.commit()

Nothing happens on the GPU until the command buffer presents the view’s drawable texture and commits it to the GPU.

When the code says renderEncoder.draw, you are merely adding that command to a list of GPU commands in the encoder that will be executed when the encoder is committed to the GPU.

Metal is an API that allows you to perform commands on the GPU. It is not by itself inherently parallel.

The GPU is parallel because within one draw it might be processing many vertices all at one time in multiple threads. Chapter 3 explains how shader cores that are idle are immediately set to work. They do both vertices in parallel and fragments in parallel.

If you understand all that, there are many Apple Metal Optimizing WWDC videos that take you through Instruments so that you can tweak your performance.

But beware of spending time optimizing when your situation doesn’t need it.

1 Like

Thanks Caroline - is there a way to monitor the cpu / gpu usage during a draw cycle to investigate the result of any optimizations (I’m writing a vector maths intensive 3D procedurally generated game)

That sounds like fun!

In Xcode, there’s the Debug Navigator (cmd 7), and the GPU Debugger (the camera icon) where you can examine a frame. But you should get familiar with Instruments, where you can do any amount of profiling.

There’s WWDC19 Getting Started with Instruments and if you put Metal into Apple’s Developer app search, then you’ll find all sorts of optimization videos.

Hey thanks for sign-posting this for me :slight_smile:

1 Like