Contents

MTLRenderCommandEncoder

Encodes configuration and draw commands for a single render pass into a command buffer.

Declaration

protocol MTLRenderCommandEncoder : MTLCommandEncoder

Mentioned in

Overview

A render pass draws a scene, or a component within a scene, to its render attachments, the outputs of a render pass. You can render to those outputs with various approaches, including techniques that apply the following:

  • Primitive drawing

  • Mesh drawing

  • Ray tracing

  • Dispatching tile shaders

To create an MTLRenderCommandEncoder instance, call the makeRenderCommandEncoder(descriptor:) method of an MTLCommandBuffer instance, or the makeRenderCommandEncoder() method of an MTLParallelRenderCommandEncoder instance.

To configure the render pass for your first drawing commands, start with a pipeline state by passing an MTLRenderPipelineState instance to the encoder’s setRenderPipelineState(_:) method. You create the pipeline states your render pass needs, typically ahead of time, by calling one or more MTLDevice methods (see Pipeline state creation).

Configure other encoder settings by calling the methods on the Render pass configuration page. For example, you may need to configure the pass’s viewport, its scissor rectangle, and the settings for depth and stencil tests.

Assign resources, such as buffers and textures, for the shaders that depend on them. For more information, see the shader-specific pages in the resource preparation section, such as Vertex shader resource preparation commands and Fragment shader resource preparation commands. If your shaders access resources through an argument buffer, make those resources resident in GPU memory by calling the methods on the Argument buffer resource preparation commands page.

Encode drawing commands after you configure the state and resources the commands depend on. The encoder maintains its current state and applies it to all subsequent draw commands. For drawing commands that need different states or resources, reconfigure the render pass appropriately and then encode those draw commands. Repeat the process for each batch of drawing commands that depend on the same render pass configuration and resources.

When you finish encoding the render pass’s commands, finalize it into the command buffer by calling the encoder’s endEncoding() method.

Command stages

Most render commands apply to one or more stages within a pass. The following table shows which stages apply to each command:

Function

MTLStages

Drawprimitives(type:vertexstart:vertexcount:)

Vertex[Image]Fragment

Drawprimitives(type:vertexstart:vertexcount:instancecount:)

Vertex[Image]Fragment

Drawprimitives(type:vertexstart:vertexcount:instancecount:baseinstance:)

Vertex[Image]Fragment

Drawprimitives(type:indirectbuffer:indirectbufferoffset:)

Vertex[Image]Fragment

Drawindexedprimitives(type:indexcount:indextype:indexbuffer:indexbufferoffset:)

Vertex[Image]Fragment

Drawindexedprimitives(type:indexcount:indextype:indexbuffer:indexbufferoffset:instancecount:)

Vertex[Image]Fragment

Drawindexedprimitives(type:indexcount:indextype:indexbuffer:indexbufferoffset:instancecount:basevertex:baseinstance:)

Vertex[Image]Fragment

Drawindexedprimitives(type:indextype:indexbuffer:indexbufferoffset:indirectbuffer:indirectbufferoffset:)

Vertex[Image]Fragment

Drawmeshthreads(_:threadsperobjectthreadgroup:threadspermeshthreadgroup:)

Object[Image]Mesh[Image]Fragment

Drawmeshthreadgroups(_:threadsperobjectthreadgroup:threadspermeshthreadgroup:)

Object[Image]Mesh[Image]Fragment

Drawmeshthreadgroups(indirectbuffer:indirectbufferoffset:threadsperobjectthreadgroup:threadspermeshthreadgroup:)

Object[Image]Mesh[Image]Fragment

Drawpatches(numberofpatchcontrolpoints:patchstart:patchcount:patchindexbuffer:patchindexbufferoffset:instancecount:baseinstance:)

Vertex[Image]Fragment

Drawpatches(numberofpatchcontrolpoints:patchindexbuffer:patchindexbufferoffset:indirectbuffer:indirectbufferoffset:)

Vertex[Image]Fragment

Drawindexedpatches(numberofpatchcontrolpoints:patchstart:patchcount:patchindexbuffer:patchindexbufferoffset:controlpointindexbuffer:controlpointindexbufferoffset:instancecount:baseinstance:)

Vertex[Image]Fragment

Drawindexedpatches(numberofpatchcontrolpoints:patchindexbuffer:patchindexbufferoffset:controlpointindexbuffer:controlpointindexbufferoffset:indirectbuffer:indirectbufferoffset:)

Vertex[Image]Fragment

Dispatchthreadspertile(_:)

Tile

Executecommandsinbuffer(_:range:)[Image]Executecommandsinbuffer:withrange:

None

Executecommandsinbuffer(_:indirectbuffer:offset:)[Image]Executecommandsinbuffer:indirectbuffer:indirectbufferoffset:

None

Samplecounters(samplebuffer:sampleindex:barrier:)

None

Draw commands don’t apply to fragment when the MTLRenderPipelineState for the draw disables rasterization. See isRasterizationEnabled.

Mesh draw commands don’t apply to object when the MTLRenderPipelineState for the draw doesn’t have an object shader.

The executeCommandsInBuffer(_:range:) and executeCommandsInBuffer(_:indirectBuffer:offset:) commands don’t apply to any stage, which means you can’t use a barrier to wait for all commands in an indirect command buffer to complete. However, each command within the MTLIndirectCommandBuffer applies to the same stages as when you encode the equivalent command directly.

For more information about stages and synchronization, see MTLStages and Resource synchronization.

Topics

Configuration commands

Resource preparation commands

Drawing with vertices

Drawing with indexed vertices

Drawing with meshes

Drawing with tessellation patches

Drawing with indexed tessellation patches

Drawing with tile shaders

Preventing resource access conflicts

Running commands from indirect command buffers

Sampling counters

Deprecated

See Also

Encoding a render pass