Metal Render Pipeline

Elements to know:

  • MTLDevice: The software reference to the GPU hardware device

  • MTLCommandQueue: Responsible for creating and organizing MTLCommandBuffers for each frame

  • MTLLibrary: Contains the source code from your vertex and fragment shader functions

  • MTLRenderPipelineState: Sets the information for the draw, such as which shader functions to use, what depth and color settings to use and how to read the vertex data

  • MTLBuffer: Holds data such as vertext information in a form that you can send to the GPU

####(Typically) only one per app instance: - MTLDevice - MTLCommandQueue - MTLLibrary

Typically on iOS there is only one available GPU. When creating a new MTLDevice via MTLCreateSystemDefaultDevice() you are returning the defaults. However, there is a possiblity that your hardware host device will have multiple (either future iOS hardware, or MacOS), in which case you would want to get a list of available GPU by quering MTLCopyAllDevices().

MTLCommandQueues hold MTLCommandBuffers, this allows the CPU to line up instructions (buffers) into the command queue which hold it in sequential order.

MTLLibrary is the result of compiled .metal files. These .metal files are writen in Metal Shading Language (MSL) which is C-ish looking. Part of the reason Metal is performant is because this compiling is done during app building or initialization of the app.

####Things that are created frequently: - MTLRenderPipelineState - MTLBuffer

MTLRenderPipelineStates are created on a semi-frequently basis, and in some case you create multiple. You may create one for depthRenderPipelineState where you describe the information to draw, shader functions, settings (depth and color) and how to read vertex data.

MTLBuffers are used to hold the specific vertex data. This information is then queued into the MTLCommandQueue.

Metal/MetalKit Glossary

As someone that has never built using OpenGL or other rendering languages I found that many metal tutorials, examples, and books do not define in one place what means what. So, this is a glossary of Metal/MetalKit terms as I learn them.

SIMD: Single Instruction Multiple Data

Rendering: The processing of an outline image using color and shading to make it appear solid and three-dimensional

Vertex: refers to a point in three dimensional space where two or more lines, curves or edges of a geometrical shape meet, such as the corners of a cube.

Rendering pipeline: parses list of vertices → GPU → shader functions process vertices → final image/texture → CPU → Screen

Shape Primitive: cube, sphere, cylinder or torus

MTKMeshBufferAllocator: manages the memory of the mesh

Render Command Encoder: Wrapping of commands to be sent to the GPU

Pipeline State: Tell the GPU that nothing will change until the state changes. Pipeline state contains pixel format, if should render with depth, vertex and fragment functions. Pipeline states are set by pipeline descriptors.

BGRA: Blue, Green, Red, Alpha

Command Buffer: Stores all commmands you ask the GPU to run

Render Pass Descriptor: Holds data (attachments) for the render destinations

Render Command Encoder: Holds all information necessary to send to the GPU so it can draw vertices